Skip to content

Commit 3c0ea30

Browse files
committed
Adjust module interface file extension for different compilers
- flyby: build HPX full using C++ modules from HPX core - flyby: modify CMake build system to properly propagate C++ module settings - flyby: fixed a couple of problems with existing C++ module integration Signed-off-by: Hartmut Kaiser <[email protected]>
1 parent 82aa799 commit 3c0ea30

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+224
-94
lines changed

cmake/HPX_AddModule.cmake

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ include(HPX_ExportTargets)
1010
include(HPX_Message)
1111
include(HPX_Option)
1212
include(HPX_PrintSummary)
13+
include(HPX_CXXModules)
1314

1415
function(add_hpx_module libname modulename)
1516
# Retrieve arguments
@@ -265,7 +266,9 @@ function(add_hpx_module libname modulename)
265266
)
266267

267268
set(lib_module_basedir ${PROJECT_BINARY_DIR}/libs/${lib}/${modulename})
268-
set(lib_module_file "${lib_module_basedir}/hpx_${modulename}.ixx")
269+
set(lib_module_file
270+
"${lib_module_basedir}/hpx_${modulename}${HPX_MODULE_INTERFACE_EXTENSION}"
271+
)
269272
configure_file(
270273
"${HPX_SOURCE_DIR}/cmake/templates/hpx.ixx.in" ${lib_module_file}
271274
@ONLY
@@ -504,10 +507,17 @@ function(add_hpx_module libname modulename)
504507
hpx_${modulename} PRIVATE HPX_${libname_upper}_EXPORTS
505508
)
506509

507-
# This is a temporary solution until all of HPX has been modularized as it
508-
# enables using header files from HPX for compiling this module.
509510
if("${libname}" STREQUAL "full")
511+
# This is a temporary solution until all of HPX has been modularized as it
512+
# enables using header files from HPX for compiling this module.
510513
target_include_directories(hpx_${modulename} PRIVATE ${HPX_SOURCE_DIR})
514+
515+
# We build the full library module using the C++ module definitions for
516+
# core.
517+
if(HPX_WITH_CXX_MODULES)
518+
hpx_configure_module_consumer(hpx_${modulename} hpx_core_module_if)
519+
target_link_libraries(hpx_${modulename} PRIVATE hpx_core_module_if)
520+
endif()
511521
endif()
512522

513523
add_hpx_source_group(

cmake/HPX_CXXModules.cmake

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,34 @@
77
include(HPX_AddCompileFlag)
88
include(HPX_Message)
99

10+
if(NOT HPX_WITH_CXX_MODULES)
11+
return()
12+
endif()
13+
14+
# Unfortunately, different compilers expect different file extensions for the
15+
# C++ module definition files.
16+
if(MSVC)
17+
set(HPX_MODULE_INTERFACE_EXTENSION ".ixx")
18+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES
19+
"AppleClang"
20+
)
21+
set(HPX_MODULE_INTERFACE_EXTENSION ".cppm")
22+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
23+
set(HPX_MODULE_INTERFACE_EXTENSION ".cxx")
24+
else()
25+
hpx_error(
26+
"C++ modules are not supported for the used compiler ('${CMAKE_CXX_COMPILER_ID}')"
27+
)
28+
endif()
29+
1030
# hpx_configure_module_producer(<producer> [MODULE_OUT_DIR <dir>])
1131
#
1232
# * Ensures a stable module output dir for producer target
1333
# * Adds compiler flags to write module cache there (Clang/GCC)
14-
# * Creates a target '<producer>_module' for consumers to link to
34+
# * Creates an interface target '<producer>_if' for consumers to link to
1535
function(hpx_configure_module_producer producer)
1636
if(NOT TARGET ${producer})
17-
hpx_error("configure_module_producer: target '${producer}' not found")
37+
hpx_error("hpx_configure_module_producer: target '${producer}' not found")
1838
endif()
1939

2040
# parse optional args
@@ -35,13 +55,16 @@ function(hpx_configure_module_producer producer)
3555
if(NOT TARGET ${_iface})
3656
add_library(${_iface} INTERFACE)
3757
target_link_libraries(${_iface} INTERFACE ${producer})
38-
# target_include_directories(${_iface} INTERFACE "${_moddir}")
3958
endif()
4059

4160
# Set a property so consumers can query the BMI directory via
4261
# get_target_property.
43-
set_target_properties(${producer} PROPERTIES EXPORT_MODULE_DIR "${_moddir}")
44-
set_target_properties(${producer} PROPERTIES CXX_SCAN_FOR_MODULES On)
62+
set_target_properties(
63+
${_iface} PROPERTIES INTERFACE_EXPORT_MODULE_DIR "${_moddir}"
64+
)
65+
66+
# Make sure consumers scan for the BMI
67+
set_target_properties(${_iface} PROPERTIES INTERFACE_CXX_SCAN_FOR_MODULES On)
4568

4669
if(MSVC)
4770
# MSVC: CMake/MSVC handle IFCs automatically; create a target for
@@ -55,32 +78,71 @@ function(hpx_configure_module_producer producer)
5578
)
5679
# Clang common flags
5780
target_compile_options(${producer} PRIVATE "-fmodule-output=${_moddir}")
58-
target_compile_options(
59-
${_iface} INTERFACE "-fprebuilt-module-path=${_moddir}"
60-
)
6181
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
62-
# GCC: try a few likely flags depending on version Prefer flags used in GCC
63-
# 11+: -fmodules-ts, -fmodules-cache-path (or -fmodule-cache-path)
82+
# GCC: modern flags
6483
hpx_add_target_compile_option_if_available(
6584
${producer} PRIVATE "-fmodule-output=${_moddir}" RESULT ok
6685
)
6786
if(NOT ok)
6887
hpx_error(
69-
"configure_module_producer: the used version of gcc does not support '-fmodule-output'"
70-
)
71-
endif()
72-
hpx_add_target_compile_option_if_available(
73-
${_iface} INTERFACE "-fprebuilt-module-path=${_moddir}" RESULT ok
74-
)
75-
if(NOT ok)
76-
hpx_error(
77-
"configure_module_producer: the used version of gcc does not support '-fprebuilt-module-path='"
88+
"hpx_configure_module_producer: the used version of gcc does not support '-fmodule-output'"
7889
)
7990
endif()
8091
else()
8192
hpx_warn(
82-
"configure_module_producer: unknown compiler '${CMAKE_CXX_COMPILER_ID}'; "
83-
"exposing CXX_MODULE_OUTPUT_DIRECTORY='${_moddir}' for manual handling"
93+
"hpx_configure_module_producer: unknown compiler '${CMAKE_CXX_COMPILER_ID}'; "
94+
"exposing EXPORT_MODULE_DIR='${_moddir}' for manual handling"
8495
)
8596
endif()
8697
endfunction()
98+
99+
# hpx_configure_module_consumer(<consumer> <producer>])
100+
#
101+
# * propagates module-related properties from producer interface target
102+
# * sets necessary consumer compiler flags for clang and gcc
103+
function(hpx_configure_module_consumer consumer producer)
104+
if(NOT TARGET ${consumer})
105+
hpx_error("hpx_configure_module_consumer: target '${consumer}' not found")
106+
endif()
107+
if(NOT TARGET ${producer})
108+
hpx_error("hpx_configure_module_consumer: target '${producer}' not found")
109+
endif()
110+
111+
target_link_libraries(${consumer} PUBLIC ${producer})
112+
get_target_property(_scan ${producer} INTERFACE_CXX_SCAN_FOR_MODULES)
113+
if(_scan)
114+
set_target_properties(${consumer} PROPERTIES CXX_SCAN_FOR_MODULES ${_scan})
115+
endif()
116+
117+
get_target_property(_module_dir ${producer} INTERFACE_EXPORT_MODULE_DIR)
118+
if(_module_dir)
119+
if(MSVC)
120+
return()
121+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID
122+
MATCHES "AppleClang"
123+
)
124+
target_compile_options(
125+
${consumer} PRIVATE "-fprebuilt-module-path=${_module_dir}"
126+
)
127+
get_target_property(_type ${consumer} TYPE)
128+
if((_type STREQUAL "SHARED_LIBRARY") OR (_type STREQUAL "EXECUTABLE"))
129+
target_link_options(${consumer} PRIVATE "-fuse-ld=lld")
130+
target_link_options(${consumer} PRIVATE "-Wl,--error-limit=0")
131+
endif()
132+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
133+
hpx_add_target_compile_option_if_available(
134+
${consumer} PRIVATE "-fprebuilt-module-path=${_module_dir}" RESULT ok
135+
)
136+
if(NOT ok)
137+
hpx_error(
138+
"hpx_configure_module_consumer: the used version of clang does not "
139+
"support '-fprebuilt-module-path='"
140+
)
141+
endif()
142+
else()
143+
hpx_warn(
144+
"hpx_configure_module_consumer: unknown compiler '${CMAKE_CXX_COMPILER_ID}'"
145+
)
146+
endif()
147+
endif()
148+
endfunction()

cmake/HPX_SetupTarget.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# Distributed under the Boost Software License, Version 1.0. (See accompanying
77
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
88

9+
include(HPX_CXXModules)
10+
911
cmake_policy(PUSH)
1012

1113
hpx_set_cmake_policy(CMP0054 NEW)
@@ -224,7 +226,7 @@ function(hpx_setup_target target)
224226
set_target_properties(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON)
225227

226228
if(HPX_WITH_CXX_MODULES AND ${target}_SCAN_FOR_MODULES)
227-
set_target_properties(${target} PROPERTIES CXX_SCAN_FOR_MODULES ON)
229+
hpx_configure_module_consumer(${target} hpx_core_module_if)
228230
else()
229231
set_target_properties(${target} PROPERTIES CXX_SCAN_FOR_MODULES OFF)
230232
target_compile_definitions(

cmake/templates/hpx.ixx.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (c) 2025 Haokun Wu
2+
// Copyright (c) 2025 Hartmut Kaiser
23
//
34
// SPDX-License-Identifier: BSL-1.0
45
// Distributed under the Boost Software License, Version 1.0. (See accompanying

cmake/templates/std_headers.hpp.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include <hpx/config/compiler_specific.hpp>
1414

1515
@cxx_standard_headers@
16+
// Some standard headers include the following headers.
17+
#include <numeric>
18+
1619
#if defined(HPX_HAVE_CXX20_COROUTINES)
1720
# if defined(__has_include)
1821
# if __has_include(<coroutine>)

init/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# Copyright (c) 2007-2023 Hartmut Kaiser
1+
# Copyright (c) 2007-2025 Hartmut Kaiser
22
# Copyright (c) 2011 Bryce Lelbach
33
# Copyright (c) 2018 Nikunj Gupta
44
#
55
# SPDX-License-Identifier: BSL-1.0
66
# Distributed under the Boost Software License, Version 1.0. (See accompanying
77
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
88

9+
include(HPX_CXXModules)
10+
911
set(hpx_init_HEADERS
1012
""
1113
CACHE INTERNAL "Headers for libhpx_init." FORCE
@@ -57,8 +59,7 @@ target_include_directories(
5759
$<INSTALL_INTERFACE:include>
5860
)
5961
if(HPX_WITH_CXX_MODULES)
60-
target_link_libraries(hpx_init PRIVATE hpx_core_module_if)
61-
set_target_properties(hpx_init PROPERTIES CXX_SCAN_FOR_MODULES ON)
62+
hpx_configure_module_consumer(hpx_init hpx_core_module_if)
6263
endif()
6364

6465
set_property(TARGET hpx_init PROPERTY FOLDER "Core")

libs/CMakeLists.txt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,23 @@ foreach(lib ${HPX_LIBS})
321321
# creation can move here as well.
322322
if(${lib} STREQUAL "full")
323323
add_subdirectory(${lib})
324+
if(HPX_WITH_CXX_MODULES)
325+
hpx_configure_module_consumer(hpx_${lib} hpx_core_module_if)
326+
endif()
324327
else()
325328
add_library(hpx_${lib} ${hpx_library_link_mode} src/empty.cpp)
326329
target_compile_definitions(hpx_${lib} PRIVATE HPX_${uppercase_lib}_EXPORTS)
330+
set_target_properties(hpx_${lib} PROPERTIES POSITION_INDEPENDENT_CODE ON)
331+
332+
if(HPX_WITH_CXX_MODULES)
333+
add_library(hpx_${lib}_module STATIC)
334+
set_target_properties(
335+
hpx_${lib}_module PROPERTIES POSITION_INDEPENDENT_CODE ON
336+
)
337+
338+
# This creates an interface target hpx_${lib}_module_if
339+
hpx_configure_module_producer(hpx_${lib}_module)
340+
endif()
327341

328342
add_subdirectory(${lib})
329343

@@ -350,7 +364,9 @@ foreach(lib ${HPX_LIBS})
350364
endforeach()
351365

352366
set(lib_module_basedir ${PROJECT_BINARY_DIR}/libs/${lib})
353-
set(lib_module_file "${lib_module_basedir}/hpx_${lib}.ixx")
367+
set(lib_module_file
368+
"${lib_module_basedir}/hpx_${lib}${HPX_MODULE_INTERFACE_EXTENSION}"
369+
)
354370
set(modulename_upper "${uppercase_lib}")
355371
configure_file(
356372
"${HPX_SOURCE_DIR}/cmake/templates/hpx.ixx.in" ${lib_module_file} @ONLY
@@ -364,17 +380,13 @@ foreach(lib ${HPX_LIBS})
364380
${std_header_file} @ONLY
365381
)
366382

367-
add_library(hpx_${lib}_module STATIC)
368383
target_link_libraries(
369384
hpx_${lib}_module
370385
PUBLIC hpx_${lib} hpx_public_flags
371386
PRIVATE hpx_private_flags
372387
)
373388
set_target_properties(hpx_${lib}_module PROPERTIES FOLDER "Core")
374389

375-
# This creates an interface target hpx_${lib}_module_if
376-
hpx_configure_module_producer(hpx_${lib}_module)
377-
378390
add_hpx_source_group(
379391
NAME hpx_${lib}_module
380392
ROOT ${lib_module_basedir}

libs/core/assertion/include/hpx/assertion/source_location.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace hpx {
4545
/// efficiently.
4646
/// It is unspecified whether the copy/move constructors and the copy/move
4747
/// assignment operators of \a source_location are trivial and/or constexpr.
48-
HPX_CXX_EXPORT struct source_location
48+
HPX_CXX_EXPORT HPX_CXX_EXTERN struct source_location
4949
{
5050
char const* filename;
5151
std::uint_least32_t line_number;

libs/core/assertion/src/assertion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ namespace hpx::assertion {
3333

3434
namespace detail {
3535

36-
HPX_CORE_EXPORT void handle_assert(hpx::source_location const& loc,
37-
char const* expr, std::string const& msg) noexcept
36+
void handle_assert(hpx::source_location const& loc, char const* expr,
37+
std::string const& msg) noexcept
3838
{
3939
if (get_handler() == nullptr)
4040
{

libs/core/config/include/hpx/config/export_definitions.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@
5353
///////////////////////////////////////////////////////////////////////////////
5454
// C++20 module export definitions
5555
#if defined(HPX_COMPILE_BMI)
56-
# if defined(HPX_HAVE_ELF_HIDDEN_VISIBILITY)
57-
# undef HPX_CORE_EXPORT
58-
# define HPX_CORE_EXPORT /* empty */
59-
# endif
6056
# define HPX_CXX_EXPORT export
6157
# define HPX_CXX_EXTERN extern "C++"
6258
# define HPX_EXTERN /* empty */
@@ -122,7 +118,7 @@
122118
#else
123119

124120
#if !defined(HPX_HAVE_CXX_MODULES) || defined(HPX_CORE_EXPORTS) || \
125-
defined(HPX_FULL_EXPORTS) || defined(HPX_BINARY_DOESNT_USE_CXX_MODULES)
121+
defined(HPX_BINARY_DOESNT_USE_CXX_MODULES)
126122
#undef HPX_COMPILE_WITH_MODULES
127123
#else
128124
#define HPX_COMPILE_WITH_MODULES

0 commit comments

Comments
 (0)