Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cargo:rustc-link-arg=FLAG instruction in build script runner #1061

Merged
merged 1 commit into from
Dec 9, 2021
Merged
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
22 changes: 19 additions & 3 deletions cargo/cargo_build_script_runner/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum BuildScriptOutput {
Cfg(String),
/// cargo:rustc-flags
Flags(String),
/// cargo:rustc-link-arg
LinkArg(String),
/// cargo:rustc-env
Env(String),
/// cargo:VAR=VALUE
Expand All @@ -59,11 +61,13 @@ impl BuildScriptOutput {
// Not a cargo directive.
return None;
}

match key_split[1] {
"rustc-link-lib" => Some(BuildScriptOutput::LinkLib(param)),
"rustc-link-search" => Some(BuildScriptOutput::LinkSearch(param)),
"rustc-cfg" => Some(BuildScriptOutput::Cfg(param)),
"rustc-flags" => Some(BuildScriptOutput::Flags(param)),
"rustc-link-arg" => Some(BuildScriptOutput::LinkArg(param)),
"rustc-env" => Some(BuildScriptOutput::Env(param)),
"rerun-if-changed" | "rerun-if-env-changed" =>
// Ignored because Bazel will re-run if those change all the time.
Expand All @@ -74,8 +78,10 @@ impl BuildScriptOutput {
eprint!("Build Script Warning: {}", split[1]);
None
}
"rustc-cdylib-link-arg" => {
"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.
eprint!(
"Warning: build script returned unsupported directive `{}`",
split[0]
Expand Down Expand Up @@ -170,6 +176,7 @@ impl BuildScriptOutput {
match flag {
BuildScriptOutput::Cfg(e) => compile_flags.push(format!("--cfg={}", e)),
BuildScriptOutput::Flags(e) => compile_flags.push(e.to_owned()),
BuildScriptOutput::LinkArg(e) => compile_flags.push(format!("-Clink-arg={}", e)),
BuildScriptOutput::LinkLib(e) => link_flags.push(format!("-l{}", e)),
BuildScriptOutput::LinkSearch(e) => link_flags.push(format!("-L{}", e)),
_ => {}
Expand Down Expand Up @@ -219,11 +226,13 @@ 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
",
);
let reader = BufReader::new(buff);
let result = BuildScriptOutput::outputs_from_reader(reader);
assert_eq!(result.len(), 10);
assert_eq!(result.len(), 12);
assert_eq!(result[0], BuildScriptOutput::LinkLib("sdfsdf".to_owned()));
assert_eq!(result[1], BuildScriptOutput::Env("FOO=BAR".to_owned()));
assert_eq!(
Expand All @@ -248,6 +257,11 @@ cargo:rustc-env=SOME_PATH=/some/absolute/path/beep
result[9],
BuildScriptOutput::Env("SOME_PATH=/some/absolute/path/beep".to_owned())
);
assert_eq!(
result[10],
BuildScriptOutput::LinkArg("-weak_framework".to_owned())
);
assert_eq!(result[11], BuildScriptOutput::LinkArg("Metal".to_owned()));

assert_eq!(
BuildScriptOutput::outputs_to_dep_env(&result, "ssh2", "/some/absolute/path"),
Expand All @@ -262,7 +276,9 @@ cargo:rustc-env=SOME_PATH=/some/absolute/path/beep
CompileAndLinkFlags {
// -Lblah was output as a rustc-flags, so even though it probably _should_ be a link
// flag, we don't treat it like one.
compile_flags: "-Lblah\n--cfg=feature=awesome".to_owned(),
compile_flags:
"-Lblah\n--cfg=feature=awesome\n-Clink-arg=-weak_framework\n-Clink-arg=Metal"
.to_owned(),
link_flags: "-lsdfsdf\n-L${pwd}/bleh".to_owned(),
}
);
Expand Down