2023-04-05 15:06:02 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2023-04-13 13:43:22 +00:00
|
|
|
pub fn build(b: *std.build.Builder) void {
|
2023-04-05 15:06:02 +00:00
|
|
|
const target = b.standardTargetOptions(.{});
|
2023-04-13 13:43:22 +00:00
|
|
|
const optimize = b.standardReleaseOptions();
|
2023-04-07 16:05:29 +00:00
|
|
|
const want_lto = b.option(bool, "lto", "Want -fLTO");
|
2023-04-05 15:06:02 +00:00
|
|
|
|
2023-04-13 13:43:22 +00:00
|
|
|
const lib = b.addStaticLibrary("llama", null);
|
2023-04-07 16:05:29 +00:00
|
|
|
lib.want_lto = want_lto;
|
2023-04-13 13:43:22 +00:00
|
|
|
lib.setTarget(target);
|
|
|
|
lib.setBuildMode(optimize);
|
2023-04-05 15:06:02 +00:00
|
|
|
lib.linkLibCpp();
|
|
|
|
lib.addIncludePath(".");
|
|
|
|
lib.addIncludePath("examples");
|
|
|
|
lib.addCSourceFiles(&.{
|
|
|
|
"ggml.c",
|
|
|
|
}, &.{"-std=c11"});
|
|
|
|
lib.addCSourceFiles(&.{
|
|
|
|
"llama.cpp",
|
|
|
|
}, &.{"-std=c++11"});
|
|
|
|
lib.install();
|
|
|
|
|
2023-04-07 16:05:29 +00:00
|
|
|
const build_args = .{ .b = b, .lib = lib, .target = target, .optimize = optimize, .want_lto = want_lto };
|
|
|
|
|
2023-04-05 15:06:02 +00:00
|
|
|
const exe = build_example("main", build_args);
|
|
|
|
_ = build_example("quantize", build_args);
|
|
|
|
_ = build_example("perplexity", build_args);
|
|
|
|
_ = build_example("embedding", build_args);
|
|
|
|
|
|
|
|
// create "zig build run" command for ./main
|
|
|
|
|
|
|
|
const run_cmd = exe.run();
|
|
|
|
run_cmd.step.dependOn(b.getInstallStep());
|
|
|
|
if (b.args) |args| {
|
|
|
|
run_cmd.addArgs(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
const run_step = b.step("run", "Run the app");
|
|
|
|
run_step.dependOn(&run_cmd.step);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_example(comptime name: []const u8, args: anytype) *std.build.LibExeObjStep {
|
|
|
|
const b = args.b;
|
|
|
|
const lib = args.lib;
|
2023-04-07 16:05:29 +00:00
|
|
|
const want_lto = args.want_lto;
|
2023-04-05 15:06:02 +00:00
|
|
|
|
2023-04-13 13:43:22 +00:00
|
|
|
const exe = b.addExecutable(name, null);
|
2023-04-07 16:05:29 +00:00
|
|
|
exe.want_lto = want_lto;
|
2023-04-13 13:43:22 +00:00
|
|
|
lib.setTarget(args.target);
|
|
|
|
lib.setBuildMode(args.optimize);
|
2023-04-05 15:06:02 +00:00
|
|
|
exe.addIncludePath(".");
|
|
|
|
exe.addIncludePath("examples");
|
|
|
|
exe.addCSourceFiles(&.{
|
|
|
|
std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{name, name}),
|
2023-04-07 16:05:29 +00:00
|
|
|
"examples/common.cpp",
|
2023-04-05 15:06:02 +00:00
|
|
|
}, &.{"-std=c++11"});
|
|
|
|
exe.linkLibrary(lib);
|
|
|
|
exe.install();
|
|
|
|
|
|
|
|
return exe;
|
|
|
|
}
|