build.zig 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const std = @import("std");
  2. pub fn build(b: *std.Build) void {
  3. const target = b.standardTargetOptions(.{});
  4. const optimize = b.standardOptimizeOption(.{});
  5. const want_lto = b.option(bool, "lto", "Want -fLTO");
  6. const lib = b.addStaticLibrary(.{
  7. .name = "llama",
  8. .target = target,
  9. .optimize = optimize,
  10. });
  11. lib.want_lto = want_lto;
  12. lib.linkLibCpp();
  13. lib.addIncludePath(".");
  14. lib.addIncludePath("examples");
  15. lib.addCSourceFiles(&.{
  16. "ggml.c",
  17. }, &.{"-std=c11"});
  18. lib.addCSourceFiles(&.{
  19. "llama.cpp",
  20. }, &.{"-std=c++11"});
  21. lib.install();
  22. const build_args = .{ .b = b, .lib = lib, .target = target, .optimize = optimize, .want_lto = want_lto };
  23. const exe = build_example("main", build_args);
  24. _ = build_example("quantize", build_args);
  25. _ = build_example("perplexity", build_args);
  26. _ = build_example("embedding", build_args);
  27. // create "zig build run" command for ./main
  28. const run_cmd = exe.run();
  29. run_cmd.step.dependOn(b.getInstallStep());
  30. if (b.args) |args| {
  31. run_cmd.addArgs(args);
  32. }
  33. const run_step = b.step("run", "Run the app");
  34. run_step.dependOn(&run_cmd.step);
  35. }
  36. fn build_example(comptime name: []const u8, args: anytype) *std.build.LibExeObjStep {
  37. const b = args.b;
  38. const lib = args.lib;
  39. const target = args.target;
  40. const optimize = args.optimize;
  41. const want_lto = args.want_lto;
  42. const exe = b.addExecutable(.{
  43. .name = name,
  44. .target = target,
  45. .optimize = optimize,
  46. });
  47. exe.want_lto = want_lto;
  48. exe.addIncludePath(".");
  49. exe.addIncludePath("examples");
  50. exe.addCSourceFiles(&.{
  51. std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{name, name}),
  52. "examples/common.cpp",
  53. }, &.{"-std=c++11"});
  54. exe.linkLibrary(lib);
  55. exe.install();
  56. return exe;
  57. }