Skip to content

Commit 427cf94

Browse files
committed
run_make_support: move impl_common_helpers into own macros module
1 parent cb12b52 commit 427cf94

File tree

7 files changed

+125
-122
lines changed

7 files changed

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

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ pub struct LlvmAr {
7070
cmd: Command,
7171
}
7272

73-
crate::impl_common_helpers!(LlvmReadobj);
74-
crate::impl_common_helpers!(LlvmProfdata);
75-
crate::impl_common_helpers!(LlvmFilecheck);
76-
crate::impl_common_helpers!(LlvmObjdump);
77-
crate::impl_common_helpers!(LlvmAr);
73+
crate::macros::impl_common_helpers!(LlvmReadobj);
74+
crate::macros::impl_common_helpers!(LlvmProfdata);
75+
crate::macros::impl_common_helpers!(LlvmFilecheck);
76+
crate::macros::impl_common_helpers!(LlvmObjdump);
77+
crate::macros::impl_common_helpers!(LlvmAr);
7878

7979
/// Generate the path to the bin directory of LLVM.
8080
#[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::macros::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)