mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2024-12-26 03:14:35 +00:00
[Zig] Fixing Zig build and improvements (#2554)
* Fix zig after console.o was split * Better include and flag management * Change LTO to option
This commit is contained in:
parent
8dae7ce684
commit
6ddeefad9b
@ -238,12 +238,17 @@ In order to build llama.cpp you have three different options.
|
|||||||
cmake --build . --config Release
|
cmake --build . --config Release
|
||||||
```
|
```
|
||||||
|
|
||||||
- Using `Zig`:
|
- Using `Zig` (version 0.11 or later):
|
||||||
|
|
||||||
|
Building for optimization levels and CPU features can be accomplished using standard build arguments, for example AVX2, FMA, F16C,
|
||||||
|
it's also possible to cross compile for other operating systems and architectures:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
zig build -Doptimize=ReleaseFast
|
zig build -Doptimize=ReleaseFast -Dtarget=x86_64-windows-gnu -Dcpu=x86_64+avx2+fma+f16c
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The `zig targets` command will give you valid options to use.
|
||||||
|
|
||||||
- Using `gmake` (FreeBSD):
|
- Using `gmake` (FreeBSD):
|
||||||
|
|
||||||
1. Install and activate [DRM in FreeBSD](https://wiki.freebsd.org/Graphics)
|
1. Install and activate [DRM in FreeBSD](https://wiki.freebsd.org/Graphics)
|
||||||
|
74
build.zig
74
build.zig
@ -1,5 +1,6 @@
|
|||||||
// Compatible with Zig Version 0.11.0
|
// Compatible with Zig Version 0.11.0
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const ArrayList = std.ArrayList;
|
||||||
const Compile = std.Build.Step.Compile;
|
const Compile = std.Build.Step.Compile;
|
||||||
const ConfigHeader = std.Build.Step.ConfigHeader;
|
const ConfigHeader = std.Build.Step.ConfigHeader;
|
||||||
const Mode = std.builtin.Mode;
|
const Mode = std.builtin.Mode;
|
||||||
@ -10,11 +11,31 @@ const Maker = struct {
|
|||||||
target: CrossTarget,
|
target: CrossTarget,
|
||||||
optimize: Mode,
|
optimize: Mode,
|
||||||
config_header: *ConfigHeader,
|
config_header: *ConfigHeader,
|
||||||
|
enable_lto: bool,
|
||||||
|
|
||||||
const cflags = .{"-std=c11"};
|
include_dirs: ArrayList([]const u8),
|
||||||
const cxxflags = .{"-std=c++11"};
|
cflags: ArrayList([]const u8),
|
||||||
|
cxxflags: ArrayList([]const u8),
|
||||||
|
objs: ArrayList(*Compile),
|
||||||
|
|
||||||
fn init(builder: *std.build.Builder) Maker {
|
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 {
|
||||||
const commit_hash = @embedFile(".git/refs/heads/master");
|
const commit_hash = @embedFile(".git/refs/heads/master");
|
||||||
const config_header = builder.addConfigHeader(
|
const config_header = builder.addConfigHeader(
|
||||||
.{ .style = .blank, .include_path = "build-info.h" },
|
.{ .style = .blank, .include_path = "build-info.h" },
|
||||||
@ -23,58 +44,71 @@ const Maker = struct {
|
|||||||
.BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline
|
.BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return Maker{
|
var m = Maker{
|
||||||
.builder = builder,
|
.builder = builder,
|
||||||
.target = builder.standardTargetOptions(.{}),
|
.target = builder.standardTargetOptions(.{}),
|
||||||
.optimize = builder.standardOptimizeOption(.{}),
|
.optimize = builder.standardOptimizeOption(.{}),
|
||||||
.config_header = config_header,
|
.config_header = config_header,
|
||||||
|
.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),
|
||||||
};
|
};
|
||||||
|
try m.addCFlag("-std=c11");
|
||||||
|
try m.addCxxFlag("-std=c++11");
|
||||||
|
try m.addProjectInclude(&.{});
|
||||||
|
try m.addProjectInclude(&.{"examples"});
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn obj(m: *const Maker, name: []const u8, src: []const u8) *Compile {
|
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 });
|
const o = m.builder.addObject(.{ .name = name, .target = m.target, .optimize = m.optimize });
|
||||||
if (std.mem.endsWith(u8, src, ".c")) {
|
if (std.mem.endsWith(u8, src, ".c")) {
|
||||||
o.addCSourceFiles(&.{src}, &cflags);
|
o.addCSourceFiles(&.{src}, m.cflags.items);
|
||||||
o.linkLibC();
|
o.linkLibC();
|
||||||
} else {
|
} else {
|
||||||
o.addCSourceFiles(&.{src}, &cxxflags);
|
o.addCSourceFiles(&.{src}, m.cxxflags.items);
|
||||||
o.linkLibCpp();
|
o.linkLibCpp();
|
||||||
}
|
}
|
||||||
o.addIncludePath(.{ .path = "." });
|
for (m.include_dirs.items) |i| o.addIncludePath(.{ .path = i });
|
||||||
o.addIncludePath(.{ .path = "./examples" });
|
o.want_lto = m.enable_lto;
|
||||||
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 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.addIncludePath(.{ .path = "." });
|
e.addCSourceFiles(&.{src}, m.cxxflags.items);
|
||||||
e.addIncludePath(.{ .path = "./examples" });
|
|
||||||
e.addCSourceFiles(&.{src}, &cxxflags);
|
|
||||||
for (deps) |d| e.addObject(d);
|
for (deps) |d| e.addObject(d);
|
||||||
|
for (m.objs.items) |o| e.addObject(o);
|
||||||
|
for (m.include_dirs.items) |i| e.addIncludePath(.{ .path = i });
|
||||||
e.linkLibC();
|
e.linkLibC();
|
||||||
e.linkLibCpp();
|
e.linkLibCpp();
|
||||||
e.addConfigHeader(m.config_header);
|
e.addConfigHeader(m.config_header);
|
||||||
m.builder.installArtifact(e);
|
m.builder.installArtifact(e);
|
||||||
|
e.want_lto = m.enable_lto;
|
||||||
// Currently a bug is preventing correct linking for optimized builds for Windows:
|
|
||||||
// https://github.com/ziglang/zig/issues/15958
|
|
||||||
if (e.target.isWindows()) {
|
|
||||||
e.want_lto = false;
|
|
||||||
}
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn build(b: *std.build.Builder) void {
|
pub fn build(b: *std.build.Builder) !void {
|
||||||
const make = Maker.init(b);
|
var make = try Maker.init(b);
|
||||||
|
make.enable_lto = b.option(bool, "lto", "Enable LTO optimization, (default: false)") orelse false;
|
||||||
|
|
||||||
|
if (b.option(bool, "k-quants", "Enable K-quants, (default: true)") orelse true) {
|
||||||
|
try make.addFlag("-DGGML_USE_K_QUANTS");
|
||||||
|
const k_quants = make.obj("k_quants", "k_quants.c");
|
||||||
|
try make.objs.append(k_quants);
|
||||||
|
}
|
||||||
|
|
||||||
const ggml = make.obj("ggml", "ggml.c");
|
const ggml = make.obj("ggml", "ggml.c");
|
||||||
const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c");
|
const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c");
|
||||||
const llama = make.obj("llama", "llama.cpp");
|
const llama = make.obj("llama", "llama.cpp");
|
||||||
const common = make.obj("common", "examples/common.cpp");
|
const common = make.obj("common", "examples/common.cpp");
|
||||||
|
const console = make.obj("common", "examples/console.cpp");
|
||||||
const grammar_parser = make.obj("grammar-parser", "examples/grammar-parser.cpp");
|
const grammar_parser = make.obj("grammar-parser", "examples/grammar-parser.cpp");
|
||||||
|
|
||||||
_ = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, llama, common, grammar_parser });
|
_ = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, llama, common, console, grammar_parser });
|
||||||
_ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, llama });
|
_ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, llama });
|
||||||
_ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, llama, common });
|
_ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, llama, common });
|
||||||
_ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, llama, common });
|
_ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, llama, common });
|
||||||
|
Loading…
Reference in New Issue
Block a user