summaryrefslogtreecommitdiffstats
path: root/Tests/RunCMake/LinkItemValidation/OnlyTargets.cmake
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2021-12-20 15:03:17 (GMT)
committerBrad King <brad.king@kitware.com>2021-12-20 17:14:07 (GMT)
commit37af6c33116af75a9538861268edab0ddc202d79 (patch)
tree8d1ec64b38370df7abf070af5cebb34638cf6ae4 /Tests/RunCMake/LinkItemValidation/OnlyTargets.cmake
parent5134f099a32e2eb0d557ad86dabd8b811709c94d (diff)
downloadCMake-37af6c33116af75a9538861268edab0ddc202d79.zip
CMake-37af6c33116af75a9538861268edab0ddc202d79.tar.gz
CMake-37af6c33116af75a9538861268edab0ddc202d79.tar.bz2
target_link_libraries: Optionally require only target names
Optionally verify that items in `LINK_LIBRARIES` and `INTERFACE_LINK_LIBRARIES` that can be target names are actually target names. Add a `LINK_LIBRARIES_ONLY_TARGETS` target property and corresponding `CMAKE_LINK_LIBRARIES_ONLY_TARGETS` variable to enable this new check. Fixes: #22858
Diffstat (limited to 'Tests/RunCMake/LinkItemValidation/OnlyTargets.cmake')
-rw-r--r--Tests/RunCMake/LinkItemValidation/OnlyTargets.cmake56
1 files changed, 56 insertions, 0 deletions
diff --git a/Tests/RunCMake/LinkItemValidation/OnlyTargets.cmake b/Tests/RunCMake/LinkItemValidation/OnlyTargets.cmake
new file mode 100644
index 0000000..9417318
--- /dev/null
+++ b/Tests/RunCMake/LinkItemValidation/OnlyTargets.cmake
@@ -0,0 +1,56 @@
+enable_language(C)
+
+set(CMAKE_LINK_LIBRARIES_ONLY_TARGETS 1)
+
+# Use imported interface library to name toolchain-provided libraries.
+add_library(toolchain::m INTERFACE IMPORTED)
+set_property(TARGET toolchain::m PROPERTY IMPORTED_LIBNAME "m")
+
+# Linking directly warns.
+add_executable(exe main.c)
+target_link_libraries(exe PRIVATE
+ -lflag_in_exe # accepted
+ /abs/path/in_exe # accepted
+ rel/path/in_exe # accepted
+ toolchain::m # accepted
+ non_target_in_exe # rejected
+ )
+
+# Link interfaces warn.
+add_library(iface INTERFACE)
+target_link_libraries(iface INTERFACE
+ -lflag_in_iface # accepted
+ /abs/path/in_iface # accepted
+ rel/path/in_iface # accepted
+ non_target_in_iface # rejected
+ )
+
+# Imported target link interfaces warn if explicitly enabled.
+add_library(iface_imported_checked INTERFACE IMPORTED)
+target_link_libraries(iface_imported_checked INTERFACE
+ -lflag_iface_imported_checked # accepted
+ /abs/path/in_iface_imported_checked # accepted
+ rel/path/in_iface_imported_checked # accepted
+ non_target_in_iface_imported_checked # rejected
+ )
+set_property(TARGET iface_imported_checked PROPERTY LINK_LIBRARIES_ONLY_TARGETS 1)
+
+# Linking directly does not warn if explicitly disabled.
+add_executable(exe_not_checked main.c)
+target_link_libraries(exe_not_checked PRIVATE
+ non_target_in_exe_not_checked
+ )
+set_property(TARGET exe_not_checked PROPERTY LINK_LIBRARIES_ONLY_TARGETS 0)
+
+# Link interfaces do not warn if explicitly disabled.
+add_library(iface_not_checked INTERFACE)
+target_link_libraries(iface_not_checked INTERFACE
+ non_target_in_iface_not_checked
+ )
+set_property(TARGET iface_not_checked PROPERTY LINK_LIBRARIES_ONLY_TARGETS 0)
+
+# Imported target link interfaces do not warn if not explicitly enabled.
+add_library(iface_imported_default INTERFACE IMPORTED)
+target_link_libraries(iface_imported_default INTERFACE
+ non_target_in_iface_imported_default
+ )