build.zig 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Compatible with Zig Version 0.11.0
  2. const std = @import("std");
  3. const Compile = std.Build.Step.Compile;
  4. const ConfigHeader = std.Build.Step.ConfigHeader;
  5. const Mode = std.builtin.Mode;
  6. const CrossTarget = std.zig.CrossTarget;
  7. const Maker = struct {
  8. builder: *std.build.Builder,
  9. target: CrossTarget,
  10. optimize: Mode,
  11. config_header: *ConfigHeader,
  12. const cflags = .{"-std=c11"};
  13. const cxxflags = .{"-std=c++11"};
  14. fn init(builder: *std.build.Builder) Maker {
  15. const commit_hash = @embedFile(".git/refs/heads/master");
  16. const config_header = builder.addConfigHeader(
  17. .{ .style = .blank, .include_path = "build-info.h" },
  18. .{
  19. .BUILD_NUMBER = 0,
  20. .BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline
  21. },
  22. );
  23. return Maker{
  24. .builder = builder,
  25. .target = builder.standardTargetOptions(.{}),
  26. .optimize = builder.standardOptimizeOption(.{}),
  27. .config_header = config_header,
  28. };
  29. }
  30. fn obj(m: *const Maker, name: []const u8, src: []const u8) *Compile {
  31. const o = m.builder.addObject(.{ .name = name, .target = m.target, .optimize = m.optimize });
  32. if (std.mem.endsWith(u8, src, ".c")) {
  33. o.addCSourceFiles(&.{src}, &cflags);
  34. o.linkLibC();
  35. } else {
  36. o.addCSourceFiles(&.{src}, &cxxflags);
  37. o.linkLibCpp();
  38. }
  39. o.addIncludePath(.{ .path = "." });
  40. o.addIncludePath(.{ .path = "./examples" });
  41. return o;
  42. }
  43. fn exe(m: *const Maker, name: []const u8, src: []const u8, deps: []const *Compile) *Compile {
  44. const e = m.builder.addExecutable(.{ .name = name, .target = m.target, .optimize = m.optimize });
  45. e.addIncludePath(.{ .path = "." });
  46. e.addIncludePath(.{ .path = "./examples" });
  47. e.addCSourceFiles(&.{src}, &cxxflags);
  48. for (deps) |d| e.addObject(d);
  49. e.linkLibC();
  50. e.linkLibCpp();
  51. e.addConfigHeader(m.config_header);
  52. m.builder.installArtifact(e);
  53. // Currently a bug is preventing correct linking for optimized builds for Windows:
  54. // https://github.com/ziglang/zig/issues/15958
  55. if (e.target.isWindows()) {
  56. e.want_lto = false;
  57. }
  58. return e;
  59. }
  60. };
  61. pub fn build(b: *std.build.Builder) void {
  62. const make = Maker.init(b);
  63. const ggml = make.obj("ggml", "ggml.c");
  64. const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c");
  65. const llama = make.obj("llama", "llama.cpp");
  66. const common = make.obj("common", "examples/common.cpp");
  67. const grammar_parser = make.obj("grammar-parser", "examples/grammar-parser.cpp");
  68. _ = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, llama, common, grammar_parser });
  69. _ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, llama });
  70. _ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, llama, common });
  71. _ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, llama, common });
  72. _ = make.exe("train-text-from-scratch", "examples/train-text-from-scratch/train-text-from-scratch.cpp", &.{ ggml, ggml_alloc, llama });
  73. const server = make.exe("server", "examples/server/server.cpp", &.{ ggml, ggml_alloc, llama, common, grammar_parser });
  74. if (server.target.isWindows()) {
  75. server.linkSystemLibrary("ws2_32");
  76. }
  77. }