Fix rust flags handling in cargo-build-sbf

This commit is contained in:
Dmitri Makarov 2022-08-10 13:17:38 -07:00 committed by mergify[bot]
parent 1dcf8cf91e
commit d42fcf2e30
4 changed files with 96 additions and 32 deletions

19
Cargo.lock generated
View File

@ -1536,6 +1536,15 @@ dependencies = [
"miniz_oxide", "miniz_oxide",
] ]
[[package]]
name = "float-cmp"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -2743,6 +2752,12 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "normalize-line-endings"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"
[[package]] [[package]]
name = "ntapi" name = "ntapi"
version = "0.3.6" version = "0.3.6"
@ -3299,8 +3314,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c6ce811d0b2e103743eec01db1c50612221f173084ce2f7941053e94b6bb474" checksum = "5c6ce811d0b2e103743eec01db1c50612221f173084ce2f7941053e94b6bb474"
dependencies = [ dependencies = [
"difflib", "difflib",
"float-cmp",
"itertools", "itertools",
"normalize-line-endings",
"predicates-core", "predicates-core",
"regex",
] ]
[[package]] [[package]]
@ -4821,6 +4839,7 @@ dependencies = [
"cargo_metadata", "cargo_metadata",
"clap 3.1.8", "clap 3.1.8",
"log", "log",
"predicates",
"regex", "regex",
"serial_test", "serial_test",
"solana-download-utils", "solana-download-utils",

View File

@ -22,6 +22,7 @@ tar = "0.4.38"
[dev-dependencies] [dev-dependencies]
assert_cmd = "*" assert_cmd = "*"
predicates = "2.0"
serial_test = "*" serial_test = "*"
[features] [features]

View File

@ -564,25 +564,6 @@ fn build_sbf_package(config: &Config, target_directory: &Path, package: &cargo_m
env::set_var("OBJDUMP", llvm_bin.join("llvm-objdump")); env::set_var("OBJDUMP", llvm_bin.join("llvm-objdump"));
env::set_var("OBJCOPY", llvm_bin.join("llvm-objcopy")); env::set_var("OBJCOPY", llvm_bin.join("llvm-objcopy"));
let rustflags = env::var("RUSTFLAGS").ok();
let mut rustflags = Cow::Borrowed(rustflags.as_deref().unwrap_or_default());
if config.remap_cwd {
rustflags = Cow::Owned(format!("{} -Zremap-cwd-prefix=", &rustflags));
}
if config.debug {
// Replace with -Zsplit-debuginfo=packed when stabilized.
rustflags = Cow::Owned(format!("{} -g", &rustflags));
}
if let Cow::Owned(flags) = rustflags {
env::set_var("RUSTFLAGS", &flags);
}
if config.verbose {
debug!(
"RUSTFLAGS=\"{}\"",
env::var("RUSTFLAGS").ok().unwrap_or_default()
);
}
// RUSTC variable overrides cargo +<toolchain> mechanism of // RUSTC variable overrides cargo +<toolchain> mechanism of
// selecting the rust compiler and makes cargo run a rust compiler // selecting the rust compiler and makes cargo run a rust compiler
// other than the one linked in BPF toolchain. We have to prevent // other than the one linked in BPF toolchain. We have to prevent
@ -593,15 +574,40 @@ fn build_sbf_package(config: &Config, target_directory: &Path, package: &cargo_m
); );
env::remove_var("RUSTC") env::remove_var("RUSTC")
} }
let cargo_target = if config.arch == "bpf" {
let mut target_rustflags = env::var("CARGO_TARGET_SBF_SOLANA_SOLANA_RUSTFLAGS") "CARGO_TARGET_BPFEL_UNKNOWN_UNKNOWN_RUSTFLAGS"
.ok() } else {
.unwrap_or_default(); "CARGO_TARGET_SBF_SOLANA_SOLANA_RUSTFLAGS"
};
let rustflags = env::var("RUSTFLAGS").ok().unwrap_or_default();
if env::var("RUSTFLAGS").is_ok() {
warn!(
"Removed RUSTFLAGS from cargo environment, because it overrides {}.",
cargo_target,
);
env::remove_var("RUSTFLAGS")
}
let target_rustflags = env::var(cargo_target).ok();
let mut target_rustflags = Cow::Borrowed(target_rustflags.as_deref().unwrap_or_default());
target_rustflags = Cow::Owned(format!("{} {}", &rustflags, &target_rustflags));
if config.remap_cwd {
target_rustflags = Cow::Owned(format!("{} -Zremap-cwd-prefix=", &target_rustflags));
}
if config.debug {
// Replace with -Zsplit-debuginfo=packed when stabilized.
target_rustflags = Cow::Owned(format!("{} -g", &target_rustflags));
}
if config.arch == "sbfv2" { if config.arch == "sbfv2" {
target_rustflags = format!("{} {}", "-C target_cpu=sbfv2", target_rustflags); target_rustflags = Cow::Owned(format!("{} -C target_cpu=sbfv2", &target_rustflags));
env::set_var( }
"CARGO_TARGET_SBF_SOLANA_SOLANA_RUSTFLAGS", if let Cow::Owned(flags) = target_rustflags {
&target_rustflags, env::set_var(cargo_target, &flags);
}
if config.verbose {
debug!(
"{}=\"{}\"",
cargo_target,
env::var(cargo_target).ok().unwrap_or_default(),
); );
} }

View File

@ -1,4 +1,7 @@
use std::{env, fs, process}; use {
predicates::prelude::*,
std::{env, fs},
};
#[macro_use] #[macro_use]
extern crate serial_test; extern crate serial_test;
@ -47,11 +50,10 @@ fn test_build() {
#[serial] #[serial]
fn test_dump() { fn test_dump() {
// This test requires rustfilt. // This test requires rustfilt.
assert!(process::Command::new("cargo") assert_cmd::Command::new("cargo")
.args(&["install", "-f", "rustfilt"]) .args(&["install", "-f", "rustfilt"])
.status() .assert()
.expect("Unable to install rustfilt required for --dump option") .success();
.success());
run_cargo_build("noop", &["--dump"], false); run_cargo_build("noop", &["--dump"], false);
let cwd = env::current_dir().expect("Unable to get current working directory"); let cwd = env::current_dir().expect("Unable to get current working directory");
let dump = cwd let dump = cwd
@ -90,3 +92,39 @@ fn test_generate_child_script_on_failre() {
fs::remove_file(scr).expect("Failed to remove script"); fs::remove_file(scr).expect("Failed to remove script");
clean_target("fail"); clean_target("fail");
} }
#[test]
#[serial]
fn test_sbfv2() {
run_cargo_build("noop", &["--arch", "sbfv2"], false);
let cwd = env::current_dir().expect("Unable to get current working directory");
let bin = cwd
.join("tests")
.join("crates")
.join("noop")
.join("target")
.join("deploy")
.join("noop.so");
let bin = bin.to_str().unwrap();
let root = cwd
.parent()
.expect("Unable to get parent directory of current working dir")
.parent()
.expect("Unable to get ../.. of current working dir");
let readelf = root
.join("sdk")
.join("bpf")
.join("dependencies")
.join("sbf-tools")
.join("llvm")
.join("bin")
.join("llvm-readelf");
assert_cmd::Command::new(readelf)
.args(&["-h", bin])
.assert()
.stdout(predicate::str::contains(
"Flags: 0x20",
))
.success();
clean_target("noop");
}