diff options
Diffstat (limited to 'crate_universe/src/rendering.rs')
-rw-r--r-- | crate_universe/src/rendering.rs | 251 |
1 files changed, 205 insertions, 46 deletions
diff --git a/crate_universe/src/rendering.rs b/crate_universe/src/rendering.rs index 8a2f9fee..b9cd992e 100644 --- a/crate_universe/src/rendering.rs +++ b/crate_universe/src/rendering.rs @@ -29,14 +29,17 @@ use crate::utils::{self, sanitize_repository_name}; // to platform labels like "@rules_rust//rust/platform:x86_64-unknown-linux-gnu". pub(crate) type Platforms = BTreeMap<String, BTreeSet<String>>; -pub struct Renderer { +pub(crate) struct Renderer { config: RenderConfig, supported_platform_triples: BTreeSet<TargetTriple>, engine: TemplateEngine, } impl Renderer { - pub fn new(config: RenderConfig, supported_platform_triples: BTreeSet<TargetTriple>) -> Self { + pub(crate) fn new( + config: RenderConfig, + supported_platform_triples: BTreeSet<TargetTriple>, + ) -> Self { let engine = TemplateEngine::new(&config); Self { config, @@ -45,7 +48,7 @@ impl Renderer { } } - pub fn render(&self, context: &Context) -> Result<BTreeMap<PathBuf, String>> { + pub(crate) fn render(&self, context: &Context) -> Result<BTreeMap<PathBuf, String>> { let mut output = BTreeMap::new(); let platforms = self.render_platform_labels(context); @@ -153,6 +156,7 @@ impl Renderer { let mut exports_files = ExportsFiles { paths: BTreeSet::from(["cargo-bazel.json".to_owned(), "defs.bzl".to_owned()]), globs: Glob { + allow_empty: true, include: BTreeSet::from(["*.bazel".to_owned()]), exclude: BTreeSet::new(), }, @@ -165,6 +169,7 @@ impl Renderer { let filegroup = Filegroup { name: "srcs".to_owned(), srcs: Glob { + allow_empty: true, include: BTreeSet::from(["*.bazel".to_owned(), "*.bzl".to_owned()]), exclude: BTreeSet::new(), }, @@ -190,7 +195,11 @@ impl Renderer { } else { rename.clone() }, - actual: self.crate_label(&krate.name, &krate.version, library_target_name), + actual: self.crate_label( + &krate.name, + &krate.version.to_string(), + library_target_name, + ), tags: BTreeSet::from(["manual".to_owned()]), }); } @@ -199,7 +208,7 @@ impl Renderer { dependencies.push(Alias { rule: alias_rule.rule(), name: alias.clone(), - actual: self.crate_label(&krate.name, &krate.version, target), + actual: self.crate_label(&krate.name, &krate.version.to_string(), target), tags: BTreeSet::from(["manual".to_owned()]), }); } @@ -240,7 +249,7 @@ impl Renderer { }, actual: self.crate_label( &krate.name, - &krate.version, + &krate.version.to_string(), &format!("{}__bin", bin.crate_name), ), tags: BTreeSet::from(["manual".to_owned()]), @@ -275,7 +284,7 @@ impl Renderer { let label = match render_build_file_template( &self.config.build_file_template, &id.name, - &id.version, + &id.version.to_string(), ) { Ok(label) => label, Err(e) => bail!(e), @@ -340,7 +349,7 @@ impl Renderer { name: "package_info".to_owned(), package_name: krate.name.clone(), package_url: krate.package_url.clone().unwrap_or_default(), - package_version: krate.version.clone(), + package_version: krate.version.to_string(), })); if has_license_ids { @@ -372,7 +381,7 @@ impl Renderer { starlark.push(Starlark::Alias(Alias { rule: AliasRule::default().rule(), name: target.crate_name.clone(), - actual: Label::from_str(&format!(":{}_build_script", krate.name)).unwrap(), + actual: Label::from_str(&format!(":{}_bs", krate.name)).unwrap(), tags: BTreeSet::from(["manual".to_owned()]), })); } @@ -426,7 +435,9 @@ impl Renderer { // on having certain Cargo environment variables set. // // Do not change this name to "cargo_build_script". - name: format!("{}_build_script", krate.name), + // + // This is set to a short suffix to avoid long path name issues on windows. + name: format!("{}_bs", krate.name), aliases: SelectDict::new(self.make_aliases(krate, true, false), platforms), build_script_env: SelectDict::new( attrs @@ -711,7 +722,7 @@ impl Renderer { if let Some(alias) = &dependency.alias { let label = self.crate_label( &dependency.id.name, - &dependency.id.version, + &dependency.id.version.to_string(), &dependency.target, ); aliases.insert((label, alias.clone()), configuration.clone()); @@ -727,7 +738,9 @@ impl Renderer { extra_deps: Select<BTreeSet<Label>>, ) -> Select<BTreeSet<Label>> { Select::merge( - deps.map(|dep| self.crate_label(&dep.id.name, &dep.id.version, &dep.target)), + deps.map(|dep| { + self.crate_label(&dep.id.name, &dep.id.version.to_string(), &dep.target) + }), extra_deps, ) } @@ -767,7 +780,7 @@ impl Renderer { } /// Write a set of [crate::context::crate_context::CrateContext] to disk. -pub fn write_outputs( +pub(crate) fn write_outputs( outputs: BTreeMap<PathBuf, String>, out_dir: &Path, dry_run: bool, @@ -805,7 +818,7 @@ pub fn write_outputs( } /// Render the Bazel label of a crate -pub fn render_crate_bazel_label( +pub(crate) fn render_crate_bazel_label( template: &str, repository_name: &str, name: &str, @@ -820,7 +833,7 @@ pub fn render_crate_bazel_label( } /// Render the Bazel label of a crate -pub fn render_crate_bazel_repository( +pub(crate) fn render_crate_bazel_repository( template: &str, repository_name: &str, name: &str, @@ -833,14 +846,14 @@ pub fn render_crate_bazel_repository( } /// Render the Bazel label of a crate -pub fn render_crate_build_file(template: &str, name: &str, version: &str) -> String { +pub(crate) fn render_crate_build_file(template: &str, name: &str, version: &str) -> String { template .replace("{name}", name) .replace("{version}", version) } /// Render the Bazel label of a vendor module label -pub fn render_module_label(template: &str, name: &str) -> Result<Label> { +pub(crate) fn render_module_label(template: &str, name: &str) -> Result<Label> { Label::from_str(&template.replace("{file}", name)) } @@ -873,6 +886,7 @@ fn make_data( Data { glob: Glob { + allow_empty: true, include: glob, exclude: COMMON_GLOB_EXCLUDES .iter() @@ -896,6 +910,8 @@ mod test { use crate::metadata::Annotations; use crate::test; + const VERSION_ZERO_ONE_ZERO: semver::Version = semver::Version::new(0, 1, 0); + fn mock_target_attributes() -> TargetAttributes { TargetAttributes { crate_name: "mock_crate".to_owned(), @@ -943,14 +959,25 @@ mod test { #[test] fn render_rust_library() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), - ..CrateContext::default() + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -969,15 +996,25 @@ mod test { #[test] fn test_disable_pipelining() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, disable_pipelining: true, - ..CrateContext::default() + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -994,20 +1031,30 @@ mod test { #[test] fn render_cargo_build_script() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::BuildScript(TargetAttributes { crate_name: "build_script_build".to_owned(), crate_root: Some("build.rs".to_owned()), ..TargetAttributes::default() })]), // Build script attributes are required. + library_target_name: None, + common_attrs: CommonAttributes::default(), build_script_attrs: Some(BuildScriptAttributes::default()), - ..CrateContext::default() + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1023,20 +1070,31 @@ mod test { assert!(build_file_content.contains("\"crate-name=mock_crate\"")); // Ensure `cargo_build_script` requirements are met - assert!(build_file_content.contains("name = \"mock_crate_build_script\"")); + assert!(build_file_content.contains("name = \"mock_crate_bs\"")); } #[test] fn render_proc_macro() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::ProcMacro(mock_target_attributes())]), - ..CrateContext::default() + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1055,14 +1113,25 @@ mod test { #[test] fn render_binary() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::Binary(mock_target_attributes())]), - ..CrateContext::default() + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1081,17 +1150,27 @@ mod test { #[test] fn render_additive_build_contents() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::Binary(mock_target_attributes())]), + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, additive_build_file_content: Some( "# Hello World from additive section!".to_owned(), ), - ..CrateContext::default() + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1127,14 +1206,25 @@ mod test { #[test] fn render_crate_repositories() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), - ..CrateContext::default() + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1149,14 +1239,25 @@ mod test { #[test] fn remote_remote_vendor_mode() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), - ..CrateContext::default() + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1177,14 +1278,25 @@ mod test { #[test] fn remote_local_vendor_mode() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), - ..CrateContext::default() + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1206,7 +1318,7 @@ mod test { #[test] fn duplicate_rustc_flags() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); let rustc_flags = vec![ "-l".to_owned(), @@ -1220,12 +1332,22 @@ mod test { CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), + library_target_name: None, common_attrs: CommonAttributes { rustc_flags: Select::from_value(rustc_flags.clone()), ..CommonAttributes::default() }, - ..CrateContext::default() + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1311,7 +1433,7 @@ mod test { .collect(), ..Context::default() }; - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); let mut crate_features: Select<BTreeSet<String>> = Select::default(); crate_features.insert("foo".to_owned(), Some("aarch64-apple-darwin".to_owned())); crate_features.insert("bar".to_owned(), None); @@ -1320,12 +1442,22 @@ mod test { CrateContext { name: crate_id.name, version: crate_id.version, + package_url: None, + repository: None, targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), + library_target_name: None, common_attrs: CommonAttributes { crate_features, ..CommonAttributes::default() }, - ..CrateContext::default() + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1353,15 +1485,25 @@ mod test { #[test] fn crate_package_metadata_without_license_ids() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { name: crate_id.name, version: crate_id.version, package_url: Some("http://www.mock_crate.com/".to_owned()), + repository: None, targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), - ..CrateContext::default() + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + license: None, + license_ids: BTreeSet::default(), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), + alias_rule: None, }, ); @@ -1395,7 +1537,7 @@ mod test { #[test] fn crate_package_metadata_with_license_ids() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { @@ -1403,8 +1545,17 @@ mod test { version: crate_id.version, package_url: Some("http://www.mock_crate.com/".to_owned()), license_ids: BTreeSet::from(["Apache-2.0".to_owned(), "MIT".to_owned()]), + license_file: None, + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), - ..CrateContext::default() + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + repository: None, + license: None, + alias_rule: None, }, ); @@ -1449,7 +1600,7 @@ mod test { #[test] fn crate_package_metadata_with_license_ids_and_file() { let mut context = Context::default(); - let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned()); + let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO); context.crates.insert( crate_id.clone(), CrateContext { @@ -1458,8 +1609,16 @@ mod test { package_url: Some("http://www.mock_crate.com/".to_owned()), license_ids: BTreeSet::from(["Apache-2.0".to_owned(), "MIT".to_owned()]), license_file: Some("LICENSE.txt".to_owned()), + additive_build_file_content: None, + disable_pipelining: false, + extra_aliased_targets: BTreeMap::default(), targets: BTreeSet::from([Rule::Library(mock_target_attributes())]), - ..CrateContext::default() + library_target_name: None, + common_attrs: CommonAttributes::default(), + build_script_attrs: None, + repository: None, + license: None, + alias_rule: None, }, ); |