c56a14b6cada9ed6a3a7de275d942e256a18db32
[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_library(toplib SHARED macos/toplib.c)
132 add_library(topmod MODULE macos/toplib.c)
133 target_link_libraries(topexe PRIVATE testlib)
134 target_link_libraries(toplib PRIVATE testlib)
135 target_link_libraries(topmod PRIVATE testlib)
136
137 set_property(TARGET topexe toplib topmod PROPERTY INSTALL_RPATH "${CMAKE_BINARY_DIR}/root-all/executable/lib")
138
139 install(TARGETS topexe toplib topmod testlib testlib_conflict RUNTIME DESTINATION executable/bin LIBRARY DESTINATION executable/lib)
140 install(TARGETS topexe toplib topmod testlib testlib_conflict RUNTIME DESTINATION bundle_executable/bin LIBRARY DESTINATION bundle_executable/lib)
141
142 install(CODE [[
143   function(exec_get_runtime_dependencies depsfile udepsfile cdepsfile)
144     file(GET_RUNTIME_DEPENDENCIES
145       RESOLVED_DEPENDENCIES_VAR deps
146       UNRESOLVED_DEPENDENCIES_VAR udeps
147       CONFLICTING_DEPENDENCIES_PREFIX cdeps
148       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$"
149       PRE_EXCLUDE_REGEXES ".*"
150       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$"
151       POST_EXCLUDE_REGEXES ".*"
152       ${ARGN}
153       )
154     list(SORT deps)
155     list(SORT udeps)
156     list(SORT cdeps_FILENAMES)
157     file(WRITE "${CMAKE_INSTALL_PREFIX}/deps/${depsfile}" "${deps}")
158     file(WRITE "${CMAKE_INSTALL_PREFIX}/deps/${udepsfile}" "${udeps}")
159     file(WRITE "${CMAKE_INSTALL_PREFIX}/deps/${cdepsfile}" "")
160     foreach(cdep IN LISTS cdeps_FILENAMES)
161       set(cdep_values ${cdeps_${cdep}})
162       list(SORT cdep_values)
163       file(APPEND "${CMAKE_INSTALL_PREFIX}/deps/${cdepsfile}" "${cdep}:${cdep_values}\n")
164     endforeach()
165   endfunction()
166
167   exec_get_runtime_dependencies(
168     deps1.txt udeps1.txt cdeps1.txt
169     EXECUTABLES
170       "${CMAKE_INSTALL_PREFIX}/executable/bin/$<TARGET_FILE_NAME:topexe>"
171     LIBRARIES
172       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
173     )
174
175   exec_get_runtime_dependencies(
176     deps2.txt udeps2.txt cdeps2.txt
177     LIBRARIES
178       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:toplib>"
179       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
180     )
181
182   exec_get_runtime_dependencies(
183     deps3.txt udeps3.txt cdeps3.txt
184     MODULES
185       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:topmod>"
186     LIBRARIES
187       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
188     )
189
190   exec_get_runtime_dependencies(
191     deps4.txt udeps4.txt cdeps4.txt
192     EXECUTABLES
193       "${CMAKE_INSTALL_PREFIX}/executable/bin/$<TARGET_FILE_NAME:topexe>"
194     LIBRARIES
195       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
196     BUNDLE_EXECUTABLE
197       "${CMAKE_INSTALL_PREFIX}/bundle_executable/bin/$<TARGET_FILE_NAME:topexe>"
198     )
199
200   exec_get_runtime_dependencies(
201     deps5.txt udeps5.txt cdeps5.txt
202     LIBRARIES
203       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:toplib>"
204       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
205     BUNDLE_EXECUTABLE "${CMAKE_INSTALL_PREFIX}/bundle_executable/bin/$<TARGET_FILE_NAME:topexe>"
206     )
207
208   exec_get_runtime_dependencies(
209     deps6.txt udeps6.txt cdeps6.txt
210     MODULES
211       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:topmod>"
212     LIBRARIES
213       "${CMAKE_INSTALL_PREFIX}/executable/lib/$<TARGET_FILE_NAME:testlib_conflict>"
214     BUNDLE_EXECUTABLE "${CMAKE_INSTALL_PREFIX}/bundle_executable/bin/$<TARGET_FILE_NAME:topexe>"
215     )
216   ]])