Skip to content

Commit cc45484

Browse files
committed
optimizing build.zig
1 parent d1328d7 commit cc45484

File tree

1 file changed

+96
-33
lines changed

1 file changed

+96
-33
lines changed

build.zig

+96-33
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ pub fn build(b: *std.Build) !void {
2828
plugin.linkLibrary(lib_godot_cpp);
2929

3030
b.installArtifact(plugin);
31+
32+
const check = b.step("check", "Check if plugin compiles");
33+
check.dependOn(&plugin.step);
3134
}
3235

3336
const BuildParams = struct {
@@ -76,29 +79,38 @@ fn build_lib_llama_cpp(params: BuildParams) !*std.Build.Step.Compile {
7679
const optimize = params.optimize;
7780
const zig_triple = try target.result.zigTriple(b.allocator);
7881

79-
const commit_hash = try std.process.Child.run(.{ .allocator = b.allocator, .argv = &.{ "git", "rev-parse", "HEAD" }, .cwd = b.pathFromRoot("llama.cpp") });
80-
const zig_version = builtin.zig_version_string;
81-
try b.build_root.handle.writeFile(.{ .sub_path = "llama.cpp/common/build-info.cpp", .data = b.fmt(
82-
\\int LLAMA_BUILD_NUMBER = {};
83-
\\char const *LLAMA_COMMIT = "{s}";
84-
\\char const *LLAMA_COMPILER = "Zig {s}";
85-
\\char const *LLAMA_BUILD_TARGET = "{s}";
86-
, .{ 0, commit_hash.stdout[0 .. commit_hash.stdout.len - 1], zig_version, zig_triple }) });
82+
const lib_llama_cpp = b.addStaticLibrary(.{
83+
.name = "llama.cpp",
84+
.target = target,
85+
.optimize = optimize,
86+
});
8787

88-
const lib_llama_cpp = b.addStaticLibrary(.{ .name = "llama.cpp", .target = target, .optimize = optimize });
88+
const build_info_run = b.addSystemCommand(&.{
89+
"echo",
90+
"-e",
91+
b.fmt(
92+
"int LLAMA_BUILD_NUMBER = {d};\\nchar const *LLAMA_COMMIT = \"$(git rev-parse HEAD)\";\\nchar const *LLAMA_COMPILER = \"Zig {s}\";\\nchar const *LLAMA_BUILD_TARGET = \"{s}\";\\n",
93+
.{ 0, builtin.zig_version_string, zig_triple },
94+
),
95+
});
96+
const build_info_wf = b.addWriteFiles();
97+
_ = build_info_wf.addCopyFile(build_info_run.captureStdOut(), "build-info.cpp");
8998

90-
var objs = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
9199
var objBuilder = ObjBuilder.init(.{
92100
.b = b,
93101
.target = target,
94102
.optimize = optimize,
95103
.include_paths = &.{ "llama.cpp", "llama.cpp/common" },
96104
});
105+
try objBuilder.c_flags.append("-std=c11");
106+
try objBuilder.cpp_flags.append("-std=c++11");
107+
108+
var objs = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
97109

98110
switch (target.result.os.tag) {
99111
.macos => {
100-
try objBuilder.flags.append("-DGGML_USE_METAL");
101-
try objs.append(objBuilder.build(.{ .name = "ggml_metal", .sources = &.{"llama.cpp/ggml-metal.m"} }));
112+
try objBuilder.base_flags.append("-DGGML_USE_METAL");
113+
try objs.append(try objBuilder.build(.{ .name = "ggml_metal", .source = "llama.cpp/ggml-metal.m" }));
102114

103115
lib_llama_cpp.linkFramework("Foundation");
104116
lib_llama_cpp.linkFramework("Metal");
@@ -119,24 +131,34 @@ fn build_lib_llama_cpp(params: BuildParams) !*std.Build.Step.Compile {
119131
const install_metal = b.addInstallFileWithDir(metal_expanded, .lib, "ggml-metal.metal");
120132
lib_llama_cpp.step.dependOn(&install_metal.step);
121133
},
134+
.linux => {
135+
try objBuilder.base_flags.append("-D_GNU_SOURCE");
136+
},
122137
else => {},
123138
}
124139

140+
const build_info_compile = try objBuilder.build(.{
141+
.name = "build_info",
142+
.source = "build-info.cpp",
143+
.root = build_info_wf.getDirectory(),
144+
});
145+
build_info_compile.step.dependOn(&build_info_wf.step);
146+
125147
try objs.appendSlice(&.{
126-
objBuilder.build(.{ .name = "ggml", .sources = &.{"llama.cpp/ggml.c"} }),
127-
objBuilder.build(.{ .name = "sgemm", .sources = &.{"llama.cpp/sgemm.cpp"} }),
128-
objBuilder.build(.{ .name = "ggml_alloc", .sources = &.{"llama.cpp/ggml-alloc.c"} }),
129-
objBuilder.build(.{ .name = "ggml_backend", .sources = &.{"llama.cpp/ggml-backend.c"} }),
130-
objBuilder.build(.{ .name = "ggml_quants", .sources = &.{"llama.cpp/ggml-quants.c"} }),
131-
objBuilder.build(.{ .name = "llama", .sources = &.{"llama.cpp/llama.cpp"} }),
132-
objBuilder.build(.{ .name = "unicode", .sources = &.{"llama.cpp/unicode.cpp"} }),
133-
objBuilder.build(.{ .name = "unicode_data", .sources = &.{"llama.cpp/unicode-data.cpp"} }),
134-
objBuilder.build(.{ .name = "common", .sources = &.{"llama.cpp/common/common.cpp"} }),
135-
objBuilder.build(.{ .name = "console", .sources = &.{"llama.cpp/common/console.cpp"} }),
136-
objBuilder.build(.{ .name = "sampling", .sources = &.{"llama.cpp/common/sampling.cpp"} }),
137-
objBuilder.build(.{ .name = "grammar_parser", .sources = &.{"llama.cpp/common/grammar-parser.cpp"} }),
138-
objBuilder.build(.{ .name = "json_schema_to_grammar", .sources = &.{"llama.cpp/common/json-schema-to-grammar.cpp"} }),
139-
objBuilder.build(.{ .name = "build_info", .sources = &.{"llama.cpp/common/build-info.cpp"} }),
148+
try objBuilder.build(.{ .name = "ggml", .source = "llama.cpp/ggml.c" }),
149+
try objBuilder.build(.{ .name = "sgemm", .source = "llama.cpp/sgemm.cpp" }),
150+
try objBuilder.build(.{ .name = "ggml_alloc", .source = "llama.cpp/ggml-alloc.c" }),
151+
try objBuilder.build(.{ .name = "ggml_backend", .source = "llama.cpp/ggml-backend.c" }),
152+
try objBuilder.build(.{ .name = "ggml_quants", .source = "llama.cpp/ggml-quants.c" }),
153+
try objBuilder.build(.{ .name = "llama", .source = "llama.cpp/llama.cpp" }),
154+
try objBuilder.build(.{ .name = "unicode", .source = "llama.cpp/unicode.cpp" }),
155+
try objBuilder.build(.{ .name = "unicode_data", .source = "llama.cpp/unicode-data.cpp" }),
156+
try objBuilder.build(.{ .name = "common", .source = "llama.cpp/common/common.cpp" }),
157+
try objBuilder.build(.{ .name = "console", .source = "llama.cpp/common/console.cpp" }),
158+
try objBuilder.build(.{ .name = "sampling", .source = "llama.cpp/common/sampling.cpp" }),
159+
try objBuilder.build(.{ .name = "grammar_parser", .source = "llama.cpp/common/grammar-parser.cpp" }),
160+
try objBuilder.build(.{ .name = "json_schema_to_grammar", .source = "llama.cpp/common/json-schema-to-grammar.cpp" }),
161+
build_info_compile,
140162
});
141163

142164
for (objs.items) |obj| {
@@ -151,7 +173,9 @@ const ObjBuilder = struct {
151173
target: std.Build.ResolvedTarget,
152174
optimize: std.builtin.OptimizeMode,
153175
include_paths: []const []const u8,
154-
flags: std.ArrayList([]const u8),
176+
c_flags: std.ArrayList([]const u8),
177+
cpp_flags: std.ArrayList([]const u8),
178+
base_flags: std.ArrayList([]const u8),
155179

156180
fn init(params: struct {
157181
b: *std.Build,
@@ -164,18 +188,57 @@ const ObjBuilder = struct {
164188
.target = params.target,
165189
.optimize = params.optimize,
166190
.include_paths = params.include_paths,
167-
.flags = std.ArrayList([]const u8).init(params.b.allocator),
191+
.c_flags = std.ArrayList([]const u8).init(params.b.allocator),
192+
.cpp_flags = std.ArrayList([]const u8).init(params.b.allocator),
193+
.base_flags = std.ArrayList([]const u8).init(params.b.allocator),
168194
};
169195
}
170196

171-
fn build(self: *ObjBuilder, params: struct { name: []const u8, sources: []const []const u8 }) *std.Build.Step.Compile {
172-
const obj = self.b.addObject(.{ .name = params.name, .target = self.target, .optimize = self.optimize });
173-
obj.addCSourceFiles(.{ .files = params.sources, .flags = self.flags.items });
197+
fn build(
198+
self: *ObjBuilder,
199+
params: struct {
200+
name: []const u8,
201+
source: []const u8,
202+
root: ?std.Build.LazyPath = null,
203+
},
204+
) !*std.Build.Step.Compile {
205+
const obj = self.b.addObject(.{
206+
.name = params.name,
207+
.target = self.target,
208+
.optimize = self.optimize,
209+
.pic = true,
210+
});
211+
212+
const Extension = enum {
213+
c,
214+
cpp,
215+
m,
216+
};
217+
const extension = std.meta.stringToEnum(
218+
Extension,
219+
std.mem.trimLeft(u8, std.fs.path.extension(params.source), "."),
220+
) orelse return error.UnknownExtension;
221+
222+
const flags = switch (extension) {
223+
.c, .m => self.c_flags,
224+
.cpp => self.cpp_flags,
225+
};
226+
try self.base_flags.appendSlice(flags.items);
227+
228+
obj.addCSourceFiles(.{
229+
.files = &.{params.source},
230+
.flags = self.base_flags.items,
231+
.root = params.root,
232+
});
174233
for (self.include_paths) |path| {
175234
obj.addIncludePath(.{ .src_path = .{ .owner = self.b, .sub_path = path } });
176235
}
177-
obj.linkLibC();
178-
obj.linkLibCpp();
236+
237+
switch (extension) {
238+
.c, .m => obj.linkLibC(),
239+
.cpp => obj.linkLibCpp(),
240+
}
241+
179242
return obj;
180243
}
181244
};

0 commit comments

Comments
 (0)