|
| 1 | +load("@prelude//rust:rust_toolchain.bzl", "PanicRuntime", "RustToolchainInfo") |
| 2 | + |
| 3 | +_DEFAULT_TRIPLE = select({ |
| 4 | + "prelude//os:linux": select({ |
| 5 | + "prelude//cpu:arm64": "aarch64-unknown-linux-gnu", |
| 6 | + "prelude//cpu:riscv64": "riscv64gc-unknown-linux-gnu", |
| 7 | + "prelude//cpu:x86_64": "x86_64-unknown-linux-gnu", |
| 8 | + }), |
| 9 | + "prelude//os:macos": select({ |
| 10 | + "prelude//cpu:arm64": "aarch64-apple-darwin", |
| 11 | + "prelude//cpu:x86_64": "x86_64-apple-darwin", |
| 12 | + }), |
| 13 | + "prelude//os:windows": select({ |
| 14 | + "prelude//cpu:arm64": select({ |
| 15 | + # Rustup's default ABI for the host on Windows is MSVC, not GNU. |
| 16 | + "DEFAULT": "aarch64-pc-windows-msvc", |
| 17 | + "prelude//abi:gnu": "aarch64-pc-windows-gnu", |
| 18 | + "prelude//abi:msvc": "aarch64-pc-windows-msvc", |
| 19 | + }), |
| 20 | + "prelude//cpu:x86_64": select({ |
| 21 | + "DEFAULT": "x86_64-pc-windows-msvc", |
| 22 | + "prelude//abi:gnu": "x86_64-pc-windows-gnu", |
| 23 | + "prelude//abi:msvc": "x86_64-pc-windows-msvc", |
| 24 | + }), |
| 25 | + }), |
| 26 | +}) |
| 27 | + |
| 28 | + |
| 29 | +def _codex_rust_toolchain_impl(ctx): |
| 30 | + # Buck doesn't have a built-in notion of "Cargo profiles", but it's useful |
| 31 | + # to provide a simple local knob that roughly matches `cargo build` vs |
| 32 | + # `cargo build --release`. |
| 33 | + # |
| 34 | + # Default is "dev" to match local development expectations. |
| 35 | + rust_profile = read_config("codex", "rust_profile", "dev") |
| 36 | + extra_rustc_flags = [] |
| 37 | + if rust_profile == "release": |
| 38 | + # Roughly mirrors Cargo's release defaults (not a perfect match). |
| 39 | + extra_rustc_flags = [ |
| 40 | + "-C", |
| 41 | + "opt-level=3", |
| 42 | + "-C", |
| 43 | + "debuginfo=0", |
| 44 | + ] |
| 45 | + |
| 46 | + return [ |
| 47 | + DefaultInfo(), |
| 48 | + RustToolchainInfo( |
| 49 | + allow_lints = ctx.attrs.allow_lints, |
| 50 | + clippy_driver = RunInfo(args = [ctx.attrs.clippy_driver]), |
| 51 | + clippy_toml = ctx.attrs.clippy_toml[DefaultInfo].default_outputs[0] if ctx.attrs.clippy_toml else None, |
| 52 | + compiler = RunInfo(args = [ctx.attrs.rustc]), |
| 53 | + default_edition = ctx.attrs.default_edition, |
| 54 | + deny_lints = ctx.attrs.deny_lints, |
| 55 | + doctests = ctx.attrs.doctests, |
| 56 | + nightly_features = ctx.attrs.nightly_features, |
| 57 | + panic_runtime = PanicRuntime("unwind"), |
| 58 | + report_unused_deps = ctx.attrs.report_unused_deps, |
| 59 | + rustc_binary_flags = ctx.attrs.rustc_binary_flags, |
| 60 | + rustc_flags = ctx.attrs.rustc_flags + extra_rustc_flags, |
| 61 | + rustc_target_triple = ctx.attrs.rustc_target_triple, |
| 62 | + rustc_test_flags = ctx.attrs.rustc_test_flags, |
| 63 | + rustdoc = RunInfo(args = [ctx.attrs.rustdoc]), |
| 64 | + rustdoc_flags = ctx.attrs.rustdoc_flags, |
| 65 | + warn_lints = ctx.attrs.warn_lints, |
| 66 | + # Enable the prelude's "metadata-only rlib" behavior consistently |
| 67 | + # across the crate graph. This avoids rustc "found possibly newer |
| 68 | + # version of crate ..." (E0460) mismatches between binaries and |
| 69 | + # libraries in large Rust graphs. |
| 70 | + advanced_unstable_linking = ctx.attrs.advanced_unstable_linking, |
| 71 | + ), |
| 72 | + ] |
| 73 | + |
| 74 | + |
| 75 | +codex_rust_toolchain = rule( |
| 76 | + impl = _codex_rust_toolchain_impl, |
| 77 | + attrs = { |
| 78 | + "advanced_unstable_linking": attrs.bool(default = True), |
| 79 | + "allow_lints": attrs.list(attrs.string(), default = []), |
| 80 | + # Prefer explicit tool paths so the Buck execution directory doesn't |
| 81 | + # affect rustup toolchain resolution. |
| 82 | + "clippy_driver": attrs.string(default = "clippy-driver"), |
| 83 | + "clippy_toml": attrs.option(attrs.dep(providers = [DefaultInfo]), default = None), |
| 84 | + "default_edition": attrs.option(attrs.string(), default = None), |
| 85 | + "deny_lints": attrs.list(attrs.string(), default = []), |
| 86 | + "doctests": attrs.bool(default = False), |
| 87 | + "nightly_features": attrs.bool(default = False), |
| 88 | + "report_unused_deps": attrs.bool(default = False), |
| 89 | + "rustc": attrs.string(default = "rustc"), |
| 90 | + "rustc_binary_flags": attrs.list(attrs.arg(), default = []), |
| 91 | + "rustc_flags": attrs.list(attrs.arg(), default = []), |
| 92 | + "rustc_target_triple": attrs.string(default = _DEFAULT_TRIPLE), |
| 93 | + "rustc_test_flags": attrs.list(attrs.arg(), default = []), |
| 94 | + "rustdoc": attrs.string(default = "rustdoc"), |
| 95 | + "rustdoc_flags": attrs.list(attrs.arg(), default = []), |
| 96 | + "warn_lints": attrs.list(attrs.string(), default = []), |
| 97 | + }, |
| 98 | + is_toolchain_rule = True, |
| 99 | +) |
0 commit comments