2023-08-07 07:35:53 +02:00
|
|
|
// Compatible with Zig Version 0.11.0
|
2023-04-05 17:06:02 +02:00
|
|
|
const std = @import("std");
|
2023-08-17 22:11:18 +02:00
|
|
|
const ArrayList = std.ArrayList;
|
2023-08-07 07:35:53 +02:00
|
|
|
const Compile = std.Build.Step.Compile;
|
|
|
|
const ConfigHeader = std.Build.Step.ConfigHeader;
|
|
|
|
const Mode = std.builtin.Mode;
|
|
|
|
const CrossTarget = std.zig.CrossTarget;
|
2023-04-05 17:06:02 +02:00
|
|
|
|
2023-08-07 07:35:53 +02:00
|
|
|
const Maker = struct {
|
|
|
|
builder: *std.build.Builder,
|
|
|
|
target: CrossTarget,
|
|
|
|
optimize: Mode,
|
|
|
|
config_header: *ConfigHeader,
|
2023-08-17 22:11:18 +02:00
|
|
|
enable_lto: bool,
|
2023-08-07 07:35:53 +02:00
|
|
|
|
2023-08-17 22:11:18 +02:00
|
|
|
include_dirs: ArrayList([]const u8),
|
|
|
|
cflags: ArrayList([]const u8),
|
|
|
|
cxxflags: ArrayList([]const u8),
|
|
|
|
objs: ArrayList(*Compile),
|
2023-08-07 07:35:53 +02:00
|
|
|
|
2023-08-17 22:11:18 +02:00
|
|
|
fn addInclude(m: *Maker, dir: []const u8) !void {
|
|
|
|
try m.include_dirs.append(dir);
|
|
|
|
}
|
|
|
|
fn addProjectInclude(m: *Maker, path: []const []const u8) !void {
|
|
|
|
try m.addInclude(try m.builder.build_root.join(m.builder.allocator, path));
|
|
|
|
}
|
|
|
|
fn addCFlag(m: *Maker, flag: []const u8) !void {
|
|
|
|
try m.cflags.append(flag);
|
|
|
|
}
|
|
|
|
fn addCxxFlag(m: *Maker, flag: []const u8) !void {
|
|
|
|
try m.cxxflags.append(flag);
|
|
|
|
}
|
|
|
|
fn addFlag(m: *Maker, flag: []const u8) !void {
|
|
|
|
try m.addCFlag(flag);
|
|
|
|
try m.addCxxFlag(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init(builder: *std.build.Builder) !Maker {
|
2023-09-21 11:08:20 +02:00
|
|
|
const target = builder.standardTargetOptions(.{});
|
2023-10-08 15:59:20 +02:00
|
|
|
const zig_version = @import("builtin").zig_version_string;
|
|
|
|
const commit_hash = try std.ChildProcess.exec(
|
|
|
|
.{ .allocator = builder.allocator, .argv = &.{ "git", "rev-parse", "HEAD" } },
|
|
|
|
);
|
2023-08-07 07:35:53 +02:00
|
|
|
const config_header = builder.addConfigHeader(
|
|
|
|
.{ .style = .blank, .include_path = "build-info.h" },
|
|
|
|
.{
|
|
|
|
.BUILD_NUMBER = 0,
|
2023-10-08 15:59:20 +02:00
|
|
|
.BUILD_COMMIT = commit_hash.stdout[0 .. commit_hash.stdout.len - 1], // omit newline
|
|
|
|
.BUILD_COMPILER = builder.fmt("Zig {s}", .{zig_version}),
|
2023-09-21 11:08:20 +02:00
|
|
|
.BUILD_TARGET = try target.allocDescription(builder.allocator),
|
2023-08-07 07:35:53 +02:00
|
|
|
},
|
|
|
|
);
|
2023-08-17 22:11:18 +02:00
|
|
|
var m = Maker{
|
2023-08-07 07:35:53 +02:00
|
|
|
.builder = builder,
|
2023-09-21 11:08:20 +02:00
|
|
|
.target = target,
|
2023-08-07 07:35:53 +02:00
|
|
|
.optimize = builder.standardOptimizeOption(.{}),
|
|
|
|
.config_header = config_header,
|
2023-08-17 22:11:18 +02:00
|
|
|
.enable_lto = false,
|
|
|
|
.include_dirs = ArrayList([]const u8).init(builder.allocator),
|
|
|
|
.cflags = ArrayList([]const u8).init(builder.allocator),
|
|
|
|
.cxxflags = ArrayList([]const u8).init(builder.allocator),
|
|
|
|
.objs = ArrayList(*Compile).init(builder.allocator),
|
2023-08-07 07:35:53 +02:00
|
|
|
};
|
2023-08-17 22:11:18 +02:00
|
|
|
try m.addCFlag("-std=c11");
|
|
|
|
try m.addCxxFlag("-std=c++11");
|
|
|
|
try m.addProjectInclude(&.{});
|
2023-09-21 11:08:20 +02:00
|
|
|
try m.addProjectInclude(&.{"common"});
|
2023-08-17 22:11:18 +02:00
|
|
|
return m;
|
2023-08-07 07:35:53 +02:00
|
|
|
}
|
2023-07-14 20:50:58 +02:00
|
|
|
|
2023-08-07 07:35:53 +02:00
|
|
|
fn obj(m: *const Maker, name: []const u8, src: []const u8) *Compile {
|
|
|
|
const o = m.builder.addObject(.{ .name = name, .target = m.target, .optimize = m.optimize });
|
2023-10-08 15:59:20 +02:00
|
|
|
if (o.target.getAbi() != .msvc)
|
|
|
|
o.defineCMacro("_GNU_SOURCE", null);
|
|
|
|
o.addConfigHeader(m.config_header);
|
2023-08-07 07:35:53 +02:00
|
|
|
if (std.mem.endsWith(u8, src, ".c")) {
|
2023-08-17 22:11:18 +02:00
|
|
|
o.addCSourceFiles(&.{src}, m.cflags.items);
|
2023-08-07 07:35:53 +02:00
|
|
|
o.linkLibC();
|
|
|
|
} else {
|
2023-08-17 22:11:18 +02:00
|
|
|
o.addCSourceFiles(&.{src}, m.cxxflags.items);
|
2023-10-08 15:59:20 +02:00
|
|
|
if (o.target.getAbi() == .msvc) {
|
|
|
|
o.linkLibC(); // need winsdk + crt
|
|
|
|
} else {
|
|
|
|
// linkLibCpp already add (libc++ + libunwind + libc)
|
|
|
|
o.linkLibCpp();
|
|
|
|
}
|
2023-08-07 07:35:53 +02:00
|
|
|
}
|
2023-09-21 11:08:20 +02:00
|
|
|
o.addConfigHeader(m.config_header);
|
2023-08-17 22:11:18 +02:00
|
|
|
for (m.include_dirs.items) |i| o.addIncludePath(.{ .path = i });
|
|
|
|
o.want_lto = m.enable_lto;
|
2023-08-07 07:35:53 +02:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exe(m: *const Maker, name: []const u8, src: []const u8, deps: []const *Compile) *Compile {
|
|
|
|
const e = m.builder.addExecutable(.{ .name = name, .target = m.target, .optimize = m.optimize });
|
2023-08-17 22:11:18 +02:00
|
|
|
e.addCSourceFiles(&.{src}, m.cxxflags.items);
|
2023-08-07 07:35:53 +02:00
|
|
|
for (deps) |d| e.addObject(d);
|
2023-08-17 22:11:18 +02:00
|
|
|
for (m.objs.items) |o| e.addObject(o);
|
|
|
|
for (m.include_dirs.items) |i| e.addIncludePath(.{ .path = i });
|
2023-10-08 15:59:20 +02:00
|
|
|
|
|
|
|
// https://github.com/ziglang/zig/issues/15448
|
|
|
|
if (e.target.getAbi() == .msvc) {
|
|
|
|
e.linkLibC(); // need winsdk + crt
|
|
|
|
} else {
|
|
|
|
// linkLibCpp already add (libc++ + libunwind + libc)
|
|
|
|
e.linkLibCpp();
|
|
|
|
}
|
2023-08-07 07:35:53 +02:00
|
|
|
e.addConfigHeader(m.config_header);
|
|
|
|
m.builder.installArtifact(e);
|
2023-08-17 22:11:18 +02:00
|
|
|
e.want_lto = m.enable_lto;
|
2023-08-07 07:35:53 +02:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
};
|
2023-06-25 07:45:44 +02:00
|
|
|
|
2023-08-17 22:11:18 +02:00
|
|
|
pub fn build(b: *std.build.Builder) !void {
|
|
|
|
var make = try Maker.init(b);
|
|
|
|
make.enable_lto = b.option(bool, "lto", "Enable LTO optimization, (default: false)") orelse false;
|
|
|
|
|
2023-08-07 07:35:53 +02:00
|
|
|
const ggml = make.obj("ggml", "ggml.c");
|
|
|
|
const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c");
|
2023-10-08 19:19:14 +02:00
|
|
|
const ggml_backend = make.obj("ggml-backend", "ggml-backend.c");
|
2023-10-29 17:32:28 +01:00
|
|
|
const ggml_quants = make.obj("ggml-quants", "ggml-quants.c");
|
2023-08-07 07:35:53 +02:00
|
|
|
const llama = make.obj("llama", "llama.cpp");
|
2023-09-21 11:08:20 +02:00
|
|
|
const common = make.obj("common", "common/common.cpp");
|
2023-10-08 15:59:20 +02:00
|
|
|
const console = make.obj("console", "common/console.cpp");
|
2023-10-11 21:35:46 +02:00
|
|
|
const sampling = make.obj("sampling", "common/sampling.cpp");
|
2023-09-21 11:08:20 +02:00
|
|
|
const grammar_parser = make.obj("grammar-parser", "common/grammar-parser.cpp");
|
2023-10-08 10:24:01 +02:00
|
|
|
const train = make.obj("train", "common/train.cpp");
|
2023-10-22 21:53:08 +02:00
|
|
|
const clip = make.obj("clip", "examples/llava/clip.cpp");
|
2023-07-14 20:50:58 +02:00
|
|
|
|
2023-10-29 17:32:28 +01:00
|
|
|
_ = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, sampling, console, grammar_parser });
|
|
|
|
_ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common });
|
|
|
|
_ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common });
|
|
|
|
_ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common });
|
|
|
|
_ = make.exe("finetune", "examples/finetune/finetune.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, train });
|
|
|
|
_ = 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, train });
|
2023-07-14 20:50:58 +02:00
|
|
|
|
2023-10-29 17:32:28 +01:00
|
|
|
const server = make.exe("server", "examples/server/server.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, sampling, grammar_parser, clip });
|
2023-08-07 07:35:53 +02:00
|
|
|
if (server.target.isWindows()) {
|
|
|
|
server.linkSystemLibrary("ws2_32");
|
2023-04-05 17:06:02 +02:00
|
|
|
}
|
|
|
|
}
|