Skip to content

Commit ccbec5a

Browse files
committed
Fix external C++ modules package consumers
1 parent 2f9325d commit ccbec5a

4 files changed

Lines changed: 53 additions & 6 deletions

File tree

cmake/HPX_CXXModules.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ function(hpx_configure_module_consumer consumer producer)
116116
hpx_error("hpx_configure_module_consumer: target '${producer}' not found")
117117
endif()
118118

119+
# Imported module metadata is only picked up from direct link dependencies.
120+
# Link the underlying module target directly when the producer follows the
121+
# '<module>_if' wrapper pattern.
122+
if(producer MATCHES "_if$")
123+
string(REGEX REPLACE "_if$" "" _producer_target "${producer}")
124+
if(TARGET ${_producer_target})
125+
target_link_libraries(${consumer} PRIVATE ${_producer_target})
126+
endif()
127+
endif()
128+
119129
target_link_libraries(${consumer} PRIVATE ${producer})
120130
get_target_property(_scan ${producer} INTERFACE_CXX_SCAN_FOR_MODULES)
121131
if(_scan)

cmake/HPX_GeneratePackage.cmake

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ write_basic_package_version_file(
1919
COMPATIBILITY AnyNewerVersion
2020
)
2121

22-
# Export HPXInternalTargets in the build directory
22+
# Export HPXInternalTargets in the build directory. Use the EXPORT
23+
# signature so CMake also generates the per-target C++ module metadata files.
2324
export(
24-
TARGETS ${HPX_EXPORT_INTERNAL_TARGETS}
25+
EXPORT HPXInternalTargets
2526
NAMESPACE HPXInternal::
2627
FILE "${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/${HPX_PACKAGE_NAME}/HPXInternalTargets.cmake"
28+
CXX_MODULES_DIRECTORY cxx-modules
2729
)
2830

2931
# Export HPXInternalTargets in the install directory
@@ -32,14 +34,17 @@ install(
3234
NAMESPACE HPXInternal::
3335
FILE HPXInternalTargets.cmake
3436
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${HPX_PACKAGE_NAME}
37+
CXX_MODULES_DIRECTORY cxx-modules
3538
COMPONENT cmake
3639
)
3740

38-
# Export HPXTargets in the build directory
41+
# Export HPXTargets in the build directory. Use the EXPORT signature so
42+
# CMake also generates the per-target C++ module metadata files.
3943
export(
40-
TARGETS ${HPX_EXPORT_TARGETS}
44+
EXPORT HPXTargets
4145
NAMESPACE HPX::
4246
FILE "${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/${HPX_PACKAGE_NAME}/HPXTargets.cmake"
47+
CXX_MODULES_DIRECTORY cxx-modules
4348
)
4449

4550
# Add aliases with the namespace for use within HPX
@@ -57,6 +62,7 @@ install(
5762
NAMESPACE HPX::
5863
FILE HPXTargets.cmake
5964
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${HPX_PACKAGE_NAME}
65+
CXX_MODULES_DIRECTORY cxx-modules
6066
COMPONENT cmake
6167
)
6268

examples/gtest_emulation/CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ if(EXISTS "${HPX_DIR}")
1414

1515
if(HPX_WITH_CXX_MODULES)
1616
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
17+
18+
# CMake only propagates imported C++ module metadata from targets that are
19+
# linked directly by the consumer. The exported HPXInternal module targets
20+
# are therefore linked explicitly for the external build tests.
21+
set(hpx_cxx_module_targets)
22+
if(TARGET HPXInternal::hpx_core_module)
23+
list(APPEND hpx_cxx_module_targets HPXInternal::hpx_core_module)
24+
endif()
25+
if(TARGET HPXInternal::hpx_full_module)
26+
list(APPEND hpx_cxx_module_targets HPXInternal::hpx_full_module)
27+
endif()
1728
endif()
1829

1930
# Add a static library which contains a main to emulate gtest_main
@@ -27,7 +38,11 @@ if(EXISTS "${HPX_DIR}")
2738

2839
# Test with the main function in a separate static library
2940
add_executable(hpx_main_ext_main hpx_main_ext_main.cpp)
30-
target_link_libraries(hpx_main_ext_main PRIVATE hpx_helper_interface)
41+
# Keep the helper interface for link order, but link module targets directly
42+
# to the executable so CMake can see the imported BMI metadata.
43+
target_link_libraries(
44+
hpx_main_ext_main PRIVATE hpx_helper_interface ${hpx_cxx_module_targets}
45+
)
3146

3247
enable_testing()
3348
add_test(hello_world_test hpx_main_ext_main)

examples/hello_world_component/CMakeLists.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ if(EXISTS "${HPX_DIR}")
1717

1818
if(HPX_WITH_CXX_MODULES)
1919
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
20+
21+
# CMake only propagates imported C++ module metadata from targets that are
22+
# linked directly by the consumer. The exported HPXInternal module targets
23+
# are therefore linked explicitly for the external build tests.
24+
set(hpx_cxx_module_targets)
25+
if(TARGET HPXInternal::hpx_core_module)
26+
list(APPEND hpx_cxx_module_targets HPXInternal::hpx_core_module)
27+
endif()
28+
if(TARGET HPXInternal::hpx_full_module)
29+
list(APPEND hpx_cxx_module_targets HPXInternal::hpx_full_module)
30+
endif()
2031
endif()
2132

2233
add_executable(hello_world_client hello_world_client.cpp)
@@ -26,11 +37,15 @@ if(EXISTS "${HPX_DIR}")
2637
if(HPX_WITH_DISTRIBUTED_RUNTIME)
2738
target_link_libraries(
2839
hello_world_component PUBLIC HPX::hpx HPX::iostreams_component
40+
${hpx_cxx_module_targets}
2941
)
3042
target_link_libraries(hello_world_component PRIVATE HPX::component)
3143
target_link_libraries(hello_world_client PRIVATE hello_world_component)
3244
endif()
33-
target_link_libraries(hello_world_client PRIVATE HPX::hpx HPX::wrap_main)
45+
target_link_libraries(
46+
hello_world_client PRIVATE HPX::hpx HPX::wrap_main
47+
${hpx_cxx_module_targets}
48+
)
3449

3550
# We still support not linking to HPX::wrap_main when
3651
# HPX_WITH_DYNAMIC_HPX_MAIN=OFF for legacy use. This can only be done using
@@ -44,6 +59,7 @@ if(EXISTS "${HPX_DIR}")
4459

4560
target_link_libraries(
4661
hello_world_client_only_hpx_init PRIVATE hello_world_component
62+
${hpx_cxx_module_targets}
4763
)
4864
endif()
4965
elseif("${SETUP_TYPE}" STREQUAL "MACROS")

0 commit comments

Comments
 (0)