Skip to content

Commit 12228d1

Browse files
bors[bot]xFrednet
andauthored
Merge #125
125: Small maintainance improvements in cargo and rustc r=Niki4tap a=xFrednet This PR does some cleanup around the rustc driver and cargo. The commits should structure the changes pretty well. --- I took a break after my exam week. It has been pretty good, but now I want and need to get back into marker. I hope that this PR is just the start of it :) --- r? `@Niki4tap` Would you still have time to review my PRs? 🙃 Co-authored-by: xFrednet <xFrednet@gmail.com>
2 parents bf43d45 + a7e4ea1 commit 12228d1

8 files changed

Lines changed: 120 additions & 158 deletions

File tree

.cargo/config.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[alias]
2-
dogfood = "run --features dev-build --bin cargo-marker"
2+
dogfood = "run --features dev-build --bin cargo-marker -- --forward-rust-flags"
33
uitest = "test --test compile_test"
4+
5+
[profile.dev]
6+
split-debuginfo = "unpacked"

cargo-marker/src/cli.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{builder::ValueParser, Arg, ArgAction, Command};
1+
use clap::{builder::ValueParser, Arg, ArgAction, ArgMatches, Command};
22

33
use crate::VERSION;
44

@@ -10,6 +10,25 @@ EXAMPLES:
1010
* `cargo marker -l ./marker_lints`
1111
"#;
1212

13+
#[allow(clippy::struct_excessive_bools)]
14+
pub struct Flags {
15+
pub verbose: bool,
16+
pub test_build: bool,
17+
pub dev_build: bool,
18+
pub forward_rust_flags: bool,
19+
}
20+
21+
impl Flags {
22+
pub fn from_args(args: &ArgMatches) -> Self {
23+
Self {
24+
verbose: args.get_flag("verbose"),
25+
test_build: args.get_flag("test-setup"),
26+
dev_build: cfg!(feature = "dev-build"),
27+
forward_rust_flags: args.get_flag("forward-rust-flags"),
28+
}
29+
}
30+
}
31+
1332
pub fn get_clap_config() -> Command {
1433
Command::new(VERSION)
1534
.arg(
@@ -32,6 +51,12 @@ pub fn get_clap_config() -> Command {
3251
.action(ArgAction::SetTrue)
3352
.help("This flag will compile the lint crate and print all relevant environment values"),
3453
)
54+
.arg(
55+
Arg::new("forward-rust-flags")
56+
.long("forward-rust-flags")
57+
.action(ArgAction::SetTrue)
58+
.help("Forwards the current `RUSTFLAGS` value during driver and lint compilation"),
59+
)
3560
.subcommand(setup_command())
3661
.subcommand(check_command())
3762
.args(check_command_args())

cargo-marker/src/driver.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{ffi::OsString, path::PathBuf, process::Command};
1616

1717
use once_cell::sync::Lazy;
1818

19-
use crate::ExitStatus;
19+
use crate::{cli::Flags, ExitStatus};
2020

2121
/// This is the driver version and toolchain, that is used by the setup command
2222
/// to install the driver.
@@ -41,7 +41,7 @@ pub fn print_driver_version() {
4141
}
4242

4343
/// This tries to install the rustc driver specified in [`DEFAULT_DRIVER_INFO`].
44-
pub fn install_driver(verbose: bool, dev_build: bool) -> Result<(), ExitStatus> {
44+
pub fn install_driver(flags: &Flags) -> Result<(), ExitStatus> {
4545
// The toolchain, driver version and api version should ideally be configurable.
4646
// However, that will require more prototyping and has a low priority rn.
4747
// See #60
@@ -50,10 +50,10 @@ pub fn install_driver(verbose: bool, dev_build: bool) -> Result<(), ExitStatus>
5050
let toolchain = &DEFAULT_DRIVER_INFO.toolchain;
5151
check_toolchain(toolchain)?;
5252

53-
build_driver(toolchain, &DEFAULT_DRIVER_INFO.version, verbose, dev_build)?;
53+
build_driver(toolchain, &DEFAULT_DRIVER_INFO.version, flags)?;
5454

5555
// We don't want to advice the user, to install the driver again.
56-
check_driver(verbose, false)
56+
check_driver(flags.verbose, false)
5757
}
5858

5959
/// This function checks if the specified toolchain is installed. This requires
@@ -73,8 +73,8 @@ fn check_toolchain(toolchain: &str) -> Result<(), ExitStatus> {
7373

7474
/// This tries to compile the driver. If successful the driver binary will
7575
/// be places next to the executable of `cargo-linter`.
76-
fn build_driver(toolchain: &str, version: &str, verbose: bool, dev_build: bool) -> Result<(), ExitStatus> {
77-
if dev_build {
76+
fn build_driver(toolchain: &str, version: &str, flags: &Flags) -> Result<(), ExitStatus> {
77+
if flags.dev_build {
7878
println!("Compiling rustc driver");
7979
} else {
8080
println!("Compiling rustc driver v{version} with {toolchain}");
@@ -83,15 +83,21 @@ fn build_driver(toolchain: &str, version: &str, verbose: bool, dev_build: bool)
8383
// Build driver
8484
let mut cmd = Command::new("cargo");
8585

86-
if !dev_build {
86+
if !flags.dev_build {
8787
cmd.arg(&format!("+{toolchain}"));
8888
}
8989

90-
if verbose {
90+
if flags.verbose {
9191
cmd.arg("--verbose");
9292
}
9393

94-
if dev_build {
94+
let mut rustc_flags = if flags.forward_rust_flags {
95+
std::env::var("RUSTFLAGS").unwrap_or_default()
96+
} else {
97+
String::new()
98+
};
99+
100+
if flags.dev_build {
95101
cmd.args(["build", "--bin", "marker_driver_rustc"]);
96102
} else {
97103
// FIXME: This currently installs the binary in Cargo's default location.
@@ -105,8 +111,11 @@ fn build_driver(toolchain: &str, version: &str, verbose: bool, dev_build: bool)
105111
//
106112
// See #60
107113
cmd.args(["install", "marker_rustc_driver", "--version", version]);
114+
rustc_flags += " --cap-lints=allow";
108115
}
116+
cmd.env("RUSTFLAGS", rustc_flags);
109117

118+
// `RUSTFLAGS` is never set for driver compilation, we might want to
110119
let status = cmd
111120
.spawn()
112121
.expect("unable to start cargo install for the driver")

cargo-marker/src/lints.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
process::Command,
55
};
66

7-
use crate::ExitStatus;
7+
use crate::{cli::Flags, ExitStatus};
88

99
pub struct LintCrateSpec<'a> {
1010
/// Optional package name (this is always UTF-8, as opposed to `dir`), exists if supplied from
@@ -42,23 +42,30 @@ impl<'a> LintCrateSpec<'a> {
4242

4343
/// Creates a debug build for this crate. The path of the build library
4444
/// will be returned, if the operation was successful.
45-
pub fn build(&self, target_dir: &Path, verbose: bool) -> Result<PathBuf, ExitStatus> {
46-
build_local_lint_crate(self, target_dir, verbose)
45+
pub fn build(&self, target_dir: &Path, flags: &Flags) -> Result<PathBuf, ExitStatus> {
46+
build_local_lint_crate(self, target_dir, flags)
4747
}
4848
}
4949

5050
/// This creates a debug build for a local crate. The path of the build library
5151
/// will be returned, if the operation was successful.
52-
fn build_local_lint_crate(krate: &LintCrateSpec<'_>, target_dir: &Path, verbose: bool) -> Result<PathBuf, ExitStatus> {
52+
fn build_local_lint_crate(krate: &LintCrateSpec<'_>, target_dir: &Path, flags: &Flags) -> Result<PathBuf, ExitStatus> {
5353
if !krate.dir.exists() {
5454
eprintln!("The given lint can't be found, searched at: `{}`", krate.dir.display());
5555
return Err(ExitStatus::LintCrateNotFound);
5656
}
5757

58+
let mut rustc_flags = if flags.forward_rust_flags {
59+
std::env::var("RUSTFLAGS").unwrap_or_default()
60+
} else {
61+
String::new()
62+
};
63+
rustc_flags += " --cap-lints=allow";
64+
5865
// Compile the lint crate
5966
let mut cmd = Command::new("cargo");
6067
cmd.arg("build");
61-
if verbose {
68+
if flags.verbose {
6269
cmd.arg("--verbose");
6370
}
6471
if let Some(name) = krate.package_name {
@@ -69,7 +76,7 @@ fn build_local_lint_crate(krate: &LintCrateSpec<'_>, target_dir: &Path, verbose:
6976
.current_dir(std::fs::canonicalize(krate.dir).unwrap())
7077
.args(["--lib", "--target-dir"])
7178
.arg(target_dir.as_os_str())
72-
.env("RUSTFLAGS", "--cap-lints=allow")
79+
.env("RUSTFLAGS", rustc_flags)
7380
.spawn()
7481
.expect("could not run cargo")
7582
.wait()

cargo-marker/src/main.rs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{
1515
process::exit,
1616
};
1717

18-
use cli::get_clap_config;
18+
use cli::{get_clap_config, Flags};
1919
use config::Config;
2020
use driver::{get_driver_path, run_driver};
2121
use lints::LintCrateSpec;
@@ -109,12 +109,10 @@ fn main() -> Result<(), ExitStatus> {
109109
.take_while(|s| s != CARGO_ARGS_SEPARATOR),
110110
);
111111

112-
let verbose = matches.get_flag("verbose");
113-
let test_build = matches.get_flag("test-setup");
114-
let dev_build = cfg!(feature = "dev-build");
112+
let flags = Flags::from_args(&matches);
115113

116114
if matches.get_flag("version") {
117-
print_version(verbose);
115+
print_version(flags.verbose);
118116
return Ok(());
119117
}
120118

@@ -127,32 +125,17 @@ fn main() -> Result<(), ExitStatus> {
127125
};
128126

129127
match matches.subcommand() {
130-
Some(("setup", _args)) => driver::install_driver(verbose, dev_build),
131-
Some(("check", args)) => run_check(
132-
&choose_lint_crates(args, config.as_ref())?,
133-
verbose,
134-
dev_build,
135-
test_build,
136-
),
137-
None => run_check(
138-
&choose_lint_crates(&matches, config.as_ref())?,
139-
verbose,
140-
dev_build,
141-
test_build,
142-
),
128+
Some(("setup", _args)) => driver::install_driver(&flags),
129+
Some(("check", args)) => run_check(&choose_lint_crates(args, config.as_ref())?, &flags),
130+
None => run_check(&choose_lint_crates(&matches, config.as_ref())?, &flags),
143131
_ => unreachable!(),
144132
}
145133
}
146134

147-
fn run_check(
148-
crate_entries: &[LintCrateSpec],
149-
verbose: bool,
150-
dev_build: bool,
151-
test_build: bool,
152-
) -> Result<(), ExitStatus> {
135+
fn run_check(crate_entries: &[LintCrateSpec], flags: &Flags) -> Result<(), ExitStatus> {
153136
// If this is a dev build, we want to recompile the driver before checking
154-
if dev_build {
155-
driver::install_driver(verbose, dev_build)?;
137+
if flags.dev_build {
138+
driver::install_driver(flags)?;
156139
}
157140

158141
if crate_entries.is_empty() {
@@ -171,7 +154,7 @@ fn run_check(
171154
println!("Compiling Lints:");
172155
let target_dir = Path::new(&*MARKER_LINT_DIR);
173156
for krate in crate_entries {
174-
let crate_file = krate.build(target_dir, verbose)?;
157+
let crate_file = krate.build(target_dir, flags)?;
175158
lint_crates.push(crate_file.as_os_str().to_os_string());
176159
}
177160

@@ -180,12 +163,12 @@ fn run_check(
180163
(OsString::from("RUSTC_WORKSPACE_WRAPPER"), get_driver_path().as_os_str().to_os_string()),
181164
(OsString::from("MARKER_LINT_CRATES"), lint_crates.join(OsStr::new(";")))
182165
];
183-
if test_build {
166+
if flags.test_build {
184167
print_env(env).unwrap();
185168
Ok(())
186169
} else {
187170
let cargo_args = std::env::args().skip_while(|c| c != CARGO_ARGS_SEPARATOR).skip(1);
188-
run_driver(env, cargo_args, verbose)
171+
run_driver(env, cargo_args, flags.verbose)
189172
}
190173
}
191174

marker_driver_rustc/src/conversion/common.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
1-
#[repr(C)]
2-
pub struct GenericIdLayout {
3-
pub krate: u32,
4-
pub index: u32,
5-
}
6-
7-
#[repr(C)]
8-
pub struct TyDefIdLayout {
9-
pub krate: u32,
10-
pub index: u32,
11-
}
12-
131
#[repr(C)]
142
pub struct DefIdLayout {
153
pub krate: u32,
164
pub index: u32,
175
}
186

19-
#[repr(C)]
20-
pub struct ItemIdLayout {
21-
pub krate: u32,
22-
pub index: u32,
23-
}
24-
257
#[repr(C)]
268
pub struct BodyIdLayout {
279
// Note: AFAIK rustc only loads bodies from the current crate, this allows
@@ -39,12 +21,6 @@ pub struct DefIdInfo {
3921
pub krate: u32,
4022
}
4123

42-
#[repr(C)]
43-
pub struct ExprIdLayout {
44-
pub owner: u32,
45-
pub index: u32,
46-
}
47-
4824
#[repr(C)]
4925
pub struct HirIdLayout {
5026
pub owner: u32,
@@ -57,12 +33,6 @@ pub struct SpanSourceInfo {
5733
pub rustc_start_offset: usize,
5834
}
5935

60-
#[repr(C)]
61-
pub struct VarIdLayout {
62-
pub owner: u32,
63-
pub index: u32,
64-
}
65-
6636
#[macro_export]
6737
macro_rules! transmute_id {
6838
($t1:ty as $t2:ty = $e:expr) => {

0 commit comments

Comments
 (0)