]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs
Rollup merge of #101219 - ehuss:update-books, r=ehuss
[rust.git] / src / tools / rust-analyzer / crates / project-model / src / build_scripts.rs
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
4 //! macro.
5 //!
6 //! This module implements this second part. We use "build script" terminology
7 //! here, but it covers procedural macros as well.
8
9 use std::{cell::RefCell, io, path::PathBuf, process::Command};
10
11 use cargo_metadata::{camino::Utf8Path, Message};
12 use la_arena::ArenaMap;
13 use paths::AbsPathBuf;
14 use rustc_hash::FxHashMap;
15 use semver::Version;
16 use serde::Deserialize;
17
18 use crate::{cfg_flag::CfgFlag, CargoConfig, CargoWorkspace, Package};
19
20 #[derive(Debug, Default, Clone, PartialEq, Eq)]
21 pub struct WorkspaceBuildScripts {
22     outputs: ArenaMap<Package, Option<BuildScriptOutput>>,
23     error: Option<String>,
24 }
25
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.
31     ///
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>,
39 }
40
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);
45             cmd.args(args);
46             return cmd;
47         }
48
49         let mut cmd = Command::new(toolchain::cargo());
50
51         cmd.args(&["check", "--quiet", "--workspace", "--message-format=json"]);
52
53         // --all-targets includes tests, benches and examples in addition to the
54         // default lib and bins. This is an independent concept from the --targets
55         // flag below.
56         cmd.arg("--all-targets");
57
58         if let Some(target) = &config.target {
59             cmd.args(&["--target", target]);
60         }
61
62         if config.all_features {
63             cmd.arg("--all-features");
64         } else {
65             if config.no_default_features {
66                 cmd.arg("--no-default-features");
67             }
68             if !config.features.is_empty() {
69                 cmd.arg("--features");
70                 cmd.arg(config.features.join(" "));
71             }
72         }
73
74         cmd
75     }
76
77     pub(crate) fn run(
78         config: &CargoConfig,
79         workspace: &CargoWorkspace,
80         progress: &dyn Fn(String),
81         toolchain: &Option<Version>,
82     ) -> io::Result<WorkspaceBuildScripts> {
83         const RUST_1_62: Version = Version::new(1, 62, 0);
84
85         match Self::run_(Self::build_command(config), config, workspace, progress) {
86             Ok(WorkspaceBuildScripts { error: Some(error), .. })
87                 if toolchain.as_ref().map_or(false, |it| *it >= RUST_1_62) =>
88             {
89                 // building build scripts failed, attempt to build with --keep-going so
90                 // that we potentially get more build data
91                 let mut cmd = Self::build_command(config);
92                 cmd.args(&["-Z", "unstable-options", "--keep-going"]).env("RUSTC_BOOTSTRAP", "1");
93                 let mut res = Self::run_(cmd, config, workspace, progress)?;
94                 res.error = Some(error);
95                 Ok(res)
96             }
97             res => res,
98         }
99     }
100
101     fn run_(
102         mut cmd: Command,
103         config: &CargoConfig,
104         workspace: &CargoWorkspace,
105         progress: &dyn Fn(String),
106     ) -> io::Result<WorkspaceBuildScripts> {
107         if config.wrap_rustc_in_build_scripts {
108             // Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
109             // that to compile only proc macros and build scripts during the initial
110             // `cargo check`.
111             let myself = std::env::current_exe()?;
112             cmd.env("RUSTC_WRAPPER", myself);
113             cmd.env("RA_RUSTC_WRAPPER", "1");
114         }
115
116         cmd.current_dir(workspace.workspace_root());
117
118         let mut res = WorkspaceBuildScripts::default();
119         let outputs = &mut res.outputs;
120         // NB: Cargo.toml could have been modified between `cargo metadata` and
121         // `cargo check`. We shouldn't assume that package ids we see here are
122         // exactly those from `config`.
123         let mut by_id: FxHashMap<String, Package> = FxHashMap::default();
124         for package in workspace.packages() {
125             outputs.insert(package, None);
126             by_id.insert(workspace[package].id.clone(), package);
127         }
128
129         let errors = RefCell::new(String::new());
130         let push_err = |err: &str| {
131             let mut e = errors.borrow_mut();
132             e.push_str(err);
133             e.push('\n');
134         };
135
136         tracing::info!("Running build scripts: {:?}", cmd);
137         let output = stdx::process::spawn_with_streaming_output(
138             cmd,
139             &mut |line| {
140                 // Copy-pasted from existing cargo_metadata. It seems like we
141                 // should be using serde_stacker here?
142                 let mut deserializer = serde_json::Deserializer::from_str(line);
143                 deserializer.disable_recursion_limit();
144                 let message = Message::deserialize(&mut deserializer)
145                     .unwrap_or_else(|_| Message::TextLine(line.to_string()));
146
147                 match message {
148                     Message::BuildScriptExecuted(message) => {
149                         let package = match by_id.get(&message.package_id.repr) {
150                             Some(&it) => it,
151                             None => return,
152                         };
153                         let cfgs = {
154                             let mut acc = Vec::new();
155                             for cfg in message.cfgs {
156                                 match cfg.parse::<CfgFlag>() {
157                                     Ok(it) => acc.push(it),
158                                     Err(err) => {
159                                         push_err(&format!(
160                                             "invalid cfg from cargo-metadata: {}",
161                                             err
162                                         ));
163                                         return;
164                                     }
165                                 };
166                             }
167                             acc
168                         };
169                         // cargo_metadata crate returns default (empty) path for
170                         // older cargos, which is not absolute, so work around that.
171                         let out_dir = message.out_dir.into_os_string();
172                         if !out_dir.is_empty() {
173                             let data = outputs[package].get_or_insert_with(Default::default);
174                             data.out_dir = Some(AbsPathBuf::assert(PathBuf::from(out_dir)));
175                             data.cfgs = cfgs;
176                         }
177                         if !message.env.is_empty() {
178                             outputs[package].get_or_insert_with(Default::default).envs =
179                                 message.env;
180                         }
181                     }
182                     Message::CompilerArtifact(message) => {
183                         let package = match by_id.get(&message.package_id.repr) {
184                             Some(it) => *it,
185                             None => return,
186                         };
187
188                         progress(format!("metadata {}", message.target.name));
189
190                         if message.target.kind.iter().any(|k| k == "proc-macro") {
191                             // Skip rmeta file
192                             if let Some(filename) =
193                                 message.filenames.iter().find(|name| is_dylib(name))
194                             {
195                                 let filename = AbsPathBuf::assert(PathBuf::from(&filename));
196                                 outputs[package]
197                                     .get_or_insert_with(Default::default)
198                                     .proc_macro_dylib_path = Some(filename);
199                             }
200                         }
201                     }
202                     Message::CompilerMessage(message) => {
203                         progress(message.target.name);
204
205                         if let Some(diag) = message.message.rendered.as_deref() {
206                             push_err(diag);
207                         }
208                     }
209                     Message::BuildFinished(_) => {}
210                     Message::TextLine(_) => {}
211                     _ => {}
212                 }
213             },
214             &mut |line| {
215                 push_err(line);
216             },
217         )?;
218
219         for package in workspace.packages() {
220             if let Some(package_build_data) = &mut outputs[package] {
221                 tracing::info!(
222                     "{}: {:?}",
223                     workspace[package].manifest.parent().display(),
224                     package_build_data,
225                 );
226                 // inject_cargo_env(package, package_build_data);
227                 if let Some(out_dir) = &package_build_data.out_dir {
228                     // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
229                     if let Some(out_dir) = out_dir.as_os_str().to_str().map(|s| s.to_owned()) {
230                         package_build_data.envs.push(("OUT_DIR".to_string(), out_dir));
231                     }
232                 }
233             }
234         }
235
236         let mut errors = errors.into_inner();
237         if !output.status.success() {
238             if errors.is_empty() {
239                 errors = "cargo check failed".to_string();
240             }
241             res.error = Some(errors);
242         }
243
244         Ok(res)
245     }
246
247     pub fn error(&self) -> Option<&str> {
248         self.error.as_deref()
249     }
250
251     pub(crate) fn get_output(&self, idx: Package) -> Option<&BuildScriptOutput> {
252         self.outputs.get(idx)?.as_ref()
253     }
254 }
255
256 // FIXME: File a better way to know if it is a dylib.
257 fn is_dylib(path: &Utf8Path) -> bool {
258     match path.extension().map(|e| e.to_string().to_lowercase()) {
259         None => false,
260         Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
261     }
262 }