2022-06-01 09:09:27 +08:00
|
|
|
const std = @import("std");
|
|
|
|
|
2023-02-16 06:05:53 +08:00
|
|
|
const Build = std.Build;
|
|
|
|
const CompileStep = std.build.CompileStep;
|
2022-06-03 09:41:57 +08:00
|
|
|
|
2022-12-08 21:16:54 +08:00
|
|
|
pub const LuaVersion = enum {
|
2022-10-04 10:20:17 +08:00
|
|
|
lua_51,
|
|
|
|
lua_52,
|
|
|
|
lua_53,
|
|
|
|
lua_54,
|
2022-10-21 04:25:34 +08:00
|
|
|
// lua_jit,
|
2022-10-04 10:20:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
fn libPath(version: LuaVersion) []const u8 {
|
|
|
|
return switch (version) {
|
|
|
|
.lua_51 => "src/ziglua-5.1/lib.zig",
|
|
|
|
.lua_52 => "src/ziglua-5.2/lib.zig",
|
|
|
|
.lua_53 => "src/ziglua-5.3/lib.zig",
|
|
|
|
.lua_54 => "src/ziglua-5.4/lib.zig",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-16 06:05:53 +08:00
|
|
|
pub fn build(b: *Build) void {
|
2022-10-04 10:20:17 +08:00
|
|
|
const version = b.option(LuaVersion, "version", "lua version to test") orelse .lua_54;
|
2022-06-01 09:09:27 +08:00
|
|
|
|
2023-02-16 06:05:53 +08:00
|
|
|
const tests = b.addTest(.{
|
|
|
|
.root_source_file = switch (version) {
|
|
|
|
.lua_51 => .{ .path = "src/ziglua-5.1/tests.zig" },
|
|
|
|
.lua_52 => .{ .path = "src/ziglua-5.2/tests.zig" },
|
|
|
|
.lua_53 => .{ .path = "src/ziglua-5.3/tests.zig" },
|
|
|
|
.lua_54 => .{ .path = "src/ziglua-5.4/tests.zig" },
|
|
|
|
},
|
2022-10-04 10:20:17 +08:00
|
|
|
});
|
2022-10-21 04:25:34 +08:00
|
|
|
link(b, tests, .{ .use_apicheck = true, .version = version });
|
2022-06-01 09:09:27 +08:00
|
|
|
|
2023-02-16 06:43:43 +08:00
|
|
|
const test_step = b.step("test", "Run ziglua tests");
|
2022-06-01 09:09:27 +08:00
|
|
|
test_step.dependOn(&tests.step);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dir() []const u8 {
|
|
|
|
return std.fs.path.dirname(@src().file) orelse ".";
|
|
|
|
}
|
|
|
|
|
2022-12-08 21:16:54 +08:00
|
|
|
pub const Options = struct {
|
2022-06-03 09:41:57 +08:00
|
|
|
/// Defines the macro LUA_USE_APICHECK in debug builds
|
|
|
|
use_apicheck: bool = false,
|
2022-10-04 10:20:17 +08:00
|
|
|
/// Defines the Lua version to build and link
|
|
|
|
version: LuaVersion = .lua_54,
|
2022-10-21 04:25:34 +08:00
|
|
|
|
|
|
|
shared: bool = false,
|
2022-06-03 09:41:57 +08:00
|
|
|
};
|
|
|
|
|
2023-02-16 06:38:04 +08:00
|
|
|
pub fn compileAndCreateModule(b: *Build, step: *CompileStep, options: Options) *std.build.Module {
|
2022-10-21 04:25:34 +08:00
|
|
|
link(b, step, options);
|
2022-10-04 10:20:17 +08:00
|
|
|
|
2022-10-21 04:25:34 +08:00
|
|
|
const lib_path = libPath(options.version);
|
2023-02-16 06:38:04 +08:00
|
|
|
return b.createModule(.{
|
|
|
|
.source_file = .{ .path = std.fs.path.join(b.allocator, &.{ dir(), lib_path }) catch unreachable },
|
|
|
|
});
|
2022-10-04 10:20:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: expose the link and package steps separately for advanced use cases?
|
2023-02-16 06:05:53 +08:00
|
|
|
fn link(b: *Build, step: *CompileStep, options: Options) void {
|
2022-10-28 11:27:56 +08:00
|
|
|
const lua = buildLua(b, step, options);
|
|
|
|
step.linkLibrary(lua);
|
2022-06-01 09:09:27 +08:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:20:17 +08:00
|
|
|
// TODO: how to test all versions? May need a make/help script to test all
|
|
|
|
// versions separately because there might be name collisions
|
2023-02-16 06:05:53 +08:00
|
|
|
fn buildLua(b: *Build, step: *CompileStep, options: Options) *CompileStep {
|
2022-10-04 10:20:17 +08:00
|
|
|
const lib_dir = switch (options.version) {
|
|
|
|
.lua_51 => "lib/lua-5.1.5/src/",
|
|
|
|
.lua_52 => "lib/lua-5.2.4/src/",
|
|
|
|
.lua_53 => "lib/lua-5.3.6/src/",
|
|
|
|
.lua_54 => "lib/lua-5.4.4/src/",
|
|
|
|
};
|
2022-06-01 09:09:27 +08:00
|
|
|
|
2022-10-21 04:25:34 +08:00
|
|
|
const lua = brk: {
|
2023-02-16 06:05:53 +08:00
|
|
|
if (options.shared) break :brk b.addSharedLibrary(.{
|
|
|
|
.name = "lua",
|
|
|
|
.target = step.target,
|
|
|
|
.optimize = step.optimize,
|
|
|
|
});
|
|
|
|
|
|
|
|
break :brk b.addStaticLibrary(.{
|
|
|
|
.name = "lua",
|
|
|
|
.target = step.target,
|
|
|
|
.optimize = step.optimize,
|
|
|
|
});
|
2022-10-21 04:25:34 +08:00
|
|
|
};
|
2023-02-16 06:05:53 +08:00
|
|
|
|
2022-10-28 11:27:56 +08:00
|
|
|
lua.linkLibC();
|
2022-06-01 09:09:27 +08:00
|
|
|
|
2023-02-16 06:05:53 +08:00
|
|
|
const apicheck = step.optimize == .Debug and options.use_apicheck;
|
2022-06-03 09:41:57 +08:00
|
|
|
|
2022-10-28 10:46:35 +08:00
|
|
|
step.addIncludePath(std.fs.path.join(b.allocator, &.{ dir(), lib_dir }) catch unreachable);
|
2022-06-01 09:09:27 +08:00
|
|
|
|
2022-10-28 10:46:35 +08:00
|
|
|
const target = (std.zig.system.NativeTargetInfo.detect(step.target) catch unreachable).target;
|
2022-06-03 09:41:57 +08:00
|
|
|
|
|
|
|
const flags = [_][]const u8{
|
|
|
|
// Standard version used in Lua Makefile
|
|
|
|
"-std=gnu99",
|
|
|
|
|
|
|
|
// Define target-specific macro
|
|
|
|
switch (target.os.tag) {
|
|
|
|
.linux => "-DLUA_USE_LINUX",
|
|
|
|
.macos => "-DLUA_USE_MACOSX",
|
|
|
|
.windows => "-DLUA_USE_WINDOWS",
|
|
|
|
else => "-DLUA_USE_POSIX",
|
2022-06-01 09:09:27 +08:00
|
|
|
},
|
2022-06-03 09:41:57 +08:00
|
|
|
|
|
|
|
// Enable api check if desired
|
|
|
|
if (apicheck) "-DLUA_USE_APICHECK" else "",
|
2022-06-01 09:09:27 +08:00
|
|
|
};
|
|
|
|
|
2022-10-04 10:20:17 +08:00
|
|
|
const lua_source_files = switch (options.version) {
|
|
|
|
.lua_51 => &lua_51_source_files,
|
|
|
|
.lua_52 => &lua_52_source_files,
|
|
|
|
.lua_53 => &lua_53_source_files,
|
|
|
|
.lua_54 => &lua_54_source_files,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (lua_source_files) |file| {
|
|
|
|
const path = std.fs.path.join(b.allocator, &.{ dir(), lib_dir, file }) catch unreachable;
|
2022-10-21 04:25:34 +08:00
|
|
|
lua.addCSourceFile(path, &flags);
|
2022-06-01 09:09:27 +08:00
|
|
|
}
|
|
|
|
|
2022-10-21 04:25:34 +08:00
|
|
|
return lua;
|
2022-06-01 09:09:27 +08:00
|
|
|
}
|
|
|
|
|
2022-10-21 06:45:59 +08:00
|
|
|
const lua_51_source_files = [_][]const u8{
|
2022-10-04 10:20:17 +08:00
|
|
|
"lapi.c",
|
|
|
|
"lcode.c",
|
|
|
|
"ldebug.c",
|
|
|
|
"ldo.c",
|
|
|
|
"ldump.c",
|
|
|
|
"lfunc.c",
|
|
|
|
"lgc.c",
|
|
|
|
"llex.c",
|
|
|
|
"lmem.c",
|
|
|
|
"lobject.c",
|
|
|
|
"lopcodes.c",
|
|
|
|
"lparser.c",
|
|
|
|
"lstate.c",
|
|
|
|
"lstring.c",
|
|
|
|
"ltable.c",
|
|
|
|
"ltm.c",
|
|
|
|
"lundump.c",
|
|
|
|
"lvm.c",
|
|
|
|
"lzio.c",
|
|
|
|
"lauxlib.c",
|
|
|
|
"lbaselib.c",
|
|
|
|
"ldblib.c",
|
|
|
|
"liolib.c",
|
|
|
|
"lmathlib.c",
|
|
|
|
"loslib.c",
|
|
|
|
"ltablib.c",
|
|
|
|
"lstrlib.c",
|
|
|
|
"loadlib.c",
|
|
|
|
"linit.c",
|
|
|
|
};
|
|
|
|
|
2022-10-21 06:45:59 +08:00
|
|
|
const lua_52_source_files = [_][]const u8{
|
2022-10-04 10:20:17 +08:00
|
|
|
"lapi.c",
|
|
|
|
"lcode.c",
|
|
|
|
"lctype.c",
|
|
|
|
"ldebug.c",
|
|
|
|
"ldo.c",
|
|
|
|
"ldump.c",
|
|
|
|
"lfunc.c",
|
|
|
|
"lgc.c",
|
|
|
|
"llex.c",
|
|
|
|
"lmem.c",
|
|
|
|
"lobject.c",
|
|
|
|
"lopcodes.c",
|
|
|
|
"lparser.c",
|
|
|
|
"lstate.c",
|
|
|
|
"lstring.c",
|
|
|
|
"ltable.c",
|
|
|
|
"ltm.c",
|
|
|
|
"lundump.c",
|
|
|
|
"lvm.c",
|
|
|
|
"lzio.c",
|
|
|
|
"lauxlib.c",
|
|
|
|
"lbaselib.c",
|
|
|
|
"lbitlib.c",
|
|
|
|
"lcorolib.c",
|
|
|
|
"ldblib.c",
|
|
|
|
"liolib.c",
|
|
|
|
"lmathlib.c",
|
|
|
|
"loslib.c",
|
|
|
|
"lstrlib.c",
|
|
|
|
"ltablib.c",
|
|
|
|
"loadlib.c",
|
|
|
|
"linit.c",
|
|
|
|
};
|
|
|
|
|
|
|
|
const lua_53_source_files = [_][]const u8{
|
|
|
|
"lapi.c",
|
|
|
|
"lcode.c",
|
|
|
|
"lctype.c",
|
|
|
|
"ldebug.c",
|
|
|
|
"ldo.c",
|
|
|
|
"ldump.c",
|
|
|
|
"lfunc.c",
|
|
|
|
"lgc.c",
|
|
|
|
"llex.c",
|
|
|
|
"lmem.c",
|
|
|
|
"lobject.c",
|
|
|
|
"lopcodes.c",
|
|
|
|
"lparser.c",
|
|
|
|
"lstate.c",
|
|
|
|
"lstring.c",
|
|
|
|
"ltable.c",
|
|
|
|
"ltm.c",
|
|
|
|
"lundump.c",
|
|
|
|
"lvm.c",
|
|
|
|
"lzio.c",
|
|
|
|
"lauxlib.c",
|
|
|
|
"lbaselib.c",
|
|
|
|
"lbitlib.c",
|
|
|
|
"lcorolib.c",
|
|
|
|
"ldblib.c",
|
|
|
|
"liolib.c",
|
|
|
|
"lmathlib.c",
|
|
|
|
"loslib.c",
|
|
|
|
"lstrlib.c",
|
|
|
|
"ltablib.c",
|
|
|
|
"lutf8lib.c",
|
|
|
|
"loadlib.c",
|
|
|
|
"linit.c",
|
|
|
|
};
|
|
|
|
|
|
|
|
const lua_54_source_files = [_][]const u8{
|
2022-06-01 09:09:27 +08:00
|
|
|
"lapi.c",
|
|
|
|
"lcode.c",
|
|
|
|
"lctype.c",
|
|
|
|
"ldebug.c",
|
|
|
|
"ldo.c",
|
|
|
|
"ldump.c",
|
|
|
|
"lfunc.c",
|
|
|
|
"lgc.c",
|
|
|
|
"llex.c",
|
|
|
|
"lmem.c",
|
|
|
|
"lobject.c",
|
|
|
|
"lopcodes.c",
|
|
|
|
"lparser.c",
|
|
|
|
"lstate.c",
|
|
|
|
"lstring.c",
|
|
|
|
"ltable.c",
|
|
|
|
"ltm.c",
|
|
|
|
"lundump.c",
|
|
|
|
"lvm.c",
|
|
|
|
"lzio.c",
|
|
|
|
"lauxlib.c",
|
|
|
|
"lbaselib.c",
|
|
|
|
"lcorolib.c",
|
|
|
|
"ldblib.c",
|
|
|
|
"liolib.c",
|
|
|
|
"lmathlib.c",
|
|
|
|
"loadlib.c",
|
|
|
|
"loslib.c",
|
|
|
|
"lstrlib.c",
|
|
|
|
"ltablib.c",
|
|
|
|
"lutf8lib.c",
|
|
|
|
"linit.c",
|
|
|
|
};
|