Skip to content

Commit

Permalink
Merge pull request rust-lang#405 from GuillaumeGomez/rustify-cargo-sh
Browse files Browse the repository at this point in the history
Rustify `cargo.sh`
  • Loading branch information
antoyo committed Dec 30, 2023
2 parents b2e0cc5 + c122376 commit a91d9e1
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/stdarch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ jobs:
if: ${{ !matrix.cargo_runner }}
run: |
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
if: ${{ matrix.cargo_runner }}
run: |
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.
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
10 changes: 5 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export CG_GCCJIT_DIR=[the full path to rustc_codegen_gcc]
### Cargo

```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.
Expand Down Expand Up @@ -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
```
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
```
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
Expand Down Expand Up @@ -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.
* 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`.
* 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).
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`
* 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:

Expand Down
114 changes: 114 additions & 0 deletions build_system/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use crate::config::ConfigInfo;
use crate::utils::{
get_toolchain, run_command_with_output_and_env_no_err, rustc_toolchain_version_info,
rustc_version_info,
};

use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::PathBuf;

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()
.and_then(|path| path.canonicalize())
.map_err(|error| format!("Failed to get current directory path: {:?}", error))?;
let current_exe = std::env::current_exe()
.and_then(|path| path.canonicalize())
.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<_>>();
// 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"] {
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();
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);
}
if run_command_with_output_and_env_no_err(&command, None, Some(&env)).is_err() {
std::process::exit(1);
}

Ok(())
}
5 changes: 5 additions & 0 deletions build_system/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::env;
use std::process;

mod build;
mod cargo;
mod clean;
mod config;
mod prepare;
Expand All @@ -23,6 +24,7 @@ fn usage() {
"\
Available commands for build_system:
cargo : Run cargo command
clean : Run clean command
prepare : Run prepare command
build : Run build command
Expand All @@ -32,6 +34,7 @@ Available commands for build_system:
}

pub enum Command {
Cargo,
Clean,
Prepare,
Build,
Expand All @@ -44,6 +47,7 @@ fn main() {
}

let command = match env::args().nth(1).as_deref() {
Some("cargo") => Command::Cargo,
Some("clean") => Command::Clean,
Some("prepare") => Command::Prepare,
Some("build") => Command::Build,
Expand All @@ -61,6 +65,7 @@ fn main() {
};

if let Err(e) = match command {
Command::Cargo => cargo::run(),
Command::Clean => clean::run(),
Command::Prepare => prepare::run(),
Command::Build => build::run(),
Expand Down
2 changes: 1 addition & 1 deletion build_system/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ fn extended_sysroot_tests(env: &Env, args: &TestArg) -> Result<(), String> {
// echo "[BENCH COMPILE] ebobby/simple-raytracer"
// hyperfine --runs "${RUN_RUNS:-10}" --warmup 1 --prepare "cargo clean" \
// "RUSTC=rustc RUSTFLAGS='' cargo build" \
// "../cargo.sh build"
// "../y.sh cargo build"

// echo "[BENCH RUN] ebobby/simple-raytracer"
// cp ./target/debug/main ./raytracer_cg_gcc
Expand Down
52 changes: 45 additions & 7 deletions build_system/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn check_exit_status(
cwd: Option<&Path>,
exit_status: ExitStatus,
output: Option<&Output>,
show_err: bool,
) -> Result<(), String> {
if exit_status.success() {
return Ok(());
Expand All @@ -46,7 +47,9 @@ fn check_exit_status(
exit_status.code()
);
let input = input.iter().map(|i| i.as_ref()).collect::<Vec<&OsStr>>();
eprintln!("Command `{:?}` failed", input);
if show_err {
eprintln!("Command `{:?}` failed", input);
}
if let Some(output) = output {
let stdout = String::from_utf8_lossy(&output.stdout);
if !stdout.is_empty() {
Expand Down Expand Up @@ -88,7 +91,7 @@ pub fn run_command_with_env(
let output = get_command_inner(input, cwd, env)
.output()
.map_err(|e| command_error(input, &cwd, e))?;
check_exit_status(input, cwd, output.status, Some(&output))?;
check_exit_status(input, cwd, output.status, Some(&output), true)?;
Ok(output)
}

Expand All @@ -101,7 +104,7 @@ pub fn run_command_with_output(
.map_err(|e| command_error(input, &cwd, e))?
.wait()
.map_err(|e| command_error(input, &cwd, e))?;
check_exit_status(input, cwd, exit_status, None)?;
check_exit_status(input, cwd, exit_status, None, true)?;
Ok(())
}

Expand All @@ -115,7 +118,21 @@ pub fn run_command_with_output_and_env(
.map_err(|e| command_error(input, &cwd, e))?
.wait()
.map_err(|e| command_error(input, &cwd, e))?;
check_exit_status(input, cwd, exit_status, None)?;
check_exit_status(input, cwd, exit_status, None, true)?;
Ok(())
}

pub fn run_command_with_output_and_env_no_err(
input: &[&dyn AsRef<OsStr>],
cwd: Option<&Path>,
env: Option<&HashMap<String, String>>,
) -> Result<(), String> {
let exit_status = get_command_inner(input, cwd, env)
.spawn()
.map_err(|e| command_error(input, &cwd, e))?
.wait()
.map_err(|e| command_error(input, &cwd, e))?;
check_exit_status(input, cwd, exit_status, None, false)?;
Ok(())
}

Expand Down Expand Up @@ -158,21 +175,42 @@ pub fn get_os_name() -> Result<String, String> {
}
}

#[derive(Default)]
#[derive(Default, PartialEq)]
pub struct RustcVersionInfo {
pub short: String,
pub version: String,
pub host: Option<String>,
pub commit_hash: 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> {
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 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(':') {
Some(("host", data)) => info.host = Some(data.trim().to_string()),
Some(("release", data)) => info.version = data.trim().to_string(),
Expand Down
23 changes: 0 additions & 23 deletions cargo.sh

This file was deleted.

6 changes: 3 additions & 3 deletions y.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e
echo "[BUILD] build system" 1>&2
cd build_system
pushd $(dirname "$0")/build_system > /dev/null
cargo build --release
cd ..
./build_system/target/release/y $@
popd > /dev/null
$(dirname "$0")/build_system/target/release/y $@

0 comments on commit a91d9e1

Please sign in to comment.