-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
57 lines (44 loc) · 1.7 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
use std::{
env,
fs::File,
io::{BufWriter, Write},
path::{Path, PathBuf},
};
mod attacks;
mod magics;
mod maps;
fn main() {
generate_model_env();
generate_attack_maps();
println!("cargo:rerun-if-env-changed=EVALFILE");
println!("cargo:rerun-if-changed=networks/model.nnue");
}
fn generate_model_env() {
let mut path = env::var("EVALFILE").map(PathBuf::from).unwrap_or_else(|_| Path::new("networks").join("model.nnue"));
if path.is_relative() {
path = Path::new(env!("CARGO_MANIFEST_DIR")).join(path);
}
println!("cargo:rustc-env=MODEL={}", path.display());
}
fn generate_attack_maps() {
let dir = env::var("OUT_DIR").unwrap();
let path = Path::new(&dir).join("lookup.rs");
let out = File::create(path).unwrap();
write(BufWriter::new(out)).unwrap();
}
fn write(mut buf: BufWriter<File>) -> Result<(), std::io::Error> {
macro_rules! write_map {
($name:tt, $type:tt, $items:expr) => {
writeln!(buf, "static {}: [{}; {}] = {:?};", $name, $type, $items.len(), $items)?;
};
}
write_map!("KING_MAP", "u64", maps::generate_king_map());
write_map!("KNIGHT_MAP", "u64", maps::generate_knight_map());
write_map!("WHITE_PAWN_MAP", "u64", maps::generate_white_pawn_map());
write_map!("BLACK_PAWN_MAP", "u64", maps::generate_black_pawn_map());
write_map!("ROOK_MAP", "u64", maps::generate_rook_map());
write_map!("BISHOP_MAP", "u64", maps::generate_bishop_map());
write_map!("ROOK_MAGICS", "MagicEntry", magics::ROOK_MAGICS);
write_map!("BISHOP_MAGICS", "MagicEntry", magics::BISHOP_MAGICS);
writeln!(buf, "struct MagicEntry {{ pub mask: u64, pub magic: u64, pub shift: u32, pub offset: u32 }}")
}