build.zig 1.8 KB

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