1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
const std = @import("std");
const build = @import("../build.zig");
const LazyPath = std.Build.LazyPath;
pub fn build_nlua0(
b: *std.Build,
io: std.Io,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
use_luajit: bool,
ziglua: *std.Build.Dependency,
lpeg: ?*std.Build.Dependency,
libluv: ?*std.Build.Step.Compile,
system_integration_options: build.SystemIntegrationOptions,
) !*std.Build.Step.Compile {
const options = b.addOptions();
options.addOption(bool, "use_luajit", use_luajit);
const nlua0_exe = b.addExecutable(.{
.name = "nlua0",
.root_module = b.createModule(.{
.root_source_file = b.path("src/nlua0.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
const nlua0_mod = nlua0_exe.root_module;
const exe_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/nlua0.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
const embedded_data = b.addModule("embedded_data", .{
.root_source_file = b.path("runtime/embedded_data.zig"),
});
for ([2]*std.Build.Module{ nlua0_mod, exe_unit_tests.root_module }) |mod| {
mod.addImport("ziglua", ziglua.module("zlua"));
mod.addImport("embedded_data", embedded_data);
// addImport already links by itself. but we need headers as well..
if (system_integration_options.lua) {
const system_lua_lib = if (use_luajit) "luajit" else "lua5.1";
mod.linkSystemLibrary(system_lua_lib, .{});
} else {
mod.linkLibrary(ziglua.artifact("lua"));
}
if (libluv) |luv| {
mod.linkLibrary(luv);
} else {
mod.linkSystemLibrary("luv", .{});
}
mod.addOptions("options", options);
mod.addIncludePath(b.path("src"));
mod.addIncludePath(b.path("src/includes_fixmelater"));
try add_lua_modules(b, io, target.result, mod, lpeg, use_luajit, true, system_integration_options);
}
// for debugging the nlua0 environment
// like this: `zig build nlua0 -- script.lua {args}`
const run_cmd = b.addRunArtifact(nlua0_exe);
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("nlua0", "Run nlua0 build tool");
run_step.dependOn(&run_cmd.step);
b.installArtifact(nlua0_exe); // DEBUG
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test_nlua0", "Run unit tests for nlua0");
test_step.dependOn(&run_exe_unit_tests.step);
return nlua0_exe;
}
pub fn add_lua_modules(
b: *std.Build,
io: std.Io,
target: std.Target,
mod: *std.Build.Module,
lpeg_dep: ?*std.Build.Dependency,
use_luajit: bool,
is_nlua0: bool,
system_integration_options: build.SystemIntegrationOptions,
) !void {
const flags = [_][]const u8{
// Standard version used in Lua Makefile
"-std=gnu99",
if (is_nlua0) "-DNVIM_NLUA0" else "",
};
if (lpeg_dep) |lpeg| {
mod.addIncludePath(lpeg.path(""));
}
mod.addCSourceFiles(.{
.files = &.{
"src/mpack/lmpack.c",
"src/mpack/mpack_core.c",
"src/mpack/object.c",
"src/mpack/conv.c",
"src/mpack/rpc.c",
},
.flags = &flags,
});
if (system_integration_options.lpeg) {
if (try findLpeg(b, io, target)) |lpeg_lib| {
mod.addLibraryPath(.{ .cwd_relative = std.fs.path.dirname(lpeg_lib).? });
mod.addObjectFile(.{ .cwd_relative = lpeg_lib });
}
} else if (lpeg_dep) |lpeg| {
mod.addCSourceFiles(.{
.root = .{ .dependency = .{ .dependency = lpeg, .sub_path = "" } },
.files = &.{
"lpcap.c",
"lpcode.c",
"lpcset.c",
"lpprint.c",
"lptree.c",
"lpvm.c",
},
.flags = &flags,
});
}
if (!use_luajit) {
mod.addCSourceFiles(.{
.files = &.{
"src/bit.c",
},
.flags = &flags,
});
}
}
pub fn build_libluv(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
lua: ?*std.Build.Step.Compile,
libuv: *std.Build.Step.Compile,
use_luajit: bool,
) !*std.Build.Step.Compile {
const upstream = b.lazyDependency("luv", .{});
const compat53 = b.lazyDependency("lua_compat53", .{});
var root_module = b.createModule(.{
.target = target,
.optimize = optimize,
});
const lib = b.addLibrary(.{
.name = "luv",
.linkage = .static,
.root_module = root_module,
});
if (lua) |lua_lib| {
root_module.linkLibrary(lua_lib);
} else {
const system_lua_lib = if (use_luajit) "luajit" else "lua5.1";
root_module.linkSystemLibrary(system_lua_lib, .{});
}
root_module.linkLibrary(libuv);
if (upstream) |dep| {
root_module.addIncludePath(dep.path("src"));
lib.installHeader(dep.path("src/luv.h"), "luv/luv.h");
root_module.addCSourceFiles(.{ .root = dep.path("src/"), .files = &.{
"luv.c",
} });
}
if (compat53) |dep| {
root_module.addIncludePath(dep.path("c-api"));
root_module.addCSourceFiles(.{ .root = dep.path("c-api"), .files = &.{
"compat-5.3.c",
} });
}
return lib;
}
fn findLpeg(b: *std.Build, io: std.Io, target: std.Target) !?[]const u8 {
const filenames = [_][]const u8{
"lpeg_a",
"lpeg",
"liblpeg_a",
"lpeg.so",
b.fmt("lpeg{s}", .{target.dynamicLibSuffix()}),
};
var code: u8 = 0;
const dirs_stdout = std.mem.trimEnd(u8, try b.runAllowFail(&[_][]const u8{
"pkg-config",
"--variable=pc_system_libdirs",
"--keep-system-cflags",
"pkg-config",
}, &code, .ignore), "\r\n");
var paths: std.ArrayList([]const u8) = try .initCapacity(b.allocator, 0);
var path_it = std.mem.tokenizeAny(u8, dirs_stdout, " ,");
while (path_it.next()) |dir| {
try paths.append(b.allocator, dir);
try paths.append(b.allocator, b.fmt("{s}/lua/5.1", .{dir}));
}
for (paths.items) |path| {
var dir = std.Io.Dir.openDirAbsolute(io, path, .{}) catch continue;
defer dir.close(io);
for (filenames) |filename| {
dir.access(io, filename, .{}) catch continue;
return b.fmt("{s}/{s}", .{ path, filename });
}
}
return null;
}
|