srctree

build.zig
82ee6a1f  Gregory Mullen const std = @import("std"); 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen pub fn build(b: *std.Build) void { 82ee6a1f  Gregory Mullen const target = b.standardTargetOptions(.{}); 82ee6a1f  Gregory Mullen const optimize = b.standardOptimizeOption(.{}); 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen const enable_libcurl = b.option(bool, "libcurl", "enable linking with libcurl") orelse false; 82ee6a1f  Gregory Mullen const options = b.addOptions(); 82ee6a1f  Gregory Mullen options.addOption(bool, "libcurl", enable_libcurl); 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen var binaries = std.ArrayList(*std.Build.Step.Compile).init(b.allocator); 82ee6a1f  Gregory Mullen defer binaries.clearAndFree(); 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen const templates_compiled = try compileTemplates(b); 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen const exe = b.addExecutable(.{ 82ee6a1f  Gregory Mullen .name = "flagpost", 82ee6a1f  Gregory Mullen .root_source_file = .{ 82ee6a1f  Gregory Mullen .path = "src/main.zig", 82ee6a1f  Gregory Mullen }, 82ee6a1f  Gregory Mullen .target = target, 82ee6a1f  Gregory Mullen .optimize = optimize, 82ee6a1f  Gregory Mullen }); 82ee6a1f  Gregory Mullen b.installArtifact(exe); 82ee6a1f  Gregory Mullen binaries.append(exe) catch unreachable; 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen const run_cmd = b.addRunArtifact(exe); 82ee6a1f  Gregory Mullen run_cmd.step.dependOn(b.getInstallStep()); 82ee6a1f  Gregory Mullen if (b.args) |args| { 82ee6a1f  Gregory Mullen run_cmd.addArgs(args); 82ee6a1f  Gregory Mullen } 82ee6a1f  Gregory Mullen const run_step = b.step("run", "Run the app"); 82ee6a1f  Gregory Mullen run_step.dependOn(&run_cmd.step); 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen const unit_tests = b.addTest(.{ 82ee6a1f  Gregory Mullen .root_source_file = .{ .path = "src/main.zig" }, 82ee6a1f  Gregory Mullen .target = target, 82ee6a1f  Gregory Mullen .optimize = optimize, 82ee6a1f  Gregory Mullen }); 82ee6a1f  Gregory Mullen binaries.append(unit_tests) catch unreachable; 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen for (binaries.items) |ex| { 82ee6a1f  Gregory Mullen ex.root_module.addImport("templates-compiled", templates_compiled); 82ee6a1f  Gregory Mullen ex.root_module.addOptions("config", options); 82ee6a1f  Gregory Mullen if (enable_libcurl) { 82ee6a1f  Gregory Mullen ex.linkSystemLibrary2("curl", .{ .preferred_link_mode = .static }); 82ee6a1f  Gregory Mullen ex.linkLibC(); 82ee6a1f  Gregory Mullen } 82ee6a1f  Gregory Mullen } 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen const run_unit_tests = b.addRunArtifact(unit_tests); 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen const test_step = b.step("test", "Run unit tests"); 82ee6a1f  Gregory Mullen test_step.dependOn(&run_unit_tests.step); 82ee6a1f  Gregory Mullen } 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen fn compileTemplates(b: *std.Build) !*std.Build.Module { 82ee6a1f  Gregory Mullen const compiled = b.addModule("templates-compiled", .{ 82ee6a1f  Gregory Mullen .root_source_file = .{ 82ee6a1f  Gregory Mullen .path = "src/template-compiled.zig", 82ee6a1f  Gregory Mullen }, 82ee6a1f  Gregory Mullen }); 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen const list = buildSrcTemplates(b) catch @panic("unable to build src files"); 82ee6a1f  Gregory Mullen const found = b.addOptions(); 82ee6a1f  Gregory Mullen found.addOption( 82ee6a1f  Gregory Mullen []const []const u8, 82ee6a1f  Gregory Mullen "names", 82ee6a1f  Gregory Mullen list, 82ee6a1f  Gregory Mullen ); 82ee6a1f  Gregory Mullen compiled.addOptions("found_templates", found); 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen for (list) |file| { 82ee6a1f  Gregory Mullen _ = compiled.addAnonymousImport(file, .{ 82ee6a1f  Gregory Mullen .root_source_file = .{ .path = file }, 82ee6a1f  Gregory Mullen }); 82ee6a1f  Gregory Mullen } 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen return compiled; 82ee6a1f  Gregory Mullen } 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen var template_list: ?[][]const u8 = null; 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen fn buildSrcTemplates(b: *std.Build) ![][]const u8 { 82ee6a1f  Gregory Mullen if (template_list) |tl| return tl; 82ee6a1f  Gregory Mullen 82ee6a1f  Gregory Mullen const tmplsrcdir = "templates"; 82ee6a1f  Gregory Mullen var cwd = std.fs.cwd(); 82ee6a1f  Gregory Mullen var idir = cwd.openDir(tmplsrcdir, .{ .iterate = true }) catch |err| { 82ee6a1f  Gregory Mullen std.debug.print("template build error {}", .{err}); 82ee6a1f  Gregory Mullen return err; 82ee6a1f  Gregory Mullen }; 82ee6a1f  Gregory Mullen var arrlist = std.ArrayList([]const u8).init(b.allocator); 82ee6a1f  Gregory Mullen var itr = idir.iterate(); 82ee6a1f  Gregory Mullen while (try itr.next()) |file| { 82ee6a1f  Gregory Mullen if (!std.mem.endsWith(u8, file.name, ".html")) continue; 82ee6a1f  Gregory Mullen try arrlist.append(b.pathJoin(&[2][]const u8{ tmplsrcdir, file.name })); 82ee6a1f  Gregory Mullen } 82ee6a1f  Gregory Mullen template_list = try arrlist.toOwnedSlice(); 82ee6a1f  Gregory Mullen return template_list.?; 82ee6a1f  Gregory Mullen }