|
| 1 | +// Compatible with Zig Version 0.11.0 |
1 | 2 | const std = @import("std"); |
2 | | -const commit_hash = @embedFile(".git/refs/heads/master"); |
| 3 | +const Compile = std.Build.Step.Compile; |
| 4 | +const ConfigHeader = std.Build.Step.ConfigHeader; |
| 5 | +const Mode = std.builtin.Mode; |
| 6 | +const CrossTarget = std.zig.CrossTarget; |
3 | 7 |
|
4 | | -// Zig Version: 0.11.0-dev.3986+e05c242cd |
5 | | -pub fn build(b: *std.build.Builder) void { |
6 | | - const target = b.standardTargetOptions(.{}); |
7 | | - const optimize = b.standardOptimizeOption(.{}); |
| 8 | +const Maker = struct { |
| 9 | + builder: *std.build.Builder, |
| 10 | + target: CrossTarget, |
| 11 | + optimize: Mode, |
| 12 | + config_header: *ConfigHeader, |
| 13 | + |
| 14 | + const cflags = .{"-std=c11"}; |
| 15 | + const cxxflags = .{"-std=c++11"}; |
| 16 | + |
| 17 | + fn init(builder: *std.build.Builder) Maker { |
| 18 | + const commit_hash = @embedFile(".git/refs/heads/master"); |
| 19 | + const config_header = builder.addConfigHeader( |
| 20 | + .{ .style = .blank, .include_path = "build-info.h" }, |
| 21 | + .{ |
| 22 | + .BUILD_NUMBER = 0, |
| 23 | + .BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline |
| 24 | + }, |
| 25 | + ); |
| 26 | + return Maker{ |
| 27 | + .builder = builder, |
| 28 | + .target = builder.standardTargetOptions(.{}), |
| 29 | + .optimize = builder.standardOptimizeOption(.{}), |
| 30 | + .config_header = config_header, |
| 31 | + }; |
| 32 | + } |
8 | 33 |
|
9 | | - const config_header = b.addConfigHeader( |
10 | | - .{ .style = .blank, .include_path = "build-info.h" }, |
11 | | - .{ |
12 | | - .BUILD_NUMBER = 0, |
13 | | - .BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline |
14 | | - }, |
15 | | - ); |
| 34 | + fn obj(m: *const Maker, name: []const u8, src: []const u8) *Compile { |
| 35 | + const o = m.builder.addObject(.{ .name = name, .target = m.target, .optimize = m.optimize }); |
| 36 | + if (std.mem.endsWith(u8, src, ".c")) { |
| 37 | + o.addCSourceFiles(&.{src}, &cflags); |
| 38 | + o.linkLibC(); |
| 39 | + } else { |
| 40 | + o.addCSourceFiles(&.{src}, &cxxflags); |
| 41 | + o.linkLibCpp(); |
| 42 | + } |
| 43 | + o.addIncludePath(.{ .path = "." }); |
| 44 | + o.addIncludePath(.{ .path = "./examples" }); |
| 45 | + return o; |
| 46 | + } |
| 47 | + |
| 48 | + fn exe(m: *const Maker, name: []const u8, src: []const u8, deps: []const *Compile) *Compile { |
| 49 | + const e = m.builder.addExecutable(.{ .name = name, .target = m.target, .optimize = m.optimize }); |
| 50 | + e.addIncludePath(.{ .path = "." }); |
| 51 | + e.addIncludePath(.{ .path = "./examples" }); |
| 52 | + e.addCSourceFiles(&.{src}, &cxxflags); |
| 53 | + for (deps) |d| e.addObject(d); |
| 54 | + e.linkLibC(); |
| 55 | + e.linkLibCpp(); |
| 56 | + e.addConfigHeader(m.config_header); |
| 57 | + m.builder.installArtifact(e); |
16 | 58 |
|
17 | | - const lib = b.addStaticLibrary(.{ |
18 | | - .name = "llama", |
19 | | - .target = target, |
20 | | - .optimize = optimize, |
21 | | - }); |
22 | | - lib.linkLibC(); |
23 | | - lib.linkLibCpp(); |
24 | | - lib.addIncludePath("."); |
25 | | - lib.addIncludePath("./examples"); |
26 | | - lib.addConfigHeader(config_header); |
27 | | - lib.addCSourceFiles(&.{"ggml.c"}, &.{"-std=c11"}); |
28 | | - lib.addCSourceFiles(&.{"llama.cpp"}, &.{"-std=c++11"}); |
29 | | - b.installArtifact(lib); |
| 59 | + // Currently a bug is preventing correct linking for optimized builds for Windows: |
| 60 | + // https://github.com/ziglang/zig/issues/15958 |
| 61 | + if (e.target.isWindows()) { |
| 62 | + e.want_lto = false; |
| 63 | + } |
| 64 | + return e; |
| 65 | + } |
| 66 | +}; |
30 | 67 |
|
31 | | - const examples = .{ |
32 | | - "main", |
33 | | - "baby-llama", |
34 | | - "embedding", |
35 | | - "metal", |
36 | | - "perplexity", |
37 | | - "quantize", |
38 | | - "quantize-stats", |
39 | | - "save-load-state", |
40 | | - "server", |
41 | | - "simple", |
42 | | - "train-text-from-scratch", |
43 | | - }; |
| 68 | +pub fn build(b: *std.build.Builder) void { |
| 69 | + const make = Maker.init(b); |
44 | 70 |
|
45 | | - inline for (examples) |example_name| { |
46 | | - const exe = b.addExecutable(.{ |
47 | | - .name = example_name, |
48 | | - .target = target, |
49 | | - .optimize = optimize, |
50 | | - }); |
51 | | - exe.addIncludePath("."); |
52 | | - exe.addIncludePath("./examples"); |
53 | | - exe.addConfigHeader(config_header); |
54 | | - exe.addCSourceFiles(&.{ |
55 | | - std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{ example_name, example_name }), |
56 | | - "examples/common.cpp", |
57 | | - }, &.{"-std=c++11"}); |
58 | | - exe.linkLibrary(lib); |
59 | | - b.installArtifact(exe); |
| 71 | + const ggml = make.obj("ggml", "ggml.c"); |
| 72 | + const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c"); |
| 73 | + const llama = make.obj("llama", "llama.cpp"); |
| 74 | + const common = make.obj("common", "examples/common.cpp"); |
| 75 | + const grammar_parser = make.obj("grammar-parser", "examples/grammar-parser.cpp"); |
60 | 76 |
|
61 | | - const run_cmd = b.addRunArtifact(exe); |
62 | | - run_cmd.step.dependOn(b.getInstallStep()); |
63 | | - if (b.args) |args| run_cmd.addArgs(args); |
| 77 | + _ = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, llama, common, grammar_parser }); |
| 78 | + _ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, llama }); |
| 79 | + _ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, llama, common }); |
| 80 | + _ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, llama, common }); |
| 81 | + _ = make.exe("train-text-from-scratch", "examples/train-text-from-scratch/train-text-from-scratch.cpp", &.{ ggml, ggml_alloc, llama }); |
64 | 82 |
|
65 | | - const run_step = b.step("run-" ++ example_name, "Run the app"); |
66 | | - run_step.dependOn(&run_cmd.step); |
| 83 | + const server = make.exe("server", "examples/server/server.cpp", &.{ ggml, ggml_alloc, llama, common, grammar_parser }); |
| 84 | + if (server.target.isWindows()) { |
| 85 | + server.linkSystemLibrary("ws2_32"); |
67 | 86 | } |
68 | 87 | } |
0 commit comments