Rewrite wasm_target to use target-libdir

Rewritten wasm_target to use target libdir from the rustc tool rather
than looking through sysroot.  This is to accomodate non-rustup
installations.
master
Charles Lanahan 2 years ago
parent 7d6501d3cd
commit dde4b47e87
  1. 60
      src/build/wasm_target.rs

@ -4,9 +4,10 @@ use crate::child;
use crate::emoji; use crate::emoji;
use crate::PBAR; use crate::PBAR;
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use log::error;
use log::info; use log::info;
use std::fmt; use std::fmt;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
struct Wasm32Check { struct Wasm32Check {
@ -80,20 +81,50 @@ fn get_rustc_sysroot() -> Result<PathBuf> {
} }
} }
/// Checks if the wasm32-unknown-unknown is present in rustc's sysroot. /// Get wasm32-unknown-unknown target libdir
fn is_wasm32_target_in_sysroot(sysroot: &Path) -> bool { fn get_rustc_wasm32_unknown_unknown_target_libdir() -> Result<PathBuf> {
let wasm32_target = "wasm32-unknown-unknown"; let command = Command::new("rustc")
.args(&[
let rustlib_path = sysroot.join("lib/rustlib"); "--target",
"wasm32-unknown-unknown",
info!("Looking for {} in {:?}", wasm32_target, rustlib_path); "--print",
"target-libdir",
])
.output()?;
if rustlib_path.join(wasm32_target).exists() { if command.status.success() {
info!("Found {} in {:?}", wasm32_target, rustlib_path); Ok(String::from_utf8(command.stdout)?.trim().into())
true
} else { } else {
info!("Failed to find {} in {:?}", wasm32_target, rustlib_path); Err(anyhow!(
false "Getting rustc's wasm32-unknown-unknown target wasn't successful. Got {}",
command.status
))
}
}
fn does_wasm32_target_libdir_exist() -> bool {
let result = get_rustc_wasm32_unknown_unknown_target_libdir();
match result {
Ok(wasm32_target_libdir_path) => {
if wasm32_target_libdir_path.exists() {
info!(
"Found wasm32-unknown-unknown in {:?}",
wasm32_target_libdir_path
);
true
} else {
info!(
"Failed to find wasm32-unknown-unknown in {:?}",
wasm32_target_libdir_path
);
false
}
}
Err(_) => {
error!("Some error in getting the target libdir!");
false
}
} }
} }
@ -101,8 +132,7 @@ fn check_wasm32_target() -> Result<Wasm32Check> {
let sysroot = get_rustc_sysroot()?; let sysroot = get_rustc_sysroot()?;
let rustc_path = which::which("rustc")?; let rustc_path = which::which("rustc")?;
// If wasm32-unknown-unknown already exists we're ok. if does_wasm32_target_libdir_exist() {
if is_wasm32_target_in_sysroot(&sysroot) {
Ok(Wasm32Check { Ok(Wasm32Check {
rustc_path, rustc_path,
sysroot, sysroot,

Loading…
Cancel
Save