Files
pezkuwi-subxt/substrate/utils/wasm-builder/src/prerequisites.rs
T
aj3n 38c3c62588 wasm-builder: manually set CARGO_TARGET_DIR (#1951)
✄
-----------------------------------------------------------------------------

Thank you for your Pull Request! 🙏 Please make sure it follows the
contribution guidelines outlined in
[this
document](https://github.com/paritytech/polkadot-sdk/blob/master/docs/CONTRIBUTING.md)
and fill
out the sections below. Once you're ready to submit your PR for review,
please
delete this section and leave only the text under the "Description"
heading.

# Description

*Please include a summary of the changes and the related issue. Please
also include relevant motivation and context,
including:*

- What does this PR do?

make 'substrate-wasm-builder' manually set 'CARGO_TARGET_DIR' to
'$project_dir/target' while building instead of unset
'CARGO_TARGET_DIR';

- Why are these changes needed?

If you using this in the `build.rs` with following content in your
`~/.cargo/config.toml':

    [build]
    target-dir = "target"

the build process will stuck because of dead lock -- two `cargo build`
on same target directory in the same time.
There is already an attempt to avoid such dead lock by unset the
`CARGO_TARGET_DIR`, but for users with config above in his build
enviroment (like me), this workaround won't work.

- How were these changes implemented and what do they affect?

Instead of unset 'CARGO_TARGET_DIR', we set 'CARGO_TARGET_DIR' to
'$project/target/', which is already assumed to be true by rest of the
code.

*Use [Github semantic

linking](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword)
to address any open issues this PR relates to or closes.*

Fixes # (issue number, *if applicable*)

Closes # (issue number, *if applicable*)

# Checklist

- [x] My PR includes a detailed description as outlined in the
"Description" section above
- [ ] My PR follows the [labeling requirements](CONTRIBUTING.md#Process)
of this project (at minimum one label for `T`
  required)
- [ ] I have made corresponding changes to the documentation (if
applicable)
- [ ] I have added tests that prove my fix is effective or that my
feature works (if applicable)

You can remove the "Checklist" section once all have been checked. Thank
you for your contribution!

✄
-----------------------------------------------------------------------------

I have built my project with this fix, there's still some warnings with
`build.target-dir` set but the building process won't hang.
I haven't found related issue in this repo. But I did find one issue
[here](https://github.com/substrate-developer-hub/substrate-node-template/issues/116).
2023-10-23 11:54:06 +02:00

182 lines
5.9 KiB
Rust

// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{write_file_if_changed, CargoCommand, CargoCommandVersioned};
use std::{fs, path::Path};
use ansi_term::Color;
use tempfile::tempdir;
/// Print an error message.
fn print_error_message(message: &str) -> String {
if super::color_output_enabled() {
Color::Red.bold().paint(message).to_string()
} else {
message.into()
}
}
/// Checks that all prerequisites are installed.
///
/// Returns the versioned cargo command on success.
pub(crate) fn check() -> Result<CargoCommandVersioned, String> {
let cargo_command = crate::get_cargo_command();
if !cargo_command.supports_substrate_wasm_env() {
return Err(print_error_message(
"Cannot compile the WASM runtime: no compatible Rust compiler found!\n\
Install at least Rust 1.68.0 or a recent nightly version.",
))
}
check_wasm_toolchain_installed(cargo_command)
}
/// Create the project that will be used to check that the wasm toolchain is installed and to
/// extract the rustc version.
fn create_check_toolchain_project(project_dir: &Path) {
let lib_rs_file = project_dir.join("src/lib.rs");
let main_rs_file = project_dir.join("src/main.rs");
let build_rs_file = project_dir.join("build.rs");
let manifest_path = project_dir.join("Cargo.toml");
write_file_if_changed(
&manifest_path,
r#"
[package]
name = "wasm-test"
version = "1.0.0"
edition = "2021"
build = "build.rs"
[lib]
name = "wasm_test"
crate-type = ["cdylib"]
[workspace]
"#,
);
write_file_if_changed(lib_rs_file, "pub fn test() {}");
// We want to know the rustc version of the rustc that is being used by our cargo command.
// The cargo command is determined by some *very* complex algorithm to find the cargo command
// that supports nightly.
// The best solution would be if there is a `cargo rustc --version` command, which sadly
// doesn't exists. So, the only available way of getting the rustc version is to build a project
// and capture the rustc version in this build process. This `build.rs` is exactly doing this.
// It gets the rustc version by calling `rustc --version` and exposing it in the `RUSTC_VERSION`
// environment variable.
write_file_if_changed(
build_rs_file,
r#"
fn main() {
let rustc_cmd = std::env::var("RUSTC").ok().unwrap_or_else(|| "rustc".into());
let rustc_version = std::process::Command::new(rustc_cmd)
.arg("--version")
.output()
.ok()
.and_then(|o| String::from_utf8(o.stdout).ok());
println!(
"cargo:rustc-env=RUSTC_VERSION={}",
rustc_version.unwrap_or_else(|| "unknown rustc version".into()),
);
}
"#,
);
// Just prints the `RURSTC_VERSION` environment variable that is being created by the
// `build.rs` script.
write_file_if_changed(
main_rs_file,
r#"
fn main() {
println!("{}", env!("RUSTC_VERSION"));
}
"#,
);
}
fn check_wasm_toolchain_installed(
cargo_command: CargoCommand,
) -> Result<CargoCommandVersioned, String> {
let temp = tempdir().expect("Creating temp dir does not fail; qed");
fs::create_dir_all(temp.path().join("src")).expect("Creating src dir does not fail; qed");
create_check_toolchain_project(temp.path());
let err_msg = print_error_message("Rust WASM toolchain not installed, please install it!");
let manifest_path = temp.path().join("Cargo.toml").display().to_string();
let mut build_cmd = cargo_command.command();
// Chdir to temp to avoid including project's .cargo/config.toml
// by accident - it can happen in some CI environments.
build_cmd.current_dir(&temp);
build_cmd.args(&[
"build",
"--target=wasm32-unknown-unknown",
"--manifest-path",
&manifest_path,
]);
if super::color_output_enabled() {
build_cmd.arg("--color=always");
}
let mut run_cmd = cargo_command.command();
// Chdir to temp to avoid including project's .cargo/config.toml
// by accident - it can happen in some CI environments.
run_cmd.current_dir(&temp);
run_cmd.args(&["run", "--manifest-path", &manifest_path]);
// manually set the `CARGO_TARGET_DIR` to prevent a cargo deadlock
let target_dir = temp.path().join("target").display().to_string();
build_cmd.env("CARGO_TARGET_DIR", &target_dir);
run_cmd.env("CARGO_TARGET_DIR", &target_dir);
// Make sure the host's flags aren't used here, e.g. if an alternative linker is specified
// in the RUSTFLAGS then the check we do here will break unless we clear these.
build_cmd.env_remove("CARGO_ENCODED_RUSTFLAGS");
run_cmd.env_remove("CARGO_ENCODED_RUSTFLAGS");
build_cmd.env_remove("RUSTFLAGS");
run_cmd.env_remove("RUSTFLAGS");
build_cmd.output().map_err(|_| err_msg.clone()).and_then(|s| {
if s.status.success() {
let version = run_cmd.output().ok().and_then(|o| String::from_utf8(o.stdout).ok());
Ok(CargoCommandVersioned::new(
cargo_command,
version.unwrap_or_else(|| "unknown rustc version".into()),
))
} else {
match String::from_utf8(s.stderr) {
Ok(ref err) if err.contains("linker `rust-lld` not found") =>
Err(print_error_message("`rust-lld` not found, please install it!")),
Ok(ref err) => Err(format!(
"{}\n\n{}\n{}\n{}{}\n",
err_msg,
Color::Yellow.bold().paint("Further error information:"),
Color::Yellow.bold().paint("-".repeat(60)),
err,
Color::Yellow.bold().paint("-".repeat(60)),
)),
Err(_) => Err(err_msg),
}
}
})
}