diff options
author | Brad King <brad.king@kitware.com> | 2015-04-07 14:43:47 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2015-04-09 15:29:18 (GMT) |
commit | 882f48e5ba335a1dbc1750c23e6dea5fa92a3adc (patch) | |
tree | 7783686bed3456d95538a4770dff27f0e24963bc /Tests/RunCMake/CMP0060/CMP0060-Common.cmake | |
parent | 318cd37097c724fac13a364fe3beb21055575ae7 (diff) | |
download | CMake-882f48e5ba335a1dbc1750c23e6dea5fa92a3adc.zip CMake-882f48e5ba335a1dbc1750c23e6dea5fa92a3adc.tar.gz CMake-882f48e5ba335a1dbc1750c23e6dea5fa92a3adc.tar.bz2 |
Link libraries by full path even in implicit directories
When CMP0003 was first introduced we wanted to link all libraries by
full path. However, some projects had problems on platforms where
find_library would find /usr/lib/libfoo.so when the project really
wanted to link to /usr/lib/<arch>/libfoo.so and had been working by
accident because pre-CMP0003 behavior used -lfoo to link.
We first tried to address that in commit v2.6.0~440 (Teach find_library
to avoid returning library paths in system directories, 2008-01-23) by
returning just "foo" for libraries in implicit link directories. This
caused problems for projects expecting find_library to always return a
full path. We ended up using the solution in commit v2.6.0~366 (...
switch library paths found in implicit link directories to use -l,
2008-01-31). However, the special case for libraries in implicit link
directories has also proven problematic and confusing.
Introduce policy CMP0060 to switch to linking all libraries by full path
even if they are in implicit link directories. Explain in the policy
documentation the factors that led to the original approach and now to
this approach.
Diffstat (limited to 'Tests/RunCMake/CMP0060/CMP0060-Common.cmake')
-rw-r--r-- | Tests/RunCMake/CMP0060/CMP0060-Common.cmake | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Tests/RunCMake/CMP0060/CMP0060-Common.cmake b/Tests/RunCMake/CMP0060/CMP0060-Common.cmake new file mode 100644 index 0000000..e0a56e6 --- /dev/null +++ b/Tests/RunCMake/CMP0060/CMP0060-Common.cmake @@ -0,0 +1,35 @@ +# Always build in a predictable configuration. For multi-config +# generators we depend on RunCMakeTest.cmake to do this for us. +if(NOT CMAKE_CONFIGURATION_TYPES) + set(CMAKE_BUILD_TYPE Debug) +endif() + +# Convince CMake that it can instruct the linker to search for the +# library of the proper linkage type, but do not really pass flags. +set(CMAKE_EXE_LINK_STATIC_C_FLAGS " ") +set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS " ") + +# Make a link line asking for the linker to search for the library +# look like a missing object file so we will get predictable content +# in the error message. This also ensures that cases expected to use +# the full path can be verified by confirming that they link. +set(CMAKE_LINK_LIBRARY_FLAG LINKFLAG_) +set(CMAKE_LINK_LIBRARY_SUFFIX _LINKSUFFIX${CMAKE_C_OUTPUT_EXTENSION}) + +# Convince CMake that our library is in an implicit linker search directory. +list(APPEND CMAKE_C_IMPLICIT_LINK_DIRECTORIES ${CMAKE_CURRENT_BINARY_DIR}/lib) + +# Create a simple library file. Place it in our library directory. +add_library(CMP0060 STATIC cmp0060.c) +set_property(TARGET CMP0060 PROPERTY + ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR}/lib) + +# Add a target to link the library file by full path. +add_executable(main1 main.c) +target_link_libraries(main1 $<TARGET_FILE:CMP0060>) +add_dependencies(main1 CMP0060) + +# Add a second target to verify the warning only appears once. +add_executable(main2 main.c) +target_link_libraries(main2 $<TARGET_FILE:CMP0060>) +add_dependencies(main2 CMP0060) |