@@ -114,7 +114,7 @@ installation of modules and install the modules into ``<install_prefix>``.
114
114
115
115
.. code-block :: bash
116
116
117
- $ git clone https://github.com/llvm/llvm-project.git
117
+ $ git clone https://github.com/llvm/llvm-project.git --depth 1
118
118
$ cd llvm-project
119
119
$ mkdir build
120
120
$ 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.
129
129
130
130
.. code-block :: cpp
131
131
132
+ // main.cpp
132
133
import std; // When importing std.compat it's not needed to import std.
133
134
import std.compat;
134
135
135
136
int main() {
136
- std::cout << "Hello modular world\n" ;
137
+ std::println( "Hello modular world") ;
137
138
::printf("Hello compat modular world\n");
138
139
}
139
140
140
141
.. code-block :: cmake
141
142
143
+ # CMakeLists.txt
142
144
cmake_minimum_required(VERSION 3.26.0 FATAL_ERROR)
143
145
project("module"
144
146
LANGUAGES CXX
@@ -172,51 +174,82 @@ directory. It consists of a ``CMakeLists.txt`` and a ``main.cpp`` file.
172
174
#
173
175
# Import the modules from libc++
174
176
#
177
+ include(std.cmake)
178
+
179
+ add_executable(main main.cpp)
175
180
181
+ .. code-block :: cmake
182
+
183
+ # std.cmake
176
184
include(FetchContent)
177
185
FetchContent_Declare(
178
- std
179
- URL "file://${LIBCXX_BUILD}/modules/c ++/v1/ "
186
+ std_module
187
+ URL "file://${LIBCXX_INSTALLED_DIR}/share/libc ++/v1"
180
188
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
181
189
SYSTEM
182
190
)
183
- FetchContent_MakeAvailable(std)
191
+
192
+ if (NOT std_module_POPULATED)
193
+ FetchContent_Populate(std_module)
194
+ endif()
184
195
185
196
#
186
- # Adjust project compiler flags
197
+ # Add std static library
187
198
#
188
199
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
+ )
196
207
197
208
#
198
- # Adjust project linker flags
209
+ # Adjust project include directories
199
210
#
200
211
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)
207
213
208
214
#
209
- # Add the project
215
+ # Adjust project compiler flags
210
216
#
211
217
212
- add_executable(main)
213
- target_sources(main
218
+ target_compile_options(std
214
219
PRIVATE
215
- main.cpp
220
+ -Wno-reserved-module-identifier
221
+ -Wno-reserved-user-defined-literal
216
222
)
217
223
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
+
218
251
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.
220
253
221
254
.. code-block :: bash
222
255
0 commit comments