mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
xtask: support installing kernel toolchain
Now that the kernel toolchain can be different, support installing it as well. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
5d0180cd35
commit
2f0315771f
3 changed files with 132 additions and 83 deletions
|
|
@ -637,6 +637,7 @@ impl Builder {
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::utils::ensure_compiler(&self.target.as_ref().map(|s| s.as_str()), false, false)?;
|
crate::utils::ensure_compiler(&self.target.as_ref().map(|s| s.as_str()), false, false)?;
|
||||||
|
crate::utils::ensure_kernel_compiler(&self.target_kernel.as_ref().map(|s| s.as_str()), false)?;
|
||||||
self.locale_override(); // apply the locale override
|
self.locale_override(); // apply the locale override
|
||||||
|
|
||||||
// ------ build the services & apps ------
|
// ------ build the services & apps ------
|
||||||
|
|
|
||||||
|
|
@ -166,7 +166,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
match task.as_deref() {
|
match task.as_deref() {
|
||||||
Some("install-toolkit") | Some("install-toolchain") => {
|
Some("install-toolkit") | Some("install-toolchain") => {
|
||||||
let arg = env::args().nth(2);
|
let arg = env::args().nth(2);
|
||||||
ensure_compiler(&Some(TARGET_TRIPLE_RISCV32), true, arg.map(|x| x == "--force").unwrap_or(false))?
|
ensure_compiler(
|
||||||
|
&Some(TARGET_TRIPLE_RISCV32),
|
||||||
|
true,
|
||||||
|
arg.map(|x| x == "--force").unwrap_or(false),
|
||||||
|
)?;
|
||||||
|
ensure_kernel_compiler(&Some(TARGET_TRIPLE_RISCV32_KERNEL), true)?;
|
||||||
}
|
}
|
||||||
// ----- renode configs --------
|
// ----- renode configs --------
|
||||||
Some("renode-image") => {
|
Some("renode-image") => {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use std::{
|
||||||
fs::{File, OpenOptions},
|
fs::{File, OpenOptions},
|
||||||
io::{Read, Write},
|
io::{Read, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::Command,
|
process::{Command, Stdio},
|
||||||
};
|
};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
@ -25,49 +25,60 @@ lazy_static! {
|
||||||
/// cache it inside an atomic boolean. If this is `true` then
|
/// cache it inside an atomic boolean. If this is `true` then
|
||||||
/// it means we can assume the check passed already.
|
/// it means we can assume the check passed already.
|
||||||
static DONE_COMPILER_CHECK: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
static DONE_COMPILER_CHECK: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||||
|
static DONE_KERNEL_COMPILER_CHECK: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||||
|
|
||||||
/// Ensure we have a compatible compiler toolchain. We use a new Target,
|
fn ask_to_install() -> bool {
|
||||||
/// and we want to give the user a friendly way of installing the latest
|
let mut buffer = String::new();
|
||||||
/// Rust toolchain.
|
let stdin = std::io::stdin();
|
||||||
pub(crate) fn ensure_compiler(
|
let mut stdout = std::io::stdout();
|
||||||
target: &Option<&str>,
|
loop {
|
||||||
force_install: bool,
|
stdout.flush().unwrap();
|
||||||
remove_existing: bool,
|
buffer.clear();
|
||||||
) -> Result<(), String> {
|
stdin.read_line(&mut buffer).unwrap();
|
||||||
use std::process::Stdio;
|
|
||||||
if DONE_COMPILER_CHECK.load(std::sync::atomic::Ordering::SeqCst) {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the sysroot for the given target. If the target does not exist,
|
let trimmed = buffer.trim();
|
||||||
/// return None.
|
|
||||||
fn get_sysroot(target: Option<&str>) -> Result<Option<String>, String> {
|
if trimmed == "n" || trimmed == "N" {
|
||||||
let mut args = vec!["--print", "sysroot"];
|
return false;
|
||||||
if let Some(target) = target {
|
|
||||||
args.push("--target");
|
|
||||||
args.push(target);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let sysroot_cmd = Command::new("rustc")
|
if trimmed == "y" || trimmed == "Y" || trimmed.is_empty() {
|
||||||
.stderr(Stdio::null())
|
return true;
|
||||||
.stdout(Stdio::piped())
|
}
|
||||||
.args(&args)
|
println!();
|
||||||
.spawn()
|
println!("Please answer 'y' or 'n'");
|
||||||
.expect("could not run rustc");
|
}
|
||||||
let sysroot_output = sysroot_cmd.wait_with_output().unwrap();
|
}
|
||||||
let have_toolchain = sysroot_output.status.success();
|
/// Return the sysroot for the given target. If the target does not exist,
|
||||||
|
/// return None.
|
||||||
|
fn get_sysroot(target: Option<&str>, check_version: bool) -> Result<Option<String>, String> {
|
||||||
|
let mut args = vec!["--print", "sysroot"];
|
||||||
|
if let Some(target) = target {
|
||||||
|
args.push("--target");
|
||||||
|
args.push(target);
|
||||||
|
}
|
||||||
|
|
||||||
let toolchain_path = String::from_utf8(sysroot_output.stdout)
|
let sysroot_cmd = Command::new("rustc")
|
||||||
.map_err(|_| "Unable to find Rust sysroot".to_owned())?
|
.stderr(Stdio::null())
|
||||||
.trim()
|
.stdout(Stdio::piped())
|
||||||
.to_owned();
|
.args(&args)
|
||||||
|
.spawn()
|
||||||
|
.expect("could not run rustc");
|
||||||
|
let sysroot_output = sysroot_cmd.wait_with_output().unwrap();
|
||||||
|
let have_toolchain = sysroot_output.status.success();
|
||||||
|
|
||||||
// Look for the "RUST_VERSION" file to ensure it's compatible with this version.
|
let toolchain_path = String::from_utf8(sysroot_output.stdout)
|
||||||
if let Some(target) = target {
|
.map_err(|_| "Unable to find Rust sysroot".to_owned())?
|
||||||
let mut version_path = PathBuf::from(&toolchain_path);
|
.trim()
|
||||||
version_path.push("lib");
|
.to_owned();
|
||||||
version_path.push("rustlib");
|
|
||||||
version_path.push(target);
|
// Look for the "RUST_VERSION" file to ensure it's compatible with this version.
|
||||||
|
if let Some(target) = target {
|
||||||
|
let mut version_path = PathBuf::from(&toolchain_path);
|
||||||
|
version_path.push("lib");
|
||||||
|
version_path.push("rustlib");
|
||||||
|
version_path.push(target);
|
||||||
|
if check_version {
|
||||||
version_path.push("RUST_VERSION");
|
version_path.push("RUST_VERSION");
|
||||||
if let Ok(mut vp) = File::open(&version_path) {
|
if let Ok(mut vp) = File::open(&version_path) {
|
||||||
let mut version_str = String::new();
|
let mut version_str = String::new();
|
||||||
|
|
@ -91,14 +102,81 @@ pub(crate) fn ensure_compiler(
|
||||||
// return Err("Outdated toolchain installed".to_owned());
|
// return Err("Outdated toolchain installed".to_owned());
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if !std::path::Path::new(&version_path).exists() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if have_toolchain { Ok(Some(toolchain_path)) } else { Ok(None) }
|
if have_toolchain { Ok(Some(toolchain_path)) } else { Ok(None) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn ensure_kernel_compiler(target: &Option<&str>, force_install: bool) -> Result<(), String> {
|
||||||
|
if DONE_KERNEL_COMPILER_CHECK.load(std::sync::atomic::Ordering::SeqCst) {
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the sysroot exists, then we're good.
|
// If the sysroot exists, then we're good.
|
||||||
let target = target.unwrap_or(TARGET_TRIPLE_RISCV32);
|
let target = target.unwrap_or(TARGET_TRIPLE_RISCV32);
|
||||||
if let Some(path) = get_sysroot(Some(target))? {
|
if get_sysroot(Some(target), false)?.is_some() {
|
||||||
|
DONE_COMPILER_CHECK.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the terminal is a tty, or if toolchain installation is forced,
|
||||||
|
// download the latest toolchain.
|
||||||
|
if !atty::is(atty::Stream::Stdin) && !force_install {
|
||||||
|
return Err(format!("Toolchain for {} not found", target));
|
||||||
|
}
|
||||||
|
|
||||||
|
if force_install {
|
||||||
|
println!("Downloading toolchain");
|
||||||
|
} else {
|
||||||
|
println!("Error: {} target was not found on this system!", target);
|
||||||
|
print!("Would you like this program to attempt to download and install it using rustup? [Y/n] ");
|
||||||
|
if ask_to_install() {
|
||||||
|
println!("Installing toolchain for {} via rustup", target);
|
||||||
|
} else {
|
||||||
|
return Err(format!("Please install the {} toolchain", target));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let rustup_command = Command::new("rustup")
|
||||||
|
.stderr(Stdio::null())
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.args(&["target", "add", target])
|
||||||
|
.spawn()
|
||||||
|
.map_err(|e| format!("could not run rustup: {}", e))?;
|
||||||
|
|
||||||
|
let rustup_output = rustup_command.wait_with_output().unwrap();
|
||||||
|
if !rustup_output.status.success() {
|
||||||
|
return Err(format!(
|
||||||
|
"could not add target {}: {}",
|
||||||
|
target,
|
||||||
|
String::from_utf8_lossy(&rustup_output.stdout)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
DONE_COMPILER_CHECK.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ensure we have a compatible compiler toolchain. We use a new Target,
|
||||||
|
/// and we want to give the user a friendly way of installing the latest
|
||||||
|
/// Rust toolchain.
|
||||||
|
pub(crate) fn ensure_compiler(
|
||||||
|
target: &Option<&str>,
|
||||||
|
force_install: bool,
|
||||||
|
remove_existing: bool,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
if DONE_COMPILER_CHECK.load(std::sync::atomic::Ordering::SeqCst) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the sysroot exists, then we're good.
|
||||||
|
let target = target.unwrap_or(TARGET_TRIPLE_RISCV32);
|
||||||
|
if let Some(path) = get_sysroot(Some(target), true)? {
|
||||||
let mut version_path = PathBuf::from(&path);
|
let mut version_path = PathBuf::from(&path);
|
||||||
version_path.push("lib");
|
version_path.push("lib");
|
||||||
version_path.push("rustlib");
|
version_path.push("rustlib");
|
||||||
|
|
@ -125,7 +203,8 @@ pub(crate) fn ensure_compiler(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since no sysroot exists, we must download a new one.
|
// Since no sysroot exists, we must download a new one.
|
||||||
let toolchain_path = get_sysroot(None)?.ok_or_else(|| "default toolchain not installed".to_owned())?;
|
let toolchain_path =
|
||||||
|
get_sysroot(None, true)?.ok_or_else(|| "default toolchain not installed".to_owned())?;
|
||||||
// If the terminal is a tty, or if toolchain installation is forced,
|
// If the terminal is a tty, or if toolchain installation is forced,
|
||||||
// download the latest toolchain.
|
// download the latest toolchain.
|
||||||
if !atty::is(atty::Stream::Stdin) && !force_install {
|
if !atty::is(atty::Stream::Stdin) && !force_install {
|
||||||
|
|
@ -139,34 +218,16 @@ pub(crate) fn ensure_compiler(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ask the user if they want to install the toolchain.
|
// Ask the user if they want to install the toolchain.
|
||||||
let mut buffer = String::new();
|
|
||||||
let stdin = std::io::stdin();
|
|
||||||
let mut stdout = std::io::stdout();
|
|
||||||
if force_install {
|
if force_install {
|
||||||
println!("Downloading toolchain");
|
println!("Downloading toolchain");
|
||||||
} else {
|
} else {
|
||||||
println!("Error: Toolchain for {} was not found on this system!", target);
|
println!("Error: Toolchain for {} was not found on this system!", target);
|
||||||
}
|
|
||||||
loop {
|
|
||||||
if force_install {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
print!("Would you like this program to attempt to download and install it? [Y/n] ");
|
print!("Would you like this program to attempt to download and install it? [Y/n] ");
|
||||||
stdout.flush().unwrap();
|
if ask_to_install() {
|
||||||
buffer.clear();
|
println!("Installing toolchain for {} into {}", target, toolchain_path);
|
||||||
stdin.read_line(&mut buffer).unwrap();
|
} else {
|
||||||
|
|
||||||
let trimmed = buffer.trim();
|
|
||||||
|
|
||||||
if trimmed == "n" || trimmed == "N" {
|
|
||||||
return Err(format!("Please install the {} toolchain", target));
|
return Err(format!("Please install the {} toolchain", target));
|
||||||
}
|
}
|
||||||
|
|
||||||
if trimmed == "y" || trimmed == "Y" || trimmed.is_empty() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
println!();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_toolchain_url(target: &str, major: u64, minor: u64, patch: u64) -> Result<String, String> {
|
fn get_toolchain_url(target: &str, major: u64, minor: u64, patch: u64) -> Result<String, String> {
|
||||||
|
|
@ -239,31 +300,13 @@ pub(crate) fn ensure_compiler(
|
||||||
println!("Downloading toolchain from {}...", toolchain_url);
|
println!("Downloading toolchain from {}...", toolchain_url);
|
||||||
|
|
||||||
print!("Download in progress...");
|
print!("Download in progress...");
|
||||||
stdout.flush().unwrap();
|
std::io::stdout().flush().ok();
|
||||||
let mut zip_data = vec![];
|
let mut zip_data = vec![];
|
||||||
{
|
{
|
||||||
let agent = ureq::builder()
|
let agent = ureq::builder().build();
|
||||||
// .middleware(CounterMiddleware(shared_state.clone()))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let mut freader = agent.get(&toolchain_url).call().map_err(|e| format!("{}", e))?.into_reader();
|
let mut freader = agent.get(&toolchain_url).call().map_err(|e| format!("{}", e))?.into_reader();
|
||||||
freader.read_to_end(&mut zip_data).map_err(|e| format!("{}", e))?;
|
freader.read_to_end(&mut zip_data).map_err(|e| format!("{}", e))?;
|
||||||
// |total_bytes, bytes_so_far, _total_uploaded, _uploaded_so_far| {
|
|
||||||
// // If either number is infinite, don't print anything and just continue.
|
|
||||||
// if total_bytes.is_infinite() || bytes_so_far.is_infinite() {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Display progress.
|
|
||||||
// print!(
|
|
||||||
// "\rDownload progress: {:3.02}% ",
|
|
||||||
// bytes_so_far / total_bytes * 100.0
|
|
||||||
// );
|
|
||||||
// stdout.flush().unwrap();
|
|
||||||
|
|
||||||
// // Return `true` to continue the transfer.
|
|
||||||
// true
|
|
||||||
// },
|
|
||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
println!("Download successful. Total data size is {} bytes", zip_data.len());
|
println!("Download successful. Total data size is {} bytes", zip_data.len());
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue