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

fix: prevent build-time deadlock when building tests in release mode #4332

Merged
merged 1 commit into from
May 28, 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: 20 additions & 2 deletions runtime/near-test-contracts/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::{fs, io, process};
use std::{env, fs, io, process};

fn main() {
if let Err(err) = try_main() {
Expand Down Expand Up @@ -29,8 +31,9 @@ fn build_contract(dir: &str, args: &[&str], output: &str) -> io::Result<()> {
cmd.current_dir(dir);
check_status(cmd)?;

let target_dir = shared_target_dir().unwrap_or_else(|| format!("./{}/target", dir).into());
fs::copy(
format!("./{}/target/wasm32-unknown-unknown/release/{}.wasm", dir, dir.replace('-', "_")),
target_dir.join(format!("wasm32-unknown-unknown/release/{}.wasm", dir.replace('-', "_"))),
format!("./res/{}.wasm", output),
)?;
println!("cargo:rerun-if-changed=./{}/src/lib.rs", dir);
Expand All @@ -41,6 +44,10 @@ fn build_contract(dir: &str, args: &[&str], output: &str) -> io::Result<()> {
fn cargo_build_cmd() -> Command {
let mut res = Command::new("cargo");
res.env("RUSTFLAGS", "-C link-arg=-s");
if let Some(target_dir) = shared_target_dir() {
res.env("CARGO_TARGET_DIR", target_dir);
}

res.args(&["build", "--target=wasm32-unknown-unknown", "--release"]);
res
}
Expand All @@ -57,3 +64,14 @@ fn check_status(mut cmd: Command) -> io::Result<()> {
}
Ok(())
}

fn shared_target_dir() -> Option<PathBuf> {
let target_dir = env::var("CARGO_TARGET_DIR").ok()?;
// Avoid sharing the same target directory with the patent Cargo
// invocation, to avoid deadlock on the target dir.
//
// That is, this logic is needed for the case like the following:
//
// CARGO_TARGET_DIR=/tmp cargo build -p near-test-contracts --release
Some(Path::new(&target_dir).join("near-test-contracts"))
}