forked from microsoft/windows-rs
/
main.rs
162 lines (137 loc) · 5.44 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::collections::{BTreeMap, BTreeSet};
use std::io::prelude::*;
use std::process::{Command, Stdio};
fn main() {
const ALL_PLATFORMS: [&str; 4] = ["x86_64_gnu", "i686_gnu", "x86_64_gnullvm", "aarch64_gnullvm"];
let mut platforms = BTreeSet::new();
for platform in std::env::args().skip(1) {
if ALL_PLATFORMS.contains(&&*platform) {
platforms.insert(platform);
} else if platform == "all" {
platforms.extend(ALL_PLATFORMS.iter().map(|s| s.to_string()));
} else {
eprintln!("Unknown platform: {}", platform);
return;
}
}
if platforms.is_empty() {
eprintln!("Please specify at least one platform or use 'all' argument");
return;
};
for platform in platforms {
let tools = if platform.ends_with("_gnu") { &["dlltool", "ar", "objcopy"][..] } else { &["llvm-dlltool", "llvm-ar"][..] };
for tool in tools {
if Command::new(tool).stdout(Stdio::null()).stderr(Stdio::null()).spawn().is_err() {
eprintln!("Could not find {}. Is it in your $PATH?", tool);
return;
}
}
build_platform(&platform, tools[0], tools[1]);
}
}
fn build_platform(platform: &str, dlltool: &str, ar: &str) {
println!("Platform: {}", platform);
let libraries = lib::libraries();
let output = std::path::PathBuf::from(format!("crates/targets/{}/lib", platform));
let _ = std::fs::remove_dir_all(&output);
std::fs::create_dir_all(&output).unwrap();
for (library, functions) in &libraries {
build_library(&output, dlltool, library, functions, platform);
}
build_mri(&output, ar, &libraries);
for library in libraries.keys() {
std::fs::remove_file(output.join(format!("lib{}.a", library))).unwrap();
}
}
fn build_library(output: &std::path::Path, dlltool: &str, library: &str, functions: &BTreeMap<String, lib::CallingConvention>, platform: &str) {
println!("{}", library);
// Note that we don't use set_extension as it confuses PathBuf when the library name includes a period.
let def_path = output.join(format!("{}.def", library));
let mut def = std::fs::File::create(&def_path).unwrap();
def.write_all(
format!(
r#"
LIBRARY {}
EXPORTS
"#,
library
)
.as_bytes(),
)
.unwrap();
for (function, calling_convention) in functions {
match calling_convention {
lib::CallingConvention::Stdcall(params) if platform.eq("i686_gnu") => def.write_all(format!("{}@{} @ 0\n", function, params).as_bytes()).unwrap(),
_ => def.write_all(format!("{} @ 0\n", function).as_bytes()).unwrap(),
}
}
drop(def);
let mut cmd = Command::new(dlltool);
cmd.current_dir(output);
if platform.eq("i686_gnu") {
cmd.arg("-k");
}
cmd.arg("-d");
cmd.arg(format!("{}.def", library));
cmd.arg("-l");
cmd.arg(format!("lib{}.a", library));
cmd.arg("-m");
cmd.arg(match platform {
"i686_gnu" => "i386",
"x86_64_gnu" | "x86_64_gnullvm" => "i386:x86-64",
"aarch64_gnullvm" => "arm64",
_ => unreachable!(),
});
if dlltool == "dlltool" {
// Work around https://sourceware.org/bugzilla/show_bug.cgi?id=29497
cmd.arg("-f");
cmd.arg(match platform {
"i686_gnu" => "--32",
"x86_64_gnu" => "--64",
_ => unreachable!(),
});
// Ensure consistency in the prefixes used by dlltool.
cmd.arg("-t");
cmd.arg(format!("{}_", library).replace('.', "_").replace('-', "_"));
}
cmd.output().unwrap();
if dlltool == "dlltool" {
// Work around lack of determinism in dlltool output, and at the same time remove
// unnecessary sections and symbols.
std::fs::rename(output.join(format!("lib{}.a", library)), output.join("tmp.a")).unwrap();
let mut cmd = Command::new("objcopy");
cmd.current_dir(output);
cmd.arg("--remove-section=.bss");
cmd.arg("--remove-section=.data");
cmd.arg("--strip-unneeded-symbol=fthunk");
cmd.arg("--strip-unneeded-symbol=hname");
cmd.arg("--strip-unneeded-symbol=.file");
cmd.arg("--strip-unneeded-symbol=.text");
cmd.arg("--strip-unneeded-symbol=.data");
cmd.arg("--strip-unneeded-symbol=.bss");
cmd.arg("--strip-unneeded-symbol=.idata$7");
cmd.arg("--strip-unneeded-symbol=.idata$5");
cmd.arg("--strip-unneeded-symbol=.idata$4");
cmd.arg("tmp.a");
cmd.arg(format!("lib{}.a", library));
cmd.output().unwrap();
std::fs::remove_file(output.join("tmp.a")).unwrap();
}
std::fs::remove_file(output.join(format!("{}.def", library))).unwrap();
}
fn build_mri(output: &std::path::Path, ar: &str, libraries: &BTreeMap<String, BTreeMap<String, lib::CallingConvention>>) {
let mri_path = output.join("unified.mri");
let mut mri = std::fs::File::create(&mri_path).unwrap();
println!("Generating {}", mri_path.to_string_lossy());
mri.write_all(b"CREATE libwindows.a\n").unwrap();
for library in libraries.keys() {
mri.write_all(format!("ADDLIB lib{}.a\n", library).as_bytes()).unwrap();
}
mri.write_all(b"SAVE\nEND\n").unwrap();
let mut cmd = Command::new(ar);
cmd.current_dir(output);
cmd.arg("-M");
cmd.stdin(std::fs::File::open(&mri_path).unwrap());
cmd.output().unwrap();
std::fs::remove_file(&mri_path).unwrap();
}