gen.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright Materialize, Inc. and contributors. All rights reserved.
  2. //
  3. // Use of this software is governed by the Business Source License
  4. // included in the LICENSE file.
  5. //
  6. // As of the Change Date specified in that file, in accordance with
  7. // the Business Source License, use of this software will be governed
  8. // by the Apache License, Version 2.0.
  9. use anyhow::Context;
  10. use cargo_toml::Manifest;
  11. use cargo_gazelle::BazelBuildFile;
  12. use cargo_gazelle::config::{CrateConfig, GlobalConfig};
  13. use cargo_gazelle::context::CrateContext;
  14. use cargo_gazelle::header::BazelHeader;
  15. use cargo_gazelle::targets::{CargoBuildScript, RustLibrary, RustTarget};
  16. pub fn main() -> Result<(), anyhow::Error> {
  17. let path = "../../../src/proto/Cargo.toml";
  18. let graph = guppy::MetadataCommand::new()
  19. .manifest_path(path)
  20. .build_graph()
  21. .context("building crate graph")?;
  22. let manifest = Manifest::from_path(path).context("reading manifest")?;
  23. let package = graph
  24. .workspace()
  25. .member_by_name(manifest.package().name())?;
  26. let config = GlobalConfig::default();
  27. let crate_config = CrateConfig::new(&package);
  28. let crate_context = CrateContext::generate(&config, &crate_config, &package)?;
  29. let build_script =
  30. CargoBuildScript::generate(&config, &crate_context, &crate_config, &package)?.unwrap();
  31. let library = RustLibrary::generate(&config, &package, &crate_config, Some(&build_script))?;
  32. #[allow(clippy::as_conversions)]
  33. let targets = vec![
  34. Box::new(library) as Box<dyn RustTarget>,
  35. Box::new(build_script),
  36. ];
  37. let bazel_file = BazelBuildFile {
  38. header: BazelHeader::generate(&targets[..]),
  39. targets,
  40. };
  41. println!("{bazel_file}");
  42. Ok(())
  43. }