Skip to content

Commit 6ddeefa

Browse files
authored
[Zig] Fixing Zig build and improvements (#2554)
* Fix zig after console.o was split * Better include and flag management * Change LTO to option
1 parent 8dae7ce commit 6ddeefa

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,17 @@ In order to build llama.cpp you have three different options.
238238
cmake --build . --config Release
239239
```
240240

241-
- Using `Zig`:
241+
- Using `Zig` (version 0.11 or later):
242+
243+
Building for optimization levels and CPU features can be accomplished using standard build arguments, for example AVX2, FMA, F16C,
244+
it's also possible to cross compile for other operating systems and architectures:
242245
243246
```bash
244-
zig build -Doptimize=ReleaseFast
247+
zig build -Doptimize=ReleaseFast -Dtarget=x86_64-windows-gnu -Dcpu=x86_64+avx2+fma+f16c
245248
```
246249
250+
The `zig targets` command will give you valid options to use.
251+
247252
- Using `gmake` (FreeBSD):
248253
249254
1. Install and activate [DRM in FreeBSD](https://wiki.freebsd.org/Graphics)

build.zig

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Compatible with Zig Version 0.11.0
22
const std = @import("std");
3+
const ArrayList = std.ArrayList;
34
const Compile = std.Build.Step.Compile;
45
const ConfigHeader = std.Build.Step.ConfigHeader;
56
const Mode = std.builtin.Mode;
@@ -10,11 +11,31 @@ const Maker = struct {
1011
target: CrossTarget,
1112
optimize: Mode,
1213
config_header: *ConfigHeader,
14+
enable_lto: bool,
1315

14-
const cflags = .{"-std=c11"};
15-
const cxxflags = .{"-std=c++11"};
16+
include_dirs: ArrayList([]const u8),
17+
cflags: ArrayList([]const u8),
18+
cxxflags: ArrayList([]const u8),
19+
objs: ArrayList(*Compile),
1620

17-
fn init(builder: *std.build.Builder) Maker {
21+
fn addInclude(m: *Maker, dir: []const u8) !void {
22+
try m.include_dirs.append(dir);
23+
}
24+
fn addProjectInclude(m: *Maker, path: []const []const u8) !void {
25+
try m.addInclude(try m.builder.build_root.join(m.builder.allocator, path));
26+
}
27+
fn addCFlag(m: *Maker, flag: []const u8) !void {
28+
try m.cflags.append(flag);
29+
}
30+
fn addCxxFlag(m: *Maker, flag: []const u8) !void {
31+
try m.cxxflags.append(flag);
32+
}
33+
fn addFlag(m: *Maker, flag: []const u8) !void {
34+
try m.addCFlag(flag);
35+
try m.addCxxFlag(flag);
36+
}
37+
38+
fn init(builder: *std.build.Builder) !Maker {
1839
const commit_hash = @embedFile(".git/refs/heads/master");
1940
const config_header = builder.addConfigHeader(
2041
.{ .style = .blank, .include_path = "build-info.h" },
@@ -23,58 +44,71 @@ const Maker = struct {
2344
.BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline
2445
},
2546
);
26-
return Maker{
47+
var m = Maker{
2748
.builder = builder,
2849
.target = builder.standardTargetOptions(.{}),
2950
.optimize = builder.standardOptimizeOption(.{}),
3051
.config_header = config_header,
52+
.enable_lto = false,
53+
.include_dirs = ArrayList([]const u8).init(builder.allocator),
54+
.cflags = ArrayList([]const u8).init(builder.allocator),
55+
.cxxflags = ArrayList([]const u8).init(builder.allocator),
56+
.objs = ArrayList(*Compile).init(builder.allocator),
3157
};
58+
try m.addCFlag("-std=c11");
59+
try m.addCxxFlag("-std=c++11");
60+
try m.addProjectInclude(&.{});
61+
try m.addProjectInclude(&.{"examples"});
62+
return m;
3263
}
3364

3465
fn obj(m: *const Maker, name: []const u8, src: []const u8) *Compile {
3566
const o = m.builder.addObject(.{ .name = name, .target = m.target, .optimize = m.optimize });
3667
if (std.mem.endsWith(u8, src, ".c")) {
37-
o.addCSourceFiles(&.{src}, &cflags);
68+
o.addCSourceFiles(&.{src}, m.cflags.items);
3869
o.linkLibC();
3970
} else {
40-
o.addCSourceFiles(&.{src}, &cxxflags);
71+
o.addCSourceFiles(&.{src}, m.cxxflags.items);
4172
o.linkLibCpp();
4273
}
43-
o.addIncludePath(.{ .path = "." });
44-
o.addIncludePath(.{ .path = "./examples" });
74+
for (m.include_dirs.items) |i| o.addIncludePath(.{ .path = i });
75+
o.want_lto = m.enable_lto;
4576
return o;
4677
}
4778

4879
fn exe(m: *const Maker, name: []const u8, src: []const u8, deps: []const *Compile) *Compile {
4980
const e = m.builder.addExecutable(.{ .name = name, .target = m.target, .optimize = m.optimize });
50-
e.addIncludePath(.{ .path = "." });
51-
e.addIncludePath(.{ .path = "./examples" });
52-
e.addCSourceFiles(&.{src}, &cxxflags);
81+
e.addCSourceFiles(&.{src}, m.cxxflags.items);
5382
for (deps) |d| e.addObject(d);
83+
for (m.objs.items) |o| e.addObject(o);
84+
for (m.include_dirs.items) |i| e.addIncludePath(.{ .path = i });
5485
e.linkLibC();
5586
e.linkLibCpp();
5687
e.addConfigHeader(m.config_header);
5788
m.builder.installArtifact(e);
58-
59-
// Currently a bug is preventing correct linking for optimized builds for Windows:
60-
// https://github.com/ziglang/zig/issues/15958
61-
if (e.target.isWindows()) {
62-
e.want_lto = false;
63-
}
89+
e.want_lto = m.enable_lto;
6490
return e;
6591
}
6692
};
6793

68-
pub fn build(b: *std.build.Builder) void {
69-
const make = Maker.init(b);
94+
pub fn build(b: *std.build.Builder) !void {
95+
var make = try Maker.init(b);
96+
make.enable_lto = b.option(bool, "lto", "Enable LTO optimization, (default: false)") orelse false;
97+
98+
if (b.option(bool, "k-quants", "Enable K-quants, (default: true)") orelse true) {
99+
try make.addFlag("-DGGML_USE_K_QUANTS");
100+
const k_quants = make.obj("k_quants", "k_quants.c");
101+
try make.objs.append(k_quants);
102+
}
70103

71104
const ggml = make.obj("ggml", "ggml.c");
72105
const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c");
73106
const llama = make.obj("llama", "llama.cpp");
74107
const common = make.obj("common", "examples/common.cpp");
108+
const console = make.obj("common", "examples/console.cpp");
75109
const grammar_parser = make.obj("grammar-parser", "examples/grammar-parser.cpp");
76110

77-
_ = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, llama, common, grammar_parser });
111+
_ = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, llama, common, console, grammar_parser });
78112
_ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, llama });
79113
_ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, llama, common });
80114
_ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, llama, common });

0 commit comments

Comments
 (0)