aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model
diff options
context:
space:
mode:
Diffstat (limited to 'crates/project_model')
-rw-r--r--crates/project_model/src/build_data.rs206
-rw-r--r--crates/project_model/src/cargo_workspace.rs197
-rw-r--r--crates/project_model/src/lib.rs1
-rw-r--r--crates/project_model/src/workspace.rs20
4 files changed, 228 insertions, 196 deletions
diff --git a/crates/project_model/src/build_data.rs b/crates/project_model/src/build_data.rs
new file mode 100644
index 000000000..82fcf23ad
--- /dev/null
+++ b/crates/project_model/src/build_data.rs
@@ -0,0 +1,206 @@
1//! Handles build script specific information
2
3use std::{
4 ffi::OsStr,
5 io::BufReader,
6 path::{Path, PathBuf},
7 process::{Command, Stdio},
8};
9
10use anyhow::Result;
11use cargo_metadata::{BuildScript, Message, Package, PackageId};
12use itertools::Itertools;
13use paths::AbsPathBuf;
14use rustc_hash::FxHashMap;
15use stdx::JodChild;
16
17use crate::{cfg_flag::CfgFlag, CargoConfig};
18
19#[derive(Debug, Clone, Default)]
20pub(crate) struct BuildDataMap {
21 data: FxHashMap<PackageId, BuildData>,
22}
23#[derive(Debug, Clone, Default, PartialEq, Eq)]
24pub struct BuildData {
25 /// List of config flags defined by this package's build script
26 pub cfgs: Vec<CfgFlag>,
27 /// List of cargo-related environment variables with their value
28 ///
29 /// If the package has a build script which defines environment variables,
30 /// they can also be found here.
31 pub envs: Vec<(String, String)>,
32 /// Directory where a build script might place its output
33 pub out_dir: Option<AbsPathBuf>,
34 /// Path to the proc-macro library file if this package exposes proc-macros
35 pub proc_macro_dylib_path: Option<AbsPathBuf>,
36}
37
38impl BuildDataMap {
39 pub(crate) fn new(
40 cargo_toml: &Path,
41 cargo_features: &CargoConfig,
42 packages: &Vec<Package>,
43 progress: &dyn Fn(String),
44 ) -> Result<BuildDataMap> {
45 let mut cmd = Command::new(toolchain::cargo());
46 cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"])
47 .arg(cargo_toml);
48
49 // --all-targets includes tests, benches and examples in addition to the
50 // default lib and bins. This is an independent concept from the --targets
51 // flag below.
52 cmd.arg("--all-targets");
53
54 if let Some(target) = &cargo_features.target {
55 cmd.args(&["--target", target]);
56 }
57
58 if cargo_features.all_features {
59 cmd.arg("--all-features");
60 } else {
61 if cargo_features.no_default_features {
62 // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
63 // https://github.com/oli-obk/cargo_metadata/issues/79
64 cmd.arg("--no-default-features");
65 }
66 if !cargo_features.features.is_empty() {
67 cmd.arg("--features");
68 cmd.arg(cargo_features.features.join(" "));
69 }
70 }
71
72 cmd.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null());
73
74 let mut child = cmd.spawn().map(JodChild)?;
75 let child_stdout = child.stdout.take().unwrap();
76 let stdout = BufReader::new(child_stdout);
77
78 let mut res = BuildDataMap::default();
79 for message in cargo_metadata::Message::parse_stream(stdout) {
80 if let Ok(message) = message {
81 match message {
82 Message::BuildScriptExecuted(BuildScript {
83 package_id,
84 out_dir,
85 cfgs,
86 env,
87 ..
88 }) => {
89 let cfgs = {
90 let mut acc = Vec::new();
91 for cfg in cfgs {
92 match cfg.parse::<CfgFlag>() {
93 Ok(it) => acc.push(it),
94 Err(err) => {
95 anyhow::bail!("invalid cfg from cargo-metadata: {}", err)
96 }
97 };
98 }
99 acc
100 };
101 let res = res.data.entry(package_id.clone()).or_default();
102 // cargo_metadata crate returns default (empty) path for
103 // older cargos, which is not absolute, so work around that.
104 if out_dir != PathBuf::default() {
105 let out_dir = AbsPathBuf::assert(out_dir);
106 res.out_dir = Some(out_dir);
107 res.cfgs = cfgs;
108 }
109
110 res.envs = env;
111 }
112 Message::CompilerArtifact(message) => {
113 progress(format!("metadata {}", message.target.name));
114
115 if message.target.kind.contains(&"proc-macro".to_string()) {
116 let package_id = message.package_id;
117 // Skip rmeta file
118 if let Some(filename) =
119 message.filenames.iter().find(|name| is_dylib(name))
120 {
121 let filename = AbsPathBuf::assert(filename.clone());
122 let res = res.data.entry(package_id.clone()).or_default();
123 res.proc_macro_dylib_path = Some(filename);
124 }
125 }
126 }
127 Message::CompilerMessage(message) => {
128 progress(message.target.name.clone());
129 }
130 Message::Unknown => (),
131 Message::BuildFinished(_) => {}
132 Message::TextLine(_) => {}
133 }
134 }
135 }
136 res.inject_cargo_env(packages);
137 Ok(res)
138 }
139
140 pub(crate) fn with_cargo_env(packages: &Vec<Package>) -> Self {
141 let mut res = Self::default();
142 res.inject_cargo_env(packages);
143 res
144 }
145
146 pub(crate) fn get(&self, id: &PackageId) -> Option<&BuildData> {
147 self.data.get(id)
148 }
149
150 fn inject_cargo_env(&mut self, packages: &Vec<Package>) {
151 for meta_pkg in packages {
152 let resource = self.data.entry(meta_pkg.id.clone()).or_default();
153 inject_cargo_env(meta_pkg, &mut resource.envs);
154
155 if let Some(out_dir) = &resource.out_dir {
156 // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
157 if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
158 resource.envs.push(("OUT_DIR".to_string(), out_dir));
159 }
160 }
161 }
162 }
163}
164
165// FIXME: File a better way to know if it is a dylib
166fn is_dylib(path: &Path) -> bool {
167 match path.extension().and_then(OsStr::to_str).map(|it| it.to_string().to_lowercase()) {
168 None => false,
169 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
170 }
171}
172
173/// Recreates the compile-time environment variables that Cargo sets.
174///
175/// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
176fn inject_cargo_env(package: &cargo_metadata::Package, env: &mut Vec<(String, String)>) {
177 // FIXME: Missing variables:
178 // CARGO, CARGO_PKG_HOMEPAGE, CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
179
180 let mut manifest_dir = package.manifest_path.clone();
181 manifest_dir.pop();
182 if let Some(cargo_manifest_dir) = manifest_dir.to_str() {
183 env.push(("CARGO_MANIFEST_DIR".into(), cargo_manifest_dir.into()));
184 }
185
186 env.push(("CARGO_PKG_VERSION".into(), package.version.to_string()));
187 env.push(("CARGO_PKG_VERSION_MAJOR".into(), package.version.major.to_string()));
188 env.push(("CARGO_PKG_VERSION_MINOR".into(), package.version.minor.to_string()));
189 env.push(("CARGO_PKG_VERSION_PATCH".into(), package.version.patch.to_string()));
190
191 let pre = package.version.pre.iter().map(|id| id.to_string()).format(".");
192 env.push(("CARGO_PKG_VERSION_PRE".into(), pre.to_string()));
193
194 let authors = package.authors.join(";");
195 env.push(("CARGO_PKG_AUTHORS".into(), authors));
196
197 env.push(("CARGO_PKG_NAME".into(), package.name.clone()));
198 env.push(("CARGO_PKG_DESCRIPTION".into(), package.description.clone().unwrap_or_default()));
199 //env.push(("CARGO_PKG_HOMEPAGE".into(), package.homepage.clone().unwrap_or_default()));
200 env.push(("CARGO_PKG_REPOSITORY".into(), package.repository.clone().unwrap_or_default()));
201 env.push(("CARGO_PKG_LICENSE".into(), package.license.clone().unwrap_or_default()));
202
203 let license_file =
204 package.license_file.as_ref().map(|buf| buf.display().to_string()).unwrap_or_default();
205 env.push(("CARGO_PKG_LICENSE_FILE".into(), license_file));
206}
diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs
index a09e1a9ff..c8a5333c4 100644
--- a/crates/project_model/src/cargo_workspace.rs
+++ b/crates/project_model/src/cargo_workspace.rs
@@ -1,24 +1,15 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use std::{ 3use std::{convert::TryInto, ops, process::Command};
4 convert::TryInto,
5 ffi::OsStr,
6 io::BufReader,
7 ops,
8 path::{Path, PathBuf},
9 process::{Command, Stdio},
10};
11 4
12use anyhow::{Context, Result}; 5use anyhow::{Context, Result};
13use base_db::Edition; 6use base_db::Edition;
14use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; 7use cargo_metadata::{CargoOpt, MetadataCommand};
15use itertools::Itertools;
16use la_arena::{Arena, Idx}; 8use la_arena::{Arena, Idx};
17use paths::{AbsPath, AbsPathBuf}; 9use paths::{AbsPath, AbsPathBuf};
18use rustc_hash::FxHashMap; 10use rustc_hash::FxHashMap;
19use stdx::JodChild;
20 11
21use crate::cfg_flag::CfgFlag; 12use crate::build_data::{BuildData, BuildDataMap};
22use crate::utf8_stdout; 13use crate::utf8_stdout;
23 14
24/// `CargoWorkspace` represents the logical structure of, well, a Cargo 15/// `CargoWorkspace` represents the logical structure of, well, a Cargo
@@ -103,17 +94,8 @@ pub struct PackageData {
103 pub features: FxHashMap<String, Vec<String>>, 94 pub features: FxHashMap<String, Vec<String>>,
104 /// List of features enabled on this package 95 /// List of features enabled on this package
105 pub active_features: Vec<String>, 96 pub active_features: Vec<String>,
106 /// List of config flags defined by this package's build script 97 /// Build script related data for this package
107 pub cfgs: Vec<CfgFlag>, 98 pub build_data: BuildData,
108 /// List of cargo-related environment variables with their value
109 ///
110 /// If the package has a build script which defines environment variables,
111 /// they can also be found here.
112 pub envs: Vec<(String, String)>,
113 /// Directory where a build script might place its output
114 pub out_dir: Option<AbsPathBuf>,
115 /// Path to the proc-macro library file if this package exposes proc-macros
116 pub proc_macro_dylib_path: Option<AbsPathBuf>,
117} 99}
118 100
119#[derive(Debug, Clone, Eq, PartialEq)] 101#[derive(Debug, Clone, Eq, PartialEq)]
@@ -246,17 +228,11 @@ impl CargoWorkspace {
246 ) 228 )
247 })?; 229 })?;
248 230
249 let mut out_dir_by_id = FxHashMap::default(); 231 let resources = if config.load_out_dirs_from_check {
250 let mut cfgs = FxHashMap::default(); 232 BuildDataMap::new(cargo_toml, config, &meta.packages, progress)?
251 let mut envs = FxHashMap::default(); 233 } else {
252 let mut proc_macro_dylib_paths = FxHashMap::default(); 234 BuildDataMap::with_cargo_env(&meta.packages)
253 if config.load_out_dirs_from_check { 235 };
254 let resources = load_extern_resources(cargo_toml, config, progress)?;
255 out_dir_by_id = resources.out_dirs;
256 cfgs = resources.cfgs;
257 envs = resources.env;
258 proc_macro_dylib_paths = resources.proc_dylib_paths;
259 }
260 236
261 let mut pkg_by_id = FxHashMap::default(); 237 let mut pkg_by_id = FxHashMap::default();
262 let mut packages = Arena::default(); 238 let mut packages = Arena::default();
@@ -267,7 +243,7 @@ impl CargoWorkspace {
267 meta.packages.sort_by(|a, b| a.id.cmp(&b.id)); 243 meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
268 for meta_pkg in meta.packages { 244 for meta_pkg in meta.packages {
269 let id = meta_pkg.id.clone(); 245 let id = meta_pkg.id.clone();
270 inject_cargo_env(&meta_pkg, envs.entry(id).or_default()); 246 let build_data = resources.get(&id).cloned().unwrap_or_default();
271 247
272 let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } = 248 let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } =
273 meta_pkg; 249 meta_pkg;
@@ -285,10 +261,7 @@ impl CargoWorkspace {
285 dependencies: Vec::new(), 261 dependencies: Vec::new(),
286 features: meta_pkg.features.into_iter().collect(), 262 features: meta_pkg.features.into_iter().collect(),
287 active_features: Vec::new(), 263 active_features: Vec::new(),
288 cfgs: cfgs.get(&id).cloned().unwrap_or_default(), 264 build_data,
289 envs: envs.get(&id).cloned().unwrap_or_default(),
290 out_dir: out_dir_by_id.get(&id).cloned(),
291 proc_macro_dylib_path: proc_macro_dylib_paths.get(&id).cloned(),
292 }); 265 });
293 let pkg_data = &mut packages[pkg]; 266 let pkg_data = &mut packages[pkg];
294 pkg_by_id.insert(id, pkg); 267 pkg_by_id.insert(id, pkg);
@@ -365,149 +338,3 @@ impl CargoWorkspace {
365 self.packages.iter().filter(|(_, v)| v.name == name).count() == 1 338 self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
366 } 339 }
367} 340}
368
369#[derive(Debug, Clone, Default)]
370pub(crate) struct ExternResources {
371 out_dirs: FxHashMap<PackageId, AbsPathBuf>,
372 proc_dylib_paths: FxHashMap<PackageId, AbsPathBuf>,
373 cfgs: FxHashMap<PackageId, Vec<CfgFlag>>,
374 env: FxHashMap<PackageId, Vec<(String, String)>>,
375}
376
377pub(crate) fn load_extern_resources(
378 cargo_toml: &Path,
379 cargo_features: &CargoConfig,
380 progress: &dyn Fn(String),
381) -> Result<ExternResources> {
382 let mut cmd = Command::new(toolchain::cargo());
383 cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"]).arg(cargo_toml);
384
385 // --all-targets includes tests, benches and examples in addition to the
386 // default lib and bins. This is an independent concept from the --targets
387 // flag below.
388 cmd.arg("--all-targets");
389
390 if let Some(target) = &cargo_features.target {
391 cmd.args(&["--target", target]);
392 }
393
394 if cargo_features.all_features {
395 cmd.arg("--all-features");
396 } else {
397 if cargo_features.no_default_features {
398 // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
399 // https://github.com/oli-obk/cargo_metadata/issues/79
400 cmd.arg("--no-default-features");
401 }
402 if !cargo_features.features.is_empty() {
403 cmd.arg("--features");
404 cmd.arg(cargo_features.features.join(" "));
405 }
406 }
407
408 cmd.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null());
409
410 let mut child = cmd.spawn().map(JodChild)?;
411 let child_stdout = child.stdout.take().unwrap();
412 let stdout = BufReader::new(child_stdout);
413
414 let mut res = ExternResources::default();
415 for message in cargo_metadata::Message::parse_stream(stdout) {
416 if let Ok(message) = message {
417 match message {
418 Message::BuildScriptExecuted(BuildScript {
419 package_id,
420 out_dir,
421 cfgs,
422 env,
423 ..
424 }) => {
425 let cfgs = {
426 let mut acc = Vec::new();
427 for cfg in cfgs {
428 match cfg.parse::<CfgFlag>() {
429 Ok(it) => acc.push(it),
430 Err(err) => {
431 anyhow::bail!("invalid cfg from cargo-metadata: {}", err)
432 }
433 };
434 }
435 acc
436 };
437 // cargo_metadata crate returns default (empty) path for
438 // older cargos, which is not absolute, so work around that.
439 if out_dir != PathBuf::default() {
440 let out_dir = AbsPathBuf::assert(out_dir);
441 res.out_dirs.insert(package_id.clone(), out_dir);
442 res.cfgs.insert(package_id.clone(), cfgs);
443 }
444
445 res.env.insert(package_id, env);
446 }
447 Message::CompilerArtifact(message) => {
448 progress(format!("metadata {}", message.target.name));
449
450 if message.target.kind.contains(&"proc-macro".to_string()) {
451 let package_id = message.package_id;
452 // Skip rmeta file
453 if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name))
454 {
455 let filename = AbsPathBuf::assert(filename.clone());
456 res.proc_dylib_paths.insert(package_id, filename);
457 }
458 }
459 }
460 Message::CompilerMessage(message) => {
461 progress(message.target.name.clone());
462 }
463 Message::Unknown => (),
464 Message::BuildFinished(_) => {}
465 Message::TextLine(_) => {}
466 }
467 }
468 }
469 Ok(res)
470}
471
472// FIXME: File a better way to know if it is a dylib
473fn is_dylib(path: &Path) -> bool {
474 match path.extension().and_then(OsStr::to_str).map(|it| it.to_string().to_lowercase()) {
475 None => false,
476 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
477 }
478}
479
480/// Recreates the compile-time environment variables that Cargo sets.
481///
482/// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
483fn inject_cargo_env(package: &cargo_metadata::Package, env: &mut Vec<(String, String)>) {
484 // FIXME: Missing variables:
485 // CARGO, CARGO_PKG_HOMEPAGE, CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
486
487 let mut manifest_dir = package.manifest_path.clone();
488 manifest_dir.pop();
489 if let Some(cargo_manifest_dir) = manifest_dir.to_str() {
490 env.push(("CARGO_MANIFEST_DIR".into(), cargo_manifest_dir.into()));
491 }
492
493 env.push(("CARGO_PKG_VERSION".into(), package.version.to_string()));
494 env.push(("CARGO_PKG_VERSION_MAJOR".into(), package.version.major.to_string()));
495 env.push(("CARGO_PKG_VERSION_MINOR".into(), package.version.minor.to_string()));
496 env.push(("CARGO_PKG_VERSION_PATCH".into(), package.version.patch.to_string()));
497
498 let pre = package.version.pre.iter().map(|id| id.to_string()).format(".");
499 env.push(("CARGO_PKG_VERSION_PRE".into(), pre.to_string()));
500
501 let authors = package.authors.join(";");
502 env.push(("CARGO_PKG_AUTHORS".into(), authors));
503
504 env.push(("CARGO_PKG_NAME".into(), package.name.clone()));
505 env.push(("CARGO_PKG_DESCRIPTION".into(), package.description.clone().unwrap_or_default()));
506 //env.push(("CARGO_PKG_HOMEPAGE".into(), package.homepage.clone().unwrap_or_default()));
507 env.push(("CARGO_PKG_REPOSITORY".into(), package.repository.clone().unwrap_or_default()));
508 env.push(("CARGO_PKG_LICENSE".into(), package.license.clone().unwrap_or_default()));
509
510 let license_file =
511 package.license_file.as_ref().map(|buf| buf.display().to_string()).unwrap_or_default();
512 env.push(("CARGO_PKG_LICENSE_FILE".into(), license_file));
513}
diff --git a/crates/project_model/src/lib.rs b/crates/project_model/src/lib.rs
index 970a7e140..525c336e6 100644
--- a/crates/project_model/src/lib.rs
+++ b/crates/project_model/src/lib.rs
@@ -6,6 +6,7 @@ mod project_json;
6mod sysroot; 6mod sysroot;
7mod workspace; 7mod workspace;
8mod rustc_cfg; 8mod rustc_cfg;
9mod build_data;
9 10
10use std::{ 11use std::{
11 fs::{read_dir, ReadDir}, 12 fs::{read_dir, ReadDir},
diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs
index 073c48af7..bc5041e5a 100644
--- a/crates/project_model/src/workspace.rs
+++ b/crates/project_model/src/workspace.rs
@@ -178,7 +178,7 @@ impl ProjectWorkspace {
178 let pkg_root = cargo[pkg].root().to_path_buf(); 178 let pkg_root = cargo[pkg].root().to_path_buf();
179 179
180 let mut include = vec![pkg_root.clone()]; 180 let mut include = vec![pkg_root.clone()];
181 include.extend(cargo[pkg].out_dir.clone()); 181 include.extend(cargo[pkg].build_data.out_dir.clone());
182 182
183 let mut exclude = vec![pkg_root.join(".git")]; 183 let mut exclude = vec![pkg_root.join(".git")];
184 if is_member { 184 if is_member {
@@ -484,23 +484,21 @@ fn add_target_crate_root(
484 for feature in pkg.active_features.iter() { 484 for feature in pkg.active_features.iter() {
485 opts.insert_key_value("feature".into(), feature.into()); 485 opts.insert_key_value("feature".into(), feature.into());
486 } 486 }
487 opts.extend(pkg.cfgs.iter().cloned()); 487 opts.extend(pkg.build_data.cfgs.iter().cloned());
488 opts 488 opts
489 }; 489 };
490 490
491 let mut env = Env::default(); 491 let mut env = Env::default();
492 for (k, v) in &pkg.envs { 492 for (k, v) in &pkg.build_data.envs {
493 env.set(k, v.clone()); 493 env.set(k, v.clone());
494 } 494 }
495 if let Some(out_dir) = &pkg.out_dir {
496 // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
497 if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
498 env.set("OUT_DIR", out_dir);
499 }
500 }
501 495
502 let proc_macro = 496 let proc_macro = pkg
503 pkg.proc_macro_dylib_path.as_ref().map(|it| proc_macro_loader(&it)).unwrap_or_default(); 497 .build_data
498 .proc_macro_dylib_path
499 .as_ref()
500 .map(|it| proc_macro_loader(&it))
501 .unwrap_or_default();
504 502
505 let display_name = CrateDisplayName::from_canonical_name(pkg.name.clone()); 503 let display_name = CrateDisplayName::from_canonical_name(pkg.name.clone());
506 let crate_id = crate_graph.add_crate_root( 504 let crate_id = crate_graph.add_crate_root(