feat: support local development using Buck2

This commit is contained in:
Michael Bolin
2025-12-23 22:40:55 -08:00
parent 38de0a1de4
commit 53edb6ebd3
19 changed files with 2432 additions and 5 deletions
+99
View File
@@ -0,0 +1,99 @@
load("@prelude//rust:rust_toolchain.bzl", "PanicRuntime", "RustToolchainInfo")
_DEFAULT_TRIPLE = select({
"prelude//os:linux": select({
"prelude//cpu:arm64": "aarch64-unknown-linux-gnu",
"prelude//cpu:riscv64": "riscv64gc-unknown-linux-gnu",
"prelude//cpu:x86_64": "x86_64-unknown-linux-gnu",
}),
"prelude//os:macos": select({
"prelude//cpu:arm64": "aarch64-apple-darwin",
"prelude//cpu:x86_64": "x86_64-apple-darwin",
}),
"prelude//os:windows": select({
"prelude//cpu:arm64": select({
# Rustup's default ABI for the host on Windows is MSVC, not GNU.
"DEFAULT": "aarch64-pc-windows-msvc",
"prelude//abi:gnu": "aarch64-pc-windows-gnu",
"prelude//abi:msvc": "aarch64-pc-windows-msvc",
}),
"prelude//cpu:x86_64": select({
"DEFAULT": "x86_64-pc-windows-msvc",
"prelude//abi:gnu": "x86_64-pc-windows-gnu",
"prelude//abi:msvc": "x86_64-pc-windows-msvc",
}),
}),
})
def _codex_rust_toolchain_impl(ctx):
# Buck doesn't have a built-in notion of "Cargo profiles", but it's useful
# to provide a simple local knob that roughly matches `cargo build` vs
# `cargo build --release`.
#
# Default is "dev" to match local development expectations.
rust_profile = read_config("codex", "rust_profile", "dev")
extra_rustc_flags = []
if rust_profile == "release":
# Roughly mirrors Cargo's release defaults (not a perfect match).
extra_rustc_flags = [
"-C",
"opt-level=3",
"-C",
"debuginfo=0",
]
return [
DefaultInfo(),
RustToolchainInfo(
allow_lints = ctx.attrs.allow_lints,
clippy_driver = RunInfo(args = [ctx.attrs.clippy_driver]),
clippy_toml = ctx.attrs.clippy_toml[DefaultInfo].default_outputs[0] if ctx.attrs.clippy_toml else None,
compiler = RunInfo(args = [ctx.attrs.rustc]),
default_edition = ctx.attrs.default_edition,
deny_lints = ctx.attrs.deny_lints,
doctests = ctx.attrs.doctests,
nightly_features = ctx.attrs.nightly_features,
panic_runtime = PanicRuntime("unwind"),
report_unused_deps = ctx.attrs.report_unused_deps,
rustc_binary_flags = ctx.attrs.rustc_binary_flags,
rustc_flags = ctx.attrs.rustc_flags + extra_rustc_flags,
rustc_target_triple = ctx.attrs.rustc_target_triple,
rustc_test_flags = ctx.attrs.rustc_test_flags,
rustdoc = RunInfo(args = [ctx.attrs.rustdoc]),
rustdoc_flags = ctx.attrs.rustdoc_flags,
warn_lints = ctx.attrs.warn_lints,
# Enable the prelude's "metadata-only rlib" behavior consistently
# across the crate graph. This avoids rustc "found possibly newer
# version of crate ..." (E0460) mismatches between binaries and
# libraries in large Rust graphs.
advanced_unstable_linking = ctx.attrs.advanced_unstable_linking,
),
]
codex_rust_toolchain = rule(
impl = _codex_rust_toolchain_impl,
attrs = {
"advanced_unstable_linking": attrs.bool(default = True),
"allow_lints": attrs.list(attrs.string(), default = []),
# Prefer explicit tool paths so the Buck execution directory doesn't
# affect rustup toolchain resolution.
"clippy_driver": attrs.string(default = "clippy-driver"),
"clippy_toml": attrs.option(attrs.dep(providers = [DefaultInfo]), default = None),
"default_edition": attrs.option(attrs.string(), default = None),
"deny_lints": attrs.list(attrs.string(), default = []),
"doctests": attrs.bool(default = False),
"nightly_features": attrs.bool(default = False),
"report_unused_deps": attrs.bool(default = False),
"rustc": attrs.string(default = "rustc"),
"rustc_binary_flags": attrs.list(attrs.arg(), default = []),
"rustc_flags": attrs.list(attrs.arg(), default = []),
"rustc_target_triple": attrs.string(default = _DEFAULT_TRIPLE),
"rustc_test_flags": attrs.list(attrs.arg(), default = []),
"rustdoc": attrs.string(default = "rustdoc"),
"rustdoc_flags": attrs.list(attrs.arg(), default = []),
"warn_lints": attrs.list(attrs.string(), default = []),
},
is_toolchain_rule = True,
)
+111
View File
@@ -0,0 +1,111 @@
load("@prelude//rust:cargo_buildscript.bzl", _prelude_buildscript_run = "buildscript_run")
load("@prelude//rust:cargo_package.bzl", "cargo")
def codex_noop_alias(**_kwargs):
# Reindeer normally emits aliases like `alias(name = "rand", actual = ":rand-0.8.5")`
# to provide stable unversioned target names. In a non-trivial workspace it's
# common to have multiple versions of the same crate in one graph, which
# leads to duplicate alias target names and buckification failures.
#
# For local Buck experiments (where we don't check in generated third-party
# BUCK files), it's simplest to disable these aliases and depend on
# versioned targets directly.
pass
def _codex_extra_srcs_for_manifest_dir(manifest_dir):
if not manifest_dir:
return []
# Use per-crate globs rooted at the crate's vendored manifest dir, which is
# passed through by Reindeer as CARGO_MANIFEST_DIR (e.g. vendor/foo-1.2.3).
#
# We include *all* files under the crate root so `include_str!` and
# `include_bytes!` work without per-crate Reindeer fixups. This is local-only
# buckification, so we prefer robustness over a minimal srcs list.
return glob(
["{}/**".format(manifest_dir)],
exclude = [
"{}/target/**".format(manifest_dir),
"{}/.git/**".format(manifest_dir),
],
)
def codex_rust_library(**kwargs):
# Make generated third-party targets consumable from anywhere in the repo.
kwargs["visibility"] = ["PUBLIC"]
env = kwargs.get("env", {})
manifest_dir = env.get("CARGO_MANIFEST_DIR")
srcs = list(kwargs.get("srcs", []))
srcs.extend(_codex_extra_srcs_for_manifest_dir(manifest_dir))
kwargs["srcs"] = srcs
cargo.rust_library(**kwargs)
def codex_rust_binary(**kwargs):
kwargs["visibility"] = ["PUBLIC"]
env = kwargs.get("env", {})
manifest_dir = env.get("CARGO_MANIFEST_DIR")
srcs = list(kwargs.get("srcs", []))
srcs.extend(_codex_extra_srcs_for_manifest_dir(manifest_dir))
kwargs["srcs"] = srcs
cargo.rust_binary(**kwargs)
def codex_buildscript_run(**kwargs):
# Many build scripts (especially those using `cc`/`cc-rs`) expect Cargo to
# provide a handful of profile env vars. Buck does not set these by default.
env = dict(kwargs.get("env", {}))
rust_profile = read_config("codex", "rust_profile", "dev")
if rust_profile == "release":
env.setdefault("OPT_LEVEL", "3")
env.setdefault("PROFILE", "release")
env.setdefault("DEBUG", "false")
else:
env.setdefault("OPT_LEVEL", "0")
env.setdefault("PROFILE", "debug")
env.setdefault("DEBUG", "true")
# Provide common Cargo cfg env vars that some build scripts expect.
env.setdefault(
"CARGO_CFG_TARGET_OS",
select({
"prelude//os:linux": "linux",
"prelude//os:macos": "macos",
"prelude//os:windows": "windows",
"DEFAULT": "",
}),
)
env.setdefault(
"CARGO_CFG_TARGET_ARCH",
select({
"prelude//cpu:arm64": "aarch64",
"prelude//cpu:x86_64": "x86_64",
"DEFAULT": "",
}),
)
env.setdefault("CARGO_CFG_TARGET_ENDIAN", "little")
env.setdefault(
"CARGO_CFG_TARGET_ENV",
select({
"prelude//os:linux": "gnu",
"DEFAULT": "",
}),
)
# Forward native link directives emitted by build scripts into rustc flags.
# Without this, crates like `ring` and `tree-sitter-*` will compile but fail
# to link due to missing native symbols.
kwargs.setdefault("rustc_link_lib", True)
kwargs.setdefault("rustc_link_search", True)
# `CARGO_MANIFEST_DIR` is expected by many build scripts. We can usually
# derive it from the `manifest_dir` parameter that buildscript_run uses.
manifest_dir = kwargs.get("manifest_dir")
if type(manifest_dir) == type(""):
env.setdefault("CARGO_MANIFEST_DIR", "$(location {})".format(manifest_dir))
kwargs["env"] = env
_prelude_buildscript_run(**kwargs)