-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
193 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[package] | ||
name = "tool_features" | ||
version = "0.1.0" | ||
authors = ["Microsoft"] | ||
edition = "2021" | ||
rust-version = "1.56" | ||
license = "MIT OR Apache-2.0" | ||
description = "Windows cargo feature search tool" | ||
repository = "https://github.com/microsoft/windows-rs" | ||
readme = "readme.md" | ||
|
||
[dependencies.indexmap] | ||
version = "2.1.0" | ||
features = ["serde"] | ||
|
||
[dependencies.serde] | ||
version = "*" | ||
features = ["derive"] | ||
|
||
[dependencies.serde_json] | ||
version = "1.0" | ||
|
||
[dependencies.windows] | ||
path = "../../libs/windows" | ||
version = "0.52.0" | ||
|
||
[dependencies.windows-bindgen] | ||
path = "../../libs/bindgen" | ||
version = "0.52.0" | ||
|
||
[dependencies.windows-metadata] | ||
path = "../../libs/metadata" | ||
version = "0.52.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
use indexmap::IndexMap; | ||
use serde::ser::SerializeSeq; | ||
use serde::{Serialize, Serializer}; | ||
use std::{fs::OpenOptions, io::Write}; | ||
use windows_metadata::Item; | ||
|
||
fn main() { | ||
match feature_index() { | ||
Ok(ok) => { | ||
OpenOptions::new() | ||
.create(true) | ||
.write(true) | ||
.truncate(true) | ||
.open("index.json") | ||
.unwrap() | ||
.write_all(ok.as_bytes()) | ||
.expect("Failed to write out index."); | ||
} | ||
Err(error) => { | ||
eprintln!("{}", error); | ||
std::process::exit(1); | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default, Serialize)] | ||
struct FeatureIndex { | ||
#[serde(serialize_with = "serialize_keys")] | ||
namespace_map: IndexMap<String, u32>, | ||
#[serde(serialize_with = "serialize_keys")] | ||
feature_map: IndexMap<String, u32>, | ||
namespaces: IndexMap<u32, Vec<FeatureIndexItem>>, | ||
} | ||
|
||
#[derive(Default, Serialize)] | ||
struct FeatureIndexItem { | ||
name: String, | ||
features: Vec<u32>, | ||
} | ||
|
||
fn serialize_keys<S>(map: &IndexMap<String, u32>, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut seq = serializer.serialize_seq(Some(map.len()))?; | ||
for (k, _v) in map { | ||
seq.serialize_element(k)?; | ||
} | ||
seq.end() | ||
} | ||
|
||
pub fn feature_index() -> windows::core::Result<String> { | ||
let files = windows_bindgen::read_input(&[]).unwrap(); | ||
let reader = windows_metadata::Reader::filter( | ||
files, | ||
&["Windows"], | ||
&[ | ||
"Windows.AI.MachineLearning.Preview", | ||
"Windows.ApplicationModel.SocialInfo", | ||
"Windows.Devices.AllJoyn", | ||
"Windows.Devices.Perception", | ||
"Windows.Security.Authentication.Identity.Provider", | ||
"Windows.Services.Cortana", | ||
"Windows.System.Power.Diagnostics", | ||
"Windows.System.Preview", | ||
"Windows.UI.Xaml", | ||
"Windows.Win32.Foundation.Metadata", | ||
"Windows.Win32.System.Diagnostics.Debug.WebApp", | ||
"Windows.Win32.System.WinRT.Xaml", | ||
"Windows.Win32.UI.Xaml", | ||
"Windows.Win32.Web.MsHtml", | ||
], | ||
); | ||
|
||
let mut feature_index = FeatureIndex { | ||
..Default::default() | ||
}; | ||
let mut next_ns_idx = 0; | ||
let mut next_feature_idx = 0; | ||
|
||
for namespace in reader.namespaces() { | ||
for item in reader.namespace_items(namespace) { | ||
let namespace_idx = feature_index | ||
.namespace_map | ||
.entry(namespace.to_string()) | ||
.or_insert_with(|| { | ||
next_ns_idx += 1; | ||
next_ns_idx | ||
}); | ||
|
||
let mut index_item = FeatureIndexItem { | ||
..Default::default() | ||
}; | ||
let mut cfg = windows_bindgen::rust::Cfg::default(); | ||
cfg.add_feature(namespace); | ||
|
||
cfg = match item { | ||
Item::Type(def) => { | ||
index_item.name = def.name().to_string(); | ||
cfg.union(&windows_bindgen::rust::type_def_cfg(def, &[])) | ||
} | ||
Item::Const(field) => { | ||
index_item.name = field.name().to_string(); | ||
cfg.union(&windows_bindgen::rust::field_cfg(field)) | ||
} | ||
Item::Fn(method, _) => { | ||
index_item.name = method.name().to_string(); | ||
cfg.union(&windows_bindgen::rust::signature_cfg(method)) | ||
} | ||
}; | ||
|
||
let cfg_features = windows_bindgen::rust::cfg_features(&cfg); | ||
index_item.features = cfg_features | ||
.iter() | ||
.map(|feature| { | ||
let feature_idx = feature_index | ||
.feature_map | ||
.entry(feature.clone()) | ||
.or_insert_with(|| { | ||
next_feature_idx += 1; | ||
next_feature_idx | ||
}); | ||
*feature_idx | ||
}) | ||
.collect(); | ||
|
||
feature_index | ||
.namespaces | ||
.entry(*namespace_idx) | ||
.or_default() | ||
.push(index_item); | ||
} | ||
} | ||
|
||
Ok(serde_json::to_string(&feature_index).unwrap()) | ||
} |