Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/jcalabro/stockfish/actions/workflows/zig-build.yml/badge.svg)](https://github.com/jcalabro/stockfish/actions/workflows/zig-build.yml)

Zig build for [Stockfish 17.1](https://github.com/official-stockfish/Stockfish/tree/sf_17.1). Requires zig 0.14.0.
Zig build for [Stockfish 17.1](https://github.com/official-stockfish/Stockfish/tree/sf_17.1). Requires zig 0.15.1, 0.15.2 or 0.16.*. Was tested with 0.16.0-dev.2261+d6b3dd25a.

Usage

Expand Down
47 changes: 30 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const heap = std.heap;
const http = std.http;
const math = std.math;
const mem = std.mem;
const Io = std.Io;

pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
Expand All @@ -31,10 +32,15 @@ pub fn build(b: *Build) !void {
const stockfish_dep = b.dependency("Stockfish", .{});
const stockfish_src_path = stockfish_dep.path("src/");

const exe = b.addExecutable(.{
.name = "stockfish",
const module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libcpp = true,
});
const exe = b.addExecutable(.{
.name = "stockfish",
.root_module = module,
});
b.installArtifact(exe);

Expand All @@ -45,21 +51,18 @@ pub fn build(b: *Build) !void {
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.lto = switch (builtin.os.tag) {
.macos => .none,
else => .full,
};
}

exe.addCSourceFiles(.{
module.addCSourceFiles(.{
.root = stockfish_src_path,
.files = &.{
"benchmark.cpp",
Expand Down Expand Up @@ -94,17 +97,27 @@ pub fn build(b: *Build) !void {

/// 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});
const result = if (@hasDecl(fs, "Dir") and @hasDecl(fs.Dir, "statFile"))
fs.cwd().statFile(nnue_file)
else
Io.Dir.cwd().statFile(b.graph.io, nnue_file, .{});

_ = result 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});

if (@hasDecl(std.process, "spawn")) {
var child = try std.process.spawn(b.graph.io, .{
.argv = &.{ "curl", "-o", nnue_file, url },
});
std.debug.assert((try child.wait(b.graph.io)).exited == 0);
} else {
var child = std.process.Child.init(&.{ "curl", "-o", nnue_file, url }, b.allocator);
try child.spawn();
_ = try child.wait();
},
else => return err,
}
}
},
else => return err,
};
}
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.name = .stockfish,
.fingerprint = 0x471e85fceadc709,
.version = "17.0.0",
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.1",
.dependencies = .{
.Stockfish = .{
.url = "https://github.com/official-stockfish/Stockfish/archive/sf_17.1.tar.gz",
Expand Down
Loading