multiversx_sc_meta_lib/tools/
build_target.rs1use colored::Colorize;
2use rustc_version::{version_meta, Version};
3use std::{
4 env,
5 ffi::OsString,
6 process::{exit, Command},
7};
8
9use crate::{
10 contract::sc_config::execute_command::execute_command, print_util, tools::RustcVersion,
11};
12
13pub const WASM32_TARGET: &str = "wasm32-unknown-unknown";
14pub const WASM32V1_TARGET: &str = "wasm32v1-none";
15const FIRST_RUSTC_VERSION_WITH_WASM32V1_TARGET: Version = Version::new(1, 85, 0);
16
17pub fn default_target() -> &'static str {
21 if is_wasm32v1_available() {
22 WASM32V1_TARGET
23 } else {
24 WASM32_TARGET
25 }
26}
27
28pub fn is_wasm32v1_available() -> bool {
29 let Ok(version) = version_meta() else {
30 return false;
31 };
32
33 version.semver >= FIRST_RUSTC_VERSION_WITH_WASM32V1_TARGET
34}
35
36fn rustup_command() -> Command {
37 let rustup = env::var_os("RUSTUP").unwrap_or_else(|| OsString::from("rustup"));
38 Command::new(rustup)
39}
40
41pub fn is_target_installed(rustc_version: &RustcVersion, target_name: &str) -> bool {
42 let mut cmd = rustup_command();
43 cmd.arg(rustc_version.to_cli_arg())
44 .arg("target")
45 .arg("list")
46 .arg("--installed");
47
48 print_util::print_rustup_check_target(rustc_version, target_name, &cmd);
49
50 let output_rustup_command = execute_command(&mut cmd, "rustup");
51 let str_output_rustup = match output_rustup_command {
52 Ok(output) => output,
53 Err(err) => {
54 println!("\n{}", err.to_string().red().bold());
55 exit(1);
56 }
57 };
58
59 let installed = str_output_rustup.contains(target_name);
60
61 print_util::print_rustup_check_target_result(installed);
62
63 installed
64}
65
66pub fn install_target(rustc_version: Option<&RustcVersion>, target_name: &str) {
67 let mut cmd = rustup_command();
68 if let Some(rustc_version) = rustc_version {
69 cmd.arg(rustc_version.to_cli_arg());
70 }
71 cmd.arg("target").arg("add").arg(target_name);
72
73 print_util::print_rustup_install_target(target_name, &cmd);
74
75 let exit_status = cmd.status().expect("failed to execute `rustup`");
76
77 assert!(
78 exit_status.success(),
79 "failed to install {target_name} target"
80 );
81
82 print_util::print_rustup_install_target_success(target_name);
83}