Skip to content

Commit

Permalink
Rework rmake support library to have a weakly-typed API with helper m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
jieyouxu committed Mar 14, 2024
1 parent 4acb17e commit 6ad3b4b
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 209 deletions.
151 changes: 22 additions & 129 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,62 +59,15 @@ impl Rustc {
self
}

/// Configure codegen options.
pub fn codegen_opt(&mut self, c: CodegenOpt) -> &mut Self {
self.cmd.arg("-C");

match c {
CodegenOpt::PreferDynamic => self.cmd.arg("prefer-dynamic=true"),
CodegenOpt::SymbolManglingVersion(v) => match v {
SymbolManglingVersion::Legacy => self.cmd.arg("symbol-mangling-version=legacy"),
SymbolManglingVersion::V0 => self.cmd.arg("symbol-mangling-version=v0"),
},
CodegenOpt::Lto(kind) => match kind {
LtoKind::Fat => self.cmd.arg("lto=fat"),
LtoKind::Thin => self.cmd.arg("lto=thin"),
LtoKind::None => self.cmd.arg("lto=false"),
},
CodegenOpt::OptLevel(level) => match level {
OptLevel::O0 => self.cmd.arg("opt-level=0"),
OptLevel::O1 => self.cmd.arg("opt-level=1"),
OptLevel::O2 => self.cmd.arg("opt-level=2"),
OptLevel::O3 => self.cmd.arg("opt-level=3"),
OptLevel::Os => self.cmd.arg("opt-level=s"),
OptLevel::Oz => self.cmd.arg("opt-level=z"),
},
CodegenOpt::OverflowChecks => self.cmd.arg("overflow-checks=true"),
CodegenOpt::Panic(strat) => match strat {
PanicStrategy::Abort => self.cmd.arg("panic=abort"),
PanicStrategy::Unwind => self.cmd.arg("panic=unwind"),
},
};

self
}

/// Specify default optimization level `-O` (alias for `-C opt-level=2`).
pub fn default_opt(&mut self) -> &mut Self {
pub fn opt(&mut self) -> &mut Self {
self.cmd.arg("-O");
self
}

/// Specify types of output files to generate. See [`EmitKind`] for kinds.
pub fn emit(&mut self, kinds: &[EmitKind]) -> &mut Self {
let kinds = kinds
.iter()
.map(|kind| match kind {
EmitKind::Metadata => "metadata",
})
.collect::<Vec<_>>();
let kinds_str: String = kinds.join(",");
self.cmd.arg(format!("--emit={kinds_str}"));
self
}

/// Set `-Z unstable-options`
pub fn enable_unstable_options(&mut self) -> &mut Self {
self.cmd.arg("-Z");
self.cmd.arg("unstable-options");
/// Specify type(s) of output files to generate.
pub fn emit(&mut self, kinds: &str) -> &mut Self {
self.cmd.arg(format!("--emit={kinds}"));
self
}

Expand All @@ -134,7 +87,7 @@ impl Rustc {
}

/// Specify path to the input file.
pub fn input_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
self.cmd.arg(path.as_ref());
self
}
Expand All @@ -146,18 +99,29 @@ impl Rustc {
self
}

// Last-resort builder methods

/// Fallback command argument provider. Prefer using semantically meaningful builder methods
/// (add them if they don't exist) whenever possible.
/// Generic command argument provider. Use `.arg("-Zname")` over `.arg("-Z").arg("arg")`.
/// This method will panic if a plain `-Z` or `-C` is passed, or if `-Z <name>` or `-C <name>`
/// is passed (note the space).
pub fn arg(&mut self, arg: &str) -> &mut Self {
assert!(
!(["-Z", "-C"].contains(&arg) || arg.starts_with("-Z ") || arg.starts_with("-C ")),
"use `-Zarg` or `-Carg` over split `-Z` `arg` or `-C` `arg`"
);
self.cmd.arg(arg);
self
}

/// Fallback command arguments provider. Prefer using semantically meaningful builder methods
/// (add them if they don't exist) whenever possible.
/// Generic command arguments provider. Use `.arg("-Zname")` over `.arg("-Z").arg("arg")`.
/// This method will panic if a plain `-Z` or `-C` is passed, or if `-Z <name>` or `-C <name>`
/// is passed (note the space).
pub fn args(&mut self, args: &[&str]) -> &mut Self {
for arg in args {
assert!(
!(["-Z", "-C"].contains(&arg) || arg.starts_with("-Z ") || arg.starts_with("-C ")),
"use `-Zarg` or `-Carg` over split `-Z` `arg` or `-C` `arg`"
);
}

self.cmd.args(args);
self
}
Expand Down Expand Up @@ -188,74 +152,3 @@ impl Rustc {
self
}
}

/// Specifies the types of output files to generate.
pub enum EmitKind {
/// Generates a file containing metadata about the crate. The default output filename is
/// `libCRATE_NAME.rmeta`.
Metadata,
}

/// Specifies codegen options.
pub enum CodegenOpt {
/// By default, rustc prefers to statically link dependencies. This option will indicate that
/// dynamic linking should be used if possible if both a static and dynamic versions of a
/// library are available.
PreferDynamic,
/// Controls the name mangling format for encoding Rust item names for the purpose of generating
/// object code and linking.
SymbolManglingVersion(SymbolManglingVersion),
/// Controls whether LLVM uses link time optimizations to produce better optimized code, using
/// whole-program analysis, at the cost of longer linking time.
Lto(LtoKind),
///
OptLevel(OptLevel),
/// Control the behavior of runtime integer overflow. When `overflow-checks` are enabled, a
/// panic will occur on overflow.
OverflowChecks,
/// Control what happens when the code panics.
Panic(PanicStrategy),
}

/// The name mangling format for encoding Rust item names for the purpose of generating object code
/// and linking.
pub enum SymbolManglingVersion {
Legacy,
V0,
}

/// Kind of LTO to perform.
pub enum LtoKind {
/// Perform "fat" LTO which attempts to perform optimizations across all crates within the
/// dependency graph.
Fat,
/// Similar to "fat", but takes substantially less time to run while still achieving performance
/// gains similar to "fat".
Thin,
/// Disable LTO.
None,
}

/// Optimization level.
pub enum OptLevel {
/// No optimizations, also turns on `cfg(debug_assertions)` (the default).
O0,
/// Basic optimizations.
O1,
/// Some optimizations.
O2,
/// All optimizations.
O3,
/// Optimize for binary size.
Os,
/// Optimize for binary size, but also turn off loop vectorization.
Oz,
}

/// What happens when the code panics.
pub enum PanicStrategy {
/// Terminate the process upon panic.
Abort,
/// Unwind the stack upon panic.
Unwind,
}
11 changes: 4 additions & 7 deletions tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@ extern crate run_make_support;

use std::path::PathBuf;

use run_make_support::{Rustc, EmitKind};
use run_make_support::Rustc;

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

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

let output = Rustc::new()
.input_file("main.rs")
.emit(&[EmitKind::Metadata])
.input("main.rs")
.emit("metadata")
.extern_("stable", &stable_path)
.inspect(|cmd| eprintln!("{:#?}", cmd))
.output();

let stderr = String::from_utf8_lossy(&output.stderr);
let version = include_str!(concat!(env!("S"), "/src/version"));
let expected_string = format!("stable since {}", version.trim());
eprintln!("expected_string = `{}`", expected_string);
eprintln!("stderr = \n{}", stderr);
assert!(stderr.contains(&expected_string));
}
27 changes: 13 additions & 14 deletions tests/run-make/a-b-a-linker-guard/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@

extern crate run_make_support;

use run_make_support::{run, run_fail, CodegenOpt, Rustc, SymbolManglingVersion};
use CodegenOpt::*;
use run_make_support::{run, run_fail, Rustc};

fn main() {
Rustc::new()
.input_file("a.rs")
.input("a.rs")
.cfg("x")
.enable_unstable_options()
.codegen_opt(PreferDynamic)
.codegen_opt(SymbolManglingVersion(SymbolManglingVersion::Legacy))
.arg("-Zunstable-options")
.arg("-Cprefer-dynamic")
.arg("-Csymbol-mangling-version=legacy")
.run();

Rustc::new()
.input_file("b.rs")
.enable_unstable_options()
.codegen_opt(PreferDynamic)
.codegen_opt(SymbolManglingVersion(SymbolManglingVersion::Legacy))
.input("b.rs")
.arg("-Zunstable-options")
.arg("-Cprefer-dynamic")
.arg("-Csymbol-mangling-version=legacy")
.run();

run("b");

Rustc::new()
.input_file("a.rs")
.input("a.rs")
.cfg("y")
.enable_unstable_options()
.codegen_opt(PreferDynamic)
.codegen_opt(SymbolManglingVersion(SymbolManglingVersion::Legacy))
.arg("-Zunstable-options")
.arg("-Cprefer-dynamic")
.arg("-Csymbol-mangling-version=legacy")
.run();

run_fail("b");
Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/wasm-abi/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
return;
}

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

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

Expand Down
13 changes: 3 additions & 10 deletions tests/run-make/wasm-custom-section/rmake.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
extern crate run_make_support;

use run_make_support::{out_dir, wasmparser, CodegenOpt, LtoKind, OptLevel, Rustc};
use run_make_support::{out_dir, wasmparser, Rustc};
use std::collections::HashMap;

use CodegenOpt::*;

fn main() {
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
return;
}

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

Rustc::new()
.input_file("bar.rs")
.target("wasm32-wasip1")
.codegen_opt(Lto(LtoKind::Fat))
.default_opt()
.run();
Rustc::new().input("bar.rs").target("wasm32-wasip1").opt().run();

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

Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/wasm-custom-sections-opt/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
return;
}

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

verify(&out_dir().join("foo.wasm"));
}
Expand Down
6 changes: 3 additions & 3 deletions tests/run-make/wasm-export-all-symbols/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ fn main() {
fn test(args: &[&str]) {
eprintln!("running with {args:?}");

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

verify_exports(
&out_dir().join("foo.wasm"),
Expand Down
12 changes: 5 additions & 7 deletions tests/run-make/wasm-import-module/rmake.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
extern crate run_make_support;

use run_make_support::{out_dir, wasmparser, CodegenOpt, LtoKind, Rustc};
use run_make_support::{out_dir, wasmparser, Rustc};
use std::collections::HashMap;
use wasmparser::TypeRef::Func;

use CodegenOpt::*;

fn main() {
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
return;
}

Rustc::new().input_file("foo.rs").target("wasm32-wasip1").run();
Rustc::new().input("foo.rs").target("wasm32-wasip1").run();
Rustc::new()
.input_file("bar.rs")
.input("bar.rs")
.target("wasm32-wasip1")
.codegen_opt(Lto(LtoKind::Fat))
.default_opt()
.arg("-Clto")
.opt()
.run();

let file = std::fs::read(&out_dir().join("bar.wasm")).unwrap();
Expand Down
10 changes: 4 additions & 6 deletions tests/run-make/wasm-panic-small/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

extern crate run_make_support;

use run_make_support::{out_dir, Rustc, CodegenOpt, LtoKind};

use CodegenOpt::*;
use run_make_support::{out_dir, Rustc};

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

Rustc::new()
.input_file("foo.rs")
.input("foo.rs")
.target("wasm32-wasip1")
.codegen_opt(Lto(LtoKind::Fat))
.default_opt()
.arg("-Clto")
.opt()
.cfg(cfg)
.run();

Expand Down
Loading

0 comments on commit 6ad3b4b

Please sign in to comment.