Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
02559fb
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
a614b50
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
9d8da8e
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
33ca2fb
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
65d0dec
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
1bcf951
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
36d5e5c
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
ee5d9ae
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
07745d8
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
8931f14
fix localtime timespec
rennergade Mar 25, 2026
d24fea7
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
781295d
compilation scripts for gcc and libcpp
rennergade Mar 25, 2026
e6c59db
compilation scripts for gcc and libcpp
rennergade Mar 26, 2026
16fecd1
compilation scripts for gcc and libcpp
rennergade Mar 26, 2026
217cd0a
compilation scripts for gcc and libcpp
rennergade Mar 26, 2026
5419290
compilation scripts for gcc and libcpp
rennergade Mar 26, 2026
b6956e1
compilation scripts for gcc and libcpp
rennergade Mar 26, 2026
e779c9a
compilation scripts for gcc and libcpp
rennergade Mar 26, 2026
e3ca0c8
compilation scripts for gcc and libcpp
rennergade Mar 26, 2026
a1a0c9e
compilation scripts for gcc and libcpp
rennergade Mar 26, 2026
bf26743
add binutils
rennergade Mar 26, 2026
9475854
add binutils
rennergade Mar 26, 2026
391d4fd
add binutils
rennergade Mar 26, 2026
dbcd0b1
Merge branch 'main' into compile-gcc-cpp
rennergade Mar 29, 2026
a9b5379
address pr comments
rennergade Apr 6, 2026
5ec9fc9
Merge branch 'main' into compile-gcc-cpp
rennergade Apr 7, 2026
1c45597
pr fixes
rennergade Apr 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 51 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ LIBTIRPC_STAMP := $(APPS_BUILD)/.stamp_libtirpc
GNULIB_STAMP := $(APPS_BUILD)/.stamp_gnulib
ZLIB_STAMP := $(APPS_BUILD)/.stamp_zlib
OPENSSL_STAMP := $(APPS_BUILD)/.stamp_openssl
LIBCXX_STAMP := $(APPS_BUILD)/.stamp_libcxx
MERGE_BASE_STAMP := $(APPS_BUILD)/.stamp_merge_base_sysroot
MERGE_TIRPC_STAMP := $(APPS_BUILD)/.stamp_merge_tirpc
MERGE_GNULIB_STAMP := $(APPS_BUILD)/.stamp_merge_gnulib
MERGE_ZLIB_STAMP := $(APPS_BUILD)/.stamp_merge_zlib
MERGE_OPENSSL_STAMP := $(APPS_BUILD)/.stamp_merge_openssl
MERGE_LIBCXX_STAMP := $(APPS_BUILD)/.stamp_merge_libcxx
MERGE_ALL_STAMP := $(APPS_BUILD)/.stamp_merge_sysroot

TOOL_ENV := $(APPS_BUILD)/.toolchain.env
Expand All @@ -41,7 +43,7 @@ TESTABLE_APPS := bash coreutils curl git grep lmbench sed tinycc
APP ?= $(TESTABLE_APPS)

# -------- Phonies -------------------------------------------------------------
.PHONY: all preflight dirs print-config check-build libtirpc gnulib zlib openssl merge-base-sysroot merge-sysroot lmbench bash nginx coreutils cpython git curl grep sed postgres clean clean-all rebuild-libs rebuild-sysroot install-bash install-nginx install-git install-curl install-grep install-sed install-lmbench install-coreutils install
.PHONY: all preflight dirs print-config check-build libtirpc gnulib zlib openssl libcxx merge-base-sysroot merge-sysroot lmbench bash nginx coreutils cpython git curl grep sed gcc binutils postgres clean clean-all rebuild-libs rebuild-sysroot install-bash install-nginx install-git install-curl install-grep install-sed install-lmbench install-coreutils install-gcc install-binutils install

all: preflight libtirpc gnulib merge-sysroot lmbench bash

Expand Down Expand Up @@ -144,6 +146,14 @@ $(OPENSSL_STAMP): $(APPS_ROOT)/openssl/compile_openssl.sh | $(TOOL_ENV)

openssl: $(OPENSSL_STAMP)

# ---------------- libc++ (via compile_libcxx.sh) ------------------------------
$(LIBCXX_STAMP): $(APPS_ROOT)/llvm-project/compile_libcxx.sh | $(TOOL_ENV)
. '$(TOOL_ENV)'
JOBS='$(JOBS)' '$(APPS_ROOT)/llvm-project/compile_libcxx.sh'
touch '$@'

libcxx: $(LIBCXX_STAMP)

# ---------------- Merge sysroot + overlay -------------------------------------
$(MERGE_BASE_STAMP): | $(TOOL_ENV)
@echo "[merge] refreshing base merged sysroot"
Expand Down Expand Up @@ -194,7 +204,16 @@ $(MERGE_OPENSSL_STAMP): $(MERGE_BASE_STAMP) $(OPENSSL_STAMP)
touch '$@'


$(MERGE_ALL_STAMP): $(MERGE_TIRPC_STAMP) $(MERGE_GNULIB_STAMP) $(MERGE_ZLIB_STAMP) $(MERGE_OPENSSL_STAMP)
$(MERGE_LIBCXX_STAMP): $(MERGE_BASE_STAMP) $(LIBCXX_STAMP)
# libc++ headers (into include/wasm32-wasi/c++/ so clang's -isystem finds them)
mkdir -p '$(MERGED_SYSROOT)/include/wasm32-wasi/c++'
rsync -a '$(APPS_OVERLAY)/usr/include/c++/' '$(MERGED_SYSROOT)/include/wasm32-wasi/c++/' || true
rsync -a '$(APPS_OVERLAY)/usr/lib/wasm32-wasi/' '$(MERGED_SYSROOT)/lib/wasm32-wasi/' || true
rsync -a '$(APPS_OVERLAY)/lib/wasm32-wasi/' '$(MERGED_SYSROOT)/lib/wasm32-wasi/' || true
touch '$@'


$(MERGE_ALL_STAMP): $(MERGE_TIRPC_STAMP) $(MERGE_GNULIB_STAMP) $(MERGE_ZLIB_STAMP) $(MERGE_OPENSSL_STAMP) $(MERGE_LIBCXX_STAMP)
touch '$@'

merge-sysroot: $(MERGE_ALL_STAMP)
Expand Down Expand Up @@ -259,12 +278,27 @@ sed: $(MERGE_BASE_STAMP)
. '$(TOOL_ENV)'
JOBS='$(JOBS)' '$(APPS_ROOT)/sed/compile_sed.sh'

# ---------------- gcc (WASM build) --------------------------------------------
# Uses gcc/compile_gcc.sh to cross-compile GCC cc1 as a wasm32-wasi binary.
# Requires libc++ in the merged sysroot (for compiling GCC's C++ source).
# Stages artifacts under build/gcc/usr/local/bin.
gcc: $(MERGE_LIBCXX_STAMP)
. '$(TOOL_ENV)'
JOBS='$(JOBS)' '$(APPS_ROOT)/gcc/compile_gcc.sh'

# ---------------- binutils (WASM build) ----------------------------------------
# Uses binutils/compile_binutils.sh to cross-compile ld and as as wasm32-wasi
# binaries. Pure C — no libc++ needed. Stages to build/binutils/usr/local/bin.
binutils: $(MERGE_ZLIB_STAMP)
. '$(TOOL_ENV)'
JOBS='$(JOBS)' '$(APPS_ROOT)/binutils/compile_binutils.sh'

rebuild-libs:
rm -f '$(LIBTIRPC_STAMP)' '$(GNULIB_STAMP)' '$(ZLIB_STAMP)' '$(OPENSSL_STAMP)' \
'$(MERGE_TIRPC_STAMP)' '$(MERGE_GNULIB_STAMP)' '$(MERGE_ZLIB_STAMP)' '$(MERGE_OPENSSL_STAMP)' '$(MERGE_ALL_STAMP)'
rm -f '$(LIBTIRPC_STAMP)' '$(GNULIB_STAMP)' '$(ZLIB_STAMP)' '$(OPENSSL_STAMP)' '$(LIBCXX_STAMP)' \
'$(MERGE_TIRPC_STAMP)' '$(MERGE_GNULIB_STAMP)' '$(MERGE_ZLIB_STAMP)' '$(MERGE_OPENSSL_STAMP)' '$(MERGE_LIBCXX_STAMP)' '$(MERGE_ALL_STAMP)'

rebuild-sysroot:
rm -f '$(MERGE_BASE_STAMP)' '$(MERGE_TIRPC_STAMP)' '$(MERGE_GNULIB_STAMP)' '$(MERGE_ZLIB_STAMP)' '$(MERGE_OPENSSL_STAMP)' '$(MERGE_ALL_STAMP)'
rm -f '$(MERGE_BASE_STAMP)' '$(MERGE_TIRPC_STAMP)' '$(MERGE_GNULIB_STAMP)' '$(MERGE_ZLIB_STAMP)' '$(MERGE_OPENSSL_STAMP)' '$(MERGE_LIBCXX_STAMP)' '$(MERGE_ALL_STAMP)'

# ---------------- cpython (WASM build) ----------------------------------------
# Placeholder target to preserve the per-app staging/layering pattern.
Expand Down Expand Up @@ -300,7 +334,13 @@ install-lmbench:
install-coreutils:
'$(APPS_ROOT)/scripts/post_install.sh' '$(LINDFS_ROOT)' '$(APPS_BUILD)' coreutils

install: install-bash install-nginx install-git install-curl install-grep install-sed install-lmbench install-coreutils
install-gcc:
'$(APPS_ROOT)/scripts/post_install.sh' '$(LINDFS_ROOT)' '$(APPS_BUILD)' gcc

install-binutils:
'$(APPS_ROOT)/scripts/post_install.sh' '$(LINDFS_ROOT)' '$(APPS_BUILD)' binutils

install: install-bash install-nginx install-git install-curl install-grep install-sed install-lmbench install-coreutils install-gcc install-binutils

clean:
$(MAKE) -C '$(APPS_ROOT)/lmbench/src' clean || true
Expand All @@ -309,6 +349,9 @@ clean:
-rm -rf '$(APPS_BIN_DIR)/nginx'
-$(MAKE) -C '$(APPS_ROOT)/nginx' clean || true
-rm -rf '$(APPS_OVERLAY)' '$(MERGED_SYSROOT)' '$(APPS_BIN_DIR)' '$(APPS_LIB_DIR)' '$(TOOL_ENV)'
-rm -f '$(LIBTIRPC_STAMP)' '$(GNULIB_STAMP)' '$(ZLIB_STAMP)' '$(OPENSSL_STAMP)'
-rm -f '$(MERGE_BASE_STAMP)' '$(MERGE_TIRPC_STAMP)' '$(MERGE_GNULIB_STAMP)' '$(MERGE_ZLIB_STAMP)' '$(MERGE_OPENSSL_STAMP)' '$(MERGE_ALL_STAMP)'
'$(APPS_ROOT)/gcc/clean.sh'
'$(APPS_ROOT)/binutils/clean.sh'
'$(APPS_ROOT)/llvm-project/clean.sh'
-rm -f '$(LIBTIRPC_STAMP)' '$(GNULIB_STAMP)' '$(ZLIB_STAMP)' '$(OPENSSL_STAMP)' '$(LIBCXX_STAMP)'
-rm -f '$(MERGE_BASE_STAMP)' '$(MERGE_TIRPC_STAMP)' '$(MERGE_GNULIB_STAMP)' '$(MERGE_ZLIB_STAMP)' '$(MERGE_OPENSSL_STAMP)' '$(MERGE_LIBCXX_STAMP)' '$(MERGE_ALL_STAMP)'
$(MAKE) -C '$(APPS_ROOT)/libtirpc' distclean || true
11 changes: 11 additions & 0 deletions binutils/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
APPS_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
APPS_BUILD="$APPS_ROOT/build"

rm -rf "$APPS_BUILD/binutils-build"
rm -rf "$APPS_BUILD/binutils"
rm -f "$SCRIPT_DIR/ld.wasm" "$SCRIPT_DIR/ld.opt.wasm" "$SCRIPT_DIR/ld.opt.cwasm"
rm -f "$SCRIPT_DIR/as.wasm" "$SCRIPT_DIR/as.opt.wasm" "$SCRIPT_DIR/as.opt.cwasm"
224 changes: 224 additions & 0 deletions binutils/compile_binutils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
#!/usr/bin/env bash
set -euo pipefail

###############################################################################
# Binutils WASI build helper for lind-wasm-apps
#
# Cross-compiles GNU Binutils 2.46.0 (ld, as) to wasm32-wasi so that the
# linker and assembler can run inside the Lind sandbox alongside cc1 from GCC.
# The resulting tools target x86_64-linux-gnu (matching GCC's --target).
#
# High-level strategy:
# 1. Out-of-tree build
# 2. Configure with --host=wasm32-unknown-wasi, --target=x86_64-linux-gnu
# Generator tools are built natively via CC_FOR_BUILD; ld/as are cross-
# compiled to wasm32 via CC.
# 3. Build with 'make all-ld all-gas' (linker + assembler only)
# 4. Stage ld and as, post-process with wasm-opt + lind-boot
#
# Prerequisites:
# - Run 'make preflight merge-sysroot' first
# - Native gcc must be on PATH (for CC_FOR_BUILD)
###############################################################################

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
APPS_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
BINUTILS_SRC="$APPS_ROOT/binutils"

APPS_BUILD="$APPS_ROOT/build"
MERGED_SYSROOT="$APPS_BUILD/sysroot_merged"
STAGE_DIR="$APPS_BUILD/binutils/usr/local/bin"
TOOL_ENV="$APPS_BUILD/.toolchain.env"

if [[ -z "${LIND_WASM_ROOT:-}" ]]; then
LIND_WASM_ROOT="$(cd "$APPS_ROOT/.." && pwd)"
fi

WASM_OPT="${WASM_OPT:-$LIND_WASM_ROOT/tools/binaryen/bin/wasm-opt}"
LIND_BOOT="${LIND_BOOT:-$LIND_WASM_ROOT/build/lind-boot}"

JOBS="${JOBS:-$(nproc 2>/dev/null || getconf _NPROCESSORS_ONLN || echo 4)}"

# ----------------------------------------------------------------------
# 1) Load toolchain from Makefile preflight
# ----------------------------------------------------------------------
if [[ -r "$TOOL_ENV" ]]; then
# shellcheck disable=SC1090
. "$TOOL_ENV"
else
echo "[binutils] ERROR: missing toolchain env '$TOOL_ENV' (run 'make preflight' first)" >&2
exit 1
fi

: "${CLANG:?missing CLANG in $TOOL_ENV}"
: "${AR:?missing AR in $TOOL_ENV}"
: "${RANLIB:?missing RANLIB in $TOOL_ENV}"
: "${NM:?missing NM in $TOOL_ENV}"

# Sanity checks
if [[ ! -d "$BINUTILS_SRC" ]]; then
echo "[binutils] ERROR: source dir not found at '$BINUTILS_SRC'" >&2
exit 1
fi
if [[ ! -d "$MERGED_SYSROOT" ]]; then
echo "[binutils] ERROR: merged sysroot '$MERGED_SYSROOT' not found. Run 'make merge-sysroot' first." >&2
exit 1
fi
if ! command -v gcc &>/dev/null; then
echo "[binutils] ERROR: native gcc required for CC_FOR_BUILD but not found on PATH" >&2
exit 1
fi

mkdir -p "$STAGE_DIR"

# ----------------------------------------------------------------------
# 2) WASM toolchain flags
# ----------------------------------------------------------------------
CC_WASM="$CLANG --target=wasm32-unknown-wasi --sysroot=$MERGED_SYSROOT \
-pthread -matomics -mbulk-memory \
-I$MERGED_SYSROOT/include -I$MERGED_SYSROOT/include/wasm32-wasi"

CFLAGS_WASM="-Os"

LDFLAGS_WASM="-Wl,--import-memory,--export-memory,--max-memory=67108864 \
-Wl,--export=__stack_pointer,--export=__stack_low \
-L$MERGED_SYSROOT/lib/wasm32-wasi \
-L$MERGED_SYSROOT/usr/lib/wasm32-wasi"

echo "[binutils] using CLANG = $CLANG"
echo "[binutils] using AR = $AR"
echo "[binutils] LIND_WASM_ROOT = $LIND_WASM_ROOT"
echo "[binutils] merged sysroot = $MERGED_SYSROOT"
echo "[binutils] stage dir = $STAGE_DIR"
echo "[binutils] CC_WASM = $CC_WASM"
echo

# ----------------------------------------------------------------------
# 3) Out-of-tree build directory
# ----------------------------------------------------------------------
BINUTILS_BUILD="$APPS_BUILD/binutils-build"
mkdir -p "$BINUTILS_BUILD"

echo "[binutils] build dir = $BINUTILS_BUILD"

# ----------------------------------------------------------------------
# 4) Configure
# ----------------------------------------------------------------------
BUILD_TRIPLET="$(gcc -dumpmachine 2>/dev/null || echo x86_64-linux-gnu)"

echo "[binutils] configuring…"
echo "[binutils] --build=$BUILD_TRIPLET"
echo "[binutils] --host=wasm32-unknown-wasi"
echo "[binutils] --target=x86_64-linux-gnu"

pushd "$BINUTILS_BUILD" >/dev/null

"$BINUTILS_SRC/configure" \
--build="$BUILD_TRIPLET" \
--host=wasm32-unknown-wasi \
--target=x86_64-linux-gnu \
--disable-shared \
--enable-static \
--disable-nls \
--disable-plugins \
--disable-gdb \
--disable-gdbserver \
--disable-gprofng \
--disable-sim \
--disable-gold \
--enable-ld=yes \
--disable-werror \
--disable-largefile \
--without-zstd \
--with-system-zlib \
CC_FOR_BUILD=gcc \
AR_FOR_BUILD=ar \
CFLAGS_FOR_BUILD="-O2 -g" \
LDFLAGS_FOR_BUILD="" \
CC="$CC_WASM" \
AR="$AR" \
NM="$NM" \
RANLIB="$RANLIB" \
CFLAGS="$CFLAGS_WASM" \
LDFLAGS="$LDFLAGS_WASM"

if [[ ! -f Makefile ]]; then
echo "[binutils] ERROR: configure failed to produce Makefile." >&2
exit 1
fi

# ----------------------------------------------------------------------
# 5) Build ld and as
# ----------------------------------------------------------------------
echo "[binutils] building (make all-ld all-gas)…"
make all-ld all-gas -j"$JOBS" V=1 MAKEINFO=true

popd >/dev/null

# ----------------------------------------------------------------------
# 6) Post-process helper
# ----------------------------------------------------------------------
post_process_binary() {
local NAME="$1"
local SRC_BIN="$2"

if [[ ! -f "$SRC_BIN" ]]; then
echo "[binutils] ERROR: $NAME binary not produced at '$SRC_BIN'" >&2
return 1
fi

local WASM_FILE="$SCRIPT_DIR/${NAME}.wasm"
local OPT_WASM="$SCRIPT_DIR/${NAME}.opt.wasm"
cp "$SRC_BIN" "$WASM_FILE"
echo "[binutils] $NAME binary size: $(du -h "$WASM_FILE" | cut -f1)"

# --- wasm-opt ---
if [[ -x "$WASM_OPT" ]]; then
echo "[binutils] running wasm-opt on $NAME (epoch-injection + asyncify + O2)…"
"$WASM_OPT" --epoch-injection --asyncify \
--debuginfo -O2 \
"$WASM_FILE" -o "$OPT_WASM"
else
echo "[binutils] ERROR: wasm-opt not found at '$WASM_OPT'; exiting." >&2
exit 1
fi

if [[ ! -f "$OPT_WASM" ]]; then
echo "[binutils] ERROR: failed to generate '$OPT_WASM'; exiting." >&2
exit 1
fi

# --- lind-boot --precompile ---
if [[ ! -x "$LIND_BOOT" ]]; then
echo "[binutils] ERROR: lind-boot not found at '$LIND_BOOT'" >&2
exit 1
fi

echo "[binutils] generating cwasm for $NAME via lind-boot --precompile…"
"$LIND_BOOT" --precompile "$OPT_WASM"

local OPT_CWASM="$SCRIPT_DIR/${NAME}.opt.cwasm"
if [[ ! -f "$OPT_CWASM" ]]; then
echo "[binutils] ERROR: precompile produced no .cwasm for $NAME" >&2
exit 1
fi

cp "$OPT_CWASM" "$STAGE_DIR/$NAME"
echo "[binutils] $NAME staged as $STAGE_DIR/$NAME (precompiled)"
}

# ----------------------------------------------------------------------
# 7) Stage ld and as
# ----------------------------------------------------------------------
LD_BIN="$BINUTILS_BUILD/ld/ld-new"
AS_BIN="$BINUTILS_BUILD/gas/as-new"
Comment thread
rennergade marked this conversation as resolved.

post_process_binary "ld" "$LD_BIN"
post_process_binary "as" "$AS_BIN"

popd >/dev/null 2>&1 || true

echo
echo "[binutils] build complete. Outputs under:"
echo " $STAGE_DIR"
ls -lh "$STAGE_DIR" || true
10 changes: 10 additions & 0 deletions gcc/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
APPS_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
APPS_BUILD="$APPS_ROOT/build"

rm -rf "$APPS_BUILD/gcc-build"
rm -rf "$APPS_BUILD/gcc"
rm -f "$SCRIPT_DIR/cc1.wasm" "$SCRIPT_DIR/cc1.opt.wasm" "$SCRIPT_DIR/cc1.opt.cwasm"
Loading