Skip to content

Commit 0e90598

Browse files
committed
[libc++][doc] Use installed std modules in external projects.
1 parent 0666cd3 commit 0e90598

File tree

1 file changed

+58
-25
lines changed

1 file changed

+58
-25
lines changed

libcxx/docs/Modules.rst

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ installation of modules and install the modules into ``<install_prefix>``.
114114

115115
.. code-block:: bash
116116
117-
$ git clone https://github.com/llvm/llvm-project.git
117+
$ git clone https://github.com/llvm/llvm-project.git --depth 1
118118
$ cd llvm-project
119119
$ mkdir build
120120
$ cmake -G Ninja -S runtimes -B build -DLIBCXX_INSTALL_MODULES=ON -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind"
@@ -129,16 +129,18 @@ directory. It consists of a ``CMakeLists.txt`` and a ``main.cpp`` file.
129129

130130
.. code-block:: cpp
131131
132+
// main.cpp
132133
import std; // When importing std.compat it's not needed to import std.
133134
import std.compat;
134135
135136
int main() {
136-
std::cout << "Hello modular world\n";
137+
std::println("Hello modular world");
137138
::printf("Hello compat modular world\n");
138139
}
139140
140141
.. code-block:: cmake
141142
143+
# CMakeLists.txt
142144
cmake_minimum_required(VERSION 3.26.0 FATAL_ERROR)
143145
project("module"
144146
LANGUAGES CXX
@@ -172,51 +174,82 @@ directory. It consists of a ``CMakeLists.txt`` and a ``main.cpp`` file.
172174
#
173175
# Import the modules from libc++
174176
#
177+
include(std.cmake)
178+
179+
add_executable(main main.cpp)
175180
181+
.. code-block:: cmake
182+
183+
# std.cmake
176184
include(FetchContent)
177185
FetchContent_Declare(
178-
std
179-
URL "file://${LIBCXX_BUILD}/modules/c++/v1/"
186+
std_module
187+
URL "file://${LIBCXX_INSTALLED_DIR}/share/libc++/v1"
180188
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
181189
SYSTEM
182190
)
183-
FetchContent_MakeAvailable(std)
191+
192+
if (NOT std_module_POPULATED)
193+
FetchContent_Populate(std_module)
194+
endif()
184195
185196
#
186-
# Adjust project compiler flags
197+
# Add std static library
187198
#
188199
189-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fprebuilt-module-path=${std_BINARY_DIR}/CMakeFiles/std.dir/>)
190-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fprebuilt-module-path=${std_BINARY_DIR}/CMakeFiles/std.compat.dir/>)
191-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
192-
# The include path needs to be set to be able to use macros from headers.
193-
# For example from, the headers <cassert> and <version>.
194-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-isystem>)
195-
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${LIBCXX_BUILD}/include/c++/v1>)
200+
add_library(std)
201+
202+
target_sources(std
203+
PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES
204+
${std_module_SOURCE_DIR}/std.cppm
205+
${std_module_SOURCE_DIR}/std.compat.cppm
206+
)
196207
197208
#
198-
# Adjust project linker flags
209+
# Adjust project include directories
199210
#
200211
201-
add_link_options($<$<COMPILE_LANGUAGE:CXX>:-nostdlib++>)
202-
add_link_options($<$<COMPILE_LANGUAGE:CXX>:-L${LIBCXX_BUILD}/lib>)
203-
add_link_options($<$<COMPILE_LANGUAGE:CXX>:-Wl,-rpath,${LIBCXX_BUILD}/lib>)
204-
# Linking against the standard c++ library is required for CMake to get the proper dependencies.
205-
link_libraries(std c++)
206-
link_libraries(std.compat c++)
212+
target_include_directories(std SYSTEM PUBLIC ${LIBCXX_INSTALLED_DIR}/include/c++/v1)
207213
208214
#
209-
# Add the project
215+
# Adjust project compiler flags
210216
#
211217
212-
add_executable(main)
213-
target_sources(main
218+
target_compile_options(std
214219
PRIVATE
215-
main.cpp
220+
-Wno-reserved-module-identifier
221+
-Wno-reserved-user-defined-literal
216222
)
217223
224+
target_compile_options(std
225+
PUBLIC
226+
-nostdinc++
227+
)
228+
229+
#
230+
# Adjust project linker flags
231+
#
232+
233+
target_link_options(std
234+
INTERFACE
235+
-nostdlib++
236+
-L${LIBCXX_INSTALLED_DIR}/lib
237+
-Wl,-rpath,${LIBCXX_INSTALLED_DIR}/lib
238+
)
239+
240+
target_link_libraries(std
241+
INTERFACE
242+
c++
243+
)
244+
245+
#
246+
# Link to the std modules by default
247+
#
248+
249+
link_libraries(std)
250+
218251
Building this project is done with the following steps, assuming the files
219-
``main.cpp`` and ``CMakeLists.txt`` are copied in the current directory.
252+
``main.cpp``, ``CMakeLists.txt``, and ``std.cmake`` are copied in the current directory.
220253

221254
.. code-block:: bash
222255

0 commit comments

Comments
 (0)