/ build.zig
build.zig
 1  const std = @import("std");
 2  const fmt = std.fmt;
 3  const fs = std.fs;
 4  const heap = std.heap;
 5  const mem = std.mem;
 6  const Compile = std.Build.Step.Compile;
 7  
 8  fn initLibConfig(b: *std.Build, lib: *Compile) void {
 9      lib.linkLibC();
10      lib.addIncludePath(b.path("src/"));
11      lib.addIncludePath(b.path("src/noise_xk/include"));
12      lib.addIncludePath(b.path("src/noise_xk/include/karmel"));
13      lib.addIncludePath(b.path("src/noise_xk/include/karmel/minimal"));
14      //lib.want_lto = false;
15  }
16  
17  pub fn build(b: *std.Build) !void {
18      const root_path = b.pathFromRoot(".");
19      var cwd = try fs.openDirAbsolute(root_path, .{});
20      defer cwd.close();
21  
22      const src_path = "src/";
23      const src_dir = try fs.Dir.openDir(cwd, src_path, .{ .iterate = true, .no_follow = true });
24  
25      const target = b.standardTargetOptions(.{});
26      const optimize = b.standardOptimizeOption(.{});
27  
28      const static_lib = b.addLibrary(.{
29          .name = "liboprf",
30          .linkage = .static,
31          .root_module = b.createModule(.{
32              .target = target,
33              .optimize = optimize,
34          }),
35      });
36  
37      const libsodium_package = b.dependency("libsodium", .{
38          .target = target,
39          .optimize = optimize,
40          .@"test" = false, // `test` is a keyword in zig
41          .static = true,
42          .shared = false
43      });
44      static_lib.linkLibrary(libsodium_package.artifact("sodium"));
45      static_lib.addIncludePath(libsodium_package.path("include"));
46  
47      b.installArtifact(static_lib);
48      initLibConfig(b, static_lib);
49  
50      const flags = &.{
51          "-fvisibility=hidden",
52          "-fPIC",
53          "-fwrapv",
54      };
55  
56      static_lib.installHeadersDirectory(b.path(src_path ++ "/noise_xk/include"), "oprf/noise_xk", .{});
57  
58      const allocator = heap.page_allocator;
59  
60      var walker = try src_dir.walk(allocator);
61      while (try walker.next()) |entry| {
62          if(mem.startsWith(u8, entry.path, "tests")) continue;
63  
64          const name = entry.basename;
65          if(mem.eql(u8, name, "xk-ex.c")) continue;
66          if(mem.eql(u8, name, "jni.c")) continue;
67  
68          if (mem.endsWith(u8, name, ".c")) {
69              const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path });
70              static_lib.addCSourceFile(.{
71                  .file = b.path(full_path),
72                  .flags = flags,
73              });
74          } else if (mem.endsWith(u8, name, ".h")) {
75              const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path });
76              if(!mem.startsWith(u8, entry.path, "noise_xk")) {
77                  const full_dest = try fmt.allocPrint(allocator, "oprf/{s}", .{ name });
78                  static_lib.installHeader(b.path(full_path), full_dest);
79              }
80          }
81      }
82  }