Skip to content

Commit 2c703ed

Browse files
authored
Rework Modules CMake to be (more) idiomatic. (#84936)
- Fix bug in documentation regarding dependencies. - Rework Modules CMake to be (more) idiomatic.
1 parent 36fd0e7 commit 2c703ed

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

libcxx/docs/Modules.rst

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -178,34 +178,13 @@ This is a small sample program that uses the module ``std``. It consists of a
178178
)
179179
FetchContent_MakeAvailable(std)
180180
181-
#
182-
# Adjust project compiler flags
183-
#
184-
185-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fprebuilt-module-path=${std_BINARY_DIR}/CMakeFiles/std.dir/>)
186-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fprebuilt-module-path=${std_BINARY_DIR}/CMakeFiles/std.compat.dir/>)
187-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
188-
# The include path needs to be set to be able to use macros from headers.
189-
# For example from, the headers <cassert> and <version>.
190-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-isystem>)
191-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${LIBCXX_BUILD}/include/c++/v1>)
192-
193-
#
194-
# Adjust project linker flags
195-
#
196-
197-
add_link_options($<$<COMPILE_LANGUAGE:CXX>:-nostdlib++>)
198-
add_link_options($<$<COMPILE_LANGUAGE:CXX>:-L${LIBCXX_BUILD}/lib>)
199-
add_link_options($<$<COMPILE_LANGUAGE:CXX>:-Wl,-rpath,${LIBCXX_BUILD}/lib>)
200-
# Linking against the standard c++ library is required for CMake to get the proper dependencies.
201-
link_libraries(std c++)
202-
link_libraries(std.compat c++)
203-
204181
#
205182
# Add the project
206183
#
207184
208185
add_executable(main)
186+
add_dependencies(main std.compat)
187+
target_link_libraries(main std.compat)
209188
target_sources(main
210189
PRIVATE
211190
main.cpp
@@ -218,13 +197,9 @@ Building this project is done with the following steps, assuming the files
218197
219198
$ mkdir build
220199
$ cmake -G Ninja -S . -B build -DCMAKE_CXX_COMPILER=<path-to-compiler> -DLIBCXX_BUILD=<build>
221-
$ ninja -j1 std -C build
222200
$ ninja -C build
223201
$ build/main
224202
225-
.. note:: The ``std`` dependencies of ``std.compat`` is not always resolved when
226-
building the ``std`` target using multiple jobs.
227-
228203
.. warning:: ``<path-to-compiler>`` should point point to the real binary and
229204
not to a symlink.
230205

libcxx/modules/CMakeLists.txt.in

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,15 @@ endif()
5050
target_compile_options(std
5151
PUBLIC
5252
-nostdinc++
53+
@LIBCXX_COMPILE_FLAGS@
54+
)
55+
target_compile_options(std
56+
PRIVATE
5357
-Wno-reserved-module-identifier
5458
-Wno-reserved-user-defined-literal
55-
@LIBCXX_COMPILE_FLAGS@
5659
)
60+
target_link_options(std PUBLIC -nostdlib++ -Wl,-rpath,@LIBCXX_LIBRARY_DIR@ -L@LIBCXX_LIBRARY_DIR@)
61+
target_link_libraries(std c++)
5762
set_target_properties(std
5863
PROPERTIES
5964
OUTPUT_NAME "c++std"
@@ -67,7 +72,7 @@ target_sources(std.compat
6772
std.compat.cppm
6873
)
6974

70-
target_include_directories(std.compat SYSTEM PRIVATE @LIBCXX_CONFIGURED_INCLUDE_DIRS@)
75+
target_include_directories(std.compat SYSTEM PUBLIC @LIBCXX_CONFIGURED_INCLUDE_DIRS@)
7176

7277
if (NOT @LIBCXX_ENABLE_EXCEPTIONS@)
7378
target_compile_options(std.compat PUBLIC -fno-exceptions)
@@ -76,13 +81,16 @@ endif()
7681
target_compile_options(std.compat
7782
PUBLIC
7883
-nostdinc++
84+
@LIBCXX_COMPILE_FLAGS@
85+
)
86+
target_compile_options(std.compat
87+
PRIVATE
7988
-Wno-reserved-module-identifier
8089
-Wno-reserved-user-defined-literal
81-
-fmodule-file=std=${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/std.dir/std.pcm
82-
@LIBCXX_COMPILE_FLAGS@
8390
)
8491
set_target_properties(std.compat
8592
PROPERTIES
8693
OUTPUT_NAME "c++std.compat"
8794
)
8895
add_dependencies(std.compat std)
96+
target_link_libraries(std.compat PUBLIC std c++)

0 commit comments

Comments
 (0)