diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 362ee30fe04..59f46a2a05e 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -56,6 +56,9 @@ pub struct CargoConfig { /// Runs cargo check on launch to figure out the correct values of OUT_DIR pub load_out_dirs_from_check: bool, + + /// rustc target + pub target: Option, } impl Default for CargoConfig { @@ -65,6 +68,7 @@ impl Default for CargoConfig { all_features: true, features: Vec::new(), load_out_dirs_from_check: false, + target: None, } } } @@ -160,6 +164,9 @@ impl CargoWorkspace { if let Some(parent) = cargo_toml.parent() { meta.current_dir(parent); } + if let Some(target) = cargo_features.target.as_ref() { + meta.other_options(&[String::from("--filter-platform"), target.clone()]); + } let meta = meta.exec().with_context(|| { format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display()) })?; diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 731cbd29181..c2b33c1dcaf 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -543,7 +543,7 @@ impl ProjectWorkspace { } } -pub fn get_rustc_cfg_options() -> CfgOptions { +pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { let mut cfg_options = CfgOptions::default(); // Some nightly-only cfgs, which are required for stdlib @@ -558,10 +558,12 @@ pub fn get_rustc_cfg_options() -> CfgOptions { match (|| -> Result { // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. - let output = Command::new("rustc") - .args(&["--print", "cfg", "-O"]) - .output() - .context("Failed to get output from rustc --print cfg -O")?; + let mut cmd = Command::new("rustc"); + cmd.args(&["--print", "cfg", "-O"]); + if let Some(target) = target { + cmd.args(&["--target", target.as_str()]); + } + let output = cmd.output().context("Failed to get output from rustc --print cfg -O")?; if !output.status.success() { bail!( "rustc --print cfg -O exited with exit code ({})", diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index d0a71120aaf..023ced6cfb9 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs @@ -149,7 +149,7 @@ pub(crate) fn load( // FIXME: cfg options? let default_cfg_options = { - let mut opts = get_rustc_cfg_options(); + let mut opts = get_rustc_cfg_options(None); opts.insert_atom("test".into()); opts.insert_atom("debug_assertion".into()); opts diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 15b7c691219..8d85c60cf92 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -131,6 +131,7 @@ impl Config { set(value, "/cargo/allFeatures", &mut self.cargo.all_features); set(value, "/cargo/features", &mut self.cargo.features); set(value, "/cargo/loadOutDirsFromCheck", &mut self.cargo.load_out_dirs_from_check); + set(value, "/cargo/target", &mut self.cargo.target); match get(value, "/procMacro/enable") { Some(true) => { diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index 34941931b78..16020648df6 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs @@ -131,7 +131,7 @@ impl WorldState { // FIXME: Read default cfgs from config let default_cfg_options = { - let mut opts = get_rustc_cfg_options(); + let mut opts = get_rustc_cfg_options(config.cargo.target.as_ref()); opts.insert_atom("test".into()); opts.insert_atom("debug_assertion".into()); opts diff --git a/editors/code/package.json b/editors/code/package.json index 442b9de98ec..a05a697526b 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -233,6 +233,14 @@ "default": false, "markdownDescription": "Run `cargo check` on startup to get the correct value for package OUT_DIRs" }, + "rust-analyzer.cargo.target": { + "type": [ + "null", + "string" + ], + "default": null, + "description": "Specify the compilation target" + }, "rust-analyzer.rustfmt.extraArgs": { "type": "array", "items": {