2022-06-01 09:09:27 +08:00
|
|
|
const std = @import("std");
|
|
|
|
|
2023-02-16 06:05:53 +08:00
|
|
|
const Build = std.Build;
|
2022-06-03 09:41:57 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
pub const LuaVersion = enum {
|
|
|
|
lua_51,
|
|
|
|
lua_52,
|
|
|
|
lua_53,
|
|
|
|
lua_54,
|
|
|
|
// lua_jit,
|
2022-10-04 10:20:17 +08:00
|
|
|
};
|
|
|
|
|
2023-02-16 06:05:53 +08:00
|
|
|
pub fn build(b: *Build) void {
|
2023-08-04 08:37:37 +08:00
|
|
|
// Remove the default install and uninstall steps
|
|
|
|
b.top_level_steps = .{};
|
|
|
|
|
2023-02-21 12:26:21 +08:00
|
|
|
const target = b.standardTargetOptions(.{});
|
|
|
|
const optimize = b.standardOptimizeOption(.{});
|
2023-08-02 09:47:13 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const lua_version = b.option(LuaVersion, "version", "Lua API and library version") orelse .lua_54;
|
2023-02-21 12:26:21 +08:00
|
|
|
const shared = b.option(bool, "shared", "Build shared library instead of static") orelse false;
|
2024-01-19 12:13:02 +08:00
|
|
|
|
2023-08-02 09:47:13 +08:00
|
|
|
// Zig module
|
2023-08-04 08:37:37 +08:00
|
|
|
const ziglua = b.addModule("ziglua", .{
|
2024-02-18 09:57:20 +08:00
|
|
|
.source_file = switch (lua_version) {
|
|
|
|
.lua_51 => .{ .path = "src/ziglua-5.1/lib.zig" },
|
|
|
|
.lua_52 => .{ .path = "src/ziglua-5.2/lib.zig" },
|
|
|
|
.lua_53 => .{ .path = "src/ziglua-5.3/lib.zig" },
|
|
|
|
.lua_54 => .{ .path = "src/ziglua-5.4/lib.zig" },
|
2024-01-16 00:06:05 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
// Lua library artifact
|
2023-02-21 12:26:21 +08:00
|
|
|
const lib_opts = .{
|
|
|
|
.name = "lua",
|
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
2024-02-18 09:57:20 +08:00
|
|
|
.version = switch (lua_version) {
|
|
|
|
.lua_51 => std.SemanticVersion{ .major = 5, .minor = 1, .patch = 5 },
|
|
|
|
.lua_52 => std.SemanticVersion{ .major = 5, .minor = 2, .patch = 4 },
|
|
|
|
.lua_53 => std.SemanticVersion{ .major = 5, .minor = 3, .patch = 6 },
|
|
|
|
.lua_54 => std.SemanticVersion{ .major = 5, .minor = 4, .patch = 6 },
|
2023-02-16 06:05:53 +08:00
|
|
|
},
|
2022-10-04 10:20:17 +08:00
|
|
|
};
|
2023-02-21 12:26:21 +08:00
|
|
|
const lib = if (shared)
|
|
|
|
b.addSharedLibrary(lib_opts)
|
|
|
|
else
|
|
|
|
b.addStaticLibrary(lib_opts);
|
2023-08-02 09:47:13 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const lib_dir = switch (lua_version) {
|
|
|
|
.lua_51 => "lib/lua-5.1/src",
|
|
|
|
.lua_52 => "lib/lua-5.2/src",
|
|
|
|
.lua_53 => "lib/lua-5.3/src",
|
|
|
|
.lua_54 => "lib/lua-5.4/src",
|
|
|
|
};
|
|
|
|
lib.addIncludePath(.{ .path = lib_dir });
|
|
|
|
|
|
|
|
const os_tag = target.os_tag orelse
|
|
|
|
(std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target.os.tag;
|
2023-08-02 09:47:13 +08:00
|
|
|
|
2022-06-03 09:41:57 +08:00
|
|
|
const flags = [_][]const u8{
|
|
|
|
// Standard version used in Lua Makefile
|
|
|
|
"-std=gnu99",
|
|
|
|
|
|
|
|
// Define target-specific macro
|
2024-02-18 09:57:20 +08:00
|
|
|
switch (os_tag) {
|
2022-06-03 09:41:57 +08:00
|
|
|
.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
|
|
|
|
2023-08-02 09:47:13 +08:00
|
|
|
// Enable api check
|
2023-02-21 12:26:21 +08:00
|
|
|
if (optimize == .Debug) "-DLUA_USE_APICHECK" else "",
|
2022-10-04 10:20:17 +08:00
|
|
|
};
|
2023-08-02 09:47:13 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const lua_source_files = switch (lua_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,
|
2023-08-02 09:47:13 +08:00
|
|
|
};
|
2024-02-18 09:57:20 +08:00
|
|
|
for (lua_source_files) |file| {
|
|
|
|
const path = std.fs.path.join(b.allocator, &.{ lib_dir, file }) catch unreachable;
|
|
|
|
lib.addCSourceFile(.{ .file = .{ .path = path }, .flags = &flags });
|
|
|
|
}
|
2023-02-21 12:26:21 +08:00
|
|
|
lib.linkLibC();
|
2023-08-02 09:47:13 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
lib.installHeader(b.pathJoin(&.{ lib_dir, "lua.h" }), "lua/lua.h");
|
|
|
|
lib.installHeader(b.pathJoin(&.{ lib_dir, "lualib.h" }), "lua/lualib.h");
|
|
|
|
lib.installHeader(b.pathJoin(&.{ lib_dir, "lauxlib.h" }), "lua/lauxlib.h");
|
|
|
|
lib.installHeader(b.pathJoin(&.{ lib_dir, "luaconf.h" }), "lua/luaconf.h");
|
2023-02-21 12:26:21 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
b.installArtifact(lib);
|
2024-01-15 14:21:39 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
// Tests
|
|
|
|
const tests = b.addTest(.{
|
|
|
|
.root_source_file = switch (lua_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" },
|
|
|
|
},
|
2023-02-21 12:26:21 +08:00
|
|
|
.optimize = optimize,
|
2024-01-19 12:20:13 +08:00
|
|
|
});
|
2024-02-18 09:57:20 +08:00
|
|
|
tests.addIncludePath(.{ .path = lib_dir });
|
|
|
|
tests.linkLibrary(lib);
|
2023-08-04 08:37:37 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const test_step = b.step("test", "Run ziglua tests");
|
|
|
|
test_step.dependOn(&tests.step);
|
2022-06-01 09:09:27 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
// Examples
|
|
|
|
const examples = [_]struct { []const u8, []const u8 }{
|
|
|
|
.{ "interpreter", "examples/interpreter.zig" },
|
|
|
|
.{ "zig-function", "examples/zig-fn.zig" },
|
2024-01-20 13:14:47 +08:00
|
|
|
};
|
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
for (examples) |example| {
|
|
|
|
const exe = b.addExecutable(.{
|
|
|
|
.name = example[0],
|
|
|
|
.root_source_file = .{ .path = example[1] },
|
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
|
|
|
exe.addModule("ziglua", ziglua);
|
|
|
|
exe.linkLibrary(lib);
|
2024-01-20 13:14:47 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const artifact = b.addInstallArtifact(exe, .{});
|
|
|
|
const exe_step = b.step(b.fmt("install-example-{s}", .{example[0]}), b.fmt("Install {s} example", .{example[0]}));
|
|
|
|
exe_step.dependOn(&artifact.step);
|
2024-01-20 13:14:47 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
|
|
run_cmd.step.dependOn(b.getInstallStep());
|
|
|
|
if (b.args) |args| run_cmd.addArgs(args);
|
2024-01-20 13:14:47 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const run_step = b.step(b.fmt("run-example-{s}", .{example[0]}), b.fmt("Run {s} example", .{example[0]}));
|
|
|
|
run_step.dependOn(&run_cmd.step);
|
2024-01-20 13:14:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const lua_51_source_files = [_][]const u8{
|
|
|
|
"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-04 10:20:17 +08:00
|
|
|
};
|
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const lua_52_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",
|
|
|
|
"loadlib.c",
|
|
|
|
"linit.c",
|
2022-06-01 09:09:27 +08:00
|
|
|
};
|
2023-12-24 07:13:33 +08:00
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
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",
|
2024-01-20 13:14:47 +08:00
|
|
|
};
|
|
|
|
|
2024-02-18 09:57:20 +08:00
|
|
|
const lua_54_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",
|
|
|
|
"lcorolib.c",
|
|
|
|
"ldblib.c",
|
|
|
|
"liolib.c",
|
|
|
|
"lmathlib.c",
|
|
|
|
"loadlib.c",
|
|
|
|
"loslib.c",
|
|
|
|
"lstrlib.c",
|
|
|
|
"ltablib.c",
|
|
|
|
"lutf8lib.c",
|
|
|
|
"linit.c",
|
2023-12-30 15:13:19 +08:00
|
|
|
};
|