Skip to content

Commit

Permalink
Rework rmake support library to use a DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyouxu committed Mar 13, 2024
1 parent 9ce37dc commit 4acb17e
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 174 deletions.
278 changes: 190 additions & 88 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
pub mod run;

use std::env;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};

pub use wasmparser;

pub use run::{run, run_fail};

pub fn out_dir() -> PathBuf {
env::var_os("TMPDIR").unwrap().into()
}
Expand All @@ -24,65 +28,148 @@ fn handle_failed_output(cmd: &str, output: Output, caller_line_number: u32) -> !
std::process::exit(1)
}

pub fn rustc() -> RustcInvocationBuilder {
RustcInvocationBuilder::new()
}

pub fn aux_build() -> AuxBuildInvocationBuilder {
AuxBuildInvocationBuilder::new()
}

/// A `rustc` invocation builder.
#[derive(Debug)]
pub struct RustcInvocationBuilder {
pub struct Rustc {
cmd: Command,
}

impl RustcInvocationBuilder {
fn new() -> Self {
impl Rustc {
// `rustc` invocation constructor methods

/// Construct a new `rustc` invocation.
pub fn new() -> Self {
let cmd = setup_common_build_cmd();
Self { cmd }
}

pub fn arg(&mut self, arg: &str) -> &mut RustcInvocationBuilder {
self.cmd.arg(arg);
/// Construct a new `rustc` invocation with `aux_build` preset (setting `--crate-type=lib`).
pub fn new_aux_build() -> Self {
let mut cmd = setup_common_build_cmd();
cmd.arg("--crate-type=lib");
Self { cmd }
}

// Argument provider methods

/// Configure the compilation environment.
pub fn cfg(&mut self, s: &str) -> &mut Self {
self.cmd.arg("--cfg");
self.cmd.arg(s);
self
}

pub fn args(&mut self, args: &[&str]) -> &mut RustcInvocationBuilder {
self.cmd.args(args);
/// 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
}

#[track_caller]
pub fn run(&mut self) -> Output {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();
/// Specify default optimization level `-O` (alias for `-C opt-level=2`).
pub fn default_opt(&mut self) -> &mut Self {
self.cmd.arg("-O");
self
}

let output = self.cmd.output().unwrap();
if !output.status.success() {
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
}
output
/// 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
}
}

#[derive(Debug)]
pub struct AuxBuildInvocationBuilder {
cmd: Command,
}
/// Set `-Z unstable-options`
pub fn enable_unstable_options(&mut self) -> &mut Self {
self.cmd.arg("-Z");
self.cmd.arg("unstable-options");
self
}

impl AuxBuildInvocationBuilder {
fn new() -> Self {
let mut cmd = setup_common_build_cmd();
cmd.arg("--crate-type=lib");
Self { cmd }
/// Specify where an external library is located.
pub fn extern_<P: AsRef<Path>>(&mut self, crate_name: &str, path: P) -> &mut Self {
assert!(
!crate_name.contains(|c: char| c.is_whitespace() || c == '\\' || c == '/'),
"crate name cannot contain whitespace or path separators"
);

let path = path.as_ref().to_string_lossy();

self.cmd.arg("--extern");
self.cmd.arg(format!("{crate_name}={path}"));

self
}

pub fn arg(&mut self, arg: &str) -> &mut AuxBuildInvocationBuilder {
/// Specify path to the input file.
pub fn input_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
self.cmd.arg(path.as_ref());
self
}

/// Specify target triple.
pub fn target(&mut self, target: &str) -> &mut Self {
assert!(!target.contains(char::is_whitespace), "target triple cannot contain spaces");
self.cmd.arg(format!("--target={target}"));
self
}

// Last-resort builder methods

/// Fallback command argument provider. Prefer using semantically meaningful builder methods
/// (add them if they don't exist) whenever possible.
pub fn arg(&mut self, arg: &str) -> &mut Self {
self.cmd.arg(arg);
self
}

/// Fallback command arguments provider. Prefer using semantically meaningful builder methods
/// (add them if they don't exist) whenever possible.
pub fn args(&mut self, args: &[&str]) -> &mut Self {
self.cmd.args(args);
self
}

// Command inspection, output and running helper methods

/// Get the [`Output`][std::process::Output] of the finished `rustc` process.
pub fn output(&mut self) -> Output {
self.cmd.output().unwrap()
}

/// Run the constructed `rustc` command and assert that it is successfully run.
#[track_caller]
pub fn run(&mut self) -> Output {
let caller_location = std::panic::Location::caller();
Expand All @@ -94,66 +181,81 @@ impl AuxBuildInvocationBuilder {
}
output
}
}

fn run_common(bin_name: &str) -> (Command, Output) {
let target = env::var("TARGET").unwrap();

let bin_name =
if target.contains("windows") { format!("{}.exe", bin_name) } else { bin_name.to_owned() };

let mut bin_path = PathBuf::new();
bin_path.push(env::var("TMPDIR").unwrap());
bin_path.push(&bin_name);
let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap();
let mut cmd = Command::new(bin_path);
cmd.env(&ld_lib_path_envvar, {
let mut paths = vec![];
paths.push(PathBuf::from(env::var("TMPDIR").unwrap()));
for p in env::split_paths(&env::var("TARGET_RPATH_ENV").unwrap()) {
paths.push(p.to_path_buf());
}
for p in env::split_paths(&env::var(&ld_lib_path_envvar).unwrap()) {
paths.push(p.to_path_buf());
}
env::join_paths(paths.iter()).unwrap()
});

if target.contains("windows") {
let mut paths = vec![];
for p in env::split_paths(&std::env::var("PATH").unwrap_or(String::new())) {
paths.push(p.to_path_buf());
}
paths.push(Path::new(&std::env::var("TARGET_RPATH_DIR").unwrap()).to_path_buf());
cmd.env("PATH", env::join_paths(paths.iter()).unwrap());
/// Inspect what the underlying [`Command`] is up to the current construction.
pub fn inspect(&mut self, f: impl FnOnce(&Command)) -> &mut Self {
f(&self.cmd);
self
}
}

let output = cmd.output().unwrap();
(cmd, output)
/// 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,
}

/// Run a built binary and make sure it succeeds.
#[track_caller]
pub fn run(bin_name: &str) -> Output {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();
/// 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),
}

let (cmd, output) = run_common(bin_name);
if !output.status.success() {
handle_failed_output(&format!("{:#?}", cmd), output, caller_line_number);
}
output
/// The name mangling format for encoding Rust item names for the purpose of generating object code
/// and linking.
pub enum SymbolManglingVersion {
Legacy,
V0,
}

/// Run a built binary and make sure it fails.
#[track_caller]
pub fn run_fail(bin_name: &str) -> Output {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();
/// 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,
}

let (cmd, output) = run_common(bin_name);
if output.status.success() {
handle_failed_output(&format!("{:#?}", cmd), output, caller_line_number);
}
output
/// 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,
}
Loading

0 comments on commit 4acb17e

Please sign in to comment.