Skip to content

Commit

Permalink
Auto merge of rust-lang#17857 - ChayimFriedman2:rust-project-cfg-grou…
Browse files Browse the repository at this point in the history
…p, r=Veykril

feat: Allow declaring cfg groups in rust-project.json, to help sharing common cfgs

Closes rust-lang#17815.
  • Loading branch information
bors committed Aug 23, 2024
2 parents e030cf0 + aa6e8d3 commit 3a097e1
Show file tree
Hide file tree
Showing 5 changed files with 619 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/tools/rust-analyzer/crates/project-model/src/project_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
use base_db::{CrateDisplayName, CrateName};
use cfg::CfgAtom;
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
use rustc_hash::FxHashMap;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{de, Deserialize, Serialize};
use span::Edition;

Expand Down Expand Up @@ -122,6 +122,25 @@ impl ProjectJson {
None => None,
};

let cfg = crate_data
.cfg_groups
.iter()
.flat_map(|cfg_extend| {
let cfg_group = data.cfg_groups.get(cfg_extend);
match cfg_group {
Some(cfg_group) => cfg_group.0.iter().cloned(),
None => {
tracing::error!(
"Unknown cfg group `{cfg_extend}` in crate `{}`",
crate_data.display_name.as_deref().unwrap_or("<unknown>"),
);
[].iter().cloned()
}
}
})
.chain(crate_data.cfg.0)
.collect();

Crate {
display_name: crate_data
.display_name
Expand All @@ -131,7 +150,7 @@ impl ProjectJson {
edition: crate_data.edition.into(),
version: crate_data.version.as_ref().map(ToString::to_string),
deps: crate_data.deps,
cfg: crate_data.cfg,
cfg,
target: crate_data.target,
env: crate_data.env,
proc_macro_dylib_path: crate_data
Expand Down Expand Up @@ -306,11 +325,17 @@ pub enum RunnableKind {
pub struct ProjectJsonData {
sysroot: Option<Utf8PathBuf>,
sysroot_src: Option<Utf8PathBuf>,
#[serde(default)]
cfg_groups: FxHashMap<String, CfgList>,
crates: Vec<CrateData>,
#[serde(default)]
runnables: Vec<RunnableData>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Default)]
#[serde(transparent)]
struct CfgList(#[serde(with = "cfg_")] Vec<CfgAtom>);

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
struct CrateData {
display_name: Option<String>,
Expand All @@ -320,8 +345,9 @@ struct CrateData {
version: Option<semver::Version>,
deps: Vec<Dep>,
#[serde(default)]
#[serde(with = "cfg_")]
cfg: Vec<CfgAtom>,
cfg_groups: FxHashSet<String>,
#[serde(default)]
cfg: CfgList,
target: Option<String>,
#[serde(default)]
env: FxHashMap<String, String>,
Expand Down
6 changes: 6 additions & 0 deletions src/tools/rust-analyzer/crates/project-model/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ fn rust_project_hello_world_project_model() {
);
}

#[test]
fn rust_project_cfg_groups() {
let (crate_graph, _proc_macros) = load_rust_project("cfg-groups.json");
check_crate_graph(crate_graph, expect_file!["../test_data/output/rust_project_cfg_groups.txt"]);
}

#[test]
fn rust_project_is_proc_macro_has_proc_macro_dep() {
let (crate_graph, _proc_macros) = load_rust_project("is-proc-macro-project.json");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"sysroot_src": null,
"cfg_groups": {
"group1": ["group1_cfg=\"some_config\"", "group1_other_cfg=\"other_config\""],
"group2": ["group2_cfg=\"yet_another_config\""]
},
"crates": [
{
"display_name": "hello_world",
"root_module": "$ROOT$src/lib.rs",
"edition": "2018",
"cfg_groups": ["group1", "group2"],
"deps": [],
"is_workspace_member": true
},
{
"display_name": "other_crate",
"root_module": "$ROOT$src/lib.rs",
"edition": "2018",
"cfg_groups": ["group2"],
"cfg": ["group2_cfg=\"fourth_config\"", "unrelated_cfg"],
"deps": [],
"is_workspace_member": true
}
]
}
Loading

0 comments on commit 3a097e1

Please sign in to comment.