Skip to content
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

feat: clang-19 only #8

Merged
merged 14 commits into from
Nov 12, 2024
30 changes: 11 additions & 19 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,13 @@ jobs:
name: 'build.sh'
runs-on: ubuntu-24.04
steps:
- run: sudo apt-get install libclang-dev ninja-build -y
- uses: actions/cache@v4
id: libcxx_with_modules
with:
path: /tmp/llvm-project/build
key: 'libcxx-${{ env.LLVM_VERSION }}'
- name: compiling libcxx ${{ env.LLVM_VERSION }} with modules
if: steps.libcxx_with_modules.outputs.cache-hit != 'true'
run: |
git clone --depth 1 --branch llvmorg-${{ env.LLVM_VERSION }} https://github.com/llvm/llvm-project.git /tmp/llvm-project
cd /tmp/llvm-project
mkdir build
CC=clang-18 CXX=clang++-18 cmake -G Ninja -S runtimes -B build -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind"
ninja -C build
- run: wget https://apt.llvm.org/llvm.sh
- run: chmod +x llvm.sh
- run: sudo ./llvm.sh 19
- run: rm ./llvm.sh
- run: sudo apt-get install libc++-19-dev ninja-build libclang-19-dev -y
- uses: actions/checkout@v4
- run: CC=clang-18 ./build.sh
- run: ./build.sh
- uses: actions/upload-artifact@v4
with:
name: cpp2b-ubuntu-24.04
Expand All @@ -77,10 +68,11 @@ jobs:
needs: build-script-linux
runs-on: ubuntu-24.04
steps:
- uses: actions/cache/restore@v4
with:
path: /tmp/llvm-project/build
key: 'libcxx-${{ env.LLVM_VERSION }}'
- run: wget https://apt.llvm.org/llvm.sh
- run: chmod +x llvm.sh
- run: sudo ./llvm.sh 19
- run: rm ./llvm.sh
- run: sudo apt-get install libc++-19-dev ninja-build libclang-19-dev -y
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
Expand Down
15 changes: 15 additions & 0 deletions build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ if %ERRORLEVEL% neq 0 (
exit %ERRORLEVEL%
)

echo INFO: compiling nlohmann.json module...
pushd %modules_dir%
cl /nologo ^
/std:c++latest /W4 /MDd /EHsc ^
/reference "%modules_dir%\std.ifc" ^
/reference "%modules_dir%\std.compat.ifc" ^
/c /interface /TP "%root_dir%src\nlohmann.json.cppm" > NUL
popd

if %ERRORLEVEL% neq 0 (
echo ERROR: failed to compile nlohmann.json module
exit %ERRORLEVEL%
)

if not exist %cppfront% (
pushd .cache\repos\cppfront\source
echo INFO: compiling cppfront...
Expand All @@ -125,6 +139,7 @@ cl /nologo "%root_dir%.cache/cpp2/source/src/main.cpp" ^
/reference "%modules_dir%\std.ifc" "%modules_dir%\std.obj" ^
/reference "%modules_dir%\std.compat.ifc" "%modules_dir%\std.compat.obj" ^
/reference "%modules_dir%\dylib.ifc" "%modules_dir%\dylib.obj" ^
/reference "%modules_dir%\nlohmann.json.ifc" "%modules_dir%\nlohmann.json.obj" ^
/reference "%modules_dir%\cpp2b.ifc" "%modules_dir%\cpp2b.obj" ^
/std:c++latest /W4 /MDd /EHsc ^
/DEBUG:FULL /Zi /FC ^
Expand Down
67 changes: 40 additions & 27 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ mkdir -p $ROOT_DIR/dist/debug
mkdir -p $ROOT_DIR/.cache/cpp2/source/src
mkdir -p $ROOT_DIR/.cache/cpp2/source/_build

CPP2B_COMPILER=${CC:=clang}

function log_info() {
echo "INFO: $1"
}
Expand All @@ -34,6 +32,18 @@ function fatal() {
exit 1
}

if [[ -z "$CC" ]]; then
COMPILER_VERSION=$(clang --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)
COMPILER_MAJOR_VERSION=$(echo $COMPILER_VERSION | cut -d. -f1)
if [ "$COMPILER_MAJOR_VERSION" -lt 19 ]; then
CC=clang-19
else
CC=clang
fi
fi

CPP2B_COMPILER=${CC}

if ! [ -x "$(command -v $CPP2B_COMPILER)" ]; then
fatal "cannot find $CPP2B_COMPILER in your PATH"
fi
Expand All @@ -44,21 +54,11 @@ fi

COMPILER_VERSION=$($CPP2B_COMPILER --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)
COMPILER_MAJOR_VERSION=$(echo $COMPILER_VERSION | cut -d. -f1)
if [ "$COMPILER_MAJOR_VERSION" -lt 18 ]; then
fatal "clang version 18 or newer only supported: detected $COMPILER_VERSION"
if [ "$COMPILER_MAJOR_VERSION" -lt 19 ]; then
fatal "clang version 19 or newer only supported: detected $COMPILER_VERSION"
fi

if [[ -z "${CPP2B_LIBCXX_BUILD_ROOT}" ]]; then
log_error "libcxx with module support must be built from source"
log_error "follow these instructions https://github.com/llvm/llvm-project/blob/main/libcxx/docs/Modules.rst"
fatal "missing CPP2B_LIBCXX_BUILD_ROOT environment variable"
fi

log_info "using libcxx build root $CPP2B_LIBCXX_BUILD_ROOT"

if ! [ -d $CPP2B_LIBCXX_BUILD_ROOT ]; then
log_fatal "directory $CPP2B_LIBCXX_BUILD_ROOT does not exist"
fi
log_info "using compiler '$CPP2B_COMPILER' version '$COMPILER_VERSION'"

function ensure_gh_repo() {
local repo=$1
Expand Down Expand Up @@ -88,9 +88,7 @@ ensure_gh_repo_subdir "hsutter/cppfront" "include"

CPPFRONT_INCLUDE_DIR=$ROOT_DIR/.cache/repos/hsutter/cppfront/include

LIBCXX_MODULES_DIR=$CPP2B_LIBCXX_BUILD_ROOT/modules
LIBCXX_INCLUDE_DIR=$CPP2B_LIBCXX_BUILD_ROOT/include
LIBCXX_LIB_DIR=$CPP2B_LIBCXX_BUILD_ROOT/lib
LLVM_ROOT=/usr/lib/llvm-$COMPILER_MAJOR_VERSION

if ! [ -x $CPPFRONT ]; then
log_info "compiling cppfront..."
Expand All @@ -100,14 +98,14 @@ if ! [ -x $CPPFRONT ]; then
fi

if ! [ -f $MODULES_DIR/std.pcm ]; then
cd $LIBCXX_MODULES_DIR/c++/v1
cd $LLVM_ROOT/share/libc++/v1
log_info "compiling std module..."

$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LIBCXX_INCLUDE_DIR/c++/v1 \
-isystem $LLVM_ROOT/include/c++/v1 \
-Wno-reserved-module-identifier \
std.cppm \
--precompile -o $MODULES_DIR/std.pcm
Expand All @@ -116,14 +114,14 @@ if ! [ -f $MODULES_DIR/std.pcm ]; then
fi

if ! [ -f $MODULES_DIR/std.compat.pcm ]; then
cd $LIBCXX_MODULES_DIR/c++/v1
cd $LLVM_ROOT/share/libc++/v1
log_info "compiling std.compat module..."

$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LIBCXX_INCLUDE_DIR/c++/v1 \
-isystem $LLVM_ROOT/include/c++/v1 \
-Wno-reserved-module-identifier \
-fprebuilt-module-path=$MODULES_DIR \
std.compat.cppm \
Expand All @@ -133,21 +131,35 @@ if ! [ -f $MODULES_DIR/std.compat.pcm ]; then
fi

if ! [ -f $MODULES_DIR/dylib.pcm ]; then
cd $LIBCXX_MODULES_DIR/c++/v1
log_info "compiling dylib module..."

$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LIBCXX_INCLUDE_DIR/c++/v1 \
-isystem $LLVM_ROOT/include/c++/v1 \
-fprebuilt-module-path=$MODULES_DIR \
"$ROOT_DIR/src/dylib.cppm" \
--precompile -o $MODULES_DIR/dylib.pcm

cd $ROOT_DIR
fi

if ! [ -f $MODULES_DIR/nlohmann.json.pcm ]; then
log_info "compiling nlohmann.json module..."

$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LLVM_ROOT/include/c++/v1 \
-fprebuilt-module-path=$MODULES_DIR \
"$ROOT_DIR/src/nlohmann.json.cppm" \
--precompile -o $MODULES_DIR/nlohmann.json.pcm

cd $ROOT_DIR
fi

log_info "compiling cpp2b module..."
if [ -f "$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm" ]; then
rm "$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm"
Expand All @@ -159,7 +171,7 @@ $CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LIBCXX_INCLUDE_DIR/c++/v1 \
-isystem $LLVM_ROOT/include/c++/v1 \
-fprebuilt-module-path=$MODULES_DIR \
"$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm" \
--precompile -o $MODULES_DIR/cpp2b.pcm
Expand All @@ -173,14 +185,15 @@ $CPP2B_COMPILER \
"$MODULES_DIR/cpp2b.pcm" \
"$MODULES_DIR/dylib.pcm" \
"$MODULES_DIR/std.compat.pcm" \
"$MODULES_DIR/nlohmann.json.pcm" \
"$ROOT_DIR/.cache/cpp2/source/src/main.cpp" \
-std=c++23 \
-fexperimental-library \
-Wno-unused-result \
-Wno-deprecated-declarations \
-fprebuilt-module-path=$MODULES_DIR \
-L$LIBCXX_LIB_DIR \
-isystem $LIBCXX_INCLUDE_DIR/c++/v1 \
-L$LLVM_ROOT/lib \
-isystem $LLVM_ROOT/include/c++/v1 \
-lc++abi \
-lc++ \
-lm \
Expand Down
4 changes: 2 additions & 2 deletions examples/static-member/src/main.cpp2
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Month: type = {
id_: int;
names: std::vector<std::string> == ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
names: () == :std::array=("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
}

main: () = {
for Month::names do(name) {
for Month::names() do(name) {
std::println("hello {}", name);
}
}
42 changes: 22 additions & 20 deletions src/main.cpp2
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import std.compat;
import cpp2b;
import dylib;
import nlohmann.json;

fs: namespace == std::filesystem;
json: type == nlohmann::json;

log_error: <Args...: type>(fmt: std::format_string<Args...>, forward args...: Args) = {
std::println("\033[0;31mERROR\033[0m: {}", std::format(fmt, args...));
Expand Down Expand Up @@ -184,13 +186,13 @@ get_vs_tools_dir: () -> fs::path = {
.fs::canonical();
}

get_libcxx_build_root: () -> fs::path = {
return cpp2b::env::get_var("CPP2B_LIBCXX_BUILD_ROOT").expect("missing 'CPP2B_LIBCXX_BUILD_ROOT' environment variable");
get_llvm_root: () -> fs::path = {
return "/usr/lib/llvm-19";
}

get_system_modules_dir: (compiler: cpp2b::compiler_type) -> fs::path = {
if compiler == cpp2b::compiler_type::msvc { return get_vs_tools_dir() / "modules"; }
if compiler == cpp2b::compiler_type::clang { return get_libcxx_build_root() / "modules" / "c++" / "v1"; }
if compiler == cpp2b::compiler_type::clang { return get_llvm_root() / "share" / "libc++" / "v1"; }
log_error("cannot find system cpp20 modules directory");
std::abort();
}
Expand Down Expand Up @@ -236,9 +238,9 @@ cl_build_cpp1_module_cmd: (name: std::string, sources, module_deps, bmi_path: fs

unix_build_cpp1_module_cmd: (compiler_cmd: std::string, name: std::string, sources, module_deps, bmi_path: fs::path) -> std::string = {
d := fs::absolute(modules_dir());
libcxx_inc_dir := get_libcxx_build_root() / "include" / "c++" / "v1";
sys_inc_dir := get_llvm_root() / "include" / "c++" / "v1";
cmd_str: std::string = std::format("{} -stdlib=libc++ -std=c++23 -fexperimental-library", compiler_cmd);
cmd_str += std::format(" -isystem \"{}\"", fs::absolute(libcxx_inc_dir).string());
cmd_str += std::format(" -isystem \"{}\"", fs::absolute(sys_inc_dir).string());
cmd_str += std::format(" -fprebuilt-module-path=\"{}\"", fs::absolute(d).string());
for sources do (src: fs::path) {
cmd_str += " \"(fs::absolute(src).string())$\"";
Expand All @@ -258,7 +260,7 @@ build_cpp1_module: (name: std::string, sources, module_deps) = {

cmd_str: std::string = "";
if compiler == cpp2b::compiler_type::msvc { cmd_str = cl_build_cpp1_module_cmd(name, sources, module_deps, bmi); }
else if compiler == cpp2b::compiler_type::clang { cmd_str = unix_build_cpp1_module_cmd("clang", name, sources, module_deps, bmi); }
else if compiler == cpp2b::compiler_type::clang { cmd_str = unix_build_cpp1_module_cmd("clang-19", name, sources, module_deps, bmi); }
else if compiler == cpp2b::compiler_type::gcc { cmd_str = unix_build_cpp1_module_cmd("gcc", name, sources, module_deps, bmi); }
else { log_error("unsupported compiler"); std::abort(); }

Expand Down Expand Up @@ -361,11 +363,11 @@ main: (args) -> int = {
}
}
} else {
libcxx_lib_dir := get_libcxx_build_root() / "lib";
sys_lib_dir := get_llvm_root() / "lib";
ld_library_path := cpp2b::env::get_var("LD_LIBRARY_PATH");
if !ld_library_path || !ld_library_path*.contains(libcxx_lib_dir.string()) {
log_info("setting LD_LIBRARY_PATH to '{}'", libcxx_lib_dir.string());
cpp2b::env::set_var("LD_LIBRARY_PATH", libcxx_lib_dir.string());
if !ld_library_path || !ld_library_path*.contains(sys_lib_dir.string()) {
log_info("setting LD_LIBRARY_PATH to '{}'", sys_lib_dir.string());
cpp2b::env::set_var("LD_LIBRARY_PATH", sys_lib_dir.string());
return run_with_args(argz);
}
}
Expand Down Expand Up @@ -577,8 +579,8 @@ cl_build_binary_cmd: (info: cpp2b_source_binary_info, bin_outpath: fs::path) ->
}

unix_build_binary_cmd: (compiler_cmd: std::string, info: cpp2b_source_binary_info, bin_outpath: fs::path) -> std::string = {
libcxx_inc_dir := get_libcxx_build_root() / "include" / "c++" / "v1";
libcxx_lib_dir := get_libcxx_build_root() / "lib";
sys_inc_dir := get_llvm_root() / "include" / "c++" / "v1";
sys_lib_dir := get_llvm_root() / "lib";
cppfront_include_dir := fs::absolute(".cache/cpp2/repos/hsutter/cppfront/include");
transpiled_src := fs::absolute(".cache/cpp2/source") / fs::path(info.src).replace_extension(".cpp");
d := fs::absolute(modules_dir());
Expand All @@ -589,8 +591,8 @@ unix_build_binary_cmd: (compiler_cmd: std::string, info: cpp2b_source_binary_inf
cmd_str += " \"(transpiled_src.string())$\"";
cmd_str += " -std=c++23 -fexperimental-library";
cmd_str += std::format(" -fprebuilt-module-path={}", d.string());
cmd_str += std::format(" -L{}", libcxx_lib_dir.string());
cmd_str += std::format(" -isystem {}", libcxx_inc_dir.string());
cmd_str += std::format(" -L{}", sys_lib_dir.string());
cmd_str += std::format(" -isystem {}", sys_inc_dir.string());
cmd_str += " -lc++abi -lc++ -lm -static -fuse-ld=lld";
cmd_str += " -I\"(cppfront_include_dir.string())$\"";
cmd_str += " -o \"(bin_outpath.string())$\"";
Expand All @@ -614,7 +616,7 @@ build_binary: (info: cpp2b_source_binary_info) -> build_binary_result = {

cmd_str: std::string = "";
if compiler == cpp2b::compiler_type::msvc { cmd_str = cl_build_binary_cmd(info, bin_outpath); }
else if compiler == cpp2b::compiler_type::clang { cmd_str = unix_build_binary_cmd("clang", info, bin_outpath); }
else if compiler == cpp2b::compiler_type::clang { cmd_str = unix_build_binary_cmd("clang-19", info, bin_outpath); }
else if compiler == cpp2b::compiler_type::gcc { cmd_str = unix_build_binary_cmd("gcc", info, bin_outpath); }
else { log_error("Unsupported compiler"); std::abort(); }

Expand Down Expand Up @@ -675,8 +677,8 @@ cl_build_build_script_cmd: (info: cpp2b_source_build_info, bin_outpath: fs::path


unix_build_build_script_cmd: (compiler_cmd: std::string, info: cpp2b_source_build_info, bin_outpath: fs::path) -> std::string = {
libcxx_inc_dir := get_libcxx_build_root() / "include" / "c++" / "v1";
libcxx_lib_dir := get_libcxx_build_root() / "lib";
sys_inc_dir := get_llvm_root() / "include" / "c++" / "v1";
sys_lib_dir := get_llvm_root() / "lib";
cppfront_include_dir := fs::absolute(".cache/cpp2/repos/hsutter/cppfront/include");
transpiled_src := fs::absolute(".cache/cpp2/source") / fs::path(info.src).replace_extension(".cpp");
d := fs::absolute(modules_dir());
Expand All @@ -687,8 +689,8 @@ unix_build_build_script_cmd: (compiler_cmd: std::string, info: cpp2b_source_buil
cmd_str += " \"(transpiled_src.string())$\"";
cmd_str += " -std=c++23 -fexperimental-library -fPIC";
cmd_str += std::format(" -fprebuilt-module-path={}", d.string());
cmd_str += std::format(" -L{}", libcxx_lib_dir.string());
cmd_str += std::format(" -isystem {}", libcxx_inc_dir.string());
cmd_str += std::format(" -L{}", sys_lib_dir.string());
cmd_str += std::format(" -isystem {}", sys_inc_dir.string());
cmd_str += " -lc++abi -lc++ -lm -fuse-ld=lld";
cmd_str += " -I\"(cppfront_include_dir.string())$\"";
cmd_str += " -o \"(bin_outpath.string())$\"";
Expand All @@ -706,7 +708,7 @@ build_build_script: (info: cpp2b_source_build_info) -> build_binary_result = {
d := fs::absolute(modules_dir());
cmd_str: std::string = "";
if compiler == cpp2b::compiler_type::msvc { cmd_str = cl_build_build_script_cmd(info, bin_outpath); }
else if compiler == cpp2b::compiler_type::clang { cmd_str = unix_build_build_script_cmd("clang", info, bin_outpath); }
else if compiler == cpp2b::compiler_type::clang { cmd_str = unix_build_build_script_cmd("clang-19", info, bin_outpath); }
else if compiler == cpp2b::compiler_type::gcc { cmd_str = unix_build_build_script_cmd("gcc", info, bin_outpath); }
else { log_error("Unsupported compiler"); std::abort(); }

Expand Down
Loading