馃Ζ a tiny cute lua web framework
1const std = @import("std");
2
3var target: std.Build.ResolvedTarget = undefined;
4var optimize: std.builtin.OptimizeMode = undefined;
5
6pub fn build(b: *std.Build) !void {
7 target = b.standardTargetOptions(.{});
8 optimize = b.standardOptimizeOption(.{});
9
10 const mod = b.addModule("maivi", .{
11 .root_source_file = b.path("src/root.zig"),
12 .target = target,
13 .optimize = optimize,
14 });
15
16 const exe = b.addExecutable(.{
17 .name = "maivi",
18 .root_module = b.createModule(.{
19 .root_source_file = b.path("src/main.zig"),
20 .target = target,
21 .optimize = optimize,
22 .imports = &.{
23 .{ .name = "maivi", .module = mod },
24 },
25 }),
26 });
27
28 const mlua_exe = try build_mlua(b, mod);
29
30 // deps =======================================================================
31
32 const libc = libc: {
33 break :libc std.zig.LibCDirs.detect(
34 b.graph.arena,
35 b.graph.io,
36 b.graph.zig_lib_directory.path orelse break :libc error.NoZigLib,
37 &target.result,
38 target.query.isNativeAbi(),
39 true,
40 null,
41 &b.graph.environ_map,
42 );
43 } catch |err| std.debug.panic("failed to locate libc: {s}", .{@errorName(err)});
44
45 var include_paths: std.ArrayList(std.Build.LazyPath) = try .initCapacity(b.allocator, libc.libc_include_dir_list.len);
46 for (libc.libc_include_dir_list) |include_dir|
47 try include_paths.append(b.allocator, .{ .cwd_relative = include_dir });
48
49 const zlua = b.dependency("zlua", .{
50 .target = target,
51 .optimize = optimize,
52 .lang = .luajit,
53 .additional_system_headers = try include_paths.toOwnedSlice(b.allocator),
54 });
55
56 mod.addImport("zlua", zlua.module("zlua"));
57 exe.root_module.addImport("zlua", zlua.module("zlua"));
58 mlua_exe.root_module.addImport("zlua", zlua.module("zlua"));
59
60 const fern = b.dependency("fern", .{
61 .target = target,
62 .optimize = optimize,
63 });
64
65 mod.addImport("fern", fern.module("fern"));
66 exe.root_module.addImport("fern", fern.module("fern"));
67 mlua_exe.root_module.addImport("fern", fern.module("fern"));
68
69 const httpz = b.dependency("httpz", .{
70 .target = target,
71 .optimize = optimize,
72 });
73
74 mod.addImport("httpz", httpz.module("httpz"));
75 exe.root_module.addImport("httpz", httpz.module("httpz"));
76
77 // exe ====================================================================
78
79 b.installArtifact(exe);
80
81 const run_step = b.step("run", "Run");
82
83 const run_cmd = b.addRunArtifact(exe);
84 run_step.dependOn(&run_cmd.step);
85
86 run_cmd.step.dependOn(b.getInstallStep());
87
88 if (b.args) |args| {
89 run_cmd.addArgs(args);
90 }
91
92 // tests ======================================================================
93
94 const mod_tests = b.addTest(.{
95 .root_module = mod,
96 });
97
98 const run_mod_tests = b.addRunArtifact(mod_tests);
99
100 const exe_tests = b.addTest(.{
101 .root_module = exe.root_module,
102 });
103
104 const run_exe_tests = b.addRunArtifact(exe_tests);
105
106 const test_step = b.step("test", "Run tests");
107 test_step.dependOn(&run_mod_tests.step);
108 test_step.dependOn(&run_exe_tests.step);
109}
110
111pub fn build_mlua(b: *std.Build, mod: *std.Build.Module) !*std.Build.Step.Compile {
112 const mlua_exe = b.addExecutable(.{
113 .name = "mlua",
114 .root_module = b.createModule(.{
115 .root_source_file = b.path("src/mlua.zig"),
116 .target = target,
117 .optimize = optimize,
118 .imports = &.{
119 .{ .name = "maivi", .module = mod },
120 },
121 }),
122 });
123
124 // for debugging the mlua environment
125 // like this: `zig build mlua -- script.lua {args}`
126 const run_cmd = b.addRunArtifact(mlua_exe);
127 if (b.args) |args| {
128 run_cmd.addArgs(args);
129 }
130 const run_step = b.step("mlua", "Run mlua build tool");
131 run_step.dependOn(&run_cmd.step);
132
133 b.installArtifact(mlua_exe);
134
135 return mlua_exe;
136}