aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-04-06 14:22:26 +0100
committerAleksey Kladov <[email protected]>2021-04-06 14:22:26 +0100
commit9ec5e6e4fdbe893f38d10dbdc609284368efdb64 (patch)
tree959faf13c99465ae23ff0bb1fd67b0a3b246c5bd /crates
parentad02bfe58fd52293d9ae4a049f98f475df9d3abb (diff)
Clearer naming
Diffstat (limited to 'crates')
-rw-r--r--crates/paths/src/lib.rs7
-rw-r--r--crates/project_model/src/build_data.rs59
-rw-r--r--crates/project_model/src/workspace.rs10
3 files changed, 47 insertions, 29 deletions
diff --git a/crates/paths/src/lib.rs b/crates/paths/src/lib.rs
index 22011cb33..f09ad37e3 100644
--- a/crates/paths/src/lib.rs
+++ b/crates/paths/src/lib.rs
@@ -1,6 +1,7 @@
1//! Thin wrappers around `std::path`, distinguishing between absolute and 1//! Thin wrappers around `std::path`, distinguishing between absolute and
2//! relative paths. 2//! relative paths.
3use std::{ 3use std::{
4 borrow::Borrow,
4 convert::{TryFrom, TryInto}, 5 convert::{TryFrom, TryInto},
5 ops, 6 ops,
6 path::{Component, Path, PathBuf}, 7 path::{Component, Path, PathBuf},
@@ -35,6 +36,12 @@ impl AsRef<AbsPath> for AbsPathBuf {
35 } 36 }
36} 37}
37 38
39impl Borrow<AbsPath> for AbsPathBuf {
40 fn borrow(&self) -> &AbsPath {
41 self.as_path()
42 }
43}
44
38impl TryFrom<PathBuf> for AbsPathBuf { 45impl TryFrom<PathBuf> for AbsPathBuf {
39 type Error = PathBuf; 46 type Error = PathBuf;
40 fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> { 47 fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> {
diff --git a/crates/project_model/src/build_data.rs b/crates/project_model/src/build_data.rs
index c2c87b207..fdf4b2d55 100644
--- a/crates/project_model/src/build_data.rs
+++ b/crates/project_model/src/build_data.rs
@@ -18,7 +18,7 @@ use stdx::JodChild;
18use crate::{cfg_flag::CfgFlag, CargoConfig}; 18use crate::{cfg_flag::CfgFlag, CargoConfig};
19 19
20#[derive(Debug, Clone, Default, PartialEq, Eq)] 20#[derive(Debug, Clone, Default, PartialEq, Eq)]
21pub(crate) struct BuildData { 21pub(crate) struct PackageBuildData {
22 /// List of config flags defined by this package's build script 22 /// List of config flags defined by this package's build script
23 pub(crate) cfgs: Vec<CfgFlag>, 23 pub(crate) cfgs: Vec<CfgFlag>,
24 /// List of cargo-related environment variables with their value 24 /// List of cargo-related environment variables with their value
@@ -32,6 +32,16 @@ pub(crate) struct BuildData {
32 pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>, 32 pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
33} 33}
34 34
35#[derive(Debug, Default, PartialEq, Eq, Clone)]
36pub(crate) struct WorkspaceBuildData {
37 per_package: FxHashMap<String, PackageBuildData>,
38}
39
40#[derive(Debug, Default, PartialEq, Eq, Clone)]
41pub struct BuildDataResult {
42 per_workspace: FxHashMap<AbsPathBuf, WorkspaceBuildData>,
43}
44
35#[derive(Clone, Debug)] 45#[derive(Clone, Debug)]
36pub(crate) struct BuildDataConfig { 46pub(crate) struct BuildDataConfig {
37 cargo_toml: AbsPathBuf, 47 cargo_toml: AbsPathBuf,
@@ -52,13 +62,6 @@ pub struct BuildDataCollector {
52 configs: FxHashMap<AbsPathBuf, BuildDataConfig>, 62 configs: FxHashMap<AbsPathBuf, BuildDataConfig>,
53} 63}
54 64
55#[derive(Debug, Default, PartialEq, Eq, Clone)]
56pub struct BuildDataResult {
57 data: FxHashMap<AbsPathBuf, BuildDataMap>,
58}
59
60pub(crate) type BuildDataMap = FxHashMap<String, BuildData>;
61
62impl BuildDataCollector { 65impl BuildDataCollector {
63 pub(crate) fn add_config(&mut self, workspace_root: &AbsPath, config: BuildDataConfig) { 66 pub(crate) fn add_config(&mut self, workspace_root: &AbsPath, config: BuildDataConfig) {
64 self.configs.insert(workspace_root.to_path_buf(), config); 67 self.configs.insert(workspace_root.to_path_buf(), config);
@@ -67,7 +70,7 @@ impl BuildDataCollector {
67 pub fn collect(&mut self, progress: &dyn Fn(String)) -> Result<BuildDataResult> { 70 pub fn collect(&mut self, progress: &dyn Fn(String)) -> Result<BuildDataResult> {
68 let mut res = BuildDataResult::default(); 71 let mut res = BuildDataResult::default();
69 for (path, config) in self.configs.iter() { 72 for (path, config) in self.configs.iter() {
70 res.data.insert( 73 res.per_workspace.insert(
71 path.clone(), 74 path.clone(),
72 collect_from_workspace( 75 collect_from_workspace(
73 &config.cargo_toml, 76 &config.cargo_toml,
@@ -81,9 +84,15 @@ impl BuildDataCollector {
81 } 84 }
82} 85}
83 86
87impl WorkspaceBuildData {
88 pub(crate) fn get(&self, package_id: &str) -> Option<&PackageBuildData> {
89 self.per_package.get(package_id)
90 }
91}
92
84impl BuildDataResult { 93impl BuildDataResult {
85 pub(crate) fn get(&self, root: &AbsPath) -> Option<&BuildDataMap> { 94 pub(crate) fn get(&self, workspace_root: &AbsPath) -> Option<&WorkspaceBuildData> {
86 self.data.get(&root.to_path_buf()) 95 self.per_workspace.get(workspace_root)
87 } 96 }
88} 97}
89 98
@@ -102,7 +111,7 @@ fn collect_from_workspace(
102 cargo_features: &CargoConfig, 111 cargo_features: &CargoConfig,
103 packages: &Vec<cargo_metadata::Package>, 112 packages: &Vec<cargo_metadata::Package>,
104 progress: &dyn Fn(String), 113 progress: &dyn Fn(String),
105) -> Result<BuildDataMap> { 114) -> Result<WorkspaceBuildData> {
106 let mut cmd = Command::new(toolchain::cargo()); 115 let mut cmd = Command::new(toolchain::cargo());
107 cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"]) 116 cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"])
108 .arg(cargo_toml.as_ref()); 117 .arg(cargo_toml.as_ref());
@@ -136,7 +145,7 @@ fn collect_from_workspace(
136 let child_stdout = child.stdout.take().unwrap(); 145 let child_stdout = child.stdout.take().unwrap();
137 let stdout = BufReader::new(child_stdout); 146 let stdout = BufReader::new(child_stdout);
138 147
139 let mut res = BuildDataMap::default(); 148 let mut res = WorkspaceBuildData::default();
140 for message in cargo_metadata::Message::parse_stream(stdout).flatten() { 149 for message in cargo_metadata::Message::parse_stream(stdout).flatten() {
141 match message { 150 match message {
142 Message::BuildScriptExecuted(BuildScript { 151 Message::BuildScriptExecuted(BuildScript {
@@ -154,16 +163,17 @@ fn collect_from_workspace(
154 } 163 }
155 acc 164 acc
156 }; 165 };
157 let res = res.entry(package_id.repr.clone()).or_default(); 166 let package_build_data =
167 res.per_package.entry(package_id.repr.clone()).or_default();
158 // cargo_metadata crate returns default (empty) path for 168 // cargo_metadata crate returns default (empty) path for
159 // older cargos, which is not absolute, so work around that. 169 // older cargos, which is not absolute, so work around that.
160 if !out_dir.as_str().is_empty() { 170 if !out_dir.as_str().is_empty() {
161 let out_dir = AbsPathBuf::assert(PathBuf::from(out_dir.into_os_string())); 171 let out_dir = AbsPathBuf::assert(PathBuf::from(out_dir.into_os_string()));
162 res.out_dir = Some(out_dir); 172 package_build_data.out_dir = Some(out_dir);
163 res.cfgs = cfgs; 173 package_build_data.cfgs = cfgs;
164 } 174 }
165 175
166 res.envs = env; 176 package_build_data.envs = env;
167 } 177 }
168 Message::CompilerArtifact(message) => { 178 Message::CompilerArtifact(message) => {
169 progress(format!("metadata {}", message.target.name)); 179 progress(format!("metadata {}", message.target.name));
@@ -173,8 +183,9 @@ fn collect_from_workspace(
173 // Skip rmeta file 183 // Skip rmeta file
174 if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name)) { 184 if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name)) {
175 let filename = AbsPathBuf::assert(PathBuf::from(&filename)); 185 let filename = AbsPathBuf::assert(PathBuf::from(&filename));
176 let res = res.entry(package_id.repr.clone()).or_default(); 186 let package_build_data =
177 res.proc_macro_dylib_path = Some(filename); 187 res.per_package.entry(package_id.repr.clone()).or_default();
188 package_build_data.proc_macro_dylib_path = Some(filename);
178 } 189 }
179 } 190 }
180 } 191 }
@@ -188,12 +199,12 @@ fn collect_from_workspace(
188 } 199 }
189 200
190 for package in packages { 201 for package in packages {
191 let build_data = res.entry(package.id.repr.clone()).or_default(); 202 let package_build_data = res.per_package.entry(package.id.repr.clone()).or_default();
192 inject_cargo_env(package, build_data); 203 inject_cargo_env(package, package_build_data);
193 if let Some(out_dir) = &build_data.out_dir { 204 if let Some(out_dir) = &package_build_data.out_dir {
194 // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!() 205 // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
195 if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) { 206 if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
196 build_data.envs.push(("OUT_DIR".to_string(), out_dir)); 207 package_build_data.envs.push(("OUT_DIR".to_string(), out_dir));
197 } 208 }
198 } 209 }
199 } 210 }
@@ -212,7 +223,7 @@ fn is_dylib(path: &Utf8Path) -> bool {
212/// Recreates the compile-time environment variables that Cargo sets. 223/// Recreates the compile-time environment variables that Cargo sets.
213/// 224///
214/// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates> 225/// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
215fn inject_cargo_env(package: &cargo_metadata::Package, build_data: &mut BuildData) { 226fn inject_cargo_env(package: &cargo_metadata::Package, build_data: &mut PackageBuildData) {
216 let env = &mut build_data.envs; 227 let env = &mut build_data.envs;
217 228
218 // FIXME: Missing variables: 229 // FIXME: Missing variables:
diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs
index 1b53fcc30..2fcd0f8fa 100644
--- a/crates/project_model/src/workspace.rs
+++ b/crates/project_model/src/workspace.rs
@@ -12,7 +12,7 @@ use proc_macro_api::ProcMacroClient;
12use rustc_hash::{FxHashMap, FxHashSet}; 12use rustc_hash::{FxHashMap, FxHashSet};
13 13
14use crate::{ 14use crate::{
15 build_data::{BuildData, BuildDataMap, BuildDataResult}, 15 build_data::{BuildDataResult, PackageBuildData, WorkspaceBuildData},
16 cargo_workspace, 16 cargo_workspace,
17 cfg_flag::CfgFlag, 17 cfg_flag::CfgFlag,
18 rustc_cfg, 18 rustc_cfg,
@@ -354,10 +354,10 @@ fn cargo_to_crate_graph(
354 proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>, 354 proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
355 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, 355 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
356 cargo: &CargoWorkspace, 356 cargo: &CargoWorkspace,
357 build_data_map: Option<&BuildDataMap>, 357 build_data_map: Option<&WorkspaceBuildData>,
358 sysroot: &Sysroot, 358 sysroot: &Sysroot,
359 rustc: &Option<CargoWorkspace>, 359 rustc: &Option<CargoWorkspace>,
360 rustc_build_data_map: Option<&BuildDataMap>, 360 rustc_build_data_map: Option<&WorkspaceBuildData>,
361) -> CrateGraph { 361) -> CrateGraph {
362 let _p = profile::span("cargo_to_crate_graph"); 362 let _p = profile::span("cargo_to_crate_graph");
363 let mut crate_graph = CrateGraph::default(); 363 let mut crate_graph = CrateGraph::default();
@@ -464,7 +464,7 @@ fn handle_rustc_crates(
464 rustc_workspace: &CargoWorkspace, 464 rustc_workspace: &CargoWorkspace,
465 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, 465 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
466 crate_graph: &mut CrateGraph, 466 crate_graph: &mut CrateGraph,
467 rustc_build_data_map: Option<&FxHashMap<String, BuildData>>, 467 rustc_build_data_map: Option<&WorkspaceBuildData>,
468 cfg_options: &CfgOptions, 468 cfg_options: &CfgOptions,
469 proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>, 469 proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
470 pkg_to_lib_crate: &mut FxHashMap<la_arena::Idx<crate::PackageData>, CrateId>, 470 pkg_to_lib_crate: &mut FxHashMap<la_arena::Idx<crate::PackageData>, CrateId>,
@@ -555,7 +555,7 @@ fn handle_rustc_crates(
555fn add_target_crate_root( 555fn add_target_crate_root(
556 crate_graph: &mut CrateGraph, 556 crate_graph: &mut CrateGraph,
557 pkg: &cargo_workspace::PackageData, 557 pkg: &cargo_workspace::PackageData,
558 build_data: Option<&BuildData>, 558 build_data: Option<&PackageBuildData>,
559 cfg_options: &CfgOptions, 559 cfg_options: &CfgOptions,
560 proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>, 560 proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
561 file_id: FileId, 561 file_id: FileId,