Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Jul 14, 2024
0 parents commit 86d7c61
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patreon: whitequark
46 changes: 46 additions & 0 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
on: [push, pull_request]
name: Build & publish
jobs:
build:
if: ${{ !contains(github.event.head_commit.message, 'skip ci') }}
runs-on: ubuntu-latest
env:
RELEASE_BRANCH: ${{ startsWith(github.event.ref, 'refs/heads/develop-') || startsWith(github.event.ref, 'refs/heads/release-') }}
steps:
- name: Check out source code
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: true # not recursive!
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip build
sudo apt update
sudo apt-get install flex bison ccache
- name: Set up caching
uses: actions/cache@v4
with:
path: ~/.cache/ccache
key: llvm-${{ hashFiles('llvm-src', 'build.sh') }}
restore-keys: |
llvm-${{ hashFiles('llvm-src', 'build.sh') }}
llvm-
- name: Set up ccache
run: |
ccache --max-size=10G -z
- name: Build WASM binaries
run: |
MAKEFLAGS=-j$(nproc) ./build.sh
- name: Upload WASM artifact
uses: actions/upload-artifact@v4
with:
name: dist-prefix
path: wasi-prefix
# ... snip...
- name: Print ccache statistics
run: |
ccache -s
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/wasi-sdk-*.*
*.wasm

/*-build
/*-prefix
/Toolchain-WASI*.cmake
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "llvm-src"]
path = llvm-src
url = https://github.com/YoWASP/llvm-project
[submodule "wasi-sdk-src"]
path = wasi-sdk-src
url = https://github.com/WebAssembly/wasi-sdk
[submodule "wasi-libc-src"]
path = wasi-libc-src
url = https://github.com/WebAssembly/wasi-libc
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
YoWASP Clang packages
=====================

Building
--------

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.

To reduce maintenance overhead, the only development environment we will support for this repository is x86_64 Linux.
2 changes: 2 additions & 0 deletions Get-LLVM-Version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include(llvm-src/cmake/Modules/LLVMVersion.cmake)
message(NOTICE ${LLVM_VERSION_MAJOR})
15 changes: 15 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (C) Catherine <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
189 changes: 189 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#!/bin/sh -ex

export SOURCE_DATE_EPOCH=$(git log -1 --format=%ct)

WASI_SDK=wasi-sdk-22.0
WASI_SDK_URL=https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-22/wasi-sdk-22.0-linux.tar.gz
if ! [ -d ${WASI_SDK} ]; then curl -L ${WASI_SDK_URL} | tar xzf -; fi
WASI_SDK_PATH=$(pwd)/${WASI_SDK}

WASI_SYSROOT="--sysroot ${WASI_SDK_PATH}/share/wasi-sysroot"
WASI_TARGET="wasm32-wasip1"
WASI_CFLAGS=""
WASI_LDFLAGS=""
WASI_TARGET_LLVM="${WASI_TARGET}-threads"
WASI_CFLAGS_LLVM="${WASI_CFLAGS} -pthread"
WASI_LDFLAGS_LLVM="${WASI_LDFLAGS}"
# LLVM has some (unreachable in our configuration) calls to mmap.
WASI_CFLAGS_LLVM="${WASI_CFLAGS_LLVM} -D_WASI_EMULATED_MMAN"
WASI_LDFLAGS_LLVM="${WASI_LDFLAGS_LLVM} -lwasi-emulated-mman"
# Depending on the code being compiled, both Clang and LLD can consume unbounded amounts of memory.
WASI_LDFLAGS_LLVM="${WASI_LDFLAGS_LLVM} -Wl,--max-memory=4294967296"
# Compiling C++ code requires a lot of stack space and can overflow and corrupt the heap.
# (For example, `#include <iostream>` alone does it in a build with the default stack size.)
WASI_LDFLAGS_LLVM="${WASI_LDFLAGS_LLVM} -Wl,-z,stack-size=1048576 -Wl,--stack-first"
# Some of the host APIs that are statically required by LLVM (notably threading) are dynamically
# never used. An LTO build removes imports of these APIs, simplifying deployment
WASI_CFLAGS_LLVM="${WASI_CFLAGS_LLVM} -flto"
WASI_LDFLAGS_LLVM="${WASI_LDFLAGS_LLVM} -flto -Wl,--strip-all"

# We need two toolchain files: one for the compiler itself (which needs threads at the moment since
# -DLLVM_ENABLE_THREADS=OFF is kind of broken), and one for the runtime libs.
cat >Toolchain-WASI.cmake <<END
set(WASI TRUE)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR wasm32)
set(CMAKE_EXECUTABLE_SUFFIX ".wasm")
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_C_COMPILER ${WASI_SDK_PATH}/bin/clang)
set(CMAKE_CXX_COMPILER ${WASI_SDK_PATH}/bin/clang++)
set(CMAKE_LINKER ${WASI_SDK_PATH}/bin/wasm-ld CACHE STRING "wasienv build")
set(CMAKE_AR ${WASI_SDK_PATH}/bin/ar CACHE STRING "wasienv build")
set(CMAKE_RANLIB ${WASI_SDK_PATH}/bin/ranlib CACHE STRING "wasienv build")
END
cp Toolchain-WASI.cmake Toolchain-WASI-LLVM.cmake
cat >>Toolchain-WASI.cmake <<END
set(CMAKE_C_COMPILER_TARGET ${WASI_TARGET})
set(CMAKE_CXX_COMPILER_TARGET ${WASI_TARGET})
set(CMAKE_C_FLAGS "${WASI_SYSROOT} ${WASI_CFLAGS}" CACHE STRING "wasienv build")
set(CMAKE_CXX_FLAGS "${WASI_SYSROOT} ${WASI_CFLAGS}" CACHE STRING "wasienv build")
set(CMAKE_EXE_LINKER_FLAGS "${WASI_LDFLAGS}" CACHE STRING "wasienv build")
END
cat >>Toolchain-WASI-LLVM.cmake <<END
set(CMAKE_C_COMPILER_TARGET ${WASI_TARGET_LLVM})
set(CMAKE_CXX_COMPILER_TARGET ${WASI_TARGET_LLVM})
set(CMAKE_C_FLAGS "${WASI_SYSROOT} ${WASI_CFLAGS_LLVM}" CACHE STRING "wasienv build")
set(CMAKE_CXX_FLAGS "${WASI_SYSROOT} ${WASI_CFLAGS_LLVM}" CACHE STRING "wasienv build")
set(CMAKE_EXE_LINKER_FLAGS "${WASI_LDFLAGS_LLVM}" CACHE STRING "wasienv build")
END

# The clang binary built as `Debug` doesn't pass Wasm validation.
# (This has cost me a hour of my life.)

LLVM_VERSION_MAJOR=$(cmake -P Get-LLVM-Version.cmake 2>&1)

if ! [ -f llvm-tblgen-build/bin/llvm-tblgen -a -f llvm-tblgen-build/bin/clang-tblgen ]; then
mkdir -p llvm-tblgen-build
cmake -B llvm-tblgen-build -S llvm-src/llvm \
-DLLVM_CCACHE_BUILD=ON \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DLLVM_BUILD_RUNTIME=OFF \
-DLLVM_BUILD_TOOLS=OFF \
-DLLVM_INCLUDE_UTILS=OFF \
-DLLVM_INCLUDE_RUNTIMES=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_BENCHMARKS=OFF \
-DLLVM_INCLUDE_DOCS=OFF \
-DLLVM_TARGETS_TO_BUILD=WebAssembly \
-DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-wasi \
-DLLVM_ENABLE_PROJECTS="clang" \
-DCLANG_BUILD_EXAMPLES=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DCLANG_INCLUDE_TESTS=OFF
cmake --build llvm-tblgen-build --target llvm-tblgen --target clang-tblgen
fi

mkdir -p llvm-build
cmake -B llvm-build -S llvm-src/llvm \
-DCMAKE_TOOLCHAIN_FILE=../Toolchain-WASI-LLVM.cmake \
-DLLVM_CCACHE_BUILD=ON \
-DLLVM_NATIVE_TOOL_DIR=$(pwd)/llvm-tblgen-build/bin \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_BUILD_SHARED_LIBS=OFF \
-DLLVM_ENABLE_PIC=OFF \
-DLLVM_BUILD_STATIC=ON \
-DLLVM_ENABLE_THREADS=OFF \
-DLLVM_BUILD_RUNTIME=OFF \
-DLLVM_BUILD_TOOLS=OFF \
-DLLVM_INCLUDE_UTILS=OFF \
-DLLVM_BUILD_UTILS=OFF \
-DLLVM_INCLUDE_RUNTIMES=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_BENCHMARKS=OFF \
-DLLVM_INCLUDE_DOCS=OFF \
-DLLVM_TARGETS_TO_BUILD=WebAssembly \
-DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-wasip1 \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DCLANG_ENABLE_ARCMT=OFF \
-DCLANG_ENABLE_STATIC_ANALYZER=OFF \
-DCLANG_INCLUDE_TESTS=OFF \
-DCLANG_BUILD_TOOLS=OFF \
-DCLANG_BUILD_EXAMPLES=OFF \
-DCLANG_INCLUDE_DOCS=OFF \
-DCLANG_LINKS_TO_CREATE="clang;clang++" \
-DLLD_BUILD_TOOLS=OFF \
-DCMAKE_INSTALL_PREFIX=llvm-prefix
# The "all" target still contains far too much stuff, even given all the options above, so build
# only Clang/LLD, explicitly. For the same reason using the "install" target is infeasible.
# I spent a while trying and it leads nowhere.
cmake --build llvm-build --target clang --target lld
cmake --build llvm-build --target install-core-resource-headers

mkdir -p wasi-prefix/bin
cp llvm-build/bin/clang wasi-prefix/bin/clang.wasm
cp llvm-build/bin/wasm-ld wasi-prefix/bin/wasm-ld.wasm
cp -r llvm-prefix/lib/clang/${LLVM_VERSION_MAJOR}/* wasi-prefix/

# Options below heavily based on wasi-sdk.
mkdir -p compiler-rt-build
cmake -B compiler-rt-build -S llvm-src/compiler-rt \
-DCMAKE_TOOLCHAIN_FILE=../Toolchain-WASI.cmake \
-DCOMPILER_RT_BAREMETAL_BUILD=ON \
-DCOMPILER_RT_BUILD_XRAY=OFF \
-DCOMPILER_RT_INCLUDE_TESTS=OFF \
-DCOMPILER_RT_HAS_FPIC_FLAG=OFF \
-DCOMPILER_RT_ENABLE_IOS=OFF \
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON \
-DCMAKE_INSTALL_PREFIX=wasi-prefix
cmake --build compiler-rt-build --target install

make -C wasi-libc-src \
CC=${WASI_SDK_PATH}/bin/clang \
AR=${WASI_SDK_PATH}/bin/ar \
NM=${WASI_SDK_PATH}/bin/nm \
TARGET_TRIPLE=${WASI_TARGET} \
SYSROOT=$(pwd)/wasi-prefix

# Options below heavily based on wasi-sdk.
mkdir -p libcxx-build
cmake -B libcxx-build -S llvm-src/runtimes \
-DCMAKE_TOOLCHAIN_FILE=../Toolchain-WASI.cmake \
-DLLVM_ENABLE_RUNTIMES:STRING="libcxx;libcxxabi" \
-DLIBCXX_ENABLE_THREADS:BOOL=OFF \
-DLIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \
-DLIBCXX_ENABLE_SHARED:BOOL=OFF \
-DLIBCXX_ENABLE_EXCEPTIONS:BOOL=OFF \
-DLIBCXX_ENABLE_FILESYSTEM:BOOL=ON \
-DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL=OFF \
-DLIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL=OFF \
-DLIBCXX_CXX_ABI=libcxxabi \
-DLIBCXX_CXX_ABI_INCLUDE_PATHS=$(pwd)/llvm-src/libcxxabi/include \
-DLIBCXX_HAS_MUSL_LIBC:BOOL=ON \
-DLIBCXX_ABI_VERSION=2 \
-DLIBCXXABI_ENABLE_THREADS:BOOL=OFF \
-DLIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \
-DLIBCXXABI_ENABLE_PIC:BOOL=OFF \
-DLIBCXXABI_ENABLE_SHARED:BOOL=OFF \
-DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF \
-DLIBCXXABI_USE_LLVM_UNWINDER:BOOL=OFF \
-DLIBCXXABI_SILENT_TERMINATE:BOOL=ON \
-DLIBCXX_LIBDIR_SUFFIX=/${WASI_TARGET} \
-DLIBCXXABI_LIBDIR_SUFFIX=/${WASI_TARGET} \
-DCMAKE_INSTALL_PREFIX=wasi-prefix
cmake --build libcxx-build --target install

# Crimees. For testing only!
cp driverdriver.py wasi-prefix/bin/
ln -sf driverdriver.py wasi-prefix/bin/clang
ln -sf driverdriver.py wasi-prefix/bin/clang++
71 changes: 71 additions & 0 deletions driverdriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python3

import os
import sys
import shlex
import subprocess

BIN_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(BIN_DIR)

LAUNCHER = [
"wasmtime", "run",
"--dir", f"{ROOT_DIR}::/wasi",
"--dir", "/tmp",
"--dir", "/",
"--dir", ".",
]
BINARIES = {
"": [f"{BIN_DIR}/clang.wasm"],
"clang": [f"{BIN_DIR}/clang.wasm"],
"clang++": [f"{BIN_DIR}/clang.wasm", "--driver-mode=g++"],
"wasm-ld": [f"{BIN_DIR}/wasm-ld.wasm"],
}


def run_command(args):
arg0, *args = args
return subprocess.check_call([*LAUNCHER, *BINARIES[os.path.basename(arg0)], *args])


def run_driver(args):
arg0, *args = args
output = subprocess.check_output([
*LAUNCHER,
*BINARIES[os.path.basename(arg0)],
"--sysroot", "/wasi",
"-resource-dir", "/wasi",
"-###",
*args,
],
stderr=subprocess.STDOUT,
text=True,
)
# horrific in-band signaling code. please do not hold me to account for writing this
state = 0
commands = []
for line in output.splitlines():
if state == 0:
if not line.startswith(
("clang", "Target:", "Thread model:", "InstalledDir:", "Build config:")):
state = 1
if state == 1:
if line == " (in-process)":
pass
elif line.startswith(" \""):
commands.append(shlex.split(line))
else:
state = 2
if state == 2:
pass
if state == 1: # valid `-###` output recognized
for command in commands:
exit_code = run_command(command)
if exit_code != 0:
sys.exit(exit_code)
else: # something else, perhaps an error? just display it
print(output)


if __name__ == "__main__":
sys.exit(run_driver(sys.argv))
1 change: 1 addition & 0 deletions llvm-src
Submodule llvm-src added at 69bd85
1 change: 1 addition & 0 deletions wasi-libc-src
Submodule wasi-libc-src added at b9e15a

0 comments on commit 86d7c61

Please sign in to comment.