diff --git a/build-engine.sh b/build-engine.sh index 92b9537..5afdcdd 100755 --- a/build-engine.sh +++ b/build-engine.sh @@ -25,39 +25,53 @@ ac_add_options --disable-clang-plugin ac_add_options --enable-jitspew ac_add_options --enable-optimize=-O3 ac_add_options --enable-js-streams +ac_add_options --disable-shared-memory +ac_add_options --wasm-no-experimental +ac_add_options --disable-wasm-extended-const +ac_add_options --disable-js-shell ac_add_options --enable-portable-baseline-interp +ac_add_options --disable-cargo-incremental ac_add_options --prefix=${working_dir}/${objdir}/dist mk_add_options MOZ_OBJDIR=${working_dir}/${objdir} mk_add_options AUTOCLOBBER=1 + EOF -target="$(uname)" -case "$target" in - Linux) - echo "ac_add_options --disable-stdcxx-compat" >> "$mozconfig" - ;; +if [[ "$mode" == "release" ]]; then +cat << EOF >> "$mozconfig" +ac_add_options --enable-strip +ac_add_options --disable-debug +EOF +else +cat << EOF >> "$mozconfig" +ac_add_options --enable-debug +EOF +fi - Darwin) - echo "ac_add_options --host=aarch64-apple-darwin" >> "$mozconfig" - ;; +cat << EOF >> "$mozconfig" +export RUSTFLAGS="-C relocation-model=pic" +export CARGOFLAGS="-Z build-std=panic_abort,std" +export CFLAGS="-fPIC" +export CXXFLAGS="-fPIC" - *) - echo "Unsupported build target: $target" - exit 1 - ;; -esac +EOF -case "$mode" in - release) - echo "ac_add_options --disable-debug" >> "$mozconfig" +# Note: the var name is chosen to not conflict with the one used by the toolchain +OS_NAME="$(uname | tr '[:upper:]' '[:lower:]')" +export OS_NAME + +case "$OS_NAME" in + linux) + echo "ac_add_options --disable-stdcxx-compat" >> "$mozconfig" ;; - debug) - echo "ac_add_options --enable-debug" >> "$mozconfig" + darwin) + echo "ac_add_options --host=aarch64-apple-darwin" >> "$mozconfig" + OS_NAME="macos" ;; *) - echo "Unknown build type: $mode" + echo "Can't build on OS $OS_NAME" exit 1 ;; esac @@ -65,8 +79,32 @@ esac # Ensure the Rust version matches that used by Gecko, and can compile to WASI rustup target add wasm32-wasi +rustup component add rust-src + +# Ensure that the expected WASI-SDK is installed +if [[ -z "${WASI_SDK_PREFIX:-}" ]]; then + sdk_url="$(sed "s/\$OS_NAME/$OS_NAME/g" "$script_dir/wasi-sdk-url")" + echo "WASI_SDK_PREFIX not set, downloading SDK from ${sdk_url} ..." + mkdir -p wasi-sdk + cd wasi-sdk + curl -LO "$sdk_url" + sdk_file="$(basename "$sdk_url")" + tar -xf "$sdk_file" + rm "$sdk_file" + WASI_SDK_PREFIX=$PWD/$(ls . | head -n 1) + export WASI_SDK_PREFIX + cd .. + echo "Downloaded and extracted. Using compiler and sysroot at ${WASI_SDK_PREFIX} for target compilation" +else + if [[ ! -d "${WASI_SDK_PREFIX}" ]]; then + echo "WASI_SDK_PREFIX set, but directory does not exist: ${WASI_SDK_PREFIX}" + exit 1 + fi + echo "WASI_SDK_PREFIX set, using compiler and sysroot at ${WASI_SDK_PREFIX} for target compilation" +fi -fetch_commits= +# If the Gecko repository hasn't been cloned yet, do so now. +# Otherwise, assume it's in the right state already. if [[ ! -a gecko-dev ]]; then # Clone Gecko repository at the required revision @@ -76,46 +114,59 @@ if [[ ! -a gecko-dev ]]; then git -C gecko-dev remote add --no-tags -t wasi-embedding \ origin "$(cat "$script_dir/gecko-repository")" - fetch_commits=1 -fi - -target_rev="$(cat "$script_dir/gecko-revision")" -if [[ -n "$fetch_commits" ]] || \ - [[ "$(git -C gecko-dev rev-parse HEAD)" != "$target_rev" ]]; then - git -C gecko-dev fetch --depth 1 origin "$target_rev" - git -C gecko-dev checkout FETCH_HEAD + target_rev="$(cat "$script_dir/gecko-revision")" + if [[ "$(git -C gecko-dev rev-parse HEAD)" != "$target_rev" ]]; then + git -C gecko-dev fetch --depth 1 origin "$target_rev" + git -C gecko-dev checkout FETCH_HEAD + fi fi -# Use Gecko's build system bootstrapping to ensure all dependencies are -# installed +## Use Gecko's build system bootstrapping to ensure all dependencies are +## installed cd gecko-dev ./mach --no-interactive bootstrap --application-choice=js --no-system-changes -# ... except, that doesn't install the wasi-sysroot, which we need, so we do -# that manually. -cd ~/.mozbuild -python3 \ - "${working_dir}/gecko-dev/mach" \ - --no-interactive \ - artifact \ - toolchain \ - --bootstrap \ - --from-build \ - sysroot-wasm32-wasi - cd "$working_dir" +export CC=${WASI_SDK_PREFIX}/bin/clang +export CXX=${WASI_SDK_PREFIX}/bin/clang++ +export AR=${WASI_SDK_PREFIX}/bin/llvm-ar +export HOST_CC=~/.mozbuild/clang/bin/clang +export HOST_CXX=~/.mozbuild/clang/bin/clang++ +export HOST_AR=~/.mozbuild/clang/bin/llvm-ar + # Build SpiderMonkey for WASI MOZCONFIG="${mozconfig}" \ MOZ_FETCHES_DIR=~/.mozbuild \ -CC=~/.mozbuild/clang/bin/clang \ -CXX=~/.mozbuild/clang/bin/clang++ \ -AR=~/.mozbuild/clang/bin/llvm-ar \ python3 "${working_dir}/gecko-dev/mach" \ --no-interactive \ - build + configure + +# Always use our own WASI sysroot, not the one mozbuild might have downloaded. +rm -rf ~/.mozbuild/sysroot-wasm32-wasi +ln -s "${WASI_SDK_PREFIX}/share/wasi-sysroot" ~/.mozbuild/sysroot-wasm32-wasi + +MOZCONFIG="${mozconfig}" \ +MOZ_FETCHES_DIR=~/.mozbuild \ + python3 "${working_dir}/gecko-dev/mach" \ + --no-interactive \ + configure + +cd "$objdir" +# Before actually building, we need to overwrite the .cargo/config file that mozbuild created, +# because otherwise we can't build the Rust stdlib with -fPIC. +# That file is created during the `pre-export` build step, so we run that now, then overwrite +# the file, then build. +make pre-export + +if [[ -f .cargo/config ]]; then + echo "" > .cargo/config +fi + +make -j$(nproc) -s # Copy header, object, and static lib files +cd .. rm -rf "$outdir" mkdir -p "$outdir/lib" @@ -126,4 +177,8 @@ while read -r file; do cp "$file" "../$outdir/lib" done < "$script_dir/object-files.list" -cp js/src/build/libjs_static.a "wasm32-wasi/${mode}/libjsrust.a" "../$outdir/lib" +cp js/src/build/libjs_static.a "../$outdir/lib" + +if [[ -f "wasm32-wasi/${mode}/libjsrust.a" ]]; then +cp "wasm32-wasi/${mode}/libjsrust.a" "../$outdir/lib" +fi diff --git a/gecko-revision b/gecko-revision index c0c1b00..86ad283 100644 --- a/gecko-revision +++ b/gecko-revision @@ -1 +1 @@ -f9cccc2d2b173c1164f603135894affef4ed43fe +0153ea840aeddb4382ad4decca96a9a30edf1179 diff --git a/rust-toolchain.toml b/rust-toolchain.toml index d3704db..80c28b7 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "1.68.2" +channel = "nightly" targets = [ "wasm32-wasi" ] profile = "minimal" diff --git a/wasi-sdk-url b/wasi-sdk-url new file mode 100644 index 0000000..7628214 --- /dev/null +++ b/wasi-sdk-url @@ -0,0 +1 @@ +https://github.com/dicej/wasi-sdk/releases/download/filesystem-fix-v3/wasi-sdk-20.25g964191c91aac-$OS_NAME.tar.gz