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,66 +36,90 @@ 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,
}
// We first need to go to the original location to ensure that the config setup will go as impl RustcTools {
// expected. fn new(command: &str) -> Result<Option<Self>, String> {
let current_dir = std::env::current_dir() let Some(args) = args(command)? else { return Ok(None) };
.and_then(|path| path.canonicalize())
.map_err(|error| format!("Failed to get current directory path: {:?}", error))?; // We first need to go to the original location to ensure that the config setup will go as
let current_exe = std::env::current_exe() // expected.
.and_then(|path| path.canonicalize()) let current_dir = std::env::current_dir()
.map_err(|error| format!("Failed to get current exe path: {:?}", error))?; .and_then(|path| path.canonicalize())
let mut parent_dir = current_exe.components().map(|comp| comp.as_os_str()).collect::<Vec<_>>(); .map_err(|error| format!("Failed to get current directory path: {:?}", error))?;
// We run this script from "build_system/target/release/y", so we need to remove these elements. let current_exe = std::env::current_exe()
for to_remove in &["y", "release", "target", "build_system"] { .and_then(|path| path.canonicalize())
if parent_dir.last().map(|part| part == to_remove).unwrap_or(false) { .map_err(|error| format!("Failed to get current exe path: {:?}", error))?;
parent_dir.pop(); let mut parent_dir =
} else { current_exe.components().map(|comp| comp.as_os_str()).collect::<Vec<_>>();
return Err(format!( // We run this script from "build_system/target/release/y", so we need to remove these elements.
"Build script not executed from `build_system/target/release/y` (in path {})", for to_remove in &["y", "release", "target", "build_system"] {
current_exe.display(), if parent_dir.last().map(|part| part == to_remove).unwrap_or(false) {
)); parent_dir.pop();
} else {
return Err(format!(
"Build script not executed from `build_system/target/release/y` (in path {})",
current_exe.display(),
));
}
} }
let parent_dir = PathBuf::from(parent_dir.join(&OsStr::new("/")));
std::env::set_current_dir(&parent_dir).map_err(|error| {
format!("Failed to go to `{}` folder: {:?}", parent_dir.display(), error)
})?;
let mut env: HashMap<String, String> = std::env::vars().collect();
let mut config = ConfigInfo::default();
config.setup(&mut env, false)?;
let toolchain = get_toolchain()?;
let toolchain_version = rustc_toolchain_version_info(&toolchain)?;
let default_version = rustc_version_info(None)?;
if toolchain_version != default_version {
println!(
"rustc_codegen_gcc is built for {} but the default rustc version is {}.",
toolchain_version.short, default_version.short,
);
println!("Using {}.", toolchain_version.short);
}
// We go back to the original folder since we now have set up everything we needed.
std::env::set_current_dir(&current_dir).map_err(|error| {
format!("Failed to go back to `{}` folder: {:?}", current_dir.display(), error)
})?;
let toolchain = format!("+{}", toolchain);
Ok(Some(Self { toolchain, args, env, config }))
} }
let parent_dir = PathBuf::from(parent_dir.join(&OsStr::new("/"))); }
std::env::set_current_dir(&parent_dir).map_err(|error| {
format!("Failed to go to `{}` folder: {:?}", parent_dir.display(), error)
})?;
let mut env: HashMap<String, String> = std::env::vars().collect(); pub fn run_cargo() -> Result<(), String> {
ConfigInfo::default().setup(&mut env, false)?; let Some(mut tools) = RustcTools::new("cargo")? else { return Ok(()) };
let toolchain = get_toolchain()?; let rustflags = tools.env.get("RUSTFLAGS").cloned().unwrap_or_default();
tools.env.insert("RUSTDOCFLAGS".to_string(), rustflags);
let toolchain_version = rustc_toolchain_version_info(&toolchain)?; let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &tools.toolchain];
let default_version = rustc_version_info(None)?; for arg in &tools.args {
if toolchain_version != default_version {
println!(
"rustc_codegen_gcc is built for {} but the default rustc version is {}.",
toolchain_version.short, default_version.short,
);
println!("Using {}.", toolchain_version.short);
}
// We go back to the original folder since we now have set up everything we needed.
std::env::set_current_dir(&current_dir).map_err(|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 mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &toolchain];
for arg in &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(())
}