Skip to content

Commit 87c4488

Browse files
committed
Remove use of @cImport for system linked lua
When linking against system Lua, we were still using cImport. Now the translate-c package is used. I still want to figure out how to use the -fsys flag for enabling the system Lua, but that is for another day.
1 parent 5cd2f36 commit 87c4488

2 files changed

Lines changed: 24 additions & 30 deletions

File tree

build.zig

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,35 @@ pub fn build(b: *Build) void {
4444
const config = b.addOptions();
4545
config.addOption(Language, "lang", lang);
4646
config.addOption(bool, "luau_use_4_vector", luau_use_4_vector);
47-
config.addOption(bool, "system_lua", system_lua);
4847
zlua.addOptions("config", config);
4948

5049
if (lang == .luau) {
5150
const vector_size: usize = if (luau_use_4_vector) 4 else 3;
5251
zlua.addCMacro("LUA_VECTOR_SIZE", b.fmt("{}", .{vector_size}));
5352
}
5453

54+
// Translate the Lua C headers in to Zig code.
55+
const translate_c = b.dependency("translate_c", .{});
56+
57+
const c_header_path = switch (lang) {
58+
.luajit => b.path("build/include/luajit_all.h"),
59+
.luau => b.path("build/include/luau_all.h"),
60+
else => b.path("build/include/lua_all.h"),
61+
};
62+
const t: Translator = .init(translate_c, .{
63+
.c_source_file = c_header_path,
64+
.target = target,
65+
.optimize = optimize,
66+
});
67+
68+
// If we've been given additional system headers, add them now.
69+
// Useful for things like linking Emscripten headers by including a new sysroot
70+
if (additional_system_headers) |headers| {
71+
t.addSystemIncludePath(headers);
72+
}
73+
74+
zlua.addImport("c", t.mod);
75+
5576
if (system_lua) {
5677
const link_mode: std.builtin.LinkMode = if (shared) .dynamic else .static;
5778
switch (lang) {
@@ -81,28 +102,8 @@ pub fn build(b: *Build) void {
81102

82103
zlua.linkLibrary(lib);
83104

84-
// lib must expose all headers included by these root headers
85-
const c_header_path = switch (lang) {
86-
.luajit => b.path("build/include/luajit_all.h"),
87-
.luau => b.path("build/include/luau_all.h"),
88-
else => b.path("build/include/lua_all.h"),
89-
};
90-
const translate_c = b.dependency("translate_c", .{});
91-
92-
const t: Translator = .init(translate_c, .{
93-
.c_source_file = c_header_path,
94-
.target = target,
95-
.optimize = optimize,
96-
});
105+
// Ensure translate C can find the Lua headers.
97106
t.addIncludePath(lib.getEmittedIncludeTree());
98-
99-
// If we've been given additional system headers, add them now
100-
// Useful for things like linking Emscripten headers by including a new sysroot
101-
if (additional_system_headers != null) {
102-
t.addSystemIncludePath(additional_system_headers.?);
103-
}
104-
105-
zlua.addImport("c", t.mod);
106107
}
107108

108109
// Tests

src/lib.zig

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ const config = @import("config");
3737
/// This is exposed in case a function or constant is missing from the ziglua
3838
/// bindings, or one of the wrapper functions imposes a limitation that
3939
/// requires access to the C function.
40-
pub const c = if (config.system_lua)
41-
@cImport({
42-
@cInclude("lua.h");
43-
@cInclude("lualib.h");
44-
@cInclude("lauxlib.h");
45-
})
46-
else
47-
@import("c");
40+
pub const c = @import("c");
4841

4942
/// Lua language version targeted
5043
pub const lang = config.lang;

0 commit comments

Comments
 (0)