Commit 43c5ab8d authored by Matt Mastracci's avatar Matt Mastracci

windows on linux prototype

parent e0a47d7f
...@@ -162,7 +162,8 @@ jobs: ...@@ -162,7 +162,8 @@ jobs:
vendor: apple vendor: apple
sys: darwin sys: darwin
windows: windows:
host: "['self-hosted', 'windows', 'x64']" host: "['self-hosted', 'linux', 'x64', 'metal']"
docker: next-swc-builder-win:latest
vendor: pc vendor: pc
sys: windows sys: windows
abi: msvc abi: msvc
...@@ -198,9 +199,6 @@ jobs: ...@@ -198,9 +199,6 @@ jobs:
name: stable - ${{ matrix.target }} - node@20 name: stable - ${{ matrix.target }} - node@20
runs-on: ${{ fromJSON(matrix.host) }} runs-on: ${{ fromJSON(matrix.host) }}
timeout-minutes: 45 timeout-minutes: 45
env:
# Disable all build caches for production/staging/force-preview deploys
NEXT_SKIP_BUILD_CACHE: ${{ contains(fromJSON('["production","staging","force-preview"]'), needs.deploy-target.outputs.value) && '1' || '' }}
steps: steps:
# Enable long paths on Windows to avoid MAX_PATH (260 char) errors # Enable long paths on Windows to avoid MAX_PATH (260 char) errors
# with deeply nested node_modules/.pnpm paths # with deeply nested node_modules/.pnpm paths
...@@ -240,7 +238,7 @@ jobs: ...@@ -240,7 +238,7 @@ jobs:
run: node scripts/normalize-version-bump.js run: node scripts/normalize-version-bump.js
- name: Cache on ${{ github.ref_name }} - name: Cache on ${{ github.ref_name }}
if: ${{ !matrix.docker && !env.NEXT_SKIP_BUILD_CACHE }} if: ${{ !matrix.docker }}
uses: ijjk/rust-cache@a34594c450817c9860143c79797a8770c4e587f5 # turbo-cache-v1.0.9 uses: ijjk/rust-cache@a34594c450817c9860143c79797a8770c4e587f5 # turbo-cache-v1.0.9
with: with:
save-if: 'true' save-if: 'true'
...@@ -253,11 +251,11 @@ jobs: ...@@ -253,11 +251,11 @@ jobs:
# Try to restore the native binary BEFORE loading the Docker image. # Try to restore the native binary BEFORE loading the Docker image.
# If the binary cache hits, we skip the expensive Docker image restore entirely. # If the binary cache hits, we skip the expensive Docker image restore entirely.
- name: Rust fingerprint - name: Rust fingerprint
if: ${{ matrix.docker && !env.NEXT_SKIP_BUILD_CACHE }} if: ${{ matrix.docker }}
run: pnpm dlx turbo@${TURBO_VERSION} run rust-fingerprint ${TURBO_ARGS} run: pnpm dlx turbo@${TURBO_VERSION} run rust-fingerprint ${TURBO_ARGS}
- name: Restore native binary cache - name: Restore native binary cache
if: ${{ matrix.docker && !env.NEXT_SKIP_BUILD_CACHE }} if: ${{ matrix.docker }}
id: native-cache id: native-cache
run: node scripts/native-cache.js --restore --target ${{ matrix.target }} && echo "HIT=yes" >> $GITHUB_OUTPUT || echo "HIT=no" >> $GITHUB_OUTPUT run: node scripts/native-cache.js --restore --target ${{ matrix.target }} && echo "HIT=yes" >> $GITHUB_OUTPUT || echo "HIT=no" >> $GITHUB_OUTPUT
...@@ -266,16 +264,17 @@ jobs: ...@@ -266,16 +264,17 @@ jobs:
if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' }} if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' }}
uses: ./.github/actions/docker-buildx uses: ./.github/actions/docker-buildx
with: with:
target: ${{ matrix.sys == 'windows' && 'windows' || 'linux' }}
image-tag: ${{ matrix.docker }} image-tag: ${{ matrix.docker }}
cache-ref: next-swc-builder cache-ref: ${{ matrix.sys == 'windows' && 'next-swc-builder-win' || 'next-swc-builder' }}
- name: Build in docker - name: Build in docker
if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' }} if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' }}
run: | run: |
docker run --rm \ docker run --rm \
-e CI -e RUST_BACKTRACE -e CARGO_TERM_COLOR \ -e CI -e RUST_BACKTRACE -e CARGO_TERM_COLOR \
-e CARGO_INCREMENTAL=0 -e CARGO_REGISTRIES_CRATES_IO_PROTOCOL \ -e CARGO_INCREMENTAL=0 -e CARGO_REGISTRIES_CRATES_IO_PROTOCOL -e TURBO_API \
${{ env.NEXT_SKIP_BUILD_CACHE && ' ' || '-e TURBO_API -e TURBO_TEAM -e TURBO_TOKEN -e TURBO_VERSION -e TURBO_CACHE=remote:rw' }} \ -e TURBO_TEAM -e TURBO_TOKEN -e TURBO_VERSION -e TURBO_CACHE="remote:rw" \
-e TARGET="${{ matrix.target }}" \ -e TARGET="${{ matrix.target }}" \
-e ABI="${{ matrix.abi }}" \ -e ABI="${{ matrix.abi }}" \
-e ARCH="${{ matrix.arch }}" \ -e ARCH="${{ matrix.arch }}" \
...@@ -289,7 +288,7 @@ jobs: ...@@ -289,7 +288,7 @@ jobs:
-xeo pipefail scripts/docker-native-build.sh -xeo pipefail scripts/docker-native-build.sh
- name: Save native binary cache - name: Save native binary cache
if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' && !env.NEXT_SKIP_BUILD_CACHE }} if: ${{ matrix.docker && steps.native-cache.outputs.HIT != 'yes' }}
run: node scripts/native-cache.js --save --target ${{ matrix.target }} run: node scripts/native-cache.js --save --target ${{ matrix.target }}
- name: 'Build' - name: 'Build'
...@@ -302,8 +301,7 @@ jobs: ...@@ -302,8 +301,7 @@ jobs:
rustc --version --verbose || true rustc --version --verbose || true
node -v node -v
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}"
TURBO_CACHE_FLAG="${NEXT_SKIP_BUILD_CACHE:+--force}" pnpm dlx turbo@${TURBO_VERSION} run ${{ matrix.build_task }} ${TURBO_ARGS} -- --target ${{ matrix.target }}
pnpm dlx turbo@${TURBO_VERSION} run ${{ matrix.build_task }} ${TURBO_ARGS} ${TURBO_CACHE_FLAG} -- --target ${{ matrix.target }}
if [ "${{ matrix.os }}" != "windows" ]; then if [ "${{ matrix.os }}" != "windows" ]; then
strip -x packages/next-swc/native/next-swc.*.node strip -x packages/next-swc/native/next-swc.*.node
fi fi
......
...@@ -75,18 +75,38 @@ jobs: ...@@ -75,18 +75,38 @@ jobs:
secrets: inherit secrets: inherit
build-native-windows: build-native-windows:
name: build-native-windows name: build-native-windows (${{ matrix.target }})
uses: ./.github/workflows/build_reusable.yml runs-on: ['self-hosted', 'linux', 'x64', 'metal']
needs: ['changes'] needs: ['changes']
if: ${{ needs.changes.outputs.docs-only == 'false' }} if: ${{ needs.changes.outputs.docs-only == 'false' }}
with: strategy:
skipInstallBuild: 'yes' fail-fast: false
stepName: 'build-native-windows' matrix:
runs_on_labels: '["windows","self-hosted","x64"]' include:
buildNativeTarget: 'x86_64-pc-windows-msvc' - target: x86_64-pc-windows-msvc
uploadNativeArtifact: 'next-swc-windows' artifact_name: next-swc-windows-x86_64-pc-windows-msvc
napi_glob: next-swc.win32-x64-msvc.node
secrets: inherit - target: aarch64-pc-windows-msvc
artifact_name: next-swc-windows-aarch64-pc-windows-msvc
napi_glob: next-swc.win32-arm64-msvc.node
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 25
- name: Build Docker image
uses: ./.github/actions/docker-buildx
with:
target: windows
image-tag: next-swc-builder-win:latest
cache-ref: next-swc-builder-win
- name: Cross-compile Windows native binary
run: node scripts/docker-native-build.js --quick --skip-image ${{ matrix.target }}
- name: Upload Windows native artifact
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: ${{ matrix.artifact_name }}
path: packages/next-swc/native/${{ matrix.napi_glob }}
retention-days: 1
build-next: build-next:
name: build-next name: build-next
......
...@@ -272,6 +272,15 @@ dependencies = [ ...@@ -272,6 +272,15 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "ar_archive_writer"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7eb93bbb63b9c227414f6eb3a0adfddca591a8ce1e9b60661bb08969b87e340b"
dependencies = [
"object 0.37.3",
]
[[package]] [[package]]
name = "arbitrary" name = "arbitrary"
version = "1.4.1" version = "1.4.1"
...@@ -1076,10 +1085,11 @@ checksum = "b28d2802395e3bccd95cc4ae984bff7444b6c1f5981da46a41360c42a2c7e2d9" ...@@ -1076,10 +1085,11 @@ checksum = "b28d2802395e3bccd95cc4ae984bff7444b6c1f5981da46a41360c42a2c7e2d9"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.2.31" version = "1.2.60"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3a42d84bb6b69d3a8b3eaacf0d88f179e1929695e1ad012b6cf64d9caaa5fd2" checksum = "43c5703da9466b66a946814e1adf53ea2c90f10063b86290cc9eb67ce3478a20"
dependencies = [ dependencies = [
"find-msvc-tools",
"jobserver", "jobserver",
"libc", "libc",
"shlex", "shlex",
...@@ -2588,6 +2598,12 @@ dependencies = [ ...@@ -2588,6 +2598,12 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]] [[package]]
name = "fixedbitset" name = "fixedbitset"
version = "0.5.7" version = "0.5.7"
...@@ -5943,10 +5959,10 @@ dependencies = [ ...@@ -5943,10 +5959,10 @@ dependencies = [
[[package]] [[package]]
name = "psm" name = "psm"
version = "0.1.21" version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/mmastrac/stacker?branch=mmastrac%2Faarch64-windows-inline-asm#5efbb6d1fc907696f3b09cbd1a0bc48d3ebe42a6"
checksum = "5787f7cda34e3033a72192c018bc5883100330f362ef279a8cbccfce8bb4e874"
dependencies = [ dependencies = [
"ar_archive_writer",
"cc", "cc",
] ]
......
...@@ -370,3 +370,6 @@ virtue = { git = "https://github.com/bgw/virtue.git", branch = "bgw/fix-generic- ...@@ -370,3 +370,6 @@ virtue = { git = "https://github.com/bgw/virtue.git", branch = "bgw/fix-generic-
mdxjs = { git = "https://github.com/vercel-labs/mdxjs-rs-turbopack.git", branch = "turbopack" } mdxjs = { git = "https://github.com/vercel-labs/mdxjs-rs-turbopack.git", branch = "turbopack" }
# Doesn't compile on recent nightly versions because of https://github.com/Michael-F-Bryan/include_dir/pull/117 # Doesn't compile on recent nightly versions because of https://github.com/Michael-F-Bryan/include_dir/pull/117
include_dir = { git = "https://github.com/vercel-labs/include_dir", branch = "turbopack" } include_dir = { git = "https://github.com/vercel-labs/include_dir", branch = "turbopack" }
# Use inline asm for Windows cross-compilation instead of GAS .s files
# https://github.com/rust-lang/stacker/pull/144
psm = { git = "https://github.com/mmastrac/stacker", branch = "mmastrac/aarch64-windows-inline-asm" }
...@@ -13,7 +13,8 @@ const fs = require('fs') ...@@ -13,7 +13,8 @@ const fs = require('fs')
const os = require('os') const os = require('os')
const REPO_ROOT = path.resolve(__dirname, '..') const REPO_ROOT = path.resolve(__dirname, '..')
const DOCKER_IMAGE = 'next-swc-builder:latest' const DOCKER_IMAGE_LINUX = 'next-swc-builder:latest'
const DOCKER_IMAGE_WIN = 'next-swc-builder-win:latest'
const TARGETS = [ const TARGETS = [
{ {
...@@ -40,6 +41,18 @@ const TARGETS = [ ...@@ -40,6 +41,18 @@ const TARGETS = [
abi: 'musl', abi: 'musl',
napiPlatform: 'linux-arm64-musl', napiPlatform: 'linux-arm64-musl',
}, },
{
target: 'x86_64-pc-windows-msvc',
arch: 'x86_64',
abi: 'msvc',
napiPlatform: 'win32-x64-msvc',
},
{
target: 'aarch64-pc-windows-msvc',
arch: 'aarch64',
abi: 'msvc',
napiPlatform: 'win32-arm64-msvc',
},
] ]
// Map uname -m to our arch names // Map uname -m to our arch names
...@@ -53,6 +66,7 @@ const { values: flags, positionals } = parseArgs({ ...@@ -53,6 +66,7 @@ const { values: flags, positionals } = parseArgs({
options: { options: {
quick: { type: 'boolean', default: false }, quick: { type: 'boolean', default: false },
'host-target': { type: 'boolean', default: false }, 'host-target': { type: 'boolean', default: false },
'skip-image': { type: 'boolean', default: false },
test: { type: 'boolean', default: false }, test: { type: 'boolean', default: false },
help: { type: 'boolean', short: 'h', default: false }, help: { type: 'boolean', short: 'h', default: false },
}, },
...@@ -61,13 +75,14 @@ const { values: flags, positionals } = parseArgs({ ...@@ -61,13 +75,14 @@ const { values: flags, positionals } = parseArgs({
if (flags.help) { if (flags.help) {
console.log( console.log(
'Usage: node scripts/docker-native-build.js [--quick] [--host-target] [--test] [filter]' 'Usage: node scripts/docker-native-build.js [--quick] [--host-target] [--skip-image] [--test] [filter]'
) )
process.exit(0) process.exit(0)
} }
const quick = flags.quick const quick = flags.quick
const hostTarget = flags['host-target'] const hostTarget = flags['host-target']
const skipImage = flags['skip-image']
const test = flags.test const test = flags.test
const filter = positionals[0] || '' const filter = positionals[0] || ''
...@@ -82,13 +97,20 @@ if (targets.length === 0) { ...@@ -82,13 +97,20 @@ if (targets.length === 0) {
process.exit(1) process.exit(1)
} }
// --- Determine which Docker image stage to use ---
const isWindows = targets.some((t) => t.target.includes('windows'))
const dockerStage = isWindows ? 'windows' : 'linux'
const DOCKER_IMAGE = isWindows ? DOCKER_IMAGE_WIN : DOCKER_IMAGE_LINUX
// --- Build Docker image locally (CI uses .github/actions/docker-buildx instead) --- // --- Build Docker image locally (CI uses .github/actions/docker-buildx instead) ---
function ensureDockerImage() { function ensureDockerImage() {
console.log(`Building Docker image: ${DOCKER_IMAGE}`) console.log(`Building Docker image: ${DOCKER_IMAGE} (target: ${dockerStage})`)
execFileSync( execFileSync(
'docker', 'docker',
[ [
'build', 'build',
'--target',
dockerStage,
'-t', '-t',
DOCKER_IMAGE, DOCKER_IMAGE,
'-f', '-f',
...@@ -99,7 +121,9 @@ function ensureDockerImage() { ...@@ -99,7 +121,9 @@ function ensureDockerImage() {
) )
} }
ensureDockerImage() if (!skipImage) {
ensureDockerImage()
}
// --- Build targets --- // --- Build targets ---
const buildTask = quick const buildTask = quick
......
...@@ -66,6 +66,15 @@ case "$TARGET" in ...@@ -66,6 +66,15 @@ case "$TARGET" in
CROSS_FLAGS="$CROSS_FLAGS -Clink-arg=--target=x86_64-linux-musl -Clink-arg=--sysroot=/opt/x86_64-linux-musl-cross/x86_64-linux-musl -Clink-arg=--gcc-toolchain=/opt/x86_64-linux-musl-cross" ;; CROSS_FLAGS="$CROSS_FLAGS -Clink-arg=--target=x86_64-linux-musl -Clink-arg=--sysroot=/opt/x86_64-linux-musl-cross/x86_64-linux-musl -Clink-arg=--gcc-toolchain=/opt/x86_64-linux-musl-cross" ;;
aarch64-unknown-linux-musl) aarch64-unknown-linux-musl)
CROSS_FLAGS="$CROSS_FLAGS -Clink-arg=--target=aarch64-linux-musl -Clink-arg=--sysroot=/opt/aarch64-linux-musl-cross/aarch64-linux-musl -Clink-arg=--gcc-toolchain=/opt/aarch64-linux-musl-cross" ;; CROSS_FLAGS="$CROSS_FLAGS -Clink-arg=--target=aarch64-linux-musl -Clink-arg=--sysroot=/opt/aarch64-linux-musl-cross/aarch64-linux-musl -Clink-arg=--gcc-toolchain=/opt/aarch64-linux-musl-cross" ;;
x86_64-pc-windows-msvc|aarch64-pc-windows-msvc)
# Windows cross-compilation is handled by cargo-xwin (invoked by napi-rs).
# cargo-xwin downloads the MSVC SDK, sets CC/linker/include paths.
# We only need to preserve base rustflags from .cargo/config.toml via
# CARGO_ENCODED_RUSTFLAGS (see below).
# Note: lld-link must be Rust's bundled LLD (v22+), not system LLD (v10),
# to support the EH continuation table that loadcfg.obj references.
CROSS_FLAGS=""
;;
*) echo "Unknown target: $TARGET"; exit 1 ;; *) echo "Unknown target: $TARGET"; exit 1 ;;
esac esac
...@@ -74,41 +83,64 @@ CROSS_CONFIG="target.${TARGET}.rustflags=[$(echo "$CROSS_FLAGS" | sed 's/\([^ ]* ...@@ -74,41 +83,64 @@ CROSS_CONFIG="target.${TARGET}.rustflags=[$(echo "$CROSS_FLAGS" | sed 's/\([^ ]*
# Resolve merged RUSTFLAGS (config.toml base + cross overrides) # Resolve merged RUSTFLAGS (config.toml base + cross overrides)
RUSTFLAGS=$(cargo rustflags --target "$TARGET" --config "$CROSS_CONFIG") RUSTFLAGS=$(cargo rustflags --target "$TARGET" --config "$CROSS_CONFIG")
export RUSTFLAGS
case "$TARGET" in
*-windows-*)
# For Windows, cargo-xwin sets its own CARGO_ENCODED_RUSTFLAGS which
# overrides config.toml. Pre-seed it with the base flags (tokio_unstable,
# share-generics, crt-static, etc.) so cargo-xwin appends to them.
CARGO_ENCODED_RUSTFLAGS=$(cargo rustflags --target "$TARGET" --encoded)
export CARGO_ENCODED_RUSTFLAGS
;;
*)
export RUSTFLAGS
;;
esac
# --- rust-lld symlink --- # --- rust-lld symlink ---
# rustc's gcc-ld/ dir has ld.lld but no 'ld' shim. gnu-lld-cc passes # rustc's gcc-ld/ dir has ld.lld but no 'ld' shim. gnu-lld-cc passes
# -B<gcc-ld-dir> to clang, which looks for 'ld' there. # -B<gcc-ld-dir> to clang, which looks for 'ld' there.
SYSROOT=$(rustc --print sysroot) # Windows uses lld-link directly, so this shim is not needed.
GCC_LD="$SYSROOT/lib/rustlib/${TARGET}/bin/gcc-ld" case "$TARGET" in
if [ -d "$GCC_LD" ] && [ ! -e "$GCC_LD/ld" ]; then *-windows-*) ;;
ln -sf ../rust-lld "$GCC_LD/ld" *)
fi SYSROOT=$(rustc --print sysroot)
GCC_LD="$SYSROOT/lib/rustlib/${TARGET}/bin/gcc-ld"
if [ -d "$GCC_LD" ] && [ ! -e "$GCC_LD/ld" ]; then
ln -sf ../rust-lld "$GCC_LD/ld"
fi
;;
esac
# --- CC/CXX for build scripts (jemalloc, ring, etc.) --- # --- CC/CXX for build scripts (jemalloc, ring, etc.) ---
# For Windows, cargo-xwin handles CC, CXX, AR, INCLUDE, and LIB automatically.
TARGET_US=$(echo "$TARGET" | tr '-' '_') TARGET_US=$(echo "$TARGET" | tr '-' '_')
unset "CC_${TARGET_US}" "CXX_${TARGET_US}" "CFLAGS_${TARGET_US}" unset "CC_${TARGET_US}" "CXX_${TARGET_US}" "CFLAGS_${TARGET_US}"
export "CC_${TARGET_US}=clang"
export "CXX_${TARGET_US}=clang++"
case "$TARGET" in case "$TARGET" in
x86_64-unknown-linux-gnu) x86_64-unknown-linux-gnu)
export "CC_${TARGET_US}=clang" "CXX_${TARGET_US}=clang++"
if [ "$HOST_ARCH" = "x86_64" ]; then if [ "$HOST_ARCH" = "x86_64" ]; then
export "CFLAGS_${TARGET_US}=--target=x86_64-linux-gnu" export "CFLAGS_${TARGET_US}=--target=x86_64-linux-gnu"
else else
export "CFLAGS_${TARGET_US}=--target=x86_64-linux-gnu --sysroot=/usr/x86_64-linux-gnu" export "CFLAGS_${TARGET_US}=--target=x86_64-linux-gnu --sysroot=/usr/x86_64-linux-gnu"
fi ;; fi ;;
aarch64-unknown-linux-gnu) aarch64-unknown-linux-gnu)
export "CC_${TARGET_US}=clang" "CXX_${TARGET_US}=clang++"
if [ "$HOST_ARCH" = "aarch64" ]; then if [ "$HOST_ARCH" = "aarch64" ]; then
export "CFLAGS_${TARGET_US}=--target=aarch64-linux-gnu" export "CFLAGS_${TARGET_US}=--target=aarch64-linux-gnu"
else else
export "CFLAGS_${TARGET_US}=--target=aarch64-linux-gnu --sysroot=/usr/aarch64-linux-gnu" export "CFLAGS_${TARGET_US}=--target=aarch64-linux-gnu --sysroot=/usr/aarch64-linux-gnu"
fi ;; fi ;;
x86_64-unknown-linux-musl) x86_64-unknown-linux-musl)
export "CC_${TARGET_US}=clang" "CXX_${TARGET_US}=clang++"
export "CFLAGS_${TARGET_US}=--target=x86_64-linux-musl --sysroot=/opt/x86_64-linux-musl-cross/x86_64-linux-musl --gcc-toolchain=/opt/x86_64-linux-musl-cross" ;; export "CFLAGS_${TARGET_US}=--target=x86_64-linux-musl --sysroot=/opt/x86_64-linux-musl-cross/x86_64-linux-musl --gcc-toolchain=/opt/x86_64-linux-musl-cross" ;;
aarch64-unknown-linux-musl) aarch64-unknown-linux-musl)
export "CC_${TARGET_US}=clang" "CXX_${TARGET_US}=clang++"
export "CFLAGS_${TARGET_US}=--target=aarch64-linux-musl --sysroot=/opt/aarch64-linux-musl-cross/aarch64-linux-musl --gcc-toolchain=/opt/aarch64-linux-musl-cross" ;; export "CFLAGS_${TARGET_US}=--target=aarch64-linux-musl --sysroot=/opt/aarch64-linux-musl-cross/aarch64-linux-musl --gcc-toolchain=/opt/aarch64-linux-musl-cross" ;;
x86_64-pc-windows-msvc|aarch64-pc-windows-msvc)
# cargo-xwin handles CC, CXX, AR, INCLUDE, and LIB automatically.
;;
esac esac
# aarch64 needs larger page size for jemalloc # aarch64 needs larger page size for jemalloc
...@@ -134,7 +166,6 @@ echo "-------------------------" ...@@ -134,7 +166,6 @@ echo "-------------------------"
rustup target add "$TARGET" rustup target add "$TARGET"
cd packages/next-swc cd packages/next-swc
npm run "$BUILD_TASK" -- --target "$TARGET" npm run "$BUILD_TASK" -- --target "$TARGET"
llvm-strip -x native/next-swc.*.node
# Show sccache stats if available # Show sccache stats if available
if command -v sccache &>/dev/null; then if command -v sccache &>/dev/null; then
...@@ -142,18 +173,29 @@ if command -v sccache &>/dev/null; then ...@@ -142,18 +173,29 @@ if command -v sccache &>/dev/null; then
sccache --show-stats || true sccache --show-stats || true
fi fi
# Post-build verification # Post-build: strip and verify
echo "--- Dynamic libraries ---" case "$TARGET" in
readelf -d native/next-swc.*.node | grep NEEDED *-windows-*)
# PE/COFF: strip debug info and show DLL imports
case "$ABI" in llvm-strip --strip-debug native/next-swc.*.node
gnu) echo "--- PE imports ---"
echo "--- GLIBC symbols by version ---" llvm-readobj --coff-imports native/next-swc.*.node || true
objdump -T native/next-swc.*.node \ ;;
| grep 'GLIBC_' \ *)
| sed 's/.*\(GLIBC_[^ ]*\) \+/\1 /' \ llvm-strip -x native/next-swc.*.node
| sort -t. -k2,2n -k3,3n \ echo "--- Dynamic libraries ---"
| awk '{vers[$1] = vers[$1] ? vers[$1] ", " $2 : $2} END {for (v in vers) print v ": " vers[v]}' \ readelf -d native/next-swc.*.node | grep NEEDED
| sort -t. -k2,2n -k3,3n
case "$ABI" in
gnu)
echo "--- GLIBC symbols by version ---"
objdump -T native/next-swc.*.node \
| grep 'GLIBC_' \
| sed 's/.*\(GLIBC_[^ ]*\) \+/\1 /' \
| sort -t. -k2,2n -k3,3n \
| awk '{vers[$1] = vers[$1] ? vers[$1] ", " $2 : $2} END {for (v in vers) print v ": " vers[v]}' \
| sort -t. -k2,2n -k3,3n
;;
esac
;; ;;
esac esac
# Multi-stage Dockerfile for building Next.js native binaries (next-swc). # Multi-stage Dockerfile for building Next.js native binaries (next-swc).
# #
# Produces a single image that can cross-compile ALL 4 Linux targets # Two final images share a common base:
# (x86_64/aarch64 × gnu/musl) from either an x86_64 or aarch64 host. # - next-swc-builder:latest — Linux targets (x86_64/aarch64 × gnu/musl)
# - next-swc-builder-win:latest — Windows targets (x86_64/aarch64 × msvc)
# #
# Build: # Build:
# docker build -t next-swc-builder:latest -f scripts/native-builder.Dockerfile . # docker build --target linux -t next-swc-builder:latest ...
# docker build --target windows -t next-swc-builder-win:latest ...
# #
# The image includes: # The base image includes:
# - Ubuntu 20.04 (glibc 2.31 — broad compatibility baseline) # - Ubuntu 22.04 (build host only — output binaries target older glibc/CRT)
# - Clang/LLD for all compilation and linking via --target # - Clang/LLD for compilation and linking
# - GNU cross-sysroots via crossbuild-essential (Ubuntu multiarch) # - Node.js 20 (build tool for npm/napi-cli)
# - musl sysroots from musl.cc (headers + libs only; clang/lld do the work)
# - Node.js 20 (glibc-linked, used as build tool for all targets)
# - Rust nightly toolchain (pinned to match rust-toolchain.toml) # - Rust nightly toolchain (pinned to match rust-toolchain.toml)
# - @napi-rs/cli for building native Node.js addons # - @napi-rs/cli and cargo-rustflags
FROM ubuntu:20.04 AS builder # ============================================================
# Stage: base — shared toolchain for all targets
# ============================================================
FROM ubuntu:22.04 AS base
# Avoid interactive prompts during apt-get
ENV DEBIAN_FRONTEND=noninteractive ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
curl ca-certificates \
clang lld llvm pkg-config wget git xz-utils libssl-dev \
&& curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y --no-install-recommends nodejs \
&& rm -rf /var/lib/apt/lists/*
# Install Rust — pinned nightly from rust-toolchain.toml
COPY rust-toolchain.toml /tmp/rust-toolchain.toml
RUN TOOLCHAIN=$(grep 'channel' /tmp/rust-toolchain.toml | sed 's/.*"\(.*\)".*/\1/') && \
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | \
sh -s -- -y --default-toolchain "$TOOLCHAIN" --profile minimal && \
rm /tmp/rust-toolchain.toml
ENV PATH="/root/.cargo/bin:${PATH}"
# Install cargo-binstall, then use it for Rust tools.
ARG CARGO_BINSTALL_VERSION=1.18.1
RUN ARCH=$(uname -m) && \
curl -fsSL "https://github.com/cargo-bins/cargo-binstall/releases/download/v${CARGO_BINSTALL_VERSION}/cargo-binstall-${ARCH}-unknown-linux-musl.tgz" \
| tar xz -C /root/.cargo/bin && \
npm i -g @napi-rs/cli@2.18.4 && \
cargo binstall --no-confirm --targets "${ARCH}-unknown-linux-musl" cargo-rustflags@0.4.0 && \
cargo binstall --no-confirm --git https://github.com/vercel/sccache sccache && \
node --version && rustc --version && napi -h > /dev/null && cargo rustflags --help > /dev/null && sccache --version
WORKDIR /build
# ============================================================
# Stage: linux — GNU and musl cross-compilation sysroots
# ============================================================
FROM base AS linux
# If the host provides an apt mirror URL (e.g. Hetzner), use it for the # If the host provides an apt mirror URL (e.g. Hetzner), use it for the
# native architecture. The suite names come from the container's own OS. # native architecture. The suite names come from the container's own OS.
ARG APT_MIRROR= ARG APT_MIRROR=
# Enable multiarch for cross-compilation sysroots. # Enable multiarch for cross-compilation sysroots.
# Write sources.list from scratch with explicit [arch=...] tags.
# On arm64 hosts: native packages from ports, foreign amd64 from archive.
# On amd64 hosts: native packages from archive, foreign arm64 from ports.
RUN HOST_ARCH=$(dpkg --print-architecture) && \ RUN HOST_ARCH=$(dpkg --print-architecture) && \
if [ "$HOST_ARCH" = "arm64" ]; then \ if [ "$HOST_ARCH" = "arm64" ]; then \
NATIVE_MIRROR="${APT_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}"; FOREIGN_ARCH=amd64; \ NATIVE_MIRROR="${APT_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}"; FOREIGN_ARCH=amd64; \
...@@ -38,31 +70,18 @@ RUN HOST_ARCH=$(dpkg --print-architecture) && \ ...@@ -38,31 +70,18 @@ RUN HOST_ARCH=$(dpkg --print-architecture) && \
fi && \ fi && \
dpkg --add-architecture "$FOREIGN_ARCH" && \ dpkg --add-architecture "$FOREIGN_ARCH" && \
printf '%s\n' \ printf '%s\n' \
"deb [arch=${HOST_ARCH}] ${NATIVE_MIRROR} focal main universe" \ "deb [arch=${HOST_ARCH}] ${NATIVE_MIRROR} jammy main universe" \
"deb [arch=${HOST_ARCH}] ${NATIVE_MIRROR} focal-updates main universe" \ "deb [arch=${HOST_ARCH}] ${NATIVE_MIRROR} jammy-updates main universe" \
"deb [arch=${HOST_ARCH}] ${NATIVE_MIRROR} focal-security main universe" \ "deb [arch=${HOST_ARCH}] ${NATIVE_MIRROR} jammy-security main universe" \
"deb [arch=${FOREIGN_ARCH}] ${FOREIGN_MIRROR} focal main universe" \ "deb [arch=${FOREIGN_ARCH}] ${FOREIGN_MIRROR} jammy main universe" \
"deb [arch=${FOREIGN_ARCH}] ${FOREIGN_MIRROR} focal-updates main universe" \ "deb [arch=${FOREIGN_ARCH}] ${FOREIGN_MIRROR} jammy-updates main universe" \
"deb [arch=${FOREIGN_ARCH}] ${FOREIGN_MIRROR} focal-security main universe" \ "deb [arch=${FOREIGN_ARCH}] ${FOREIGN_MIRROR} jammy-security main universe" \
> /etc/apt/sources.list > /etc/apt/sources.list && \
apt-get update && apt-get install -y --no-install-recommends \
# Core build tools + GNU cross-compilation sysroots + Node.js 20 via nodesource. crossbuild-essential-amd64 crossbuild-essential-arm64 \
# crossbuild-essential installs headers + libs in the multiarch layout
# that clang finds via --target. Both archs installed so the image
# works on either host architecture.
RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certificates && \
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
apt-get install -y --no-install-recommends \
nodejs \
clang lld llvm pkg-config wget git xz-utils libssl-dev \
crossbuild-essential-amd64 crossbuild-essential-arm64 \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# Download musl cross-toolchains from musl.cc for their sysroots # Download musl cross-toolchains from musl.cc for their sysroots.
# (headers, crt files, libc, libgcc). Clang + rust-lld handle compilation
# and linking; we only need the target libraries.
# Also copy GCC's crt files and libgcc into the sysroot lib dir — clang 10
# doesn't search the --gcc-toolchain path for these files.
# https://musl.cc/ # https://musl.cc/
RUN cd /opt && \ RUN cd /opt && \
for TRIPLE in aarch64-linux-musl x86_64-linux-musl; do \ for TRIPLE in aarch64-linux-musl x86_64-linux-musl; do \
...@@ -72,31 +91,38 @@ RUN cd /opt && \ ...@@ -72,31 +91,38 @@ RUN cd /opt && \
/opt/${TRIPLE}-cross/${TRIPLE}/lib/; \ /opt/${TRIPLE}-cross/${TRIPLE}/lib/; \
done done
# Install Rust — pinned nightly from rust-toolchain.toml
# The COPY of rust-toolchain.toml ensures the image rebuilds when the toolchain changes.
COPY rust-toolchain.toml /tmp/rust-toolchain.toml
RUN TOOLCHAIN=$(grep 'channel' /tmp/rust-toolchain.toml | sed 's/.*"\(.*\)".*/\1/') && \
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | \
sh -s -- -y --default-toolchain "$TOOLCHAIN" --profile minimal && \
rm /tmp/rust-toolchain.toml
ENV PATH="/root/.cargo/bin:${PATH}"
# Add all 4 Linux rustup targets
RUN rustup target add \ RUN rustup target add \
x86_64-unknown-linux-gnu \ x86_64-unknown-linux-gnu \
aarch64-unknown-linux-gnu \ aarch64-unknown-linux-gnu \
x86_64-unknown-linux-musl \ x86_64-unknown-linux-musl \
aarch64-unknown-linux-musl aarch64-unknown-linux-musl
# Install cargo-binstall, then use it for Rust tools. # ============================================================
ARG CARGO_BINSTALL_VERSION=1.18.1 # Stage: windows — MSVC cross-compilation via cargo-xwin
RUN ARCH=$(uname -m) && \ # ============================================================
curl -fsSL "https://github.com/cargo-bins/cargo-binstall/releases/download/v${CARGO_BINSTALL_VERSION}/cargo-binstall-${ARCH}-unknown-linux-musl.tgz" \ FROM base AS windows
| tar xz -C /root/.cargo/bin && \
npm i -g @napi-rs/cli@2.18.4 && \
cargo binstall --no-confirm --targets "${ARCH}-unknown-linux-musl" cargo-rustflags@0.4.0 && \
cargo binstall --no-confirm --git https://github.com/vercel/sccache sccache && \
node --version && rustc --version && napi -h > /dev/null && cargo rustflags --help > /dev/null && sccache --version
WORKDIR /build RUN cargo binstall --no-confirm cargo-xwin@0.21.5
RUN rustup target add \
x86_64-pc-windows-msvc \
aarch64-pc-windows-msvc
# Use Rust's bundled LLD (v22+) for lld-link — it supports /guard:ehcont
# which the xwin CRT's loadcfg.obj requires. Ubuntu's system LLD is too old.
# rust-lld auto-detects flavor from argv[0] "lld-link".
# clang-cl is clang in MSVC-compatible mode (same binary, different argv[0]).
RUN SYSROOT=$(rustc --print sysroot) && \
HOST=$(rustc -vV | grep host | cut -d' ' -f2) && \
ln -sf "$SYSROOT/lib/rustlib/$HOST/bin/rust-lld" /usr/local/bin/lld-link && \
ln -sf llvm-ar /usr/bin/llvm-lib && \
ln -sf clang /usr/bin/clang-cl
# Pre-cache MSVC SDK for cargo-xwin so it doesn't re-download on every build.
RUN mkdir -p /tmp/_xwin_seed/src && \
echo "fn main(){}" > /tmp/_xwin_seed/src/main.rs && \
printf '[package]\nname="d"\nversion="0.0.0"\nedition="2021"\n' > /tmp/_xwin_seed/Cargo.toml && \
cd /tmp/_xwin_seed && \
cargo xwin check --target x86_64-pc-windows-msvc && \
cargo xwin check --target aarch64-pc-windows-msvc && \
rm -rf /tmp/_xwin_seed
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment