Add rustc command to build system

This commit is contained in:
Guillaume Gomez 2024-04-05 17:32:19 +02:00
parent 5eb8d854d1
commit 00ad2634d5
2 changed files with 81 additions and 53 deletions

View File

@ -29,6 +29,7 @@ fn usage() {
Available commands for build_system: Available commands for build_system:
cargo : Run cargo command cargo : Run cargo command
rustc : Run rustc command
clean : Run clean command clean : Run clean command
prepare : Run prepare command prepare : Run prepare command
build : Run build command build : Run build command
@ -45,6 +46,7 @@ pub enum Command {
CloneGcc, CloneGcc,
Prepare, Prepare,
Build, Build,
Rustc,
Test, Test,
Info, Info,
} }
@ -56,6 +58,7 @@ fn main() {
let command = match env::args().nth(1).as_deref() { let command = match env::args().nth(1).as_deref() {
Some("cargo") => Command::Cargo, Some("cargo") => Command::Cargo,
Some("rustc") => Command::Rustc,
Some("clean") => Command::Clean, Some("clean") => Command::Clean,
Some("prepare") => Command::Prepare, Some("prepare") => Command::Prepare,
Some("build") => Command::Build, Some("build") => Command::Build,
@ -76,6 +79,7 @@ fn main() {
if let Err(e) = match command { if let Err(e) = match command {
Command::Cargo => rust_tools::run_cargo(), Command::Cargo => rust_tools::run_cargo(),
Command::Rustc => rust_tools::run_rustc(),
Command::Clean => clean::run(), Command::Clean => clean::run(),
Command::Prepare => prepare::run(), Command::Prepare => prepare::run(),
Command::Build => build::run(), Command::Build => build::run(),

View File

@ -9,7 +9,7 @@
use std::path::PathBuf; use std::path::PathBuf;
fn args(command: &str) -> Result<Option<Vec<String>>, String> { fn args(command: &str) -> Result<Option<Vec<String>>, String> {
// We skip the binary and the "cargo" option. // We skip the binary and the "cargo"/"rustc" option.
if let Some("--help") = std::env::args().skip(2).next().as_deref() { if let Some("--help") = std::env::args().skip(2).next().as_deref() {
usage(command); usage(command);
return Ok(None); return Ok(None);
@ -36,11 +36,16 @@ fn usage(command: &str) {
) )
} }
pub fn run_cargo() -> Result<(), String> { struct RustcTools {
let args = match args("cargo")? { env: HashMap<String, String>,
Some(a) => a, args: Vec<String>,
None => return Ok(()), toolchain: String,
}; config: ConfigInfo,
}
impl RustcTools {
fn new(command: &str) -> Result<Option<Self>, String> {
let Some(args) = args(command)? else { return Ok(None) };
// We first need to go to the original location to ensure that the config setup will go as // We first need to go to the original location to ensure that the config setup will go as
// expected. // expected.
@ -50,7 +55,8 @@ pub fn run_cargo() -> Result<(), String> {
let current_exe = std::env::current_exe() let current_exe = std::env::current_exe()
.and_then(|path| path.canonicalize()) .and_then(|path| path.canonicalize())
.map_err(|error| format!("Failed to get current exe path: {:?}", error))?; .map_err(|error| format!("Failed to get current exe path: {:?}", error))?;
let mut parent_dir = current_exe.components().map(|comp| comp.as_os_str()).collect::<Vec<_>>(); let mut parent_dir =
current_exe.components().map(|comp| comp.as_os_str()).collect::<Vec<_>>();
// We run this script from "build_system/target/release/y", so we need to remove these elements. // We run this script from "build_system/target/release/y", so we need to remove these elements.
for to_remove in &["y", "release", "target", "build_system"] { for to_remove in &["y", "release", "target", "build_system"] {
if parent_dir.last().map(|part| part == to_remove).unwrap_or(false) { if parent_dir.last().map(|part| part == to_remove).unwrap_or(false) {
@ -68,7 +74,8 @@ pub fn run_cargo() -> Result<(), String> {
})?; })?;
let mut env: HashMap<String, String> = std::env::vars().collect(); let mut env: HashMap<String, String> = std::env::vars().collect();
ConfigInfo::default().setup(&mut env, false)?; let mut config = ConfigInfo::default();
config.setup(&mut env, false)?;
let toolchain = get_toolchain()?; let toolchain = get_toolchain()?;
let toolchain_version = rustc_toolchain_version_info(&toolchain)?; let toolchain_version = rustc_toolchain_version_info(&toolchain)?;
@ -85,17 +92,34 @@ pub fn run_cargo() -> Result<(), String> {
std::env::set_current_dir(&current_dir).map_err(|error| { std::env::set_current_dir(&current_dir).map_err(|error| {
format!("Failed to go back to `{}` folder: {:?}", current_dir.display(), error) format!("Failed to go back to `{}` folder: {:?}", current_dir.display(), error)
})?; })?;
let rustflags = env.get("RUSTFLAGS").cloned().unwrap_or_default();
env.insert("RUSTDOCFLAGS".to_string(), rustflags);
let toolchain = format!("+{}", toolchain); let toolchain = format!("+{}", toolchain);
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &toolchain]; Ok(Some(Self { toolchain, args, env, config }))
for arg in &args { }
}
pub fn run_cargo() -> Result<(), String> {
let Some(mut tools) = RustcTools::new("cargo")? else { return Ok(()) };
let rustflags = tools.env.get("RUSTFLAGS").cloned().unwrap_or_default();
tools.env.insert("RUSTDOCFLAGS".to_string(), rustflags);
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &tools.toolchain];
for arg in &tools.args {
command.push(arg); command.push(arg);
} }
if run_command_with_output_and_env_no_err(&command, None, Some(&env)).is_err() { if run_command_with_output_and_env_no_err(&command, None, Some(&tools.env)).is_err() {
std::process::exit(1); std::process::exit(1);
} }
Ok(()) Ok(())
} }
pub fn run_rustc() -> Result<(), String> {
let Some(tools) = RustcTools::new("rustc")? else { return Ok(()) };
let mut command = tools.config.rustc_command_vec();
for arg in &tools.args {
command.push(arg);
}
if run_command_with_output_and_env_no_err(&command, None, Some(&tools.env)).is_err() {
std::process::exit(1);
}
Ok(())
}