Skip to content

[libc++][doc] Use installed std modules in external projects. #80601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
228 changes: 198 additions & 30 deletions libcxx/docs/Modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,40 @@ Users need to be able to build their own BMI files.

Currently this requires a local build of libc++ with modules enabled. Since
modules are not part of the installation yet, they are used from the build
directory. First libc++ needs to be build with module support enabled.
directory. First libc++ needs to be build with module support enabled. If
the libc++ is built with installation of modules enabled, they can also be
used from the installation directory. The following instructions enable the
installation of modules and install the modules into ``<install_prefix>``.

.. code-block:: bash

$ git clone https://github.com/llvm/llvm-project.git
$ git clone https://github.com/llvm/llvm-project.git --depth 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to keep the instructions generic, if users like --depth 1 they are free to do so.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

$ cd llvm-project
$ mkdir build
$ cmake -G Ninja -S runtimes -B build -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind"
$ ninja -C build
$ cmake -G Ninja -S runtimes -B build -DLIBCXX_INSTALL_MODULES=ON -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind"
$ cmake --build build -- -j $(nproc)
$ cmake --install build --prefix <install_prefix>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really prefer to keep the ninja -C build and add ninja install -C build. This is the style we use in other parts of the libc++ documentation.


The above ``build`` directory will be referred to as ``<build>`` in the
rest of these instructions.

This is a small sample program that uses the module ``std``. It consists of a
``CMakeLists.txt`` and a ``main.cpp`` file.
This is a small sample program that uses the module ``std`` from build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is a small sample program that uses the module ``std`` from build
This is a small sample program that uses the module ``std`` from the build

directory. It consists of a ``CMakeLists.txt`` and a ``main.cpp`` file.

.. code-block:: cpp

// main.cpp
import std; // When importing std.compat it's not needed to import std.
import std.compat;

int main() {
std::cout << "Hello modular world\n";
std::println("Hello modular world");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't make unrelated changes in the same patch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will change it back

::printf("Hello compat modular world\n");
}

.. code-block:: cmake

# CMakeLists.txt
cmake_minimum_required(VERSION 3.26.0 FATAL_ERROR)
project("module"
LANGUAGES CXX
Expand Down Expand Up @@ -168,51 +174,82 @@ This is a small sample program that uses the module ``std``. It consists of a
#
# Import the modules from libc++
#
include(std.cmake)

add_executable(main main.cpp)

.. code-block:: cmake

# std.cmake
include(FetchContent)
FetchContent_Declare(
std
URL "file://${LIBCXX_BUILD}/modules/c++/v1/"
std_module
URL "file://${LIBCXX_INSTALLED_DIR}/share/libc++/v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the example should use the ${LIBCXX_BUILD} right? Are the other parts of the changes in this part intended?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not intended. I'm not quite good at git. The rebase messed everything and I failed to recheck this. I will fix these things.

DOWNLOAD_EXTRACT_TIMESTAMP TRUE
SYSTEM
)
FetchContent_MakeAvailable(std)

if (NOT std_module_POPULATED)
FetchContent_Populate(std_module)
endif()

#
# Adjust project compiler flags
# Add std static library
#

add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fprebuilt-module-path=${std_BINARY_DIR}/CMakeFiles/std.dir/>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fprebuilt-module-path=${std_BINARY_DIR}/CMakeFiles/std.compat.dir/>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
# The include path needs to be set to be able to use macros from headers.
# For example from, the headers <cassert> and <version>.
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-isystem>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${LIBCXX_BUILD}/include/c++/v1>)
add_library(std)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing "STATIC"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add it


target_sources(std
PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES
${std_module_SOURCE_DIR}/std.cppm
${std_module_SOURCE_DIR}/std.compat.cppm
)

#
# Adjust project linker flags
# Adjust project include directories
#

add_link_options($<$<COMPILE_LANGUAGE:CXX>:-nostdlib++>)
add_link_options($<$<COMPILE_LANGUAGE:CXX>:-L${LIBCXX_BUILD}/lib>)
add_link_options($<$<COMPILE_LANGUAGE:CXX>:-Wl,-rpath,${LIBCXX_BUILD}/lib>)
# Linking against the standard c++ library is required for CMake to get the proper dependencies.
link_libraries(std c++)
link_libraries(std.compat c++)
target_include_directories(std SYSTEM PUBLIC ${LIBCXX_INSTALLED_DIR}/include/c++/v1)

#
# Add the project
# Adjust project compiler flags
#

add_executable(main)
target_sources(main
target_compile_options(std
PRIVATE
main.cpp
-Wno-reserved-module-identifier
-Wno-reserved-user-defined-literal
)

target_compile_options(std
PUBLIC
-nostdinc++
)

#
# Adjust project linker flags
#

target_link_options(std
INTERFACE
-nostdlib++
-L${LIBCXX_INSTALLED_DIR}/lib
-Wl,-rpath,${LIBCXX_INSTALLED_DIR}/lib
)

target_link_libraries(std
INTERFACE
c++
)

#
# Link to the std modules by default
#

link_libraries(std)

Building this project is done with the following steps, assuming the files
``main.cpp`` and ``CMakeLists.txt`` are copied in the current directory.
``main.cpp``, ``CMakeLists.txt``, and ``std.cmake`` are copied in the current directory.

.. code-block:: bash

Expand All @@ -225,6 +262,137 @@ Building this project is done with the following steps, assuming the files
.. note:: The ``std`` dependencies of ``std.compat`` is not always resolved when
building the ``std`` target using multiple jobs.

This is another small sample program that uses the module ``std`` from
installation directory. It consists of a ``CMakeLists.txt``, an
``std.cmake``, and a ``main.cpp`` file. The ``main.cpp`` is the same as
the previous example.
Comment on lines +235 to +237
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
installation directory. It consists of a ``CMakeLists.txt``, an
``std.cmake``, and a ``main.cpp`` file. The ``main.cpp`` is the same as
the previous example.
the installation directory. It consists of a ``CMakeLists.txt``,
a ``std.cmake``, and the ``main.cpp`` from the previous example.


.. code-block:: cmake

# CMakeLists.txt
cmake_minimum_required(VERSION 3.26.0 FATAL_ERROR)
project("module"
LANGUAGES CXX
)

#
# Set language version used
#

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
# Libc++ doesn't support compiler extensions for modules.
set(CMAKE_CXX_EXTENSIONS OFF)

#
# Enable modules in CMake
#

# This is required to write your own modules in your project.
if(CMAKE_VERSION VERSION_LESS "3.28.0")
if(CMAKE_VERSION VERSION_LESS "3.27.0")
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
else()
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "aa1f7df0-828a-4fcd-9afc-2dc80491aca7")
endif()
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
else()
cmake_policy(VERSION 3.28)
endif()

#
# Import the modules from libc++
#
include(std.cmake)

add_executable(main main.cpp)

.. code-block:: cmake

# std.cmake
include(FetchContent)
FetchContent_Declare(
std_module
URL "file://${LIBCXX_INSTALLED_DIR}/share/libc++/v1"
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
SYSTEM
)

if (NOT std_module_POPULATED)
FetchContent_Populate(std_module)
endif()
Comment on lines +290 to +292
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the preferred way for CMake to do this.

Suggested change
if (NOT std_module_POPULATED)
FetchContent_Populate(std_module)
endif()
FetchContent_MakeAvailable(std_module)


#
# Add std static library
#

add_library(std STATIC)

target_sources(std
PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES
${std_module_SOURCE_DIR}/std.cppm
${std_module_SOURCE_DIR}/std.compat.cppm
)

#
# Adjust project include directories
#

target_include_directories(std SYSTEM PUBLIC ${LIBCXX_INSTALLED_DIR}/include/c++/v1)

#
# Adjust project compiler flags
#

target_compile_options(std
PRIVATE
-Wno-reserved-module-identifier
-Wno-reserved-user-defined-literal
)

target_compile_options(std
PUBLIC
-nostdinc++
)

#
# Adjust project linker flags
#

target_link_options(std
INTERFACE
-nostdlib++
-L${LIBCXX_INSTALLED_DIR}/lib
-Wl,-rpath,${LIBCXX_INSTALLED_DIR}/lib
)

target_link_libraries(std
INTERFACE
c++
)

#
# Link to the std modules by default
#

link_libraries(std)

Building this project is done with the following steps, assuming the files
``main.cpp``, ``CMakeLists.txt``, and ``std.cmake`` are copied in the current directory.

.. code-block:: bash

$ mkdir build
$ cmake -S . -B build -G Ninja -DCMAKE_CXX_COMPILER=<path-to-compiler> -DLIBCXX_INSTALLED_DIR=<install_prefix>
$ cmake --build build
$ ./build/main

.. warning:: You need more than clang itself to build a project using modules.
Specifically, you will need ``clang-scan-deps``. For example, in Ubuntu, you
need to use ``sudo ./llvm.sh 17 all`` rather than ``sudo ./llvm.sh 17`` showed
in `LLVM Debian/Ubuntu nightly packages <https://apt.llvm.org>`__ to install
essential components to build this project.

Comment on lines +359 to +364
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this part. This is also required when using the build directory.

.. warning:: ``<path-to-compiler>`` should point point to the real binary and
not to a symlink.

Expand Down