From b8cc38f3a28190d39ef56f936f5d909688a8374e Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 26 Feb 2024 17:37:10 -0500 Subject: Tests/CXXModules: add tests that the C++ std targets don't get exported These targets are purely internal and should never be exported. --- Tests/RunCMake/CXXModules/RunCMakeTest.cmake | 7 +++ .../import-std-not-in-export-build/CMakeLists.txt | 51 ++++++++++++++++++++ .../import-std-not-in-export-build/main.cxx | 6 +++ .../test/CMakeLists.txt | 45 ++++++++++++++++++ .../import-std-not-in-export-build/uses-std.cxx | 7 +++ .../CMakeLists.txt | 55 ++++++++++++++++++++++ .../import-std-not-in-export-install/main.cxx | 6 +++ .../test/CMakeLists.txt | 45 ++++++++++++++++++ .../import-std-not-in-export-install/uses-std.cxx | 7 +++ 9 files changed, 229 insertions(+) create mode 100644 Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/CMakeLists.txt create mode 100644 Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/main.cxx create mode 100644 Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/test/CMakeLists.txt create mode 100644 Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/uses-std.cxx create mode 100644 Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/CMakeLists.txt create mode 100644 Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/main.cxx create mode 100644 Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/test/CMakeLists.txt create mode 100644 Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/uses-std.cxx diff --git a/Tests/RunCMake/CXXModules/RunCMakeTest.cmake b/Tests/RunCMake/CXXModules/RunCMakeTest.cmake index 2fd4c46..84e2085 100644 --- a/Tests/RunCMake/CXXModules/RunCMakeTest.cmake +++ b/Tests/RunCMake/CXXModules/RunCMakeTest.cmake @@ -243,6 +243,13 @@ if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION) set(RunCMake_CXXModules_NO_TEST 1) run_cxx_module_test(import-std-no-std-property) unset(RunCMake_CXXModules_NO_TEST) + + if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION) + run_cxx_module_test(import-std-not-in-export-build) + + set(RunCMake_CXXModules_INSTALL 1) + run_cxx_module_test(import-std-not-in-export-install) + unset(RunCMake_CXXModules_INSTALL) endif () endif () diff --git a/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/CMakeLists.txt new file mode 100644 index 0000000..b7ab279 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/CMakeLists.txt @@ -0,0 +1,51 @@ +cmake_minimum_required(VERSION 3.29) +project(cxx_modules_import_std_not_in_export CXX) + +include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake") + +set(CMAKE_CXX_MODULE_STD 1) + +add_library(import_std_not_in_export) +target_sources(import_std_not_in_export + PUBLIC + FILE_SET use_std TYPE CXX_MODULES FILES + uses-std.cxx) +target_compile_features(import_std_not_in_export PUBLIC cxx_std_23) + +add_executable(main + main.cxx) +target_link_libraries(main PRIVATE import_std_not_in_export) + +install(TARGETS import_std_not_in_export + EXPORT export + ARCHIVE DESTINATION "lib" + FILE_SET use_std DESTINATION "lib/cxx/miu") +export(EXPORT export + NAMESPACE CXXModules:: + FILE "${CMAKE_CURRENT_BINARY_DIR}/import_std_not_in_export-targets.cmake") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/import_std_not_in_export-config.cmake" + "include(\"\${CMAKE_CURRENT_LIST_DIR}/import_std_not_in_export-targets.cmake\") +set(\${CMAKE_FIND_PACKAGE_NAME}_FOUND 1) +") + +add_test(NAME main COMMAND main) + +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 import_std_not_in_export_build + COMMAND + "${CMAKE_COMMAND}" + "-Dexpected_dir=${CMAKE_CURRENT_SOURCE_DIR}" + "-Dimport_std_not_in_export_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/import-std-not-in-export-build/main.cxx b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/main.cxx new file mode 100644 index 0000000..a6dd8d8 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/main.cxx @@ -0,0 +1,6 @@ +import uses_std; + +int main(int argc, char* argv[]) +{ + return f(); +} diff --git a/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/test/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/test/CMakeLists.txt new file mode 100644 index 0000000..4e994d2 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/test/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.29) +project(cxx_modules_library NONE) + +find_package(import_std_not_in_export REQUIRED) + +if (NOT TARGET CXXModules::import_std_not_in_export) + message(FATAL_ERROR + "Missing imported target") +endif () + +function (check_property expected property) + get_property(actual TARGET CXXModules::import_std_not_in_export + PROPERTY "${property}") + if (NOT DEFINED actual) + if (NOT expected STREQUAL "") + message(SEND_ERROR + "Mismatch for ${property}:\n expected: ${expected}\n actual: NOT-DEFINED") + endif () + elseif (NOT actual STREQUAL expected) + message(SEND_ERROR + "Mismatch for ${property}:\n expected: ${expected}\n actual: ${actual}") + endif () +endfunction () + +check_property("" "IMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES") +check_property("" "IMPORTED_CXX_MODULES_COMPILE_DEFINITIONS") +check_property("cxx_std_23" "IMPORTED_CXX_MODULES_COMPILE_FEATURES") +check_property("" "IMPORTED_CXX_MODULES_LINK_LIBRARIES") +check_property("" "INTERFACE_LINK_LIBRARIES") +check_property("1" "CXX_MODULE_STD") + +# Extract the export-dependent targets from the export file. +file(STRINGS "${import_std_not_in_export_DIR}/import_std_not_in_export-targets.cmake" usage_dependent_targets + REGEX "foreach._target ") +# Rudimentary argument splitting. +string(REPLACE " " ";" usage_dependent_targets "${usage_dependent_targets}") +# Remove exported "target" names. +list(FILTER usage_dependent_targets EXCLUDE REGEX "CXXModules::") +# Strip quotes. +string(REPLACE "\"" "" usage_dependent_targets "${usage_dependent_targets}") + +if ("__CMAKE::CXX23" IN_LIST usage_dependent_targets) + message(SEND_ERROR + "The main export requires the '__CMAKE::CXX23' target") +endif () diff --git a/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/uses-std.cxx b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/uses-std.cxx new file mode 100644 index 0000000..aa45dd1 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-build/uses-std.cxx @@ -0,0 +1,7 @@ +export module uses_std; +import std; + +export int f() +{ + return 0; +} diff --git a/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/CMakeLists.txt new file mode 100644 index 0000000..8b9295d --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/CMakeLists.txt @@ -0,0 +1,55 @@ +cmake_minimum_required(VERSION 3.29) +project(cxx_modules_import_std_not_in_export CXX) + +include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake") + +set(CMAKE_CXX_MODULE_STD 1) + +add_library(import_std_not_in_export) +target_sources(import_std_not_in_export + PUBLIC + FILE_SET use_std TYPE CXX_MODULES FILES + uses-std.cxx) +target_compile_features(import_std_not_in_export PUBLIC cxx_std_23) + +add_executable(main + main.cxx) +target_link_libraries(main PRIVATE import_std_not_in_export) + +install(TARGETS import_std_not_in_export + EXPORT export + ARCHIVE DESTINATION "lib" + FILE_SET use_std DESTINATION "lib/cxx/miu") +install( + EXPORT export + NAMESPACE CXXModules:: + DESTINATION "lib/cmake/import_std_not_in_export" + FILE "import_std_not_in_export-targets.cmake") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/import_std_not_in_export-config.cmake" + "include(\"\${CMAKE_CURRENT_LIST_DIR}/import_std_not_in_export-targets.cmake\") +set(\${CMAKE_FIND_PACKAGE_NAME}_FOUND 1) +") +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/import_std_not_in_export-config.cmake" + DESTINATION "lib/cmake/import_std_not_in_export") + +add_test(NAME main COMMAND main) + +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 import_std_not_in_export_build + COMMAND + "${CMAKE_COMMAND}" + "-Dexpected_dir=${CMAKE_INSTALL_PREFIX}/lib/cxx/miu" + "-Dimport_std_not_in_export_DIR=${CMAKE_INSTALL_PREFIX}/lib/cmake/import_std_not_in_export" + ${generator} + -S "${CMAKE_CURRENT_SOURCE_DIR}/test" + -B "${CMAKE_CURRENT_BINARY_DIR}/test") diff --git a/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/main.cxx b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/main.cxx new file mode 100644 index 0000000..a6dd8d8 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/main.cxx @@ -0,0 +1,6 @@ +import uses_std; + +int main(int argc, char* argv[]) +{ + return f(); +} diff --git a/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/test/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/test/CMakeLists.txt new file mode 100644 index 0000000..4e994d2 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/test/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.29) +project(cxx_modules_library NONE) + +find_package(import_std_not_in_export REQUIRED) + +if (NOT TARGET CXXModules::import_std_not_in_export) + message(FATAL_ERROR + "Missing imported target") +endif () + +function (check_property expected property) + get_property(actual TARGET CXXModules::import_std_not_in_export + PROPERTY "${property}") + if (NOT DEFINED actual) + if (NOT expected STREQUAL "") + message(SEND_ERROR + "Mismatch for ${property}:\n expected: ${expected}\n actual: NOT-DEFINED") + endif () + elseif (NOT actual STREQUAL expected) + message(SEND_ERROR + "Mismatch for ${property}:\n expected: ${expected}\n actual: ${actual}") + endif () +endfunction () + +check_property("" "IMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES") +check_property("" "IMPORTED_CXX_MODULES_COMPILE_DEFINITIONS") +check_property("cxx_std_23" "IMPORTED_CXX_MODULES_COMPILE_FEATURES") +check_property("" "IMPORTED_CXX_MODULES_LINK_LIBRARIES") +check_property("" "INTERFACE_LINK_LIBRARIES") +check_property("1" "CXX_MODULE_STD") + +# Extract the export-dependent targets from the export file. +file(STRINGS "${import_std_not_in_export_DIR}/import_std_not_in_export-targets.cmake" usage_dependent_targets + REGEX "foreach._target ") +# Rudimentary argument splitting. +string(REPLACE " " ";" usage_dependent_targets "${usage_dependent_targets}") +# Remove exported "target" names. +list(FILTER usage_dependent_targets EXCLUDE REGEX "CXXModules::") +# Strip quotes. +string(REPLACE "\"" "" usage_dependent_targets "${usage_dependent_targets}") + +if ("__CMAKE::CXX23" IN_LIST usage_dependent_targets) + message(SEND_ERROR + "The main export requires the '__CMAKE::CXX23' target") +endif () diff --git a/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/uses-std.cxx b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/uses-std.cxx new file mode 100644 index 0000000..aa45dd1 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/import-std-not-in-export-install/uses-std.cxx @@ -0,0 +1,7 @@ +export module uses_std; +import std; + +export int f() +{ + return 0; +} -- cgit v0.12