Skip to content

Commit 08be2d4

Browse files
authored
Don't modify the host compiler's sysroot (#445)
This commit updates the building of the wasi-sdk sysroot to leverage the `-resource-dir` argument from Clang to avoid modifying the host compiler's sysroot with compiler-rt things. This should help improve the experience of building a standalone sysroot with whatever host Clang is on the system. Closes #444
1 parent 1a3d5ee commit 08be2d4

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

cmake/wasi-sdk-sysroot.cmake

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ option(WASI_SDK_INCLUDE_TESTS "Whether or not to build tests by default" OFF)
1212

1313
set(wasi_tmp_install ${CMAKE_CURRENT_BINARY_DIR}/install)
1414
set(wasi_sysroot ${wasi_tmp_install}/share/wasi-sysroot)
15+
set(wasi_resource_dir ${wasi_tmp_install}/lib/clang/${clang_version})
16+
17+
# Force usage of the custom-built resource-dir and sysroot for the rest of the
18+
# wasi compiles.
19+
add_compile_options(-resource-dir ${wasi_resource_dir})
20+
add_compile_options(--sysroot ${wasi_sysroot})
1521

1622
if(WASI_SDK_DEBUG_PREFIX_MAP)
1723
add_compile_options(
1824
-fdebug-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=wasisdk://v${wasi_sdk_version})
1925
endif()
2026

21-
2227
# Default arguments for builds of cmake projects (mostly LLVM-based) to forward
2328
# along much of our own configuration into these projects.
2429
set(default_cmake_args
@@ -45,7 +50,6 @@ endif()
4550
# compiler-rt build logic
4651
# =============================================================================
4752

48-
set(compiler_rt_dst ${wasi_tmp_install}/lib/clang/${clang_version})
4953
ExternalProject_Add(compiler-rt-build
5054
SOURCE_DIR "${llvm_proj_dir}/compiler-rt"
5155
CMAKE_ARGS
@@ -58,7 +62,7 @@ ExternalProject_Add(compiler-rt-build
5862
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
5963
-DCMAKE_C_COMPILER_TARGET=wasm32-wasi
6064
-DCOMPILER_RT_OS_DIR=wasi
61-
-DCMAKE_INSTALL_PREFIX=${compiler_rt_dst}
65+
-DCMAKE_INSTALL_PREFIX=${wasi_resource_dir}
6266
EXCLUDE_FROM_ALL ON
6367
USES_TERMINAL_CONFIGURE ON
6468
USES_TERMINAL_BUILD ON
@@ -69,26 +73,30 @@ ExternalProject_Add(compiler-rt-build
6973
# around some headers and make copies of the `wasi` directory as `wasip1` and
7074
# `wasip2`
7175
execute_process(
72-
COMMAND ${CMAKE_C_COMPILER} -print-runtime-dir
73-
OUTPUT_VARIABLE clang_runtime_dir
76+
COMMAND ${CMAKE_C_COMPILER} -print-resource-dir
77+
OUTPUT_VARIABLE clang_resource_dir
7478
OUTPUT_STRIP_TRAILING_WHITESPACE)
75-
cmake_path(GET clang_runtime_dir PARENT_PATH clang_runtime_libdir) # chop off `wasi`
76-
cmake_path(GET clang_runtime_libdir PARENT_PATH clang_sysroot_dir) # chop off `lib`
7779
add_custom_target(compiler-rt-post-build
80+
# The `${wasi_resource_dir}` folder is going to get used as `-resource-dir`
81+
# for future compiles. Copy the host compiler's own headers into this
82+
# directory to ensure that all host-defined headers all work as well.
7883
COMMAND ${CMAKE_COMMAND} -E copy_directory
79-
${clang_sysroot_dir} ${compiler_rt_dst}
84+
${clang_resource_dir}/include ${wasi_resource_dir}/include
85+
86+
# Copy the `lib/wasi` folder to `libc/wasi{p1,p2}` to ensure that those
87+
# OS-strings also work for looking up the compiler-rt.a file.
8088
COMMAND ${CMAKE_COMMAND} -E copy_directory
81-
${compiler_rt_dst}/lib/wasi ${compiler_rt_dst}/lib/wasip1
89+
${wasi_resource_dir}/lib/wasi ${wasi_resource_dir}/lib/wasip1
8290
COMMAND ${CMAKE_COMMAND} -E copy_directory
83-
${compiler_rt_dst}/lib/wasi ${compiler_rt_dst}/lib/wasip2
84-
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different
85-
${compiler_rt_dst}/lib ${clang_runtime_libdir}
91+
${wasi_resource_dir}/lib/wasi ${wasi_resource_dir}/lib/wasip2
92+
8693
COMMENT "finalizing compiler-rt installation"
8794
)
8895
add_dependencies(compiler-rt-post-build compiler-rt-build)
8996

9097
add_custom_target(compiler-rt DEPENDS compiler-rt-build compiler-rt-post-build)
9198

99+
92100
# =============================================================================
93101
# wasi-libc build logic
94102
# =============================================================================

tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ include(CTest)
55
enable_testing()
66
set(CMAKE_EXECUTABLE_SUFFIX ".wasm")
77

8-
add_compile_options(--sysroot=${wasi_sysroot})
9-
add_link_options(--sysroot=${wasi_sysroot})
8+
add_compile_options(--sysroot=${wasi_sysroot} -resource-dir ${wasi_resource_dir})
9+
add_link_options(--sysroot=${wasi_sysroot} -resource-dir ${wasi_resource_dir})
1010

1111
# Sanity check setup
1212
if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL WASI)

0 commit comments

Comments
 (0)