const std = @import("std"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); // options ==================================================================== const options = b.addOptions(); const prefix_runtime_path = b.getInstallPath(.prefix, "share/maivi/runtime"); const prefix_runtime_fallback = "/usr/local/share/maivi/runtime"; const use_runtime = b.option(bool, "runtime", "use a runtime directory instead of compiling the lua code") orelse false; const runtime_path = b.option([]const u8, "runtimepath", "path where the runtime will be installed by the packager") orelse blk: { if (prefix_runtime_path[0] == '/') break :blk prefix_runtime_path; std.log.warn("prefix path is not absolute. maivi runtime will fallback to {s}", .{prefix_runtime_fallback}); break :blk prefix_runtime_fallback; }; const opt_md4c = b.option(bool, "md4c", "md4c support") orelse false; const use_llvm = b.option(bool, "llvm", "llvm"); options.addOption(bool, "runtime", use_runtime); options.addOption([]const u8, "runtimepath", runtime_path); options.addOption(bool, "md4c", opt_md4c); const mod = b.addModule("maivi", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); mod.addOptions("options", options); // executables ================================================================ const exe = b.addExecutable(.{ .name = "maivi", .use_llvm = use_llvm, .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "maivi", .module = mod }, }, }), }); b.installArtifact(exe); const run_step = b.step("run", "Run"); const run_cmd = b.addRunArtifact(exe); run_step.dependOn(&run_cmd.step); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| run_cmd.addArgs(args); const mlua_exe = try build_mlua(b, b.createModule(.{ .root_source_file = b.path("src/mlua.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "maivi", .module = mod }}, })); // dependencies =============================================================== const libc = libc: { break :libc std.zig.LibCDirs.detect( b.graph.arena, b.graph.io, b.graph.zig_lib_directory.path orelse break :libc error.NoZigLib, &target.result, target.query.isNativeAbi(), true, null, &b.graph.environ_map, ); } catch |err| std.debug.panic("failed to locate libc: {s}", .{@errorName(err)}); var include_paths: std.ArrayList(std.Build.LazyPath) = try .initCapacity(b.allocator, libc.libc_include_dir_list.len); for (libc.libc_include_dir_list) |include_dir| try include_paths.append(b.allocator, .{ .cwd_relative = include_dir }); const zlua = b.dependency("zlua", .{ .target = target, .optimize = optimize, .lang = .luajit, .additional_system_headers = try include_paths.toOwnedSlice(b.allocator), }); mod.addImport("zlua", zlua.module("zlua")); exe.root_module.addImport("zlua", zlua.module("zlua")); mlua_exe.root_module.addImport("zlua", zlua.module("zlua")); const inspect_lua = b.dependency("inspect_lua", .{}); const meow = b.addWriteFiles(); _ = meow.addCopyFile(inspect_lua.path("inspect.lua"), "inspect.lua"); const meow_f = meow.add("inspect_lua.zig", \\pub const inspect_module = @embedFile("inspect.lua"); ); mod.addAnonymousImport("inspect_lua", .{ .root_source_file = meow_f, }); const fern = b.dependency("fern", .{ .target = target, .optimize = optimize, }); mod.addImport("fern", fern.module("fern")); exe.root_module.addImport("fern", fern.module("fern")); mlua_exe.root_module.addImport("fern", fern.module("fern")); if (opt_md4c) { const md4c = b.addTranslateC(.{ .root_source_file = b.path("src/md4c.h"), .target = target, .optimize = optimize, }); md4c.linkSystemLibrary("md4c", .{}); md4c.linkSystemLibrary("md4c-html", .{}); const md4c_mod = md4c.createModule(); mod.addImport("md4c", md4c_mod); } // runtime ================================================================ if (use_runtime) { const runtime = b.addWriteFiles(); const runtimedir = runtime.addCopyDirectory(b.path("src/lua/runtime"), "runtime", .{ .include_extensions = &.{"lua"}, }); b.installDirectory(.{ .install_dir = .prefix, .install_subdir = "share/maivi/runtime", .source_dir = runtimedir, }); } // tests ====================================================================== const mod_tests = b.addTest(.{ .root_module = mod, }); const run_mod_tests = b.addRunArtifact(mod_tests); const exe_tests = b.addTest(.{ .root_module = exe.root_module, }); const run_exe_tests = b.addRunArtifact(exe_tests); const test_step = b.step("test", "Run tests"); test_step.dependOn(&run_mod_tests.step); test_step.dependOn(&run_exe_tests.step); } pub fn build_mlua(b: *std.Build, mod: *std.Build.Module) !*std.Build.Step.Compile { const mlua_exe = b.addExecutable(.{ .name = "mlua", .root_module = mod }); // for debugging the mlua environment // like this: `zig build mlua -- script.lua {args}` const run_cmd = b.addRunArtifact(mlua_exe); if (b.args) |args| run_cmd.addArgs(args); const run_step = b.step("mlua", "Run mlua build tool"); run_step.dependOn(&run_cmd.step); b.installArtifact(mlua_exe); return mlua_exe; }