2 # - This module provides the function
3 # target_link_libraries_with_dynamic_lookup which can be used to
4 # "weakly" link a loadable module.
6 # Link a library to a target such that the symbols are resolved at
7 # run-time not link-time. This should be used when compiling a
8 # loadable module when the symbols should be resolve from the run-time
9 # environment where the module is loaded, and not a specific system
12 # Specifically, for OSX it uses undefined dynamic_lookup. This is
13 # similar to using "-shared" on Linux where undefined symbols are
16 # Additionally, the linker is checked to see if it supports undefined
17 # symbols when linking a shared library. If it does then the library
18 # is not linked when specified with this function.
20 # http://blog.tim-smith.us/2015/09/python-extension-modules-os-x/
23 # The following functions are defined:
25 # _get_target_type(<ResultVar> <Target>)
27 # **INTERNAL** Shorthand for querying an abbreviated version of the target type
28 # of the given ``<Target>``. ``<ResultVar>`` is set to "STATIC" for a
29 # STATIC_LIBRARY, "SHARED" for a SHARED_LIBRARY, "MODULE" for a MODULE_LIBRARY,
30 # and "EXE" for an EXECUTABLE.
35 # The abbreviated version of the ``<Target>``'s type.
38 # _test_weak_link_project(<TargetType>
43 # **INTERNAL** Attempt to compile and run a test project where a target of type
44 # ``<TargetType>`` is weakly-linked against a dependency of type ``<LibType>``.
45 # ``<TargetType>`` can be one of "STATIC", "SHARED", "MODULE", or "EXE".
46 # ``<LibType>`` can be one of "STATIC", "SHARED", or "MODULE".
51 # Whether the current C toolchain can produce a working target binary of type
52 # ``<TargetType>`` that is weakly-linked against a dependency target of type
56 # List of flags to add to the linker command to produce a working target
57 # binary of type ``<TargetType>`` that is weakly-linked against a dependency
58 # target of type ``<LibType>``.
61 # check_dynamic_lookup(<TargetType>
66 # Check if the linker requires a command line flag to allow leaving symbols
67 # unresolved when producing a target of type ``<TargetType>`` that is
68 # weakly-linked against a dependency of type ``<LibType>``. ``<TargetType>``
69 # can be one of "STATIC", "SHARED", "MODULE", or "EXE". ``<LibType>`` can be
70 # one of "STATIC", "SHARED", or "MODULE". The result is cached between
71 # invocations and recomputed only when the value of CMake's linker flag list
72 # changes; ``CMAKE_STATIC_LINKER_FLAGS`` if ``<TargetType>`` is "STATIC", and
73 # ``CMAKE_SHARED_LINKER_FLAGS`` otherwise.
79 # Whether the current C toolchain supports weak-linking for target binaries of
80 # type ``<TargetType>`` that are weakly-linked against a dependency target of
84 # List of flags to add to the linker command to produce a working target
85 # binary of type ``<TargetType>`` that is weakly-linked against a dependency
86 # target of type ``<LibType>``.
88 # ``HAS_DYNAMIC_LOOKUP_<TargetType>_<LibType>``
89 # Cached, global alias for ``<ResultVar>``
91 # ``DYNAMIC_LOOKUP_FLAGS_<TargetType>_<LibType>``
92 # Cached, global alias for ``<LinkFlagsVar>``
95 # target_link_libraries_with_dynamic_lookup(<Target> [<Libraries>])
97 # Like proper linking, except that the given ``<Libraries>`` are not necessarily
98 # linked. Instead, the ``<Target>`` is produced in a manner that allows for
99 # symbols unresolved within it to be resolved at runtime, presumably by the
100 # given ``<Libraries>``. If such a target can be produced, the provided
101 # ``<Libraries>`` are not actually linked. On platforms that do not support
102 # weak-linking, this function works just like ``target_link_libraries``.
104 function(_get_target_type result_var target)
105 set(target_type "SHARED_LIBRARY")
107 get_property(target_type TARGET ${target} PROPERTY TYPE)
112 if(target_type STREQUAL "STATIC_LIBRARY")
116 if(target_type STREQUAL "SHARED_LIBRARY")
120 if(target_type STREQUAL "MODULE_LIBRARY")
124 if(target_type STREQUAL "EXECUTABLE")
128 set(${result_var} ${result} PARENT_SCOPE)
132 function(_test_weak_link_project
138 set(gnu_ld_ignore "-Wl,--unresolved-symbols=ignore-all")
139 set(osx_dynamic_lookup "-undefined dynamic_lookup")
142 foreach(link_flag_spec gnu_ld_ignore osx_dynamic_lookup no_flag)
143 set(link_flag "${${link_flag_spec}}")
145 set(test_project_dir "${PROJECT_BINARY_DIR}/CMakeTmp")
146 set(test_project_dir "${test_project_dir}/${project_name}")
147 set(test_project_dir "${test_project_dir}/${link_flag_spec}")
148 set(test_project_dir "${test_project_dir}/${target_type}")
149 set(test_project_dir "${test_project_dir}/${lib_type}")
151 set(test_project_src_dir "${test_project_dir}/src")
152 set(test_project_bin_dir "${test_project_dir}/build")
154 file(MAKE_DIRECTORY ${test_project_src_dir})
155 file(MAKE_DIRECTORY ${test_project_bin_dir})
157 set(mod_type "STATIC")
158 set(link_mod_lib TRUE)
159 set(link_exe_lib TRUE)
160 set(link_exe_mod FALSE)
162 if("${target_type}" STREQUAL "EXE")
163 set(link_exe_lib FALSE)
164 set(link_exe_mod TRUE)
166 set(mod_type "${target_type}")
169 if("${mod_type}" STREQUAL "MODULE")
170 set(link_mod_lib FALSE)
174 file(WRITE "${test_project_src_dir}/CMakeLists.txt" "
175 cmake_minimum_required(VERSION ${CMAKE_VERSION})
176 project(${project_name} C)
178 include_directories(${test_project_src_dir})
180 add_library(number ${lib_type} number.c)
181 add_library(counter ${mod_type} counter.c)
184 if("${mod_type}" STREQUAL "MODULE")
185 file(APPEND "${test_project_src_dir}/CMakeLists.txt" "
186 set_target_properties(counter PROPERTIES PREFIX \"\")
191 file(APPEND "${test_project_src_dir}/CMakeLists.txt" "
192 target_link_libraries(counter number)
194 elseif(NOT link_flag STREQUAL "")
195 file(APPEND "${test_project_src_dir}/CMakeLists.txt" "
196 set_target_properties(counter PROPERTIES LINK_FLAGS \"${link_flag}\")
200 file(APPEND "${test_project_src_dir}/CMakeLists.txt" "
201 add_executable(main main.c)
205 file(APPEND "${test_project_src_dir}/CMakeLists.txt" "
206 target_link_libraries(main number)
208 elseif(NOT link_flag STREQUAL "")
209 file(APPEND "${test_project_src_dir}/CMakeLists.txt" "
210 target_link_libraries(main \"${link_flag}\")
215 file(APPEND "${test_project_src_dir}/CMakeLists.txt" "
216 target_link_libraries(main counter)
219 file(APPEND "${test_project_src_dir}/CMakeLists.txt" "
220 target_link_libraries(main \"${CMAKE_DL_LIBS}\")
224 file(WRITE "${test_project_src_dir}/number.c" "
228 void set_number(int number) { _number = number; }
229 int get_number() { return _number; }
232 file(WRITE "${test_project_src_dir}/number.h" "
235 extern void set_number(int);
236 extern int get_number(void);
240 file(WRITE "${test_project_src_dir}/counter.c" "
243 int result = get_number();
244 set_number(result + 1);
249 file(WRITE "${test_project_src_dir}/counter.h" "
252 extern int count(void);
256 file(WRITE "${test_project_src_dir}/main.c" "
263 file(APPEND "${test_project_src_dir}/main.c" "
268 file(APPEND "${test_project_src_dir}/main.c" "
270 int result = get_number();
271 set_number(result + 1);
275 int main(int argc, char **argv) {
280 file(APPEND "${test_project_src_dir}/main.c" "
281 void *counter_module;
284 counter_module = dlopen(\"./counter.so\", RTLD_LAZY | RTLD_GLOBAL);
285 if(!counter_module) goto error;
287 count = dlsym(counter_module, \"count\");
288 if(!count) goto error;
292 file(APPEND "${test_project_src_dir}/main.c" "
293 result = count() != 0 ? EXIT_FAILURE :
294 my_count() != 1 ? EXIT_FAILURE :
295 my_count() != 2 ? EXIT_FAILURE :
296 count() != 3 ? EXIT_FAILURE :
297 count() != 4 ? EXIT_FAILURE :
298 count() != 5 ? EXIT_FAILURE :
299 my_count() != 6 ? EXIT_FAILURE : EXIT_SUCCESS;
303 file(APPEND "${test_project_src_dir}/main.c" "
306 fprintf(stderr, \"Error occured:\\n %s\\n\", dlerror());
310 if(counter_module) dlclose(counter_module);
314 file(APPEND "${test_project_src_dir}/main.c" "
320 if(APPLE AND ${CMAKE_VERSION} VERSION_GREATER 2.8.11)
321 set(_rpath_arg "-DCMAKE_MACOSX_RPATH='${CMAKE_MACOSX_RPATH}'")
324 try_compile(project_compiles
325 "${test_project_bin_dir}"
326 "${test_project_src_dir}"
329 "-DCMAKE_SHARED_LINKER_FLAGS='${CMAKE_SHARED_LINKER_FLAGS}'"
331 OUTPUT_VARIABLE compile_output)
337 execute_process(COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR}
338 "${test_project_bin_dir}/main"
339 WORKING_DIRECTORY "${test_project_bin_dir}"
340 RESULT_VARIABLE project_works
341 OUTPUT_VARIABLE run_output
342 ERROR_VARIABLE run_output)
346 "Weak Link ${target_type} -> ${lib_type} (${link_flag_spec})")
348 if(project_works EQUAL 0)
349 set(project_works TRUE)
350 message(STATUS "Performing Test ${test_description} - Success")
352 set(project_works FALSE)
353 message(STATUS "Performing Test ${test_description} - Failed")
354 file(APPEND ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CMakeError.log
355 "Performing Test ${test_description} failed with the "
356 "following output:\n"
357 "BUILD\n-----\n${compile_output}\nRUN\n---\n${run_output}\n")
360 set(${can_weak_link_var} ${project_works} PARENT_SCOPE)
362 set(${project_name} ${link_flag} PARENT_SCOPE)
369 function(check_dynamic_lookup
372 has_dynamic_lookup_var
375 # hash the CMAKE_FLAGS passed and check cache to know if we need to rerun
376 if("${target_type}" STREQUAL "STATIC")
377 string(MD5 cmake_flags_hash "${CMAKE_STATIC_LINKER_FLAGS}")
379 string(MD5 cmake_flags_hash "${CMAKE_SHARED_LINKER_FLAGS}")
382 set(cache_var "HAS_DYNAMIC_LOOKUP_${target_type}_${lib_type}")
383 set(cache_hash_var "HAS_DYNAMIC_LOOKUP_${target_type}_${lib_type}_hash")
384 set(result_var "DYNAMIC_LOOKUP_FLAGS_${target_type}_${lib_type}")
386 if( NOT DEFINED ${cache_hash_var}
387 OR NOT "${${cache_hash_var}}" STREQUAL "${cmake_flags_hash}")
388 unset(${cache_var} CACHE)
391 if(NOT DEFINED ${cache_var})
394 if(NOT CMAKE_CROSSCOMPILING)
396 elseif(CMAKE_CROSSCOMPILING AND CMAKE_CROSSCOMPILING_EMULATOR)
401 set(has_dynamic_lookup FALSE)
404 _test_weak_link_project(${target_type}
410 set(caveat " (when linking ${target_type} against ${lib_type})")
412 set(${cache_var} "${has_dynamic_lookup}"
414 "linker supports dynamic lookup for undefined symbols${caveat}")
416 set(${result_var} "${link_flags}"
418 "linker flags for dynamic lookup${caveat}")
420 set(${cache_hash_var} "${cmake_flags_hash}"
421 CACHE INTERNAL "hashed flags for ${cache_var} check")
424 set(${has_dynamic_lookup_var} "${${cache_var}}" PARENT_SCOPE)
425 set(${link_flags_var} "${${result_var}}" PARENT_SCOPE)
429 function(target_link_libraries_with_dynamic_lookup target)
430 _get_target_type(target_type ${target})
437 _get_target_type(lib_type ${lib})
438 check_dynamic_lookup(${target_type}
441 dynamic_lookup_flags)
443 if(has_dynamic_lookup)
444 if(dynamic_lookup_flags)
445 if("${target_type}" STREQUAL "EXE")
446 list(APPEND link_items "${dynamic_lookup_flags}")
448 list(APPEND link_props "${dynamic_lookup_flags}")
452 list(APPEND link_libs "${lib}")
457 list(REMOVE_DUPLICATES link_props)
461 list(REMOVE_DUPLICATES link_items)
465 list(REMOVE_DUPLICATES link_libs)
469 set_target_properties(${target}
470 PROPERTIES LINK_FLAGS "${link_props}")
473 set(links "${link_items}" "${link_libs}")
475 target_link_libraries(${target} "${links}")