diff options
author | Brad King <brad.king@kitware.com> | 2018-09-07 16:59:52 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2018-09-12 17:06:36 (GMT) |
commit | a1ad0a699be3a2e9e3a18cc07c3bf069dedcfbfc (patch) | |
tree | 1be5538764c5520f53e8c0938061512a59e4479a /Tests/CMakeCommands | |
parent | 9bbae5ae2870082a3e62596e25f53dcdadaa51a9 (diff) | |
download | CMake-a1ad0a699be3a2e9e3a18cc07c3bf069dedcfbfc.zip CMake-a1ad0a699be3a2e9e3a18cc07c3bf069dedcfbfc.tar.gz CMake-a1ad0a699be3a2e9e3a18cc07c3bf069dedcfbfc.tar.bz2 |
target_link_libraries: Allow use with targets in other directories
Previously the command did not allow naming targets on the LHS that
were not created in the calling directory. Lift this restriction to
enable more flexible use by projects.
Targets named on the RHS will need to be looked up during generation in
the scope of the call site rather than the scope of the LHS target.
Introduce an internal syntax in `[INTERFACE_]LINK_LIBRARIES` properties
to specify target names that need to be looked up in a directory other
than that containing the target on which the property is set. Add
minimal documentation of the syntax to help users that encounter it.
Unfortunately CMake previously did allow such calls in the case that
only `INTERFACE` libraries are specified, but those libraries would be
looked up in the target's directory rather than the caller's. Add
policy `CMP0079` to enable the new behavior with new lookup scope in a
compatible way.
Fixes: #17943
Diffstat (limited to 'Tests/CMakeCommands')
6 files changed, 84 insertions, 0 deletions
diff --git a/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt b/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt index e11f980..85ce1f7 100644 --- a/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt +++ b/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt @@ -130,3 +130,15 @@ target_link_libraries(newsignature1 PRIVATE depC INTERFACE depD PUBLIC depB PRIV assert_property(newsignature1 INTERFACE_LINK_LIBRARIES "depD;depB") assert_property(newsignature1 LINK_LIBRARIES "depC;depB;subdirlib") + +#---------------------------------------------------------------------------- +# Test cross-directory linking. +cmake_policy(PUSH) +cmake_policy(SET CMP0079 NEW) +add_executable(TopDir TopDir.c) +add_subdirectory(SubDirA) +add_subdirectory(SubDirB) +target_link_libraries(SubDirB TopDirImported) +add_library(TopDirImported IMPORTED INTERFACE) +target_compile_definitions(TopDirImported INTERFACE DEF_TopDirImported) +cmake_policy(POP) diff --git a/Tests/CMakeCommands/target_link_libraries/SubDirA/CMakeLists.txt b/Tests/CMakeCommands/target_link_libraries/SubDirA/CMakeLists.txt new file mode 100644 index 0000000..4dae103 --- /dev/null +++ b/Tests/CMakeCommands/target_link_libraries/SubDirA/CMakeLists.txt @@ -0,0 +1,15 @@ +add_executable(SubDirA SubDirA.c) + +# Link to a target imported in this directory that would not normally +# be visible to the directory in which TopDir is defined. +target_link_libraries(TopDir PUBLIC SameNameImported) + +# Link SubDirA to a target imported in this directory that has the same +# name as a target imported in SubDirB's directory. SubDirB will also +# tell us to link its copy. At compile time we verify both are linked. +target_link_libraries(SubDirA PRIVATE SameNameImported) + +# Import a target with the same name as a target imported in SubDirB. +# Distinguish this copy by having a unique usage requirement. +add_library(SameNameImported IMPORTED INTERFACE) +target_compile_definitions(SameNameImported INTERFACE DEF_SameNameImportedSubDirA) diff --git a/Tests/CMakeCommands/target_link_libraries/SubDirA/SubDirA.c b/Tests/CMakeCommands/target_link_libraries/SubDirA/SubDirA.c new file mode 100644 index 0000000..4706bb9 --- /dev/null +++ b/Tests/CMakeCommands/target_link_libraries/SubDirA/SubDirA.c @@ -0,0 +1,14 @@ +#ifndef DEF_SameNameImportedSubDirA +# error "DEF_SameNameImportedSubDirA is not defined but should be!" +#endif +#ifndef DEF_SameNameImportedSubDirB +# error "DEF_SameNameImportedSubDirB is not defined but should be!" +#endif +#ifdef DEF_TopDirImported +# error "DEF_TopDirImported is defined but should not be!" +#endif + +int main(void) +{ + return 0; +} diff --git a/Tests/CMakeCommands/target_link_libraries/SubDirB/CMakeLists.txt b/Tests/CMakeCommands/target_link_libraries/SubDirB/CMakeLists.txt new file mode 100644 index 0000000..7c918e6 --- /dev/null +++ b/Tests/CMakeCommands/target_link_libraries/SubDirB/CMakeLists.txt @@ -0,0 +1,15 @@ +add_executable(SubDirB SubDirB.c) + +# Link to a target imported in this directory that would not normally +# be visible to the directory in which TopDir is defined. +target_link_libraries(TopDir PUBLIC SameNameImported) + +# Link SubDirA to a target imported in this directory that has the same +# name as a target imported in SubDirA's directory. We verify when +# compiling SubDirA that it sees our target and its own. +target_link_libraries(SubDirA PRIVATE SameNameImported) + +# Import a target with the same name as a target imported in SubDirA. +# Distinguish this copy by having a unique usage requirement. +add_library(SameNameImported IMPORTED INTERFACE) +target_compile_definitions(SameNameImported INTERFACE DEF_SameNameImportedSubDirB) diff --git a/Tests/CMakeCommands/target_link_libraries/SubDirB/SubDirB.c b/Tests/CMakeCommands/target_link_libraries/SubDirB/SubDirB.c new file mode 100644 index 0000000..6e56729 --- /dev/null +++ b/Tests/CMakeCommands/target_link_libraries/SubDirB/SubDirB.c @@ -0,0 +1,14 @@ +#ifdef DEF_SameNameImportedSubDirA +# error "DEF_SameNameImportedSubDirA is defined but should not be!" +#endif +#ifdef DEF_SameNameImportedSubDirB +# error "DEF_SameNameImportedSubDirB is defined but should not be!" +#endif +#ifndef DEF_TopDirImported +# error "DEF_TopDirImported is not defined but should be!" +#endif + +int main(void) +{ + return 0; +} diff --git a/Tests/CMakeCommands/target_link_libraries/TopDir.c b/Tests/CMakeCommands/target_link_libraries/TopDir.c new file mode 100644 index 0000000..4706bb9 --- /dev/null +++ b/Tests/CMakeCommands/target_link_libraries/TopDir.c @@ -0,0 +1,14 @@ +#ifndef DEF_SameNameImportedSubDirA +# error "DEF_SameNameImportedSubDirA is not defined but should be!" +#endif +#ifndef DEF_SameNameImportedSubDirB +# error "DEF_SameNameImportedSubDirB is not defined but should be!" +#endif +#ifdef DEF_TopDirImported +# error "DEF_TopDirImported is defined but should not be!" +#endif + +int main(void) +{ + return 0; +} |