Skip to content

Commit 6ad3b4b

Browse files
committed
Rework rmake support library to have a weakly-typed API with helper methods
1 parent 4acb17e commit 6ad3b4b

File tree

14 files changed

+76
-209
lines changed

14 files changed

+76
-209
lines changed

src/tools/run-make-support/src/lib.rs

+22-129
Original file line numberDiff line numberDiff line change
@@ -59,62 +59,15 @@ impl Rustc {
5959
self
6060
}
6161

62-
/// Configure codegen options.
63-
pub fn codegen_opt(&mut self, c: CodegenOpt) -> &mut Self {
64-
self.cmd.arg("-C");
65-
66-
match c {
67-
CodegenOpt::PreferDynamic => self.cmd.arg("prefer-dynamic=true"),
68-
CodegenOpt::SymbolManglingVersion(v) => match v {
69-
SymbolManglingVersion::Legacy => self.cmd.arg("symbol-mangling-version=legacy"),
70-
SymbolManglingVersion::V0 => self.cmd.arg("symbol-mangling-version=v0"),
71-
},
72-
CodegenOpt::Lto(kind) => match kind {
73-
LtoKind::Fat => self.cmd.arg("lto=fat"),
74-
LtoKind::Thin => self.cmd.arg("lto=thin"),
75-
LtoKind::None => self.cmd.arg("lto=false"),
76-
},
77-
CodegenOpt::OptLevel(level) => match level {
78-
OptLevel::O0 => self.cmd.arg("opt-level=0"),
79-
OptLevel::O1 => self.cmd.arg("opt-level=1"),
80-
OptLevel::O2 => self.cmd.arg("opt-level=2"),
81-
OptLevel::O3 => self.cmd.arg("opt-level=3"),
82-
OptLevel::Os => self.cmd.arg("opt-level=s"),
83-
OptLevel::Oz => self.cmd.arg("opt-level=z"),
84-
},
85-
CodegenOpt::OverflowChecks => self.cmd.arg("overflow-checks=true"),
86-
CodegenOpt::Panic(strat) => match strat {
87-
PanicStrategy::Abort => self.cmd.arg("panic=abort"),
88-
PanicStrategy::Unwind => self.cmd.arg("panic=unwind"),
89-
},
90-
};
91-
92-
self
93-
}
94-
9562
/// Specify default optimization level `-O` (alias for `-C opt-level=2`).
96-
pub fn default_opt(&mut self) -> &mut Self {
63+
pub fn opt(&mut self) -> &mut Self {
9764
self.cmd.arg("-O");
9865
self
9966
}
10067

101-
/// Specify types of output files to generate. See [`EmitKind`] for kinds.
102-
pub fn emit(&mut self, kinds: &[EmitKind]) -> &mut Self {
103-
let kinds = kinds
104-
.iter()
105-
.map(|kind| match kind {
106-
EmitKind::Metadata => "metadata",
107-
})
108-
.collect::<Vec<_>>();
109-
let kinds_str: String = kinds.join(",");
110-
self.cmd.arg(format!("--emit={kinds_str}"));
111-
self
112-
}
113-
114-
/// Set `-Z unstable-options`
115-
pub fn enable_unstable_options(&mut self) -> &mut Self {
116-
self.cmd.arg("-Z");
117-
self.cmd.arg("unstable-options");
68+
/// Specify type(s) of output files to generate.
69+
pub fn emit(&mut self, kinds: &str) -> &mut Self {
70+
self.cmd.arg(format!("--emit={kinds}"));
11871
self
11972
}
12073

@@ -134,7 +87,7 @@ impl Rustc {
13487
}
13588

13689
/// Specify path to the input file.
137-
pub fn input_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
90+
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
13891
self.cmd.arg(path.as_ref());
13992
self
14093
}
@@ -146,18 +99,29 @@ impl Rustc {
14699
self
147100
}
148101

149-
// Last-resort builder methods
150-
151-
/// Fallback command argument provider. Prefer using semantically meaningful builder methods
152-
/// (add them if they don't exist) whenever possible.
102+
/// Generic command argument provider. Use `.arg("-Zname")` over `.arg("-Z").arg("arg")`.
103+
/// This method will panic if a plain `-Z` or `-C` is passed, or if `-Z <name>` or `-C <name>`
104+
/// is passed (note the space).
153105
pub fn arg(&mut self, arg: &str) -> &mut Self {
106+
assert!(
107+
!(["-Z", "-C"].contains(&arg) || arg.starts_with("-Z ") || arg.starts_with("-C ")),
108+
"use `-Zarg` or `-Carg` over split `-Z` `arg` or `-C` `arg`"
109+
);
154110
self.cmd.arg(arg);
155111
self
156112
}
157113

158-
/// Fallback command arguments provider. Prefer using semantically meaningful builder methods
159-
/// (add them if they don't exist) whenever possible.
114+
/// Generic command arguments provider. Use `.arg("-Zname")` over `.arg("-Z").arg("arg")`.
115+
/// This method will panic if a plain `-Z` or `-C` is passed, or if `-Z <name>` or `-C <name>`
116+
/// is passed (note the space).
160117
pub fn args(&mut self, args: &[&str]) -> &mut Self {
118+
for arg in args {
119+
assert!(
120+
!(["-Z", "-C"].contains(&arg) || arg.starts_with("-Z ") || arg.starts_with("-C ")),
121+
"use `-Zarg` or `-Carg` over split `-Z` `arg` or `-C` `arg`"
122+
);
123+
}
124+
161125
self.cmd.args(args);
162126
self
163127
}
@@ -188,74 +152,3 @@ impl Rustc {
188152
self
189153
}
190154
}
191-
192-
/// Specifies the types of output files to generate.
193-
pub enum EmitKind {
194-
/// Generates a file containing metadata about the crate. The default output filename is
195-
/// `libCRATE_NAME.rmeta`.
196-
Metadata,
197-
}
198-
199-
/// Specifies codegen options.
200-
pub enum CodegenOpt {
201-
/// By default, rustc prefers to statically link dependencies. This option will indicate that
202-
/// dynamic linking should be used if possible if both a static and dynamic versions of a
203-
/// library are available.
204-
PreferDynamic,
205-
/// Controls the name mangling format for encoding Rust item names for the purpose of generating
206-
/// object code and linking.
207-
SymbolManglingVersion(SymbolManglingVersion),
208-
/// Controls whether LLVM uses link time optimizations to produce better optimized code, using
209-
/// whole-program analysis, at the cost of longer linking time.
210-
Lto(LtoKind),
211-
///
212-
OptLevel(OptLevel),
213-
/// Control the behavior of runtime integer overflow. When `overflow-checks` are enabled, a
214-
/// panic will occur on overflow.
215-
OverflowChecks,
216-
/// Control what happens when the code panics.
217-
Panic(PanicStrategy),
218-
}
219-
220-
/// The name mangling format for encoding Rust item names for the purpose of generating object code
221-
/// and linking.
222-
pub enum SymbolManglingVersion {
223-
Legacy,
224-
V0,
225-
}
226-
227-
/// Kind of LTO to perform.
228-
pub enum LtoKind {
229-
/// Perform "fat" LTO which attempts to perform optimizations across all crates within the
230-
/// dependency graph.
231-
Fat,
232-
/// Similar to "fat", but takes substantially less time to run while still achieving performance
233-
/// gains similar to "fat".
234-
Thin,
235-
/// Disable LTO.
236-
None,
237-
}
238-
239-
/// Optimization level.
240-
pub enum OptLevel {
241-
/// No optimizations, also turns on `cfg(debug_assertions)` (the default).
242-
O0,
243-
/// Basic optimizations.
244-
O1,
245-
/// Some optimizations.
246-
O2,
247-
/// All optimizations.
248-
O3,
249-
/// Optimize for binary size.
250-
Os,
251-
/// Optimize for binary size, but also turn off loop vectorization.
252-
Oz,
253-
}
254-
255-
/// What happens when the code panics.
256-
pub enum PanicStrategy {
257-
/// Terminate the process upon panic.
258-
Abort,
259-
/// Unwind the stack upon panic.
260-
Unwind,
261-
}

tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@ extern crate run_make_support;
77

88
use std::path::PathBuf;
99

10-
use run_make_support::{Rustc, EmitKind};
10+
use run_make_support::Rustc;
1111

1212
fn main() {
13-
Rustc::new_aux_build().input_file("stable.rs").emit(&[EmitKind::Metadata]).run();
13+
Rustc::new_aux_build().input("stable.rs").emit("metadata").run();
1414

1515
let mut stable_path = PathBuf::from(env!("TMPDIR"));
1616
stable_path.push("libstable.rmeta");
1717

1818
let output = Rustc::new()
19-
.input_file("main.rs")
20-
.emit(&[EmitKind::Metadata])
19+
.input("main.rs")
20+
.emit("metadata")
2121
.extern_("stable", &stable_path)
22-
.inspect(|cmd| eprintln!("{:#?}", cmd))
2322
.output();
2423

2524
let stderr = String::from_utf8_lossy(&output.stderr);
2625
let version = include_str!(concat!(env!("S"), "/src/version"));
2726
let expected_string = format!("stable since {}", version.trim());
28-
eprintln!("expected_string = `{}`", expected_string);
29-
eprintln!("stderr = \n{}", stderr);
3027
assert!(stderr.contains(&expected_string));
3128
}

tests/run-make/a-b-a-linker-guard/rmake.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,32 @@
55

66
extern crate run_make_support;
77

8-
use run_make_support::{run, run_fail, CodegenOpt, Rustc, SymbolManglingVersion};
9-
use CodegenOpt::*;
8+
use run_make_support::{run, run_fail, Rustc};
109

1110
fn main() {
1211
Rustc::new()
13-
.input_file("a.rs")
12+
.input("a.rs")
1413
.cfg("x")
15-
.enable_unstable_options()
16-
.codegen_opt(PreferDynamic)
17-
.codegen_opt(SymbolManglingVersion(SymbolManglingVersion::Legacy))
14+
.arg("-Zunstable-options")
15+
.arg("-Cprefer-dynamic")
16+
.arg("-Csymbol-mangling-version=legacy")
1817
.run();
1918

2019
Rustc::new()
21-
.input_file("b.rs")
22-
.enable_unstable_options()
23-
.codegen_opt(PreferDynamic)
24-
.codegen_opt(SymbolManglingVersion(SymbolManglingVersion::Legacy))
20+
.input("b.rs")
21+
.arg("-Zunstable-options")
22+
.arg("-Cprefer-dynamic")
23+
.arg("-Csymbol-mangling-version=legacy")
2524
.run();
2625

2726
run("b");
2827

2928
Rustc::new()
30-
.input_file("a.rs")
29+
.input("a.rs")
3130
.cfg("y")
32-
.enable_unstable_options()
33-
.codegen_opt(PreferDynamic)
34-
.codegen_opt(SymbolManglingVersion(SymbolManglingVersion::Legacy))
31+
.arg("-Zunstable-options")
32+
.arg("-Cprefer-dynamic")
33+
.arg("-Csymbol-mangling-version=legacy")
3534
.run();
3635

3736
run_fail("b");

tests/run-make/wasm-abi/rmake.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
return;
1010
}
1111

12-
Rustc::new().input_file("foo.rs").target("wasm32-wasip1").run();
12+
Rustc::new().input("foo.rs").target("wasm32-wasip1").run();
1313

1414
let file = out_dir().join("foo.wasm");
1515

tests/run-make/wasm-custom-section/rmake.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
extern crate run_make_support;
22

3-
use run_make_support::{out_dir, wasmparser, CodegenOpt, LtoKind, OptLevel, Rustc};
3+
use run_make_support::{out_dir, wasmparser, Rustc};
44
use std::collections::HashMap;
55

6-
use CodegenOpt::*;
7-
86
fn main() {
97
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
108
return;
119
}
1210

13-
Rustc::new().input_file("foo.rs").target("wasm32-wasip1").run();
11+
Rustc::new().input("foo.rs").target("wasm32-wasip1").run();
1412

15-
Rustc::new()
16-
.input_file("bar.rs")
17-
.target("wasm32-wasip1")
18-
.codegen_opt(Lto(LtoKind::Fat))
19-
.default_opt()
20-
.run();
13+
Rustc::new().input("bar.rs").target("wasm32-wasip1").opt().run();
2114

2215
let file = std::fs::read(&out_dir().join("bar.wasm")).unwrap();
2316

tests/run-make/wasm-custom-sections-opt/rmake.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
return;
1010
}
1111

12-
Rustc::new().input_file("foo.rs").target("wasm32-wasip1").default_opt().run();
12+
Rustc::new().input("foo.rs").target("wasm32-wasip1").opt().run();
1313

1414
verify(&out_dir().join("foo.wasm"));
1515
}

tests/run-make/wasm-export-all-symbols/rmake.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ fn main() {
1818
fn test(args: &[&str]) {
1919
eprintln!("running with {args:?}");
2020

21-
Rustc::new().input_file("bar.rs").target("wasm32-wasip1").args(args).run();
22-
Rustc::new().input_file("foo.rs").target("wasm32-wasip1").args(args).run();
23-
Rustc::new().input_file("main.rs").target("wasm32-wasip1").args(args).run();
21+
Rustc::new().input("bar.rs").target("wasm32-wasip1").args(args).run();
22+
Rustc::new().input("foo.rs").target("wasm32-wasip1").args(args).run();
23+
Rustc::new().input("main.rs").target("wasm32-wasip1").args(args).run();
2424

2525
verify_exports(
2626
&out_dir().join("foo.wasm"),

tests/run-make/wasm-import-module/rmake.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
extern crate run_make_support;
22

3-
use run_make_support::{out_dir, wasmparser, CodegenOpt, LtoKind, Rustc};
3+
use run_make_support::{out_dir, wasmparser, Rustc};
44
use std::collections::HashMap;
55
use wasmparser::TypeRef::Func;
66

7-
use CodegenOpt::*;
8-
97
fn main() {
108
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
119
return;
1210
}
1311

14-
Rustc::new().input_file("foo.rs").target("wasm32-wasip1").run();
12+
Rustc::new().input("foo.rs").target("wasm32-wasip1").run();
1513
Rustc::new()
16-
.input_file("bar.rs")
14+
.input("bar.rs")
1715
.target("wasm32-wasip1")
18-
.codegen_opt(Lto(LtoKind::Fat))
19-
.default_opt()
16+
.arg("-Clto")
17+
.opt()
2018
.run();
2119

2220
let file = std::fs::read(&out_dir().join("bar.wasm")).unwrap();

tests/run-make/wasm-panic-small/rmake.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
extern crate run_make_support;
44

5-
use run_make_support::{out_dir, Rustc, CodegenOpt, LtoKind};
6-
7-
use CodegenOpt::*;
5+
use run_make_support::{out_dir, Rustc};
86

97
fn main() {
108
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
@@ -21,10 +19,10 @@ fn test(cfg: &str) {
2119
eprintln!("running cfg {cfg:?}");
2220

2321
Rustc::new()
24-
.input_file("foo.rs")
22+
.input("foo.rs")
2523
.target("wasm32-wasip1")
26-
.codegen_opt(Lto(LtoKind::Fat))
27-
.default_opt()
24+
.arg("-Clto")
25+
.opt()
2826
.cfg(cfg)
2927
.run();
3028

0 commit comments

Comments
 (0)