1 //! Workspace information we get from cargo consists of two pieces. The first is
2 //! the output of `cargo metadata`. The second is the output of running
3 //! `build.rs` files (`OUT_DIR` env var, extra cfg flags) and compiling proc
6 //! This module implements this second part. We use "build script" terminology
7 //! here, but it covers procedural macros as well.
9 use std::{cell::RefCell, io, path::PathBuf, process::Command};
11 use cargo_metadata::{camino::Utf8Path, Message};
12 use la_arena::ArenaMap;
13 use paths::AbsPathBuf;
14 use rustc_hash::FxHashMap;
16 use serde::Deserialize;
18 use crate::{cfg_flag::CfgFlag, CargoConfig, CargoWorkspace, Package};
20 #[derive(Debug, Default, Clone, PartialEq, Eq)]
21 pub struct WorkspaceBuildScripts {
22 outputs: ArenaMap<Package, Option<BuildScriptOutput>>,
23 error: Option<String>,
26 #[derive(Debug, Clone, Default, PartialEq, Eq)]
27 pub(crate) struct BuildScriptOutput {
28 /// List of config flags defined by this package's build script.
29 pub(crate) cfgs: Vec<CfgFlag>,
30 /// List of cargo-related environment variables with their value.
32 /// If the package has a build script which defines environment variables,
33 /// they can also be found here.
34 pub(crate) envs: Vec<(String, String)>,
35 /// Directory where a build script might place its output.
36 pub(crate) out_dir: Option<AbsPathBuf>,
37 /// Path to the proc-macro library file if this package exposes proc-macros.
38 pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
41 impl WorkspaceBuildScripts {
42 fn build_command(config: &CargoConfig) -> Command {
43 if let Some([program, args @ ..]) = config.run_build_script_command.as_deref() {
44 let mut cmd = Command::new(program);
46 cmd.envs(&config.extra_env);
50 let mut cmd = Command::new(toolchain::cargo());
51 cmd.envs(&config.extra_env);
53 cmd.args(&["check", "--quiet", "--workspace", "--message-format=json"]);
55 // --all-targets includes tests, benches and examples in addition to the
56 // default lib and bins. This is an independent concept from the --targets
58 cmd.arg("--all-targets");
60 if let Some(target) = &config.target {
61 cmd.args(&["--target", target]);
64 if config.all_features {
65 cmd.arg("--all-features");
67 if config.no_default_features {
68 cmd.arg("--no-default-features");
70 if !config.features.is_empty() {
71 cmd.arg("--features");
72 cmd.arg(config.features.join(" "));
81 workspace: &CargoWorkspace,
82 progress: &dyn Fn(String),
83 toolchain: &Option<Version>,
84 ) -> io::Result<WorkspaceBuildScripts> {
85 const RUST_1_62: Version = Version::new(1, 62, 0);
87 match Self::run_(Self::build_command(config), config, workspace, progress) {
88 Ok(WorkspaceBuildScripts { error: Some(error), .. })
89 if toolchain.as_ref().map_or(false, |it| *it >= RUST_1_62) =>
91 // building build scripts failed, attempt to build with --keep-going so
92 // that we potentially get more build data
93 let mut cmd = Self::build_command(config);
94 cmd.args(&["-Z", "unstable-options", "--keep-going"]).env("RUSTC_BOOTSTRAP", "1");
95 let mut res = Self::run_(cmd, config, workspace, progress)?;
96 res.error = Some(error);
105 config: &CargoConfig,
106 workspace: &CargoWorkspace,
107 progress: &dyn Fn(String),
108 ) -> io::Result<WorkspaceBuildScripts> {
109 if config.wrap_rustc_in_build_scripts {
110 // Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
111 // that to compile only proc macros and build scripts during the initial
113 let myself = std::env::current_exe()?;
114 cmd.env("RUSTC_WRAPPER", myself);
115 cmd.env("RA_RUSTC_WRAPPER", "1");
118 cmd.current_dir(workspace.workspace_root());
120 let mut res = WorkspaceBuildScripts::default();
121 let outputs = &mut res.outputs;
122 // NB: Cargo.toml could have been modified between `cargo metadata` and
123 // `cargo check`. We shouldn't assume that package ids we see here are
124 // exactly those from `config`.
125 let mut by_id: FxHashMap<String, Package> = FxHashMap::default();
126 for package in workspace.packages() {
127 outputs.insert(package, None);
128 by_id.insert(workspace[package].id.clone(), package);
131 let errors = RefCell::new(String::new());
132 let push_err = |err: &str| {
133 let mut e = errors.borrow_mut();
138 tracing::info!("Running build scripts: {:?}", cmd);
139 let output = stdx::process::spawn_with_streaming_output(
142 // Copy-pasted from existing cargo_metadata. It seems like we
143 // should be using serde_stacker here?
144 let mut deserializer = serde_json::Deserializer::from_str(line);
145 deserializer.disable_recursion_limit();
146 let message = Message::deserialize(&mut deserializer)
147 .unwrap_or_else(|_| Message::TextLine(line.to_string()));
150 Message::BuildScriptExecuted(message) => {
151 let package = match by_id.get(&message.package_id.repr) {
156 let mut acc = Vec::new();
157 for cfg in message.cfgs {
158 match cfg.parse::<CfgFlag>() {
159 Ok(it) => acc.push(it),
162 "invalid cfg from cargo-metadata: {}",
171 // cargo_metadata crate returns default (empty) path for
172 // older cargos, which is not absolute, so work around that.
173 let out_dir = message.out_dir.into_os_string();
174 if !out_dir.is_empty() {
175 let data = outputs[package].get_or_insert_with(Default::default);
176 data.out_dir = Some(AbsPathBuf::assert(PathBuf::from(out_dir)));
179 if !message.env.is_empty() {
180 outputs[package].get_or_insert_with(Default::default).envs =
184 Message::CompilerArtifact(message) => {
185 let package = match by_id.get(&message.package_id.repr) {
190 progress(format!("metadata {}", message.target.name));
192 if message.target.kind.iter().any(|k| k == "proc-macro") {
194 if let Some(filename) =
195 message.filenames.iter().find(|name| is_dylib(name))
197 let filename = AbsPathBuf::assert(PathBuf::from(&filename));
199 .get_or_insert_with(Default::default)
200 .proc_macro_dylib_path = Some(filename);
204 Message::CompilerMessage(message) => {
205 progress(message.target.name);
207 if let Some(diag) = message.message.rendered.as_deref() {
211 Message::BuildFinished(_) => {}
212 Message::TextLine(_) => {}
221 for package in workspace.packages() {
222 if let Some(package_build_data) = &mut outputs[package] {
225 workspace[package].manifest.parent().display(),
228 // inject_cargo_env(package, package_build_data);
229 if let Some(out_dir) = &package_build_data.out_dir {
230 // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
231 if let Some(out_dir) = out_dir.as_os_str().to_str().map(|s| s.to_owned()) {
232 package_build_data.envs.push(("OUT_DIR".to_string(), out_dir));
238 let mut errors = errors.into_inner();
239 if !output.status.success() {
240 if errors.is_empty() {
241 errors = "cargo check failed".to_string();
243 res.error = Some(errors);
249 pub fn error(&self) -> Option<&str> {
250 self.error.as_deref()
253 pub(crate) fn get_output(&self, idx: Package) -> Option<&BuildScriptOutput> {
254 self.outputs.get(idx)?.as_ref()
258 // FIXME: File a better way to know if it is a dylib.
259 fn is_dylib(path: &Utf8Path) -> bool {
260 match path.extension().map(|e| e.to_string().to_lowercase()) {
262 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),