瀏覽代碼

Tests/CXXModules: test exporting modules which include headers

Test that headers that are part of the same target are available to
modules in the target itself.
Ben Boeckel 1 年之前
父節點
當前提交
051c2110c8

+ 8 - 0
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 ()

+ 49 - 0
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")

+ 14 - 0
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;
+}

+ 3 - 0
Tests/RunCMake/CXXModules/examples/export-with-headers-build/include/subdir/header.h

@@ -0,0 +1,3 @@
+#pragma once
+
+#define from_subdir_header_h 1

+ 19 - 0
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 ()

+ 51 - 0
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")

+ 14 - 0
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;
+}

+ 3 - 0
Tests/RunCMake/CXXModules/examples/export-with-headers-install/include/subdir/header.h

@@ -0,0 +1,3 @@
+#pragma once
+
+#define from_subdir_header_h 1

+ 21 - 0
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 ()

+ 2 - 0
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 ()