Skip to content

Commit

Permalink
pw_rust: Add --config support to gen_rust_project
Browse files Browse the repository at this point in the history
Change-Id: Ib9c3ef69055fcf9bae37e75ed8a013a32194e36a
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/267015
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
Pigweed-Auto-Submit: Erik Gilling <konkers@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Docs-Not-Needed: Erik Gilling <konkers@google.com>
  • Loading branch information
konkers authored and CQ Bot Account committed Feb 11, 2025
1 parent f5ee80b commit f85f008
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ single_version_override(
# gets the path to those files wrong.
# TODO: https://pwbug.dev/3392708481 - Upstream below patch
"//pw_rust/bazel_patches:0004-Add-rust_srcs_subdir-to-rust_analyzer_toolchain.patch",

# rust_analyzer: Support passing a --config option to Bazel
# https://github.com/bazelbuild/rules_rust/pull/3254
"//pw_rust/bazel_patches:0005-rust_analyzer-Support-passing-a-config-option-to-Baz.patch",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
From 24ad91945214b26ac49be84a80736530641f1ff1 Mon Sep 17 00:00:00 2001
From: Erik Gilling <konkers@google.com>
Date: Mon, 10 Feb 2025 16:49:57 -0800
Subject: [PATCH] rust_analyzer: Support passing a --config option to Bazel

This is useful if you want to analyze code using a different configuration
than the default. An example of this might be under a different platform.
---
tools/rust_analyzer/aquery.rs | 6 ++++++
tools/rust_analyzer/lib.rs | 9 +++++++++
tools/rust_analyzer/main.rs | 6 ++++++
3 files changed, 21 insertions(+)

diff --git a/tools/rust_analyzer/aquery.rs b/tools/rust_analyzer/aquery.rs
index bc98913b..74c7d97e 100644
--- a/tools/rust_analyzer/aquery.rs
+++ b/tools/rust_analyzer/aquery.rs
@@ -63,6 +63,7 @@ pub struct CrateSpecSource {

pub fn get_crate_specs(
bazel: &Path,
+ config: &Option<String>,
workspace: &Path,
execution_root: &Path,
targets: &[String],
@@ -70,6 +71,10 @@ pub fn get_crate_specs(
) -> anyhow::Result<BTreeSet<CrateSpec>> {
log::debug!("Get crate specs with targets: {:?}", targets);
let target_pattern = format!("deps({})", targets.join("+"));
+ let config_args = match config {
+ Some(config) => vec!["--config", config],
+ None => Vec::new(),
+ };

let aquery_output = Command::new(bazel)
.current_dir(workspace)
@@ -77,6 +82,7 @@ pub fn get_crate_specs(
.env_remove("BUILD_WORKING_DIRECTORY")
.env_remove("BUILD_WORKSPACE_DIRECTORY")
.arg("aquery")
+ .args(config_args)
.arg("--include_aspects")
.arg("--include_artifacts")
.arg(format!(
diff --git a/tools/rust_analyzer/lib.rs b/tools/rust_analyzer/lib.rs
index 7a6eb28d..43b2c190 100644
--- a/tools/rust_analyzer/lib.rs
+++ b/tools/rust_analyzer/lib.rs
@@ -10,11 +10,16 @@ mod rust_project;

pub fn generate_crate_info(
bazel: impl AsRef<Path>,
+ config: &Option<String>,
workspace: impl AsRef<Path>,
rules_rust: impl AsRef<str>,
targets: &[String],
) -> anyhow::Result<()> {
log::debug!("Building rust_analyzer_crate_spec files for {:?}", targets);
+ let config_args = match config {
+ Some(config) => vec!["--config", config],
+ None => Vec::new(),
+ };

let output = Command::new(bazel.as_ref())
.current_dir(workspace.as_ref())
@@ -22,6 +27,7 @@ pub fn generate_crate_info(
.env_remove("BUILD_WORKING_DIRECTORY")
.env_remove("BUILD_WORKSPACE_DIRECTORY")
.arg("build")
+ .args(config_args)
.arg("--norun_validations")
.arg(format!(
"--aspects={}//rust:defs.bzl%rust_analyzer_aspect",
@@ -42,8 +48,10 @@ pub fn generate_crate_info(
Ok(())
}

+#[allow(clippy::too_many_arguments)]
pub fn write_rust_project(
bazel: impl AsRef<Path>,
+ config: &Option<String>,
workspace: impl AsRef<Path>,
rules_rust_name: &impl AsRef<str>,
targets: &[String],
@@ -53,6 +61,7 @@ pub fn write_rust_project(
) -> anyhow::Result<()> {
let crate_specs = aquery::get_crate_specs(
bazel.as_ref(),
+ config,
workspace.as_ref(),
execution_root.as_ref(),
targets,
diff --git a/tools/rust_analyzer/main.rs b/tools/rust_analyzer/main.rs
index df4b2f9b..f1dc0143 100644
--- a/tools/rust_analyzer/main.rs
+++ b/tools/rust_analyzer/main.rs
@@ -36,6 +36,7 @@ fn main() -> anyhow::Result<()> {
// Generate the crate specs.
generate_crate_info(
&config.bazel,
+ &config.config,
workspace_root,
rules_rust_name,
&config.targets,
@@ -44,6 +45,7 @@ fn main() -> anyhow::Result<()> {
// Use the generated files to write rust-project.json.
write_rust_project(
&config.bazel,
+ &config.config,
workspace_root,
&rules_rust_name,
&config.targets,
@@ -120,6 +122,10 @@ struct Config {
#[clap(long, env = "OUTPUT_BASE")]
output_base: Option<PathBuf>,

+ /// A config to pass to Bazel invocations with `--config=<config>`.
+ #[clap(long)]
+ config: Option<String>,
+
/// The path to a Bazel binary
#[clap(long, default_value = "bazel")]
bazel: PathBuf,
--
2.48.1.502.g6dc24dfdaf-goog

0 comments on commit f85f008

Please sign in to comment.