-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
109 lines (94 loc) · 3.41 KB
/
build.zig
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
const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;
const Allocator = mem.Allocator;
const Build = std.Build;
const fmt = std.fmt;
const fs = std.fs;
const heap = std.heap;
const http = std.http;
const math = std.math;
const mem = std.mem;
pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const opts = b.addOptions();
const embed_nets = b.option(bool, "embed-nets", "Whether or not to embed the NNUE file in the executable (default: true)") orelse true;
opts.addOption(bool, "embed-nets", embed_nets);
const small_net = b.option([]const u8, "small-net", "Name of the small NNUE (default: \"nn-37f18f62d772.nnue\")") orelse "nn-37f18f62d772.nnue";
opts.addOption([]const u8, "small-net", small_net);
const big_net = b.option([]const u8, "big-net", "Name of the big NNUE (default: \"nn-1111cefa1111.nnue\")") orelse "nn-1111cefa1111.nnue";
opts.addOption([]const u8, "big-net", big_net);
try downloadNNUE(b, small_net);
try downloadNNUE(b, big_net);
const stockfish_dep = b.dependency("Stockfish", .{});
const stockfish_src_path = stockfish_dep.path("src/");
const exe = b.addExecutable(.{
.name = "stockfish",
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("run", "Build and run stockfish");
run_step.dependOn(&run_cmd.step);
exe.linkLibC();
exe.linkLibCpp();
if (optimize != .Debug) {
exe.root_module.pic = true;
exe.pie = true;
exe.root_module.omit_frame_pointer = true;
exe.root_module.strip = true;
exe.want_lto = switch (builtin.os.tag) {
.macos => false,
else => true,
};
}
exe.addCSourceFiles(.{
.root = stockfish_src_path,
.files = &.{
"movegen.cpp",
"engine.cpp",
"nnue/features/half_ka_v2_hm.cpp",
"nnue/nnue_misc.cpp",
"nnue/network.cpp",
"memory.cpp",
"uci.cpp",
"search.cpp",
"ucioption.cpp",
"tt.cpp",
"bitboard.cpp",
"score.cpp",
"main.cpp",
"thread.cpp",
"benchmark.cpp",
"position.cpp",
"movepick.cpp",
"timeman.cpp",
"misc.cpp",
"tune.cpp",
"syzygy/tbprobe.cpp",
"evaluate.cpp",
},
.flags = &.{
if (embed_nets) "" else "-DNNUE_EMBEDDING_OFF=1",
},
});
}
/// The first time we run "zig build", we need to download the necessary nnue files
fn downloadNNUE(b: *Build, nnue_file: []const u8) !void {
_ = fs.cwd().statFile(nnue_file) catch |err| {
switch (err) {
error.FileNotFound => {
const url = try fmt.allocPrint(b.allocator, "https://data.stockfishchess.org/nn/{s}", .{nnue_file});
std.debug.print("No nnue file found, downloading {s}\n\n", .{url});
var child = std.process.Child.init(&.{ "curl", "-o", nnue_file, url }, b.allocator);
try child.spawn();
_ = try child.wait();
},
else => return err,
}
};
}