Skip to content

refactor(cargo): Clean up cargo build configs #75498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: canary
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
63 changes: 13 additions & 50 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,22 @@ TURBO_PNPM_WORKSPACE_DIR = { value = "", relative = true }
[alias]
xtask = "run --package xtask --"

[build]
[target.'cfg(any(unix, not(unix)))']
# Cargo will merge rustflags from `target` sections together, but `target`
# sections are mutually exclusive with `build` sections. Define this in an
# always-matched `target` section, so that we don't have to duplicate flags
# everywhere.
# https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags
rustflags = [
"--cfg",
"tokio_unstable",
"--cfg=tokio_unstable",
"-Zshare-generics=y", # make the current crate share its generic instantiations
"-Zthreads=8", # parallel frontend https://blog.rust-lang.org/2023/11/09/parallel-rustc.html
"-Csymbol-mangling-version=v0",
]
rustdocflags = []
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already the default? Probably just not cleaned up from prior changes.


[target.x86_64-pc-windows-msvc]
linker = "rust-lld"
rustflags = [
"--cfg",
"tokio_unstable",
"-Zshare-generics=y",
"-C",
"target-feature=+crt-static"
]

[target.i686-pc-windows-msvc]
rustflags = [
"--cfg",
"tokio_unstable",
"-Zshare-generics=y",
"-C",
"target-feature=+crt-static"
]
Comment on lines -28 to -35
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dropped support for 32-bit Windows a few months back because nobody is using it.

rustflags = ["-Ctarget-feature=+crt-static"]

[target.aarch64-pc-windows-msvc]
linker = "rust-lld"
Expand All @@ -42,38 +30,13 @@ linker = "rust-lld"

[target.'cfg(all(target_os = "linux", target_env = "gnu"))']
rustflags = [
"--cfg",
"tokio_unstable",
"-Zshare-generics=y",
"-Zthreads=8",
"-Zunstable-options",
"-Csymbol-mangling-version=v0",
"-Clinker-flavor=gnu-lld-cc",
"-Clink-self-contained=+linker",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently I added this line when I enabled lld and I'm not sure why. Doesn't seem to be useful?

]

[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-musl-gcc"
# Config need to be mirrowed to .github/workflows/build_and_deploy.yml
rustflags = [
"--cfg",
"tokio_unstable",
"-Zshare-generics=y",
"-Csymbol-mangling-version=v0",
"-Ctarget-feature=-crt-static",
"-Clink-arg=-lgcc",
]

[target.x86_64-unknown-linux-musl]
# Config need to be mirrowed to .github/workflows/build_and_deploy.yml
rustflags = [
"--cfg",
"tokio_unstable",
"-Zshare-generics=y",
"-Zthreads=8",
"-Csymbol-mangling-version=v0",
"-Ctarget-feature=-crt-static",
]
[target.'cfg(all(target_os = "linux", target_env = "musl"))']
# Rustflags for musl targets need to be mirrored to `.github/workflows/build_and_deploy.yml`
# The napi cli sets a RUSTFLAGS environment variable that will otherwise override these options:
# https://github.com/napi-rs/napi-rs/blob/2d8e19d7c626405287b73a52dc/cli/src/api/build.ts#L467-L485
rustflags = ["-Ctarget-feature=-crt-static"]

[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
Comment on lines -78 to -79
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to 32-bit Windows, I don't think anyone should be using this. Turbopack's only going to be useful on tiny projects if limited to 2 GB of RAM, plus arm is a niche platform to begin with. It doesn't look like we had CI for this anyways.

12 changes: 12 additions & 0 deletions .cargo/release-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Overrides to `.cargo/config.toml` used by `pnpm build-native-release`/`pnpm
# build-native-no-plugin-release` and the `build_and_deploy` GitHub actions
# workflow.
#
# When using the napi CLI or the `pack-next` script, you can use
# `--cargo-flags=--config=.cargo/release-config.toml` to enable this.
#
# cargo's merging logic combines rustflags in this file with those in the
# relevant target sections of `.cargo/config.toml`

[profile.release]
strip = "symbols"
123 changes: 72 additions & 51 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,12 @@ jobs:
# --env-mode loose is a breaking change required with turbo 2.x since Strict mode is now the default
# TODO: we should add the relevant envs later to to switch to strict mode
build: |
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" "turbo@${TURBO_VERSION}" && corepack enable
turbo run build-native-release -vvv --env-mode loose --remote-cache-timeout 90 --summarize -- --target x86_64-apple-darwin
strip -x packages/next-swc/native/next-swc.*.node
set -ex
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" "turbo@${TURBO_VERSION}"
corepack enable
turbo run build-native-release -vvv --env-mode loose \
--remote-cache-timeout 90 --summarize -- \
--target x86_64-apple-darwin

- host:
- 'self-hosted'
Expand All @@ -159,9 +162,12 @@ jobs:
# --env-mode loose is a breaking change required with turbo 2.x since Strict mode is now the default
# TODO: we should add the relevant envs later to to switch to strict mode
build: |
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" "turbo@${TURBO_VERSION}" && corepack enable
turbo run build-native-release -vvv --env-mode loose --remote-cache-timeout 90 --summarize -- --target aarch64-apple-darwin
strip -x packages/next-swc/native/next-swc.*.node
set -ex
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" "turbo@${TURBO_VERSION}"
corepack enable
turbo run build-native-release -vvv --env-mode loose \
--remote-cache-timeout 90 --summarize -- \
--target aarch64-apple-darwin

- host:
- 'self-hosted'
Expand All @@ -171,9 +177,12 @@ jobs:
# --env-mode loose is a breaking change required with turbo 2.x since Strict mode is now the default
# TODO: we should add the relevant envs later to to switch to strict mode
build: |
set -ex
corepack enable
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" "turbo@${TURBO_VERSION}"
turbo run build-native-release -vvv --env-mode loose --remote-cache-timeout 90 --summarize -- --target x86_64-pc-windows-msvc
turbo run build-native-release -vvv --env-mode loose \
--remote-cache-timeout 90 --summarize -- \
--target x86_64-pc-windows-msvc
target: 'x86_64-pc-windows-msvc'

- host:
Expand All @@ -185,9 +194,12 @@ jobs:
# --env-mode loose is a breaking change required with turbo 2.x since Strict mode is now the default
# TODO: we should add the relevant envs later to to switch to strict mode
build: |
set -ex
corepack enable
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" "turbo@${TURBO_VERSION}"
turbo run build-native-no-plugin-release -vvv --env-mode loose --remote-cache-timeout 90 --summarize -- --target aarch64-pc-windows-msvc
turbo run build-native-no-plugin-release -vvv --env-mode loose \
--remote-cache-timeout 90 --summarize -- \
--target aarch64-pc-windows-msvc

- host:
- 'self-hosted'
Expand All @@ -204,16 +216,16 @@ jobs:
# - zig linker with portable glibc is avoided as it has known issues with static tls + node.js + multi threaded
# environment.
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:stable-2023-09-17-x64
build: >-
set -ex &&
apt update &&
apt install -y pkg-config xz-utils dav1d libdav1d-dev &&
rustup show &&
rustup target add x86_64-unknown-linux-gnu &&
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" &&
unset CC_x86_64_unknown_linux_gnu && unset CC &&
cd packages/next-swc && npm run build-native-release -- --target x86_64-unknown-linux-gnu &&
strip native/next-swc.*.node &&
build: |
set -ex
apt update
apt install -y pkg-config xz-utils dav1d libdav1d-dev
rustup show
rustup target add x86_64-unknown-linux-gnu
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}"
unset CC_x86_64_unknown_linux_gnu && unset CC
cd packages/next-swc && npm run build-native-release -- \
--target x86_64-unknown-linux-gnu
objdump -T native/next-swc.*.node | grep GLIBC_

- host:
Expand All @@ -224,16 +236,20 @@ jobs:

target: 'x86_64-unknown-linux-musl'
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:stable-2023-09-17-alpine
build: >-
set -ex &&
apk update &&
apk add --no-cache libc6-compat pkgconfig dav1d libdav1d dav1d-dev clang-static llvm-dev &&
rustup show &&
rustup target add x86_64-unknown-linux-musl &&
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" &&
export RUSTFLAGS='--cfg tokio_unstable -Zshare-generics=y -Zthreads=8 -Csymbol-mangling-version=v0 -Ctarget-feature=-crt-static' &&
cd packages/next-swc && npm run build-native-release -- --target x86_64-unknown-linux-musl &&
strip native/next-swc.*.node
build: |
set -ex
apk update
apk add --no-cache libc6-compat pkgconfig dav1d libdav1d dav1d-dev clang-static llvm-dev
rustup show
rustup target add x86_64-unknown-linux-musl
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}"
cd packages/next-swc
# Rustflags for musl targets need to be copied from `.cargo/config.toml`
# The napi cli sets RUSTFLAGS which will otherwise override these options:
# https://github.com/napi-rs/napi-rs/blob/2d8e19d7c6264/cli/src/api/build.ts#L467-L485
export RUSTFLAGS="--cfg=tokio_unstable -Zshare-generics=y -Zthreads=8 \
-Csymbol-mangling-version=v0 -Ctarget-feature=-crt-static"
npm run build-native-release -- --target x86_64-unknown-linux-musl

- host:
- 'self-hosted'
Expand All @@ -243,18 +259,19 @@ jobs:

target: 'aarch64-unknown-linux-gnu'
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:stable-2023-09-17-aarch64
build: >-
set -ex &&
apt update &&
apt install -y pkg-config xz-utils dav1d libdav1d-dev &&
export JEMALLOC_SYS_WITH_LG_PAGE=16 &&
rustup show &&
rustup target add aarch64-unknown-linux-gnu &&
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" &&
export CC_aarch64_unknown_linux_gnu=/usr/bin/clang &&
export CFLAGS_aarch64_unknown_linux_gnu=\"--target=aarch64-unknown-linux-gnu --sysroot=/usr/aarch64-unknown-linux-gnu\" &&
cd packages/next-swc && npm run build-native-release -- --target aarch64-unknown-linux-gnu &&
llvm-strip -x native/next-swc.*.node &&
build: |
set -ex
apt update
apt install -y pkg-config xz-utils dav1d libdav1d-dev
export JEMALLOC_SYS_WITH_LG_PAGE=16
rustup show
rustup target add aarch64-unknown-linux-gnu
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}"
export CC_aarch64_unknown_linux_gnu=/usr/bin/clang
export CFLAGS_aarch64_unknown_linux_gnu="--target=aarch64-unknown-linux-gnu \
--sysroot=/usr/aarch64-unknown-linux-gnu"
cd packages/next-swc
npm run build-native-release -- --target aarch64-unknown-linux-gnu
objdump -T native/next-swc.*.node | grep GLIBC_

- host:
Expand All @@ -265,17 +282,21 @@ jobs:

target: 'aarch64-unknown-linux-musl'
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:stable-2023-09-17-alpine
build: >-
set -ex &&
apk update &&
apk add --no-cache libc6-compat pkgconfig dav1d libdav1d dav1d-dev clang-static llvm-dev &&
export JEMALLOC_SYS_WITH_LG_PAGE=16 &&
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" &&
rustup show &&
rustup target add aarch64-unknown-linux-musl &&
export RUSTFLAGS='--cfg tokio_unstable -Zshare-generics=y -Zthreads=8 -Zunstable-options -Csymbol-mangling-version=v0 -Clinker-flavor=gnu-lld-cc -Clink-self-contained=+linker' &&
cd packages/next-swc && npm run build-native-release -- --target aarch64-unknown-linux-musl &&
llvm-strip -x native/next-swc.*.node
build: |
set -ex
apk update
apk add --no-cache libc6-compat pkgconfig dav1d libdav1d dav1d-dev clang-static llvm-dev
export JEMALLOC_SYS_WITH_LG_PAGE=16
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}"
rustup show
rustup target add aarch64-unknown-linux-musl
cd packages/next-swc
# Rustflags for musl targets need to be copied from `.cargo/config.toml`
# The napi cli sets RUSTFLAGS which will otherwise override these options:
# https://github.com/napi-rs/napi-rs/blob/2d8e19d7c6264/cli/src/api/build.ts#L467-L485
export RUSTFLAGS="--cfg=tokio_unstable -Zshare-generics=y -Zthreads=8 \
-Csymbol-mangling-version=v0 -Ctarget-feature=-crt-static"
npm run build-native-release -- --target aarch64-unknown-linux-musl

name: stable - ${{ matrix.settings.target }} - node@16
runs-on: ${{ matrix.settings.host }}
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"scripts": {
"clean": "node ../../scripts/rm.mjs native",
"build-native": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --features plugin,image-extended --js false native",
"build-native-release": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --release --features plugin,image-extended,tracing/release_max_level_info --js false native",
"build-native-release": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --release --cargo-flags=--config=.cargo/release-config.toml --features plugin,image-extended,tracing/release_max_level_info --js false native",
"build-native-no-plugin": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --features image-webp --js false native",
"build-native-no-plugin-release": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --release --features image-webp,tracing/release_max_level_info --js false native",
"build-native-no-plugin-release": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --release --cargo-flags=--config=.cargo/release-config.toml --features image-webp,tracing/release_max_level_info --js false native",
"build-native-wasi": "npx --package=@napi-rs/cli@3.0.0-alpha.45 napi build --platform --target wasm32-wasip1-threads -p next-swc-napi --cwd ../../ --output-dir packages/next-swc/native --no-default-features",
"build-wasm": "wasm-pack build ../../crates/wasm --scope=next",
"cache-build-native": "[ -d native ] && echo $(ls native)",
Expand Down
Loading