robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
4.9 KiB170 linesraw
1const std = @import("std");
2const build = @import("../build.zig");
3const LazyPath = std.Build.LazyPath;
4
5pub fn build_nlua0(
6    b: *std.Build,
7    target: std.Build.ResolvedTarget,
8    optimize: std.builtin.OptimizeMode,
9    use_luajit: bool,
10    ziglua: *std.Build.Dependency,
11    libluv: ?*std.Build.Step.Compile,
12    system_integration_options: build.SystemIntegrationOptions,
13) !*std.Build.Step.Compile {
14    const options = b.addOptions();
15    options.addOption(bool, "use_luajit", use_luajit);
16
17    const nlua0_exe = b.addExecutable(.{
18        .name = "nlua0",
19        .root_module = b.createModule(.{
20            .root_source_file = b.path("src/nlua0.zig"),
21            .target = target,
22            .optimize = optimize,
23            .link_libc = true,
24        }),
25        .use_llvm = true,
26    });
27    const nlua0_mod = nlua0_exe.root_module;
28
29    const exe_unit_tests = b.addTest(.{
30        .root_module = b.createModule(.{
31            .root_source_file = b.path("src/nlua0.zig"),
32            .target = target,
33            .optimize = optimize,
34            .link_libc = true,
35        }),
36    });
37
38    const embedded_data = b.addModule("embedded_data", .{
39        .root_source_file = b.path("runtime/embedded_data.zig"),
40    });
41
42    for ([2]*std.Build.Module{ nlua0_mod, exe_unit_tests.root_module }) |mod| {
43        mod.addImport("ziglua", ziglua.module("zlua"));
44        mod.addImport("embedded_data", embedded_data);
45        // addImport already links by itself. but we need headers as well..
46        if (system_integration_options.lua) {
47            const system_lua_lib = if (use_luajit) "luajit" else "lua5.1";
48            mod.linkSystemLibrary(system_lua_lib, .{});
49        } else {
50            mod.linkLibrary(ziglua.artifact("lua"));
51        }
52        if (libluv) |luv| {
53            mod.linkLibrary(luv);
54        } else {
55            mod.linkSystemLibrary("luv", .{});
56        }
57
58        mod.addOptions("options", options);
59
60        mod.addIncludePath(b.path("src"));
61        mod.addIncludePath(b.path("src/includes_fixmelater"));
62        try add_lua_modules(mod, use_luajit, true);
63    }
64
65    // for debugging the nlua0 environment
66    // like this: `zig build nlua0 -- script.lua {args}`
67    const run_cmd = b.addRunArtifact(nlua0_exe);
68    if (b.args) |args| {
69        run_cmd.addArgs(args);
70    }
71    const run_step = b.step("nlua0", "Run nlua0 build tool");
72    run_step.dependOn(&run_cmd.step);
73
74    b.installArtifact(nlua0_exe); // DEBUG
75
76    const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
77
78    const test_step = b.step("test_nlua0", "Run unit tests for nlua0");
79    test_step.dependOn(&run_exe_unit_tests.step);
80
81    return nlua0_exe;
82}
83
84pub fn add_lua_modules(
85    mod: *std.Build.Module,
86    use_luajit: bool,
87    is_nlua0: bool,
88) !void {
89    const flags = [_][]const u8{
90        // Standard version used in Lua Makefile
91        "-std=gnu99",
92        if (is_nlua0) "-DNVIM_NLUA0" else "",
93    };
94
95    mod.addCSourceFiles(.{
96        .files = &.{
97            "src/mpack/lmpack.c",
98            "src/mpack/mpack_core.c",
99            "src/mpack/object.c",
100            "src/mpack/conv.c",
101            "src/mpack/rpc.c",
102        },
103        .flags = &flags,
104    });
105    mod.addCSourceFiles(.{
106        .files = &.{
107            "src/lpeg/lpcap.c",
108            "src/lpeg/lpcode.c",
109            "src/lpeg/lpcset.c",
110            "src/lpeg/lpprint.c",
111            "src/lpeg/lptree.c",
112            "src/lpeg/lpvm.c",
113        },
114        .flags = &flags,
115    });
116
117    if (!use_luajit) {
118        mod.addCSourceFiles(.{
119            .files = &.{
120                "src/bit.c",
121            },
122            .flags = &flags,
123        });
124    }
125}
126
127pub fn build_libluv(
128    b: *std.Build,
129    target: std.Build.ResolvedTarget,
130    optimize: std.builtin.OptimizeMode,
131    lua: ?*std.Build.Step.Compile,
132    libuv: *std.Build.Step.Compile,
133    use_luajit: bool,
134) !*std.Build.Step.Compile {
135    const upstream = b.lazyDependency("luv", .{});
136    const compat53 = b.lazyDependency("lua_compat53", .{});
137    var root_module = b.createModule(.{
138        .target = target,
139        .optimize = optimize,
140    });
141    const lib = b.addLibrary(.{
142        .name = "luv",
143        .linkage = .static,
144        .root_module = root_module,
145    });
146
147    if (lua) |lua_lib| {
148        root_module.linkLibrary(lua_lib);
149    } else {
150        const system_lua_lib = if (use_luajit) "luajit" else "lua5.1";
151        root_module.linkSystemLibrary(system_lua_lib, .{});
152    }
153    root_module.linkLibrary(libuv);
154
155    if (upstream) |dep| {
156        root_module.addIncludePath(dep.path("src"));
157        lib.installHeader(dep.path("src/luv.h"), "luv/luv.h");
158        root_module.addCSourceFiles(.{ .root = dep.path("src/"), .files = &.{
159            "luv.c",
160        } });
161    }
162    if (compat53) |dep| {
163        root_module.addIncludePath(dep.path("c-api"));
164        root_module.addCSourceFiles(.{ .root = dep.path("c-api"), .files = &.{
165            "compat-5.3.c",
166        } });
167    }
168
169    return lib;
170}