-
Notifications
You must be signed in to change notification settings - Fork 4.6k
build: generate bun_core::build_options from Config #30749
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * Generates `${codegenDir}/build_options.rs` — Rust constants derived from | ||
| * `Config`, `include!()`'d by `bun_core::build_options`. | ||
| * | ||
| * Replaces the `option_env!("BUN_*")` handshake (a dozen env vars exported | ||
| * by `rust.ts`, read back in `bun_core`). `Config` is now the single source | ||
| * of truth: literal `pub const`s land on disk, so a bare `cargo check` / | ||
| * rust-analyzer sees the real version/sha/paths instead of placeholder | ||
| * defaults, and the env-var name list isn't maintained in two places. | ||
| * | ||
| * `bun_core/build.rs` emits `rerun-if-changed` on the file; `writeIfChanged` | ||
| * keeps the mtime stable so a reconfigure with the same sha doesn't | ||
| * recompile `bun_core` and its dependents. | ||
| * | ||
| * Target-dependent constants (`ENABLE_TINYCC`, `ENABLE_ASAN`, `ENABLE_LOGS`) | ||
| * stay as `cfg!()` expressions inside the generated file rather than literals | ||
| * so a `cargo check --target <other-triple>` against the same generated file | ||
| * still evaluates them per-target. | ||
| * | ||
| * Written at configure time alongside `depVersionsHeader.ts` / | ||
| * `cargo-config.ts` — it's a constant manifest, not a build edge. | ||
| */ | ||
|
|
||
| import { mkdirSync } from "node:fs"; | ||
| import { resolve } from "node:path"; | ||
| import type { Config } from "./config.ts"; | ||
| import { writeIfChanged } from "./fs.ts"; | ||
|
|
||
| /** Rust string literal for `s`. JSON escaping is a strict subset of Rust's. */ | ||
| const rstr = (s: string): string => JSON.stringify(s); | ||
| /** Rust byte-string literal for `s` (the `&[u8]` constants). */ | ||
| const rbstr = (s: string): string => `b${JSON.stringify(s)}`; | ||
|
Check failure on line 32 in scripts/build/buildOptionsRs.ts
|
||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| export function generateBuildOptionsRs(cfg: Config): string { | ||
| const outPath = resolve(cfg.codegenDir, "build_options.rs"); | ||
| const [major, minor, patch] = cfg.version.split("."); | ||
|
|
||
| const lines: string[] = [ | ||
| "// Generated by scripts/build/buildOptionsRs.ts from `Config` at configure", | ||
| "// time. Do not edit. Regenerate with `bun bd`.", | ||
| "", | ||
| `pub const SHA: &str = ${rstr(cfg.revision)};`, | ||
| `pub const REPORTED_NODEJS_VERSION: &str = ${rstr(cfg.nodejsVersion)};`, | ||
| `pub const RELEASE_SAFE: bool = ${cfg.assertions};`, | ||
| `pub const BASELINE: bool = ${cfg.baseline};`, | ||
| `pub const IS_CANARY: bool = ${cfg.canary};`, | ||
| `pub const CANARY_REVISION: &str = ${rstr(cfg.canaryRevision)};`, | ||
| `pub const ENABLE_FUZZILLI: bool = ${cfg.fuzzilli};`, | ||
| `pub const FALLBACK_HTML_VERSION: &str = "0000000000000000";`, | ||
| "pub const VERSION: crate::Version = crate::Version {", | ||
| ` major: ${Number(major)},`, | ||
| ` minor: ${Number(minor)},`, | ||
| ` patch: ${Number(patch)},`, | ||
| "};", | ||
| `pub const BASE_PATH: &[u8] = ${rbstr(cfg.cwd)};`, | ||
| `pub const CODEGEN_PATH: &[u8] = ${rbstr(cfg.codegenDir)};`, | ||
| "", | ||
| "// Target/profile-derived — kept as `cfg!()` so cross-target", | ||
| "// `cargo check` evaluates per-triple. Values agree with `Config`:", | ||
| "// rust.ts sets `--cfg=bun_asan` ⇔ `cfg.asan`, and `cfg.tinycc`'s", | ||
| "// default (config.ts) is the negation of this predicate.", | ||
| "pub const ENABLE_LOGS: bool = cfg!(debug_assertions);", | ||
| "pub const ENABLE_ASAN: bool = cfg!(bun_asan);", | ||
| "pub const ENABLE_TINYCC: bool = !cfg!(any(", | ||
| ` all(windows, target_arch = "aarch64"),`, | ||
| ` target_os = "android",`, | ||
| ` target_os = "freebsd",`, | ||
| "));", | ||
| "", | ||
| ]; | ||
|
|
||
| mkdirSync(cfg.codegenDir, { recursive: true }); | ||
| writeIfChanged(outPath, lines.join("\n")); | ||
| return outPath; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| //! Export `BUN_CODEGEN_DIR` and fingerprint `build_options.rs` for | ||
| //! `include!(concat!(env!("BUN_CODEGEN_DIR"), "/build_options.rs"))`. | ||
| //! | ||
| //! `build_options.rs` is written at configure time by | ||
| //! `scripts/build/buildOptionsRs.ts` from the resolved `Config` (sha, | ||
| //! version, baseline, …). This script does NOT run the generator — it just | ||
| //! resolves the path and tells cargo to track the file so a sha/version | ||
| //! change recompiles `bun_core`. Mirrors `src/{jsc,runtime}/build.rs`. | ||
|
|
||
| use std::env; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| fn main() { | ||
| // src/bun_core → repo root is two up. | ||
| let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); | ||
| let repo = manifest | ||
| .parent() | ||
| .and_then(Path::parent) | ||
| .expect("repo root from CARGO_MANIFEST_DIR") | ||
| .to_path_buf(); | ||
|
|
||
| let codegen_dir = env::var("BUN_CODEGEN_DIR") | ||
| .map(PathBuf::from) | ||
| .unwrap_or_else(|_| repo.join("build/debug/codegen")); | ||
|
|
||
| let build_options = codegen_dir.join("build_options.rs"); | ||
| if !build_options.exists() { | ||
| panic!( | ||
| "build_options.rs not found at {} — run `bun bd --configure-only` first", | ||
| build_options.display() | ||
| ); | ||
| } | ||
|
|
||
| println!("cargo:rustc-env=BUN_CODEGEN_DIR={}", codegen_dir.display()); | ||
| println!("cargo:rerun-if-changed={}", build_options.display()); | ||
| println!("cargo:rerun-if-env-changed=BUN_CODEGEN_DIR"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.