Skip to content

Commit ed1c214

Browse files
authored
zig : add build.zig (ggml-org#773)
Co-authored-by: Locria Cyber <74560659+locriacyber@users.noreply.github.com>
1 parent 0c44427 commit ed1c214

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ compile_commands.json
3333
.venv
3434
__pycache__
3535
.swiftpm
36+
37+
zig-out/
38+
zig-cache/

build.zig

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)