|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +pub fn build(b: *std.Build) void { |
| 4 | + const target = b.standardTargetOptions(.{}); |
| 5 | + const optimize = b.standardOptimizeOption(.{}); |
| 6 | + |
| 7 | + const lib = b.addStaticLibrary(.{ |
| 8 | + .name = "llama", |
| 9 | + .target = target, |
| 10 | + .optimize = optimize, |
| 11 | + }); |
| 12 | + lib.linkLibCpp(); |
| 13 | + lib.addIncludePath("."); |
| 14 | + lib.addIncludePath("examples"); |
| 15 | + lib.addCSourceFiles(&.{ |
| 16 | + "ggml.c", |
| 17 | + }, &.{"-std=c11"}); |
| 18 | + lib.addCSourceFiles(&.{ |
| 19 | + "llama.cpp", |
| 20 | + "examples/common.cpp", |
| 21 | + }, &.{"-std=c++11"}); |
| 22 | + lib.install(); |
| 23 | + |
| 24 | + const build_args = .{ .b = b, .lib = lib, .target = target, .optimize = optimize }; |
| 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); |
| 36 | + } |
| 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 target = args.target; |
| 46 | + const optimize = args.optimize; |
| 47 | + |
| 48 | + const exe = b.addExecutable(.{ |
| 49 | + .name = name, |
| 50 | + .target = target, |
| 51 | + .optimize = optimize, |
| 52 | + }); |
| 53 | + exe.addIncludePath("."); |
| 54 | + exe.addIncludePath("examples"); |
| 55 | + exe.addCSourceFiles(&.{ |
| 56 | + std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{name, name}), |
| 57 | + }, &.{"-std=c++11"}); |
| 58 | + exe.linkLibrary(lib); |
| 59 | + exe.install(); |
| 60 | + |
| 61 | + return exe; |
| 62 | +} |
0 commit comments