4864: Add optional target to crates in json project r=Nashenas88 a=Nashenas88

Lookup default cfgs per target when generating cfg list.

Should fully address #4840

CC @woody77 

Co-authored-by: Paul Daniel Faria <Nashenas88@users.noreply.github.com>
Co-authored-by: Paul Daniel Faria <nashenas88@users.noreply.github.com>
This commit is contained in:
bors[bot] 2020-07-04 18:00:17 +00:00 committed by GitHub
commit 4cfe0070dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View file

@ -46,4 +46,14 @@ impl CfgOptions {
pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) {
self.key_values.insert((key, value));
}
pub fn append(&mut self, other: &CfgOptions) {
for atom in &other.atoms {
self.atoms.insert(atom.clone());
}
for (key, value) in &other.key_values {
self.key_values.insert((key.clone(), value.clone()));
}
}
}

View file

@ -246,6 +246,7 @@ impl ProjectWorkspace {
let mut crate_graph = CrateGraph::default();
match self {
ProjectWorkspace::Json { project } => {
let mut target_cfg_map = FxHashMap::<Option<&str>, CfgOptions>::default();
let crates: FxHashMap<_, _> = project
.crates
.iter()
@ -265,6 +266,14 @@ impl ProjectWorkspace {
.proc_macro_dylib_path
.clone()
.map(|it| proc_macro_client.by_dylib_path(&it));
let target = krate.target.as_deref().or(target);
let target_cfgs = target_cfg_map
.entry(target.clone())
.or_insert_with(|| get_rustc_cfg_options(target.as_deref()));
let mut cfg_options = krate.cfg.clone();
cfg_options.append(target_cfgs);
// FIXME: No crate name in json definition such that we cannot add OUT_DIR to env
Some((
CrateId(seq_index as u32),
@ -273,7 +282,7 @@ impl ProjectWorkspace {
krate.edition,
// FIXME json definitions can store the crate name
None,
krate.cfg.clone(),
cfg_options,
env,
proc_macro.unwrap_or_default(),
),

View file

@ -31,6 +31,7 @@ pub struct Crate {
pub(crate) edition: Edition,
pub(crate) deps: Vec<Dependency>,
pub(crate) cfg: CfgOptions,
pub(crate) target: Option<String>,
pub(crate) out_dir: Option<AbsPathBuf>,
pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
}
@ -65,6 +66,7 @@ impl ProjectJson {
}
cfg
},
target: crate_data.target,
out_dir: crate_data.out_dir.map(|it| base.join(it)),
proc_macro_dylib_path: crate_data.proc_macro_dylib_path.map(|it| base.join(it)),
})
@ -86,6 +88,7 @@ struct CrateData {
deps: Vec<DepData>,
#[serde(default)]
cfg: FxHashSet<String>,
target: Option<String>,
out_dir: Option<PathBuf>,
proc_macro_dylib_path: Option<PathBuf>,
}