diff --git a/.gitmodules b/.gitmodules index e8b2eace4..0691635ce 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,7 +3,7 @@ url = https://github.com/llvm/llvm-project [submodule "src/wasi-libc"] path = src/wasi-libc - url = https://github.com/CraneStation/wasi-libc + url = https://github.com/WebAssembly/wasi-libc [submodule "src/config"] path = src/config url = https://git.savannah.gnu.org/git/config.git diff --git a/Makefile b/Makefile index 319cabd90..9926503b6 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,9 @@ clean: build/llvm.BUILT: mkdir -p build/llvm + # Apply https://reviews.llvm.org/D153293 as a patch until we're ready to + # upgrade to LLVM 17 + (cd $(LLVM_PROJ_DIR) && patch -p1 -N < ../../llvm-D153293-backport.patch) || true cd build/llvm && cmake -G Ninja \ -DCMAKE_BUILD_TYPE=MinSizeRel \ -DLLVM_ENABLE_TERMINFO=OFF \ @@ -107,21 +110,24 @@ build/llvm.BUILT: llvm-config touch build/llvm.BUILT -build/wasi-libc.BUILT: build/llvm.BUILT +build/wasi-libc.BUILT: build/compiler-rt.BUILT $(MAKE) -C $(ROOT_DIR)/src/wasi-libc \ CC=$(BUILD_PREFIX)/bin/clang \ AR=$(BUILD_PREFIX)/bin/llvm-ar \ NM=$(BUILD_PREFIX)/bin/llvm-nm \ - SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot + SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ + BUILTINS_LIB=$(BUILD_PREFIX)/lib/clang/$(CLANG_VERSION)/lib/wasi/libclang_rt.builtins-wasm32.a \ + default libc_so $(MAKE) -C $(ROOT_DIR)/src/wasi-libc \ CC=$(BUILD_PREFIX)/bin/clang \ AR=$(BUILD_PREFIX)/bin/llvm-ar \ NM=$(BUILD_PREFIX)/bin/llvm-nm \ SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ + BUILTINS_LIB=$(BUILD_PREFIX)/lib/clang/$(CLANG_VERSION)/lib/wasi/libclang_rt.builtins-wasm32.a \ THREAD_MODEL=posix touch build/wasi-libc.BUILT -build/compiler-rt.BUILT: build/llvm.BUILT build/wasi-libc.BUILT +build/compiler-rt.BUILT: build/llvm.BUILT # Do the build, and install it. mkdir -p build/compiler-rt cd build/compiler-rt && cmake -G Ninja \ @@ -151,6 +157,8 @@ build/compiler-rt.BUILT: build/llvm.BUILT build/wasi-libc.BUILT touch build/compiler-rt.BUILT # Flags for libcxx and libcxxabi. +# $(1): pthreads ON or OFF +# $(2): shared libraries ON or OFF LIBCXX_CMAKE_FLAGS = \ -DCMAKE_C_COMPILER_WORKS=ON \ -DCMAKE_CXX_COMPILER_WORKS=ON \ @@ -161,38 +169,39 @@ LIBCXX_CMAKE_FLAGS = \ -DLLVM_CONFIG_PATH=$(ROOT_DIR)/build/llvm/bin/llvm-config \ -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ -DCXX_SUPPORTS_CXX11=ON \ - -DLIBCXX_ENABLE_THREADS:BOOL=@PTHREAD@ \ - -DLIBCXX_HAS_PTHREAD_API:BOOL=@PTHREAD@ \ + -DLIBCXX_ENABLE_THREADS:BOOL=$(1) \ + -DLIBCXX_HAS_PTHREAD_API:BOOL=$(1) \ -DLIBCXX_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ -DLIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \ -DLIBCXX_HAS_WIN32_THREAD_API:BOOL=OFF \ -DLLVM_COMPILER_CHECKED=ON \ -DCMAKE_BUILD_TYPE=RelWithDebugInfo \ - -DLIBCXX_ENABLE_SHARED:BOOL=OFF \ + -DLIBCXX_ENABLE_SHARED:BOOL=$(2) \ -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL=OFF \ -DLIBCXX_ENABLE_EXCEPTIONS:BOOL=OFF \ -DLIBCXX_ENABLE_FILESYSTEM:BOOL=OFF \ + -DLIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL=OFF \ -DLIBCXX_CXX_ABI=libcxxabi \ -DLIBCXX_CXX_ABI_INCLUDE_PATHS=$(LLVM_PROJ_DIR)/libcxxabi/include \ -DLIBCXX_HAS_MUSL_LIBC:BOOL=ON \ -DLIBCXX_ABI_VERSION=2 \ -DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF \ - -DLIBCXXABI_ENABLE_SHARED:BOOL=OFF \ + -DLIBCXXABI_ENABLE_SHARED:BOOL=$(2) \ -DLIBCXXABI_SILENT_TERMINATE:BOOL=ON \ - -DLIBCXXABI_ENABLE_THREADS:BOOL=@PTHREAD@ \ - -DLIBCXXABI_HAS_PTHREAD_API:BOOL=@PTHREAD@ \ + -DLIBCXXABI_ENABLE_THREADS:BOOL=$(1) \ + -DLIBCXXABI_HAS_PTHREAD_API:BOOL=$(1) \ -DLIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ -DLIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \ -DLIBCXXABI_HAS_WIN32_THREAD_API:BOOL=OFF \ - -DLIBCXXABI_ENABLE_PIC:BOOL=OFF \ + -DLIBCXXABI_ENABLE_PIC:BOOL=$(2) \ -DWASI_SDK_PREFIX=$(BUILD_PREFIX) \ -DUNIX:BOOL=ON \ --debug-trycompile -build/libcxx.BUILT: build/llvm.BUILT build/compiler-rt.BUILT build/wasi-libc.BUILT +build/libcxx.BUILT: build/llvm.BUILT build/wasi-libc.BUILT # Do the build. mkdir -p build/libcxx - cd build/libcxx && cmake -G Ninja $(LIBCXX_CMAKE_FLAGS:@PTHREAD@=OFF) \ + cd build/libcxx && cmake -G Ninja $(call LIBCXX_CMAKE_FLAGS,OFF,ON) \ -DCMAKE_SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ -DCMAKE_C_FLAGS="$(DEBUG_PREFIX_MAP) $(EXTRA_CFLAGS)" \ -DCMAKE_CXX_FLAGS="$(DEBUG_PREFIX_MAP) $(EXTRA_CXXFLAGS)" \ @@ -202,7 +211,7 @@ build/libcxx.BUILT: build/llvm.BUILT build/compiler-rt.BUILT build/wasi-libc.BUI $(LLVM_PROJ_DIR)/runtimes ninja $(NINJA_FLAGS) -C build/libcxx mkdir -p build/libcxx-threads - cd build/libcxx-threads && cmake -G Ninja $(LIBCXX_CMAKE_FLAGS:@PTHREAD@=ON) \ + cd build/libcxx-threads && cmake -G Ninja $(call LIBCXX_CMAKE_FLAGS,ON,OFF) \ -DCMAKE_SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ -DCMAKE_C_FLAGS="$(DEBUG_PREFIX_MAP) -pthread $(EXTRA_CFLAGS)" \ -DCMAKE_CXX_FLAGS="$(DEBUG_PREFIX_MAP) -pthread $(EXTRA_CXXFLAGS)" \ diff --git a/llvm-D153293-backport.patch b/llvm-D153293-backport.patch new file mode 100644 index 000000000..180773864 --- /dev/null +++ b/llvm-D153293-backport.patch @@ -0,0 +1,40 @@ +diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp +index a1c4cd9ef9c7..4cbc0794f420 100644 +--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp ++++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp +@@ -101,13 +101,16 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA, + << CM << A->getOption().getName(); + } + } +- if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) ++ if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, options::OPT_shared)) + CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1))); + if (Entry) { + CmdArgs.push_back(Args.MakeArgString("--entry")); + CmdArgs.push_back(Args.MakeArgString(Entry)); + } + ++ if (Args.hasArg(options::OPT_shared)) ++ CmdArgs.push_back(Args.MakeArgString("-shared")); ++ + AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); + + if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { +diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp +index 630c786a3dc7..788e08e3c8a9 100644 +--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp ++++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp +@@ -98,13 +98,6 @@ static Reloc::Model getEffectiveRelocModel(std::optional RM, + return Reloc::Static; + } + +- if (!TT.isOSEmscripten()) { +- // Relocation modes other than static are currently implemented in a way +- // that only works for Emscripten, so disable them if we aren't targeting +- // Emscripten. +- return Reloc::Static; +- } +- + return *RM; + } + diff --git a/src/wasi-libc b/src/wasi-libc index ec4566bea..d4dae8964 160000 --- a/src/wasi-libc +++ b/src/wasi-libc @@ -1 +1 @@ -Subproject commit ec4566beae84e54952637f0bf61bee4b4cacc087 +Subproject commit d4dae89648d654cdcad6b0aa8b265bf957f07531 diff --git a/wasi-sdk-pthread.cmake b/wasi-sdk-pthread.cmake index ba660234b..2165a0fff 100644 --- a/wasi-sdk-pthread.cmake +++ b/wasi-sdk-pthread.cmake @@ -41,3 +41,6 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) + + +set(CMAKE_POSITION_INDEPENDENT_CODE On) diff --git a/wasi-sdk.cmake b/wasi-sdk.cmake index f6a0059c8..a77d20a05 100644 --- a/wasi-sdk.cmake +++ b/wasi-sdk.cmake @@ -38,3 +38,5 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) + +set(CMAKE_POSITION_INDEPENDENT_CODE On)