From dde4b47e8738579b8e0c0b7e8501b9e70cb89dd8 Mon Sep 17 00:00:00 2001 From: Charles Lanahan Date: Mon, 30 Oct 2023 18:38:21 -0400 Subject: [PATCH] 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. --- src/build/wasm_target.rs | 60 ++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/src/build/wasm_target.rs b/src/build/wasm_target.rs index 8631086f..609669c2 100644 --- a/src/build/wasm_target.rs +++ b/src/build/wasm_target.rs @@ -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 { @@ -80,20 +81,50 @@ fn get_rustc_sysroot() -> Result { } } -/// 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 { + 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 + } } } @@ -101,8 +132,7 @@ fn check_wasm32_target() -> Result { 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,