Skip to content

Commit 9783228

Browse files
committed
More work towards making shared library builds work
Specifically: - Support specifying WASI-SDK via file or env var - Use Rust Nightly toolchain - Let mozbuild manage the host compiler, but take over managing the WASI compiler and sysroot - Improve some build config settings
1 parent 429c02f commit 9783228

File tree

4 files changed

+100
-61
lines changed

4 files changed

+100
-61
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ jobs:
3131
run: |
3232
mkdir dist
3333
sudo apt-get update -y
34-
wget --no-verbose https://github.com/dicej/wasi-sdk/releases/download/filesystem-fix-v3/wasi-sdk-20.25g964191c91aac-linux.tar.gz
35-
tar -xf wasi-sdk-20.25g964191c91aac-linux.tar.gz
36-
export WASI_SDK_PREFIX=$PWD/wasi-sdk-20.25g964191c91aac
3734
bash ./build-engine.sh release
3835
tar -a -cf dist/spidermonkey-wasm-static-lib_release.tar.gz release
3936
rm -rf release obj-release

build-engine.sh

Lines changed: 98 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,86 @@ ac_add_options --disable-clang-plugin
2525
ac_add_options --enable-jitspew
2626
ac_add_options --enable-optimize=-O3
2727
ac_add_options --enable-js-streams
28+
ac_add_options --disable-shared-memory
29+
ac_add_options --wasm-no-experimental
30+
ac_add_options --disable-wasm-extended-const
31+
ac_add_options --disable-js-shell
2832
ac_add_options --enable-portable-baseline-interp
33+
ac_add_options --disable-cargo-incremental
2934
ac_add_options --prefix=${working_dir}/${objdir}/dist
3035
mk_add_options MOZ_OBJDIR=${working_dir}/${objdir}
3136
mk_add_options AUTOCLOBBER=1
37+
38+
EOF
39+
40+
if [[ "$mode" == "release" ]]; then
41+
cat << EOF >> "$mozconfig"
42+
ac_add_options --enable-strip
43+
ac_add_options --disable-debug
44+
EOF
45+
else
46+
cat << EOF >> "$mozconfig"
47+
ac_add_options --enable-debug
48+
EOF
49+
fi
50+
51+
cat << EOF >> "$mozconfig"
3252
export RUSTFLAGS="-C relocation-model=pic"
33-
export RUSTC_BOOTSTRAP=1
53+
export CARGOFLAGS="-Z build-std=panic_abort,std"
3454
export CFLAGS="-fPIC"
3555
export CXXFLAGS="-fPIC"
3656
3757
EOF
3858

39-
target="$(uname)"
40-
case "$target" in
41-
Linux)
59+
# Note: the var name is chosen to not conflict with the one used by the toolchain
60+
OS_NAME="$(uname | tr '[:upper:]' '[:lower:]')"
61+
export OS_NAME
62+
63+
case "$OS_NAME" in
64+
linux)
4265
echo "ac_add_options --disable-stdcxx-compat" >> "$mozconfig"
4366
;;
4467

45-
Darwin)
68+
darwin)
4669
echo "ac_add_options --host=aarch64-apple-darwin" >> "$mozconfig"
70+
OS_NAME="macos"
4771
;;
4872

4973
*)
50-
echo "Unsupported build target: $target"
51-
exit 1
52-
;;
53-
esac
54-
55-
case "$mode" in
56-
release)
57-
echo "ac_add_options --disable-debug" >> "$mozconfig"
58-
;;
59-
60-
debug)
61-
echo "ac_add_options --enable-debug" >> "$mozconfig"
62-
;;
63-
64-
*)
65-
echo "Unknown build type: $mode"
74+
echo "Can't build on OS $OS_NAME"
6675
exit 1
6776
;;
6877
esac
6978

7079

7180
# Ensure the Rust version matches that used by Gecko, and can compile to WASI
7281
rustup target add wasm32-wasi
82+
rustup component add rust-src
83+
84+
# Ensure that the expected WASI-SDK is installed
85+
if [[ -z "${WASI_SDK_PREFIX:-}" ]]; then
86+
sdk_url="$(sed "s/\$OS_NAME/$OS_NAME/g" "$script_dir/wasi-sdk-url")"
87+
echo "WASI_SDK_PREFIX not set, downloading SDK from ${sdk_url} ..."
88+
mkdir -p wasi-sdk
89+
cd wasi-sdk
90+
curl -LO "$sdk_url"
91+
sdk_file="$(basename "$sdk_url")"
92+
tar -xf "$sdk_file"
93+
rm "$sdk_file"
94+
WASI_SDK_PREFIX=$PWD/$(ls . | head -n 1)
95+
export WASI_SDK_PREFIX
96+
cd ..
97+
echo "Downloaded and extracted. Using compiler and sysroot at ${WASI_SDK_PREFIX} for target compilation"
98+
else
99+
if [[ ! -d "${WASI_SDK_PREFIX}" ]]; then
100+
echo "WASI_SDK_PREFIX set, but directory does not exist: ${WASI_SDK_PREFIX}"
101+
exit 1
102+
fi
103+
echo "WASI_SDK_PREFIX set, using compiler and sysroot at ${WASI_SDK_PREFIX} for target compilation"
104+
fi
73105

74-
fetch_commits=
106+
# If the Gecko repository hasn't been cloned yet, do so now.
107+
# Otherwise, assume it's in the right state already.
75108
if [[ ! -a gecko-dev ]]; then
76109

77110
# Clone Gecko repository at the required revision
@@ -81,55 +114,59 @@ if [[ ! -a gecko-dev ]]; then
81114
git -C gecko-dev remote add --no-tags -t wasi-embedding \
82115
origin "$(cat "$script_dir/gecko-repository")"
83116

84-
fetch_commits=1
117+
target_rev="$(cat "$script_dir/gecko-revision")"
118+
if [[ "$(git -C gecko-dev rev-parse HEAD)" != "$target_rev" ]]; then
119+
git -C gecko-dev fetch --depth 1 origin "$target_rev"
120+
git -C gecko-dev checkout FETCH_HEAD
121+
fi
85122
fi
86123

87-
target_rev="$(cat "$script_dir/gecko-revision")"
88-
if [[ -n "$fetch_commits" ]] || \
89-
[[ "$(git -C gecko-dev rev-parse HEAD)" != "$target_rev" ]]; then
90-
git -C gecko-dev fetch --depth 1 origin "$target_rev"
91-
git -C gecko-dev checkout FETCH_HEAD
92-
fi
93-
94-
# Use Gecko's build system bootstrapping to ensure all dependencies are
95-
# installed
124+
## Use Gecko's build system bootstrapping to ensure all dependencies are
125+
## installed
96126
cd gecko-dev
97127
./mach --no-interactive bootstrap --application-choice=js --no-system-changes
98128

99-
# ... except, that doesn't install the wasi-sysroot, which we need, so we do
100-
# that manually.
101-
cd ~/.mozbuild
102-
python3 \
103-
"${working_dir}/gecko-dev/mach" \
104-
--no-interactive \
105-
artifact \
106-
toolchain \
107-
--bootstrap \
108-
--from-build \
109-
sysroot-wasm32-wasi
110-
111129
cd "$working_dir"
112130

113-
if [[ -z "${WASI_SDK_PREFIX}" ]]; then
114-
echo "WASI_SDK_PREFIX not set, using default compiler and sysroot"
115-
else
116-
mv ~/.mozbuild/clang ~/.mozbuild/clang.orig
117-
mv ~/.mozbuild/sysroot-wasm32-wasi ~/.mozbuild/sysroot-wasm32-wasi.orig
118-
ln -s "${WASI_SDK_PREFIX}" ~/.mozbuild/clang
119-
ln -s "${WASI_SDK_PREFIX}/share/wasi-sysroot" ~/.mozbuild/sysroot-wasm32-wasi
120-
fi
131+
export CC=${WASI_SDK_PREFIX}/bin/clang
132+
export CXX=${WASI_SDK_PREFIX}/bin/clang++
133+
export AR=${WASI_SDK_PREFIX}/bin/llvm-ar
134+
export HOST_CC=~/.mozbuild/clang/bin/clang
135+
export HOST_CXX=~/.mozbuild/clang/bin/clang++
136+
export HOST_AR=~/.mozbuild/clang/bin/llvm-ar
121137

122138
# Build SpiderMonkey for WASI
123139
MOZCONFIG="${mozconfig}" \
124140
MOZ_FETCHES_DIR=~/.mozbuild \
125-
CC=~/.mozbuild/clang/bin/clang \
126-
CXX=~/.mozbuild/clang/bin/clang++ \
127-
AR=~/.mozbuild/clang/bin/llvm-ar \
128141
python3 "${working_dir}/gecko-dev/mach" \
129142
--no-interactive \
130-
build
143+
configure
144+
145+
# Always use our own WASI sysroot, not the one mozbuild might have downloaded.
146+
rm -rf ~/.mozbuild/sysroot-wasm32-wasi
147+
ln -s "${WASI_SDK_PREFIX}/share/wasi-sysroot" ~/.mozbuild/sysroot-wasm32-wasi
148+
149+
MOZCONFIG="${mozconfig}" \
150+
MOZ_FETCHES_DIR=~/.mozbuild \
151+
python3 "${working_dir}/gecko-dev/mach" \
152+
--no-interactive \
153+
configure
154+
155+
cd "$objdir"
156+
# Before actually building, we need to overwrite the .cargo/config file that mozbuild created,
157+
# because otherwise we can't build the Rust stdlib with -fPIC.
158+
# That file is created during the `pre-export` build step, so we run that now, then overwrite
159+
# the file, then build.
160+
make pre-export
161+
162+
if [[ -f .cargo/config ]]; then
163+
echo "" > .cargo/config
164+
fi
165+
166+
make -j$(nproc) -s
131167

132168
# Copy header, object, and static lib files
169+
cd ..
133170
rm -rf "$outdir"
134171
mkdir -p "$outdir/lib"
135172

@@ -140,4 +177,8 @@ while read -r file; do
140177
cp "$file" "../$outdir/lib"
141178
done < "$script_dir/object-files.list"
142179

143-
cp js/src/build/libjs_static.a "wasm32-wasi/${mode}/libjsrust.a" "../$outdir/lib"
180+
cp js/src/build/libjs_static.a "../$outdir/lib"
181+
182+
if [[ -f "wasm32-wasi/${mode}/libjsrust.a" ]]; then
183+
cp "wasm32-wasi/${mode}/libjsrust.a" "../$outdir/lib"
184+
fi

rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[toolchain]
2-
channel = "1.75"
2+
channel = "nightly"
33
targets = [ "wasm32-wasi" ]
44
profile = "minimal"

wasi-sdk-url

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/dicej/wasi-sdk/releases/download/filesystem-fix-v3/wasi-sdk-20.25g964191c91aac-$OS_NAME.tar.gz

0 commit comments

Comments
 (0)