Skip to content

Commit 7584b08

Browse files
authored
Support new cargo:: prefixes in build script outputs. (#3211)
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]>
1 parent 61f4186 commit 7584b08

File tree

1 file changed

+50
-38
lines changed
  • cargo/cargo_build_script_runner

1 file changed

+50
-38
lines changed

cargo/cargo_build_script_runner/lib.rs

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ pub struct CompileAndLinkFlags {
2929
/// Enum containing all the considered return value from the script
3030
#[derive(Debug, Clone, PartialEq, Eq)]
3131
pub enum BuildScriptOutput {
32-
/// cargo:rustc-link-lib
32+
/// cargo::rustc-link-lib
3333
LinkLib(String),
34-
/// cargo:rustc-link-search
34+
/// cargo::rustc-link-search
3535
LinkSearch(String),
36-
/// cargo:rustc-cfg
36+
/// cargo::rustc-cfg
3737
Cfg(String),
38-
/// cargo:rustc-flags
38+
/// cargo::rustc-flags
3939
Flags(String),
40-
/// cargo:rustc-link-arg
40+
/// cargo::rustc-link-arg
4141
LinkArg(String),
42-
/// cargo:rustc-env
42+
/// cargo::rustc-env
4343
Env(String),
44-
/// cargo:VAR=VALUE
44+
/// cargo::VAR=VALUE
4545
DepEnv(String),
4646
}
4747

@@ -50,7 +50,7 @@ impl BuildScriptOutput {
5050
///
5151
/// Examples
5252
/// ```rust
53-
/// assert_eq!(BuildScriptOutput::new("cargo:rustc-link-lib=lib"), Some(BuildScriptOutput::LinkLib("lib".to_owned())));
53+
/// assert_eq!(BuildScriptOutput::new("cargo::rustc-link-lib=lib"), Some(BuildScriptOutput::LinkLib("lib".to_owned())));
5454
/// ```
5555
fn new(line: &str) -> Option<BuildScriptOutput> {
5656
let split = line.splitn(2, '=').collect::<Vec<_>>();
@@ -59,13 +59,18 @@ impl BuildScriptOutput {
5959
return None;
6060
}
6161
let param = split[1].trim().to_owned();
62-
let key_split = split[0].splitn(2, ':').collect::<Vec<_>>();
63-
if key_split.len() <= 1 || key_split[0] != "cargo" {
64-
// Not a cargo directive.
65-
return None;
66-
}
62+
let cargo_instruction_name = {
63+
if split[0].starts_with("cargo::") {
64+
&split[0][7..]
65+
} else if split[0].starts_with("cargo:") {
66+
&split[0][6..]
67+
} else {
68+
// Not a cargo directive.
69+
return None;
70+
}
71+
};
6772

68-
match key_split[1] {
73+
match cargo_instruction_name {
6974
"rustc-link-lib" => Some(BuildScriptOutput::LinkLib(param)),
7075
"rustc-link-search" => Some(BuildScriptOutput::LinkSearch(param)),
7176
"rustc-cfg" => Some(BuildScriptOutput::Cfg(param)),
@@ -82,20 +87,20 @@ impl BuildScriptOutput {
8287
None
8388
}
8489
"rustc-cdylib-link-arg" | "rustc-link-arg-bin" | "rustc-link-arg-bins" => {
85-
// cargo:rustc-cdylib-link-arg=FLAG — Passes custom flags to a linker for cdylib crates.
86-
// cargo:rustc-link-arg-bin=BIN=FLAG – Passes custom flags to a linker for the binary BIN.
87-
// cargo:rustc-link-arg-bins=FLAG – Passes custom flags to a linker for binaries.
90+
// cargo::rustc-cdylib-link-arg=FLAG — Passes custom flags to a linker for cdylib crates.
91+
// cargo::rustc-link-arg-bin=BIN=FLAG – Passes custom flags to a linker for the binary BIN.
92+
// cargo::rustc-link-arg-bins=FLAG – Passes custom flags to a linker for binaries.
8893
eprint!(
8994
"Warning: build script returned unsupported directive `{}`",
9095
split[0]
9196
);
9297
None
9398
}
9499
_ => {
95-
// cargo:KEY=VALUE — Metadata, used by links scripts.
100+
// cargo::KEY=VALUE — Metadata, used by links scripts.
96101
Some(BuildScriptOutput::DepEnv(format!(
97102
"{}={}",
98-
key_split[1].to_uppercase().replace('-', "_"),
103+
cargo_instruction_name.to_uppercase().replace('-', "_"),
99104
param
100105
)))
101106
}
@@ -230,24 +235,27 @@ mod tests {
230235
fn test_from_read_buffer_to_env_and_flags() {
231236
let buff = Cursor::new(
232237
"
233-
cargo:rustc-link-lib=sdfsdf
234-
cargo:rustc-env=FOO=BAR
235-
cargo:rustc-link-search=/some/absolute/path/bleh
236-
cargo:rustc-env=BAR=FOO
237-
cargo:rustc-flags=-Lblah
238-
cargo:rerun-if-changed=ignored
239-
cargo:rustc-cfg=feature=awesome
240-
cargo:version=123
241-
cargo:version_number=1010107f
242-
cargo:include_path=/some/absolute/path/include
243-
cargo:rustc-env=SOME_PATH=/some/absolute/path/beep
244-
cargo:rustc-link-arg=-weak_framework
245-
cargo:rustc-link-arg=Metal
246-
cargo:rustc-env=no_trailing_newline=true",
238+
cargo::rustc-link-lib=sdfsdf
239+
cargo::rustc-env=FOO=BAR
240+
cargo::rustc-link-search=/some/absolute/path/bleh
241+
cargo::rustc-env=BAR=FOO
242+
cargo::rustc-flags=-Lblah
243+
cargo::rerun-if-changed=ignored
244+
cargo::rustc-cfg=feature=awesome
245+
cargo::version=123
246+
cargo::version_number=1010107f
247+
cargo::include_path=/some/absolute/path/include
248+
cargo::rustc-env=SOME_PATH=/some/absolute/path/beep
249+
cargo::rustc-link-arg=-weak_framework
250+
cargo::rustc-link-arg=Metal
251+
cargo::rustc-env=no_trailing_newline=true
252+
non-cargo-prefixes::are-ignored=true
253+
non-assignment-instructions-are-ignored
254+
cargo:rustc-env=old_cargo_colon_prefix_works=true",
247255
);
248256
let reader = BufReader::new(buff);
249257
let result = BuildScriptOutput::outputs_from_reader(reader);
250-
assert_eq!(result.len(), 13);
258+
assert_eq!(result.len(), 14);
251259
assert_eq!(result[0], BuildScriptOutput::LinkLib("sdfsdf".to_owned()));
252260
assert_eq!(result[1], BuildScriptOutput::Env("FOO=BAR".to_owned()));
253261
assert_eq!(
@@ -281,13 +289,17 @@ cargo:rustc-env=no_trailing_newline=true",
281289
result[12],
282290
BuildScriptOutput::Env("no_trailing_newline=true".to_owned())
283291
);
292+
assert_eq!(
293+
result[13],
294+
BuildScriptOutput::Env("old_cargo_colon_prefix_works=true".to_owned())
295+
);
284296
assert_eq!(
285297
BuildScriptOutput::outputs_to_dep_env(&result, "ssh2", "/some/absolute/path"),
286298
"DEP_SSH2_VERSION=123\nDEP_SSH2_VERSION_NUMBER=1010107f\nDEP_SSH2_INCLUDE_PATH=${pwd}/include".to_owned()
287299
);
288300
assert_eq!(
289301
BuildScriptOutput::outputs_to_env(&result, "/some/absolute/path"),
290-
"FOO=BAR\nBAR=FOO\nSOME_PATH=${pwd}/beep\nno_trailing_newline=true".to_owned()
302+
"FOO=BAR\nBAR=FOO\nSOME_PATH=${pwd}/beep\nno_trailing_newline=true\nold_cargo_colon_prefix_works=true".to_owned()
291303
);
292304
assert_eq!(
293305
BuildScriptOutput::outputs_to_flags(&result, "/some/absolute/path"),
@@ -307,9 +319,9 @@ cargo:rustc-env=no_trailing_newline=true",
307319
fn invalid_utf8() {
308320
let buff = Cursor::new(
309321
b"
310-
cargo:rustc-env=valid1=1
311-
cargo:rustc-env=invalid=\xc3\x28
312-
cargo:rustc-env=valid2=2
322+
cargo::rustc-env=valid1=1
323+
cargo::rustc-env=invalid=\xc3\x28
324+
cargo::rustc-env=valid2=2
313325
",
314326
);
315327
let reader = BufReader::new(buff);

0 commit comments

Comments
 (0)