Skip to content

Commit 622dde9

Browse files
author
Simon Galli
committed
build: Add option for a user provided header file
This allows overriding Lua specific funtions like lua_[un]lock.
1 parent b1fa628 commit 622dde9

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

build.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ pub fn build(b: *Build) void {
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;
2121
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;
22+
const lua_user_h = b.option(Build.LazyPath, "lua_user_h", "Lazy path to user supplied c header file") orelse null;
2223

2324
if (lang == .luau and shared) {
2425
std.debug.panic("Luau does not support compiling or loading shared modules", .{});
2526
}
2627

28+
if (lua_user_h != null and (lang == .luajit or lang == .luau)) {
29+
std.debug.panic("Only basic lua supports a user provided header file", .{});
30+
}
31+
2732
// Zig module
2833
const zlua = b.addModule("zlua", .{
2934
.root_source_file = b.path("src/lib.zig"),
@@ -44,7 +49,7 @@ pub fn build(b: *Build) void {
4449
const lib = switch (lang) {
4550
.luajit => luajit_setup.configure(b, target, optimize, upstream, shared),
4651
.luau => luau_setup.configure(b, target, optimize, upstream, luau_use_4_vector),
47-
else => lua_setup.configure(b, target, optimize, upstream, lang, shared, library_name),
52+
else => lua_setup.configure(b, target, optimize, upstream, lang, shared, library_name, lua_user_h),
4853
};
4954

5055
// Expose the Lua artifact, and get an install step that header translation can refer to

build/lua.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub const Language = enum {
1212
luau,
1313
};
1414

15-
pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, upstream: *Build.Dependency, lang: Language, shared: bool, library_name: []const u8) *Step.Compile {
15+
pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, upstream: *Build.Dependency, lang: Language, shared: bool, library_name: []const u8, lua_user_h: ?Build.LazyPath) *Step.Compile {
1616
const version: std.SemanticVersion = switch (lang) {
1717
.lua51 => .{ .major = 5, .minor = 1, .patch = 5 },
1818
.lua52 => .{ .major = 5, .minor = 2, .patch = 4 },
@@ -38,6 +38,8 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
3838

3939
lib.addIncludePath(upstream.path("src"));
4040

41+
const user_header = "user.h";
42+
4143
const flags = [_][]const u8{
4244
// Standard version used in Lua Makefile
4345
"-std=gnu99",
@@ -55,6 +57,8 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
5557

5658
// Build as DLL for windows if shared
5759
if (target.result.os.tag == .windows and shared) "-DLUA_BUILD_AS_DLL" else "",
60+
61+
if (lua_user_h) |_| b.fmt("-DLUA_USER_H=\"{s}\"", .{user_header}) else "",
5862
};
5963

6064
const lua_source_files = switch (lang) {
@@ -87,6 +91,11 @@ pub fn configure(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.
8791
lib.installHeader(upstream.path("src/lauxlib.h"), "lauxlib.h");
8892
lib.installHeader(upstream.path("src/luaconf.h"), "luaconf.h");
8993

94+
if (lua_user_h) |user_h| {
95+
lib.addIncludePath(user_h.dirname());
96+
lib.installHeader(user_h, user_header);
97+
}
98+
9099
return lib;
91100
}
92101

0 commit comments

Comments
 (0)