Skip to content

Commit 8803e74

Browse files
bors[bot]FuriouZz
andauthored
Merge #4166
4166: Defining a default target to support cross-compilation targets r=matklad a=FuriouZz Related to #4163 Co-authored-by: Christophe MASSOLIN <christophe.massolin@gmail.com>
2 parents df7b590 + 04e32fb commit 8803e74

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed

crates/ra_project_model/src/cargo_workspace.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ pub struct CargoConfig {
5656

5757
/// Runs cargo check on launch to figure out the correct values of OUT_DIR
5858
pub load_out_dirs_from_check: bool,
59+
60+
/// rustc target
61+
pub target: Option<String>,
5962
}
6063

6164
impl Default for CargoConfig {
@@ -65,6 +68,7 @@ impl Default for CargoConfig {
6568
all_features: true,
6669
features: Vec::new(),
6770
load_out_dirs_from_check: false,
71+
target: None,
6872
}
6973
}
7074
}
@@ -160,6 +164,9 @@ impl CargoWorkspace {
160164
if let Some(parent) = cargo_toml.parent() {
161165
meta.current_dir(parent);
162166
}
167+
if let Some(target) = cargo_features.target.as_ref() {
168+
meta.other_options(&[String::from("--filter-platform"), target.clone()]);
169+
}
163170
let meta = meta.exec().with_context(|| {
164171
format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())
165172
})?;

crates/ra_project_model/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ impl ProjectWorkspace {
543543
}
544544
}
545545

546-
pub fn get_rustc_cfg_options() -> CfgOptions {
546+
pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
547547
let mut cfg_options = CfgOptions::default();
548548

549549
// Some nightly-only cfgs, which are required for stdlib
@@ -558,10 +558,12 @@ pub fn get_rustc_cfg_options() -> CfgOptions {
558558

559559
match (|| -> Result<String> {
560560
// `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
561-
let output = Command::new("rustc")
562-
.args(&["--print", "cfg", "-O"])
563-
.output()
564-
.context("Failed to get output from rustc --print cfg -O")?;
561+
let mut cmd = Command::new("rustc");
562+
cmd.args(&["--print", "cfg", "-O"]);
563+
if let Some(target) = target {
564+
cmd.args(&["--target", target.as_str()]);
565+
}
566+
let output = cmd.output().context("Failed to get output from rustc --print cfg -O")?;
565567
if !output.status.success() {
566568
bail!(
567569
"rustc --print cfg -O exited with exit code ({})",

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub(crate) fn load(
149149

150150
// FIXME: cfg options?
151151
let default_cfg_options = {
152-
let mut opts = get_rustc_cfg_options();
152+
let mut opts = get_rustc_cfg_options(None);
153153
opts.insert_atom("test".into());
154154
opts.insert_atom("debug_assertion".into());
155155
opts

crates/rust-analyzer/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ impl Config {
131131
set(value, "/cargo/allFeatures", &mut self.cargo.all_features);
132132
set(value, "/cargo/features", &mut self.cargo.features);
133133
set(value, "/cargo/loadOutDirsFromCheck", &mut self.cargo.load_out_dirs_from_check);
134+
set(value, "/cargo/target", &mut self.cargo.target);
134135

135136
match get(value, "/procMacro/enable") {
136137
Some(true) => {

crates/rust-analyzer/src/world.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl WorldState {
131131

132132
// FIXME: Read default cfgs from config
133133
let default_cfg_options = {
134-
let mut opts = get_rustc_cfg_options();
134+
let mut opts = get_rustc_cfg_options(config.cargo.target.as_ref());
135135
opts.insert_atom("test".into());
136136
opts.insert_atom("debug_assertion".into());
137137
opts

editors/code/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@
233233
"default": false,
234234
"markdownDescription": "Run `cargo check` on startup to get the correct value for package OUT_DIRs"
235235
},
236+
"rust-analyzer.cargo.target": {
237+
"type": [
238+
"null",
239+
"string"
240+
],
241+
"default": null,
242+
"description": "Specify the compilation target"
243+
},
236244
"rust-analyzer.rustfmt.extraArgs": {
237245
"type": "array",
238246
"items": {

0 commit comments

Comments
 (0)