aboutsummaryrefslogtreecommitdiff
path: root/build.zig
blob: 2287d2a2c442b151b3fe621e4ecb46f34607a037 (plain)
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
const std = @import("std");
const commit_hash = @embedFile(".git/refs/heads/master");

// Zig Version: 0.11.0-dev.3986+e05c242cd
pub fn build(b: *std.build.Builder) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const config_header = b.addConfigHeader(
        .{ .style = .blank, .include_path = "build-info.h" },
        .{
            .BUILD_NUMBER = 0,
            .BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline
        },
    );

    const lib = b.addStaticLibrary(.{
        .name = "llama",
        .target = target,
        .optimize = optimize,
    });
    lib.linkLibC();
    lib.linkLibCpp();
    lib.addIncludePath(".");
    lib.addIncludePath("./examples");
    lib.addConfigHeader(config_header);
    lib.addCSourceFiles(&.{"ggml.c"}, &.{"-std=c11"});
    lib.addCSourceFiles(&.{"llama.cpp"}, &.{"-std=c++11"});
    b.installArtifact(lib);

    const examples = .{
        "main",
        "baby-llama",
        "embedding",
        "metal",
        "perplexity",
        "quantize",
        "quantize-stats",
        "save-load-state",
        "server",
        "simple",
        "train-text-from-scratch",
    };

    inline for (examples) |example_name| {
        const exe = b.addExecutable(.{
            .name = example_name,
            .target = target,
            .optimize = optimize,
        });
        exe.addIncludePath(".");
        exe.addIncludePath("./examples");
        exe.addConfigHeader(config_header);
        exe.addCSourceFiles(&.{
            std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{ example_name, example_name }),
            "examples/common.cpp",
        }, &.{"-std=c++11"});
        exe.linkLibrary(lib);
        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-" ++ example_name, "Run the app");
        run_step.dependOn(&run_cmd.step);
    }
}