Skip to content

Commit

Permalink
Rewrite wasm_target to use target-libdir
Browse files Browse the repository at this point in the history
Rewritten wasm_target to use target libdir from the rustc tool rather
than looking through sysroot.  This is to accomodate non-rustup
installations.
  • Loading branch information
daidoji committed Oct 30, 2023
1 parent 7d6501d commit dde4b47
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions src/build/wasm_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use crate::child;
use crate::emoji;
use crate::PBAR;
use anyhow::{anyhow, bail, Context, Result};
use log::error;
use log::info;
use std::fmt;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::process::Command;

struct Wasm32Check {
Expand Down Expand Up @@ -80,29 +81,58 @@ fn get_rustc_sysroot() -> Result<PathBuf> {
}
}

/// Checks if the wasm32-unknown-unknown is present in rustc's sysroot.
fn is_wasm32_target_in_sysroot(sysroot: &Path) -> bool {
let wasm32_target = "wasm32-unknown-unknown";

let rustlib_path = sysroot.join("lib/rustlib");

info!("Looking for {} in {:?}", wasm32_target, rustlib_path);
/// Get wasm32-unknown-unknown target libdir
fn get_rustc_wasm32_unknown_unknown_target_libdir() -> Result<PathBuf> {
let command = Command::new("rustc")
.args(&[
"--target",
"wasm32-unknown-unknown",
"--print",
"target-libdir",
])
.output()?;

if rustlib_path.join(wasm32_target).exists() {
info!("Found {} in {:?}", wasm32_target, rustlib_path);
true
if command.status.success() {
Ok(String::from_utf8(command.stdout)?.trim().into())
} else {
info!("Failed to find {} in {:?}", wasm32_target, rustlib_path);
false
Err(anyhow!(
"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
}
}
}

fn check_wasm32_target() -> Result<Wasm32Check> {
let sysroot = get_rustc_sysroot()?;
let rustc_path = which::which("rustc")?;

// If wasm32-unknown-unknown already exists we're ok.
if is_wasm32_target_in_sysroot(&sysroot) {
if does_wasm32_target_libdir_exist() {
Ok(Wasm32Check {
rustc_path,
sysroot,
Expand Down

0 comments on commit dde4b47

Please sign in to comment.