cargo_embargo: Parse out raw_name from src_path
When cargo_embargo is configured to use `cargo metadata` instead of
running `cargo build`, we don't get the expected raw name for extern
crates unless we expect the source path. This in turn means we don't
generate the correct crate names for Trusty rules.mk files.
Test: manually, generate rules.mk for ciborium then build Trusty
Change-Id: Ie8c5b2868cd56b7f7e6d8294b41465682f5ac734
diff --git a/tools/cargo_embargo/src/cargo/metadata.rs b/tools/cargo_embargo/src/cargo/metadata.rs
index 1c10b63..5f7ed38 100644
--- a/tools/cargo_embargo/src/cargo/metadata.rs
+++ b/tools/cargo_embargo/src/cargo/metadata.rs
@@ -283,7 +283,11 @@
bail!("Package {} didn't have any library or proc-macro targets", dependency.name);
};
let lib_name = target.name.replace('-', "_");
- let raw_name = target.name.clone();
+ // This is ugly but looking at the source path is the easiest way to tell if the raw
+ // crate name uses a hyphen instead of an underscore. It won't work if it uses both.
+ let raw_name = target.name.replace('_', "-");
+ let src_path = target.src_path.to_str().expect("failed to convert src_path to string");
+ let raw_name = if src_path.contains(&raw_name) { raw_name } else { lib_name.clone() };
let name =
if let Some(rename) = &dependency.rename { rename.clone() } else { lib_name.clone() };
@@ -294,7 +298,6 @@
} else {
ExternType::Rust
};
-
Ok(Extern { name, lib_name, raw_name, extern_type })
}