Tests/CXXModules: add tests that the C++ std targets don't get exported

These targets are purely internal and should never be exported.
This commit is contained in:
Ben Boeckel
2024-02-26 17:37:10 -05:00
parent 16bf12d1e8
commit b8cc38f3a2
9 changed files with 229 additions and 0 deletions

View File

@@ -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 ()

View File

@@ -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")

View File

@@ -0,0 +1,6 @@
import uses_std;
int main(int argc, char* argv[])
{
return f();
}

View File

@@ -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 "<UNDEF>")
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("<UNDEF>" "IMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES")
check_property("<UNDEF>" "IMPORTED_CXX_MODULES_COMPILE_DEFINITIONS")
check_property("cxx_std_23" "IMPORTED_CXX_MODULES_COMPILE_FEATURES")
check_property("" "IMPORTED_CXX_MODULES_LINK_LIBRARIES")
check_property("<UNDEF>" "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 ()

View File

@@ -0,0 +1,7 @@
export module uses_std;
import std;
export int f()
{
return 0;
}

View File

@@ -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")

View File

@@ -0,0 +1,6 @@
import uses_std;
int main(int argc, char* argv[])
{
return f();
}

View File

@@ -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 "<UNDEF>")
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("<UNDEF>" "IMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES")
check_property("<UNDEF>" "IMPORTED_CXX_MODULES_COMPILE_DEFINITIONS")
check_property("cxx_std_23" "IMPORTED_CXX_MODULES_COMPILE_FEATURES")
check_property("" "IMPORTED_CXX_MODULES_LINK_LIBRARIES")
check_property("<UNDEF>" "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 ()

View File

@@ -0,0 +1,7 @@
export module uses_std;
import std;
export int f()
{
return 0;
}