Skip to content

Commit 2e52b08

Browse files
Rustify cargo.sh
1 parent b2e0cc5 commit 2e52b08

File tree

7 files changed

+135
-34
lines changed

7 files changed

+135
-34
lines changed

.github/workflows/stdarch.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ jobs:
126126
if: ${{ !matrix.cargo_runner }}
127127
run: |
128128
cd build_sysroot/sysroot_src/library/stdarch/
129-
CHANNEL=release TARGET=x86_64-unknown-linux-gnu CG_RUSTFLAGS="-Ainternal_features" ../../../../cargo.sh test
129+
CHANNEL=release TARGET=x86_64-unknown-linux-gnu CG_RUSTFLAGS="-Ainternal_features" ../../../../y.sh cargo test
130130
131131
- name: Run stdarch tests
132132
if: ${{ matrix.cargo_runner }}
133133
run: |
134134
cd build_sysroot/sysroot_src/library/stdarch/
135135
# FIXME: these tests fail when the sysroot is compiled with LTO because of a missing symbol in proc-macro.
136-
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
136+
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

Readme.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export CG_GCCJIT_DIR=[the full path to rustc_codegen_gcc]
7979
### Cargo
8080

8181
```bash
82-
$ CHANNEL="release" $CG_GCCJIT_DIR/cargo.sh run
82+
$ CHANNEL="release" $CG_GCCJIT_DIR/y.sh cargo run
8383
```
8484

8585
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
230230
### How to send arguments to the GCC linker
231231
232232
```
233-
CG_RUSTFLAGS="-Clink-args=-save-temps -v" ../cargo.sh build
233+
CG_RUSTFLAGS="-Clink-args=-save-temps -v" ../y.sh cargo build
234234
```
235235
236236
### How to see the personality functions in the asm dump
237237
238238
```
239-
CG_RUSTFLAGS="-Clink-arg=-save-temps -v -Clink-arg=-dA" ../cargo.sh build
239+
CG_RUSTFLAGS="-Clink-arg=-save-temps -v -Clink-arg=-dA" ../y.sh cargo build
240240
```
241241
242242
### How to see the LLVM IR for a sysroot crate
@@ -324,13 +324,13 @@ generate it in [gimple.md](./doc/gimple.md).
324324
* Run `./y.sh prepare --cross` so that the sysroot is patched for the cross-compiling case.
325325
* Set the path to the cross-compiling libgccjit in `gcc_path`.
326326
* 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`.
327-
* Build your project by specifying the target: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../cargo.sh build --target m68k-unknown-linux-gnu`.
327+
* Build your project by specifying the target: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../y.sh cargo build --target m68k-unknown-linux-gnu`.
328328

329329
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).
330330
Then, you can use it the following way:
331331

332332
* 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`
333-
* 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`.
333+
* 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`.
334334

335335
If you get the following error:
336336

build_system/src/cargo.rs

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use crate::config::ConfigInfo;
2+
use crate::utils::{
3+
get_toolchain, run_command_with_output_and_env, rustc_toolchain_version_info,
4+
rustc_version_info,
5+
};
6+
7+
use std::collections::HashMap;
8+
use std::ffi::OsStr;
9+
10+
fn args() -> Result<Option<Vec<String>>, String> {
11+
// We skip the binary and the "cargo" option.
12+
if let Some("--help") = std::env::args().skip(2).next().as_deref() {
13+
usage();
14+
return Ok(None);
15+
}
16+
let args = std::env::args().skip(2).collect::<Vec<_>>();
17+
if args.is_empty() {
18+
return Err(
19+
"Expected at least one argument for `cargo` subcommand, found none".to_string(),
20+
);
21+
}
22+
Ok(Some(args))
23+
}
24+
25+
fn usage() {
26+
println!(
27+
r#"
28+
`cargo` command help:
29+
30+
[args] : Arguments to be passed to the cargo command
31+
--help : Show this help
32+
"#
33+
)
34+
}
35+
36+
pub fn run() -> Result<(), String> {
37+
let args = match args()? {
38+
Some(a) => a,
39+
None => return Ok(()),
40+
};
41+
42+
// We first need to go to the original location to ensure that the config setup will go as
43+
// expected.
44+
let current_dir = std::env::current_dir()
45+
.map_err(|error| format!("Failed to get current directory path: {:?}", error))?;
46+
let current_exe = std::env::current_exe()
47+
.map_err(|error| format!("Failed to get current exe path: {:?}", error))?;
48+
let parent_dir = match current_exe.parent() {
49+
Some(parent) => parent,
50+
None => {
51+
return Err(format!(
52+
"Cannot get parent of current executable path `{}`",
53+
current_exe.display()
54+
));
55+
}
56+
};
57+
std::env::set_current_dir(&parent_dir).map_err(|error| {
58+
format!(
59+
"Failed to go to `{}` folder: {:?}",
60+
parent_dir.display(),
61+
error
62+
)
63+
})?;
64+
65+
let mut env: HashMap<String, String> = std::env::vars().collect();
66+
ConfigInfo::default().setup(&mut env, None)?;
67+
let toolchain = get_toolchain()?;
68+
69+
let toolchain_version = rustc_toolchain_version_info(&toolchain)?;
70+
let default_version = rustc_version_info(None)?;
71+
if toolchain_version != default_version {
72+
println!(
73+
"rustc_codegen_gcc is built for {} but the default rustc version is {}.",
74+
toolchain_version.short, default_version.short,
75+
);
76+
println!("Using {}.", toolchain_version.short);
77+
}
78+
79+
// We go back to the original folder since we now have set up everything we needed.
80+
std::env::set_current_dir(&current_dir).map_err(|error| {
81+
format!(
82+
"Failed to go back to `{}` folder: {:?}",
83+
current_dir.display(),
84+
error
85+
)
86+
})?;
87+
88+
let rustflags = env.get("RUSTFLAGS").cloned().unwrap_or_default();
89+
env.insert("RUSTDOCFLAGS".to_string(), rustflags);
90+
let toolchain = format!("+{}", toolchain);
91+
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &toolchain];
92+
for arg in &args {
93+
command.push(arg);
94+
}
95+
run_command_with_output_and_env(&command, None, Some(&env))?;
96+
97+
Ok(())
98+
}

build_system/src/main.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env;
22
use std::process;
33

44
mod build;
5+
mod cargo;
56
mod clean;
67
mod config;
78
mod prepare;
@@ -23,6 +24,7 @@ fn usage() {
2324
"\
2425
Available commands for build_system:
2526
27+
cargo : Run cargo command
2628
clean : Run clean command
2729
prepare : Run prepare command
2830
build : Run build command
@@ -32,6 +34,7 @@ Available commands for build_system:
3234
}
3335

3436
pub enum Command {
37+
Cargo,
3538
Clean,
3639
Prepare,
3740
Build,
@@ -44,6 +47,7 @@ fn main() {
4447
}
4548

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

6367
if let Err(e) = match command {
68+
Command::Cargo => cargo::run(),
6469
Command::Clean => clean::run(),
6570
Command::Prepare => prepare::run(),
6671
Command::Build => build::run(),

build_system/src/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ fn extended_sysroot_tests(env: &Env, args: &TestArg) -> Result<(), String> {
796796
// echo "[BENCH COMPILE] ebobby/simple-raytracer"
797797
// hyperfine --runs "${RUN_RUNS:-10}" --warmup 1 --prepare "cargo clean" \
798798
// "RUSTC=rustc RUSTFLAGS='' cargo build" \
799-
// "../cargo.sh build"
799+
// "../y.sh cargo build"
800800

801801
// echo "[BENCH RUN] ebobby/simple-raytracer"
802802
// cp ./target/debug/main ./raytracer_cg_gcc

build_system/src/utils.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,42 @@ pub fn get_os_name() -> Result<String, String> {
158158
}
159159
}
160160

161-
#[derive(Default)]
161+
#[derive(Default, PartialEq)]
162162
pub struct RustcVersionInfo {
163+
pub short: String,
163164
pub version: String,
164165
pub host: Option<String>,
165166
pub commit_hash: Option<String>,
166167
pub commit_date: Option<String>,
167168
}
168169

170+
pub fn rustc_toolchain_version_info(toolchain: &str) -> Result<RustcVersionInfo, String> {
171+
rustc_version_info_inner(None, Some(toolchain))
172+
}
173+
169174
pub fn rustc_version_info(rustc: Option<&str>) -> Result<RustcVersionInfo, String> {
170-
let output = run_command(&[&rustc.unwrap_or("rustc"), &"-vV"], None)?;
175+
rustc_version_info_inner(rustc, None)
176+
}
177+
178+
fn rustc_version_info_inner(
179+
rustc: Option<&str>,
180+
toolchain: Option<&str>,
181+
) -> Result<RustcVersionInfo, String> {
182+
let output = if let Some(toolchain) = toolchain {
183+
run_command(&[&rustc.unwrap_or("rustc"), &toolchain, &"-vV"], None)
184+
} else {
185+
run_command(&[&rustc.unwrap_or("rustc"), &"-vV"], None)
186+
}?;
171187
let content = std::str::from_utf8(&output.stdout).unwrap_or("");
172188

173189
let mut info = RustcVersionInfo::default();
190+
let mut lines = content.split('\n');
191+
info.short = match lines.next() {
192+
Some(s) => s.to_string(),
193+
None => return Err("failed to retrieve rustc version".to_string()),
194+
};
174195

175-
for line in content.split('\n').map(|line| line.trim()) {
196+
for line in lines.map(|line| line.trim()) {
176197
match line.split_once(':') {
177198
Some(("host", data)) => info.host = Some(data.trim().to_string()),
178199
Some(("release", data)) => info.version = data.trim().to_string(),

cargo.sh

-23
This file was deleted.

0 commit comments

Comments
 (0)