Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c761cb2

Browse files
committed
run_make_support: move impl_common_helpers into own macros module
1 parent 0da95bd commit c761cb2

File tree

7 files changed

+124
-121
lines changed

7 files changed

+124
-121
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct Cc {
2020
cmd: Command,
2121
}
2222

23-
crate::impl_common_helpers!(Cc);
23+
crate::macros::impl_common_helpers!(Cc);
2424

2525
impl Cc {
2626
/// Construct a new platform-specific C compiler invocation.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct Clang {
1616
cmd: Command,
1717
}
1818

19-
crate::impl_common_helpers!(Clang);
19+
crate::macros::impl_common_helpers!(Clang);
2020

2121
impl Clang {
2222
/// Construct a new `clang` invocation. `clang` is not always available for all targets.

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

Lines changed: 3 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod command;
99
pub mod diff;
1010
pub mod fs_wrapper;
1111
pub mod llvm;
12+
mod macros;
1213
pub mod run;
1314
pub mod rustc;
1415
pub mod rustdoc;
@@ -37,6 +38,8 @@ pub use run::{cmd, run, run_fail, run_with_args};
3738
pub use rustc::{aux_build, bare_rustc, rustc, Rustc};
3839
pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc};
3940

41+
use command::{Command, CompletedProcess};
42+
4043
#[track_caller]
4144
#[must_use]
4245
pub fn env_var(name: &str) -> String {
@@ -531,116 +534,3 @@ pub fn run_in_tmpdir<F: FnOnce()>(callback: F) {
531534
env::set_current_dir(original_dir).unwrap();
532535
fs::remove_dir_all(tmpdir).unwrap();
533536
}
534-
535-
/// Implement common helpers for command wrappers. This assumes that the command wrapper is a struct
536-
/// containing a `cmd: Command` field. The provided helpers are:
537-
///
538-
/// 1. Generic argument acceptors: `arg` and `args` (delegated to [`Command`]). These are intended
539-
/// to be *fallback* argument acceptors, when specific helpers don't make sense. Prefer to add
540-
/// new specific helper methods over relying on these generic argument providers.
541-
/// 2. Environment manipulation methods: `env`, `env_remove` and `env_clear`: these delegate to
542-
/// methods of the same name on [`Command`].
543-
/// 3. Output and execution: `run` and `run_fail` are provided. These are
544-
/// higher-level convenience methods which wait for the command to finish running and assert
545-
/// that the command successfully ran or failed as expected. They return
546-
/// [`CompletedProcess`], which can be used to assert the stdout/stderr/exit code of the executed
547-
/// process.
548-
///
549-
/// Example usage:
550-
///
551-
/// ```ignore (illustrative)
552-
/// struct CommandWrapper { cmd: Command } // <- required `cmd` field
553-
///
554-
/// crate::impl_common_helpers!(CommandWrapper);
555-
///
556-
/// impl CommandWrapper {
557-
/// // ... additional specific helper methods
558-
/// }
559-
/// ```
560-
macro_rules! impl_common_helpers {
561-
($wrapper: ident) => {
562-
impl $wrapper {
563-
/// Specify an environment variable.
564-
pub fn env<K, V>(&mut self, key: K, value: V) -> &mut Self
565-
where
566-
K: AsRef<::std::ffi::OsStr>,
567-
V: AsRef<::std::ffi::OsStr>,
568-
{
569-
self.cmd.env(key, value);
570-
self
571-
}
572-
573-
/// Remove an environmental variable.
574-
pub fn env_remove<K>(&mut self, key: K) -> &mut Self
575-
where
576-
K: AsRef<::std::ffi::OsStr>,
577-
{
578-
self.cmd.env_remove(key);
579-
self
580-
}
581-
582-
/// Generic command argument provider. Prefer specific helper methods if possible.
583-
/// Note that for some executables, arguments might be platform specific. For C/C++
584-
/// compilers, arguments might be platform *and* compiler specific.
585-
pub fn arg<S>(&mut self, arg: S) -> &mut Self
586-
where
587-
S: AsRef<::std::ffi::OsStr>,
588-
{
589-
self.cmd.arg(arg);
590-
self
591-
}
592-
593-
/// Generic command arguments provider. Prefer specific helper methods if possible.
594-
/// Note that for some executables, arguments might be platform specific. For C/C++
595-
/// compilers, arguments might be platform *and* compiler specific.
596-
pub fn args<V, S>(&mut self, args: V) -> &mut Self
597-
where
598-
V: AsRef<[S]>,
599-
S: AsRef<::std::ffi::OsStr>,
600-
{
601-
self.cmd.args(args.as_ref());
602-
self
603-
}
604-
605-
/// Inspect what the underlying [`Command`] is up to the
606-
/// current construction.
607-
pub fn inspect<I>(&mut self, inspector: I) -> &mut Self
608-
where
609-
I: FnOnce(&::std::process::Command),
610-
{
611-
self.cmd.inspect(inspector);
612-
self
613-
}
614-
615-
/// Run the constructed command and assert that it is successfully run.
616-
#[track_caller]
617-
pub fn run(&mut self) -> crate::command::CompletedProcess {
618-
self.cmd.run()
619-
}
620-
621-
/// Run the constructed command and assert that it does not successfully run.
622-
#[track_caller]
623-
pub fn run_fail(&mut self) -> crate::command::CompletedProcess {
624-
self.cmd.run_fail()
625-
}
626-
627-
/// Run the command but do not check its exit status.
628-
/// Only use if you explicitly don't care about the exit status.
629-
/// Prefer to use [`Self::run`] and [`Self::run_fail`]
630-
/// whenever possible.
631-
#[track_caller]
632-
pub fn run_unchecked(&mut self) -> crate::command::CompletedProcess {
633-
self.cmd.run_unchecked()
634-
}
635-
636-
/// Set the path where the command will be run.
637-
pub fn current_dir<P: AsRef<::std::path::Path>>(&mut self, path: P) -> &mut Self {
638-
self.cmd.current_dir(path);
639-
self
640-
}
641-
}
642-
};
643-
}
644-
645-
use crate::command::{Command, CompletedProcess};
646-
pub(crate) use impl_common_helpers;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ pub struct LlvmObjdump {
5757
cmd: Command,
5858
}
5959

60-
crate::impl_common_helpers!(LlvmReadobj);
61-
crate::impl_common_helpers!(LlvmProfdata);
62-
crate::impl_common_helpers!(LlvmFilecheck);
63-
crate::impl_common_helpers!(LlvmObjdump);
60+
crate::macros::impl_common_helpers!(LlvmReadobj);
61+
crate::macros::impl_common_helpers!(LlvmProfdata);
62+
crate::macros::impl_common_helpers!(LlvmFilecheck);
63+
crate::macros::impl_common_helpers!(LlvmObjdump);
6464

6565
/// Generate the path to the bin directory of LLVM.
6666
#[must_use]
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/// Implement common helpers for command wrappers. This assumes that the command wrapper is a struct
2+
/// containing a `cmd: Command` field. The provided helpers are:
3+
///
4+
/// 1. Generic argument acceptors: `arg` and `args` (delegated to [`Command`]). These are intended
5+
/// to be *fallback* argument acceptors, when specific helpers don't make sense. Prefer to add
6+
/// new specific helper methods over relying on these generic argument providers.
7+
/// 2. Environment manipulation methods: `env`, `env_remove` and `env_clear`: these delegate to
8+
/// methods of the same name on [`Command`].
9+
/// 3. Output and execution: `run` and `run_fail` are provided. These are higher-level convenience
10+
/// methods which wait for the command to finish running and assert that the command successfully
11+
/// ran or failed as expected. They return [`CompletedProcess`], which can be used to assert the
12+
/// stdout/stderr/exit code of the executed process.
13+
///
14+
/// Example usage:
15+
///
16+
/// ```ignore (illustrative)
17+
/// struct CommandWrapper { cmd: Command } // <- required `cmd` field
18+
///
19+
/// crate::macors::impl_common_helpers!(CommandWrapper);
20+
///
21+
/// impl CommandWrapper {
22+
/// // ... additional specific helper methods
23+
/// }
24+
/// ```
25+
///
26+
/// [`Command`]: crate::command::Command
27+
/// [`CompletedProcess`]: crate::command::CompletedProcess
28+
macro_rules! impl_common_helpers {
29+
($wrapper: ident) => {
30+
impl $wrapper {
31+
/// Specify an environment variable.
32+
pub fn env<K, V>(&mut self, key: K, value: V) -> &mut Self
33+
where
34+
K: AsRef<::std::ffi::OsStr>,
35+
V: AsRef<::std::ffi::OsStr>,
36+
{
37+
self.cmd.env(key, value);
38+
self
39+
}
40+
41+
/// Remove an environmental variable.
42+
pub fn env_remove<K>(&mut self, key: K) -> &mut Self
43+
where
44+
K: AsRef<::std::ffi::OsStr>,
45+
{
46+
self.cmd.env_remove(key);
47+
self
48+
}
49+
50+
/// Generic command argument provider. Prefer specific helper methods if possible.
51+
/// Note that for some executables, arguments might be platform specific. For C/C++
52+
/// compilers, arguments might be platform *and* compiler specific.
53+
pub fn arg<S>(&mut self, arg: S) -> &mut Self
54+
where
55+
S: AsRef<::std::ffi::OsStr>,
56+
{
57+
self.cmd.arg(arg);
58+
self
59+
}
60+
61+
/// Generic command arguments provider. Prefer specific helper methods if possible.
62+
/// Note that for some executables, arguments might be platform specific. For C/C++
63+
/// compilers, arguments might be platform *and* compiler specific.
64+
pub fn args<V, S>(&mut self, args: V) -> &mut Self
65+
where
66+
V: AsRef<[S]>,
67+
S: AsRef<::std::ffi::OsStr>,
68+
{
69+
self.cmd.args(args.as_ref());
70+
self
71+
}
72+
73+
/// Inspect what the underlying [`Command`] is up to the
74+
/// current construction.
75+
pub fn inspect<I>(&mut self, inspector: I) -> &mut Self
76+
where
77+
I: FnOnce(&::std::process::Command),
78+
{
79+
self.cmd.inspect(inspector);
80+
self
81+
}
82+
83+
/// Run the constructed command and assert that it is successfully run.
84+
#[track_caller]
85+
pub fn run(&mut self) -> crate::command::CompletedProcess {
86+
self.cmd.run()
87+
}
88+
89+
/// Run the constructed command and assert that it does not successfully run.
90+
#[track_caller]
91+
pub fn run_fail(&mut self) -> crate::command::CompletedProcess {
92+
self.cmd.run_fail()
93+
}
94+
95+
/// Run the command but do not check its exit status.
96+
/// Only use if you explicitly don't care about the exit status.
97+
/// Prefer to use [`Self::run`] and [`Self::run_fail`]
98+
/// whenever possible.
99+
#[track_caller]
100+
pub fn run_unchecked(&mut self) -> crate::command::CompletedProcess {
101+
self.cmd.run_unchecked()
102+
}
103+
104+
/// Set the path where the command will be run.
105+
pub fn current_dir<P: AsRef<::std::path::Path>>(&mut self, path: P) -> &mut Self {
106+
self.cmd.current_dir(path);
107+
self
108+
}
109+
}
110+
};
111+
}
112+
113+
pub(crate) use impl_common_helpers;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct Rustc {
3131
cmd: Command,
3232
}
3333

34-
crate::impl_common_helpers!(Rustc);
34+
crate::macros::impl_common_helpers!(Rustc);
3535

3636
#[track_caller]
3737
fn setup_common() -> Command {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Rustdoc {
2222
cmd: Command,
2323
}
2424

25-
crate::impl_common_helpers!(Rustdoc);
25+
crate::macros::impl_common_helpers!(Rustdoc);
2626

2727
#[track_caller]
2828
fn setup_common() -> Command {

0 commit comments

Comments
 (0)