Skip to content

Commit 033453a

Browse files
mordantehawkinsw
andauthored
[libc++][doc] Updates module build instructions. (#89413)
CMake has landed experimental support for using the Standard modules. This will be part of the CMake 3.30 release. This updates the build instructions to use modules with CMake. The changes have been tested locally. --------- Co-authored-by: Will Hawkins <[email protected]>
1 parent 1a8935a commit 033453a

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

libcxx/docs/Modules.rst

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ Some of the current limitations
6969
* The path to the compiler may not be a symlink, ``clang-scan-deps`` does
7070
not handle that case properly
7171
* Libc++ is not tested with modules instead of headers
72-
* Clang supports modules using GNU extensions, but libc++ does not work using
73-
GNU extensions.
7472
* Clang:
7573
* Including headers after importing the ``std`` module may fail. This is
7674
hard to solve and there is a work-around by first including all headers
@@ -105,9 +103,17 @@ Users need to be able to build their own BMI files.
105103
system vendors, with the goal that building the BMI files is done by
106104
the build system.
107105

108-
Currently this requires a local build of libc++ with modules enabled. Since
109-
modules are not part of the installation yet, they are used from the build
110-
directory. First libc++ needs to be build with module support enabled.
106+
Currently there are two ways to build modules
107+
108+
* Use a local build of modules from the build directory. This requires
109+
Clang 17 or later and CMake 3.26 or later.
110+
111+
* Use the installed modules. This requires Clang 18.1.2 or later and
112+
a recent build of CMake. The CMake changes will be part of CMake 3.30. This
113+
method requires you or your distribution to enable module installation.
114+
115+
Using the local build
116+
~~~~~~~~~~~~~~~~~~~~~
111117

112118
.. code-block:: bash
113119
@@ -136,7 +142,7 @@ This is a small sample program that uses the module ``std``. It consists of a
136142
.. code-block:: cmake
137143
138144
cmake_minimum_required(VERSION 3.26.0 FATAL_ERROR)
139-
project("module"
145+
project("example"
140146
LANGUAGES CXX
141147
)
142148
@@ -146,7 +152,6 @@ This is a small sample program that uses the module ``std``. It consists of a
146152
147153
set(CMAKE_CXX_STANDARD 23)
148154
set(CMAKE_CXX_STANDARD_REQUIRED YES)
149-
# Libc++ doesn't support compiler extensions for modules.
150155
set(CMAKE_CXX_EXTENSIONS OFF)
151156
152157
#
@@ -214,6 +219,64 @@ Building this project is done with the following steps, assuming the files
214219

215220
``error: module file _deps/std-build/CMakeFiles/std.dir/std.pcm cannot be loaded due to a configuration mismatch with the current compilation [-Wmodule-file-config-mismatch]``
216221

222+
223+
Using the installed modules
224+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
225+
226+
CMake has added experimental support for importing the Standard modules. This
227+
is available in the current nightly builds and will be part of the 3.30
228+
release. Currently CMake only supports importing the Standard modules in C++23
229+
and later. Enabling this for C++20 is on the TODO list of the CMake
230+
developers.
231+
232+
The example uses the same ``main.cpp`` as above. It uses the following
233+
``CMakeLists.txt``:
234+
235+
.. code-block:: cmake
236+
237+
# This requires a recent nightly build.
238+
# This will be part of CMake 3.30.0.
239+
cmake_minimum_required(VERSION 3.29.0 FATAL_ERROR)
240+
241+
# Enables the Standard module support. This needs to be done
242+
# before selecting the languages.
243+
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "0e5b6991-d74f-4b3d-a41c-cf096e0b2508")
244+
set(CMAKE_CXX_MODULE_STD ON)
245+
246+
project("example"
247+
LANGUAGES CXX
248+
)
249+
250+
#
251+
# Set language version used
252+
#
253+
254+
set(CMAKE_CXX_STANDARD 23)
255+
set(CMAKE_CXX_STANDARD_REQUIRED YES)
256+
# Currently CMake requires extensions enabled when using import std.
257+
# https://gitlab.kitware.com/cmake/cmake/-/issues/25916
258+
# https://gitlab.kitware.com/cmake/cmake/-/issues/25539
259+
set(CMAKE_CXX_EXTENSIONS ON)
260+
261+
add_executable(main)
262+
target_sources(main
263+
PRIVATE
264+
main.cpp
265+
)
266+
267+
Building this project is done with the following steps, assuming the files
268+
``main.cpp`` and ``CMakeLists.txt`` are copied in the current directory.
269+
270+
.. code-block:: bash
271+
272+
$ mkdir build
273+
$ cmake -G Ninja -S . -B build -DCMAKE_CXX_COMPILER=<path-to-compiler> -DCMAKE_CXX_FLAGS=-stdlib=libc++
274+
$ ninja -C build
275+
$ build/main
276+
277+
.. warning:: ``<path-to-compiler>`` should point point to the real binary and
278+
not to a symlink.
279+
217280
If you have questions about modules feel free to ask them in the ``#libcxx``
218281
channel on `LLVM's Discord server <https://discord.gg/jzUbyP26tQ>`__.
219282

0 commit comments

Comments
 (0)