summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorRobert Maynard <rmaynard@nvidia.com>2023-04-11 15:49:52 -0400
committerRobert Maynard <rmaynard@nvidia.com>2023-05-04 09:39:06 -0400
commitc42630ee62df80e649211e99c510cab7ac28fc0b (patch)
tree8b450a62d4e4c9559b8fe03b11575bde8d154608 /Tests
parent0fb923c46041d67110c8e0907afdf66b3b25f25a (diff)
downloadcmake-c42630ee62df80e649211e99c510cab7ac28fc0b.tar.gz
cmGeneratorExpressionNode: implement `COMPILE_ONLY` genex
This generator expression is the inverse of `LINK_ONLY` and only coveys usage requirements for the purposes of compilation. Its intended use is to avoid needing to export targets that do not have link usage requirements (e.g., header-only libraries) when used by another target. See: #15415
Diffstat (limited to 'Tests')
-rw-r--r--Tests/CMakeCommands/target_link_libraries/CMakeLists.txt16
-rw-r--r--Tests/CMakeCommands/target_link_libraries/compile_only.cpp8
-rw-r--r--Tests/ExportImport/Export/CMakeLists.txt10
-rw-r--r--Tests/ExportImport/Export/testLib2.c4
-rw-r--r--Tests/RunCMake/GeneratorExpression/COMPILE_ONLY-not-compiling-stderr.txt2
5 files changed, 39 insertions, 1 deletions
diff --git a/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt b/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt
index b2365ca92b..8231a5cd9f 100644
--- a/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt
+++ b/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt
@@ -155,3 +155,19 @@ target_link_libraries(TopDir SubDirC)
add_library(TopDirImported IMPORTED INTERFACE)
target_compile_definitions(TopDirImported INTERFACE DEF_TopDirImported)
cmake_policy(POP)
+
+#----------------------------------------------------------------------------
+# Test $<COMPILE_ONLY:> genex.
+cmake_policy(SET CMP0099 NEW)
+add_library(dont_link_too SHARED compile_only.cpp)
+target_compile_definitions(dont_link_too PUBLIC USE_EXAMPLE)
+target_link_options(dont_link_too INTERFACE invalid_link_option)
+target_link_libraries(dont_link_too INTERFACE invalid_link_library)
+
+add_library(uses_compile_only_genex SHARED compile_only.cpp)
+target_link_libraries(uses_compile_only_genex PUBLIC $<COMPILE_ONLY:dont_link_too>)
+
+add_library(uses_compile_only_genex_static STATIC compile_only.cpp)
+target_link_libraries(uses_compile_only_genex_static PRIVATE $<COMPILE_ONLY:dont_link_too>)
+add_executable(uses_via_static_linking main.cxx)
+target_link_libraries(uses_via_static_linking PRIVATE uses_compile_only_genex_static)
diff --git a/Tests/CMakeCommands/target_link_libraries/compile_only.cpp b/Tests/CMakeCommands/target_link_libraries/compile_only.cpp
new file mode 100644
index 0000000000..7519bd0c6e
--- /dev/null
+++ b/Tests/CMakeCommands/target_link_libraries/compile_only.cpp
@@ -0,0 +1,8 @@
+
+#ifndef USE_EXAMPLE
+# error "Missing propagated define"
+#endif
+
+// Solaris needs non-empty content so ensure
+// we have at least one symbol
+int Solaris_requires_a_symbol_here = 0;
diff --git a/Tests/ExportImport/Export/CMakeLists.txt b/Tests/ExportImport/Export/CMakeLists.txt
index 6f19c13c01..67f2fcb8cc 100644
--- a/Tests/ExportImport/Export/CMakeLists.txt
+++ b/Tests/ExportImport/Export/CMakeLists.txt
@@ -22,9 +22,18 @@ add_executable(testExe2 testExe2.c)
set_property(TARGET testExe2 PROPERTY ENABLE_EXPORTS 1)
set_property(TARGET testExe2 PROPERTY LINK_INTERFACE_LIBRARIES testExe2lib)
+add_library(compileOnly INTERFACE)
+target_compile_definitions(compileOnly INTERFACE FROM_compileOnly)
+target_link_options(compileOnly INTERFACE -fthis-flag-does-not-exist)
+
add_library(testLib1 STATIC testLib1.c)
add_library(testLib2 STATIC testLib2.c)
target_link_libraries(testLib2 testLib1)
+target_link_libraries(testLib2
+ PRIVATE
+ testLib1
+ "$<COMPILE_ONLY:compileOnly>")
+
# Test install(FILES) with generator expressions referencing testLib1.
add_custom_command(TARGET testLib1 POST_BUILD
@@ -556,6 +565,7 @@ install(FILES
# Install and export from install tree.
install(
TARGETS
+ compileOnly
testExe1 testLib1 testLib2 testExe2 testLib3 testLib4 testExe3 testExe4
testExe2lib testLib4lib testLib4libdbg testLib4libopt
testLib6 testLib7 testLib8
diff --git a/Tests/ExportImport/Export/testLib2.c b/Tests/ExportImport/Export/testLib2.c
index 7a5206f828..f5faffa5ea 100644
--- a/Tests/ExportImport/Export/testLib2.c
+++ b/Tests/ExportImport/Export/testLib2.c
@@ -1,4 +1,8 @@
+#ifndef FROM_compileOnly
+# error "Usage requirements from `compileOnly` not found"
+#endif
+
extern int testLib1(void);
int testLib2(void)
diff --git a/Tests/RunCMake/GeneratorExpression/COMPILE_ONLY-not-compiling-stderr.txt b/Tests/RunCMake/GeneratorExpression/COMPILE_ONLY-not-compiling-stderr.txt
index 5e7fce2a1b..8c93d59ac3 100644
--- a/Tests/RunCMake/GeneratorExpression/COMPILE_ONLY-not-compiling-stderr.txt
+++ b/Tests/RunCMake/GeneratorExpression/COMPILE_ONLY-not-compiling-stderr.txt
@@ -3,6 +3,6 @@ CMake Error at COMPILE_ONLY-not-compiling.cmake:1 \(add_custom_target\):
\$<COMPILE_ONLY:something>
- \$<COMPILE_ONLY:...> may only be used via linking
+ \$<COMPILE_ONLY:...> may only be used for linking
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)