Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/build_data_writer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@rules_rust//rust:defs.bzl", "rust_library")
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup") # buildifier: disable=bzl-visibility

package(default_visibility = ["//:__subpackages__"])

licenses(["notice"])

rust_library(
name = "build_data_writer_lib",
srcs = ["src/lib.rs"],
edition = "2024",
deps = [
"@crates//:anyhow",
"@crates//:lexopt",
],
)

distribution_filegroup(
name = "distribution",
)
220 changes: 220 additions & 0 deletions crates/build_data_writer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
//! Library for writing Bazel target build data and workspace status stamping info.
//!
//! Configuration is read from the environment variables `OUTPUT`, `TARGET`, `STAMPED`,
//! `VERSION_FILE`, and `INFO_FILE`, matching the `build_data_writer.sh` script it replaces.
//! The command line accepts no arguments other than `-h`/`--help`.

// Errors must be returned, not panicked; see the `rust-lint` config in .bazelrc.
#![deny(clippy::expect_used, clippy::unwrap_used)]

use std::env;
use std::ffi::OsString;
use std::fs::{self, File};
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use anyhow::{Context, Result, bail};
use lexopt::prelude::*;

mod flags {
// keep-sorted start
pub const HELP: &str = "help";
pub const HELP_SHORT: char = 'h';
// keep-sorted end
}

/// Environment variables consumed by the tool.
mod env_vars {
// keep-sorted start
pub const INFO_FILE: &str = "INFO_FILE";
pub const OUTPUT: &str = "OUTPUT";
pub const STAMPED: &str = "STAMPED";
pub const TARGET: &str = "TARGET";
pub const VERSION_FILE: &str = "VERSION_FILE";
// keep-sorted end
}

/// Usage text for `--help`. `lexopt` does not generate help, so this is maintained by hand.
const USAGE: &str = "\
Usage: build_data_writer

Write a build_data.txt file describing a target and its workspace status.
All configuration comes from environment variables; no arguments are accepted.

Environment variables:
OUTPUT Path of the build data file to write (required)
TARGET Label of the target being built (required)
STAMPED TRUE or FALSE; whether workspace status stamping is enabled (required)
VERSION_FILE Path to volatile-status.txt to append; ignored when empty
INFO_FILE Path to stable-status.txt to append; ignored when empty

Options:
-h, --help Print this help";

/// Configuration options for writing a `build_data.txt` file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Options {
/// Path to the output `build_data.txt` file.
pub output: PathBuf,
/// Target label string (e.g. `//pkg:target`).
pub target: String,
/// Whether stamping is enabled.
pub stamped: bool,
/// Optional path to Bazel's `volatile-status.txt` (`ctx.version_file`).
pub version_file: Option<PathBuf>,
/// Optional path to Bazel's `stable-status.txt` (`ctx.info_file`).
pub info_file: Option<PathBuf>,
}

/// Result of parsing the command line and environment.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Cli {
/// `--help` was requested.
Help,
/// Write the build data file with the given options.
Run(Options),
}

fn parse_stamped(val: &str) -> Result<bool> {
match val {
"TRUE" => Ok(true),
"FALSE" => Ok(false),
other => bail!("{} must be TRUE or FALSE, got '{other}'", env_vars::STAMPED),
}
}

fn required_env<F>(get_env: &F, key: &str) -> Result<String>
where
F: Fn(&str) -> Option<OsString>,
{
get_env(key)
.with_context(|| format!("missing required environment variable: {key}"))?
.into_string()
.map_err(|raw| anyhow::anyhow!("{key} environment variable is not valid UTF-8: {raw:?}"))
}

/// Parses command-line arguments (excluding `argv[0]` and subcommand name) and the environment
/// provided by `get_env` into [`Cli`].
///
/// # Errors
///
/// Returns an error when an argument other than `-h`/`--help` is given, when `OUTPUT`, `TARGET`,
/// or `STAMPED` is unset, or when `STAMPED` is neither `TRUE` nor `FALSE`.
pub fn parse_env_and_args<I, T, F>(args: I, get_env: F) -> Result<Cli>
where
I: IntoIterator<Item = T>,
T: Into<OsString>,
F: Fn(&str) -> Option<OsString>,
{
let mut parser = lexopt::Parser::from_args(args.into_iter().map(Into::into));
if let Some(arg) = parser.next()? {
match arg {
Short(flags::HELP_SHORT) | Long(flags::HELP) => return Ok(Cli::Help),
_ => bail!("{}", arg.unexpected()),
}
}

let output = get_env(env_vars::OUTPUT)
.map(PathBuf::from)
.with_context(|| {
format!(
"missing required environment variable: {}",
env_vars::OUTPUT
)
})?;
let target = required_env(&get_env, env_vars::TARGET)?;
let stamped = parse_stamped(&required_env(&get_env, env_vars::STAMPED)?)?;
let version_file = get_env(env_vars::VERSION_FILE)
.filter(|s| !s.is_empty())
.map(PathBuf::from);
let info_file = get_env(env_vars::INFO_FILE)
.filter(|s| !s.is_empty())
.map(PathBuf::from);

Ok(Cli::Run(Options {
output,
target,
stamped,
version_file,
info_file,
}))
}

/// Parses command-line arguments (excluding `argv[0]` and subcommand name) and the process
/// environment into [`Cli`].
///
/// # Errors
///
/// See [`parse_env_and_args`].
pub fn parse_args<I, T>(args: I) -> Result<Cli>
where
I: IntoIterator<Item = T>,
T: Into<OsString>,
{
parse_env_and_args(args, |key| env::var_os(key))
}

/// Parses CLI arguments (excluding `argv[0]` and subcommand name) and the process environment,
/// writes the build data file, and returns the process exit code.
pub fn run_cli<I, T>(args: I) -> ExitCode
where
I: IntoIterator<Item = T>,
T: Into<OsString>,
{
let options = match parse_args(args) {
Ok(Cli::Help) => {
println!("{USAGE}");
return ExitCode::SUCCESS;
}
Ok(Cli::Run(opts)) => opts,
Err(err) => {
eprintln!("build_data_writer: error: {err:#}");
return ExitCode::FAILURE;
}
};
if let Err(err) = write_build_data(&options) {
eprintln!("build_data_writer: error: {err:#}");
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}

/// Writes the build data file according to `options`.
///
/// # Errors
///
/// Returns an error when the output file cannot be created or written, or when a configured
/// status file cannot be read.
pub fn write_build_data(options: &Options) -> Result<()> {
let file = File::create(&options.output)
.with_context(|| format!("creating {}", options.output.display()))?;
let mut writer = BufWriter::new(file);

writeln!(writer, "TARGET {}", options.target)?;
writeln!(
writer,
"STAMPED {}",
if options.stamped { "TRUE" } else { "FALSE" }
)?;

if let Some(version_file) = &options.version_file {
append_status_file(&mut writer, version_file)?;
}
if let Some(info_file) = &options.info_file {
append_status_file(&mut writer, info_file)?;
}

writer
.flush()
.with_context(|| format!("writing {}", options.output.display()))
}

fn append_status_file<W: Write>(writer: &mut W, path: &Path) -> Result<()> {
let contents = fs::read(path).with_context(|| format!("reading {}", path.display()))?;
writer.write_all(&contents)?;
if !contents.is_empty() && !contents.ends_with(b"\n") {
writer.write_all(b"\n")?;
}
Ok(())
}
1 change: 1 addition & 0 deletions crates/shrubbery/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rust_library(
srcs = ["src/lib.rs"],
edition = "2024",
deps = [
"//crates/build_data_writer:build_data_writer_lib",
"//crates/exe_zip_maker:exe_zip_maker_lib",
"//crates/extract_wheel_files:extract_wheel_files_lib",
"//crates/py_console_script_gen:py_console_script_gen_lib",
Expand Down
3 changes: 3 additions & 0 deletions crates/shrubbery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::process::ExitCode;
/// Subcommand name constants supported by `shrubbery`.
pub mod subcommands {
// keep-sorted start case=no
pub const BUILD_DATA_WRITER: &str = "build_data_writer";
pub const EXE_ZIP_MAKER: &str = "exe_zip_maker";
pub const EXTRACT_WHEEL_FILES: &str = "extract_wheel_files";
pub const PY_CONSOLE_SCRIPT_GEN: &str = "py_console_script_gen";
Expand All @@ -25,6 +26,7 @@ pub mod subcommands {
/// Canonical subcommand names supported by `shrubbery`.
pub const SUBCOMMANDS: &[&str] = &[
// keep-sorted start case=no
subcommands::BUILD_DATA_WRITER,
subcommands::EXE_ZIP_MAKER,
subcommands::EXTRACT_WHEEL_FILES,
subcommands::PY_CONSOLE_SCRIPT_GEN,
Expand Down Expand Up @@ -75,6 +77,7 @@ where

match sub_raw.as_ref() {
// keep-sorted start case=no
subcommands::BUILD_DATA_WRITER => build_data_writer_lib::run_cli(iter),
subcommands::EXE_ZIP_MAKER => exe_zip_maker_lib::run_cli(iter),
subcommands::EXTRACT_WHEEL_FILES => extract_wheel_files_lib::run_cli(iter),
subcommands::PY_CONSOLE_SCRIPT_GEN => py_console_script_gen_lib::run_cli(iter),
Expand Down
43 changes: 27 additions & 16 deletions python/private/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1723,22 +1723,33 @@ def _write_build_data(ctx):
)

action_args = ctx.actions.args()
writer_file = ctx.files._build_data_writer[0]
if writer_file.path.endswith(".ps1"):
# powershell.exe is used for broader compatibility
# It is installed by default on most Windows versions
action_exe = "powershell.exe"
action_args.add_all([
# Bypass execution policy is needed because,
# by default, Windows blocks ps1 scripts.
"-ExecutionPolicy",
"Bypass",
"-File",
writer_file,
])
inputs.add(writer_file)
writer = get_shrubbery_tool(
ctx,
"build_data_writer",
ctx.attr._build_data_writer,
)
if writer.is_rust:
action_exe = writer.files_to_run
action_toolchain = writer.toolchain
action_args.add_all(writer.prefix_args)
else:
action_exe = ctx.attr._build_data_writer[DefaultInfo].files_to_run
action_toolchain = None
writer_file = ctx.files._build_data_writer[0]
if writer_file.path.endswith(".ps1"):
# powershell.exe is used for broader compatibility
# It is installed by default on most Windows versions
action_exe = "powershell.exe"
action_args.add_all([
# Bypass execution policy is needed because,
# by default, Windows blocks ps1 scripts.
"-ExecutionPolicy",
"Bypass",
"-File",
writer_file,
])
inputs.add(writer_file)
else:
action_exe = writer.files_to_run

ctx.actions.run(
executable = action_exe,
Expand All @@ -1756,7 +1767,7 @@ def _write_build_data(ctx):
outputs = [build_data],
mnemonic = "PyWriteBuildData",
progress_message = "Reticulating %{label} build data",
toolchain = None,
toolchain = action_toolchain,
)
return build_data

Expand Down
15 changes: 15 additions & 0 deletions tests/build_data_writer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@rules_rust//rust:defs.bzl", "rust_test")

package(default_visibility = ["//:__subpackages__"])

licenses(["notice"])

rust_test(
name = "build_data_writer_test",
size = "small",
srcs = ["build_data_writer_test.rs"],
edition = "2024",
deps = [
"//crates/build_data_writer:build_data_writer_lib",
],
)
Loading
Loading