Skip to content

Commit

Permalink
Support new cargo:: prefixes in build script outputs. (#3211)
Browse files Browse the repository at this point in the history
In Rust 1.77.0, the preferred build directive prefix for Cargo commands
was changed from `cargo:` to `cargo::`. (The single-colon prefix is
still supported, but is no longer the default in documentation.)

With this commit, the `cargo_build_script_runner` tool now supports both
prefixes.

This fixes issue #2980.

Co-authored-by: Joel Lathrop <[email protected]>
  • Loading branch information
joell and joell authored Jan 28, 2025
1 parent 61f4186 commit 7584b08
Showing 1 changed file with 50 additions and 38 deletions.
88 changes: 50 additions & 38 deletions cargo/cargo_build_script_runner/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ pub struct CompileAndLinkFlags {
/// Enum containing all the considered return value from the script
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BuildScriptOutput {
/// cargo:rustc-link-lib
/// cargo::rustc-link-lib
LinkLib(String),
/// cargo:rustc-link-search
/// cargo::rustc-link-search
LinkSearch(String),
/// cargo:rustc-cfg
/// cargo::rustc-cfg
Cfg(String),
/// cargo:rustc-flags
/// cargo::rustc-flags
Flags(String),
/// cargo:rustc-link-arg
/// cargo::rustc-link-arg
LinkArg(String),
/// cargo:rustc-env
/// cargo::rustc-env
Env(String),
/// cargo:VAR=VALUE
/// cargo::VAR=VALUE
DepEnv(String),
}

Expand All @@ -50,7 +50,7 @@ impl BuildScriptOutput {
///
/// Examples
/// ```rust
/// assert_eq!(BuildScriptOutput::new("cargo:rustc-link-lib=lib"), Some(BuildScriptOutput::LinkLib("lib".to_owned())));
/// assert_eq!(BuildScriptOutput::new("cargo::rustc-link-lib=lib"), Some(BuildScriptOutput::LinkLib("lib".to_owned())));
/// ```
fn new(line: &str) -> Option<BuildScriptOutput> {
let split = line.splitn(2, '=').collect::<Vec<_>>();
Expand All @@ -59,13 +59,18 @@ impl BuildScriptOutput {
return None;
}
let param = split[1].trim().to_owned();
let key_split = split[0].splitn(2, ':').collect::<Vec<_>>();
if key_split.len() <= 1 || key_split[0] != "cargo" {
// Not a cargo directive.
return None;
}
let cargo_instruction_name = {
if split[0].starts_with("cargo::") {
&split[0][7..]
} else if split[0].starts_with("cargo:") {
&split[0][6..]
} else {
// Not a cargo directive.
return None;
}
};

match key_split[1] {
match cargo_instruction_name {
"rustc-link-lib" => Some(BuildScriptOutput::LinkLib(param)),
"rustc-link-search" => Some(BuildScriptOutput::LinkSearch(param)),
"rustc-cfg" => Some(BuildScriptOutput::Cfg(param)),
Expand All @@ -82,20 +87,20 @@ impl BuildScriptOutput {
None
}
"rustc-cdylib-link-arg" | "rustc-link-arg-bin" | "rustc-link-arg-bins" => {
// cargo:rustc-cdylib-link-arg=FLAG — Passes custom flags to a linker for cdylib crates.
// cargo:rustc-link-arg-bin=BIN=FLAG – Passes custom flags to a linker for the binary BIN.
// cargo:rustc-link-arg-bins=FLAG – Passes custom flags to a linker for binaries.
// cargo::rustc-cdylib-link-arg=FLAG — Passes custom flags to a linker for cdylib crates.
// cargo::rustc-link-arg-bin=BIN=FLAG – Passes custom flags to a linker for the binary BIN.
// cargo::rustc-link-arg-bins=FLAG – Passes custom flags to a linker for binaries.
eprint!(
"Warning: build script returned unsupported directive `{}`",
split[0]
);
None
}
_ => {
// cargo:KEY=VALUE — Metadata, used by links scripts.
// cargo::KEY=VALUE — Metadata, used by links scripts.
Some(BuildScriptOutput::DepEnv(format!(
"{}={}",
key_split[1].to_uppercase().replace('-', "_"),
cargo_instruction_name.to_uppercase().replace('-', "_"),
param
)))
}
Expand Down Expand Up @@ -230,24 +235,27 @@ mod tests {
fn test_from_read_buffer_to_env_and_flags() {
let buff = Cursor::new(
"
cargo:rustc-link-lib=sdfsdf
cargo:rustc-env=FOO=BAR
cargo:rustc-link-search=/some/absolute/path/bleh
cargo:rustc-env=BAR=FOO
cargo:rustc-flags=-Lblah
cargo:rerun-if-changed=ignored
cargo:rustc-cfg=feature=awesome
cargo:version=123
cargo:version_number=1010107f
cargo:include_path=/some/absolute/path/include
cargo:rustc-env=SOME_PATH=/some/absolute/path/beep
cargo:rustc-link-arg=-weak_framework
cargo:rustc-link-arg=Metal
cargo:rustc-env=no_trailing_newline=true",
cargo::rustc-link-lib=sdfsdf
cargo::rustc-env=FOO=BAR
cargo::rustc-link-search=/some/absolute/path/bleh
cargo::rustc-env=BAR=FOO
cargo::rustc-flags=-Lblah
cargo::rerun-if-changed=ignored
cargo::rustc-cfg=feature=awesome
cargo::version=123
cargo::version_number=1010107f
cargo::include_path=/some/absolute/path/include
cargo::rustc-env=SOME_PATH=/some/absolute/path/beep
cargo::rustc-link-arg=-weak_framework
cargo::rustc-link-arg=Metal
cargo::rustc-env=no_trailing_newline=true
non-cargo-prefixes::are-ignored=true
non-assignment-instructions-are-ignored
cargo:rustc-env=old_cargo_colon_prefix_works=true",
);
let reader = BufReader::new(buff);
let result = BuildScriptOutput::outputs_from_reader(reader);
assert_eq!(result.len(), 13);
assert_eq!(result.len(), 14);
assert_eq!(result[0], BuildScriptOutput::LinkLib("sdfsdf".to_owned()));
assert_eq!(result[1], BuildScriptOutput::Env("FOO=BAR".to_owned()));
assert_eq!(
Expand Down Expand Up @@ -281,13 +289,17 @@ cargo:rustc-env=no_trailing_newline=true",
result[12],
BuildScriptOutput::Env("no_trailing_newline=true".to_owned())
);
assert_eq!(
result[13],
BuildScriptOutput::Env("old_cargo_colon_prefix_works=true".to_owned())
);
assert_eq!(
BuildScriptOutput::outputs_to_dep_env(&result, "ssh2", "/some/absolute/path"),
"DEP_SSH2_VERSION=123\nDEP_SSH2_VERSION_NUMBER=1010107f\nDEP_SSH2_INCLUDE_PATH=${pwd}/include".to_owned()
);
assert_eq!(
BuildScriptOutput::outputs_to_env(&result, "/some/absolute/path"),
"FOO=BAR\nBAR=FOO\nSOME_PATH=${pwd}/beep\nno_trailing_newline=true".to_owned()
"FOO=BAR\nBAR=FOO\nSOME_PATH=${pwd}/beep\nno_trailing_newline=true\nold_cargo_colon_prefix_works=true".to_owned()
);
assert_eq!(
BuildScriptOutput::outputs_to_flags(&result, "/some/absolute/path"),
Expand All @@ -307,9 +319,9 @@ cargo:rustc-env=no_trailing_newline=true",
fn invalid_utf8() {
let buff = Cursor::new(
b"
cargo:rustc-env=valid1=1
cargo:rustc-env=invalid=\xc3\x28
cargo:rustc-env=valid2=2
cargo::rustc-env=valid1=1
cargo::rustc-env=invalid=\xc3\x28
cargo::rustc-env=valid2=2
",
);
let reader = BufReader::new(buff);
Expand Down

0 comments on commit 7584b08

Please sign in to comment.