Skip to content

Commit e5d3a24

Browse files
authored
Merge pull request #167 from jyn514/less-metadata
Don't run `cargo metadata` for tools that don't need it
2 parents ad48aa5 + db53115 commit e5d3a24

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct Context {
4545
impl Context {
4646
/* Constructors */
4747
/// Get a context structure from a built artifact.
48-
fn from_artifact(metadata: Metadata, artifact: &Artifact) -> Result<Self> {
48+
fn from_artifact(metadata: &Metadata, artifact: &Artifact) -> Result<Self> {
4949
// Currently there is no clean way to get the target triple from cargo so we can only make
5050
// an approximation, we do this by extracting the target triple from the artifacts path.
5151
// For more info on the path structure see: https://doc.rust-lang.org/cargo/guide/build-cache.html
@@ -54,7 +54,7 @@ impl Context {
5454
// See: https://github.com/rust-lang/cargo/issues/5579, https://github.com/rust-lang/cargo/issues/8002
5555

5656
// Should always succeed.
57-
let target_path = artifact.filenames[0].strip_prefix(metadata.target_directory)?;
57+
let target_path = artifact.filenames[0].strip_prefix(&metadata.target_directory)?;
5858
let target_name = if let Some(Utf8Component::Normal(path)) = target_path.components().next()
5959
{
6060
// TODO: How will custom profiles impact this?
@@ -279,8 +279,9 @@ To see all the flags the proxied tool accepts run `cargo-{} -- --help`.{}",
279279
}
280280
}
281281

282-
pub fn run(tool: Tool, matches: ArgMatches) -> Result<i32> {
282+
fn get_metadata(tool: &Tool, matches: &ArgMatches) -> Result<Metadata> {
283283
let mut metadata_command = MetadataCommand::new();
284+
metadata_command.no_deps();
284285
if tool.needs_build() {
285286
if let Some(features) = matches.get_many::<String>("features") {
286287
metadata_command.features(CargoOpt::SomeFeatures(
@@ -302,6 +303,10 @@ pub fn run(tool: Tool, matches: ArgMatches) -> Result<i32> {
302303
bail!("Unable to find workspace members");
303304
}
304305

306+
Ok(metadata)
307+
}
308+
309+
pub fn run(tool: Tool, matches: ArgMatches) -> Result<i32> {
305310
let mut tool_args = vec![];
306311
if let Some(args) = matches.get_many::<String>("args") {
307312
tool_args.extend(args.map(|s| s.as_str()));
@@ -310,19 +315,20 @@ pub fn run(tool: Tool, matches: ArgMatches) -> Result<i32> {
310315
let tool_help = tool_args.first() == Some(&"--help");
311316

312317
let target_artifact = if tool.needs_build() && !tool_help {
313-
cargo_build(&matches, &metadata)?
318+
let metadata = get_metadata(&tool, &matches)?;
319+
cargo_build(&matches, &metadata)?.map(|a| (a, metadata))
314320
} else {
315321
None
316322
};
317323

318324
let mut lltool = Command::new(format!("rust-{}", tool.name()));
319325

320326
if tool == Tool::Objdump {
321-
let ctxt = if let Some(artifact) = &target_artifact {
327+
let ctxt = if let Some((artifact, metadata)) = &target_artifact {
322328
Context::from_artifact(metadata, artifact)?
323329
} else {
324330
Context::from_flag(
325-
metadata,
331+
get_metadata(&tool, &matches)?,
326332
matches.get_one::<String>("target").map(|s| s.as_str()),
327333
)?
328334
};
@@ -347,7 +353,7 @@ pub fn run(tool: Tool, matches: ArgMatches) -> Result<i32> {
347353

348354
if tool.needs_build() {
349355
// Artifact
350-
if let Some(artifact) = &target_artifact {
356+
if let Some((artifact, _)) = &target_artifact {
351357
let file = match &artifact.executable {
352358
// Example and bins have an executable
353359
Some(val) => val,

0 commit comments

Comments
 (0)