Skip to content

Commit 5d7148d

Browse files
committed
add should cache to bootstrap command
1 parent ab747c1 commit 5d7148d

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

src/bootstrap/src/utils/exec.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ impl OutputMode {
5757

5858
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
5959
pub struct CommandCacheKey {
60-
program: String,
61-
args: Vec<String>,
62-
envs: Vec<(String, String)>,
60+
program: OsString,
61+
args: Vec<OsString>,
62+
envs: Vec<(OsString, OsString)>,
6363
cwd: Option<PathBuf>,
6464
}
6565

@@ -77,32 +77,39 @@ pub struct CommandCacheKey {
7777
/// [allow_failure]: BootstrapCommand::allow_failure
7878
/// [delay_failure]: BootstrapCommand::delay_failure
7979
pub struct BootstrapCommand {
80-
program: OsString,
81-
args: Vec<OsString>,
82-
envs: Vec<(OsString, OsString)>,
83-
cwd: Option<PathBuf>,
84-
80+
cache_key: CommandCacheKey,
8581
command: Command,
8682
pub failure_behavior: BehaviorOnFailure,
8783
// Run the command even during dry run
8884
pub run_always: bool,
8985
// This field makes sure that each command is executed (or disarmed) before it is dropped,
9086
// to avoid forgetting to execute a command.
9187
drop_bomb: DropBomb,
88+
should_cache: bool,
9289
}
9390

9491
impl<'a> BootstrapCommand {
9592
#[track_caller]
9693
pub fn new<S: AsRef<OsStr>>(program: S) -> Self {
97-
Command::new(program).into()
94+
Self {
95+
should_cache: true,
96+
cache_key: CommandCacheKey {
97+
program: program.as_ref().to_os_string(),
98+
..CommandCacheKey::default()
99+
},
100+
..Command::new(program).into()
101+
}
98102
}
99-
100103
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
101-
self.args.push(arg.as_ref().to_os_string());
104+
self.cache_key.args.push(arg.as_ref().to_os_string());
102105
self.command.arg(arg.as_ref());
103106
self
104107
}
105108

109+
pub fn should_cache(&self) -> bool {
110+
self.should_cache
111+
}
112+
106113
pub fn args<I, S>(&mut self, args: I) -> &mut Self
107114
where
108115
I: IntoIterator<Item = S>,
@@ -119,7 +126,7 @@ impl<'a> BootstrapCommand {
119126
K: AsRef<OsStr>,
120127
V: AsRef<OsStr>,
121128
{
122-
self.envs.push((key.as_ref().to_os_string(), val.as_ref().to_os_string()));
129+
self.cache_key.envs.push((key.as_ref().to_os_string(), val.as_ref().to_os_string()));
123130
self.command.env(key, val);
124131
self
125132
}
@@ -138,7 +145,7 @@ impl<'a> BootstrapCommand {
138145
}
139146

140147
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self {
141-
self.cwd = Some(dir.as_ref().to_path_buf());
148+
self.cache_key.cwd = Some(dir.as_ref().to_path_buf());
142149
self.command.current_dir(dir);
143150
self
144151
}
@@ -205,6 +212,7 @@ impl<'a> BootstrapCommand {
205212
// We don't know what will happen with the returned command, so we need to mark this
206213
// command as executed proactively.
207214
self.mark_as_executed();
215+
self.should_cache = false;
208216
&mut self.command
209217
}
210218

@@ -232,12 +240,7 @@ impl<'a> BootstrapCommand {
232240
}
233241

234242
pub fn cache_key(&self) -> CommandCacheKey {
235-
CommandCacheKey {
236-
program: self.program.clone(),
237-
args: self.args.clone(),
238-
envs: self.envs.clone(),
239-
cwd: self.cwd.clone(),
240-
}
243+
self.cache_key.clone()
241244
}
242245
}
243246

@@ -254,10 +257,8 @@ impl From<Command> for BootstrapCommand {
254257
let program = command.get_program().to_owned();
255258

256259
Self {
257-
program: program.clone(),
258-
args: Vec::new(),
259-
envs: Vec::new(),
260-
cwd: None,
260+
cache_key: CommandCacheKey::default(),
261+
should_cache: false,
261262
command,
262263
failure_behavior: BehaviorOnFailure::Exit,
263264
run_always: false,

src/bootstrap/src/utils/execution_context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ impl ExecutionContext {
148148
) -> CommandOutput {
149149
let cache_key = command.cache_key();
150150

151-
if let Some(cached_output) = self.command_cache.get(&cache_key) {
151+
if command.should_cache()
152+
&& let Some(cached_output) = self.command_cache.get(&cache_key)
153+
{
152154
command.mark_as_executed();
153155
if self.dry_run() && !command.run_always {
154156
return CommandOutput::default();
@@ -159,7 +161,7 @@ impl ExecutionContext {
159161

160162
let output = self.start(command, stdout, stderr).wait_for_output(self);
161163

162-
if !self.dry_run() || command.run_always {
164+
if !self.dry_run() || command.run_always && command.should_cache() {
163165
self.command_cache.insert(cache_key, output.clone());
164166
}
165167

0 commit comments

Comments
 (0)