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