Skip to content

Commit

Permalink
chore: decouple cargo-near from rustup
Browse files Browse the repository at this point in the history
  • Loading branch information
dndll committed Apr 12, 2024
1 parent 7a3eaa0 commit 4803218
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
8 changes: 2 additions & 6 deletions cargo-near/src/commands/build_command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use camino::Utf8PathBuf;
use colored::Colorize;
use near_abi::BuildInfo;
use sha2::{Digest, Sha256};
use std::io::BufRead;

use crate::commands::abi_command::abi;
use crate::commands::abi_command::abi::{AbiCompression, AbiFormat, AbiResult};
use crate::common::ColorPreference;
use crate::types::{manifest::CargoManifestPath, metadata::CrateMetadata};
use crate::util;
use crate::{commands::abi_command::abi, util::wasm32_target_libdir_exists};

const COMPILATION_TARGET: &str = "wasm32-unknown-unknown";

Expand All @@ -17,10 +16,7 @@ pub fn run(args: super::BuildCommand) -> color_eyre::eyre::Result<util::Compilat
color.apply();

util::handle_step("Checking the host environment...", || {
if !util::invoke_rustup(["target", "list", "--installed"])?
.lines()
.any(|target| target.as_ref().map_or(false, |t| t == COMPILATION_TARGET))
{
if !wasm32_target_libdir_exists() {
color_eyre::eyre::bail!("rust target `{}` is not installed", COMPILATION_TARGET);
}
Ok(())
Expand Down
66 changes: 65 additions & 1 deletion cargo-near/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::collections::{BTreeMap, HashSet};
use std::ffi::OsStr;
use std::fs;
use std::io::{BufRead, BufReader};
use std::process::Command;
use std::{
collections::{BTreeMap, HashSet},
path::PathBuf,
};
use std::{env, thread};

use camino::{Utf8Path, Utf8PathBuf};
use cargo_metadata::{Artifact, Message};
use color_eyre::eyre::{ContextCompat, WrapErr};
use log::{error, info};

use crate::common::ColorPreference;
use crate::types::manifest::CargoManifestPath;
Expand Down Expand Up @@ -315,3 +319,63 @@ pub(crate) fn extract_abi_entries(
}
Ok(entries)
}

pub(crate) const COMPILATION_TARGET: &str = "wasm32-unknown-unknown";

fn get_rustc_wasm32_unknown_unknown_target_libdir() -> color_eyre::eyre::Result<PathBuf> {
let command = Command::new("rustc")
.args(["--target", COMPILATION_TARGET, "--print", "target-libdir"])
.output()?;

if command.status.success() {
Ok(String::from_utf8(command.stdout)?.trim().into())
} else {
color_eyre::eyre::bail!(
"Getting rustc's wasm32-unknown-unknown target wasn't successful. Got {}",
command.status,
)
}
}

pub fn wasm32_target_libdir_exists() -> 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 {COMPILATION_TARGET} in {:?}",
wasm32_target_libdir_path
);
true
} else {
info!(
"Failed to find {COMPILATION_TARGET} in {:?}",
wasm32_target_libdir_path
);
false
}
}
Err(_) => {
error!("Some error in getting the target libdir, trying rustup..");

invoke_rustup(["target", "list", "--installed"])
.map(|stdout| {
stdout
.lines()
.any(|target| target.as_ref().map_or(false, |t| t == COMPILATION_TARGET))
})
.is_ok()
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_name() {
todo!()
}
}

0 comments on commit 4803218

Please sign in to comment.