macos.cmake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. enable_language(C)
  2. set(testlib_names
  3. preexcluded
  4. executable_path
  5. executable_path_bundle
  6. executable_path_postexcluded
  7. loader_path
  8. loader_path_unresolved
  9. loader_path_postexcluded
  10. rpath
  11. rpath_unresolved
  12. rpath_postexcluded
  13. rpath_executable_path
  14. rpath_executable_path_bundle
  15. rpath_executable_path_postexcluded
  16. rpath_loader_path
  17. rpath_loader_path_unresolved
  18. rpath_loader_path_postexcluded
  19. normal
  20. normal_unresolved
  21. normal_postexcluded
  22. conflict
  23. )
  24. file(REMOVE "${CMAKE_BINARY_DIR}/testlib.c")
  25. add_library(testlib SHARED "${CMAKE_BINARY_DIR}/testlib.c")
  26. foreach(name ${testlib_names})
  27. if(name STREQUAL "normal")
  28. file(WRITE "${CMAKE_BINARY_DIR}/normal.c" "extern void rpath(void);\nvoid normal(void)\n{\n rpath();\n}\n")
  29. else()
  30. file(WRITE "${CMAKE_BINARY_DIR}/${name}.c" "void ${name}(void) {}\n")
  31. endif()
  32. add_library(${name} SHARED "${CMAKE_BINARY_DIR}/${name}.c")
  33. file(APPEND "${CMAKE_BINARY_DIR}/testlib.c" "extern void ${name}(void);\n")
  34. endforeach()
  35. file(APPEND "${CMAKE_BINARY_DIR}/testlib.c" "void testlib(void)\n{\n")
  36. foreach(name ${testlib_names})
  37. file(APPEND "${CMAKE_BINARY_DIR}/testlib.c" " ${name}();\n")
  38. endforeach()
  39. file(APPEND "${CMAKE_BINARY_DIR}/testlib.c" "}\n")
  40. set_property(TARGET ${testlib_names} PROPERTY BUILD_WITH_INSTALL_NAME_DIR 1)
  41. target_link_libraries(normal PRIVATE rpath)
  42. set_property(TARGET normal PROPERTY INSTALL_RPATH
  43. "${CMAKE_BINARY_DIR}/root-all/executable/lib/normal/../rpath"
  44. )
  45. file(WRITE "${CMAKE_BINARY_DIR}/testlib_conflict.c" "extern void conflict(void);\nvoid testlib_conflict(void)\n{\n conflict();\n}\n")
  46. add_library(testlib_conflict SHARED "${CMAKE_BINARY_DIR}/testlib_conflict.c")
  47. target_link_libraries(testlib_conflict PRIVATE conflict)
  48. set_property(TARGET testlib PROPERTY INSTALL_RPATH
  49. "${CMAKE_BINARY_DIR}/root-all/executable/lib/rpath"
  50. "${CMAKE_BINARY_DIR}/root-all/executable/lib/rpath_unresolved"
  51. "${CMAKE_BINARY_DIR}/root-all/executable/lib/rpath_postexcluded"
  52. "${CMAKE_BINARY_DIR}/root-all/executable/lib/conflict"
  53. @executable_path/../lib/rpath_executable_path
  54. @executable_path/../lib/rpath_executable_path_unresolved
  55. @executable_path/../lib/rpath_executable_path_postexcluded
  56. @loader_path/rpath_loader_path
  57. @loader_path/rpath_loader_path_unresolved
  58. @loader_path/rpath_loader_path_postexcluded
  59. )
  60. set_property(TARGET testlib_conflict PROPERTY INSTALL_RPATH
  61. "${CMAKE_BINARY_DIR}/root-all/executable/lib/conflict2"
  62. )
  63. foreach(t
  64. executable_path
  65. executable_path_postexcluded
  66. loader_path
  67. loader_path_postexcluded
  68. rpath
  69. rpath_postexcluded
  70. rpath_executable_path
  71. rpath_executable_path_postexcluded
  72. rpath_loader_path
  73. rpath_loader_path_postexcluded
  74. conflict
  75. )
  76. install(TARGETS ${t} DESTINATION executable/lib/${t})
  77. endforeach()
  78. install(TARGETS conflict DESTINATION executable/lib/conflict2)
  79. foreach(t
  80. executable_path_bundle
  81. executable_path_postexcluded
  82. loader_path_postexcluded
  83. rpath_postexcluded
  84. rpath_executable_path_bundle
  85. rpath_executable_path_postexcluded
  86. rpath_loader_path_postexcluded
  87. )
  88. install(TARGETS ${t} DESTINATION bundle_executable/lib/${t})
  89. endforeach()
  90. foreach(t executable_path executable_path_bundle executable_path_postexcluded)
  91. set_property(TARGET ${t} PROPERTY INSTALL_NAME_DIR @executable_path/../lib/${t})
  92. endforeach()
  93. foreach(t loader_path loader_path_unresolved loader_path_postexcluded)
  94. set_property(TARGET ${t} PROPERTY INSTALL_NAME_DIR @loader_path/${t})
  95. endforeach()
  96. foreach(t
  97. rpath
  98. rpath_unresolved
  99. rpath_postexcluded
  100. rpath_executable_path
  101. rpath_executable_path_bundle
  102. rpath_executable_path_postexcluded
  103. rpath_loader_path
  104. rpath_loader_path_unresolved
  105. rpath_loader_path_postexcluded
  106. conflict
  107. )
  108. set_property(TARGET ${t} PROPERTY INSTALL_NAME_DIR @rpath)
  109. endforeach()
  110. foreach(t normal normal_unresolved normal_postexcluded)
  111. set_property(TARGET ${t} PROPERTY INSTALL_NAME_DIR "${CMAKE_BINARY_DIR}/root-all/executable/lib/${t}")
  112. if(NOT t STREQUAL "normal_unresolved")
  113. install(TARGETS ${t} DESTINATION executable/lib/${t})
  114. endif()
  115. endforeach()
  116. target_link_libraries(testlib PRIVATE ${testlib_names})
  117. add_executable(topexe macos/topexe.c)
  118. add_executable(topexe_weak macos/topexe.c)
  119. add_library(toplib SHARED macos/toplib.c)
  120. add_library(topmod MODULE macos/toplib.c)
  121. target_link_libraries(topexe PRIVATE testlib)
  122. target_link_libraries(topexe_weak PRIVATE "-weak_library" testlib)
  123. target_link_libraries(toplib PRIVATE testlib)
  124. target_link_libraries(topmod PRIVATE testlib)
  125. set_property(TARGET topexe toplib topmod PROPERTY INSTALL_RPATH "${CMAKE_BINARY_DIR}/root-all/executable/lib")
  126. set_property(TARGET topexe_weak toplib topmod PROPERTY INSTALL_RPATH "${CMAKE_BINARY_DIR}/root-all/executable/lib")
  127. install(TARGETS topexe topexe_weak toplib topmod testlib testlib_conflict RUNTIME DESTINATION executable/bin LIBRARY DESTINATION executable/lib)
  128. install(TARGETS topexe topexe_weak toplib topmod testlib testlib_conflict RUNTIME DESTINATION bundle_executable/bin LIBRARY DESTINATION bundle_executable/lib)
  129. install(CODE [[
  130. function(exec_get_runtime_dependencies depsfile udepsfile cdepsfile)
  131. file(GET_RUNTIME_DEPENDENCIES
  132. RESOLVED_DEPENDENCIES_VAR deps
  133. UNRESOLVED_DEPENDENCIES_VAR udeps
  134. CONFLICTING_DEPENDENCIES_PREFIX cdeps
  135. PRE_INCLUDE_REGEXES "^.*/lib(testlib|executable_path|executable_path_bundle|executable_path_postexcluded|loader_path|loader_path_unresolved|loader_path_postexcluded|rpath|rpath_unresolved|rpath_postexcluded|rpath_executable_path|rpath_executable_path_bundle|rpath_executable_path_postexcluded|rpath_loader_path|rpath_loader_path_unresolved|rpath_loader_path_postexcluded|normal|normal_unresolved|normal_postexcluded|conflict|System\\.B)\\.dylib$"
  136. PRE_EXCLUDE_REGEXES ".*"
  137. POST_INCLUDE_REGEXES "^.*/lib(testlib|executable_path|executable_path_bundle|loader_path|rpath|rpath_executable_path|rpath_executable_path_bundle|rpath_loader_path|normal|conflict|System\\.B)\\.dylib$"
  138. POST_EXCLUDE_REGEXES ".*"
  139. ${ARGN}
  140. )
  141. list(SORT deps)
  142. list(SORT udeps)
  143. list(SORT cdeps_FILENAMES)
  144. file(WRITE "${CMAKE_INSTALL_PREFIX}/deps/${depsfile}" "${deps}")
  145. file(WRITE "${CMAKE_INSTALL_PREFIX}/deps/${udepsfile}" "${udeps}")
  146. file(WRITE "${CMAKE_INSTALL_PREFIX}/deps/${cdepsfile}" "")
  147. foreach(cdep IN LISTS cdeps_FILENAMES)
  148. set(cdep_values ${cdeps_${cdep}})
  149. list(SORT cdep_values)
  150. file(APPEND "${CMAKE_INSTALL_PREFIX}/deps/${cdepsfile}" "${cdep}:${cdep_values}\n")
  151. endforeach()
  152. endfunction()
  153. exec_get_runtime_dependencies(
  154. deps1.txt udeps1.txt cdeps1.txt
  155. EXECUTABLES
  156. "${CMAKE_INSTALL_PREFIX}/executable/bin/$<TARGET_FILE_NAME:topexe>"
  157. LIBRARIES
  158. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
  159. )
  160. exec_get_runtime_dependencies(
  161. deps2.txt udeps2.txt cdeps2.txt
  162. LIBRARIES
  163. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:toplib>"
  164. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
  165. )
  166. exec_get_runtime_dependencies(
  167. deps3.txt udeps3.txt cdeps3.txt
  168. MODULES
  169. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:topmod>"
  170. LIBRARIES
  171. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
  172. )
  173. exec_get_runtime_dependencies(
  174. deps4.txt udeps4.txt cdeps4.txt
  175. EXECUTABLES
  176. "${CMAKE_INSTALL_PREFIX}/executable/bin/$<TARGET_FILE_NAME:topexe>"
  177. LIBRARIES
  178. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
  179. BUNDLE_EXECUTABLE
  180. "${CMAKE_INSTALL_PREFIX}/bundle_executable/bin/$<TARGET_FILE_NAME:topexe>"
  181. )
  182. exec_get_runtime_dependencies(
  183. deps5.txt udeps5.txt cdeps5.txt
  184. LIBRARIES
  185. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:toplib>"
  186. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
  187. BUNDLE_EXECUTABLE "${CMAKE_INSTALL_PREFIX}/bundle_executable/bin/$<TARGET_FILE_NAME:topexe>"
  188. )
  189. exec_get_runtime_dependencies(
  190. deps6.txt udeps6.txt cdeps6.txt
  191. MODULES
  192. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:topmod>"
  193. LIBRARIES
  194. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
  195. BUNDLE_EXECUTABLE "${CMAKE_INSTALL_PREFIX}/bundle_executable/bin/$<TARGET_FILE_NAME:topexe>"
  196. )
  197. exec_get_runtime_dependencies(
  198. deps7.txt udeps7.txt cdeps7.txt
  199. EXECUTABLES
  200. "${CMAKE_INSTALL_PREFIX}/executable/bin/$<TARGET_FILE_NAME:topexe_weak>"
  201. LIBRARIES
  202. "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
  203. )
  204. ]])