Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions cargo-auditable/src/auditable_from_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,13 @@ fn proc_macro_packages(metadata: &cargo_metadata::Metadata) -> HashSet<&str> {
.packages
.iter()
.filter_map(|pkg| {
// As of Rust 1.88 a single crate cannot be both a proc macro and something else.
// Checking that length is 1 is purely to hedge against support for it being added in the future.
if pkg.targets.len() == 1
&& pkg.targets[0].kind.len() == 1
&& pkg.targets[0].kind[0] == TargetKind::ProcMacro
{
// As of Rust 1.100 a single crate cannot be both a proc macro and something else.
// There may be extra targets such as tests or a build script, so we ignore those.
let is_proc_macro = pkg
.targets
.iter()
.any(|target| target.kind.len() == 1 && target.kind[0] == TargetKind::ProcMacro);
if is_proc_macro {
Some(pkg.id.repr.as_str())
} else {
None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[workspace]
resolver = "2"
members = ["app", "derive", "macro_only_dep", "shared"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "app"
version = "0.1.0"
edition = "2021"

[dependencies]
derive = { path = "../derive" }
shared = { path = "../shared" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "derive"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
macro_only_dep = { path = "../macro_only_dep" }
shared = { path = "../shared" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro]
pub fn derive(_input: TokenStream) -> TokenStream {
TokenStream::new()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[test]
fn smoke() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "macro_only_dep"
version = "0.1.0"
edition = "2021"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn helper() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "shared"
version = "0.1.0"
edition = "2021"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn runtime() {}
42 changes: 42 additions & 0 deletions cargo-auditable/tests/it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,48 @@ fn test_proc_macro_inner(sbom: bool) {
assert_eq!(syn_info.kind, DependencyKind::Build);
}

#[test]
fn test_proc_macro_with_test_target() {
test_proc_macro_with_test_target_inner(false);
test_proc_macro_with_test_target_inner(true);
}

fn test_proc_macro_with_test_target_inner(sbom: bool) {
// The proc macro crate has an integration test target, so it has more than one target.
// It must still be recorded as a build-time-only dependency.
let workspace_cargo_toml = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/proc_macro_dependency/Cargo.toml");
let bins = run_cargo_auditable(workspace_cargo_toml, &[], &[], sbom);
eprintln!("Proc macro with test target binary map: {bins:?}");

let binary = &bins.get("app").unwrap()[0];
let dep_info = get_dependency_info(binary);
eprintln!("{binary} dependency info: {dep_info:?}");

let derive_info = dep_info
.packages
.iter()
.find(|p| p.name == "derive")
.expect("Could not find 'derive' in the embedded dependency list!");
assert_eq!(derive_info.kind, DependencyKind::Build);

// Dependencies only reachable through the proc macro are also build-time-only.
let macro_only_dep_info = dep_info
.packages
.iter()
.find(|p| p.name == "macro_only_dep")
.expect("Could not find 'macro_only_dep' in the embedded dependency list!");
assert_eq!(macro_only_dep_info.kind, DependencyKind::Build);

// A dependency used by both the app and the proc macro remains a runtime dependency.
let shared_info = dep_info
.packages
.iter()
.find(|p| p.name == "shared")
.expect("Could not find 'shared' in the embedded dependency list!");
assert_eq!(shared_info.kind, DependencyKind::Runtime);
}

#[test]
fn test_bare_linker() {
test_bare_linker_inner(false);
Expand Down
Loading