build.zig 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 lib = b.addStaticLibrary(.{
  6. .name = "llama",
  7. .target = target,
  8. .optimize = optimize,
  9. });
  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. "examples/common.cpp",
  19. }, &.{"-std=c++11"});
  20. lib.install();
  21. const build_args = .{ .b = b, .lib = lib, .target = target, .optimize = optimize };
  22. const exe = build_example("main", build_args);
  23. _ = build_example("quantize", build_args);
  24. _ = build_example("perplexity", build_args);
  25. _ = build_example("embedding", build_args);
  26. // create "zig build run" command for ./main
  27. const run_cmd = exe.run();
  28. run_cmd.step.dependOn(b.getInstallStep());
  29. if (b.args) |args| {
  30. run_cmd.addArgs(args);
  31. }
  32. const run_step = b.step("run", "Run the app");
  33. run_step.dependOn(&run_cmd.step);
  34. }
  35. fn build_example(comptime name: []const u8, args: anytype) *std.build.LibExeObjStep {
  36. const b = args.b;
  37. const lib = args.lib;
  38. const target = args.target;
  39. const optimize = args.optimize;
  40. const exe = b.addExecutable(.{
  41. .name = name,
  42. .target = target,
  43. .optimize = optimize,
  44. });
  45. exe.addIncludePath(".");
  46. exe.addIncludePath("examples");
  47. exe.addCSourceFiles(&.{
  48. std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{name, name}),
  49. }, &.{"-std=c++11"});
  50. exe.linkLibrary(lib);
  51. exe.install();
  52. return exe;
  53. }