diff --git a/tools/cargo_embargo/src/cargo_out.rs b/tools/cargo_embargo/src/cargo_out.rs index c0648416a..fcf467f88 100644 --- a/tools/cargo_embargo/src/cargo_out.rs +++ b/tools/cargo_embargo/src/cargo_out.rs @@ -35,6 +35,8 @@ pub enum CrateType { ProcMacro, // --test Test, + // "--cfg test" without --test. (Assume it is a test with the harness disabled. + TestNoHarness, } /// Info extracted from `CargoOut` for a crate. @@ -383,8 +385,13 @@ impl Crate { if out.main_src.as_os_str().is_empty() { bail!("missing main source file"); } + // Must have at least one type. if out.types.is_empty() { - bail!("must specify one of --test or --crate-type"); + if out.cfgs.contains(&"test".to_string()) { + out.types.push(CrateType::TestNoHarness); + } else { + bail!("failed to detect crate type. did not have --crate-type or --test or '--cfg test'"); + } } if out.types.contains(&CrateType::Test) && out.types.len() != 1 { bail!("cannot specify both --test and --crate-type"); diff --git a/tools/cargo_embargo/src/main.rs b/tools/cargo_embargo/src/main.rs index 80645985e..dd2caf594 100644 --- a/tools/cargo_embargo/src/main.rs +++ b/tools/cargo_embargo/src/main.rs @@ -360,7 +360,14 @@ fn write_android_bp( }; for c in crates { - modules.extend(crate_to_bp_modules(c, cfg, package_cfg, &extra_srcs)?); + modules.extend(crate_to_bp_modules(c, cfg, package_cfg, &extra_srcs).with_context( + || { + format!( + "failed to generate bp module for crate \"{}\" with package name \"{}\"", + c.name, c.package_name + ) + }, + )?); } if modules.is_empty() { return Ok(()); @@ -444,10 +451,17 @@ fn crate_to_bp_modules( let stem = "lib".to_string() + &crate_.name; ("rust_proc_macro".to_string(), stem.clone(), stem) } - CrateType::Test => { + CrateType::Test | CrateType::TestNoHarness => { let suffix = crate_.main_src.to_string_lossy().into_owned(); let suffix = suffix.replace('/', "_").replace(".rs", ""); let stem = crate_.package_name.clone() + "_test_" + &suffix; + if crate_type == &CrateType::TestNoHarness { + eprintln!( + "WARNING: ignoring test \"{}\" with harness=false. not supported yet", + stem + ); + return Ok(Vec::new()); + } ("rust_test".to_string() + host, stem.clone(), stem) } };