aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model
diff options
context:
space:
mode:
authorGalilée 'Bill' Enguehard <[email protected]>2020-05-21 22:27:38 +0100
committerGalilée 'Bill' Enguehard <[email protected]>2020-05-21 22:27:38 +0100
commit7fece3bdd2450c0807f7dd742239cae95f0cc65e (patch)
tree866c4db826c959e79c63a6727bdb9f2c61e6fc4f /crates/ra_project_model
parentdb926218b2082077750291f8426ddd28b284cd08 (diff)
parent59732df8d40dfadc6dcf5951265416576399712a (diff)
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into modname_spacing
Diffstat (limited to 'crates/ra_project_model')
-rw-r--r--crates/ra_project_model/Cargo.toml5
-rw-r--r--crates/ra_project_model/src/cargo_workspace.rs35
-rw-r--r--crates/ra_project_model/src/lib.rs108
-rw-r--r--crates/ra_project_model/src/sysroot.rs41
4 files changed, 87 insertions, 102 deletions
diff --git a/crates/ra_project_model/Cargo.toml b/crates/ra_project_model/Cargo.toml
index 5e651fe70..e4a60f4c0 100644
--- a/crates/ra_project_model/Cargo.toml
+++ b/crates/ra_project_model/Cargo.toml
@@ -11,11 +11,12 @@ doctest = false
11log = "0.4.8" 11log = "0.4.8"
12rustc-hash = "1.1.0" 12rustc-hash = "1.1.0"
13 13
14cargo_metadata = "0.9.1" 14cargo_metadata = "0.10.0"
15 15
16ra_arena = { path = "../ra_arena" } 16ra_arena = { path = "../ra_arena" }
17ra_db = { path = "../ra_db" }
18ra_cfg = { path = "../ra_cfg" } 17ra_cfg = { path = "../ra_cfg" }
18ra_db = { path = "../ra_db" }
19ra_toolchain = { path = "../ra_toolchain" }
19ra_proc_macro = { path = "../ra_proc_macro" } 20ra_proc_macro = { path = "../ra_proc_macro" }
20 21
21serde = { version = "1.0.106", features = ["derive"] } 22serde = { version = "1.0.106", features = ["derive"] }
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs
index 362ee30fe..a306ce95f 100644
--- a/crates/ra_project_model/src/cargo_workspace.rs
+++ b/crates/ra_project_model/src/cargo_workspace.rs
@@ -1,7 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use std::{ 3use std::{
4 env,
5 ffi::OsStr, 4 ffi::OsStr,
6 ops, 5 ops,
7 path::{Path, PathBuf}, 6 path::{Path, PathBuf},
@@ -56,6 +55,9 @@ pub struct CargoConfig {
56 55
57 /// Runs cargo check on launch to figure out the correct values of OUT_DIR 56 /// Runs cargo check on launch to figure out the correct values of OUT_DIR
58 pub load_out_dirs_from_check: bool, 57 pub load_out_dirs_from_check: bool,
58
59 /// rustc target
60 pub target: Option<String>,
59} 61}
60 62
61impl Default for CargoConfig { 63impl Default for CargoConfig {
@@ -65,6 +67,7 @@ impl Default for CargoConfig {
65 all_features: true, 67 all_features: true,
66 features: Vec::new(), 68 features: Vec::new(),
67 load_out_dirs_from_check: false, 69 load_out_dirs_from_check: false,
70 target: None,
68 } 71 }
69 } 72 }
70} 73}
@@ -83,6 +86,7 @@ pub struct PackageData {
83 pub dependencies: Vec<PackageDependency>, 86 pub dependencies: Vec<PackageDependency>,
84 pub edition: Edition, 87 pub edition: Edition,
85 pub features: Vec<String>, 88 pub features: Vec<String>,
89 pub cfgs: Vec<String>,
86 pub out_dir: Option<PathBuf>, 90 pub out_dir: Option<PathBuf>,
87 pub proc_macro_dylib_path: Option<PathBuf>, 91 pub proc_macro_dylib_path: Option<PathBuf>,
88} 92}
@@ -141,12 +145,8 @@ impl CargoWorkspace {
141 cargo_toml: &Path, 145 cargo_toml: &Path,
142 cargo_features: &CargoConfig, 146 cargo_features: &CargoConfig,
143 ) -> Result<CargoWorkspace> { 147 ) -> Result<CargoWorkspace> {
144 let _ = Command::new(cargo_binary())
145 .arg("--version")
146 .output()
147 .context("failed to run `cargo --version`, is `cargo` in PATH?")?;
148
149 let mut meta = MetadataCommand::new(); 148 let mut meta = MetadataCommand::new();
149 meta.cargo_path(ra_toolchain::cargo());
150 meta.manifest_path(cargo_toml); 150 meta.manifest_path(cargo_toml);
151 if cargo_features.all_features { 151 if cargo_features.all_features {
152 meta.features(CargoOpt::AllFeatures); 152 meta.features(CargoOpt::AllFeatures);
@@ -160,15 +160,20 @@ impl CargoWorkspace {
160 if let Some(parent) = cargo_toml.parent() { 160 if let Some(parent) = cargo_toml.parent() {
161 meta.current_dir(parent); 161 meta.current_dir(parent);
162 } 162 }
163 if let Some(target) = cargo_features.target.as_ref() {
164 meta.other_options(vec![String::from("--filter-platform"), target.clone()]);
165 }
163 let meta = meta.exec().with_context(|| { 166 let meta = meta.exec().with_context(|| {
164 format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display()) 167 format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())
165 })?; 168 })?;
166 169
167 let mut out_dir_by_id = FxHashMap::default(); 170 let mut out_dir_by_id = FxHashMap::default();
171 let mut cfgs = FxHashMap::default();
168 let mut proc_macro_dylib_paths = FxHashMap::default(); 172 let mut proc_macro_dylib_paths = FxHashMap::default();
169 if cargo_features.load_out_dirs_from_check { 173 if cargo_features.load_out_dirs_from_check {
170 let resources = load_extern_resources(cargo_toml, cargo_features)?; 174 let resources = load_extern_resources(cargo_toml, cargo_features)?;
171 out_dir_by_id = resources.out_dirs; 175 out_dir_by_id = resources.out_dirs;
176 cfgs = resources.cfgs;
172 proc_macro_dylib_paths = resources.proc_dylib_paths; 177 proc_macro_dylib_paths = resources.proc_dylib_paths;
173 } 178 }
174 179
@@ -194,6 +199,7 @@ impl CargoWorkspace {
194 edition, 199 edition,
195 dependencies: Vec::new(), 200 dependencies: Vec::new(),
196 features: Vec::new(), 201 features: Vec::new(),
202 cfgs: cfgs.get(&id).cloned().unwrap_or_default(),
197 out_dir: out_dir_by_id.get(&id).cloned(), 203 out_dir: out_dir_by_id.get(&id).cloned(),
198 proc_macro_dylib_path: proc_macro_dylib_paths.get(&id).cloned(), 204 proc_macro_dylib_path: proc_macro_dylib_paths.get(&id).cloned(),
199 }); 205 });
@@ -275,13 +281,14 @@ impl CargoWorkspace {
275pub struct ExternResources { 281pub struct ExternResources {
276 out_dirs: FxHashMap<PackageId, PathBuf>, 282 out_dirs: FxHashMap<PackageId, PathBuf>,
277 proc_dylib_paths: FxHashMap<PackageId, PathBuf>, 283 proc_dylib_paths: FxHashMap<PackageId, PathBuf>,
284 cfgs: FxHashMap<PackageId, Vec<String>>,
278} 285}
279 286
280pub fn load_extern_resources( 287pub fn load_extern_resources(
281 cargo_toml: &Path, 288 cargo_toml: &Path,
282 cargo_features: &CargoConfig, 289 cargo_features: &CargoConfig,
283) -> Result<ExternResources> { 290) -> Result<ExternResources> {
284 let mut cmd = Command::new(cargo_binary()); 291 let mut cmd = Command::new(ra_toolchain::cargo());
285 cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); 292 cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml);
286 if cargo_features.all_features { 293 if cargo_features.all_features {
287 cmd.arg("--all-features"); 294 cmd.arg("--all-features");
@@ -297,13 +304,13 @@ pub fn load_extern_resources(
297 304
298 let mut res = ExternResources::default(); 305 let mut res = ExternResources::default();
299 306
300 for message in cargo_metadata::parse_messages(output.stdout.as_slice()) { 307 for message in cargo_metadata::Message::parse_stream(output.stdout.as_slice()) {
301 if let Ok(message) = message { 308 if let Ok(message) = message {
302 match message { 309 match message {
303 Message::BuildScriptExecuted(BuildScript { package_id, out_dir, .. }) => { 310 Message::BuildScriptExecuted(BuildScript { package_id, out_dir, cfgs, .. }) => {
304 res.out_dirs.insert(package_id, out_dir); 311 res.out_dirs.insert(package_id.clone(), out_dir);
312 res.cfgs.insert(package_id, cfgs);
305 } 313 }
306
307 Message::CompilerArtifact(message) => { 314 Message::CompilerArtifact(message) => {
308 if message.target.kind.contains(&"proc-macro".to_string()) { 315 if message.target.kind.contains(&"proc-macro".to_string()) {
309 let package_id = message.package_id; 316 let package_id = message.package_id;
@@ -316,6 +323,8 @@ pub fn load_extern_resources(
316 } 323 }
317 Message::CompilerMessage(_) => (), 324 Message::CompilerMessage(_) => (),
318 Message::Unknown => (), 325 Message::Unknown => (),
326 Message::BuildFinished(_) => {}
327 Message::TextLine(_) => {}
319 } 328 }
320 } 329 }
321 } 330 }
@@ -329,7 +338,3 @@ fn is_dylib(path: &Path) -> bool {
329 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"), 338 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
330 } 339 }
331} 340}
332
333fn cargo_binary() -> String {
334 env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())
335}
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index 731cbd291..a2e9f65ef 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -8,7 +8,7 @@ use std::{
8 fs::{read_dir, File, ReadDir}, 8 fs::{read_dir, File, ReadDir},
9 io::{self, BufReader}, 9 io::{self, BufReader},
10 path::{Path, PathBuf}, 10 path::{Path, PathBuf},
11 process::Command, 11 process::{Command, Output},
12}; 12};
13 13
14use anyhow::{bail, Context, Result}; 14use anyhow::{bail, Context, Result};
@@ -35,7 +35,7 @@ pub enum ProjectWorkspace {
35/// `PackageRoot` describes a package root folder. 35/// `PackageRoot` describes a package root folder.
36/// Which may be an external dependency, or a member of 36/// Which may be an external dependency, or a member of
37/// the current workspace. 37/// the current workspace.
38#[derive(Clone)] 38#[derive(Debug, Clone)]
39pub struct PackageRoot { 39pub struct PackageRoot {
40 /// Path to the root folder 40 /// Path to the root folder
41 path: PathBuf, 41 path: PathBuf,
@@ -88,46 +88,28 @@ impl ProjectRoot {
88 } 88 }
89 89
90 pub fn discover(path: &Path) -> io::Result<Vec<ProjectRoot>> { 90 pub fn discover(path: &Path) -> io::Result<Vec<ProjectRoot>> {
91 if let Some(project_json) = find_rust_project_json(path) { 91 if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") {
92 return Ok(vec![ProjectRoot::ProjectJson(project_json)]); 92 return Ok(vec![ProjectRoot::ProjectJson(project_json)]);
93 } 93 }
94 return find_cargo_toml(path) 94 return find_cargo_toml(path)
95 .map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect()); 95 .map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect());
96 96
97 fn find_rust_project_json(path: &Path) -> Option<PathBuf> {
98 if path.ends_with("rust-project.json") {
99 return Some(path.to_path_buf());
100 }
101
102 let mut curr = Some(path);
103 while let Some(path) = curr {
104 let candidate = path.join("rust-project.json");
105 if candidate.exists() {
106 return Some(candidate);
107 }
108 curr = path.parent();
109 }
110
111 None
112 }
113
114 fn find_cargo_toml(path: &Path) -> io::Result<Vec<PathBuf>> { 97 fn find_cargo_toml(path: &Path) -> io::Result<Vec<PathBuf>> {
115 if path.ends_with("Cargo.toml") { 98 match find_in_parent_dirs(path, "Cargo.toml") {
116 return Ok(vec![path.to_path_buf()]); 99 Some(it) => Ok(vec![it]),
100 None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)),
117 } 101 }
102 }
118 103
119 if let Some(p) = find_cargo_toml_in_parent_dir(path) { 104 fn find_in_parent_dirs(path: &Path, target_file_name: &str) -> Option<PathBuf> {
120 return Ok(vec![p]); 105 if path.ends_with(target_file_name) {
106 return Some(path.to_owned());
121 } 107 }
122 108
123 let entities = read_dir(path)?;
124 Ok(find_cargo_toml_in_child_dir(entities))
125 }
126
127 fn find_cargo_toml_in_parent_dir(path: &Path) -> Option<PathBuf> {
128 let mut curr = Some(path); 109 let mut curr = Some(path);
110
129 while let Some(path) = curr { 111 while let Some(path) = curr {
130 let candidate = path.join("Cargo.toml"); 112 let candidate = path.join(target_file_name);
131 if candidate.exists() { 113 if candidate.exists() {
132 return Some(candidate); 114 return Some(candidate);
133 } 115 }
@@ -139,14 +121,11 @@ impl ProjectRoot {
139 121
140 fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> { 122 fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> {
141 // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects 123 // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects
142 let mut valid_canditates = vec![]; 124 entities
143 for entity in entities.filter_map(Result::ok) { 125 .filter_map(Result::ok)
144 let candidate = entity.path().join("Cargo.toml"); 126 .map(|it| it.path().join("Cargo.toml"))
145 if candidate.exists() { 127 .filter(|it| it.exists())
146 valid_canditates.push(candidate) 128 .collect()
147 }
148 }
149 valid_canditates
150 } 129 }
151 } 130 }
152} 131}
@@ -398,7 +377,18 @@ impl ProjectWorkspace {
398 let edition = cargo[pkg].edition; 377 let edition = cargo[pkg].edition;
399 let cfg_options = { 378 let cfg_options = {
400 let mut opts = default_cfg_options.clone(); 379 let mut opts = default_cfg_options.clone();
401 opts.insert_features(cargo[pkg].features.iter().map(Into::into)); 380 for feature in cargo[pkg].features.iter() {
381 opts.insert_key_value("feature".into(), feature.into());
382 }
383 for cfg in cargo[pkg].cfgs.iter() {
384 match cfg.find('=') {
385 Some(split) => opts.insert_key_value(
386 cfg[..split].into(),
387 cfg[split + 1..].trim_matches('"').into(),
388 ),
389 None => opts.insert_atom(cfg.into()),
390 };
391 }
402 opts 392 opts
403 }; 393 };
404 let mut env = Env::default(); 394 let mut env = Env::default();
@@ -543,7 +533,7 @@ impl ProjectWorkspace {
543 } 533 }
544} 534}
545 535
546pub fn get_rustc_cfg_options() -> CfgOptions { 536pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
547 let mut cfg_options = CfgOptions::default(); 537 let mut cfg_options = CfgOptions::default();
548 538
549 // Some nightly-only cfgs, which are required for stdlib 539 // Some nightly-only cfgs, which are required for stdlib
@@ -556,23 +546,18 @@ pub fn get_rustc_cfg_options() -> CfgOptions {
556 } 546 }
557 } 547 }
558 548
559 match (|| -> Result<String> { 549 let rustc_cfgs = || -> Result<String> {
560 // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. 550 // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
561 let output = Command::new("rustc") 551 let mut cmd = Command::new(ra_toolchain::rustc());
562 .args(&["--print", "cfg", "-O"]) 552 cmd.args(&["--print", "cfg", "-O"]);
563 .output() 553 if let Some(target) = target {
564 .context("Failed to get output from rustc --print cfg -O")?; 554 cmd.args(&["--target", target.as_str()]);
565 if !output.status.success() {
566 bail!(
567 "rustc --print cfg -O exited with exit code ({})",
568 output
569 .status
570 .code()
571 .map_or(String::from("no exit code"), |code| format!("{}", code))
572 );
573 } 555 }
556 let output = output(cmd)?;
574 Ok(String::from_utf8(output.stdout)?) 557 Ok(String::from_utf8(output.stdout)?)
575 })() { 558 }();
559
560 match rustc_cfgs {
576 Ok(rustc_cfgs) => { 561 Ok(rustc_cfgs) => {
577 for line in rustc_cfgs.lines() { 562 for line in rustc_cfgs.lines() {
578 match line.find('=') { 563 match line.find('=') {
@@ -585,8 +570,21 @@ pub fn get_rustc_cfg_options() -> CfgOptions {
585 } 570 }
586 } 571 }
587 } 572 }
588 Err(e) => log::error!("failed to get rustc cfgs: {}", e), 573 Err(e) => log::error!("failed to get rustc cfgs: {:#}", e),
589 } 574 }
590 575
591 cfg_options 576 cfg_options
592} 577}
578
579fn output(mut cmd: Command) -> Result<Output> {
580 let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?;
581 if !output.status.success() {
582 match String::from_utf8(output.stderr) {
583 Ok(stderr) if !stderr.is_empty() => {
584 bail!("{:?} failed, {}\nstderr:\n{}", cmd, output.status, stderr)
585 }
586 _ => bail!("{:?} failed, {}", cmd, output.status),
587 }
588 }
589 Ok(output)
590}
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 55ff5ad80..a8a196e64 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -1,14 +1,16 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use anyhow::{bail, Context, Result};
4use std::{ 3use std::{
5 env, ops, 4 env, ops,
6 path::{Path, PathBuf}, 5 path::{Path, PathBuf},
7 process::{Command, Output}, 6 process::Command,
8}; 7};
9 8
9use anyhow::{bail, Result};
10use ra_arena::{Arena, Idx}; 10use ra_arena::{Arena, Idx};
11 11
12use crate::output;
13
12#[derive(Default, Debug, Clone)] 14#[derive(Default, Debug, Clone)]
13pub struct Sysroot { 15pub struct Sysroot {
14 crates: Arena<SysrootCrateData>, 16 crates: Arena<SysrootCrateData>,
@@ -84,43 +86,22 @@ impl Sysroot {
84 } 86 }
85} 87}
86 88
87fn create_command_text(program: &str, args: &[&str]) -> String {
88 format!("{} {}", program, args.join(" "))
89}
90
91fn run_command_in_cargo_dir(cargo_toml: &Path, program: &str, args: &[&str]) -> Result<Output> {
92 let output = Command::new(program)
93 .current_dir(cargo_toml.parent().unwrap())
94 .args(args)
95 .output()
96 .context(format!("{} failed", create_command_text(program, args)))?;
97 if !output.status.success() {
98 match output.status.code() {
99 Some(code) => bail!(
100 "failed to run the command: '{}' exited with code {}",
101 create_command_text(program, args),
102 code
103 ),
104 None => bail!(
105 "failed to run the command: '{}' terminated by signal",
106 create_command_text(program, args)
107 ),
108 };
109 }
110 Ok(output)
111}
112
113fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> { 89fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
114 if let Ok(path) = env::var("RUST_SRC_PATH") { 90 if let Ok(path) = env::var("RUST_SRC_PATH") {
115 return Ok(path.into()); 91 return Ok(path.into());
116 } 92 }
117 let rustc_output = run_command_in_cargo_dir(cargo_toml, "rustc", &["--print", "sysroot"])?; 93 let current_dir = cargo_toml.parent().unwrap();
94 let mut rustc = Command::new(ra_toolchain::rustc());
95 rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
96 let rustc_output = output(rustc)?;
118 let stdout = String::from_utf8(rustc_output.stdout)?; 97 let stdout = String::from_utf8(rustc_output.stdout)?;
119 let sysroot_path = Path::new(stdout.trim()); 98 let sysroot_path = Path::new(stdout.trim());
120 let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); 99 let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
121 100
122 if !src_path.exists() { 101 if !src_path.exists() {
123 run_command_in_cargo_dir(cargo_toml, "rustup", &["component", "add", "rust-src"])?; 102 let mut rustup = Command::new(ra_toolchain::rustup());
103 rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
104 let _output = output(rustup)?;
124 } 105 }
125 if !src_path.exists() { 106 if !src_path.exists() {
126 bail!( 107 bail!(