build.zig 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const std = @import("std");
  2. const commit_hash = @embedFile(".git/refs/heads/master");
  3. // Zig Version: 0.11.0-dev.3986+e05c242cd
  4. pub fn build(b: *std.build.Builder) void {
  5. const target = b.standardTargetOptions(.{});
  6. const optimize = b.standardOptimizeOption(.{});
  7. const config_header = b.addConfigHeader(
  8. .{ .style = .blank, .include_path = "build-info.h" },
  9. .{
  10. .BUILD_NUMBER = 0,
  11. .BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline
  12. },
  13. );
  14. const lib = b.addStaticLibrary(.{
  15. .name = "llama",
  16. .target = target,
  17. .optimize = optimize,
  18. });
  19. lib.linkLibC();
  20. lib.linkLibCpp();
  21. lib.addIncludePath(".");
  22. lib.addIncludePath("./examples");
  23. lib.addConfigHeader(config_header);
  24. lib.addCSourceFiles(&.{"ggml.c"}, &.{"-std=c11"});
  25. lib.addCSourceFiles(&.{"llama.cpp"}, &.{"-std=c++11"});
  26. b.installArtifact(lib);
  27. const examples = .{
  28. "main",
  29. "baby-llama",
  30. "embedding",
  31. "metal",
  32. "perplexity",
  33. "quantize",
  34. "quantize-stats",
  35. "save-load-state",
  36. "server",
  37. "simple",
  38. "train-text-from-scratch",
  39. };
  40. inline for (examples) |example_name| {
  41. const exe = b.addExecutable(.{
  42. .name = example_name,
  43. .target = target,
  44. .optimize = optimize,
  45. });
  46. exe.addIncludePath(".");
  47. exe.addIncludePath("./examples");
  48. exe.addConfigHeader(config_header);
  49. exe.addCSourceFiles(&.{
  50. std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{ example_name, example_name }),
  51. "examples/common.cpp",
  52. }, &.{"-std=c++11"});
  53. exe.linkLibrary(lib);
  54. b.installArtifact(exe);
  55. const run_cmd = b.addRunArtifact(exe);
  56. run_cmd.step.dependOn(b.getInstallStep());
  57. if (b.args) |args| run_cmd.addArgs(args);
  58. const run_step = b.step("run-" ++ example_name, "Run the app");
  59. run_step.dependOn(&run_cmd.step);
  60. }
  61. }