summaryrefslogtreecommitdiffstats
path: root/Tests/RunCMake/InterfaceLibrary
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2020-07-20 17:19:26 (GMT)
committerBrad King <brad.king@kitware.com>2020-08-07 12:46:32 (GMT)
commit439191313363caea225a508634495c50d4cc60dd (patch)
treeeb1156c1fbf93b4a9ea23ec812ba49ef7a195846 /Tests/RunCMake/InterfaceLibrary
parentafb998704e67d3d3ce5b24c112cb06e770fca78d (diff)
downloadCMake-439191313363caea225a508634495c50d4cc60dd.zip
CMake-439191313363caea225a508634495c50d4cc60dd.tar.gz
CMake-439191313363caea225a508634495c50d4cc60dd.tar.bz2
Add INTERFACE libraries to generated buildsystem if they have SOURCES
INTERFACE libraries were created with the intention of collecting usage requirements for use by other targets via `target_link_libraries`. Therefore they were not allowed to have SOURCES and were not included in the generated buildsystem. In practice, this has become limiting: * Header-only libraries do have sources, they just do not compile. Developers should be able to edit those sources (the header files) in their IDE. * Header-only libraries may need to generate some of their header files via custom commands. Some projects work around these limitations by pairing each interface library with an `add_custom_target` that makes the header files and custom commands appear in the generated buildsystem and in IDEs. Lift such limitations by allowing INTERFACE libraries to have SOURCES. For those with sources, add a corresponding build target to the generated buildsystem. Fixes: #19145
Diffstat (limited to 'Tests/RunCMake/InterfaceLibrary')
-rw-r--r--Tests/RunCMake/InterfaceLibrary/ConfigSources.cmake2
-rw-r--r--Tests/RunCMake/InterfaceLibrary/EmptySources-build2-result.txt1
-rw-r--r--Tests/RunCMake/InterfaceLibrary/EmptySources-build2-stdout.txt1
-rw-r--r--Tests/RunCMake/InterfaceLibrary/EmptySources.cmake8
-rw-r--r--Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build1-check.cmake4
-rw-r--r--Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build2-check.cmake4
-rw-r--r--Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build3-result.txt1
-rw-r--r--Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build3-stdout.txt1
-rw-r--r--Tests/RunCMake/InterfaceLibrary/ExcludeFromAll.cmake7
-rw-r--r--Tests/RunCMake/InterfaceLibrary/PublicSources-build3-result.txt1
-rw-r--r--Tests/RunCMake/InterfaceLibrary/PublicSources-build3-stdout.txt1
-rw-r--r--Tests/RunCMake/InterfaceLibrary/PublicSources.cmake20
-rw-r--r--Tests/RunCMake/InterfaceLibrary/RunCMakeTest.cmake24
-rw-r--r--Tests/RunCMake/InterfaceLibrary/iface.c4
-rw-r--r--Tests/RunCMake/InterfaceLibrary/iface_broken.c1
-rw-r--r--Tests/RunCMake/InterfaceLibrary/invalid_signature-stderr.txt15
-rw-r--r--Tests/RunCMake/InterfaceLibrary/invalid_signature.cmake6
-rw-r--r--Tests/RunCMake/InterfaceLibrary/use_iface.c6
18 files changed, 89 insertions, 18 deletions
diff --git a/Tests/RunCMake/InterfaceLibrary/ConfigSources.cmake b/Tests/RunCMake/InterfaceLibrary/ConfigSources.cmake
new file mode 100644
index 0000000..631a845
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/ConfigSources.cmake
@@ -0,0 +1,2 @@
+# Test an interface library added to the build system by a per-config source.
+add_library(iface INTERFACE $<$<CONFIG:NotAConfig>:${CMAKE_CURRENT_SOURCE_DIR}/iface.c>)
diff --git a/Tests/RunCMake/InterfaceLibrary/EmptySources-build2-result.txt b/Tests/RunCMake/InterfaceLibrary/EmptySources-build2-result.txt
new file mode 100644
index 0000000..d197c91
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/EmptySources-build2-result.txt
@@ -0,0 +1 @@
+[^0]
diff --git a/Tests/RunCMake/InterfaceLibrary/EmptySources-build2-stdout.txt b/Tests/RunCMake/InterfaceLibrary/EmptySources-build2-stdout.txt
new file mode 100644
index 0000000..aac9172
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/EmptySources-build2-stdout.txt
@@ -0,0 +1 @@
+iface2|Invalid project
diff --git a/Tests/RunCMake/InterfaceLibrary/EmptySources.cmake b/Tests/RunCMake/InterfaceLibrary/EmptySources.cmake
new file mode 100644
index 0000000..f452394
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/EmptySources.cmake
@@ -0,0 +1,8 @@
+# Test an interface library added to the build system by empty SOURCES.
+add_library(iface INTERFACE)
+set_property(TARGET iface PROPERTY SOURCES "")
+
+# ...but not added by unset SOURCES.
+add_library(iface2 INTERFACE)
+set_property(TARGET iface2 PROPERTY SOURCES "")
+set_property(TARGET iface2 PROPERTY SOURCES)
diff --git a/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build1-check.cmake b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build1-check.cmake
new file mode 100644
index 0000000..6500e48
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build1-check.cmake
@@ -0,0 +1,4 @@
+if(EXISTS "${RunCMake_TEST_BINARY_DIR}/iface.txt")
+ set(RunCMake_TEST_FAILED "iface target built as part of 'all'")
+ return()
+endif()
diff --git a/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build2-check.cmake b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build2-check.cmake
new file mode 100644
index 0000000..0977c24
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build2-check.cmake
@@ -0,0 +1,4 @@
+if(NOT EXISTS "${RunCMake_TEST_BINARY_DIR}/iface.txt")
+ set(RunCMake_TEST_FAILED "iface target not built")
+ return()
+endif()
diff --git a/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build3-result.txt b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build3-result.txt
new file mode 100644
index 0000000..d197c91
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build3-result.txt
@@ -0,0 +1 @@
+[^0]
diff --git a/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build3-stdout.txt b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build3-stdout.txt
new file mode 100644
index 0000000..aac9172
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll-build3-stdout.txt
@@ -0,0 +1 @@
+iface2|Invalid project
diff --git a/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll.cmake b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll.cmake
new file mode 100644
index 0000000..714161d
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/ExcludeFromAll.cmake
@@ -0,0 +1,7 @@
+# Test an interface library with a custom command, but excluded from all.
+add_custom_command(OUTPUT iface.txt COMMAND ${CMAKE_COMMAND} -E touch iface.txt)
+add_library(iface INTERFACE EXCLUDE_FROM_ALL iface.txt)
+
+# Test that EXCLUDE_FROM_ALL is allowed even if the interface library has
+# no sources, and does not cause it to appear in the build system.
+add_library(iface2 INTERFACE EXCLUDE_FROM_ALL)
diff --git a/Tests/RunCMake/InterfaceLibrary/PublicSources-build3-result.txt b/Tests/RunCMake/InterfaceLibrary/PublicSources-build3-result.txt
new file mode 100644
index 0000000..d197c91
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/PublicSources-build3-result.txt
@@ -0,0 +1 @@
+[^0]
diff --git a/Tests/RunCMake/InterfaceLibrary/PublicSources-build3-stdout.txt b/Tests/RunCMake/InterfaceLibrary/PublicSources-build3-stdout.txt
new file mode 100644
index 0000000..aac9172
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/PublicSources-build3-stdout.txt
@@ -0,0 +1 @@
+iface2|Invalid project
diff --git a/Tests/RunCMake/InterfaceLibrary/PublicSources.cmake b/Tests/RunCMake/InterfaceLibrary/PublicSources.cmake
new file mode 100644
index 0000000..24785bb
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/PublicSources.cmake
@@ -0,0 +1,20 @@
+cmake_policy(SET CMP0076 NEW)
+enable_language(C)
+
+# Test that an interface library can have PUBLIC sources.
+# This causes the target to appear in the build system
+# *and* causes consumers to use the source.
+add_library(iface INTERFACE)
+target_sources(iface
+ PUBLIC iface.c
+ # Private sources do not compile here or propagate.
+ PRIVATE iface_broken.c
+ )
+
+# Test that an intermediate interface library does not get the
+# sources and does not appear in the build system.
+add_library(iface2 INTERFACE)
+target_link_libraries(iface2 INTERFACE iface)
+
+add_executable(use_iface use_iface.c)
+target_link_libraries(use_iface PRIVATE iface2)
diff --git a/Tests/RunCMake/InterfaceLibrary/RunCMakeTest.cmake b/Tests/RunCMake/InterfaceLibrary/RunCMakeTest.cmake
index b84bc33..834b3c8 100644
--- a/Tests/RunCMake/InterfaceLibrary/RunCMakeTest.cmake
+++ b/Tests/RunCMake/InterfaceLibrary/RunCMakeTest.cmake
@@ -10,3 +10,27 @@ run_cmake(add_custom_command-TARGET)
run_cmake(IMPORTED_LIBNAME-bad-value)
run_cmake(IMPORTED_LIBNAME-non-iface)
run_cmake(IMPORTED_LIBNAME-non-imported)
+
+function(run_WithSources CASE)
+ if(NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
+ set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
+ endif()
+ set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build)
+ run_cmake(${CASE})
+ set(RunCMake_TEST_NO_CLEAN 1)
+ foreach(build IN LISTS ARGN)
+ if(build MATCHES "^([^:]+)$")
+ run_cmake_command(${CASE}-${CMAKE_MATCH_1} ${CMAKE_COMMAND} --build . --config Debug)
+ elseif(build MATCHES "^([^:]+):([^:,]+)(,merge)?$")
+ if(CMAKE_MATCH_3 STREQUAL ",merge")
+ set(RunCMake_TEST_OUTPUT_MERGE 1)
+ endif()
+ run_cmake_command(${CASE}-${CMAKE_MATCH_1} ${CMAKE_COMMAND} --build . --config Debug --target ${CMAKE_MATCH_2})
+ endif()
+ endforeach()
+endfunction()
+
+run_WithSources(ConfigSources "build1:iface")
+run_WithSources(EmptySources "build1:iface" "build2:iface2,merge")
+run_WithSources(ExcludeFromAll "build1" "build2:iface" "build3:iface2,merge")
+run_WithSources(PublicSources "build1" "build2:iface" "build3:iface2,merge")
diff --git a/Tests/RunCMake/InterfaceLibrary/iface.c b/Tests/RunCMake/InterfaceLibrary/iface.c
new file mode 100644
index 0000000..c7e7372
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/iface.c
@@ -0,0 +1,4 @@
+int iface(void)
+{
+ return 0;
+}
diff --git a/Tests/RunCMake/InterfaceLibrary/iface_broken.c b/Tests/RunCMake/InterfaceLibrary/iface_broken.c
new file mode 100644
index 0000000..4ff7f31
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/iface_broken.c
@@ -0,0 +1 @@
+#error This file should not compile
diff --git a/Tests/RunCMake/InterfaceLibrary/invalid_signature-stderr.txt b/Tests/RunCMake/InterfaceLibrary/invalid_signature-stderr.txt
index 6374b33..763f9f8 100644
--- a/Tests/RunCMake/InterfaceLibrary/invalid_signature-stderr.txt
+++ b/Tests/RunCMake/InterfaceLibrary/invalid_signature-stderr.txt
@@ -1,8 +1,3 @@
-CMake Error at invalid_signature.cmake:2 \(add_library\):
- add_library INTERFACE library requires no source arguments.
-Call Stack \(most recent call first\):
- CMakeLists.txt:3 \(include\)
-+
CMake Error at invalid_signature.cmake:3 \(add_library\):
add_library INTERFACE library specified with conflicting/multiple types.
Call Stack \(most recent call first\):
@@ -73,16 +68,6 @@ CMake Error at invalid_signature.cmake:16 \(add_library\):
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
+
-CMake Error at invalid_signature.cmake:17 \(add_library\):
- add_library INTERFACE library may not be used with EXCLUDE_FROM_ALL.
-Call Stack \(most recent call first\):
- CMakeLists.txt:3 \(include\)
-+
-CMake Error at invalid_signature.cmake:18 \(add_library\):
- add_library INTERFACE library may not be used with EXCLUDE_FROM_ALL.
-Call Stack \(most recent call first\):
- CMakeLists.txt:3 \(include\)
-+
CMake Error at invalid_signature.cmake:20 \(add_library\):
add_library GLOBAL option may only be used with IMPORTED libraries.
Call Stack \(most recent call first\):
diff --git a/Tests/RunCMake/InterfaceLibrary/invalid_signature.cmake b/Tests/RunCMake/InterfaceLibrary/invalid_signature.cmake
index 4e53534..2a575b5 100644
--- a/Tests/RunCMake/InterfaceLibrary/invalid_signature.cmake
+++ b/Tests/RunCMake/InterfaceLibrary/invalid_signature.cmake
@@ -1,5 +1,5 @@
-add_library(iface1 INTERFACE empty.cpp)
+
add_library(iface3 STATIC INTERFACE)
add_library(iface4 STATIC INTERFACE empty.cpp)
add_library(iface5 SHARED INTERFACE)
@@ -14,7 +14,7 @@ add_library(iface13 INTERFACE UNKNOWN)
add_library(iface14 INTERFACE ALIAS)
add_library(iface15 ALIAS INTERFACE)
add_library(iface16 INTERFACE INTERFACE)
-add_library(iface17 INTERFACE EXCLUDE_FROM_ALL)
-add_library(iface18 EXCLUDE_FROM_ALL INTERFACE)
+
+
# add_library(iface19 GLOBAL INTERFACE) Tested separately
add_library(iface20 INTERFACE GLOBAL)
diff --git a/Tests/RunCMake/InterfaceLibrary/use_iface.c b/Tests/RunCMake/InterfaceLibrary/use_iface.c
new file mode 100644
index 0000000..66293e4
--- /dev/null
+++ b/Tests/RunCMake/InterfaceLibrary/use_iface.c
@@ -0,0 +1,6 @@
+extern int iface(void);
+
+int main(void)
+{
+ return iface();
+}