xtask: support multiple different targets

This commit is contained in:
eupn 2023-03-04 18:15:48 +04:00
parent 47b5b88438
commit 3cf4cafb05
No known key found for this signature in database
GPG key ID: 0BBE77FB42014014
5 changed files with 75 additions and 26 deletions

1
Cargo.lock generated
View file

@ -5157,6 +5157,7 @@ dependencies = [
"atty",
"chrono",
"filetime",
"lazy_static",
"rustc_version 0.4.0",
"serde",
"serde_json",

View file

@ -18,4 +18,5 @@ serde = { version = "1.0.130", features = ["derive"] }
tempfile = "3.3.0"
ureq = { version = "2.5.0", features = ["json"] }
svd2utra = "0.1.11"
lazy_static = "1.4.0"
# toml_edit = "0.14.4" # would be used to verify Cargo.toml files, but it's too hard to do this right now.

View file

@ -5,7 +5,7 @@ use std::{
path::{Path, PathBuf},
process::Command,
};
use crate::{DynError, TARGET_TRIPLE, app_manifest::generate_app_menus, MemorySpec};
use crate::{DynError, app_manifest::generate_app_menus, MemorySpec};
#[allow(dead_code)]
#[derive(Copy, Clone, Debug)]
@ -148,7 +148,7 @@ impl Builder {
features: Vec::new(),
stream: BuildStream::Release,
min_ver: crate::MIN_XOUS_VERSION.to_string(),
target: Some(crate::TARGET_TRIPLE.to_string()),
target: Some(crate::TARGET_TRIPLE_RISCV32.to_string()),
utra_target: format!("precursor-{}", crate::PRECURSOR_SOC_VERSION).to_string(),
run_svd2repl: false,
locale_override: None,
@ -217,7 +217,7 @@ impl Builder {
}
/// Configure for renode targets
pub fn target_renode<'a>(&'a mut self) -> &'a mut Builder {
self.target = Some(crate::TARGET_TRIPLE.to_string());
self.target = Some(crate::TARGET_TRIPLE_RISCV32.to_string());
self.stream = BuildStream::Release;
self.run_svd2repl = true;
self.utra_target = "renode".to_string();
@ -229,7 +229,7 @@ impl Builder {
/// to call it anyways just in case the defaults change. The `soc_version` should
/// be just the gitrev of the soc version, not the entire feature name.
pub fn target_precursor<'a>(&'a mut self, soc_version: &str) -> &'a mut Builder {
self.target = Some(crate::TARGET_TRIPLE.to_string());
self.target = Some(crate::TARGET_TRIPLE_RISCV32.to_string());
self.stream = BuildStream::Release;
self.utra_target = format!("precursor-{}", soc_version).to_string();
self.run_svd2repl = false;
@ -238,6 +238,17 @@ impl Builder {
self
}
/// Configure for ARM targets
pub fn target_arm(&mut self) -> &mut Builder {
self.target = Some(crate::TARGET_TRIPLE_ARM.to_string());
self.stream = BuildStream::Debug;
self.utra_target = "atsama5d27".to_string();
self.run_svd2repl = false;
self.loader = CrateSpec::Local("loader".to_string(), false);
self.kernel = CrateSpec::Local("xous-kernel".to_string(), false);
self
}
/// Override the default kernel. For example, to use the kernel from crates.io, specify as "xous-kernel@0.9.9"
#[allow(dead_code)]
pub fn use_kernel<'a>(&'a mut self, spec: &str) -> &'a mut Builder {
@ -492,15 +503,13 @@ impl Builder {
// no services were specified - don't build anything
return Ok(())
}
crate::utils::ensure_compiler(&Some(TARGET_TRIPLE), false, false)?;
self.locale_override(); // apply the locale override
// ------ configure target generation feature flags ------
let target = if self.utra_target.contains("renode") {
self.features.push("renode".into());
self.loader_features.push("renode".into());
self.kernel_features.push("renode".into());
Some(crate::TARGET_TRIPLE)
Some(crate::TARGET_TRIPLE_RISCV32)
} else if self.utra_target.contains("hosted") {
self.features.push("hosted".into());
// there is no loader in hosed mode
@ -513,11 +522,18 @@ impl Builder {
self.kernel_features.push(format!("utralib/{}", &self.utra_target));
self.loader_features.push("precursor".into());
self.loader_features.push(format!("utralib/{}", &self.utra_target));
Some(crate::TARGET_TRIPLE)
Some(crate::TARGET_TRIPLE_RISCV32)
} else if self.utra_target.contains("atsama5d2") {
self.kernel_features.push("atsama5d27".into());
self.loader_features.push("atsama5d27".into());
Some(crate::TARGET_TRIPLE_ARM)
} else {
return Err("Target unknown: please check your UTRA target".into());
};
crate::utils::ensure_compiler(&self.target.as_ref().map(|s| s.as_str()), false, false)?;
self.locale_override(); // apply the locale override
// ------ build the services & apps ------
let mut app_names = Vec::<String>::new();
for app in self.apps.iter() {
@ -653,7 +669,7 @@ impl Builder {
}
// ---------- extract SVD file path, as computed by utralib ----------
let svd_spec_path = format!("target/{}/{}/build/SVD_PATH", TARGET_TRIPLE, self.stream.to_str());
let svd_spec_path = format!("target/{}/{}/build/SVD_PATH", self.target.as_ref().expect("target"), self.stream.to_str());
let mut svd_spec_file = OpenOptions::new()
.read(true)
.open(&svd_spec_path)?;
@ -665,7 +681,7 @@ impl Builder {
services_path.append(&mut self.enumerate_binary_files()?);
// --------- package up and sign a binary image ----------
let (inie, inif) = self.split_xip(services_path);
let (inie, inif) = self.split_xip(services_path.clone());
let output_bundle = self.create_image(
&kernel_path[0],
&inie,
@ -770,7 +786,7 @@ impl Builder {
let mut output_file = PathBuf::new();
output_file.push("target");
output_file.push(TARGET_TRIPLE);
output_file.push(self.target.as_ref().expect("target"));
output_file.push(stream);
output_file.push("xous_presign.img");
args.push(output_file.to_str().unwrap());
@ -811,7 +827,7 @@ impl Builder {
for item in [&self.services[..], &self.apps[..]].concat() {
match item {
CrateSpec::Prebuilt(name, url, _xip) => {
let exec_name = format!("target/{}/{}/{}", TARGET_TRIPLE, self.stream.to_str(), name);
let exec_name = format!("target/{}/{}/{}", self.target.as_ref().expect("target"), self.stream.to_str(), name);
println!("Fetching {} executable from build server...", name);
let mut exec_file = OpenOptions::new()
.read(true)

View file

@ -41,7 +41,9 @@ const PRECURSOR_SOC_VERSION: &str = "70190e2";
const MIN_XOUS_VERSION: &str = "v0.9.8-791";
/// target triple for precursor builds
const TARGET_TRIPLE: &str = "riscv32imac-unknown-xous-elf";
pub(crate) const TARGET_TRIPLE_RISCV32: &str = "riscv32imac-unknown-xous-elf";
/// target triple for ARM builds
pub(crate) const TARGET_TRIPLE_ARM: &str = "armv7a-unknown-xous-elf";
// because I have nowhere else to note this. The commit that contains the rkyv-enum derive
// refactor to work around warnings thrown by Rust 1.64.0 is: f815ed85b58b671178fbf53b4cea34186fc406eb
@ -159,7 +161,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Some("install-toolkit") | Some("install-toolchain") => {
let arg = env::args().nth(2);
ensure_compiler(
&Some(TARGET_TRIPLE),
&Some(TARGET_TRIPLE_RISCV32),
true,
arg.map(|x| x == "--force").unwrap_or(false),
)?
@ -369,6 +371,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.add_feature("avalanchetest");
}
// ------ ARM hardware image configs ------
Some("arm-tiny") => {
builder.target_arm()
.add_services(&base_pkgs.into_iter().map(String::from).collect())
.add_services(&get_cratespecs());
}
// ---- other single-purpose commands ----
Some("generate-locales") => generate_locales()?,
Some("wycheproof-import") => whycheproof_import()?,

View file

@ -1,3 +1,5 @@
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::{
fs::{File, OpenOptions},
io::{Read, Write},
@ -5,10 +7,22 @@ use std::{
process::Command,
};
use crate::TARGET_TRIPLE;
use crate::{cargo, project_root};
use crate::{TARGET_TRIPLE_RISCV32, TARGET_TRIPLE_ARM};
const TOOLCHAIN_RELEASE_URL: &str = "https://api.github.com/repos/betrusted-io/rust/releases";
const TOOLCHAIN_RELEASE_URL_ARM: &str =
"https://api.github.com/repos/Foundation-Devices/rust/releases";
lazy_static! {
static ref TOOLCHAIN_RELEASE_URLS: HashMap<String, String> = HashMap::from([
(TARGET_TRIPLE_RISCV32.to_owned(), TOOLCHAIN_RELEASE_URL.to_owned()),
(
TARGET_TRIPLE_ARM.to_owned(),
TOOLCHAIN_RELEASE_URL_ARM.to_owned()
),
]);
}
/// Since we use the same TARGET for all calls to `build()`,
/// cache it inside an atomic boolean. If this is `true` then
@ -86,12 +100,12 @@ pub(crate) fn ensure_compiler(
}
// If the sysroot exists, then we're good.
let target = target.unwrap_or(TARGET_TRIPLE);
let target = target.unwrap_or(TARGET_TRIPLE_RISCV32);
if let Some(path) = get_sysroot(Some(target))? {
let mut version_path = PathBuf::from(&path);
version_path.push("lib");
version_path.push("rustlib");
version_path.push(TARGET_TRIPLE);
version_path.push(target);
if remove_existing {
println!("Target path exists, removing it");
std::fs::remove_dir_all(version_path)
@ -99,13 +113,13 @@ pub(crate) fn ensure_compiler(
println!("Also removing target directories for existing toolchain");
let mut target_main = project_root();
target_main.push("target");
target_main.push(TARGET_TRIPLE);
target_main.push(target);
std::fs::remove_dir_all(target_main).ok();
let mut target_loader = project_root();
target_loader.push("loader");
target_loader.push("target");
target_loader.push(TARGET_TRIPLE);
target_loader.push(target);
std::fs::remove_dir_all(target_loader).ok();
} else {
DONE_COMPILER_CHECK.store(true, std::sync::atomic::Ordering::SeqCst);
@ -162,8 +176,16 @@ pub(crate) fn ensure_compiler(
println!();
}
fn get_toolchain_url(major: u64, minor: u64, patch: u64) -> Result<String, String> {
let j: serde_json::Value = ureq::get(TOOLCHAIN_RELEASE_URL)
fn get_toolchain_url(
target: &str,
major: u64,
minor: u64,
patch: u64,
) -> Result<String, String> {
let url = TOOLCHAIN_RELEASE_URLS
.get(target)
.ok_or_else(|| format!("Can't find toolchain URL for target {}", target))?;
let j: serde_json::Value = ureq::get(&url)
.set("Accept", "application/vnd.github.v3+json")
.call()
.map_err(|e| format!("{}", e))?
@ -218,12 +240,12 @@ pub(crate) fn ensure_compiler(
}
if let Some((_k, v)) = tag_urls.into_iter().last() {
// println!("Found candidate entry: v{} url {}", k, v);
// println!("Found candidate entry: v{} url {}", _k, v);
return Ok(v);
}
Err(format!("No toolchains found for Rust {}", target_prefix))
}
let toolchain_url = get_toolchain_url(ver.major, ver.minor, ver.patch)?;
let toolchain_url = get_toolchain_url(target, ver.major, ver.minor, ver.patch)?;
println!(
"Attempting to install toolchain for {} into {}",
@ -377,8 +399,8 @@ pub(crate) fn whycheproof_import() -> Result<(), crate::DynError> {
}
pub(crate) fn track_language_changes(last_lang: &str) -> Result<(), crate::DynError> {
let last_config = format!("target/{}/LAST_LANG", TARGET_TRIPLE);
std::fs::create_dir_all(format!("target/{}/", TARGET_TRIPLE)).unwrap();
let last_config = format!("target/{}/LAST_LANG", TARGET_TRIPLE_RISCV32);
std::fs::create_dir_all(format!("target/{}/", TARGET_TRIPLE_RISCV32)).unwrap();
let mut contents = String::new();
let changed = match OpenOptions::new()
@ -407,4 +429,4 @@ pub(crate) fn track_language_changes(last_lang: &str) -> Result<(), crate::DynEr
println!("No change to the target locale language of {}", contents);
}
Ok(())
}
}