|
1 | 1 | const std = @import("std");
|
2 | 2 |
|
| 3 | +// Zig Version: 0.11.0-dev.3379+629f0d23b |
3 | 4 | pub fn build(b: *std.build.Builder) void {
|
4 | 5 | const target = b.standardTargetOptions(.{});
|
5 |
| - const optimize = b.standardReleaseOptions(); |
6 |
| - const want_lto = b.option(bool, "lto", "Want -fLTO"); |
7 |
| - |
8 |
| - const lib = b.addStaticLibrary("llama", null); |
9 |
| - lib.want_lto = want_lto; |
10 |
| - lib.setTarget(target); |
11 |
| - lib.setBuildMode(optimize); |
| 6 | + const optimize = b.standardOptimizeOption(.{}); |
| 7 | + const lib = b.addStaticLibrary(.{ |
| 8 | + .name = "llama", |
| 9 | + .target = target, |
| 10 | + .optimize = optimize, |
| 11 | + }); |
| 12 | + lib.linkLibC(); |
12 | 13 | lib.linkLibCpp();
|
13 | 14 | lib.addIncludePath(".");
|
14 |
| - lib.addIncludePath("examples"); |
| 15 | + lib.addIncludePath("./examples"); |
15 | 16 | lib.addCSourceFiles(&.{
|
16 | 17 | "ggml.c",
|
17 | 18 | }, &.{"-std=c11"});
|
18 | 19 | lib.addCSourceFiles(&.{
|
19 | 20 | "llama.cpp",
|
20 | 21 | }, &.{"-std=c++11"});
|
21 |
| - lib.install(); |
22 |
| - |
23 |
| - const build_args = .{ .b = b, .lib = lib, .target = target, .optimize = optimize, .want_lto = want_lto }; |
24 |
| - |
25 |
| - const exe = build_example("main", build_args); |
26 |
| - _ = build_example("quantize", build_args); |
27 |
| - _ = build_example("perplexity", build_args); |
28 |
| - _ = build_example("embedding", build_args); |
29 |
| - |
30 |
| - // create "zig build run" command for ./main |
31 |
| - |
32 |
| - const run_cmd = exe.run(); |
33 |
| - run_cmd.step.dependOn(b.getInstallStep()); |
34 |
| - if (b.args) |args| { |
35 |
| - run_cmd.addArgs(args); |
| 22 | + b.installArtifact(lib); |
| 23 | + |
| 24 | + const examples = .{ |
| 25 | + "main", |
| 26 | + "baby-llama", |
| 27 | + "embedding", |
| 28 | + // "metal", |
| 29 | + "perplexity", |
| 30 | + "quantize", |
| 31 | + "quantize-stats", |
| 32 | + "save-load-state", |
| 33 | + // "server", |
| 34 | + "simple", |
| 35 | + "train-text-from-scratch", |
| 36 | + }; |
| 37 | + |
| 38 | + inline for (examples) |example_name| { |
| 39 | + const exe = b.addExecutable(.{ |
| 40 | + .name = example_name, |
| 41 | + .target = target, |
| 42 | + .optimize = optimize, |
| 43 | + }); |
| 44 | + exe.addIncludePath("."); |
| 45 | + exe.addIncludePath("./examples"); |
| 46 | + exe.addCSourceFiles(&.{ |
| 47 | + std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{example_name, example_name}), |
| 48 | + "examples/common.cpp", |
| 49 | + }, &.{"-std=c++11"}); |
| 50 | + exe.linkLibrary(lib); |
| 51 | + b.installArtifact(exe); |
| 52 | + const run_cmd = b.addRunArtifact(exe); |
| 53 | + run_cmd.step.dependOn(b.getInstallStep()); |
| 54 | + if (b.args) |args| run_cmd.addArgs(args); |
| 55 | + const run_step = b.step("run_" ++ example_name, "Run the app"); |
| 56 | + run_step.dependOn(&run_cmd.step); |
36 | 57 | }
|
37 |
| - |
38 |
| - const run_step = b.step("run", "Run the app"); |
39 |
| - run_step.dependOn(&run_cmd.step); |
40 |
| -} |
41 |
| - |
42 |
| -fn build_example(comptime name: []const u8, args: anytype) *std.build.LibExeObjStep { |
43 |
| - const b = args.b; |
44 |
| - const lib = args.lib; |
45 |
| - const want_lto = args.want_lto; |
46 |
| - |
47 |
| - const exe = b.addExecutable(name, null); |
48 |
| - exe.want_lto = want_lto; |
49 |
| - lib.setTarget(args.target); |
50 |
| - lib.setBuildMode(args.optimize); |
51 |
| - exe.addIncludePath("."); |
52 |
| - exe.addIncludePath("examples"); |
53 |
| - exe.addCSourceFiles(&.{ |
54 |
| - std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{name, name}), |
55 |
| - "examples/common.cpp", |
56 |
| - }, &.{"-std=c++11"}); |
57 |
| - exe.linkLibrary(lib); |
58 |
| - exe.install(); |
59 |
| - |
60 |
| - return exe; |
61 | 58 | }
|
0 commit comments