Rustify cargo.sh

This commit is contained in:
Guillaume Gomez 2023-12-21 23:46:41 +01:00
parent b2e0cc5cec
commit 2e52b08800
7 changed files with 135 additions and 34 deletions

View File

@ -126,11 +126,11 @@ jobs:
if: ${{ !matrix.cargo_runner }} if: ${{ !matrix.cargo_runner }}
run: | run: |
cd build_sysroot/sysroot_src/library/stdarch/ cd build_sysroot/sysroot_src/library/stdarch/
CHANNEL=release TARGET=x86_64-unknown-linux-gnu CG_RUSTFLAGS="-Ainternal_features" ../../../../cargo.sh test CHANNEL=release TARGET=x86_64-unknown-linux-gnu CG_RUSTFLAGS="-Ainternal_features" ../../../../y.sh cargo test
- name: Run stdarch tests - name: Run stdarch tests
if: ${{ matrix.cargo_runner }} if: ${{ matrix.cargo_runner }}
run: | run: |
cd build_sysroot/sysroot_src/library/stdarch/ cd build_sysroot/sysroot_src/library/stdarch/
# FIXME: these tests fail when the sysroot is compiled with LTO because of a missing symbol in proc-macro. # FIXME: these tests fail when the sysroot is compiled with LTO because of a missing symbol in proc-macro.
STDARCH_TEST_EVERYTHING=1 CHANNEL=release CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="${{ matrix.cargo_runner }}" TARGET=x86_64-unknown-linux-gnu CG_RUSTFLAGS="-Ainternal_features" ../../../../cargo.sh test -- --skip rtm --skip tbm --skip sse4a STDARCH_TEST_EVERYTHING=1 CHANNEL=release CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="${{ matrix.cargo_runner }}" TARGET=x86_64-unknown-linux-gnu CG_RUSTFLAGS="-Ainternal_features" ../../../../y.sh cargo test -- --skip rtm --skip tbm --skip sse4a

View File

@ -79,7 +79,7 @@ export CG_GCCJIT_DIR=[the full path to rustc_codegen_gcc]
### Cargo ### Cargo
```bash ```bash
$ CHANNEL="release" $CG_GCCJIT_DIR/cargo.sh run $ CHANNEL="release" $CG_GCCJIT_DIR/y.sh cargo run
``` ```
If you compiled cg_gccjit in debug mode (aka you didn't pass `--release` to `./y.sh test`) you should use `CHANNEL="debug"` instead or omit `CHANNEL="release"` completely. If you compiled cg_gccjit in debug mode (aka you didn't pass `--release` to `./y.sh test`) you should use `CHANNEL="debug"` instead or omit `CHANNEL="release"` completely.
@ -230,13 +230,13 @@ Run do the command with `-v -save-temps` and then extract the `lto1` line from t
### How to send arguments to the GCC linker ### How to send arguments to the GCC linker
``` ```
CG_RUSTFLAGS="-Clink-args=-save-temps -v" ../cargo.sh build CG_RUSTFLAGS="-Clink-args=-save-temps -v" ../y.sh cargo build
``` ```
### How to see the personality functions in the asm dump ### How to see the personality functions in the asm dump
``` ```
CG_RUSTFLAGS="-Clink-arg=-save-temps -v -Clink-arg=-dA" ../cargo.sh build CG_RUSTFLAGS="-Clink-arg=-save-temps -v -Clink-arg=-dA" ../y.sh cargo build
``` ```
### How to see the LLVM IR for a sysroot crate ### How to see the LLVM IR for a sysroot crate
@ -324,13 +324,13 @@ generate it in [gimple.md](./doc/gimple.md).
* Run `./y.sh prepare --cross` so that the sysroot is patched for the cross-compiling case. * Run `./y.sh prepare --cross` so that the sysroot is patched for the cross-compiling case.
* Set the path to the cross-compiling libgccjit in `gcc_path`. * Set the path to the cross-compiling libgccjit in `gcc_path`.
* Make sure you have the linker for your target (for instance `m68k-unknown-linux-gnu-gcc`) in your `$PATH`. Currently, the linker name is hardcoded as being `$TARGET-gcc`. Specify the target when building the sysroot: `./y.sh build --target-triple m68k-unknown-linux-gnu`. * Make sure you have the linker for your target (for instance `m68k-unknown-linux-gnu-gcc`) in your `$PATH`. Currently, the linker name is hardcoded as being `$TARGET-gcc`. Specify the target when building the sysroot: `./y.sh build --target-triple m68k-unknown-linux-gnu`.
* Build your project by specifying the target: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../cargo.sh build --target m68k-unknown-linux-gnu`. * Build your project by specifying the target: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../y.sh cargo build --target m68k-unknown-linux-gnu`.
If the target is not yet supported by the Rust compiler, create a [target specification file](https://docs.rust-embedded.org/embedonomicon/custom-target.html) (note that the `arch` specified in this file must be supported by the rust compiler). If the target is not yet supported by the Rust compiler, create a [target specification file](https://docs.rust-embedded.org/embedonomicon/custom-target.html) (note that the `arch` specified in this file must be supported by the rust compiler).
Then, you can use it the following way: Then, you can use it the following way:
* Add the target specification file using `--target` as an **absolute** path to build the sysroot: `./y.sh build --target-triple m68k-unknown-linux-gnu --target $(pwd)/m68k-unknown-linux-gnu.json` * Add the target specification file using `--target` as an **absolute** path to build the sysroot: `./y.sh build --target-triple m68k-unknown-linux-gnu --target $(pwd)/m68k-unknown-linux-gnu.json`
* Build your project by specifying the target specification file: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../cargo.sh build --target path/to/m68k-unknown-linux-gnu.json`. * Build your project by specifying the target specification file: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../y.sh cargo build --target path/to/m68k-unknown-linux-gnu.json`.
If you get the following error: If you get the following error:

98
build_system/src/cargo.rs Normal file
View File

@ -0,0 +1,98 @@
use crate::config::ConfigInfo;
use crate::utils::{
get_toolchain, run_command_with_output_and_env, rustc_toolchain_version_info,
rustc_version_info,
};
use std::collections::HashMap;
use std::ffi::OsStr;
fn args() -> Result<Option<Vec<String>>, String> {
// We skip the binary and the "cargo" option.
if let Some("--help") = std::env::args().skip(2).next().as_deref() {
usage();
return Ok(None);
}
let args = std::env::args().skip(2).collect::<Vec<_>>();
if args.is_empty() {
return Err(
"Expected at least one argument for `cargo` subcommand, found none".to_string(),
);
}
Ok(Some(args))
}
fn usage() {
println!(
r#"
`cargo` command help:
[args] : Arguments to be passed to the cargo command
--help : Show this help
"#
)
}
pub fn run() -> Result<(), String> {
let args = match args()? {
Some(a) => a,
None => return Ok(()),
};
// We first need to go to the original location to ensure that the config setup will go as
// expected.
let current_dir = std::env::current_dir()
.map_err(|error| format!("Failed to get current directory path: {:?}", error))?;
let current_exe = std::env::current_exe()
.map_err(|error| format!("Failed to get current exe path: {:?}", error))?;
let parent_dir = match current_exe.parent() {
Some(parent) => parent,
None => {
return Err(format!(
"Cannot get parent of current executable path `{}`",
current_exe.display()
));
}
};
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();
ConfigInfo::default().setup(&mut env, None)?;
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 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);
}
run_command_with_output_and_env(&command, None, Some(&env))?;
Ok(())
}

View File

@ -2,6 +2,7 @@
use std::process; use std::process;
mod build; mod build;
mod cargo;
mod clean; mod clean;
mod config; mod config;
mod prepare; mod prepare;
@ -23,6 +24,7 @@ fn usage() {
"\ "\
Available commands for build_system: Available commands for build_system:
cargo : Run cargo 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
@ -32,6 +34,7 @@ fn usage() {
} }
pub enum Command { pub enum Command {
Cargo,
Clean, Clean,
Prepare, Prepare,
Build, Build,
@ -44,6 +47,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("clean") => Command::Clean, Some("clean") => Command::Clean,
Some("prepare") => Command::Prepare, Some("prepare") => Command::Prepare,
Some("build") => Command::Build, Some("build") => Command::Build,
@ -61,6 +65,7 @@ fn main() {
}; };
if let Err(e) = match command { if let Err(e) = match command {
Command::Cargo => cargo::run(),
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

@ -796,7 +796,7 @@ fn extended_sysroot_tests(env: &Env, args: &TestArg) -> Result<(), String> {
// echo "[BENCH COMPILE] ebobby/simple-raytracer" // echo "[BENCH COMPILE] ebobby/simple-raytracer"
// hyperfine --runs "${RUN_RUNS:-10}" --warmup 1 --prepare "cargo clean" \ // hyperfine --runs "${RUN_RUNS:-10}" --warmup 1 --prepare "cargo clean" \
// "RUSTC=rustc RUSTFLAGS='' cargo build" \ // "RUSTC=rustc RUSTFLAGS='' cargo build" \
// "../cargo.sh build" // "../y.sh cargo build"
// echo "[BENCH RUN] ebobby/simple-raytracer" // echo "[BENCH RUN] ebobby/simple-raytracer"
// cp ./target/debug/main ./raytracer_cg_gcc // cp ./target/debug/main ./raytracer_cg_gcc

View File

@ -158,21 +158,42 @@ pub fn get_os_name() -> Result<String, String> {
} }
} }
#[derive(Default)] #[derive(Default, PartialEq)]
pub struct RustcVersionInfo { pub struct RustcVersionInfo {
pub short: String,
pub version: String, pub version: String,
pub host: Option<String>, pub host: Option<String>,
pub commit_hash: Option<String>, pub commit_hash: Option<String>,
pub commit_date: Option<String>, pub commit_date: Option<String>,
} }
pub fn rustc_toolchain_version_info(toolchain: &str) -> Result<RustcVersionInfo, String> {
rustc_version_info_inner(None, Some(toolchain))
}
pub fn rustc_version_info(rustc: Option<&str>) -> Result<RustcVersionInfo, String> { pub fn rustc_version_info(rustc: Option<&str>) -> Result<RustcVersionInfo, String> {
let output = run_command(&[&rustc.unwrap_or("rustc"), &"-vV"], None)?; rustc_version_info_inner(rustc, None)
}
fn rustc_version_info_inner(
rustc: Option<&str>,
toolchain: Option<&str>,
) -> Result<RustcVersionInfo, String> {
let output = if let Some(toolchain) = toolchain {
run_command(&[&rustc.unwrap_or("rustc"), &toolchain, &"-vV"], None)
} else {
run_command(&[&rustc.unwrap_or("rustc"), &"-vV"], None)
}?;
let content = std::str::from_utf8(&output.stdout).unwrap_or(""); let content = std::str::from_utf8(&output.stdout).unwrap_or("");
let mut info = RustcVersionInfo::default(); let mut info = RustcVersionInfo::default();
let mut lines = content.split('\n');
info.short = match lines.next() {
Some(s) => s.to_string(),
None => return Err("failed to retrieve rustc version".to_string()),
};
for line in content.split('\n').map(|line| line.trim()) { for line in lines.map(|line| line.trim()) {
match line.split_once(':') { match line.split_once(':') {
Some(("host", data)) => info.host = Some(data.trim().to_string()), Some(("host", data)) => info.host = Some(data.trim().to_string()),
Some(("release", data)) => info.version = data.trim().to_string(), Some(("release", data)) => info.version = data.trim().to_string(),

View File

@ -1,23 +0,0 @@
#!/usr/bin/env bash
if [ -z $CHANNEL ]; then
export CHANNEL='debug'
fi
pushd $(dirname "$0") >/dev/null
source config.sh
# read nightly compiler from rust-toolchain file
TOOLCHAIN=$(cat rust-toolchain | grep channel | sed 's/channel = "\(.*\)"/\1/')
popd >/dev/null
if [[ $(${RUSTC} -V) != $(${RUSTC} +${TOOLCHAIN} -V) ]]; then
echo "rustc_codegen_gcc is build for $(rustc +${TOOLCHAIN} -V) but the default rustc version is $(rustc -V)."
echo "Using $(rustc +${TOOLCHAIN} -V)."
fi
cmd=$1
shift
RUSTDOCFLAGS="$RUSTFLAGS" cargo +${TOOLCHAIN} $cmd $@