From fcc3b7532e36631f15bb4a020051c70cc0f394fc Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 13 Jun 2025 06:57:11 +0900 Subject: [PATCH] Enable per-target runtime directory for compiler-rt (#533) This change allows us to have per-target libclang_rt.* libraries instead of per-OS. This is particularly required for w/threads and wo/threads targets, whose rt libraries could be incompatible even with the same OS string. This change starts building `compiler-rt` for w/threads and wo/threads separately, so that we can enable thread-related features for p1-threads target for building upcoming ASan clang_rt libraries. Also `COMPILER_RT_OS_DIR` is not set anymore as it's unused when `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR` is enabled. --- README.md | 4 +- ci/merge-artifacts.sh | 8 ++-- cmake/wasi-sdk-sysroot.cmake | 72 ++++++++++++++++++++---------------- 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index b275e8a..19b4770 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,9 @@ convenience. One could also use a standard Clang installation, build a sysroot from the sources mentioned above, and compile with `--target=wasm32-wasi --sysroot=/path/to/sysroot`. In this scenario, one would also need the -`libclang_rt.builtins-wasm32.a` objects available separately in the [release +`libclang_rt.*.a` objects available separately in the [release downloads][releases] which must be extracted into -`$CLANG_INSTALL_DIR/$CLANG_VERSION/lib/wasi/`. +`$CLANG_INSTALL_DIR/$CLANG_VERSION/lib/`. ## Clone diff --git a/ci/merge-artifacts.sh b/ci/merge-artifacts.sh index 4a69e24..e64b68e 100755 --- a/ci/merge-artifacts.sh +++ b/ci/merge-artifacts.sh @@ -62,12 +62,10 @@ for build in dist-*; do tar xf $sysroot -C dist/$sdk_dir/share/wasi-sysroot --strip-components 1 mv dist/$sdk_dir/share/wasi-sysroot/VERSION dist/$sdk_dir - # Setup the compiler-rt library for wasi,wasip1,wasip2 + # Setup the compiler-rt library for all targets. rtlibdir=$(dirname $(find dist/$sdk_dir/lib -name include))/lib - mkdir -p $rtlibdir/wasi - tar xf $compiler_rt -C $rtlibdir/wasi --strip-components 1 - cp -r $rtlibdir/wasi $rtlibdir/wasip1 - cp -r $rtlibdir/wasi $rtlibdir/wasip2 + mkdir -p $rtlibdir + tar xf $compiler_rt -C $rtlibdir --strip-components 1 tar czf dist/$sdk_dir.tar.gz -C dist $sdk_dir diff --git a/cmake/wasi-sdk-sysroot.cmake b/cmake/wasi-sdk-sysroot.cmake index 21c8c4f..37fa118 100644 --- a/cmake/wasi-sdk-sysroot.cmake +++ b/cmake/wasi-sdk-sysroot.cmake @@ -66,31 +66,38 @@ endif() # compiler-rt build logic # ============================================================================= -ExternalProject_Add(compiler-rt-build - SOURCE_DIR "${llvm_proj_dir}/compiler-rt" - CMAKE_ARGS - ${default_cmake_args} - -DCOMPILER_RT_BAREMETAL_BUILD=ON - -DCOMPILER_RT_BUILD_XRAY=OFF - -DCOMPILER_RT_INCLUDE_TESTS=OFF - -DCOMPILER_RT_HAS_FPIC_FLAG=OFF - -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON - -DCOMPILER_RT_BUILD_SANITIZERS=OFF - -DCOMPILER_RT_BUILD_XRAY=OFF - -DCOMPILER_RT_BUILD_LIBFUZZER=OFF - -DCOMPILER_RT_BUILD_PROFILE=OFF - -DCOMPILER_RT_BUILD_CTX_PROFILE=OFF - -DCOMPILER_RT_BUILD_MEMPROF=OFF - -DCOMPILER_RT_BUILD_ORC=OFF - -DCOMPILER_RT_BUILD_GWP_ASAN=OFF - -DCMAKE_C_COMPILER_TARGET=wasm32-wasi - -DCOMPILER_RT_OS_DIR=wasi - -DCMAKE_INSTALL_PREFIX=${wasi_resource_dir} - EXCLUDE_FROM_ALL ON - USES_TERMINAL_CONFIGURE ON - USES_TERMINAL_BUILD ON - USES_TERMINAL_INSTALL ON -) +add_custom_target(compiler-rt-build) +function(define_compiler_rt target) + ExternalProject_Add(compiler-rt-build-${target} + SOURCE_DIR "${llvm_proj_dir}/compiler-rt" + CMAKE_ARGS + ${default_cmake_args} + -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON + -DCOMPILER_RT_BAREMETAL_BUILD=ON + -DCOMPILER_RT_BUILD_XRAY=OFF + -DCOMPILER_RT_INCLUDE_TESTS=OFF + -DCOMPILER_RT_HAS_FPIC_FLAG=OFF + -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON + -DCOMPILER_RT_BUILD_SANITIZERS=OFF + -DCOMPILER_RT_BUILD_XRAY=OFF + -DCOMPILER_RT_BUILD_LIBFUZZER=OFF + -DCOMPILER_RT_BUILD_PROFILE=OFF + -DCOMPILER_RT_BUILD_CTX_PROFILE=OFF + -DCOMPILER_RT_BUILD_MEMPROF=OFF + -DCOMPILER_RT_BUILD_ORC=OFF + -DCOMPILER_RT_BUILD_GWP_ASAN=OFF + -DCMAKE_C_COMPILER_TARGET=${target} + -DCMAKE_INSTALL_PREFIX=${wasi_resource_dir} + EXCLUDE_FROM_ALL ON + USES_TERMINAL_CONFIGURE ON + USES_TERMINAL_BUILD ON + USES_TERMINAL_INSTALL ON + ) + add_dependencies(compiler-rt-build compiler-rt-build-${target}) +endfunction() + +define_compiler_rt(wasm32-wasi) +define_compiler_rt(wasm32-wasip1-threads) # In addition to the default installation of `compiler-rt` itself also copy # around some headers and make copies of the `wasi` directory as `wasip1` and @@ -106,12 +113,15 @@ add_custom_target(compiler-rt-post-build COMMAND ${CMAKE_COMMAND} -E copy_directory ${clang_resource_dir}/include ${wasi_resource_dir}/include - # Copy the `lib/wasi` folder to `libc/wasi{p1,p2}` to ensure that those + # Copy the `lib/wasm32-unknown-wasi` folder to `lib/wasm32-unknown-wasi{p1,p2}` to ensure that those # OS-strings also work for looking up the compiler-rt.a file. COMMAND ${CMAKE_COMMAND} -E copy_directory - ${wasi_resource_dir}/lib/wasi ${wasi_resource_dir}/lib/wasip1 + ${wasi_resource_dir}/lib/wasm32-unknown-wasi ${wasi_resource_dir}/lib/wasm32-unknown-wasip1 + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${wasi_resource_dir}/lib/wasm32-unknown-wasi ${wasi_resource_dir}/lib/wasm32-unknown-wasip2 + # Copy the `lib/wasm32-unknown-wasip1-threads` folder to `lib/wasm32-unknown-wasi-threads` COMMAND ${CMAKE_COMMAND} -E copy_directory - ${wasi_resource_dir}/lib/wasi ${wasi_resource_dir}/lib/wasip2 + ${wasi_resource_dir}/lib/wasm32-unknown-wasip1-threads ${wasi_resource_dir}/lib/wasm32-unknown-wasi-threads COMMENT "finalizing compiler-rt installation" ) @@ -357,10 +367,10 @@ include(wasi-sdk-dist) set(dist_dir ${CMAKE_CURRENT_BINARY_DIR}/dist) -# Tarball with just `compiler-rt` builtins within it +# Tarball with just `compiler-rt` libraries within it wasi_sdk_add_tarball(dist-compiler-rt - ${dist_dir}/libclang_rt.builtins-wasm32-wasi-${wasi_sdk_version}.tar.gz - ${wasi_resource_dir}/lib/wasi) + ${dist_dir}/libclang_rt-${wasi_sdk_version}.tar.gz + ${wasi_resource_dir}/lib) add_dependencies(dist-compiler-rt compiler-rt) # Tarball with the whole sysroot