This commit is contained in:
iceBear67
2026-07-24 15:20:50 +00:00
parent 3b0a360ac8
commit 17b805ea94
6 changed files with 47 additions and 32 deletions
+35 -8
View File
@@ -6,16 +6,43 @@ set -euo pipefail
cd "$(dirname "$0")"
# The engine's Go dependencies (sing) contain three source files that reference
# golang.org/x/sys/unix, which has no equivalent for GOARCH=wasm. wasmbuild/
# supplies wasm-safe stubs and gen-overlay.sh maps them in via `go build
# -overlay`. Only this wasm build uses the overlay; native builds/tests do not.
overlay="$(mktemp)"
trap 'rm -f "$overlay"' EXIT
bash wasmbuild/gen-overlay.sh > "$overlay"
# The engine's Go dependency sing contains three source files that reference
# golang.org/x/sys/unix, which has no GOARCH=wasm equivalent:
# common/buf/buffer_unix.go, common/bufio/vectorised_unix.go,
# common/bufio/copy_direct_posix.go
# We can't patch them where they live: the module cache is read-only and, since
# go1.25, `go build -overlay` refuses to replace files beneath GOMODCACHE. So we
# materialize a writable copy of the sing module outside the cache, drop the
# wasm-safe stubs from wasmbuild/_stubs over those three files, and build the wasm
# against an alternate module file that `replace`s sing with the patched copy.
# Native builds and tests use the unmodified go.mod and the real sing — none of
# this touches them. All the files below are gitignored build artifacts.
go mod download github.com/sagernet/sing
singdir="$(go list -m -f '{{.Dir}}' github.com/sagernet/sing)"
singver="$(go list -m -f '{{.Version}}' github.com/sagernet/sing)"
work="$PWD/wasmbuild/.singtree"
if [ "$(cat "$work/.version" 2>/dev/null || true)" != "$singver" ]; then
echo "staging wasm-patched sing $singver"
rm -rf "$work"
mkdir -p "$work"
cp -a "$singdir/." "$work/"
chmod -R u+w "$work"
cp wasmbuild/_stubs/buf_buffer_unix.go "$work/common/buf/buffer_unix.go"
cp wasmbuild/_stubs/bufio_vectorised_unix.go "$work/common/bufio/vectorised_unix.go"
cp wasmbuild/_stubs/bufio_copy_direct_posix.go "$work/common/bufio/copy_direct_posix.go"
printf '%s\n' "$singver" > "$work/.version"
fi
# Alternate module file pointing sing at the patched copy. Regenerated each build
# so it tracks go.mod/go.sum; removed on exit.
trap 'rm -f go.wasm.mod go.wasm.sum' EXIT
cp go.mod go.wasm.mod
cp go.sum go.wasm.sum
printf '\nreplace github.com/sagernet/sing => %s\n' "$work" >> go.wasm.mod
echo "building web/singvis.wasm (GOOS=js GOARCH=wasm)…"
GOOS=js GOARCH=wasm go build -overlay "$overlay" -trimpath -ldflags="-s -w" \
GOOS=js GOARCH=wasm go build -modfile=go.wasm.mod -trimpath -ldflags="-s -w" \
-o web/singvis.wasm ./cmd/wasm
# Ship the Go runtime's JS support shim next to the wasm. Newer Go keeps it under