Skip to content

Commit

Permalink
build from a tar
Browse files Browse the repository at this point in the history
  • Loading branch information
jcalabro committed Dec 11, 2024
1 parent 6eb6503 commit eb9fe5f
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/zig-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Continuous Integration

on:
push:
branches: ['*']

pull_request:
branches: ['*']

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up LLVM and Clang
run: |
sudo apt-get update
sudo apt-get install -y lld llvm llvm-dev clang
- name: Set up Zig
uses: mlugg/setup-zig@v1

- name: Run `build`
run: zig build
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Files from build
**/*.o
**/*.s
src/.depend

# Built binary
src/stockfish*
src/-lstdc++.res

# Neural network for the NNUE evaluation
**/*.nnue

# Files generated by the instrumented tests
tsan.supp
__pycache__/
tests/syzygy
tests/bench_tmp.epd

.zig-cache/
zig-out/
111 changes: 111 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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 a nnue file from the
/// internet. We search for the correct file to download in the macro
/// #define EvalFileDefaultName in evaluate.h.
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,
}
};
}
17 changes: 17 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.{
.name = "Stockfish",
.version = "17.0.0",
.minimum_zig_version = "0.13.0",
.dependencies = .{
.Stockfish = .{
.url = "https://github.com/official-stockfish/Stockfish/archive/sf_17.tar.gz",
.hash = "1220d8a16d771e844df720de6eb3b2a816411eb21372d8c2ae001f82af5715f55cfc",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"LICENSE",
"README.md",
},
}

0 comments on commit eb9fe5f

Please sign in to comment.