feat(xtask): add --config flag for cargo config

Problem: there is no way to pass extra cargo config
(e.g. vendored sources) through xtask to the
underlying cargo invocations.

Solution: accept --config flags in cargo xtask and
thread them through all internal cargo invocations.
The cargo() helper now returns a Command with the
config flags already applied.
This commit is contained in:
Sylvain Bellemare 2026-03-15 17:25:07 +09:00
parent e9c408fbca
commit fa14fa78c6
No known key found for this signature in database
GPG key ID: C38AE0ECCAB7D5C8
3 changed files with 41 additions and 26 deletions

View file

@ -184,6 +184,7 @@ pub(crate) struct Builder {
detached_app_features: Vec<String>, detached_app_features: Vec<String>,
git_describe: Option<String>, git_describe: Option<String>,
git_rev: Option<String>, git_rev: Option<String>,
cargo_configs: Vec<String>,
} }
impl Builder { impl Builder {
@ -219,9 +220,15 @@ impl Builder {
detached_app_features: Vec::new(), detached_app_features: Vec::new(),
git_describe: None, git_describe: None,
git_rev: None, git_rev: None,
cargo_configs: Vec::new(),
} }
} }
pub fn set_cargo_configs(&mut self, configs: Vec<String>) -> &mut Builder {
self.cargo_configs = configs;
self
}
pub fn set_git_describe(&mut self, git_describe: String) -> &mut Builder { pub fn set_git_describe(&mut self, git_describe: String) -> &mut Builder {
self.git_describe = Some(git_describe); self.git_describe = Some(git_describe);
self self
@ -675,7 +682,7 @@ impl Builder {
} }
println!(); println!();
// build // build
let status = Command::new(cargo()).current_dir(project_root()).args(&local_args).status()?; let status = cargo(&self.cargo_configs).current_dir(project_root()).args(&local_args).status()?;
if !status.success() { if !status.success() {
return Err("Local build failed".into()); return Err("Local build failed".into());
} }
@ -700,7 +707,7 @@ impl Builder {
} }
println!(" {} {}", name, version); println!(" {} {}", name, version);
// build // build
let status = Command::new(cargo()) let status = cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([&remote_args[..], &[name, "--version", version].to_vec()[..]].concat()) .args([&remote_args[..], &[name, "--version", version].to_vec()[..]].concat())
.status()?; .status()?;
@ -906,7 +913,7 @@ impl Builder {
print!(" {}", arg); print!(" {}", arg);
} }
println!(); println!();
let status = Command::new(cargo()).current_dir(dir).args(&hosted_args).status()?; let status = cargo(&self.cargo_configs).current_dir(dir).args(&hosted_args).status()?;
if !status.success() { if !status.success() {
return Err("cargo run failed to launch hosted mode".into()); return Err("cargo run failed to launch hosted mode".into());
} }
@ -985,7 +992,7 @@ impl Builder {
presign_file presign_file
.push(format!("{}-presign.img", self.loader.name().unwrap_or("baremetal".to_string()))); .push(format!("{}-presign.img", self.loader.name().unwrap_or("baremetal".to_string())));
let status = Command::new(cargo()) let status = cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([ .args([
"run", "run",
@ -1021,7 +1028,7 @@ impl Builder {
Some(gd) => vec!["--git-describe", gd], Some(gd) => vec!["--git-describe", gd],
None => vec![], None => vec![],
}; };
Command::new(cargo()) cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([ .args([
"run", "run",
@ -1080,7 +1087,7 @@ impl Builder {
// ------ if targeting renode, regenerate the Platform file ----- // ------ if targeting renode, regenerate the Platform file -----
if self.run_svd2repl { if self.run_svd2repl {
Command::new(cargo()) cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([ .args([
"run", "run",
@ -1116,7 +1123,7 @@ impl Builder {
loader_bin.push("loader.bin"); loader_bin.push("loader.bin");
let mut loader_presign = output_bundle.parent().unwrap().to_owned(); let mut loader_presign = output_bundle.parent().unwrap().to_owned();
loader_presign.push("loader_presign.bin"); loader_presign.push("loader_presign.bin");
let status = Command::new(cargo()) let status = cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([ .args([
"run", "run",
@ -1139,7 +1146,7 @@ impl Builder {
None => vec![], None => vec![],
}; };
let status = if self.utra_target.contains("bao1x") { let status = if self.utra_target.contains("bao1x") {
Command::new(cargo()) cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([ .args([
"run", "run",
@ -1166,7 +1173,7 @@ impl Builder {
.args(&git_describe_args) .args(&git_describe_args)
.status()? .status()?
} else { } else {
Command::new(cargo()) cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([ .args([
"run", "run",
@ -1195,7 +1202,7 @@ impl Builder {
xous_img_path.push("xous.img"); xous_img_path.push("xous.img");
let status = if self.utra_target.contains("bao1x") { let status = if self.utra_target.contains("bao1x") {
Command::new(cargo()) cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([ .args([
"run", "run",
@ -1223,7 +1230,7 @@ impl Builder {
.args(&git_describe_args) .args(&git_describe_args)
.status()? .status()?
} else { } else {
Command::new(cargo()) cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([ .args([
"run", "run",
@ -1357,7 +1364,7 @@ impl Builder {
args.push(git_describe); args.push(git_describe);
} }
let status = Command::new(cargo()).current_dir(project_root()).args(&args).status()?; let status = cargo(&self.cargo_configs).current_dir(project_root()).args(&args).status()?;
if !status.success() { if !status.success() {
return Err("cargo build failed".into()); return Err("cargo build failed".into());
@ -1391,7 +1398,7 @@ impl Builder {
let detached_offset = let detached_offset =
format!("{}", bao1x_api::offsets::dabao::APP_RRAM_START - bao1x_api::offsets::KERNEL_START); format!("{}", bao1x_api::offsets::dabao::APP_RRAM_START - bao1x_api::offsets::KERNEL_START);
args.push(&detached_offset); args.push(&detached_offset);
let status = Command::new(cargo()).current_dir(project_root()).args(&args).status()?; let status = cargo(&self.cargo_configs).current_dir(project_root()).args(&args).status()?;
if !status.success() { if !status.success() {
return Err("cargo build failed".into()); return Err("cargo build failed".into());
@ -1404,7 +1411,7 @@ impl Builder {
Some(gd) => vec!["--git-describe", gd], Some(gd) => vec!["--git-describe", gd],
None => vec![], None => vec![],
}; };
Command::new(cargo()) cargo(&self.cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args([ .args([
"run", "run",
@ -1508,7 +1515,13 @@ impl Builder {
} }
} }
pub fn cargo() -> String { env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()) } pub fn cargo(configs: &[String]) -> Command {
let mut cmd = Command::new(env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()));
for path in configs {
cmd.args(["--config", path]);
}
cmd
}
pub fn project_root() -> PathBuf { pub fn project_root() -> PathBuf {
Path::new(&env!("CARGO_MANIFEST_DIR")).ancestors().nth(1).unwrap().to_path_buf() Path::new(&env!("CARGO_MANIFEST_DIR")).ancestors().nth(1).unwrap().to_path_buf()

View file

@ -67,6 +67,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Some(rev) = git_rev_opt { if let Some(rev) = git_rev_opt {
builder.set_git_rev(rev); builder.set_git_rev(rev);
} }
let cargo_configs = get_flag("--config")?;
builder.set_cargo_configs(cargo_configs.clone());
if do_version { if do_version {
builder.add_feature("timestamp"); builder.add_feature("timestamp");
}; };
@ -184,7 +186,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
for feature in features { for feature in features {
builder.add_feature(&feature); builder.add_feature(&feature);
if feature.starts_with("xous/lang-") { if feature.starts_with("xous/lang-") {
track_language_changes(&feature)?; track_language_changes(&feature, &cargo_configs)?;
language_set = true; language_set = true;
} }
} }
@ -203,7 +205,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if !language_set { if !language_set {
// the default language is english // the default language is english
track_language_changes("en")?; track_language_changes("en", &cargo_configs)?;
} }
let gdb_stub = env::args().filter(|x| x == "--gdb-stub").count() != 0; let gdb_stub = env::args().filter(|x| x == "--gdb-stub").count() != 0;
if gdb_stub { if gdb_stub {
@ -622,7 +624,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
local_args.push("artybio"); local_args.push("artybio");
let status = let status =
std::process::Command::new(cargo()).current_dir(project_root()).args(&local_args).status()?; cargo(&cargo_configs).current_dir(project_root()).args(&local_args).status()?;
if !status.success() { if !status.success() {
return Err("Baremetal build failed".into()); return Err("Baremetal build failed".into());
} }
@ -862,8 +864,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
// ---- other single-purpose commands ---- // ---- other single-purpose commands ----
Some("generate-locales") => generate_locales()?, Some("generate-locales") => generate_locales(&cargo_configs)?,
Some("wycheproof-import") => wycheproof_import()?, Some("wycheproof-import") => wycheproof_import(&cargo_configs)?,
Some("dummy-template") => generate_app_menus(&Vec::new()), Some("dummy-template") => generate_app_menus(&Vec::new()),
task => { task => {
if let Some(task) = task { if let Some(task) = task {

View file

@ -371,12 +371,12 @@ pub(crate) fn ensure_compiler(
} }
/// Regenerate the locales files. This is only done when the command is explicitly run. /// Regenerate the locales files. This is only done when the command is explicitly run.
pub(crate) fn generate_locales() -> Result<(), std::io::Error> { pub(crate) fn generate_locales(cargo_configs: &[String]) -> Result<(), std::io::Error> {
let ts = filetime::FileTime::from_system_time(std::time::SystemTime::now()); let ts = filetime::FileTime::from_system_time(std::time::SystemTime::now());
filetime::set_file_mtime("locales/src/lib.rs", ts)?; filetime::set_file_mtime("locales/src/lib.rs", ts)?;
let mut path = project_root(); let mut path = project_root();
path.push("locales"); path.push("locales");
let status = Command::new(cargo()).current_dir(path).args(["build", "--package", "locales"]).status()?; let status = cargo(cargo_configs).current_dir(path).args(["build", "--package", "locales"]).status()?;
if !status.success() { if !status.success() {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Couldn't generate the locales")); return Err(std::io::Error::new(std::io::ErrorKind::Other, "Couldn't generate the locales"));
} }
@ -384,10 +384,10 @@ pub(crate) fn generate_locales() -> Result<(), std::io::Error> {
} }
/// Import the Wycheproof test vectors /// Import the Wycheproof test vectors
pub(crate) fn wycheproof_import() -> Result<(), crate::DynError> { pub(crate) fn wycheproof_import(cargo_configs: &[String]) -> Result<(), crate::DynError> {
let input_file = "tools/wycheproof-import/x25519_test.json"; let input_file = "tools/wycheproof-import/x25519_test.json";
let output_file = "services/shellchat/src/cmds/x25519_test.bin"; let output_file = "services/shellchat/src/cmds/x25519_test.bin";
let status = Command::new(cargo()) let status = cargo(cargo_configs)
.current_dir(project_root()) .current_dir(project_root())
.args(["run", "--package", "wycheproof-import", "--", input_file, output_file]) .args(["run", "--package", "wycheproof-import", "--", input_file, output_file])
.status()?; .status()?;
@ -401,7 +401,7 @@ pub(crate) fn wycheproof_import() -> Result<(), crate::DynError> {
Ok(()) Ok(())
} }
pub(crate) fn track_language_changes(last_lang: &str) -> Result<(), crate::DynError> { pub(crate) fn track_language_changes(last_lang: &str, cargo_configs: &[String]) -> Result<(), crate::DynError> {
let last_config = "target/LAST_LANG"; let last_config = "target/LAST_LANG";
let mut contents = String::new(); let mut contents = String::new();
@ -416,7 +416,7 @@ pub(crate) fn track_language_changes(last_lang: &str) -> Result<(), crate::DynEr
println!("Locale language changed to {}", last_lang); println!("Locale language changed to {}", last_lang);
let mut file = OpenOptions::new().create(true).write(true).truncate(true).open(last_config).unwrap(); let mut file = OpenOptions::new().create(true).write(true).truncate(true).open(last_config).unwrap();
write!(file, "{}", last_lang).unwrap(); write!(file, "{}", last_lang).unwrap();
generate_locales()? generate_locales(cargo_configs)?
} else { } else {
println!("No change to the target locale language of {}", contents); println!("No change to the target locale language of {}", contents);
} }