Skip to content

Commit 32785c6

Browse files
Allow to specify the channel for the rustc command
1 parent ccaf719 commit 32785c6

5 files changed

Lines changed: 40 additions & 19 deletions

File tree

Readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,13 @@ If you compiled cg_gccjit in debug mode (aka you didn't pass `--release` to `./y
146146
If you want to run `rustc` directly, you can do so with:
147147

148148
```bash
149-
$ ./y.sh rustc my_crate.rs
149+
$ ./y.sh rustc --release -- my_crate.rs
150+
```
151+
152+
All arguments after `--` are sent the underlying `rustc` binary. If you want to compile in "debug" mode, remove the `--release` argument:
153+
154+
```bash
155+
$ ./y.sh rustc -- my_crate.rs
150156
```
151157

152158
You can do the same manually (although we don't recommend it):

build_system/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn build_codegen(args: &mut BuildArg) -> Result<(), String> {
227227
}
228228
run_command_with_output_and_env(&command, None, Some(&env))?;
229229

230-
args.config_info.setup(&mut env, false)?;
230+
args.config_info.setup(&mut env, false, true)?;
231231

232232
// We voluntarily ignore the error.
233233
let _ = fs::remove_dir_all("target/out");

build_system/src/config.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ impl ConfigInfo {
314314
&mut self,
315315
env: &mut HashMap<String, String>,
316316
use_system_gcc: bool,
317+
generate_out_dir: bool,
317318
) -> Result<(), String> {
318319
env.insert("CARGO_INCREMENTAL".to_string(), "0".to_string());
319320

@@ -444,12 +445,12 @@ impl ConfigInfo {
444445

445446
self.rustc_command = vec![rustc];
446447
self.rustc_command.extend_from_slice(&rustflags);
447-
self.rustc_command.extend_from_slice(&[
448-
"-L".to_string(),
449-
format!("crate={}", self.cargo_target_dir),
450-
"--out-dir".to_string(),
451-
self.cargo_target_dir.clone(),
452-
]);
448+
self.rustc_command
449+
.extend_from_slice(&["-L".to_string(), format!("crate={}", self.cargo_target_dir)]);
450+
if generate_out_dir {
451+
self.rustc_command
452+
.extend_from_slice(&["--out-dir".to_string(), self.cargo_target_dir.clone()]);
453+
}
453454

454455
if !env.contains_key("RUSTC_LOG") {
455456
env.insert("RUSTC_LOG".to_string(), "warn".to_string());

build_system/src/rust_tools.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,34 @@ use std::path::PathBuf;
77
use crate::config::ConfigInfo;
88
use crate::utils::{get_toolchain, rustc_toolchain_version_info, rustc_version_info};
99

10-
fn args(command: &str) -> Result<Option<Vec<String>>, String> {
10+
fn args(
11+
command: &str,
12+
expect_dash_dash: bool,
13+
) -> Result<Option<(ConfigInfo, Vec<String>)>, String> {
1114
// We skip the binary and the "cargo"/"rustc" option.
1215
if let Some("--help") = std::env::args().nth(2).as_deref() {
1316
usage(command);
1417
return Ok(None);
1518
}
16-
let args = std::env::args().skip(2).collect::<Vec<_>>();
19+
let mut config = ConfigInfo::default();
20+
let mut args = std::env::args().skip(2);
21+
if expect_dash_dash {
22+
while let Some(arg) = args.next() {
23+
if arg == "--" {
24+
break;
25+
}
26+
if let Ok(false) = config.parse_argument(&arg, &mut args) {
27+
return Err(format!("Unknown config option {arg:?}"));
28+
}
29+
}
30+
}
31+
let args = args.collect::<Vec<_>>();
1732
if args.is_empty() {
1833
return Err(format!(
19-
"Expected at least one argument for `{command}` subcommand, found none"
34+
"Expected at least one argument for `{command}` subcommand (after `--`), found none"
2035
));
2136
}
22-
Ok(Some(args))
37+
Ok(Some((config, args)))
2338
}
2439

2540
fn usage(command: &str) {
@@ -41,8 +56,8 @@ struct RustcTools {
4156
}
4257

4358
impl RustcTools {
44-
fn new(command: &str) -> Result<Option<Self>, String> {
45-
let Some(args) = args(command)? else { return Ok(None) };
59+
fn new(command: &str, expect_dash_dash: bool) -> Result<Option<Self>, String> {
60+
let Some((mut config, args)) = args(command, expect_dash_dash)? else { return Ok(None) };
4661

4762
// We first need to go to the original location to ensure that the config setup will go as
4863
// expected.
@@ -71,8 +86,7 @@ impl RustcTools {
7186
})?;
7287

7388
let mut env: HashMap<String, String> = std::env::vars().collect();
74-
let mut config = ConfigInfo::default();
75-
config.setup(&mut env, false)?;
89+
config.setup(&mut env, false, false)?;
7690
let toolchain = get_toolchain()?;
7791

7892
let toolchain_version = rustc_toolchain_version_info(&toolchain)?;
@@ -115,7 +129,7 @@ fn exec(input: &[&dyn AsRef<OsStr>], env: &HashMap<String, String>) -> Result<()
115129
}
116130

117131
pub fn run_cargo() -> Result<(), String> {
118-
let Some(mut tools) = RustcTools::new("cargo")? else { return Ok(()) };
132+
let Some(mut tools) = RustcTools::new("cargo", false)? else { return Ok(()) };
119133
let rustflags = tools.env.get("RUSTFLAGS").cloned().unwrap_or_default();
120134
tools.env.insert("RUSTDOCFLAGS".to_string(), rustflags);
121135
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &tools.toolchain];
@@ -126,7 +140,7 @@ pub fn run_cargo() -> Result<(), String> {
126140
}
127141

128142
pub fn run_rustc() -> Result<(), String> {
129-
let Some(tools) = RustcTools::new("rustc")? else { return Ok(()) };
143+
let Some(tools) = RustcTools::new("rustc", true)? else { return Ok(()) };
130144
let mut command = tools.config.rustc_command_vec();
131145
for arg in &tools.args {
132146
command.push(arg);

build_system/src/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ pub fn run() -> Result<(), String> {
13581358
return Ok(());
13591359
}
13601360

1361-
args.config_info.setup(&mut env, args.use_system_gcc)?;
1361+
args.config_info.setup(&mut env, args.use_system_gcc, true)?;
13621362

13631363
if args.runners.is_empty() {
13641364
run_all(&env, &args)?;

0 commit comments

Comments
 (0)