const std = @import("std");
const builtin = std.builtin;
const zbs = std.build;

pub fn build(b: *zbs.Builder) void {
    const target = b.standardTargetOptions(.{});
    const mode = b.standardReleaseOptions();

    const strip = b.option(bool, "strip", "Omit debug information") orelse false;

    const cmph = b.addStaticLibrary("cmph", null);
    cmph.setTarget(target);
    cmph.setBuildMode(mode);
    cmph.linkLibC();
    cmph.addCSourceFiles(&.{
        "deps/cmph/src/bdz.c",
        "deps/cmph/src/bdz_ph.c",
        "deps/cmph/src/bmz8.c",
        "deps/cmph/src/bmz.c",
        "deps/cmph/src/brz.c",
        "deps/cmph/src/buffer_entry.c",
        "deps/cmph/src/buffer_manager.c",
        "deps/cmph/src/chd.c",
        "deps/cmph/src/chd_ph.c",
        "deps/cmph/src/chm.c",
        "deps/cmph/src/cmph.c",
        "deps/cmph/src/cmph_structs.c",
        "deps/cmph/src/compressed_rank.c",
        "deps/cmph/src/compressed_seq.c",
        "deps/cmph/src/fch_buckets.c",
        "deps/cmph/src/fch.c",
        "deps/cmph/src/graph.c",
        "deps/cmph/src/hash.c",
        "deps/cmph/src/jenkins_hash.c",
        "deps/cmph/src/linear_string_map.c",
        "deps/cmph/src/miller_rabin.c",
        "deps/cmph/src/select.c",
        "deps/cmph/src/vqueue.c",
    }, &.{
        "-W",
        "-Wno-unused-function",
        //"-DDEBUG",
    });
    cmph.strip = strip;
    cmph.omit_frame_pointer = true;
    cmph.addIncludeDir("deps/cmph/src");
    cmph.addIncludeDir("include/deps/cmph");

    const bdz = b.addStaticLibrary("bdz", null);
    bdz.setTarget(target);
    bdz.setBuildMode(mode);
    bdz.linkLibC();
    bdz.addCSourceFiles(&.{
        "deps/bdz_read.c",
        "deps/cmph/src/hash.c",
        "deps/cmph/src/jenkins_hash.c",
    }, &.{
        "-W",
        "-Wno-unused-function",
        //"-DDEBUG",
    });
    bdz.omit_frame_pointer = true;
    bdz.addIncludeDir("deps/cmph/src");
    bdz.addIncludeDir("include/deps/cmph");

    {
        const exe = b.addExecutable("turbonss-unix2db", "src/turbonss-unix2db.zig");
        exe.strip = strip;
        exe.setTarget(target);
        exe.setBuildMode(mode);
        addCmphDeps(exe, cmph);
        exe.install();
    }

    {
        const exe = b.addExecutable("turbonss-analyze", "src/turbonss-analyze.zig");
        exe.strip = strip;
        exe.setTarget(target);
        exe.setBuildMode(mode);
        exe.install();
    }

    {
        const exe = b.addExecutable("turbonss-makecorpus", "src/turbonss-makecorpus.zig");
        exe.strip = strip;
        exe.setTarget(target);
        exe.setBuildMode(mode);
        exe.install();
    }

    {
        const exe = b.addExecutable("turbonss-getent", "src/turbonss-getent.zig");
        exe.strip = strip;
        exe.linkLibC();
        exe.linkLibrary(bdz);
        exe.addIncludeDir("deps/cmph/src");
        exe.setTarget(target);
        exe.setBuildMode(mode);
        exe.install();
    }

    {
        const so = b.addSharedLibrary("nss_turbo", "src/libnss.zig", .{
            .versioned = builtin.Version{
                .major = 2,
                .minor = 0,
                .patch = 0,
            },
        });
        so.strip = strip;
        so.linkLibC();
        so.linkLibrary(bdz);
        so.addIncludeDir("deps/cmph/src");
        so.setTarget(target);
        so.setBuildMode(mode);
        so.install();
    }

    {
        const src_test = b.addTest("src/test_all.zig");
        addCmphDeps(src_test, cmph);
        const test_step = b.step("test", "Run the tests");
        test_step.dependOn(&src_test.step);
    }
}

fn addCmphDeps(exe: *zbs.LibExeObjStep, cmph: *zbs.LibExeObjStep) void {
    exe.linkLibC();
    exe.linkLibrary(cmph);
    exe.addIncludeDir("deps/cmph/src");
}