Skip to content

Commit e31fb67

Browse files
committed
Update build.zig
1 parent 15499eb commit e31fb67

File tree

1 file changed

+93
-30
lines changed

1 file changed

+93
-30
lines changed

build.zig

Lines changed: 93 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
// Compatible with Zig Version 0.11.0
1+
// Compatible with Zig Version 0.12.0-dev.xxxx
22
const std = @import("std");
33
const ArrayList = std.ArrayList;
44
const Compile = std.Build.Step.Compile;
55
const ConfigHeader = std.Build.Step.ConfigHeader;
6-
const Mode = std.builtin.Mode;
7-
const CrossTarget = std.zig.CrossTarget;
6+
const Mode = std.builtin.OptimizeMode;
7+
const Target = std.Build.ResolvedTarget;
88

99
const Maker = struct {
10-
builder: *std.build.Builder,
11-
target: CrossTarget,
10+
builder: *std.Build,
11+
target: Target,
1212
optimize: Mode,
1313
enable_lto: bool,
1414

1515
include_dirs: ArrayList([]const u8),
1616
cflags: ArrayList([]const u8),
1717
cxxflags: ArrayList([]const u8),
18-
objs: ArrayList(*Compile),
1918

2019
fn addInclude(m: *Maker, dir: []const u8) !void {
2120
try m.include_dirs.append(dir);
@@ -34,10 +33,10 @@ const Maker = struct {
3433
try m.addCxxFlag(flag);
3534
}
3635

37-
fn init(builder: *std.build.Builder) !Maker {
36+
fn init(builder: *std.Build) !Maker {
3837
const target = builder.standardTargetOptions(.{});
3938
const zig_version = @import("builtin").zig_version_string;
40-
const commit_hash = try std.ChildProcess.exec(
39+
const commit_hash = try std.ChildProcess.run(
4140
.{ .allocator = builder.allocator, .argv = &.{ "git", "rev-parse", "HEAD" } },
4241
);
4342
try std.fs.cwd().writeFile("common/build-info.cpp", builder.fmt(
@@ -46,7 +45,8 @@ const Maker = struct {
4645
\\char const *LLAMA_COMPILER = "Zig {s}";
4746
\\char const *LLAMA_BUILD_TARGET = "{s}";
4847
\\
49-
, .{ 0, commit_hash.stdout[0 .. commit_hash.stdout.len - 1], zig_version, try target.allocDescription(builder.allocator) }));
48+
, .{ 0, commit_hash.stdout[0 .. commit_hash.stdout.len - 1], zig_version, try target.query.zigTriple(builder.allocator) }));
49+
5050
var m = Maker{
5151
.builder = builder,
5252
.target = target,
@@ -55,27 +55,33 @@ const Maker = struct {
5555
.include_dirs = ArrayList([]const u8).init(builder.allocator),
5656
.cflags = ArrayList([]const u8).init(builder.allocator),
5757
.cxxflags = ArrayList([]const u8).init(builder.allocator),
58-
.objs = ArrayList(*Compile).init(builder.allocator),
5958
};
6059

6160
try m.addCFlag("-std=c11");
6261
try m.addCxxFlag("-std=c++11");
62+
63+
if (m.target.result.abi == .gnu) {
64+
try m.addFlag("-D_GNU_SOURCE");
65+
}
66+
if (m.target.result.os.tag == .macos) {
67+
try m.addFlag("-D_DARWIN_C_SOURCE");
68+
}
69+
try m.addFlag("-D_XOPEN_SOURCE=600");
70+
6371
try m.addProjectInclude(&.{});
6472
try m.addProjectInclude(&.{"common"});
6573
return m;
6674
}
6775

6876
fn obj(m: *const Maker, name: []const u8, src: []const u8) *Compile {
6977
const o = m.builder.addObject(.{ .name = name, .target = m.target, .optimize = m.optimize });
70-
if (o.target.getAbi() != .msvc)
71-
o.defineCMacro("_GNU_SOURCE", null);
7278

73-
if (std.mem.endsWith(u8, src, ".c")) {
74-
o.addCSourceFiles(&.{src}, m.cflags.items);
79+
if (std.mem.endsWith(u8, src, ".c") or std.mem.endsWith(u8, src, ".m")) {
80+
o.addCSourceFiles(.{ .files = &.{src}, .flags = m.cflags.items });
7581
o.linkLibC();
7682
} else {
77-
o.addCSourceFiles(&.{src}, m.cxxflags.items);
78-
if (o.target.getAbi() == .msvc) {
83+
o.addCSourceFiles(.{ .files = &.{src}, .flags = m.cxxflags.items });
84+
if (m.target.result.abi == .msvc) {
7985
o.linkLibC(); // need winsdk + crt
8086
} else {
8187
// linkLibCpp already add (libc++ + libunwind + libc)
@@ -89,28 +95,63 @@ const Maker = struct {
8995

9096
fn exe(m: *const Maker, name: []const u8, src: []const u8, deps: []const *Compile) *Compile {
9197
const e = m.builder.addExecutable(.{ .name = name, .target = m.target, .optimize = m.optimize });
92-
e.addCSourceFiles(&.{src}, m.cxxflags.items);
98+
e.addCSourceFiles(.{ .files = &.{src}, .flags = m.cxxflags.items });
9399
for (deps) |d| e.addObject(d);
94-
for (m.objs.items) |o| e.addObject(o);
95100
for (m.include_dirs.items) |i| e.addIncludePath(.{ .path = i });
96101

97102
// https://github.com/ziglang/zig/issues/15448
98-
if (e.target.getAbi() == .msvc) {
103+
if (m.target.result.abi == .msvc) {
99104
e.linkLibC(); // need winsdk + crt
100105
} else {
101106
// linkLibCpp already add (libc++ + libunwind + libc)
102107
e.linkLibCpp();
103108
}
104109
m.builder.installArtifact(e);
105110
e.want_lto = m.enable_lto;
111+
112+
const run = m.builder.addRunArtifact(e);
113+
if (m.builder.args) |args| {
114+
run.addArgs(args);
115+
}
116+
const step = m.builder.step(name, std.fmt.allocPrint(m.builder.allocator, "Run the {s} example", .{name}) catch @panic("OOM"));
117+
step.dependOn(&run.step);
118+
106119
return e;
107120
}
108121
};
109122

110-
pub fn build(b: *std.build.Builder) !void {
123+
pub fn build(b: *std.Build) !void {
111124
var make = try Maker.init(b);
112125
make.enable_lto = b.option(bool, "lto", "Enable LTO optimization, (default: false)") orelse false;
113126

127+
// Options
128+
const llama_vulkan = b.option(bool, "llama-vulkan", "Enable Vulkan backend for Llama, (default: false)") orelse false;
129+
const llama_metal = b.option(bool, "llama-metal", "Enable Metal backend for Llama, (default: false, true for macos)") orelse (make.target.result.os.tag == .macos);
130+
const llama_no_accelerate = b.option(bool, "llama-no-accelerate", "Disable Accelerate framework for Llama, (default: false)") orelse false;
131+
const llama_accelerate = !llama_no_accelerate and make.target.result.os.tag == .macos;
132+
133+
// Flags
134+
if (llama_accelerate) {
135+
try make.addFlag("-DGGML_USE_ACCELERATE");
136+
try make.addFlag("-DACCELERATE_USE_LAPACK");
137+
try make.addFlag("-DACCELERATE_LAPACK_ILP64");
138+
}
139+
140+
// Objects
141+
var extras = ArrayList(*Compile).init(b.allocator);
142+
143+
if (llama_vulkan) {
144+
try make.addFlag("-DGGML_USE_VULKAN");
145+
const ggml_vulkan = make.obj("ggml-vulkan", "ggml-vulkan.cpp");
146+
try extras.append(ggml_vulkan);
147+
}
148+
149+
if (llama_metal) {
150+
try make.addFlag("-DGGML_USE_METAL");
151+
const ggml_metal = make.obj("ggml-metal", "ggml-metal.m");
152+
try extras.append(ggml_metal);
153+
}
154+
114155
const ggml = make.obj("ggml", "ggml.c");
115156
const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c");
116157
const ggml_backend = make.obj("ggml-backend", "ggml-backend.c");
@@ -121,19 +162,41 @@ pub fn build(b: *std.build.Builder) !void {
121162
const console = make.obj("console", "common/console.cpp");
122163
const sampling = make.obj("sampling", "common/sampling.cpp");
123164
const grammar_parser = make.obj("grammar-parser", "common/grammar-parser.cpp");
124-
const train = make.obj("train", "common/train.cpp");
125165
const clip = make.obj("clip", "examples/llava/clip.cpp");
166+
const train = make.obj("train", "common/train.cpp");
126167
const llava = make.obj("llava", "examples/llava/llava.cpp");
127168

128-
_ = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, sampling, console, grammar_parser });
129-
_ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo });
130-
_ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo });
131-
_ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo });
132-
_ = make.exe("finetune", "examples/finetune/finetune.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train });
133-
_ = make.exe("train-text-from-scratch", "examples/train-text-from-scratch/train-text-from-scratch.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train });
134-
135-
const server = make.exe("server", "examples/server/server.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, sampling, grammar_parser, clip, llava });
136-
if (server.target.isWindows()) {
169+
// Executables
170+
const server = make.exe("server", "examples/server/server.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, sampling, console, grammar_parser, clip, llava });
171+
if (make.target.result.os.tag == .windows) {
137172
server.linkSystemLibrary("ws2_32");
138173
}
174+
175+
const exes = [_]*Compile{
176+
make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, sampling, console, grammar_parser, clip }),
177+
make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo }),
178+
make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo }),
179+
make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo }),
180+
make.exe("finetune", "examples/finetune/finetune.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train }),
181+
make.exe("train-text-from-scratch", "examples/train-text-from-scratch/train-text-from-scratch.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train }),
182+
server,
183+
};
184+
185+
for (exes) |e| {
186+
for (extras.items) |o| e.addObject(o);
187+
188+
if (llama_vulkan) {
189+
e.linkSystemLibrary("vulkan");
190+
}
191+
192+
if (llama_metal) {
193+
e.linkFramework("Foundation");
194+
e.linkFramework("Metal");
195+
e.linkFramework("MetalKit");
196+
}
197+
198+
if (llama_accelerate) {
199+
e.linkFramework("Accelerate");
200+
}
201+
}
139202
}

0 commit comments

Comments
 (0)