From ec32c8c15dbcf286c0bafcfee83aaafb2d29ed1c Mon Sep 17 00:00:00 2001 From: Damien DeVille Date: Thu, 9 Dec 2021 11:06:31 -0800 Subject: [PATCH] Support `cargo:rustc-link-arg=FLAG` in build script runner --- cargo/cargo_build_script_runner/lib.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/cargo/cargo_build_script_runner/lib.rs b/cargo/cargo_build_script_runner/lib.rs index e2d1b7033d..29e8a7caed 100644 --- a/cargo/cargo_build_script_runner/lib.rs +++ b/cargo/cargo_build_script_runner/lib.rs @@ -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 @@ -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. @@ -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] @@ -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)), _ => {} @@ -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!( @@ -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"), @@ -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(), } );