build: add new option to define LUA_USE_APICHECK

Adds an options struct to the public link function used to enable Lua
api argument checks. This will be enabled in debug builds only!
This commit is contained in:
Nathan Craddock 2022-06-02 19:41:57 -06:00
parent dd41779537
commit 66d457d282
No known key found for this signature in database
GPG Key ID: ABE41A31B52E9DA7

View File

@ -1,10 +1,13 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const Builder = std.build.Builder;
const LibExeObjStep = std.build.LibExeObjStep;
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const tests = b.addTest("src/ziglua.zig");
link(b, tests);
link(b, tests, .{ .use_apicheck = true });
tests.setBuildMode(mode);
const test_step = b.step("test", "Run ziglua library tests");
@ -15,40 +18,45 @@ fn dir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}
pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep) void {
const lib = buildLua(b, step);
const Options = struct {
/// Defines the macro LUA_USE_APICHECK in debug builds
use_apicheck: bool = false,
};
pub fn link(b: *Builder, step: *LibExeObjStep, options: Options) void {
const lib = buildLua(b, step, options);
step.linkLibrary(lib);
step.linkLibC();
}
const lib_dir = "lib/lua-5.4.4/src/";
fn buildLua(b: *std.build.Builder, step: *std.build.LibExeObjStep) *std.build.LibExeObjStep {
fn buildLua(b: *Builder, step: *LibExeObjStep, options: Options) *LibExeObjStep {
const lib_path = std.fs.path.join(b.allocator, &.{ dir(), "src/ziglua.zig" }) catch unreachable;
const lib = b.addStaticLibrary("lua", lib_path);
lib.setBuildMode(step.build_mode);
lib.setTarget(step.target);
const apicheck = step.build_mode == .Debug and options.use_apicheck;
step.addIncludeDir(std.fs.path.join(b.allocator, &.{ dir(), lib_dir }) catch unreachable);
const target = (std.zig.system.NativeTargetInfo.detect(b.allocator, lib.target) catch unreachable).target;
const flags = switch (target.os.tag) {
.linux => [_][]const u8{
"-std=gnu99",
"-DLUA_USE_LINUX",
},
.macos => [_][]const u8{
"-std=gnu99",
"-DLUA_USE_MACOSX",
},
.windows => [_][]const u8{
"-std=gnu99",
"-DLUA_USE_WINDOWS",
},
else => [_][]const u8{
"-std=gnu99",
"-DLUA_USE_POSIX",
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",
},
// Enable api check if desired
if (apicheck) "-DLUA_USE_APICHECK" else "",
};
inline for (lua_source_files) |file| {