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>
This commit is contained in:
bors[bot] 2020-05-05 17:25:52 +00:00 committed by GitHub
commit 8803e748a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 7 deletions

View file

@ -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<String>,
}
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())
})?;

View file

@ -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<String> {
// `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 ({})",

View file

@ -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

View file

@ -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) => {

View file

@ -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

View file

@ -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": {