├── .github ├── FUNDING.yml └── workflows │ └── package.yml ├── .gitignore ├── .gitmodules ├── CONTRIBUTING.md ├── Get-LLVM-Version.cmake ├── LICENSE.txt ├── build.sh └── driverdriver.py /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | patreon: whitequark 2 | -------------------------------------------------------------------------------- /.github/workflows/package.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | name: Build & publish 3 | jobs: 4 | build: 5 | if: ${{ !contains(github.event.head_commit.message, 'skip ci') }} 6 | runs-on: ubuntu-latest 7 | env: 8 | RELEASE_BRANCH: ${{ startsWith(github.event.ref, 'refs/heads/develop-') || startsWith(github.event.ref, 'refs/heads/release-') }} 9 | steps: 10 | - name: Check out source code 11 | uses: actions/checkout@v4 12 | with: 13 | fetch-depth: 0 14 | submodules: true # not recursive! 15 | - name: Set up Python 16 | uses: actions/setup-python@v5 17 | with: 18 | python-version: '3.x' 19 | - name: Install dependencies 20 | run: | 21 | python -m pip install --upgrade pip build 22 | sudo apt update 23 | sudo apt-get install flex bison ccache 24 | - name: Set up caching 25 | uses: actions/cache@v4 26 | with: 27 | path: ~/.cache/ccache 28 | key: llvm-${{ hashFiles('llvm-src', 'build.sh') }} 29 | restore-keys: | 30 | llvm-${{ hashFiles('llvm-src', 'build.sh') }} 31 | llvm- 32 | - name: Set up ccache 33 | run: | 34 | ccache --max-size=10G -z 35 | - name: Build WASM binaries 36 | run: | 37 | MAKEFLAGS=-j$(nproc) ./build.sh 38 | - name: Build WASM artifact 39 | run: | 40 | tar cvf wasi-prefix.tar wasi-prefix 41 | - name: Upload WASM artifact 42 | uses: actions/upload-artifact@v4 43 | with: 44 | name: dist-prefix 45 | path: wasi-prefix.tar 46 | # ... snip... 47 | - name: Print ccache statistics 48 | run: | 49 | ccache -s 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /wasi-sdk-*.* 2 | *.wasm 3 | 4 | /*-build 5 | /*-prefix 6 | /Toolchain-WASI*.cmake 7 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "llvm-src"] 2 | path = llvm-src 3 | url = https://github.com/YoWASP/llvm-project 4 | [submodule "wasi-sdk-src"] 5 | path = wasi-sdk-src 6 | url = https://github.com/WebAssembly/wasi-sdk 7 | [submodule "wasi-libc-src"] 8 | path = wasi-libc-src 9 | url = https://github.com/WebAssembly/wasi-libc 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | YoWASP Clang packages 2 | ===================== 3 | 4 | Building 5 | -------- 6 | 7 | The primary build environment for this repository is the `ubuntu-latest` GitHub CI runner; packages are built on every push and automatically published from the `release` branch to PyPI. 8 | 9 | To reduce maintenance overhead, the only development environment we will support for this repository is x86_64 Linux. 10 | -------------------------------------------------------------------------------- /Get-LLVM-Version.cmake: -------------------------------------------------------------------------------- 1 | include(llvm-src/cmake/Modules/LLVMVersion.cmake) 2 | message(NOTICE ${LLVM_VERSION_MAJOR}) 3 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (C) Catherine 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | 3 | export SOURCE_DATE_EPOCH=$(git log -1 --format=%ct) 4 | 5 | WASI_SDK=wasi-sdk-22.0 6 | WASI_SDK_URL=https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-22/wasi-sdk-22.0-linux.tar.gz 7 | if ! [ -d ${WASI_SDK} ]; then curl -L ${WASI_SDK_URL} | tar xzf -; fi 8 | WASI_SDK_PATH=$(pwd)/${WASI_SDK} 9 | 10 | WASI_SYSROOT="--sysroot ${WASI_SDK_PATH}/share/wasi-sysroot" 11 | WASI_TARGET="wasm32-wasip1" 12 | WASI_CFLAGS="" 13 | WASI_LDFLAGS="" 14 | WASI_TARGET_LLVM="${WASI_TARGET}-threads" 15 | WASI_CFLAGS_LLVM="${WASI_CFLAGS} -pthread" 16 | WASI_LDFLAGS_LLVM="${WASI_LDFLAGS}" 17 | # LLVM has some (unreachable in our configuration) calls to mmap. 18 | WASI_CFLAGS_LLVM="${WASI_CFLAGS_LLVM} -D_WASI_EMULATED_MMAN" 19 | WASI_LDFLAGS_LLVM="${WASI_LDFLAGS_LLVM} -lwasi-emulated-mman" 20 | # Depending on the code being compiled, both Clang and LLD can consume unbounded amounts of memory. 21 | WASI_LDFLAGS_LLVM="${WASI_LDFLAGS_LLVM} -Wl,--max-memory=4294967296" 22 | # Compiling C++ code requires a lot of stack space and can overflow and corrupt the heap. 23 | # (For example, `#include ` alone does it in a build with the default stack size.) 24 | WASI_LDFLAGS_LLVM="${WASI_LDFLAGS_LLVM} -Wl,-z,stack-size=1048576 -Wl,--stack-first" 25 | # Some of the host APIs that are statically required by LLVM (notably threading) are dynamically 26 | # never used. An LTO build removes imports of these APIs, simplifying deployment 27 | WASI_CFLAGS_LLVM="${WASI_CFLAGS_LLVM} -flto" 28 | WASI_LDFLAGS_LLVM="${WASI_LDFLAGS_LLVM} -flto -Wl,--strip-all" 29 | 30 | # We need two toolchain files: one for the compiler itself (which needs threads at the moment since 31 | # -DLLVM_ENABLE_THREADS=OFF is kind of broken), and one for the runtime libs. 32 | cat >Toolchain-WASI.cmake <>Toolchain-WASI.cmake <>Toolchain-WASI-LLVM.cmake <&1) 71 | 72 | if ! [ -f llvm-tblgen-build/bin/llvm-tblgen -a -f llvm-tblgen-build/bin/clang-tblgen ]; then 73 | mkdir -p llvm-tblgen-build 74 | cmake -B llvm-tblgen-build -S llvm-src/llvm \ 75 | -DLLVM_CCACHE_BUILD=ON \ 76 | -DCMAKE_BUILD_TYPE=MinSizeRel \ 77 | -DLLVM_BUILD_RUNTIME=OFF \ 78 | -DLLVM_BUILD_TOOLS=OFF \ 79 | -DLLVM_INCLUDE_UTILS=OFF \ 80 | -DLLVM_INCLUDE_RUNTIMES=OFF \ 81 | -DLLVM_INCLUDE_EXAMPLES=OFF \ 82 | -DLLVM_INCLUDE_TESTS=OFF \ 83 | -DLLVM_INCLUDE_BENCHMARKS=OFF \ 84 | -DLLVM_INCLUDE_DOCS=OFF \ 85 | -DLLVM_TARGETS_TO_BUILD=WebAssembly \ 86 | -DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-wasi \ 87 | -DLLVM_ENABLE_PROJECTS="clang" \ 88 | -DCLANG_BUILD_EXAMPLES=OFF \ 89 | -DCLANG_BUILD_TOOLS=OFF \ 90 | -DCLANG_INCLUDE_TESTS=OFF 91 | cmake --build llvm-tblgen-build --target llvm-tblgen --target clang-tblgen 92 | fi 93 | 94 | mkdir -p llvm-build 95 | cmake -B llvm-build -S llvm-src/llvm \ 96 | -DCMAKE_TOOLCHAIN_FILE=../Toolchain-WASI-LLVM.cmake \ 97 | -DLLVM_CCACHE_BUILD=ON \ 98 | -DLLVM_NATIVE_TOOL_DIR=$(pwd)/llvm-tblgen-build/bin \ 99 | -DCMAKE_BUILD_TYPE=MinSizeRel \ 100 | -DLLVM_ENABLE_ASSERTIONS=ON \ 101 | -DLLVM_BUILD_SHARED_LIBS=OFF \ 102 | -DLLVM_ENABLE_PIC=OFF \ 103 | -DLLVM_BUILD_STATIC=ON \ 104 | -DLLVM_ENABLE_THREADS=OFF \ 105 | -DLLVM_BUILD_RUNTIME=OFF \ 106 | -DLLVM_BUILD_TOOLS=OFF \ 107 | -DLLVM_INCLUDE_UTILS=OFF \ 108 | -DLLVM_BUILD_UTILS=OFF \ 109 | -DLLVM_INCLUDE_RUNTIMES=OFF \ 110 | -DLLVM_INCLUDE_EXAMPLES=OFF \ 111 | -DLLVM_INCLUDE_TESTS=OFF \ 112 | -DLLVM_INCLUDE_BENCHMARKS=OFF \ 113 | -DLLVM_INCLUDE_DOCS=OFF \ 114 | -DLLVM_TARGETS_TO_BUILD=WebAssembly \ 115 | -DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-wasip1 \ 116 | -DLLVM_TOOL_BUGPOINT_BUILD=OFF \ 117 | -DLLVM_TOOL_BUGPOINT_PASSES_BUILD=OFF \ 118 | -DLLVM_TOOL_DSYMUTIL_BUILD=OFF \ 119 | -DLLVM_TOOL_DXIL_DIS_BUILD=OFF \ 120 | -DLLVM_TOOL_GOLD_BUILD=OFF \ 121 | -DLLVM_TOOL_LLC_BUILD=OFF \ 122 | -DLLVM_TOOL_LLI_BUILD=OFF \ 123 | -DLLVM_TOOL_LLVM_AR_BUILD=ON \ 124 | -DLLVM_TOOL_LLVM_AS_BUILD=ON \ 125 | -DLLVM_TOOL_LLVM_AS_FUZZER_BUILD=OFF \ 126 | -DLLVM_TOOL_LLVM_BCANALYZER_BUILD=OFF \ 127 | -DLLVM_TOOL_LLVM_CAT_BUILD=OFF \ 128 | -DLLVM_TOOL_LLVM_CFI_VERIFY_BUILD=OFF \ 129 | -DLLVM_TOOL_LLVM_CONFIG_BUILD=OFF \ 130 | -DLLVM_TOOL_LLVM_COV_BUILD=ON \ 131 | -DLLVM_TOOL_LLVM_CVTRES_BUILD=OFF \ 132 | -DLLVM_TOOL_LLVM_CXXDUMP_BUILD=OFF \ 133 | -DLLVM_TOOL_LLVM_CXXFILT_BUILD=ON \ 134 | -DLLVM_TOOL_LLVM_CXXMAP_BUILD=OFF \ 135 | -DLLVM_TOOL_LLVM_C_TEST_BUILD=OFF \ 136 | -DLLVM_TOOL_LLVM_DEBUGINFOD_BUILD=OFF \ 137 | -DLLVM_TOOL_LLVM_DEBUGINFOD_FIND_BUILD=OFF \ 138 | -DLLVM_TOOL_LLVM_DEBUGINFO_ANALYZER_BUILD=OFF \ 139 | -DLLVM_TOOL_LLVM_DIFF_BUILD=OFF \ 140 | -DLLVM_TOOL_LLVM_DIS_BUILD=OFF \ 141 | -DLLVM_TOOL_LLVM_DIS_FUZZER_BUILD=OFF \ 142 | -DLLVM_TOOL_LLVM_DLANG_DEMANGLE_FUZZER_BUILD=OFF \ 143 | -DLLVM_TOOL_LLVM_DRIVER_BUILD=ON \ 144 | -DLLVM_TOOL_LLVM_DWARFDUMP_BUILD=ON \ 145 | -DLLVM_TOOL_LLVM_DWARFUTIL_BUILD=OFF \ 146 | -DLLVM_TOOL_LLVM_DWP_BUILD=OFF \ 147 | -DLLVM_TOOL_LLVM_EXEGESIS_BUILD=OFF \ 148 | -DLLVM_TOOL_LLVM_EXTRACT_BUILD=OFF \ 149 | -DLLVM_TOOL_LLVM_GSYMUTIL_BUILD=OFF \ 150 | -DLLVM_TOOL_LLVM_IFS_BUILD=OFF \ 151 | -DLLVM_TOOL_LLVM_ISEL_FUZZER_BUILD=OFF \ 152 | -DLLVM_TOOL_LLVM_ITANIUM_DEMANGLE_FUZZER_BUILD=OFF \ 153 | -DLLVM_TOOL_LLVM_JITLINK_BUILD=OFF \ 154 | -DLLVM_TOOL_LLVM_JITLISTENER_BUILD=OFF \ 155 | -DLLVM_TOOL_LLVM_LIBTOOL_DARWIN_BUILD=OFF \ 156 | -DLLVM_TOOL_LLVM_LINK_BUILD=ON \ 157 | -DLLVM_TOOL_LLVM_LIPO_BUILD=OFF \ 158 | -DLLVM_TOOL_LLVM_LTO2_BUILD=OFF \ 159 | -DLLVM_TOOL_LLVM_LTO_BUILD=OFF \ 160 | -DLLVM_TOOL_LLVM_MCA_BUILD=OFF \ 161 | -DLLVM_TOOL_LLVM_MC_ASSEMBLE_FUZZER_BUILD=OFF \ 162 | -DLLVM_TOOL_LLVM_MC_BUILD=OFF \ 163 | -DLLVM_TOOL_LLVM_MC_DISASSEMBLE_FUZZER_BUILD=OFF \ 164 | -DLLVM_TOOL_LLVM_MICROSOFT_DEMANGLE_FUZZER_BUILD=OFF \ 165 | -DLLVM_TOOL_LLVM_ML_BUILD=OFF \ 166 | -DLLVM_TOOL_LLVM_MODEXTRACT_BUILD=OFF \ 167 | -DLLVM_TOOL_LLVM_MT_BUILD=OFF \ 168 | -DLLVM_TOOL_LLVM_NM_BUILD=OFF \ 169 | -DLLVM_TOOL_LLVM_OBJCOPY_BUILD=ON \ 170 | -DLLVM_TOOL_LLVM_OBJDUMP_BUILD=ON \ 171 | -DLLVM_TOOL_LLVM_OPT_FUZZER_BUILD=OFF \ 172 | -DLLVM_TOOL_LLVM_OPT_REPORT_BUILD=OFF \ 173 | -DLLVM_TOOL_LLVM_PDBUTIL_BUILD=OFF \ 174 | -DLLVM_TOOL_LLVM_PROFDATA_BUILD=OFF \ 175 | -DLLVM_TOOL_LLVM_PROFGEN_BUILD=OFF \ 176 | -DLLVM_TOOL_LLVM_RC_BUILD=OFF \ 177 | -DLLVM_TOOL_LLVM_READOBJ_BUILD=OFF \ 178 | -DLLVM_TOOL_LLVM_READTAPI_BUILD=OFF \ 179 | -DLLVM_TOOL_LLVM_REDUCE_BUILD=OFF \ 180 | -DLLVM_TOOL_LLVM_REMARKUTIL_BUILD=OFF \ 181 | -DLLVM_TOOL_LLVM_RTDYLD_BUILD=OFF \ 182 | -DLLVM_TOOL_LLVM_RUST_DEMANGLE_FUZZER_BUILD=OFF \ 183 | -DLLVM_TOOL_LLVM_SHLIB_BUILD=OFF \ 184 | -DLLVM_TOOL_LLVM_SIM_BUILD=OFF \ 185 | -DLLVM_TOOL_LLVM_SIZE_BUILD=ON \ 186 | -DLLVM_TOOL_LLVM_SPECIAL_CASE_LIST_FUZZER_BUILD=OFF \ 187 | -DLLVM_TOOL_LLVM_SPLIT_BUILD=OFF \ 188 | -DLLVM_TOOL_LLVM_STRESS_BUILD=OFF \ 189 | -DLLVM_TOOL_LLVM_STRINGS_BUILD=OFF \ 190 | -DLLVM_TOOL_LLVM_SYMBOLIZER_BUILD=ON \ 191 | -DLLVM_TOOL_LLVM_TLI_CHECKER_BUILD=OFF \ 192 | -DLLVM_TOOL_LLVM_UNDNAME_BUILD=OFF \ 193 | -DLLVM_TOOL_LLVM_XRAY_BUILD=OFF \ 194 | -DLLVM_TOOL_LLVM_YAML_NUMERIC_PARSER_FUZZER_BUILD=OFF \ 195 | -DLLVM_TOOL_LLVM_YAML_PARSER_FUZZER_BUILD=OFF \ 196 | -DLLVM_TOOL_LTO_BUILD=OFF \ 197 | -DLLVM_TOOL_OBJ2YAML_BUILD=OFF \ 198 | -DLLVM_TOOL_OPT_BUILD=OFF \ 199 | -DLLVM_TOOL_OPT_VIEWER_BUILD=OFF \ 200 | -DLLVM_TOOL_REDUCE_CHUNK_LIST_BUILD=OFF \ 201 | -DLLVM_TOOL_REMARKS_SHLIB_BUILD=OFF \ 202 | -DLLVM_TOOL_SANCOV_BUILD=OFF \ 203 | -DLLVM_TOOL_SANSTATS_BUILD=OFF \ 204 | -DLLVM_TOOL_SPIRV_TOOLS_BUILD=OFF \ 205 | -DLLVM_TOOL_VERIFY_USELISTORDER_BUILD=OFF \ 206 | -DLLVM_TOOL_VFABI_DEMANGLE_FUZZER_BUILD=OFF \ 207 | -DLLVM_TOOL_XCODE_TOOLCHAIN_BUILD=OFF \ 208 | -DLLVM_TOOL_YAML2OBJ_BUILD=OFF \ 209 | -DLLVM_ENABLE_PROJECTS="clang;lld" \ 210 | -DCLANG_ENABLE_ARCMT=OFF \ 211 | -DCLANG_ENABLE_STATIC_ANALYZER=OFF \ 212 | -DCLANG_INCLUDE_TESTS=OFF \ 213 | -DCLANG_BUILD_TOOLS=OFF \ 214 | -DCLANG_TOOL_CLANG_SCAN_DEPS_BUILD=OFF \ 215 | -DCLANG_TOOL_CLANG_INSTALLAPI_BUILD=OFF \ 216 | -DCLANG_BUILD_EXAMPLES=OFF \ 217 | -DCLANG_INCLUDE_DOCS=OFF \ 218 | -DCLANG_LINKS_TO_CREATE="clang;clang++" \ 219 | -DLLD_BUILD_TOOLS=OFF \ 220 | -DCMAKE_INSTALL_PREFIX=llvm-prefix 221 | # The "all" target still contains far too much stuff, even given all the options above, so build 222 | # only Clang/LLD, explicitly. For the same reason using the "install" target is infeasible. 223 | # I spent a while trying and it leads nowhere. 224 | cmake --build llvm-build --target llvm-driver 225 | cmake --build llvm-build --target install-core-resource-headers 226 | 227 | # Install the driver (which is a multi-call binary) manually. 228 | mkdir -p wasi-prefix/bin 229 | cp llvm-build/bin/llvm wasi-prefix/bin/llvm.wasm 230 | 231 | # Install "core" headers (which are a part of the compiler and not the libc) manually. 232 | # Having the LLVM major version is an inconvenience in our case, since the compiler data files 233 | # are embedded into the binary and they won't ever conflict between versions. 234 | cp -r llvm-prefix/lib/clang/${LLVM_VERSION_MAJOR}/* wasi-prefix/ 235 | 236 | # Options below heavily based on wasi-sdk. 237 | mkdir -p compiler-rt-build 238 | cmake -B compiler-rt-build -S llvm-src/compiler-rt \ 239 | -DCMAKE_TOOLCHAIN_FILE=../Toolchain-WASI.cmake \ 240 | -DCOMPILER_RT_BAREMETAL_BUILD=ON \ 241 | -DCOMPILER_RT_BUILD_XRAY=OFF \ 242 | -DCOMPILER_RT_INCLUDE_TESTS=OFF \ 243 | -DCOMPILER_RT_HAS_FPIC_FLAG=OFF \ 244 | -DCOMPILER_RT_ENABLE_IOS=OFF \ 245 | -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \ 246 | -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON \ 247 | -DCMAKE_INSTALL_PREFIX=wasi-prefix 248 | cmake --build compiler-rt-build --target install 249 | 250 | make -C wasi-libc-src \ 251 | CC=${WASI_SDK_PATH}/bin/clang \ 252 | AR=${WASI_SDK_PATH}/bin/ar \ 253 | NM=${WASI_SDK_PATH}/bin/nm \ 254 | TARGET_TRIPLE=${WASI_TARGET} \ 255 | SYSROOT=$(pwd)/wasi-prefix 256 | 257 | # Options below heavily based on wasi-sdk. 258 | mkdir -p libcxx-build 259 | cmake -B libcxx-build -S llvm-src/runtimes \ 260 | -DCMAKE_TOOLCHAIN_FILE=../Toolchain-WASI.cmake \ 261 | -DLLVM_ENABLE_RUNTIMES:STRING="libcxx;libcxxabi" \ 262 | -DLIBCXX_ENABLE_THREADS:BOOL=OFF \ 263 | -DLIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \ 264 | -DLIBCXX_ENABLE_SHARED:BOOL=OFF \ 265 | -DLIBCXX_ENABLE_EXCEPTIONS:BOOL=OFF \ 266 | -DLIBCXX_ENABLE_FILESYSTEM:BOOL=ON \ 267 | -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL=OFF \ 268 | -DLIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL=OFF \ 269 | -DLIBCXX_CXX_ABI=libcxxabi \ 270 | -DLIBCXX_CXX_ABI_INCLUDE_PATHS=$(pwd)/llvm-src/libcxxabi/include \ 271 | -DLIBCXX_HAS_MUSL_LIBC:BOOL=ON \ 272 | -DLIBCXX_ABI_VERSION=2 \ 273 | -DLIBCXXABI_ENABLE_THREADS:BOOL=OFF \ 274 | -DLIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \ 275 | -DLIBCXXABI_ENABLE_PIC:BOOL=OFF \ 276 | -DLIBCXXABI_ENABLE_SHARED:BOOL=OFF \ 277 | -DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF \ 278 | -DLIBCXXABI_USE_LLVM_UNWINDER:BOOL=OFF \ 279 | -DLIBCXXABI_SILENT_TERMINATE:BOOL=ON \ 280 | -DLIBCXX_LIBDIR_SUFFIX=/${WASI_TARGET} \ 281 | -DLIBCXXABI_LIBDIR_SUFFIX=/${WASI_TARGET} \ 282 | -DCMAKE_INSTALL_PREFIX=wasi-prefix 283 | cmake --build libcxx-build --target install 284 | 285 | # Crimees. For testing only! 286 | cp driverdriver.py wasi-prefix/bin/ 287 | for tool in $(__DRIVERDRIVER_LIST=1 ./driverdriver.py); do 288 | ln -sf driverdriver.py wasi-prefix/bin/${tool} 289 | done 290 | -------------------------------------------------------------------------------- /driverdriver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import os 4 | import sys 5 | import shlex 6 | import subprocess 7 | 8 | 9 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 10 | LAUNCHER = [ 11 | "wasmtime", "run", 12 | "--dir", f"{ROOT_DIR}::/wasi", 13 | "--dir", "/tmp", 14 | "--dir", "/", 15 | "--dir", ".", 16 | f"{ROOT_DIR}/bin/llvm.wasm", 17 | ] 18 | DIR_ARGS = [ 19 | "--sysroot", "/wasi", 20 | "-resource-dir", "/wasi", 21 | ] 22 | 23 | 24 | def run_llvm_tool(args): 25 | arg0, *args = args 26 | return subprocess.call([*LAUNCHER, os.path.basename(arg0), *args]) 27 | 28 | 29 | def run_clang_driver(args): 30 | arg0, *args = args 31 | if "-###" in args: 32 | return run_llvm_tool([arg0, *DIR_ARGS, *args]) 33 | output = subprocess.check_output( 34 | [*LAUNCHER, os.path.basename(arg0), "-###", *DIR_ARGS, *args], 35 | stderr=subprocess.STDOUT, 36 | text=True, 37 | ) 38 | # horrific in-band signaling code. please do not hold me to account for writing this 39 | state = 0 40 | commands = [] 41 | for line in output.splitlines(): 42 | if state == 0: 43 | if not line.startswith( 44 | ("clang", "Target:", "Thread model:", "InstalledDir:", "Build config:")): 45 | state = 1 46 | if state == 1: 47 | if line == " (in-process)": # doesn't seem to be used with `llvm` driver enableds 48 | pass 49 | elif line.startswith(" \""): 50 | commands.append(shlex.split(line)) 51 | else: 52 | state = 2 53 | if state == 2: 54 | pass 55 | if state == 1: # valid `-###` output recognized 56 | for command in commands: 57 | if command[0] == "": # clang would normally run this (usually cc1/cc1as) in-process 58 | del command[0] 59 | exit_code = run_llvm_tool(command) 60 | if exit_code != 0: 61 | return exit_code 62 | else: # something else, perhaps an error? just display it 63 | print(output) 64 | 65 | 66 | RUNNERS = { 67 | "addr2line": run_llvm_tool, 68 | "size": run_llvm_tool, 69 | "objdump": run_llvm_tool, 70 | "objcopy": run_llvm_tool, 71 | "strip": run_llvm_tool, 72 | "c++filt": run_llvm_tool, 73 | "ar": run_llvm_tool, 74 | "ranlib": run_llvm_tool, 75 | "wasm-ld": run_llvm_tool, 76 | "clang": run_clang_driver, 77 | "clang++": run_clang_driver, 78 | } 79 | 80 | 81 | if __name__ == "__main__": 82 | if os.getenv("__DRIVERDRIVER_LIST") == "1": 83 | print(" ".join(runner for runner in RUNNERS if runner)) 84 | else: 85 | sys.exit(RUNNERS[os.path.basename(sys.argv[0])](sys.argv)) 86 | --------------------------------------------------------------------------------