forked from lichess-org/fishnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
305 lines (265 loc) · 8.96 KB
/
build.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std::{env, fs, fs::File, io, path::Path, process::Command};
use glob::glob;
const EVAL_FILE: &str = "nn-13406b1dcbe0.nnue";
fn has_target_feature(feature: &str) -> bool {
env::var("CARGO_CFG_TARGET_FEATURE")
.unwrap()
.split(',')
.any(|f| f == feature)
}
macro_rules! has_builder_feature {
($feature:tt) => {{
#[cfg(target_arch = "x86_64")]
{
is_x86_feature_detected!($feature)
}
#[cfg(not(target_arch = "x86_64"))]
{
false
}
}};
}
#[cfg(target_arch = "x86_64")]
fn cross_compiling() -> bool {
env::var("CARGO_CFG_TARGET_ARCH").unwrap() != "x86_64"
}
#[cfg(target_arch = "aarch64")]
fn cross_compiling() -> bool {
env::var("CARGO_CFG_TARGET_ARCH").unwrap() != "aarch64"
}
struct Target {
arch: &'static str,
pgo: bool,
}
impl Target {
fn build(&self, src_dir: &'static str, name: &'static str) {
let release = env::var("PROFILE").unwrap() == "release";
let windows = env::var("CARGO_CFG_TARGET_FAMILY").unwrap() == "windows";
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let pgo = release && !cross_compiling() && (self.pgo || env::var("SDE_PATH").is_ok());
let exe = format!(
"{}-{}{}",
name,
self.arch,
if windows { ".exe" } else { "" }
);
if release && !pgo {
println!(
"cargo:warning=Building {} without profile-guided optimization",
exe
);
}
let make = if target_os == "freebsd" {
"gmake"
} else {
"make"
};
assert!(
Command::new(make)
.arg("--version")
.status()
.unwrap_or_else(|err| panic!(
"{}. Is `{}` installed?\n\
* Debian: sudo apt install build-essential\n\
* Arch: sudo pacman -S base-devel\n",
err, make
))
.success(),
"make --version"
);
assert!(
Command::new(make)
.current_dir(src_dir)
.env("MAKEFLAGS", env::var("CARGO_MAKEFLAGS").unwrap())
.env(
"CXXFLAGS",
format!(
"{} -DNNUE_EMBEDDING_OFF",
env::var("CXXFLAGS").unwrap_or_default()
)
)
.arg("-B")
.args(env::var("CXX").ok().map(|cxx| format!("CXX={}", cxx)))
.arg(format!(
"COMP={}",
if windows {
"mingw"
} else if target_os == "linux" {
"gcc"
} else {
"clang"
}
))
.arg(format!("ARCH={}", self.arch))
.arg(format!("EXE={}", exe))
.arg(if pgo { "profile-build" } else { "build" })
.status()
.unwrap()
.success(),
"make build"
);
assert!(
Command::new(make)
.current_dir(src_dir)
.env("MAKEFLAGS", env::var("CARGO_MAKEFLAGS").unwrap())
.arg(format!("EXE={}", exe))
.arg("strip")
.status()
.unwrap()
.success(),
"make strip"
);
compress(src_dir, &exe);
assert!(
Command::new(make)
.current_dir(src_dir)
.env("MAKEFLAGS", env::var("CARGO_MAKEFLAGS").unwrap())
.arg("clean")
.status()
.unwrap()
.success(),
"make clean"
);
println!(
"cargo:rustc-cfg={}",
exe.replace(|ch| ch == '.' || ch == '-', "_")
);
}
fn build_official(&self) {
self.build("Stockfish/src", "stockfish");
}
fn build_fairy(&self) {
self.build("Fairy-Stockfish/src", "fairy-stockfish");
}
fn build_both(&self) {
self.build_official();
self.build_fairy();
}
}
fn stockfish_build() {
// Note: The target arch of the build script is the architecture of the
// builder and decides if pgo is possible. It is not necessarily the same
// as CARGO_CFG_TARGET_ARCH, the target arch of the fishnet binary.
//
// Can skip building more broadly compatible Stockfish binaries and return
// early when building with something like -C target-cpu=native.
match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86_64" => {
Target {
arch: "x86-64-vnni512",
pgo: has_builder_feature!("avx512dq")
&& has_builder_feature!("avx512vl")
&& has_builder_feature!("avx512vnni"),
}
.build_both();
if has_target_feature("avx512dq")
&& has_target_feature("avx512vl")
&& has_target_feature("avx512vnni")
{
return;
}
Target {
arch: "x86-64-avx512",
pgo: has_builder_feature!("avx512f") && has_builder_feature!("avx512bw"),
}
.build_both();
if has_target_feature("avx512f") && has_target_feature("avx512bw") {
return;
}
Target {
arch: "x86-64-bmi2",
pgo: has_builder_feature!("bmi2"),
}
.build_both();
if has_target_feature("bmi2") {
// Fast bmi2 can not be detected at compile time.
}
Target {
arch: "x86-64-avx2",
pgo: has_builder_feature!("avx2"),
}
.build_both();
if has_target_feature("avx2") {
return;
}
Target {
arch: "x86-64-sse41-popcnt",
pgo: has_builder_feature!("sse4.1") && has_builder_feature!("popcnt"),
}
.build_both();
if has_target_feature("sse4.1") && has_target_feature("popcnt") {
return;
}
Target {
arch: "x86-64",
pgo: cfg!(target_arch = "x86_64"),
}
.build_both();
}
"aarch64" => {
if env::var("CARGO_CFG_TARGET_OS").unwrap() == "macos" {
Target {
arch: "apple-silicon",
pgo: cfg!(target_arch = "aarch64"),
}
.build_both();
} else {
Target {
arch: "armv8",
pgo: cfg!(target_arch = "aarch64"),
}
.build_both();
}
}
target_arch => {
unimplemented!("Stockfish build for {} not supported", target_arch);
}
}
}
fn compress(dir: &str, file: &str) {
let compressed =
File::create(Path::new(&env::var("OUT_DIR").unwrap()).join(&format!("{}.xz", file)))
.unwrap();
let mut encoder = xz2::write::XzEncoder::new(compressed, 6);
let uncompressed_path = Path::new(dir).join(file);
let mut uncompressed = File::open(&uncompressed_path).unwrap();
io::copy(&mut uncompressed, &mut encoder).unwrap();
encoder.finish().unwrap();
fs::remove_file(uncompressed_path).unwrap();
}
fn hooks() {
println!("cargo:rerun-if-changed=Cargo.lock");
println!("cargo:rerun-if-env-changed=CXX");
println!("cargo:rerun-if-env-changed=CXXFLAGS");
println!("cargo:rerun-if-env-changed=SDE_PATH");
println!("cargo:rustc-env=EVAL_FILE={}", EVAL_FILE);
println!("cargo:rerun-if-changed=Stockfish/src/Makefile");
for entry in glob("Stockfish/src/**/*.cpp").unwrap() {
println!("cargo:rerun-if-changed={}", entry.unwrap().display());
}
for entry in glob("Stockfish/src/**/*.h").unwrap() {
println!("cargo:rerun-if-changed={}", entry.unwrap().display());
}
println!("cargo:rerun-if-changed=Fairy-Stockfish/src/Makefile");
for entry in glob("Fairy-Stockfish/src/**/*.cpp").unwrap() {
println!("cargo:rerun-if-changed={}", entry.unwrap().display());
}
for entry in glob("Fairy-Stockfish/src/**/*.h").unwrap() {
println!("cargo:rerun-if-changed={}", entry.unwrap().display());
}
}
fn main() {
hooks();
stockfish_build();
compress("Stockfish/src", EVAL_FILE);
auditable_build::collect_dependency_list();
// Resource compilation may fail when toolchain does not match target,
// e.g. windows-msvc toolchain with windows-gnu target.
#[cfg(target_family = "windows")]
winres::WindowsResource::new()
.set_icon("favicon.ico")
.compile()
.unwrap_or_else(|err| {
println!("cargo:warning=Resource compiler not invoked: {}", err);
});
}