robin/maivi

a tiny cute lua web framework

git clone https://git.robinwobin.dev/robin/maivi

robinbuild: prefix paths61e6d44

push-srxvuswpkqxx
5.9 KiB173 linesraw
1const std = @import("std");
2
3pub fn build(b: *std.Build) !void {
4    const target = b.standardTargetOptions(.{});
5    const optimize = b.standardOptimizeOption(.{});
6
7    // options ====================================================================
8
9    const options = b.addOptions();
10
11    const prefix_runtime_path = b.getInstallPath(.prefix, "share/maivi/runtime");
12    const prefix_runtime_fallback = "/usr/local/share/maivi/runtime";
13    const use_runtime = b.option(bool, "runtime", "use a runtime directory instead of compiling the lua code") orelse false;
14    const runtime_path = b.option([]const u8, "runtimepath", "path where the runtime will be installed by the packager") orelse blk: {
15        if (prefix_runtime_path[0] == '/') break :blk prefix_runtime_path;
16        std.log.warn("prefix path is not absolute. maivi runtime will fallback to {s}", .{prefix_runtime_fallback});
17        break :blk prefix_runtime_fallback;
18    };
19    const opt_md4c = b.option(bool, "md4c", "md4c support") orelse false;
20
21    const use_llvm = b.option(bool, "llvm", "llvm");
22
23    options.addOption(bool, "runtime", use_runtime);
24    options.addOption([]const u8, "runtimepath", runtime_path);
25    options.addOption(bool, "md4c", opt_md4c);
26
27    const mod = b.addModule("maivi", .{
28        .root_source_file = b.path("src/root.zig"),
29        .target = target,
30        .optimize = optimize,
31    });
32
33    mod.addOptions("options", options);
34
35    // executables ================================================================
36
37    const exe = b.addExecutable(.{
38        .name = "maivi",
39        .use_llvm = use_llvm,
40        .root_module = b.createModule(.{
41            .root_source_file = b.path("src/main.zig"),
42            .target = target,
43            .optimize = optimize,
44            .imports = &.{
45                .{ .name = "maivi", .module = mod },
46            },
47        }),
48    });
49
50    b.installArtifact(exe);
51    const run_step = b.step("run", "Run");
52    const run_cmd = b.addRunArtifact(exe);
53    run_step.dependOn(&run_cmd.step);
54    run_cmd.step.dependOn(b.getInstallStep());
55    if (b.args) |args| run_cmd.addArgs(args);
56
57    const mlua_exe = try build_mlua(b, b.createModule(.{
58        .root_source_file = b.path("src/mlua.zig"),
59        .target = target,
60        .optimize = optimize,
61        .imports = &.{.{ .name = "maivi", .module = mod }},
62    }));
63
64    // dependencies ===============================================================
65
66    const libc = libc: {
67        break :libc std.zig.LibCDirs.detect(
68            b.graph.arena,
69            b.graph.io,
70            b.graph.zig_lib_directory.path orelse break :libc error.NoZigLib,
71            &target.result,
72            target.query.isNativeAbi(),
73            true,
74            null,
75            &b.graph.environ_map,
76        );
77    } catch |err| std.debug.panic("failed to locate libc: {s}", .{@errorName(err)});
78
79    var include_paths: std.ArrayList(std.Build.LazyPath) = try .initCapacity(b.allocator, libc.libc_include_dir_list.len);
80    for (libc.libc_include_dir_list) |include_dir|
81        try include_paths.append(b.allocator, .{ .cwd_relative = include_dir });
82
83    const zlua = b.dependency("zlua", .{
84        .target = target,
85        .optimize = optimize,
86        .lang = .luajit,
87        .additional_system_headers = try include_paths.toOwnedSlice(b.allocator),
88    });
89
90    mod.addImport("zlua", zlua.module("zlua"));
91    exe.root_module.addImport("zlua", zlua.module("zlua"));
92    mlua_exe.root_module.addImport("zlua", zlua.module("zlua"));
93
94    const inspect_lua = b.dependency("inspect_lua", .{});
95
96    const meow = b.addWriteFiles();
97    _ = meow.addCopyFile(inspect_lua.path("inspect.lua"), "inspect.lua");
98    const meow_f = meow.add("inspect_lua.zig",
99        \\pub const inspect_module = @embedFile("inspect.lua");
100    );
101    mod.addAnonymousImport("inspect_lua", .{
102        .root_source_file = meow_f,
103    });
104
105    const fern = b.dependency("fern", .{
106        .target = target,
107        .optimize = optimize,
108    });
109
110    mod.addImport("fern", fern.module("fern"));
111    exe.root_module.addImport("fern", fern.module("fern"));
112    mlua_exe.root_module.addImport("fern", fern.module("fern"));
113
114    if (opt_md4c) {
115        const md4c = b.addTranslateC(.{
116            .root_source_file = b.path("src/md4c.h"),
117            .target = target,
118            .optimize = optimize,
119        });
120        md4c.linkSystemLibrary("md4c", .{});
121        md4c.linkSystemLibrary("md4c-html", .{});
122        const md4c_mod = md4c.createModule();
123        mod.addImport("md4c", md4c_mod);
124    }
125
126    // runtime ================================================================
127
128    if (use_runtime) {
129        const runtime = b.addWriteFiles();
130
131        const runtimedir = runtime.addCopyDirectory(b.path("src/lua/runtime"), "runtime", .{
132            .include_extensions = &.{"lua"},
133        });
134
135        b.installDirectory(.{
136            .install_dir = .prefix,
137            .install_subdir = "share/maivi/runtime",
138            .source_dir = runtimedir,
139        });
140    }
141
142    // tests ======================================================================
143
144    const mod_tests = b.addTest(.{
145        .root_module = mod,
146    });
147
148    const run_mod_tests = b.addRunArtifact(mod_tests);
149
150    const exe_tests = b.addTest(.{
151        .root_module = exe.root_module,
152    });
153
154    const run_exe_tests = b.addRunArtifact(exe_tests);
155
156    const test_step = b.step("test", "Run tests");
157    test_step.dependOn(&run_mod_tests.step);
158    test_step.dependOn(&run_exe_tests.step);
159}
160
161pub fn build_mlua(b: *std.Build, mod: *std.Build.Module) !*std.Build.Step.Compile {
162    const mlua_exe = b.addExecutable(.{ .name = "mlua", .root_module = mod });
163
164    // for debugging the mlua environment
165    // like this: `zig build mlua -- script.lua {args}`
166    const run_cmd = b.addRunArtifact(mlua_exe);
167    if (b.args) |args| run_cmd.addArgs(args);
168    const run_step = b.step("mlua", "Run mlua build tool");
169    run_step.dependOn(&run_cmd.step);
170    b.installArtifact(mlua_exe);
171
172    return mlua_exe;
173}