Skip to content

Commit fc0e9c8

Browse files
authored
[libc++][modules] Re-add build dir CMakeLists.txt. (#81370)
This CMakeLists.txt is used to build modules without build system support. This was removed in d06ae33. This is used in the documentation how to use modules. Made some minor changes to make it work with the std.compat module using the std module. Note the CMakeLists.txt in the build dir should be removed once build system support is generally available.
1 parent 9dd2c59 commit fc0e9c8

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

libcxx/docs/Modules.rst

+4
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,13 @@ Building this project is done with the following steps, assuming the files
218218
219219
$ mkdir build
220220
$ cmake -G Ninja -S . -B build -DCMAKE_CXX_COMPILER=<path-to-compiler> -DLIBCXX_BUILD=<build>
221+
$ ninja -j1 std -C build
221222
$ ninja -C build
222223
$ build/main
223224
225+
.. note:: The ``std`` dependencies of ``std.compat`` is not always resolved when
226+
building the ``std`` target using multiple jobs.
227+
224228
.. warning:: ``<path-to-compiler>`` should point point to the real binary and
225229
not to a symlink.
226230

libcxx/modules/CMakeLists.txt

+20
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ set(LIBCXX_MODULE_STD_COMPAT_SOURCES
137137
std.compat/cwctype.inc
138138
)
139139

140+
# TODO MODULES the CMakeLists.txt in the build directory is only temporary.
141+
# This allows using as available in the build directory. Once build systems
142+
# have proper support for the installed files this will be removed.
143+
if ("${LIBCXX_GENERATED_INCLUDE_DIR}" STREQUAL "${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}")
144+
# This typically happens when the target is not installed.
145+
set(LIBCXX_CONFIGURED_INCLUDE_DIRS "${LIBCXX_GENERATED_INCLUDE_DIR}")
146+
else()
147+
# It's important that the arch directory be included first so that its header files
148+
# which interpose on the default include dir be included instead of the default ones.
149+
set(LIBCXX_CONFIGURED_INCLUDE_DIRS
150+
"${LIBCXX_GENERATED_INCLUDE_TARGET_DIR};${LIBCXX_GENERATED_INCLUDE_DIR}"
151+
)
152+
endif()
153+
configure_file(
154+
"CMakeLists.txt.in"
155+
"${LIBCXX_GENERATED_MODULE_DIR}/CMakeLists.txt"
156+
@ONLY
157+
)
158+
140159
set(LIBCXX_MODULE_STD_INCLUDE_SOURCES)
141160
foreach(file ${LIBCXX_MODULE_STD_SOURCES})
142161
set(
@@ -166,6 +185,7 @@ configure_file(
166185
)
167186

168187
set(_all_modules)
188+
list(APPEND _all_modules "${LIBCXX_GENERATED_MODULE_DIR}/CMakeLists.txt")
169189
list(APPEND _all_modules "${LIBCXX_GENERATED_MODULE_DIR}/std.cppm")
170190
list(APPEND _all_modules "${LIBCXX_GENERATED_MODULE_DIR}/std.compat.cppm")
171191
foreach(file ${LIBCXX_MODULE_STD_SOURCES} ${LIBCXX_MODULE_STD_COMPAT_SOURCES})

libcxx/modules/CMakeLists.txt.in

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
cmake_minimum_required(VERSION 3.26)
2+
3+
project(libc++-modules LANGUAGES CXX)
4+
5+
# Enable CMake's module support
6+
if(CMAKE_VERSION VERSION_LESS "3.28.0")
7+
if(CMAKE_VERSION VERSION_LESS "3.27.0")
8+
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
9+
else()
10+
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "aa1f7df0-828a-4fcd-9afc-2dc80491aca7")
11+
endif()
12+
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
13+
else()
14+
cmake_policy(VERSION 3.28)
15+
endif()
16+
17+
# Default to C++ extensions being off. Libc++'s modules support have trouble
18+
# with extensions right now.
19+
set(CMAKE_CXX_EXTENSIONS OFF)
20+
21+
# Propagates the CMake options to the modules.
22+
#
23+
# This uses the std module hard-coded since the std.compat module does not
24+
# depend on these flags.
25+
macro(compile_define_if_not condition def)
26+
if (NOT ${condition})
27+
target_compile_definitions(std PRIVATE ${def})
28+
endif()
29+
endmacro()
30+
macro(compile_define_if condition def)
31+
if (${condition})
32+
target_compile_definitions(std PRIVATE ${def})
33+
endif()
34+
endmacro()
35+
36+
### STD
37+
38+
add_library(std)
39+
target_sources(std
40+
PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES
41+
std.cppm
42+
)
43+
44+
target_include_directories(std SYSTEM PRIVATE @LIBCXX_CONFIGURED_INCLUDE_DIRS@)
45+
46+
if (NOT @LIBCXX_ENABLE_EXCEPTIONS@)
47+
target_compile_options(std PUBLIC -fno-exceptions)
48+
endif()
49+
50+
target_compile_options(std
51+
PUBLIC
52+
-nostdinc++
53+
-Wno-reserved-module-identifier
54+
-Wno-reserved-user-defined-literal
55+
@LIBCXX_COMPILE_FLAGS@
56+
)
57+
set_target_properties(std
58+
PROPERTIES
59+
OUTPUT_NAME "c++std"
60+
)
61+
62+
### STD.COMPAT
63+
64+
add_library(std.compat)
65+
target_sources(std.compat
66+
PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES
67+
std.compat.cppm
68+
)
69+
70+
target_include_directories(std.compat SYSTEM PRIVATE @LIBCXX_CONFIGURED_INCLUDE_DIRS@)
71+
72+
if (NOT @LIBCXX_ENABLE_EXCEPTIONS@)
73+
target_compile_options(std.compat PUBLIC -fno-exceptions)
74+
endif()
75+
76+
target_compile_options(std.compat
77+
PUBLIC
78+
-nostdinc++
79+
-Wno-reserved-module-identifier
80+
-Wno-reserved-user-defined-literal
81+
-fmodule-file=std=${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/std.dir/std.pcm
82+
@LIBCXX_COMPILE_FLAGS@
83+
)
84+
set_target_properties(std.compat
85+
PROPERTIES
86+
OUTPUT_NAME "c++std.compat"
87+
)
88+
add_dependencies(std.compat std)

0 commit comments

Comments
 (0)