Project Background
We’re implementing std modules in ⚙ D144994 [Draft][libc++][modules] Adds std module.. Although I thought to discuss this after we land that patch, it looks like people are more interested in this topic and it should be beneficial to receive more opinions.
Language & Implementation Background
To use modules, we need to compile *.cppm files to (.pcm files (Binary Module Interface, BMI for short) first. Then we can use the modules. For the std module, we need to compile the std-*.cppm
files to std.pcm
files first, then the consumers can import std modules.
The goal of standard c++20 modules is to port the BMI files. However, due to the current implementation limitations, we can only distribute the *.cppm
files only instead of BMI files now. This is the choice of MSVC too. See Tutorial: Import the standard library (STL) using modules from the command line (C++) | Microsoft Learn.
For the compatibility limitations in clang, we can find more information in Standard C++ Modules — Clang 17.0.0git documentation. Simply, the BMI may only be reusable if the triple {Compiler, Flags, Sources} are the same. Note that the Compiler
here doesn’t mean GCC
and Clang
only. Clang16.0.0
is different from Clang16.0.1
in this case.
What do we want to discuss here?
As mentioned above, we need to distribute *.cppm files and we should compile the *.cppm files into std.pcm
for users to import it. Then the question may be:
(1) Where should the *.cppm files live by default?
(2) Where should the std.pcm
live by default?
Especially for the second question, we need to tell the answer to the compiler as a default location. So that we can use clang++ -std=c++23 Hello.cpp
to compile the following codes:
import std;
int main() {
std::cout << "Hello World.\n";
return 0;
}
It sounds bad to tell the user that he need to know more to compile a hello world example.
For the second question, my suggestion may be ${CLANG_EXECUTABLE}/../lib/clang/{version}/modules
. Since ${CLANG_EXECUTABLE}/../lib/clang/{version}/lib
is the default location for the compiler_rt libraries, like libclang_rt.asan_cxx-x86_64.a
. Since both the BMI and the libclang_rt libraries are tight to the compiler versions, I feel it may be good to keep the logic for std modules too.
For the first question, I feel we should keep the same logic with headers. For example, given we’re going to install headers to $PREFIX/include
, we should install the *.cppm files to $PREFIX/modules
. I don’t want to resue $PREFIX/include
since we shouldn’t include *.cppm files.
How should we distribute/package std modules?
This is an open question that need to explore.
Previously I use the following scripts in the downstream to build rpm packages:
# std-module.spec
...
Requires: clang_xxx # for example clang17
%prep
%build
%install
mkdir -p <path-to-the-install-destination-of-std-module>
cp `.cppm` files to <path-to-the-install-destination-of-std-module>
cp ../../../build.sh <path-to-the-install-destination-of-std-module>
...
%post
<path-to-the-install-destination-of-std-module>/build.sh
The meaning of the scripts is that we will only port *.cppm
files in the rpm package. Then when users try to install the rpm package, the installer will try to compile the *.cppm
files automatically.
I guess people may feel it is an overkill to compile files during the installation. My intention is that the users (especially new comers who use modules first) may feel better if they can compile a hello world example by the following command lines:
apt install cmake clang libcxx
clang++ -std=c++23 Hello.cpp
I feel it is much more friendly than asking the users to learn how to compile std modules (like Tutorial: Import the standard library (STL) using modules from the command line (C++) | Microsoft Learn ) first.
Summary
I think we need to discuss the default location for std modules at first, including the location for *.cppm
files and the location for std.pcm
file. Then we can discuss how to distribute them.
Since the named modules is a brand new topic, I guess every one don’t know how to handle it properly. So every opinion/question is welcomed.