Skip to content

Commit 7ae2dcd

Browse files
committed
refactor: convert build_data_writer build tool to Rust
Writing target `build_data.txt` stamps uses separate shell and PowerShell scripts across Unix and Windows hosts. Add `//crates/build_data_writer:build_data_writer_lib`, register `build_data_writer` on `shrubbery`, and route `PyWriteBuildData` through `get_shrubbery_tool` with the platform scripts as fallback.
1 parent 4b1d2e6 commit 7ae2dcd

8 files changed

Lines changed: 581 additions & 17 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_library")
2+
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup") # buildifier: disable=bzl-visibility
3+
4+
package(default_visibility = ["//:__subpackages__"])
5+
6+
licenses(["notice"])
7+
8+
rust_library(
9+
name = "build_data_writer_lib",
10+
srcs = ["src/lib.rs"],
11+
edition = "2024",
12+
deps = [
13+
"@crates//:anyhow",
14+
"@crates//:lexopt",
15+
],
16+
)
17+
18+
distribution_filegroup(
19+
name = "distribution",
20+
)
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
//! Library for writing Bazel target build data and workspace status stamping info.
2+
//!
3+
//! Configuration is read from the environment variables `OUTPUT`, `TARGET`, `STAMPED`,
4+
//! `VERSION_FILE`, and `INFO_FILE`, matching the `build_data_writer.sh` script it replaces.
5+
//! The command line accepts no arguments other than `-h`/`--help`.
6+
7+
// Errors must be returned, not panicked; see the `rust-lint` config in .bazelrc.
8+
#![deny(clippy::expect_used, clippy::unwrap_used)]
9+
10+
use std::env;
11+
use std::ffi::OsString;
12+
use std::fs::{self, File};
13+
use std::io::{BufWriter, Write};
14+
use std::path::{Path, PathBuf};
15+
use std::process::ExitCode;
16+
17+
use anyhow::{Context, Result, bail};
18+
use lexopt::prelude::*;
19+
20+
mod flags {
21+
// keep-sorted start
22+
pub const HELP: &str = "help";
23+
pub const HELP_SHORT: char = 'h';
24+
// keep-sorted end
25+
}
26+
27+
/// Environment variables consumed by the tool.
28+
mod env_vars {
29+
// keep-sorted start
30+
pub const INFO_FILE: &str = "INFO_FILE";
31+
pub const OUTPUT: &str = "OUTPUT";
32+
pub const STAMPED: &str = "STAMPED";
33+
pub const TARGET: &str = "TARGET";
34+
pub const VERSION_FILE: &str = "VERSION_FILE";
35+
// keep-sorted end
36+
}
37+
38+
/// Usage text for `--help`. `lexopt` does not generate help, so this is maintained by hand.
39+
const USAGE: &str = "\
40+
Usage: build_data_writer
41+
42+
Write a build_data.txt file describing a target and its workspace status.
43+
All configuration comes from environment variables; no arguments are accepted.
44+
45+
Environment variables:
46+
OUTPUT Path of the build data file to write (required)
47+
TARGET Label of the target being built (required)
48+
STAMPED TRUE or FALSE; whether workspace status stamping is enabled (required)
49+
VERSION_FILE Path to volatile-status.txt to append; ignored when empty
50+
INFO_FILE Path to stable-status.txt to append; ignored when empty
51+
52+
Options:
53+
-h, --help Print this help";
54+
55+
/// Configuration options for writing a `build_data.txt` file.
56+
#[derive(Debug, Clone, PartialEq, Eq)]
57+
pub struct Options {
58+
/// Path to the output `build_data.txt` file.
59+
pub output: PathBuf,
60+
/// Target label string (e.g. `//pkg:target`).
61+
pub target: String,
62+
/// Whether stamping is enabled.
63+
pub stamped: bool,
64+
/// Optional path to Bazel's `volatile-status.txt` (`ctx.version_file`).
65+
pub version_file: Option<PathBuf>,
66+
/// Optional path to Bazel's `stable-status.txt` (`ctx.info_file`).
67+
pub info_file: Option<PathBuf>,
68+
}
69+
70+
/// Result of parsing the command line and environment.
71+
#[derive(Debug, Clone, PartialEq, Eq)]
72+
pub enum Cli {
73+
/// `--help` was requested.
74+
Help,
75+
/// Write the build data file with the given options.
76+
Run(Options),
77+
}
78+
79+
fn parse_stamped(val: &str) -> Result<bool> {
80+
match val {
81+
"TRUE" => Ok(true),
82+
"FALSE" => Ok(false),
83+
other => bail!("{} must be TRUE or FALSE, got '{other}'", env_vars::STAMPED),
84+
}
85+
}
86+
87+
fn required_env<F>(get_env: &F, key: &str) -> Result<String>
88+
where
89+
F: Fn(&str) -> Option<OsString>,
90+
{
91+
get_env(key)
92+
.with_context(|| format!("missing required environment variable: {key}"))?
93+
.into_string()
94+
.map_err(|raw| anyhow::anyhow!("{key} environment variable is not valid UTF-8: {raw:?}"))
95+
}
96+
97+
/// Parses command-line arguments (excluding `argv[0]` and subcommand name) and the environment
98+
/// provided by `get_env` into [`Cli`].
99+
///
100+
/// # Errors
101+
///
102+
/// Returns an error when an argument other than `-h`/`--help` is given, when `OUTPUT`, `TARGET`,
103+
/// or `STAMPED` is unset, or when `STAMPED` is neither `TRUE` nor `FALSE`.
104+
pub fn parse_env_and_args<I, T, F>(args: I, get_env: F) -> Result<Cli>
105+
where
106+
I: IntoIterator<Item = T>,
107+
T: Into<OsString>,
108+
F: Fn(&str) -> Option<OsString>,
109+
{
110+
let mut parser = lexopt::Parser::from_args(args.into_iter().map(Into::into));
111+
if let Some(arg) = parser.next()? {
112+
match arg {
113+
Short(flags::HELP_SHORT) | Long(flags::HELP) => return Ok(Cli::Help),
114+
_ => bail!("{}", arg.unexpected()),
115+
}
116+
}
117+
118+
let output = get_env(env_vars::OUTPUT)
119+
.map(PathBuf::from)
120+
.with_context(|| {
121+
format!(
122+
"missing required environment variable: {}",
123+
env_vars::OUTPUT
124+
)
125+
})?;
126+
let target = required_env(&get_env, env_vars::TARGET)?;
127+
let stamped = parse_stamped(&required_env(&get_env, env_vars::STAMPED)?)?;
128+
let version_file = get_env(env_vars::VERSION_FILE)
129+
.filter(|s| !s.is_empty())
130+
.map(PathBuf::from);
131+
let info_file = get_env(env_vars::INFO_FILE)
132+
.filter(|s| !s.is_empty())
133+
.map(PathBuf::from);
134+
135+
Ok(Cli::Run(Options {
136+
output,
137+
target,
138+
stamped,
139+
version_file,
140+
info_file,
141+
}))
142+
}
143+
144+
/// Parses command-line arguments (excluding `argv[0]` and subcommand name) and the process
145+
/// environment into [`Cli`].
146+
///
147+
/// # Errors
148+
///
149+
/// See [`parse_env_and_args`].
150+
pub fn parse_args<I, T>(args: I) -> Result<Cli>
151+
where
152+
I: IntoIterator<Item = T>,
153+
T: Into<OsString>,
154+
{
155+
parse_env_and_args(args, |key| env::var_os(key))
156+
}
157+
158+
/// Parses CLI arguments (excluding `argv[0]` and subcommand name) and the process environment,
159+
/// writes the build data file, and returns the process exit code.
160+
pub fn run_cli<I, T>(args: I) -> ExitCode
161+
where
162+
I: IntoIterator<Item = T>,
163+
T: Into<OsString>,
164+
{
165+
let options = match parse_args(args) {
166+
Ok(Cli::Help) => {
167+
println!("{USAGE}");
168+
return ExitCode::SUCCESS;
169+
}
170+
Ok(Cli::Run(opts)) => opts,
171+
Err(err) => {
172+
eprintln!("build_data_writer: error: {err:#}");
173+
return ExitCode::FAILURE;
174+
}
175+
};
176+
if let Err(err) = write_build_data(&options) {
177+
eprintln!("build_data_writer: error: {err:#}");
178+
return ExitCode::FAILURE;
179+
}
180+
ExitCode::SUCCESS
181+
}
182+
183+
/// Writes the build data file according to `options`.
184+
///
185+
/// # Errors
186+
///
187+
/// Returns an error when the output file cannot be created or written, or when a configured
188+
/// status file cannot be read.
189+
pub fn write_build_data(options: &Options) -> Result<()> {
190+
let file = File::create(&options.output)
191+
.with_context(|| format!("creating {}", options.output.display()))?;
192+
let mut writer = BufWriter::new(file);
193+
194+
writeln!(writer, "TARGET {}", options.target)?;
195+
writeln!(
196+
writer,
197+
"STAMPED {}",
198+
if options.stamped { "TRUE" } else { "FALSE" }
199+
)?;
200+
201+
if let Some(version_file) = &options.version_file {
202+
append_status_file(&mut writer, version_file)?;
203+
}
204+
if let Some(info_file) = &options.info_file {
205+
append_status_file(&mut writer, info_file)?;
206+
}
207+
208+
writer
209+
.flush()
210+
.with_context(|| format!("writing {}", options.output.display()))
211+
}
212+
213+
fn append_status_file<W: Write>(writer: &mut W, path: &Path) -> Result<()> {
214+
let contents = fs::read(path).with_context(|| format!("reading {}", path.display()))?;
215+
writer.write_all(&contents)?;
216+
if !contents.is_empty() && !contents.ends_with(b"\n") {
217+
writer.write_all(b"\n")?;
218+
}
219+
Ok(())
220+
}

‎crates/shrubbery/BUILD.bazel‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rust_library(
1010
srcs = ["src/lib.rs"],
1111
edition = "2024",
1212
deps = [
13+
"//crates/build_data_writer:build_data_writer_lib",
1314
"//crates/exe_zip_maker:exe_zip_maker_lib",
1415
"//crates/extract_wheel_files:extract_wheel_files_lib",
1516
"//crates/py_console_script_gen:py_console_script_gen_lib",

‎crates/shrubbery/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::process::ExitCode;
1010
/// Subcommand name constants supported by `shrubbery`.
1111
pub mod subcommands {
1212
// keep-sorted start case=no
13+
pub const BUILD_DATA_WRITER: &str = "build_data_writer";
1314
pub const EXE_ZIP_MAKER: &str = "exe_zip_maker";
1415
pub const EXTRACT_WHEEL_FILES: &str = "extract_wheel_files";
1516
pub const PY_CONSOLE_SCRIPT_GEN: &str = "py_console_script_gen";
@@ -25,6 +26,7 @@ pub mod subcommands {
2526
/// Canonical subcommand names supported by `shrubbery`.
2627
pub const SUBCOMMANDS: &[&str] = &[
2728
// keep-sorted start case=no
29+
subcommands::BUILD_DATA_WRITER,
2830
subcommands::EXE_ZIP_MAKER,
2931
subcommands::EXTRACT_WHEEL_FILES,
3032
subcommands::PY_CONSOLE_SCRIPT_GEN,
@@ -75,6 +77,7 @@ where
7577

7678
match sub_raw.as_ref() {
7779
// keep-sorted start case=no
80+
subcommands::BUILD_DATA_WRITER => build_data_writer_lib::run_cli(iter),
7881
subcommands::EXE_ZIP_MAKER => exe_zip_maker_lib::run_cli(iter),
7982
subcommands::EXTRACT_WHEEL_FILES => extract_wheel_files_lib::run_cli(iter),
8083
subcommands::PY_CONSOLE_SCRIPT_GEN => py_console_script_gen_lib::run_cli(iter),

‎python/private/py_executable.bzl‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,22 +1723,33 @@ def _write_build_data(ctx):
17231723
)
17241724

17251725
action_args = ctx.actions.args()
1726-
writer_file = ctx.files._build_data_writer[0]
1727-
if writer_file.path.endswith(".ps1"):
1728-
# powershell.exe is used for broader compatibility
1729-
# It is installed by default on most Windows versions
1730-
action_exe = "powershell.exe"
1731-
action_args.add_all([
1732-
# Bypass execution policy is needed because,
1733-
# by default, Windows blocks ps1 scripts.
1734-
"-ExecutionPolicy",
1735-
"Bypass",
1736-
"-File",
1737-
writer_file,
1738-
])
1739-
inputs.add(writer_file)
1726+
writer = get_shrubbery_tool(
1727+
ctx,
1728+
"build_data_writer",
1729+
ctx.attr._build_data_writer,
1730+
)
1731+
if writer.is_rust:
1732+
action_exe = writer.files_to_run
1733+
action_toolchain = writer.toolchain
1734+
action_args.add_all(writer.prefix_args)
17401735
else:
1741-
action_exe = ctx.attr._build_data_writer[DefaultInfo].files_to_run
1736+
action_toolchain = None
1737+
writer_file = ctx.files._build_data_writer[0]
1738+
if writer_file.path.endswith(".ps1"):
1739+
# powershell.exe is used for broader compatibility
1740+
# It is installed by default on most Windows versions
1741+
action_exe = "powershell.exe"
1742+
action_args.add_all([
1743+
# Bypass execution policy is needed because,
1744+
# by default, Windows blocks ps1 scripts.
1745+
"-ExecutionPolicy",
1746+
"Bypass",
1747+
"-File",
1748+
writer_file,
1749+
])
1750+
inputs.add(writer_file)
1751+
else:
1752+
action_exe = writer.files_to_run
17421753

17431754
ctx.actions.run(
17441755
executable = action_exe,
@@ -1756,7 +1767,7 @@ def _write_build_data(ctx):
17561767
outputs = [build_data],
17571768
mnemonic = "PyWriteBuildData",
17581769
progress_message = "Reticulating %{label} build data",
1759-
toolchain = None,
1770+
toolchain = action_toolchain,
17601771
)
17611772
return build_data
17621773

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_test")
2+
3+
package(default_visibility = ["//:__subpackages__"])
4+
5+
licenses(["notice"])
6+
7+
rust_test(
8+
name = "build_data_writer_test",
9+
size = "small",
10+
srcs = ["build_data_writer_test.rs"],
11+
edition = "2024",
12+
deps = [
13+
"//crates/build_data_writer:build_data_writer_lib",
14+
],
15+
)

0 commit comments

Comments
 (0)