diff options
author | Brad King <brad.king@kitware.com> | 2024-04-30 14:10:26 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2024-04-30 14:10:34 (GMT) |
commit | c5d8fa3f2c478c4dd3d822a8cede018d2e6697b5 (patch) | |
tree | 825785832175e14dbe9ce398d90cffae12b0eae9 | |
parent | 8f44e1a07b16aba866cc28d635e41aca3744d8c4 (diff) | |
parent | 2041f7c9bf0ccdf1afc1be829d00977a46f280bb (diff) | |
download | CMake-c5d8fa3f2c478c4dd3d822a8cede018d2e6697b5.zip CMake-c5d8fa3f2c478c4dd3d822a8cede018d2e6697b5.tar.gz CMake-c5d8fa3f2c478c4dd3d822a8cede018d2e6697b5.tar.bz2 |
Merge topic 'exported-modules-with-headers' into release-3.29
2041f7c9bf cmGeneratorTarget: add the original target as a COMPILE_ONLY link
051c2110c8 Tests/CXXModules: test exporting modules which include headers
Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !9469
11 files changed, 188 insertions, 0 deletions
diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index a40af8b..f507807 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -8483,6 +8483,10 @@ bool cmGeneratorTarget::DiscoverSyntheticTargets(cmSyntheticTargetCache& cache, // generation. tgt->CopyImportedCxxModulesProperties(model); + tgt->AddLinkLibrary(*mf, + cmStrCat("$<COMPILE_ONLY:", model->GetName(), '>'), + GENERAL_LibraryType); + // Apply usage requirements to the target. usage.ApplyToTarget(tgt); diff --git a/Tests/RunCMake/CXXModules/RunCMakeTest.cmake b/Tests/RunCMake/CXXModules/RunCMakeTest.cmake index 14829a1..6547701 100644 --- a/Tests/RunCMake/CXXModules/RunCMakeTest.cmake +++ b/Tests/RunCMake/CXXModules/RunCMakeTest.cmake @@ -267,6 +267,7 @@ if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION) run_cxx_module_test(export-transitive-targets-build) run_cxx_module_test(export-transitive-modules1-build) run_cxx_module_test(export-transitive-modules-build export-transitive-modules-build "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/export-transitive-modules1-build-build" ) + run_cxx_module_test(export-with-headers-build) if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION AND "bmionly" IN_LIST CMake_TEST_MODULE_COMPILATION) @@ -287,6 +288,9 @@ if ("export_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION) set(test_suffix export-transitive-modules-build) run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DTRANSITIVE_MODULES=1) + + set(test_suffix export-with-headers-build) + run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-build" -DWITH_HEADERS=1) endif () endif () @@ -308,6 +312,7 @@ if ("install_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION) run_cxx_module_test(export-transitive-targets-install) run_cxx_module_test(export-transitive-modules1-install) run_cxx_module_test(export-transitive-modules-install export-transitive-modules-install "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/export-transitive-modules1-install-install" ) + run_cxx_module_test(export-with-headers-install) if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION AND "bmionly" IN_LIST CMake_TEST_MODULE_COMPILATION) @@ -329,6 +334,9 @@ if ("install_bmi" IN_LIST CMake_TEST_MODULE_COMPILATION) set(test_suffix export-transitive-modules-install) run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DTRANSITIVE_MODULES=1) + + set(test_suffix export-with-headers-install) + run_cxx_module_test(import-modules "import-modules-${test_suffix}" "-DCMAKE_PREFIX_PATH=${RunCMake_BINARY_DIR}/examples/${test_suffix}-install" -DWITH_HEADERS=1) set(RunCMake_CXXModules_INSTALL 1) endif () endif () diff --git a/Tests/RunCMake/CXXModules/examples/export-with-headers-build/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/export-with-headers-build/CMakeLists.txt new file mode 100644 index 0000000..e329189 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/export-with-headers-build/CMakeLists.txt @@ -0,0 +1,49 @@ +cmake_minimum_required(VERSION 3.28) +project(export_with_headers CXX) + +include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake") + +add_library(export_with_headers) +target_sources(export_with_headers + PUBLIC + FILE_SET headers TYPE HEADERS + BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" + FILES + "${CMAKE_CURRENT_SOURCE_DIR}/include/subdir/header.h" + PUBLIC + FILE_SET modules TYPE CXX_MODULES + FILES + importable.cxx) +target_compile_features(export_with_headers PUBLIC cxx_std_20) + +install(TARGETS export_with_headers + EXPORT CXXModules + FILE_SET headers + FILE_SET modules DESTINATION ${CMAKE_INSTALL_LIBDIR}/miu) +export(EXPORT CXXModules + NAMESPACE CXXModules:: + FILE "${CMAKE_CURRENT_BINARY_DIR}/export_with_headers-targets.cmake" + CXX_MODULES_DIRECTORY "export_with_headers-cxx-modules") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/export_with_headers-config.cmake" + "include(\"\${CMAKE_CURRENT_LIST_DIR}/export_with_headers-targets.cmake\") +set(\${CMAKE_FIND_PACKAGE_NAME}_FOUND 1) +") + +set(generator + -G "${CMAKE_GENERATOR}") +if (CMAKE_GENERATOR_TOOLSET) + list(APPEND generator + -T "${CMAKE_GENERATOR_TOOLSET}") +endif () +if (CMAKE_GENERATOR_PLATFORM) + list(APPEND generator + -A "${CMAKE_GENERATOR_PLATFORM}") +endif () + +add_test(NAME export_with_headers_build + COMMAND + "${CMAKE_COMMAND}" + "-Dexport_with_headers_DIR=${CMAKE_CURRENT_BINARY_DIR}" + ${generator} + -S "${CMAKE_CURRENT_SOURCE_DIR}/test" + -B "${CMAKE_CURRENT_BINARY_DIR}/test") diff --git a/Tests/RunCMake/CXXModules/examples/export-with-headers-build/importable.cxx b/Tests/RunCMake/CXXModules/examples/export-with-headers-build/importable.cxx new file mode 100644 index 0000000..83d5b50 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/export-with-headers-build/importable.cxx @@ -0,0 +1,14 @@ +module; + +#include <subdir/header.h> + +#ifndef from_subdir_header_h +# error "Define from header found" +#endif + +export module importable; + +export int from_import() +{ + return 0; +} diff --git a/Tests/RunCMake/CXXModules/examples/export-with-headers-build/include/subdir/header.h b/Tests/RunCMake/CXXModules/examples/export-with-headers-build/include/subdir/header.h new file mode 100644 index 0000000..81e8215 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/export-with-headers-build/include/subdir/header.h @@ -0,0 +1,3 @@ +#pragma once + +#define from_subdir_header_h 1 diff --git a/Tests/RunCMake/CXXModules/examples/export-with-headers-build/test/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/export-with-headers-build/test/CMakeLists.txt new file mode 100644 index 0000000..11b0dae --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/export-with-headers-build/test/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.28) +project(cxx_modules_library NONE) + +find_package(export_with_headers REQUIRED) + +if (NOT TARGET CXXModules::export_with_headers) + message(FATAL_ERROR + "Missing imported target") +endif () + +get_property(iface_includes TARGET CXXModules::export_with_headers + PROPERTY INTERFACE_INCLUDE_DIRECTORIES) +set(include_dir "${CMAKE_CURRENT_SOURCE_DIR}") +cmake_path(GET include_dir PARENT_PATH include_dir) +string(APPEND include_dir "/include") +if (NOT iface_includes STREQUAL "${include_dir};$<BUILD_INTERFACE:${include_dir}>") + message(FATAL_ERROR + "Incorrect include interface for CXXModules::export_with_headers:\n ${iface_includes}") +endif () diff --git a/Tests/RunCMake/CXXModules/examples/export-with-headers-install/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/export-with-headers-install/CMakeLists.txt new file mode 100644 index 0000000..f6aefaf --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/export-with-headers-install/CMakeLists.txt @@ -0,0 +1,51 @@ +cmake_minimum_required(VERSION 3.28) +project(export_with_headers CXX) + +include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake") + +add_library(export_with_headers) +target_sources(export_with_headers + PUBLIC + FILE_SET headers TYPE HEADERS + BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" + FILES + "${CMAKE_CURRENT_SOURCE_DIR}/include/subdir/header.h" + PUBLIC + FILE_SET modules TYPE CXX_MODULES + FILES + importable.cxx) +target_compile_features(export_with_headers PUBLIC cxx_std_20) + +install(TARGETS export_with_headers + EXPORT CXXModules + FILE_SET headers + FILE_SET modules DESTINATION lib/miu) +install(EXPORT CXXModules + NAMESPACE CXXModules:: + DESTINATION "lib/cmake/export_with_headers" + FILE "export_with_headers-targets.cmake") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/export_with_headers-config.cmake" + "include(\"\${CMAKE_CURRENT_LIST_DIR}/export_with_headers-targets.cmake\") +set(\${CMAKE_FIND_PACKAGE_NAME}_FOUND 1) +") +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/export_with_headers-config.cmake" + DESTINATION "lib/cmake/export_with_headers") + +set(generator + -G "${CMAKE_GENERATOR}") +if (CMAKE_GENERATOR_TOOLSET) + list(APPEND generator + -T "${CMAKE_GENERATOR_TOOLSET}") +endif () +if (CMAKE_GENERATOR_PLATFORM) + list(APPEND generator + -A "${CMAKE_GENERATOR_PLATFORM}") +endif () + +add_test(NAME export_with_headers_build + COMMAND + "${CMAKE_COMMAND}" + "-Dexport_with_headers_DIR=${CMAKE_INSTALL_PREFIX}/lib/cmake/export_with_headers" + ${generator} + -S "${CMAKE_CURRENT_SOURCE_DIR}/test" + -B "${CMAKE_CURRENT_BINARY_DIR}/test") diff --git a/Tests/RunCMake/CXXModules/examples/export-with-headers-install/importable.cxx b/Tests/RunCMake/CXXModules/examples/export-with-headers-install/importable.cxx new file mode 100644 index 0000000..83d5b50 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/export-with-headers-install/importable.cxx @@ -0,0 +1,14 @@ +module; + +#include <subdir/header.h> + +#ifndef from_subdir_header_h +# error "Define from header found" +#endif + +export module importable; + +export int from_import() +{ + return 0; +} diff --git a/Tests/RunCMake/CXXModules/examples/export-with-headers-install/include/subdir/header.h b/Tests/RunCMake/CXXModules/examples/export-with-headers-install/include/subdir/header.h new file mode 100644 index 0000000..81e8215 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/export-with-headers-install/include/subdir/header.h @@ -0,0 +1,3 @@ +#pragma once + +#define from_subdir_header_h 1 diff --git a/Tests/RunCMake/CXXModules/examples/export-with-headers-install/test/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/export-with-headers-install/test/CMakeLists.txt new file mode 100644 index 0000000..40d66cb --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/export-with-headers-install/test/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.28) +project(cxx_modules_library NONE) + +find_package(export_with_headers REQUIRED) + +if (NOT TARGET CXXModules::export_with_headers) + message(FATAL_ERROR + "Missing imported target") +endif () + +get_property(iface_includes TARGET CXXModules::export_with_headers + PROPERTY INTERFACE_INCLUDE_DIRECTORIES) +set(include_dir "${export_with_headers_DIR}") +cmake_path(GET include_dir PARENT_PATH include_dir) +cmake_path(GET include_dir PARENT_PATH include_dir) +cmake_path(GET include_dir PARENT_PATH include_dir) +string(APPEND include_dir "/include") +if (NOT iface_includes STREQUAL "$<BUILD_INTERFACE:${include_dir}>") + message(FATAL_ERROR + "Incorrect include interface for CXXModules::export_with_headers:\n ${iface_includes}") +endif () diff --git a/Tests/RunCMake/CXXModules/examples/import-modules/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/import-modules/CMakeLists.txt index c2bbb2e..494b91a 100644 --- a/Tests/RunCMake/CXXModules/examples/import-modules/CMakeLists.txt +++ b/Tests/RunCMake/CXXModules/examples/import-modules/CMakeLists.txt @@ -15,6 +15,8 @@ elseif (TRANSITIVE_TARGETS) set(package_name "export_transitive_targets") elseif (TRANSITIVE_MODULES) set(package_name "export_transitive_modules") +elseif (WITH_HEADERS) + set(package_name "export_with_headers") else () set(package_name "export_interfaces") endif () |