update build.zig

This commit is contained in:
hazelnutcloud 2024-02-12 16:06:41 +08:00
parent 8e8d76cb39
commit 03477279df

View file

@ -11,6 +11,8 @@ const Maker = struct {
target: Target, target: Target,
optimize: Mode, optimize: Mode,
enable_lto: bool, enable_lto: bool,
build_all: bool,
install_libs: bool,
include_dirs: ArrayList([]const u8), include_dirs: ArrayList([]const u8),
cflags: ArrayList([]const u8), cflags: ArrayList([]const u8),
@ -53,6 +55,8 @@ const Maker = struct {
.target = target, .target = target,
.optimize = builder.standardOptimizeOption(.{}), .optimize = builder.standardOptimizeOption(.{}),
.enable_lto = false, .enable_lto = false,
.build_all = false,
.install_libs = false,
.include_dirs = ArrayList([]const u8).init(builder.allocator), .include_dirs = ArrayList([]const u8).init(builder.allocator),
.cflags = ArrayList([]const u8).init(builder.allocator), .cflags = ArrayList([]const u8).init(builder.allocator),
.cxxflags = ArrayList([]const u8).init(builder.allocator), .cxxflags = ArrayList([]const u8).init(builder.allocator),
@ -75,8 +79,8 @@ const Maker = struct {
return m; return m;
} }
fn obj(m: *const Maker, name: []const u8, src: []const u8) *Compile { fn lib(m: *const Maker, name: []const u8, src: []const u8) *Compile {
const o = m.builder.addObject(.{ .name = name, .target = m.target, .optimize = m.optimize }); const o = m.builder.addStaticLibrary(.{ .name = name, .target = m.target, .optimize = m.optimize });
if (std.mem.endsWith(u8, src, ".c") or std.mem.endsWith(u8, src, ".m")) { if (std.mem.endsWith(u8, src, ".c") or std.mem.endsWith(u8, src, ".m")) {
o.addCSourceFiles(.{ .files = &.{src}, .flags = m.cflags.items }); o.addCSourceFiles(.{ .files = &.{src}, .flags = m.cflags.items });
@ -92,13 +96,17 @@ const Maker = struct {
} }
for (m.include_dirs.items) |i| o.addIncludePath(.{ .path = i }); for (m.include_dirs.items) |i| o.addIncludePath(.{ .path = i });
o.want_lto = m.enable_lto; o.want_lto = m.enable_lto;
if (m.install_libs) m.builder.installArtifact(o);
return o; return o;
} }
fn exe(m: *const Maker, name: []const u8, src: []const u8, deps: []const *Compile) *Compile { fn exe(m: *const Maker, name: []const u8, src: []const u8, deps: []const *Compile) ?*Compile {
const opt = m.builder.option(bool, name, std.fmt.allocPrint(m.builder.allocator, "Build and install the {s} executable", .{name}) catch @panic("OOM")) orelse false;
if (!opt and !m.build_all) return null;
const e = m.builder.addExecutable(.{ .name = name, .target = m.target, .optimize = m.optimize }); const e = m.builder.addExecutable(.{ .name = name, .target = m.target, .optimize = m.optimize });
e.addCSourceFiles(.{ .files = &.{src}, .flags = m.cxxflags.items }); e.addCSourceFiles(.{ .files = &.{src}, .flags = m.cxxflags.items });
for (deps) |d| e.addObject(d); for (deps) |d| e.linkLibrary(d);
for (m.include_dirs.items) |i| e.addIncludePath(.{ .path = i }); for (m.include_dirs.items) |i| e.addIncludePath(.{ .path = i });
// https://github.com/ziglang/zig/issues/15448 // https://github.com/ziglang/zig/issues/15448
@ -117,6 +125,8 @@ const Maker = struct {
pub fn build(b: *std.Build) !void { pub fn build(b: *std.Build) !void {
var make = try Maker.init(b); var make = try Maker.init(b);
make.enable_lto = b.option(bool, "lto", "Enable LTO optimization, (default: false)") orelse false; make.enable_lto = b.option(bool, "lto", "Enable LTO optimization, (default: false)") orelse false;
make.build_all = b.option(bool, "build-all", "Build all executables, (default: false)") orelse false;
make.install_libs = b.option(bool, "install-libs", "Install all libraries, (default: false)") orelse false;
// Options // Options
const llama_vulkan = b.option(bool, "llama-vulkan", "Enable Vulkan backend for Llama, (default: false)") orelse false; const llama_vulkan = b.option(bool, "llama-vulkan", "Enable Vulkan backend for Llama, (default: false)") orelse false;
@ -131,33 +141,33 @@ pub fn build(b: *std.Build) !void {
try make.addFlag("-DACCELERATE_LAPACK_ILP64"); try make.addFlag("-DACCELERATE_LAPACK_ILP64");
} }
// Objects // Libraries
var extra_objs = ArrayList(*Compile).init(b.allocator); var extra_libs = ArrayList(*Compile).init(b.allocator);
if (llama_vulkan) { if (llama_vulkan) {
try make.addFlag("-DGGML_USE_VULKAN"); try make.addFlag("-DGGML_USE_VULKAN");
const ggml_vulkan = make.obj("ggml-vulkan", "ggml-vulkan.cpp"); const ggml_vulkan = make.lib("ggml-vulkan", "ggml-vulkan.cpp");
try extra_objs.append(ggml_vulkan); try extra_libs.append(ggml_vulkan);
} }
if (llama_metal) { if (llama_metal) {
try make.addFlag("-DGGML_USE_METAL"); try make.addFlag("-DGGML_USE_METAL");
const ggml_metal = make.obj("ggml-metal", "ggml-metal.m"); const ggml_metal = make.lib("ggml-metal", "ggml-metal.m");
try extra_objs.append(ggml_metal); try extra_libs.append(ggml_metal);
} }
const ggml = make.obj("ggml", "ggml.c"); const ggml = make.lib("ggml", "ggml.c");
const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c"); const ggml_alloc = make.lib("ggml-alloc", "ggml-alloc.c");
const ggml_backend = make.obj("ggml-backend", "ggml-backend.c"); const ggml_backend = make.lib("ggml-backend", "ggml-backend.c");
const ggml_quants = make.obj("ggml-quants", "ggml-quants.c"); const ggml_quants = make.lib("ggml-quants", "ggml-quants.c");
const llama = make.obj("llama", "llama.cpp"); const llama = make.lib("llama", "llama.cpp");
const buildinfo = make.obj("common", "common/build-info.cpp"); const buildinfo = make.lib("common", "common/build-info.cpp");
const common = make.obj("common", "common/common.cpp"); const common = make.lib("common", "common/common.cpp");
const console = make.obj("console", "common/console.cpp"); const console = make.lib("console", "common/console.cpp");
const sampling = make.obj("sampling", "common/sampling.cpp"); const sampling = make.lib("sampling", "common/sampling.cpp");
const grammar_parser = make.obj("grammar-parser", "common/grammar-parser.cpp"); const grammar_parser = make.lib("grammar-parser", "common/grammar-parser.cpp");
const clip = make.obj("clip", "examples/llava/clip.cpp"); const clip = make.lib("clip", "examples/llava/clip.cpp");
const train = make.obj("train", "common/train.cpp"); const train = make.lib("train", "common/train.cpp");
// Executables // Executables
const main = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, sampling, console, grammar_parser, clip }); const main = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, sampling, console, grammar_parser, clip });
@ -167,27 +177,28 @@ pub fn build(b: *std.Build) !void {
const finetune = make.exe("finetune", "examples/finetune/finetune.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train }); const finetune = make.exe("finetune", "examples/finetune/finetune.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train });
const train_text_from_scratch = 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 }); const train_text_from_scratch = 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 });
const server = make.exe("server", "examples/server/server.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, sampling, console, grammar_parser, clip }); const server = make.exe("server", "examples/server/server.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, sampling, console, grammar_parser, clip });
if (make.target.result.os.tag == .windows) { if (make.target.result.os.tag == .windows and server != null) {
server.linkSystemLibrary("ws2_32"); server.?.linkSystemLibrary("ws2_32");
} }
const exes = [_]*Compile{ main, server, quantize, perplexity, embedding, finetune, train_text_from_scratch }; const exes = [_]?*Compile{ main, server, quantize, perplexity, embedding, finetune, train_text_from_scratch };
for (exes) |e| { for (exes) |e| {
for (extra_objs.items) |o| e.addObject(o); if (e == null) continue;
for (extra_libs.items) |o| e.?.addObject(o);
if (llama_vulkan) { if (llama_vulkan) {
e.linkSystemLibrary("vulkan"); e.?.linkSystemLibrary("vulkan");
} }
if (llama_metal) { if (llama_metal) {
e.linkFramework("Foundation"); e.?.linkFramework("Foundation");
e.linkFramework("Metal"); e.?.linkFramework("Metal");
e.linkFramework("MetalKit"); e.?.linkFramework("MetalKit");
} }
if (llama_accelerate) { if (llama_accelerate) {
e.linkFramework("Accelerate"); e.?.linkFramework("Accelerate");
} }
} }
} }