Skip to content

run-make-support: tidy up support library #123699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
run-make-support: use macro to implement common methods
Removes the manual copy-pasta'd implementation of common methods.
  • Loading branch information
jieyouxu committed Apr 13, 2024
commit 3d115b9cc9cf511b49d2e4812ae6a530bc6f61df
39 changes: 3 additions & 36 deletions src/tools/run-make-support/src/cc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use std::path::Path;
use std::process::{Command, Output};
use std::process::Command;

use crate::{bin_name, cygpath_windows, handle_failed_output, is_msvc, is_windows, tmp_dir, uname};

Expand All @@ -19,6 +19,8 @@ pub struct Cc {
cmd: Command,
}

crate::impl_common_helpers!(Cc);

impl Cc {
/// Construct a new platform-specific C compiler invocation.
///
Expand All @@ -43,22 +45,6 @@ impl Cc {
self
}

/// Add a *platform-and-compiler-specific* argument. Please consult the docs for the various
/// possible C compilers on the various platforms to check which arguments are legal for
/// which compiler.
pub fn arg(&mut self, flag: &str) -> &mut Self {
self.cmd.arg(flag);
self
}

/// Add multiple *platform-and-compiler-specific* arguments. Please consult the docs for the
/// various possible C compilers on the various platforms to check which arguments are legal
/// for which compiler.
pub fn args(&mut self, args: &[&str]) -> &mut Self {
self.cmd.args(args);
self
}

/// Specify `-o` or `-Fe`/`-Fo` depending on platform/compiler. This assumes that the executable
/// is under `$TMPDIR`.
pub fn out_exe(&mut self, name: &str) -> &mut Self {
Expand All @@ -85,25 +71,6 @@ impl Cc {

self
}

/// Run the constructed C invocation command and assert that it is successfully run.
#[track_caller]
pub fn run(&mut self) -> Output {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();

let output = self.cmd.output().unwrap();
if !output.status.success() {
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
}
output
}

/// 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
}
}

/// `EXTRACFLAGS`
Expand Down
59 changes: 3 additions & 56 deletions src/tools/run-make-support/src/rustc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use std::ffi::{OsStr, OsString};
use std::ffi::OsString;
use std::path::Path;
use std::process::{Command, Output};

Expand All @@ -21,6 +21,8 @@ pub struct Rustc {
cmd: Command,
}

crate::impl_common_helpers!(Rustc);

fn setup_common() -> Command {
let rustc = env::var("RUSTC").unwrap();
let mut cmd = Command::new(rustc);
Expand Down Expand Up @@ -133,12 +135,6 @@ impl Rustc {
self
}

/// Generic command argument provider. Use `.arg("-Zname")` over `.arg("-Z").arg("arg")`.
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
self.cmd.arg(arg);
self
}

/// Specify the crate type.
pub fn crate_type(&mut self, crate_type: &str) -> &mut Self {
self.cmd.arg("--crate-type");
Expand All @@ -153,49 +149,6 @@ impl Rustc {
self
}

/// Generic command arguments provider. Use `.arg("-Zname")` over `.arg("-Z").arg("arg")`.
pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Self {
self.cmd.args(args);
self
}

pub fn env(&mut self, name: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> &mut Self {
self.cmd.env(name, value);
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();
let caller_line_number = caller_location.line();

let output = self.cmd.output().unwrap();
if !output.status.success() {
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
}
output
}

#[track_caller]
pub fn run_fail(&mut self) -> Output {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();

let output = self.cmd.output().unwrap();
if output.status.success() {
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
}
output
}

#[track_caller]
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
let caller_location = std::panic::Location::caller();
Expand All @@ -207,10 +160,4 @@ impl Rustc {
}
output
}

/// 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
}
}
22 changes: 2 additions & 20 deletions src/tools/run-make-support/src/rustdoc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env;
use std::ffi::OsStr;
use std::path::Path;
use std::process::{Command, Output};

Expand All @@ -20,6 +19,8 @@ pub struct Rustdoc {
cmd: Command,
}

crate::impl_common_helpers!(Rustdoc);

fn setup_common() -> Command {
let rustdoc = env::var("RUSTDOC").unwrap();
let mut cmd = Command::new(rustdoc);
Expand Down Expand Up @@ -61,25 +62,6 @@ impl Rustdoc {
self
}

/// Generic command argument provider. Use `.arg("-Zname")` over `.arg("-Z").arg("arg")`.
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
self.cmd.arg(arg);
self
}

/// Run the build `rustdoc` command and assert that the run is successful.
#[track_caller]
pub fn run(&mut self) -> Output {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();

let output = self.cmd.output().unwrap();
if !output.status.success() {
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
}
output
}

#[track_caller]
pub fn run_fail_assert_exit_code(&mut self, code: i32) -> Output {
let caller_location = std::panic::Location::caller();
Expand Down