/
main.rs
133 lines (111 loc) · 4.8 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use rayon::prelude::*;
use std::collections::*;
use std::io::prelude::*;
fn main() {
let mut rustfmt = true;
let mut expect_namespace = false;
let mut namespace = String::new();
for arg in std::env::args() {
match arg.as_str() {
"-p" => rustfmt = false,
"-n" => expect_namespace = true,
_ => {
if expect_namespace {
namespace = arg;
}
}
}
}
let mut output = std::path::PathBuf::from("crates/libs/sys/src/Windows");
if namespace.is_empty() {
let _ = std::fs::remove_dir_all(&output);
}
output.pop();
let files = vec![metadata::reader::File::new("crates/libs/metadata/default/Windows.winmd").unwrap(), metadata::reader::File::new("crates/libs/metadata/default/Windows.Win32.winmd").unwrap(), metadata::reader::File::new("crates/libs/metadata/default/Windows.Win32.Interop.winmd").unwrap()];
let reader = &metadata::reader::Reader::new(&files);
if !namespace.is_empty() {
let tree = reader.tree(&namespace, &[]).expect("Namespace not found");
gen_tree(reader, &output, &tree, rustfmt);
return;
}
let win32 = reader.tree("Windows.Win32", &lib::EXCLUDE_NAMESPACES).expect("`Windows.Win32` namespace not found");
let root = metadata::reader::Tree { namespace: "Windows", nested: BTreeMap::from([("Win32", win32)]) };
let trees = root.flatten();
trees.par_iter().for_each(|tree| gen_tree(reader, &output, tree, rustfmt));
output.pop();
output.push("Cargo.toml");
let mut file = std::fs::File::create(&output).unwrap();
file.write_all(
r#"
[package]
name = "windows-sys"
version = "0.41.0"
authors = ["Microsoft"]
edition = "2018"
license = "MIT OR Apache-2.0"
description = "Rust for Windows"
repository = "https://github.com/microsoft/windows-rs"
readme = "../../../docs/readme.md"
rust-version = "1.49"
[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-msvc"
targets = []
all-features = true
[target.i686-pc-windows-msvc.dependencies]
windows_i686_msvc = { path = "../../targets/i686_msvc", version = "0.41.0" }
[target.i686-uwp-windows-msvc.dependencies]
windows_i686_msvc = { path = "../../targets/i686_msvc", version = "0.41.0" }
[target.x86_64-pc-windows-msvc.dependencies]
windows_x86_64_msvc = { path = "../../targets/x86_64_msvc", version = "0.41.0" }
[target.x86_64-uwp-windows-msvc.dependencies]
windows_x86_64_msvc = { path = "../../targets/x86_64_msvc", version = "0.41.0" }
[target.aarch64-pc-windows-msvc.dependencies]
windows_aarch64_msvc = { path = "../../targets/aarch64_msvc", version = "0.41.0" }
[target.aarch64-uwp-windows-msvc.dependencies]
windows_aarch64_msvc = { path = "../../targets/aarch64_msvc", version = "0.41.0" }
[target.aarch64-pc-windows-gnullvm.dependencies]
windows_aarch64_gnullvm = { path = "../../targets/aarch64_gnullvm", version = "0.41.0" }
[target.i686-pc-windows-gnu.dependencies]
windows_i686_gnu = { path = "../../targets/i686_gnu", version = "0.41.0" }
[target.i686-uwp-windows-gnu.dependencies]
windows_i686_gnu = { path = "../../targets/i686_gnu", version = "0.41.0" }
[target.x86_64-pc-windows-gnu.dependencies]
windows_x86_64_gnu = { path = "../../targets/x86_64_gnu", version = "0.41.0" }
[target.x86_64-uwp-windows-gnu.dependencies]
windows_x86_64_gnu = { path = "../../targets/x86_64_gnu", version = "0.41.0" }
[target.x86_64-pc-windows-gnullvm.dependencies]
windows_x86_64_gnullvm = { path = "../../targets/x86_64_gnullvm", version = "0.41.0" }
[features]
default = []
deprecated = []
"#
.as_bytes(),
)
.unwrap();
// Skip the root Windows tree while writing features
for tree in trees.iter().skip(1) {
let feature = tree.namespace[root.namespace.len() + 1..].replace('.', "_");
if let Some(pos) = feature.rfind('_') {
let dependency = &feature[..pos];
file.write_all(format!("{} = [\"{}\"]\n", feature, dependency).as_bytes()).unwrap();
} else {
file.write_all(format!("{} = []\n", feature).as_bytes()).unwrap();
}
}
std::fs::copy("license-mit", "crates/libs/sys/license-mit").unwrap();
std::fs::copy("license-apache-2.0", "crates/libs/sys/license-apache-2.0").unwrap();
}
fn gen_tree(reader: &metadata::reader::Reader, output: &std::path::Path, tree: &metadata::reader::Tree, rustfmt: bool) {
println!("{}", tree.namespace);
let mut path = std::path::PathBuf::from(output);
path.push(tree.namespace.replace('.', "/"));
std::fs::create_dir_all(&path).unwrap();
let mut gen = bindgen::Gen::new(reader);
gen.namespace = tree.namespace;
gen.sys = true;
gen.cfg = true;
gen.doc = true;
let mut tokens = bindgen::namespace(&gen, tree);
lib::format(tree.namespace, &mut tokens, rustfmt);
std::fs::write(path.join("mod.rs"), tokens).unwrap();
}