From 30d9e2bd0746b9ecb1129915e205ea710981bf98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-=C3=89tienne=20Meunier?= Date: Tue, 20 Nov 2018 19:20:55 +0000 Subject: [PATCH 1/5] Fixing wasm-pack on non-rustup setups --- Cargo.toml | 1 + src/build.rs | 23 +++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 25 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index b486071b..568327c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ tar = "0.4.16" toml = "0.4" which = "2.0.0" zip = "0.4.2" +tempfile = "3.0" [dev-dependencies] tempfile = "3" diff --git a/src/build.rs b/src/build.rs index 4628d37c..056a37c6 100644 --- a/src/build.rs +++ b/src/build.rs @@ -6,9 +6,12 @@ use emoji; use failure::{Error, ResultExt}; use progressbar::Step; use slog::Logger; +use std::io::Write; use std::path::Path; use std::process::Command; +use std::fs::File; use std::str; +use tempfile; use PBAR; /// Ensure that `rustc` is present and that it is >= 1.30.0 @@ -55,6 +58,26 @@ fn rustc_minor_version() -> Option { pub fn rustup_add_wasm_target(log: &Logger, step: &Step) -> Result<(), Error> { let msg = format!("{}Adding WASM target...", emoji::TARGET); PBAR.step(step, &msg); + + // Checking wether we can already compile to wasm with the rustc + // we have in scope. + let dir = tempfile::TempDir::new()?; + let p = dir.path().join("main.rs"); + { + let mut f = File::create(&p)?; + writeln!(f, "fn main(){{}}")?; + } + match Command::new("rustc") + .arg("--target") + .arg("wasm32-unknown-unknown") + .arg(p.to_str().unwrap()) + .status() + { + Ok(ref e) if e.success() => return Ok(()), + _ => {} + } + + // If not, using rustup to add it. let mut cmd = Command::new("rustup"); cmd.arg("target").arg("add").arg("wasm32-unknown-unknown"); child::run(log, cmd, "rustup") diff --git a/src/lib.rs b/src/lib.rs index 2cec7a83..266408b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,7 @@ extern crate slog; extern crate slog_async; extern crate slog_term; extern crate tar; +extern crate tempfile; extern crate toml; extern crate which; extern crate zip; From b92a3366eb31f75359b33b0cc4af8e6aa324f974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-=C3=89tienne=20Meunier?= Date: Tue, 20 Nov 2018 19:21:54 +0000 Subject: [PATCH 2/5] Rustfmt --- src/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build.rs b/src/build.rs index 056a37c6..3a0da67b 100644 --- a/src/build.rs +++ b/src/build.rs @@ -6,10 +6,10 @@ use emoji; use failure::{Error, ResultExt}; use progressbar::Step; use slog::Logger; +use std::fs::File; use std::io::Write; use std::path::Path; use std::process::Command; -use std::fs::File; use std::str; use tempfile; use PBAR; From 4803a201c8993423514049d9555f35a262b68d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Fri, 22 Feb 2019 21:37:54 +0100 Subject: [PATCH 3/5] feat: Check for wasm32-unknown-unknown in sysroot First check if wasm32-unknown-unknown is present in rustc's sysroot and if it is, then we're fine. Otherwise check if we're using rustup and add the wasm32 target and if we're not using rustup, then bail with an error stating that the wasm32 target couldn't be found. --- Cargo.toml | 1 - src/build.rs | 102 +++++++++++++++++++++++++++++++++---------- src/command/build.rs | 10 ++--- src/command/test.rs | 8 ++-- src/lib.rs | 1 - 5 files changed, 87 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 11e3f0f4..a0315885 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,6 @@ siphasher = "0.2.3" structopt = "0.2" toml = "0.4" which = "2.0.0" -tempfile = "3.0" binary-install = { version = "0.0.1", path = "./binary-install" } walkdir = "2" diff --git a/src/build.rs b/src/build.rs index 804892bb..183102bc 100644 --- a/src/build.rs +++ b/src/build.rs @@ -4,11 +4,12 @@ use child; use command::build::BuildProfile; use emoji; use failure::{Error, ResultExt}; +use log::info; use progressbar::Step; use std::path::Path; -use std::process::Command; +use std::process::{Command, Stdio}; use std::str; -use tempfile; +use std::string::FromUtf8Error; use PBAR; /// Ensure that `rustc` is present and that it is >= 1.30.0 @@ -50,35 +51,88 @@ fn rustc_minor_version() -> Option { otry!(pieces.next()).parse().ok() } -/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for -/// current toolchain -pub fn rustup_add_wasm_target(step: &Step) -> Result<(), Error> { - let msg = format!("{}Adding WASM target...", emoji::TARGET); - PBAR.step(step, &msg); +/// Get rustc's sysroot as a String +fn get_rustc_sysroot() -> Result { + String::from_utf8( + Command::new("rustc") + .args(&["--print", "sysroot"]) + .stdout(Stdio::piped()) + .output() + .unwrap() + .stdout, + ) +} + +/// Checks if the wasm32-unknown-unknown is present in rustc's sysroot. +fn is_wasm32_target_in_sysroot(sysroot: &str) -> Result { + let wasm32_target = "wasm32-unknown-unknown"; + + info!("Looking for {} in {}", wasm32_target, sysroot); + + let rustlib_path = &format!("{}/lib/rustlib", sysroot.replace("\n", "")); - // Checking wether we can already compile to wasm with the rustc - // we have in scope. - let dir = tempfile::TempDir::new()?; - let p = dir.path().join("main.rs"); - { - let mut f = File::create(&p)?; - writeln!(f, "fn main(){{}}")?; + let ls_command = Command::new("ls") + .arg(rustlib_path) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; + + let grep_command_status = Command::new("grep") + .args(&["-x", wasm32_target]) + .stdin(ls_command.stdout.unwrap()) + .status(); + + match grep_command_status { + Ok(status) if status.success() => { + info!("Found {} in {}", wasm32_target, sysroot); + Ok(true) + } + _ => { + info!("Failed to find {} in {}", wasm32_target, sysroot); + Ok(false) + } } - match Command::new("rustc") - .arg("--target") - .arg("wasm32-unknown-unknown") - .arg(p.to_str().unwrap()) - .status() - { - Ok(ref e) if e.success() => return Ok(()), - _ => {} +} + +fn check_wasm32_target() -> Result { + let sysroot = get_rustc_sysroot()?; + + // If wasm32-unknown-unknown already exists we're ok. + match is_wasm32_target_in_sysroot(&sysroot) { + Ok(true) => Ok(true), + // If it doesn't exist, then we need to check if we're using rustup. + _ => { + // If sysroot contains .rustup, then we can assume we're using rustup + // and use rustup to add the wasm32-unknown-unknown target. + if sysroot.contains(".rustup") { + rustup_add_wasm_target() + } else { + Ok(false) + } + } } +} - // If not, using rustup to add it. +/// Add wasm32-unknown-unknown using `rustup`. +fn rustup_add_wasm_target() -> Result { let mut cmd = Command::new("rustup"); cmd.arg("target").arg("add").arg("wasm32-unknown-unknown"); child::run(cmd, "rustup").context("Adding the wasm32-unknown-unknown target with rustup")?; - Ok(()) + + Ok(true) +} + +/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for +/// current toolchain +pub fn check_for_wasm32_target(step: &Step) -> Result<(), Error> { + let msg = format!("{}Checking for WASM target...", emoji::TARGET); + PBAR.step(step, &msg); + + // Check if wasm32 target is present, otherwise bail. + match check_wasm32_target() { + Ok(true) => Ok(()), + _ => bail!("wasm32-unknown-unknown target not found!"), + } } /// Run `cargo build` targetting `wasm32-unknown-unknown`. diff --git a/src/command/build.rs b/src/command/build.rs index e6830301..fd0370a1 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -236,7 +236,7 @@ impl Build { BuildMode::Normal => steps![ step_check_rustc_version, step_check_crate_config, - step_add_wasm_target, + step_check_for_wasm_target, step_build_wasm, step_create_dir, step_copy_readme, @@ -281,10 +281,10 @@ impl Build { Ok(()) } - fn step_add_wasm_target(&mut self, step: &Step) -> Result<(), Error> { - info!("Adding wasm-target..."); - build::rustup_add_wasm_target(step)?; - info!("Adding wasm-target was successful."); + fn step_check_for_wasm_target(&mut self, step: &Step) -> Result<(), Error> { + info!("Checking for wasm-target..."); + build::check_for_wasm32_target(step)?; + info!("Checking for wasm-target was successful."); Ok(()) } diff --git a/src/command/test.rs b/src/command/test.rs index f07d4a9f..7f6f398f 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -196,7 +196,7 @@ impl Test { match self.mode { BuildMode::Normal => steps![ step_check_rustc_version, - step_add_wasm_target, + step_check_for_wasm_target, step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, @@ -208,7 +208,7 @@ impl Test { step_test_safari if self.safari, ], BuildMode::Force => steps![ - step_add_wasm_target, + step_check_for_wasm_target, step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, @@ -240,9 +240,9 @@ impl Test { Ok(()) } - fn step_add_wasm_target(&mut self, step: &Step) -> Result<(), Error> { + fn step_check_for_wasm_target(&mut self, step: &Step) -> Result<(), Error> { info!("Adding wasm-target..."); - build::rustup_add_wasm_target(step)?; + build::check_for_wasm32_target(step)?; info!("Adding wasm-target was successful."); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 5c4809a3..97b136fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,6 @@ extern crate structopt; extern crate binary_install; extern crate dialoguer; extern crate log; -extern crate tempfile; extern crate toml; extern crate walkdir; From 4b9fd878dcd5d5456f894f9f17136485e0fe5d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Mon, 11 Mar 2019 21:25:46 +0100 Subject: [PATCH 4/5] refactor: Use path.exists() instead of using `ls` and `grep`, stop unwraping --- src/build.rs | 89 +++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/src/build.rs b/src/build.rs index 33e7ecf9..7552a0a2 100644 --- a/src/build.rs +++ b/src/build.rs @@ -6,10 +6,9 @@ use emoji; use failure::{Error, ResultExt}; use log::info; use progressbar::Step; -use std::path::Path; -use std::process::{Command, Stdio}; +use std::path::{Path, PathBuf}; +use std::process::Command; use std::str; -use std::string::FromUtf8Error; use PBAR; /// Ensure that `rustc` is present and that it is >= 1.30.0 @@ -51,46 +50,36 @@ fn rustc_minor_version() -> Option { otry!(pieces.next()).parse().ok() } -/// Get rustc's sysroot as a String -fn get_rustc_sysroot() -> Result { - String::from_utf8( - Command::new("rustc") - .args(&["--print", "sysroot"]) - .stdout(Stdio::piped()) - .output() - .unwrap() - .stdout, - ) +/// Get rustc's sysroot as a PathBuf +fn get_rustc_sysroot() -> Result { + let command = Command::new("rustc") + .args(&["--print", "sysroot"]) + .output()?; + + if command.status.success() { + Ok(String::from_utf8(command.stdout)?.trim().into()) + } else { + Err(format_err!( + "Getting rustc's sysroot wasn't successful. Got {}", + command.status + )) + } } /// Checks if the wasm32-unknown-unknown is present in rustc's sysroot. -fn is_wasm32_target_in_sysroot(sysroot: &str) -> Result { +fn is_wasm32_target_in_sysroot(sysroot: &PathBuf) -> bool { let wasm32_target = "wasm32-unknown-unknown"; - info!("Looking for {} in {}", wasm32_target, sysroot); - - let rustlib_path = &format!("{}/lib/rustlib", sysroot.replace("\n", "")); - - let ls_command = Command::new("ls") - .arg(rustlib_path) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; + let rustlib_path = sysroot.join("lib/rustlib"); - let grep_command_status = Command::new("grep") - .args(&["-x", wasm32_target]) - .stdin(ls_command.stdout.unwrap()) - .status(); + info!("Looking for {} in {:?}", wasm32_target, rustlib_path); - match grep_command_status { - Ok(status) if status.success() => { - info!("Found {} in {}", wasm32_target, sysroot); - Ok(true) - } - _ => { - info!("Failed to find {} in {}", wasm32_target, sysroot); - Ok(false) - } + if rustlib_path.join(wasm32_target).exists() { + info!("Found {} in {:?}", wasm32_target, rustlib_path); + true + } else { + info!("Failed to find {} in {:?}", wasm32_target, rustlib_path); + false } } @@ -98,28 +87,27 @@ fn check_wasm32_target() -> Result { let sysroot = get_rustc_sysroot()?; // If wasm32-unknown-unknown already exists we're ok. - match is_wasm32_target_in_sysroot(&sysroot) { - Ok(true) => Ok(true), - // If it doesn't exist, then we need to check if we're using rustup. - _ => { - // If sysroot contains .rustup, then we can assume we're using rustup - // and use rustup to add the wasm32-unknown-unknown target. - if sysroot.contains(".rustup") { - rustup_add_wasm_target() - } else { - Ok(false) - } + if is_wasm32_target_in_sysroot(&sysroot) { + Ok(true) + // If it doesn't exist, then we need to check if we're using rustup. + } else { + // If sysroot contains .rustup, then we can assume we're using rustup + // and use rustup to add the wasm32-unknown-unknown target. + if sysroot.to_string_lossy().contains(".rustup") { + rustup_add_wasm_target().map(|()| true) + } else { + Ok(false) } } } /// Add wasm32-unknown-unknown using `rustup`. -fn rustup_add_wasm_target() -> Result { +fn rustup_add_wasm_target() -> Result<(), Error> { let mut cmd = Command::new("rustup"); cmd.arg("target").arg("add").arg("wasm32-unknown-unknown"); child::run(cmd, "rustup").context("Adding the wasm32-unknown-unknown target with rustup")?; - Ok(true) + Ok(()) } /// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for @@ -131,7 +119,8 @@ pub fn check_for_wasm32_target(step: &Step) -> Result<(), Error> { // Check if wasm32 target is present, otherwise bail. match check_wasm32_target() { Ok(true) => Ok(()), - _ => bail!("wasm32-unknown-unknown target not found!"), + Ok(false) => bail!("wasm32-unknown-unknown target not found!"), + Err(err) => Err(err), } } From a0af7b974a68fa0bb78955621184f4b069bce921 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Fri, 15 Mar 2019 18:53:26 -0500 Subject: [PATCH 5/5] fix: merge --- src/build.rs | 4 ++-- src/command/build.rs | 2 +- src/command/test.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/build.rs b/src/build.rs index e841370c..2b32b674 100644 --- a/src/build.rs +++ b/src/build.rs @@ -109,9 +109,9 @@ fn rustup_add_wasm_target() -> Result<(), Error> { /// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for /// current toolchain -pub fn check_for_wasm32_target(step: &Step) -> Result<(), Error> { +pub fn check_for_wasm32_target() -> Result<(), Error> { let msg = format!("{}Checking for the Wasm target...", emoji::TARGET); - PBAR.step(step, &msg); + PBAR.info(&msg); // Check if wasm32 target is present, otherwise bail. match check_wasm32_target() { diff --git a/src/command/build.rs b/src/command/build.rs index 6e91de50..924ef8e6 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -310,7 +310,7 @@ impl Build { fn step_check_for_wasm_target(&mut self) -> Result<(), Error> { info!("Checking for wasm-target..."); - build::check_for_wasm32_target(step)?; + build::check_for_wasm32_target()?; info!("Checking for wasm-target was successful."); Ok(()) } diff --git a/src/command/test.rs b/src/command/test.rs index a0482f0c..67436aad 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -236,7 +236,7 @@ impl Test { fn step_check_for_wasm_target(&mut self) -> Result<(), Error> { info!("Adding wasm-target..."); - build::check_for_wasm32_target(step)?; + build::check_for_wasm32_target()?; info!("Adding wasm-target was successful."); Ok(()) }