Skip to content

Commit 2640b31

Browse files
Switch to config.toml instead of gcc-path
1 parent 357cae8 commit 2640b31

File tree

9 files changed

+173
-68
lines changed

9 files changed

+173
-68
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ tools/llvmint-2
2828
# The `llvm` folder is generated by the `tools/generate_intrinsics.py` script to update intrinsics.
2929
llvm
3030
build_system/target
31+
config.toml

Diff for: Readme.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,27 @@ $ make check-jit
4949
$ make check-jit RUNTESTFLAGS="-v -v -v jit.exp=jit.dg/test-asm.cc"
5050
```
5151

52-
**Put the path to your custom build of libgccjit in the file `gcc_path`.**
52+
**Put the path to your custom build of libgccjit in the file `config.toml`.**
53+
54+
If you followed the instructions exactly as written (ie, you have created a `gcc-build` folder
55+
where gcc is built), the only thing you need to do is:
56+
57+
```bash
58+
$ cp config.example.toml config.toml
59+
```
60+
61+
But if you did something different, you also need to set the `gcc-path` value in `config.toml` with
62+
the result of this command:
5363

5464
```bash
55-
$ dirname $(readlink -f `find . -name libgccjit.so`) > gcc_path
65+
$ dirname $(readlink -f `find . -name libgccjit.so`)
5666
```
5767

5868
Then you can run commands like this:
5969

6070
```bash
6171
$ ./y.sh prepare # download and patch sysroot src and install hyperfine for benchmarking
62-
$ LIBRARY_PATH=$(cat gcc_path) LD_LIBRARY_PATH=$(cat gcc_path) ./y.sh build --release --features master
72+
$ ./y.sh build --release --features master
6373
```
6474

6575
To run the tests:
@@ -100,7 +110,7 @@ error: failed to copy bitcode to object file: No such file or directory (os erro
100110
> You should prefer using the Cargo method.
101111
102112
```bash
103-
$ LIBRARY_PATH=$(cat gcc_path) LD_LIBRARY_PATH=$(cat gcc_path) rustc +$(cat $CG_GCCJIT_DIR/rust-toolchain | grep 'channel' | cut -d '=' -f 2 | sed 's/"//g' | sed 's/ //g') -Cpanic=abort -Zcodegen-backend=$CG_GCCJIT_DIR/target/release/librustc_codegen_gcc.so --sysroot $CG_GCCJIT_DIR/build_sysroot/sysroot my_crate.rs
113+
$ LIBRARY_PATH="[gcc-path value]" LD_LIBRARY_PATH="[gcc-path value]" rustc +$(cat $CG_GCCJIT_DIR/rust-toolchain | grep 'channel' | cut -d '=' -f 2 | sed 's/"//g' | sed 's/ //g') -Cpanic=abort -Zcodegen-backend=$CG_GCCJIT_DIR/target/release/librustc_codegen_gcc.so --sysroot $CG_GCCJIT_DIR/build_sysroot/sysroot my_crate.rs
104114
```
105115

106116
## Env vars
@@ -322,7 +332,7 @@ generate it in [gimple.md](./doc/gimple.md).
322332
#### Configuring rustc_codegen_gcc
323333

324334
* Run `./y.sh prepare --cross` so that the sysroot is patched for the cross-compiling case.
325-
* Set the path to the cross-compiling libgccjit in `gcc_path`.
335+
* Set the path to the cross-compiling libgccjit in `gcc-path` (in `config.toml`).
326336
* 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`.
327337
* Build your project by specifying the target: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../y.sh cargo build --target m68k-unknown-linux-gnu`.
328338

@@ -338,4 +348,4 @@ If you get the following error:
338348
/usr/bin/ld: unrecognised emulation mode: m68kelf
339349
```
340350

341-
Make sure you set `gcc_path` to the install directory.
351+
Make sure you set `gcc-path` (in `config.toml`) to the install directory.

Diff for: build_system/Cargo.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: build_system/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ name = "y"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[dependencies]
7+
boml = "0.3.1"
8+
69
[[bin]]
710
name = "y"
811
path = "src/main.rs"

Diff for: build_system/src/build.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::config::{Channel, ConfigInfo};
2-
use crate::utils::{get_gcc_path, run_command, run_command_with_output_and_env, walk_dir};
2+
use crate::utils::{run_command, run_command_with_output_and_env, walk_dir};
33
use std::collections::HashMap;
44
use std::ffi::OsStr;
55
use std::fs;
@@ -8,17 +8,12 @@ use std::path::Path;
88
#[derive(Default)]
99
struct BuildArg {
1010
flags: Vec<String>,
11-
gcc_path: String,
1211
config_info: ConfigInfo,
1312
}
1413

1514
impl BuildArg {
1615
fn new() -> Result<Option<Self>, String> {
17-
let gcc_path = get_gcc_path()?;
18-
let mut build_arg = Self {
19-
gcc_path,
20-
..Default::default()
21-
};
16+
let mut build_arg = Self::default();
2217
// We skip binary name and the `build` command.
2318
let mut args = std::env::args().skip(2);
2419

@@ -169,7 +164,8 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
169164
fs::create_dir_all(&sysroot_src_path).map_err(|error| {
170165
format!(
171166
"Failed to create directory `{}`: {:?}",
172-
sysroot_src_path.display(), error
167+
sysroot_src_path.display(),
168+
error
173169
)
174170
})?;
175171
run_command(
@@ -188,8 +184,14 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
188184
fn build_codegen(args: &mut BuildArg) -> Result<(), String> {
189185
let mut env = HashMap::new();
190186

191-
env.insert("LD_LIBRARY_PATH".to_string(), args.gcc_path.clone());
192-
env.insert("LIBRARY_PATH".to_string(), args.gcc_path.clone());
187+
env.insert(
188+
"LD_LIBRARY_PATH".to_string(),
189+
args.config_info.gcc_path.clone(),
190+
);
191+
env.insert(
192+
"LIBRARY_PATH".to_string(),
193+
args.config_info.gcc_path.clone(),
194+
);
193195

194196
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &"rustc"];
195197
if args.config_info.channel == Channel::Release {
@@ -205,7 +207,7 @@ fn build_codegen(args: &mut BuildArg) -> Result<(), String> {
205207
}
206208
run_command_with_output_and_env(&command, None, Some(&env))?;
207209

208-
args.config_info.setup(&mut env, Some(&args.gcc_path))?;
210+
args.config_info.setup(&mut env, None)?;
209211

210212
// We voluntarily ignore the error.
211213
let _ = fs::remove_dir_all("target/out");
@@ -227,6 +229,7 @@ pub fn run() -> Result<(), String> {
227229
Some(args) => args,
228230
None => return Ok(()),
229231
};
232+
args.config_info.setup_gcc_path(None)?;
230233
build_codegen(&mut args)?;
231234
Ok(())
232235
}

Diff for: build_system/src/config.rs

+105-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use crate::utils::{get_gcc_path, get_os_name, rustc_version_info, split_args};
1+
use crate::utils::{get_os_name, rustc_version_info, split_args};
22
use std::collections::HashMap;
33
use std::env as std_env;
44
use std::ffi::OsStr;
5+
use std::fs;
6+
use std::path::Path;
7+
8+
use boml::{types::TomlValue, Toml};
59

610
#[derive(Default, PartialEq, Eq, Clone, Copy, Debug)]
711
pub enum Channel {
@@ -19,6 +23,72 @@ impl Channel {
1923
}
2024
}
2125

26+
fn failed_config_parsing(err: &str) -> Result<ConfigFile, String> {
27+
Err(format!(
28+
"Failed to parse `{}`: {}",
29+
ConfigFile::CONFIG_FILE,
30+
err
31+
))
32+
}
33+
34+
#[derive(Default)]
35+
pub struct ConfigFile {
36+
gcc_path: Option<String>,
37+
download_gccjit: Option<bool>,
38+
}
39+
40+
impl ConfigFile {
41+
pub const CONFIG_FILE: &'static str = "config.toml";
42+
43+
pub fn new() -> Result<Self, String> {
44+
let content = fs::read_to_string(Self::CONFIG_FILE).map_err(|_| {
45+
format!(
46+
"Failed to read `{}`. Take a look at `Readme.md` to see how to set up the project",
47+
Self::CONFIG_FILE,
48+
)
49+
})?;
50+
let toml = Toml::parse(&content).map_err(|err| {
51+
format!(
52+
"Error occurred around `{}`: {:?}",
53+
&content[err.start..=err.end],
54+
err.kind
55+
)
56+
})?;
57+
let mut config = Self::default();
58+
for (key, value) in toml.iter() {
59+
match (key, value) {
60+
("gcc-path", TomlValue::String(value)) => {
61+
config.gcc_path = Some(value.as_str().to_string())
62+
}
63+
("gcc-path", _) => {
64+
return failed_config_parsing("Expected a string for `gcc-path`")
65+
}
66+
("download-gccjit", TomlValue::Boolean(value)) => {
67+
config.download_gccjit = Some(*value)
68+
}
69+
("download-gccjit", _) => {
70+
return failed_config_parsing("Expected a boolean for `download-gccjit`")
71+
}
72+
_ => return failed_config_parsing(&format!("Unknown key `{}`", key)),
73+
}
74+
}
75+
if config.gcc_path.is_none() && config.download_gccjit.is_none() {
76+
return failed_config_parsing(
77+
"At least one of `gcc-path` or `download-gccjit` value must be set",
78+
);
79+
}
80+
if let Some(gcc_path) = config.gcc_path.as_mut() {
81+
let path = Path::new(gcc_path);
82+
*gcc_path = path
83+
.canonicalize()
84+
.map_err(|err| format!("Failed to get absolute path of `{}`: {:?}", gcc_path, err))?
85+
.display()
86+
.to_string();
87+
}
88+
Ok(config)
89+
}
90+
}
91+
2292
#[derive(Default, Debug)]
2393
pub struct ConfigInfo {
2494
pub target: String,
@@ -33,6 +103,7 @@ pub struct ConfigInfo {
33103
pub sysroot_panic_abort: bool,
34104
pub cg_backend_path: String,
35105
pub sysroot_path: String,
106+
pub gcc_path: String,
36107
}
37108

38109
impl ConfigInfo {
@@ -80,18 +151,43 @@ impl ConfigInfo {
80151
command
81152
}
82153

154+
pub fn setup_gcc_path(&mut self, override_gcc_path: Option<&str>) -> Result<(), String> {
155+
let ConfigFile { gcc_path, .. } = ConfigFile::new()?;
156+
157+
self.gcc_path = match override_gcc_path {
158+
Some(path) => {
159+
if gcc_path.is_some() {
160+
println!("overriding setting from `{}`", ConfigFile::CONFIG_FILE);
161+
}
162+
path.to_string()
163+
}
164+
None => {
165+
match gcc_path {
166+
Some(path) => path,
167+
// FIXME: Once we support "download", rewrite this.
168+
None => {
169+
return Err(format!(
170+
"missing `gcc-path` value from `{}`",
171+
ConfigFile::CONFIG_FILE
172+
))
173+
}
174+
}
175+
}
176+
};
177+
Ok(())
178+
}
179+
83180
pub fn setup(
84181
&mut self,
85182
env: &mut HashMap<String, String>,
86-
gcc_path: Option<&str>,
183+
override_gcc_path: Option<&str>,
87184
) -> Result<(), String> {
88185
env.insert("CARGO_INCREMENTAL".to_string(), "0".to_string());
89186

90-
let gcc_path = match gcc_path {
91-
Some(path) => path.to_string(),
92-
None => get_gcc_path()?,
93-
};
94-
env.insert("GCC_PATH".to_string(), gcc_path.clone());
187+
if self.gcc_path.is_empty() || override_gcc_path.is_some() {
188+
self.setup_gcc_path(override_gcc_path)?;
189+
}
190+
env.insert("GCC_PATH".to_string(), self.gcc_path.clone());
95191

96192
if self.cargo_target_dir.is_empty() {
97193
match env.get("CARGO_TARGET_DIR").filter(|dir| !dir.is_empty()) {
@@ -225,7 +321,9 @@ impl ConfigInfo {
225321
// line option to change it.
226322
target = current_dir.join("target/out").display(),
227323
sysroot = sysroot.display(),
324+
gcc_path = self.gcc_path,
228325
);
326+
env.insert("LIBRARY_PATH".to_string(), ld_library_path.clone());
229327
env.insert("LD_LIBRARY_PATH".to_string(), ld_library_path.clone());
230328
env.insert("DYLD_LIBRARY_PATH".to_string(), ld_library_path);
231329

Diff for: build_system/src/test.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::build;
22
use crate::config::{Channel, ConfigInfo};
33
use crate::utils::{
4-
get_gcc_path, get_toolchain, git_clone, remove_file, run_command, run_command_with_env,
4+
get_toolchain, git_clone, remove_file, run_command, run_command_with_env,
55
run_command_with_output_and_env, rustc_version_info, split_args, walk_dir,
66
};
77

@@ -109,7 +109,7 @@ fn show_usage() {
109109
struct TestArg {
110110
no_default_features: bool,
111111
build_only: bool,
112-
gcc_path: String,
112+
gcc_path: Option<String>,
113113
runners: BTreeSet<String>,
114114
flags: Vec<String>,
115115
backend: Option<String>,
@@ -181,12 +181,10 @@ impl TestArg {
181181
}
182182
}
183183

184-
test_arg.gcc_path = if use_system_gcc {
184+
if use_system_gcc {
185185
println!("Using system GCC");
186-
"gcc".to_string()
187-
} else {
188-
get_gcc_path()?
189-
};
186+
test_arg.gcc_path = Some("gcc".to_string());
187+
}
190188
}
191189
match (test_arg.current_part, test_arg.nb_parts) {
192190
(Some(_), Some(_)) | (None, None) => {}
@@ -488,7 +486,8 @@ fn std_tests(env: &Env, args: &TestArg) -> Result<(), String> {
488486
}
489487

490488
fn setup_rustc(env: &mut Env, args: &TestArg) -> Result<(), String> {
491-
let toolchain = format!("+{channel}-{host}",
489+
let toolchain = format!(
490+
"+{channel}-{host}",
492491
channel = get_toolchain()?, // May also include date
493492
host = args.config_info.host_triple
494493
);
@@ -527,7 +526,12 @@ fn setup_rustc(env: &mut Env, args: &TestArg) -> Result<(), String> {
527526
}
528527
})?;
529528
let rustc = String::from_utf8(
530-
run_command_with_env(&[&"rustup", &toolchain, &"which", &"rustc"], rust_dir, Some(env))?.stdout,
529+
run_command_with_env(
530+
&[&"rustup", &toolchain, &"which", &"rustc"],
531+
rust_dir,
532+
Some(env),
533+
)?
534+
.stdout,
531535
)
532536
.map_err(|error| format!("Failed to retrieve rustc path: {:?}", error))
533537
.and_then(|rustc| {
@@ -1162,16 +1166,23 @@ pub fn run() -> Result<(), String> {
11621166
};
11631167
let mut env: HashMap<String, String> = std::env::vars().collect();
11641168

1165-
env.insert("LD_LIBRARY_PATH".to_string(), args.gcc_path.clone());
1166-
env.insert("LIBRARY_PATH".to_string(), args.gcc_path.clone());
1169+
args.config_info.setup_gcc_path(None)?;
1170+
env.insert(
1171+
"LIBRARY_PATH".to_string(),
1172+
args.config_info.gcc_path.clone(),
1173+
);
1174+
env.insert(
1175+
"LD_LIBRARY_PATH".to_string(),
1176+
args.config_info.gcc_path.clone(),
1177+
);
11671178

11681179
build_if_no_backend(&env, &args)?;
11691180
if args.build_only {
11701181
println!("Since it's build only, exiting...");
11711182
return Ok(());
11721183
}
11731184

1174-
args.config_info.setup(&mut env, Some(&args.gcc_path))?;
1185+
args.config_info.setup(&mut env, args.gcc_path.as_deref())?;
11751186

11761187
if args.runners.is_empty() {
11771188
run_all(&env, &args)?;

0 commit comments

Comments
 (0)