Remove default_cfg_options, pass target instead so it can be used for building cargo workspaces

This commit is contained in:
Paul Daniel Faria 2020-06-08 10:23:20 -04:00
parent 3937b225e7
commit 9c35f135b9
3 changed files with 19 additions and 38 deletions

View File

@ -250,7 +250,7 @@ impl ProjectWorkspace {
pub fn to_crate_graph(
&self,
default_cfg_options: &CfgOptions,
target: Option<&String>,
extern_source_roots: &FxHashMap<PathBuf, ExternSourceId>,
proc_macro_client: &ProcMacroClient,
load: &mut dyn FnMut(&Path) -> Option<FileId>,
@ -269,7 +269,7 @@ impl ProjectWorkspace {
json_project::Edition::Edition2018 => Edition::Edition2018,
};
let cfg_options = {
let mut opts = default_cfg_options.clone();
let mut opts = CfgOptions::default();
for cfg in &krate.cfg {
match cfg.find('=') {
None => opts.insert_atom(cfg.into()),
@ -343,14 +343,13 @@ impl ProjectWorkspace {
}
}
ProjectWorkspace::Cargo { cargo, sysroot } => {
let mut cfg_options = get_rustc_cfg_options(target);
let sysroot_crates: FxHashMap<_, _> = sysroot
.crates()
.filter_map(|krate| {
let file_id = load(&sysroot[krate].root)?;
// Crates from sysroot have `cfg(test)` disabled
let cfg_options = default_cfg_options.clone();
let env = Env::default();
let extern_source = ExternSource::default();
let proc_macro = vec![];
@ -361,7 +360,7 @@ impl ProjectWorkspace {
file_id,
Edition::Edition2018,
Some(crate_name),
cfg_options,
cfg_options.clone(),
env,
extern_source,
proc_macro,
@ -392,6 +391,10 @@ impl ProjectWorkspace {
let mut pkg_to_lib_crate = FxHashMap::default();
let mut pkg_crates = FxHashMap::default();
// Add test cfg for non-sysroot crates
cfg_options.insert_atom("test".into());
// Next, create crates for each package, target pair
for pkg in cargo.packages() {
let mut lib_tgt = None;
@ -400,12 +403,7 @@ impl ProjectWorkspace {
if let Some(file_id) = load(root) {
let edition = cargo[pkg].edition;
let cfg_options = {
let mut opts = {
let mut opts = default_cfg_options.clone();
opts.insert_atom("test".into());
opts
};
let mut opts = cfg_options.clone();
for feature in cargo[pkg].features.iter() {
opts.insert_key_value("feature".into(), feature.into());
}
@ -562,7 +560,7 @@ impl ProjectWorkspace {
}
}
pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
let mut cfg_options = CfgOptions::default();
// Some nightly-only cfgs, which are required for stdlib
@ -602,6 +600,8 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
Err(e) => log::error!("failed to get rustc cfgs: {:#}", e),
}
cfg_options.insert_atom("debug_assertion".into());
cfg_options
}

View File

@ -8,8 +8,7 @@ use crossbeam_channel::{unbounded, Receiver};
use ra_db::{ExternSourceId, FileId, SourceRootId};
use ra_ide::{AnalysisChange, AnalysisHost};
use ra_project_model::{
get_rustc_cfg_options, CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest,
ProjectWorkspace,
CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest, ProjectWorkspace,
};
use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
use rustc_hash::{FxHashMap, FxHashSet};
@ -148,25 +147,14 @@ pub(crate) fn load(
}
}
// FIXME: cfg options?
let default_cfg_options = {
let mut opts = get_rustc_cfg_options(None);
opts.insert_atom("debug_assertion".into());
opts
};
let crate_graph = ws.to_crate_graph(
&default_cfg_options,
&extern_source_roots,
proc_macro_client,
&mut |path: &Path| {
let crate_graph =
ws.to_crate_graph(None, &extern_source_roots, proc_macro_client, &mut |path: &Path| {
// Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
let path = path.canonicalize().ok()?;
let vfs_file = vfs.load(&path);
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
vfs_file.map(vfs_file_to_id)
},
);
});
log::debug!("crate graph: {:?}", crate_graph);
analysis_change.set_crate_graph(crate_graph);

View File

@ -15,7 +15,7 @@ use ra_flycheck::{Flycheck, FlycheckConfig};
use ra_ide::{
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
};
use ra_project_model::{get_rustc_cfg_options, ProcMacroClient, ProjectWorkspace};
use ra_project_model::{ProcMacroClient, ProjectWorkspace};
use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch};
use relative_path::RelativePathBuf;
use stdx::format_to;
@ -135,13 +135,6 @@ impl GlobalState {
}
}
// FIXME: Read default cfgs from config
let default_cfg_options = {
let mut opts = get_rustc_cfg_options(config.cargo.target.as_ref());
opts.insert_atom("debug_assertion".into());
opts
};
let proc_macro_client = match &config.proc_macro_srv {
None => ProcMacroClient::dummy(),
Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
@ -167,7 +160,7 @@ impl GlobalState {
};
for ws in workspaces.iter() {
crate_graph.extend(ws.to_crate_graph(
&default_cfg_options,
config.cargo.target.as_ref(),
&extern_source_roots,
&proc_macro_client,
&mut load,