mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 12:48:00 +00:00
Make wasm-builder print the rustc version (#7351)
* Make `wasm-builder` print the rustc version This makes `wasm-builder` print the rustc version that is being used to compile the project. This is rather useful, because people can check faster if the used rustc version is maybe known for being broken with Substrate. * Apply suggestions from code review * Add some comments
This commit is contained in:
@@ -15,7 +15,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::fs;
|
||||
use crate::{CargoCommandVersioned, CargoCommand, write_file_if_changed};
|
||||
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use tempfile::tempdir;
|
||||
use ansi_term::Color;
|
||||
@@ -31,33 +33,33 @@ fn print_error_message(message: &str) -> String {
|
||||
|
||||
/// Checks that all prerequisites are installed.
|
||||
///
|
||||
/// # Returns
|
||||
/// Returns `None` if everything was found and `Some(ERR_MSG)` if something could not be found.
|
||||
pub fn check() -> Option<String> {
|
||||
if !check_nightly_installed(){
|
||||
return Some(print_error_message("Rust nightly not installed, please install it!"))
|
||||
/// Returns the versioned cargo command on success.
|
||||
pub(crate) fn check() -> Result<CargoCommandVersioned, String> {
|
||||
let cargo_command = crate::get_nightly_cargo();
|
||||
|
||||
if !cargo_command.is_nightly() {
|
||||
return Err(print_error_message("Rust nightly not installed, please install it!"))
|
||||
}
|
||||
|
||||
check_wasm_toolchain_installed()
|
||||
check_wasm_toolchain_installed(cargo_command)
|
||||
}
|
||||
|
||||
fn check_nightly_installed() -> bool {
|
||||
crate::get_nightly_cargo().is_nightly()
|
||||
}
|
||||
/// 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");
|
||||
|
||||
fn check_wasm_toolchain_installed() -> Option<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");
|
||||
|
||||
let test_file = temp.path().join("src/lib.rs");
|
||||
let manifest_path = temp.path().join("Cargo.toml");
|
||||
|
||||
fs::write(&manifest_path,
|
||||
write_file_if_changed(
|
||||
&manifest_path,
|
||||
r#"
|
||||
[package]
|
||||
name = "wasm-test"
|
||||
version = "1.0.0"
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
|
||||
[lib]
|
||||
name = "wasm_test"
|
||||
@@ -65,27 +67,78 @@ fn check_wasm_toolchain_installed() -> Option<String> {
|
||||
|
||||
[workspace]
|
||||
"#,
|
||||
).expect("Writing wasm-test manifest does not fail; qed");
|
||||
fs::write(&test_file, "pub fn test() {}")
|
||||
.expect("Writing to the test file does not fail; qed");
|
||||
);
|
||||
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 = manifest_path.display().to_string();
|
||||
|
||||
let mut build_cmd = crate::get_nightly_cargo().command();
|
||||
let manifest_path = temp.path().join("Cargo.toml").display().to_string();
|
||||
|
||||
let mut build_cmd = cargo_command.command();
|
||||
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();
|
||||
run_cmd.args(&["run", "--manifest-path", &manifest_path]);
|
||||
|
||||
build_cmd
|
||||
.output()
|
||||
.map_err(|_| err_msg.clone())
|
||||
.and_then(|s|
|
||||
if s.status.success() {
|
||||
Ok(())
|
||||
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") => {
|
||||
@@ -105,5 +158,4 @@ fn check_wasm_toolchain_installed() -> Option<String> {
|
||||
}
|
||||
}
|
||||
)
|
||||
.err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user