mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
xtask: offer to download the toolchain
If the toolchain does not exist, offer to download it for the user. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
05801f3f06
commit
3c917d9be4
2 changed files with 209 additions and 25 deletions
|
|
@ -1,12 +1,13 @@
|
||||||
[package]
|
[package]
|
||||||
name = "xtask"
|
name = "xtask"
|
||||||
|
description = "Cargo-based build system for Xous"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Sean Cross <sean@xobs.io>"]
|
authors = ["Sean Cross <sean@xobs.io>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
curl = "0.4"
|
||||||
|
atty = "0.2"
|
||||||
filetime = "0.2"
|
filetime = "0.2"
|
||||||
#"ed25519-dalek" = "1"
|
rustc_version = "0.4"
|
||||||
#"pem" = "0.8"
|
zip = "0.5"
|
||||||
|
|
@ -9,6 +9,9 @@ type DynError = Box<dyn std::error::Error>;
|
||||||
|
|
||||||
const PROGRAM_TARGET: &str = "riscv32imac-unknown-xous-elf";
|
const PROGRAM_TARGET: &str = "riscv32imac-unknown-xous-elf";
|
||||||
const KERNEL_TARGET: &str = "riscv32imac-unknown-xous-elf";
|
const KERNEL_TARGET: &str = "riscv32imac-unknown-xous-elf";
|
||||||
|
const TOOLCHAIN_URL_PREFIX: &str =
|
||||||
|
"https://github.com/betrusted-io/rust/releases/latest/download/riscv32imac-unknown-xous_";
|
||||||
|
const TOOLCHAIN_URL_SUFFIX: &str = ".zip";
|
||||||
|
|
||||||
enum MemorySpec {
|
enum MemorySpec {
|
||||||
SvdFile(String),
|
SvdFile(String),
|
||||||
|
|
@ -539,35 +542,215 @@ fn build_kernel(debug: bool) -> Result<PathBuf, DynError> {
|
||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Since we use the same TARGET for all calls to `build()`,
|
||||||
|
/// cache it inside an atomic boolean. If this is `true` then
|
||||||
|
/// it means we can assume the check passed already.
|
||||||
static DONE_COMPILER_CHECK: std::sync::atomic::AtomicBool =
|
static DONE_COMPILER_CHECK: std::sync::atomic::AtomicBool =
|
||||||
std::sync::atomic::AtomicBool::new(false);
|
std::sync::atomic::AtomicBool::new(false);
|
||||||
|
|
||||||
|
/// 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.
|
||||||
fn ensure_compiler(target: &Option<&str>) -> Result<(), String> {
|
fn ensure_compiler(target: &Option<&str>) -> Result<(), String> {
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
if DONE_COMPILER_CHECK.load(std::sync::atomic::Ordering::SeqCst) {
|
if DONE_COMPILER_CHECK.load(std::sync::atomic::Ordering::SeqCst) {
|
||||||
return Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
let target = target.unwrap_or(PROGRAM_TARGET).to_owned();
|
|
||||||
let have_toolchain = Command::new("rustc")
|
|
||||||
.stderr(Stdio::null())
|
|
||||||
.stdout(Stdio::null())
|
|
||||||
.args(&[
|
|
||||||
"--target",
|
|
||||||
&target,
|
|
||||||
"--print",
|
|
||||||
"sysroot",
|
|
||||||
])
|
|
||||||
.spawn()
|
|
||||||
.unwrap()
|
|
||||||
.wait()
|
|
||||||
.unwrap()
|
|
||||||
.success();
|
|
||||||
|
|
||||||
DONE_COMPILER_CHECK.store(have_toolchain, std::sync::atomic::Ordering::SeqCst);
|
/// Return the sysroot for the given target. If the target does not exist,
|
||||||
if have_toolchain {
|
/// return None.
|
||||||
Ok(())
|
fn get_sysroot(target: Option<&str>) -> Result<Option<String>, String> {
|
||||||
} else {
|
let mut args = vec!["--print", "sysroot"];
|
||||||
Err(format!("Toolchain for {} not found", target))
|
if let Some(target) = target {
|
||||||
|
args.push("--target");
|
||||||
|
args.push(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
let sysroot_cmd = Command::new("rustc")
|
||||||
|
.stderr(Stdio::null())
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.args(&args)
|
||||||
|
.spawn()
|
||||||
|
.expect("could not run rustc");
|
||||||
|
let sysroot_output = sysroot_cmd.wait_with_output().unwrap();
|
||||||
|
let have_toolchain = sysroot_output.status.success();
|
||||||
|
|
||||||
|
let toolchain_path = String::from_utf8(sysroot_output.stdout)
|
||||||
|
.map_err(|_| "Unable to find Rust sysroot".to_owned())?
|
||||||
|
.trim()
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
if have_toolchain {
|
||||||
|
Ok(Some(toolchain_path))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the sysroot exists, then we're good.
|
||||||
|
let target = target.unwrap_or(PROGRAM_TARGET);
|
||||||
|
if get_sysroot(Some(target))?.is_some() {
|
||||||
|
DONE_COMPILER_CHECK.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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())?;
|
||||||
|
// If the terminal is a tty, offer to download the latest toolchain.
|
||||||
|
if !atty::is(atty::Stream::Stdin) {
|
||||||
|
return Err(format!("Toolchain for {} not found", target));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Version 1.54 was the last major version that was released.
|
||||||
|
let ver = rustc_version::version().unwrap();
|
||||||
|
if ver.major == 1 && ver.minor < 54 {
|
||||||
|
return Err("Rust 1.54 or higher is required".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
println!();
|
||||||
|
println!(
|
||||||
|
"Error: Toolchain for {} was not found on this system!",
|
||||||
|
target
|
||||||
|
);
|
||||||
|
loop {
|
||||||
|
print!("Would you like this program to attempt to download and install it? [Y/n] ");
|
||||||
|
stdout.flush().unwrap();
|
||||||
|
buffer.clear();
|
||||||
|
stdin.read_line(&mut buffer).unwrap();
|
||||||
|
|
||||||
|
let trimmed = buffer.trim();
|
||||||
|
|
||||||
|
if trimmed == "n" || trimmed == "N" {
|
||||||
|
return Err(format!("Please install the {} toolchain", target));
|
||||||
|
}
|
||||||
|
|
||||||
|
if trimmed == "y" || trimmed == "Y" || trimmed.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
|
let toolchain_url = format!(
|
||||||
|
"{}{}.{}.{}{}",
|
||||||
|
TOOLCHAIN_URL_PREFIX, ver.major, ver.minor, ver.patch, TOOLCHAIN_URL_SUFFIX
|
||||||
|
);
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Attempting to install toolchain for {} into {}",
|
||||||
|
target, toolchain_path
|
||||||
|
);
|
||||||
|
println!("Downloading from {}...", toolchain_url);
|
||||||
|
|
||||||
|
print!("Download rogress: 0%");
|
||||||
|
stdout.flush().unwrap();
|
||||||
|
let mut zip_data = vec![];
|
||||||
|
{
|
||||||
|
let mut easy = curl::easy::Easy::new();
|
||||||
|
easy.url(&toolchain_url).unwrap();
|
||||||
|
easy.follow_location(true).unwrap();
|
||||||
|
easy.progress(true).unwrap();
|
||||||
|
let mut transfer = easy.transfer();
|
||||||
|
transfer
|
||||||
|
.progress_function(
|
||||||
|
|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
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
transfer
|
||||||
|
.write_function(|data| {
|
||||||
|
zip_data.extend_from_slice(data);
|
||||||
|
Ok(data.len())
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
transfer
|
||||||
|
.perform()
|
||||||
|
.map_err(|e| format!("Unable to download toolchain: {}", e))?;
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
println!(
|
||||||
|
"Download successful. Total data size is {} bytes",
|
||||||
|
zip_data.len()
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Extract the zipfile to the target directory, ensuring that all files
|
||||||
|
/// contained within are created.
|
||||||
|
fn extract_zip<P: std::io::Read + std::io::Seek, P2: AsRef<Path>>(
|
||||||
|
archive_data: P,
|
||||||
|
extract_to: P2,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let mut archive = zip::ZipArchive::new(archive_data)
|
||||||
|
.map_err(|e| format!("unable to extract zip: {}", e))?;
|
||||||
|
for i in 0..archive.len() {
|
||||||
|
let mut entry_in_archive = archive
|
||||||
|
.by_index(i)
|
||||||
|
.map_err(|e| format!("unable to locate file index {}: {}", i, e))?;
|
||||||
|
// println!(
|
||||||
|
// "Trying to extract file {}",
|
||||||
|
// entry_in_archive.mangled_name().display()
|
||||||
|
// );
|
||||||
|
|
||||||
|
let output_path = extract_to.as_ref().join(entry_in_archive.mangled_name());
|
||||||
|
if entry_in_archive.is_dir() {
|
||||||
|
std::fs::create_dir_all(&output_path).map_err(|e| {
|
||||||
|
format!(
|
||||||
|
"unable to create directory {}: {}",
|
||||||
|
output_path.display(),
|
||||||
|
e
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
} else {
|
||||||
|
// Create the parent directory if necessary
|
||||||
|
if let Some(parent) = output_path.parent() {
|
||||||
|
if !parent.exists() {
|
||||||
|
std::fs::create_dir_all(&parent).map_err(|e| {
|
||||||
|
format!(
|
||||||
|
"unable to create directory {}: {}",
|
||||||
|
output_path.display(),
|
||||||
|
e
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut outfile = std::fs::File::create(&output_path).map_err(|e| {
|
||||||
|
format!("unable to create file {}: {}", output_path.display(), e)
|
||||||
|
})?;
|
||||||
|
std::io::copy(&mut entry_in_archive, &mut outfile).map_err(|e| {
|
||||||
|
format!(
|
||||||
|
"unable to write extracted file {}: {}",
|
||||||
|
output_path.display(),
|
||||||
|
e
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
println!("Extracting toolchain to {}...", toolchain_path);
|
||||||
|
extract_zip(std::io::Cursor::new(zip_data), &toolchain_path)?;
|
||||||
|
|
||||||
|
println!("Toolchain successfully installed");
|
||||||
|
|
||||||
|
DONE_COMPILER_CHECK.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(
|
fn build(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue