Skip to content

Commit a3c23ef

Browse files
authored
Merge pull request #186 from p00f/system-lua
allow using system lua
2 parents e7f0b39 + da836c1 commit a3c23ef

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

build.zig

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn build(b: *Build) void {
1818
const lang = b.option(Language, "lang", "Lua language version to build") orelse .lua54;
1919
const library_name = b.option([]const u8, "library_name", "Library name for lua linking, default is `lua`") orelse "lua";
2020
const shared = b.option(bool, "shared", "Build shared library instead of static") orelse false;
21+
const system_lua = b.option(bool, "system_lua", "Use system lua") orelse false;
2122
const luau_use_4_vector = b.option(bool, "luau_use_4_vector", "Build Luau to use 4-vectors instead of the default 3-vector.") orelse false;
2223
const lua_user_h = b.option(Build.LazyPath, "lua_user_h", "Lazy path to user supplied c header file") orelse null;
2324

@@ -30,22 +31,36 @@ pub fn build(b: *Build) void {
3031
}
3132

3233
// Zig module
33-
const zlua = b.addModule("zlua", .{
34+
const zlua = if (system_lua) b.addModule("zlua", .{
35+
.root_source_file = b.path("src/lib.zig"),
36+
.target = target,
37+
}) else b.addModule("zlua", .{
3438
.root_source_file = b.path("src/lib.zig"),
3539
});
3640

3741
// Expose build configuration to the ziglua module
3842
const config = b.addOptions();
3943
config.addOption(Language, "lang", lang);
4044
config.addOption(bool, "luau_use_4_vector", luau_use_4_vector);
45+
config.addOption(bool, "system_lua", system_lua);
4146
zlua.addOptions("config", config);
4247

4348
if (lang == .luau) {
4449
const vector_size: usize = if (luau_use_4_vector) 4 else 3;
4550
zlua.addCMacro("LUA_VECTOR_SIZE", b.fmt("{}", .{vector_size}));
4651
}
4752

48-
if (b.lazyDependency(@tagName(lang), .{})) |upstream| {
53+
if (system_lua) {
54+
const link_mode: std.builtin.LinkMode = if (shared) .dynamic else .static;
55+
switch (lang) {
56+
.lua51 => zlua.linkSystemLibrary("lua5.1", .{ .preferred_link_mode = link_mode }),
57+
.lua52 => zlua.linkSystemLibrary("lua5.2", .{ .preferred_link_mode = link_mode }),
58+
.lua53 => zlua.linkSystemLibrary("lua5.3", .{ .preferred_link_mode = link_mode }),
59+
.lua54 => zlua.linkSystemLibrary("lua5.4", .{ .preferred_link_mode = link_mode }),
60+
.luajit => zlua.linkSystemLibrary("luajit", .{ .preferred_link_mode = link_mode }),
61+
.luau => @panic("luau not supported for system lua"),
62+
}
63+
} else if (b.lazyDependency(@tagName(lang), .{})) |upstream| {
4964
const lib = switch (lang) {
5065
.luajit => luajit_setup.configure(b, target, optimize, upstream, shared),
5166
.luau => luau_setup.configure(b, target, optimize, upstream, luau_use_4_vector),

src/lib.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ const std = @import("std");
1818
pub const def = @import("define.zig");
1919
pub const define = def.define;
2020

21-
const c = @import("c");
22-
2321
const config = @import("config");
2422

23+
const c = if (config.system_lua)
24+
@cImport({
25+
@cInclude("lua.h");
26+
@cInclude("lualib.h");
27+
@cInclude("lauxlib.h");
28+
})
29+
else
30+
@import("c");
31+
2532
/// Lua language version targeted
2633
pub const lang = config.lang;
2734

0 commit comments

Comments
 (0)