Skip to content

Commit

Permalink
move custom allocator flag and add rustls-tls comment (#60128)
Browse files Browse the repository at this point in the history
### What?

enable the custom allocator flag to enable mialloc.
allow to configure custom allocator on napi level.

### Why?

It's faster and we had it enabled before.
It was disable before as `next-core` is used with no default features in
workspace

Native Build: https://github.com/vercel/next.js/actions/runs/7388725004


Closes PACK-2185

---------

Co-authored-by: OJ Kwon <1210596+kwonoj@users.noreply.github.com>
  • Loading branch information
sokra and kwonoj authored Jan 11, 2024
1 parent 01c34b3 commit f45a15b
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ jobs:
rustup target add aarch64-unknown-linux-gnu &&
npm i -g "@napi-rs/cli@${NAPI_CLI_VERSION}" &&
export CC_aarch64_unknown_linux_gnu=/usr/aarch64-unknown-linux-gnu/bin/aarch64-unknown-linux-gnu-gcc &&
cd packages/next-swc && npm run build-native-release -- --target aarch64-unknown-linux-gnu --features plugin,rustls-tls,tracing/release_max_level_info &&
cd packages/next-swc && npm run build-native-release -- --target aarch64-unknown-linux-gnu --features plugin,tracing/release_max_level_info &&
llvm-strip -x native/next-swc.*.node &&
objdump -T native/next-swc.*.node | grep GLIBC_
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/setup-nextjs-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ jobs:
export TASKS_FS=$(printf 'patch.\\"%s\\".%s.git=\\"%s?rev=%s\\"' "$TURBOPACK_REMOTE" "turbo-tasks-fs" "$TURBOPACK_REMOTE" "$GITHUB_SHA")
echo "Trying to build next-swc with turbopack $GITHUB_SHA"
hyperfine --min-runs 1 --show-output 'pnpm run --filter=@next/swc build-native --features plugin,rustls-tls --release --cargo-flags="--config $BINDING --config $TASKS --config $TASKS_FS"'
hyperfine --min-runs 1 --show-output 'pnpm run --filter=@next/swc build-native --features plugin --release --cargo-flags="--config $BINDING --config $TASKS --config $TASKS_FS"'
echo "built=pass" >> $GITHUB_OUTPUT
echo "Successfully built next-swc with turbopack $GITHUB_SHA"
- name: Build next-swc
if: steps.build-next-swc-turbopack-patch.outputs.built != 'pass'
run: |
echo "Looks like we could not apply latest turbopack to next-swc. Trying to build next-swc with published turbopack. This might happen when there is a breaking changes in turbopack, and next.js is not yet updated."
hyperfine --min-runs 1 --show-output 'pnpm run --filter=@next/swc build-native --features plugin,rustls-tls --release'
hyperfine --min-runs 1 --show-output 'pnpm run --filter=@next/swc build-native --features plugin --release'
echo "Successfully built next-swc with published turbopack"
- name: Build next.js
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ opt-level = 3

[workspace.dependencies]
# Workspace crates
next-api = { path = "packages/next-swc/crates/next-api", default-features = false }
next-build = { path = "packages/next-swc/crates/next-build", default-features = false }
next-core = { path = "packages/next-swc/crates/next-core", default-features = false }
next-api = { path = "packages/next-swc/crates/next-api" }
next-build = { path = "packages/next-swc/crates/next-build" }
next-core = { path = "packages/next-swc/crates/next-core" }
next-custom-transforms = { path = "packages/next-swc/crates/next-custom-transforms" }

# SWC crates
Expand Down
18 changes: 18 additions & 0 deletions packages/next-swc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ Build wasm bindings to integrate with next.js
pnpm build-wasm
```

### napi bindings feature matrix

Due to platform differences napi bindings selectively enables supported features.
See below tables for the currently enabled features.

| arch\platform | Linux(gnu) | Linux(musl) | Darwin | Win32 |
| ------------- | ---------- | ----------- | --------- | --------- |
| ia32 | | | | a,b,d,e |
| x64 | a,b,d,e,f | a,b,d,e,f | a,b,d,e,f | a,b,d,e,f |
| aarch64 | a,d,e,f | a,d,e,f | a,b,d,e,f | a,b,c,e |

- a: `turbo_tasks_malloc`,
- b: `turbo_tasks_malloc_custom_allocator`,
- c: `native-tls`,
- d: `rustls-tls`,
- e: `image-extended` (webp)
- f: `plugin`

### Package hierarchies

`@next/swc` consist of multiple rust packages to enable features. See below for the high level hierarchies.
Expand Down
29 changes: 21 additions & 8 deletions packages/next-swc/crates/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ publish = false
crate-type = ["cdylib", "rlib"]

[features]
default = ["rustls-tls"]
# Instead of enabling all the plugin-related features by default, make it explicitly specified
# when build (i.e napi --build --features plugin), same for the wasm as well.
# this is due to some of transitive dependencies have features cannot be enabled at the same time
Expand All @@ -20,11 +19,7 @@ plugin = [
"next-custom-transforms/plugin",
"next-core/plugin",
]
sentry_native_tls = ["sentry", "sentry/native-tls", "native-tls"]
sentry_rustls = ["sentry", "sentry/rustls", "rustls-tls"]

native-tls = ["next-core/native-tls"]
rustls-tls = ["next-core/rustls-tls"]
image-webp = ["next-core/image-webp"]
image-avif = ["next-core/image-avif"]
# Enable all the available image codec support.
Expand All @@ -39,6 +34,27 @@ __internal_dhat-heap = ["dhat"]
# effectively does nothing.
__internal_dhat-ad-hoc = ["dhat"]

# Making custom_allocator as default feature will break some targets (i.e aarch64-linux), controlling it with
# build-time cfg instead.
#
# [NOTE] this is a workaround to enable downstream features for the own pkgs, since
# cargo does not support per-target features enablement.
[target.'cfg(all(target_os = "linux", target_arch = "aarch64"))'.dependencies]
turbopack-binding = { workspace = true, features = ["__turbo_tasks_malloc"] }

[target.'cfg(all(target_os = "linux", not(target_arch = "aarch64")))'.dependencies]
turbopack-binding = { workspace = true, features = ["__turbo_tasks_malloc", "__turbo_tasks_malloc_custom_allocator"] }

[target.'cfg(not(target_os = "linux"))'.dependencies]
turbopack-binding = { workspace = true, features = ["__turbo_tasks_malloc", "__turbo_tasks_malloc_custom_allocator"] }

# Enable specific tls features per-target.
[target.'cfg(all(target_os = "windows", target_arch = "aarch64"))'.dependencies]
next-core = { workspace = true, features = ["native-tls"] }

[target.'cfg(not(all(target_os = "windows", target_arch = "aarch64")))'.dependencies]
next-core = { workspace = true, features = ["rustls-tls"] }

[lints]
workspace = true

Expand Down Expand Up @@ -81,9 +97,6 @@ turbopack-binding = { workspace = true, features = [
url = {workspace = true}
urlencoding = {workspace = true}

[target.'cfg(not(all(target_os = "linux", target_env = "musl", target_arch = "aarch64")))'.dependencies]
turbopack-binding = { workspace = true, features = ["__turbo_tasks_malloc"] }

# There are few build targets we can't use native-tls which default features rely on,
# allow to specify alternative (rustls) instead via features.
# Note to opt in rustls default-features should be disabled
Expand Down
6 changes: 0 additions & 6 deletions packages/next-swc/crates/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,3 @@ fn register() {
include!(concat!(env!("OUT_DIR"), "/register.rs"));
});
}

#[cfg(all(feature = "native-tls", feature = "rustls-tls"))]
compile_error!("You can't enable both `native-tls` and `rustls-tls`");

#[cfg(all(not(feature = "native-tls"), not(feature = "rustls-tls")))]
compile_error!("You have to enable one of the TLS backends: `native-tls` or `rustls-tls`");
7 changes: 0 additions & 7 deletions packages/next-swc/crates/next-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ autobenches = false
[lib]
bench = false

[features]
default = ["custom_allocator"]
custom_allocator = [
"turbopack-binding/__turbo_tasks_malloc",
"turbopack-binding/__turbo_tasks_malloc_custom_allocator",
]

[lints]
workspace = true

Expand Down
12 changes: 0 additions & 12 deletions packages/next-swc/crates/next-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,11 @@ autobenches = false
bench = false

[features]
# By default, we enable native-tls for reqwest via downstream transitive features.
# This is for the convenience of running daily dev workflows, i.e running
# `cargo xxx` without explicitly specifying features, not that we want to
# promote this as default backend. Actual configuration is done when building next-swc,
# and also turbopack standalone when we have it.
default = ["custom_allocator", "native-tls"]
tokio_console = [
"dep:console-subscriber",
"tokio/tracing",
"turbo-tasks/tokio_tracing",
]
native-tls = ["next-core/native-tls"]
rustls-tls = ["next-core/rustls-tls"]
custom_allocator = [
"turbopack-binding/__turbo_tasks_malloc",
"turbopack-binding/__turbo_tasks_malloc_custom_allocator",
]
serializable = []
profile = []

Expand Down
6 changes: 6 additions & 0 deletions packages/next-swc/crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ pub fn register() {
turbopack::ecmascript_plugin::register();
include!(concat!(env!("OUT_DIR"), "/register.rs"));
}

#[cfg(all(feature = "native-tls", feature = "rustls-tls"))]
compile_error!("You can't enable both `native-tls` and `rustls-tls`");

#[cfg(all(not(feature = "native-tls"), not(feature = "rustls-tls")))]
compile_error!("You have to enable one of the TLS backends: `native-tls` or `rustls-tls`");
10 changes: 5 additions & 5 deletions packages/next-swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"private": true,
"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,rustls-tls,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,rustls-tls,image-extended,tracing/release_max_level_info --js false 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-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-woa": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --cargo-flags=--no-default-features --features native-tls,image-webp --js false native",
"build-native-no-plugin-woa-release": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --release --cargo-flags=--no-default-features --features native-tls,image-webp,tracing/release_max_level_info --js false native",
"build-native-no-plugin-woa": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --cargo-flags=--no-default-features --features image-webp --js false native",
"build-native-no-plugin-woa-release": "napi build --platform -p next-swc-napi --cargo-cwd ../../ --cargo-name next_swc_napi --release --cargo-flags=--no-default-features --features image-webp,tracing/release_max_level_info --js false native",
"build-wasm": "wasm-pack build crates/wasm --scope=next",
"cache-build-native": "echo $(ls native)",
"rust-check-fmt": "cd ../..; cargo fmt -- --check",
"rust-check-clippy": "cargo clippy --workspace --all-targets -- -D warnings -A deprecated",
"rust-check-napi-rustls": "cargo check -p next-swc-napi --features=rustls-tls",
"rust-check-napi-rustls": "cargo check -p next-swc-napi",
"test-cargo-unit": "cargo nextest run --workspace --release --no-fail-fast"
},
"napi": {
Expand Down

0 comments on commit f45a15b

Please sign in to comment.