Skip to content

Commit b493939

Browse files
committed
wip: zig apicheck
1 parent 0adce79 commit b493939

5 files changed

Lines changed: 105 additions & 7 deletions

File tree

build/header_gen.zig

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Concatenates two files together.
2+
3+
pub fn main(init: std.process.Init) !void {
4+
const arena = init.arena.allocator();
5+
6+
var iter = try init.minimal.args.iterateAllocator(arena);
7+
8+
// Skip executable name
9+
_ = iter.next();
10+
11+
const file1 = iter.next() orelse @panic("Missing file1 argument");
12+
const file2 = iter.next() orelse @panic("Missing file2 argument");
13+
const output_path = iter.next() orelse @panic("Missing output_path argument");
14+
if (iter.next() != null) @panic("Too many arguments");
15+
16+
const output_file = try Io.Dir.cwd().openFile(init.io, output_file, .{});
17+
defer output_file.close(init.io);
18+
var out_buf: [4096]u8 = undefined;
19+
var writer = output_file.writer(init.io, &out_buf);
20+
defer (&writer.interface).flush() catch {};
21+
22+
{
23+
const file = try Io.Dir.cwd().openFile(init.io, file1, .{ .mode = .read_only });
24+
defer file.deinit(init.io);
25+
26+
var buf: [4096]u8 = undefined;
27+
var reader = file.reader(init.io, &buf);
28+
29+
try (&reader.interface).stream(&writer.interface, .unlimited);
30+
}
31+
32+
try (&writer.interface).writeByte('\n');
33+
34+
{
35+
const file = try Io.Dir.cwd().openFile(init.io, file2, .{ .mode = .read_only });
36+
defer file.deinit(init.io);
37+
38+
var buf: [4096]u8 = undefined;
39+
var reader = file.reader(init.io, &buf);
40+
41+
try (&reader.interface).stream(&writer.interface, .unlimited);
42+
}
43+
}
44+
45+
const std = @import("std");
46+
const Allocator = std.mem.Allocator;
47+
const Io = std.Io;
48+
const File = Io.File;

build/lua.zig

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const std = @import("std");
33
const Build = std.Build;
44
const Step = std.Build.Step;
55

6-
const applyPatchToFile = @import("utils.zig").applyPatchToFile;
6+
const utils = @import("utils.zig");
7+
const applyPatchToFile = utils.applyPatchToFile;
8+
const concatenateFiles = utils.concatenateFiles;
79

810
const build = @import("../build.zig");
911

@@ -56,6 +58,8 @@ pub fn configure(
5658

5759
const user_header = "user.h";
5860

61+
const enable_apicheck = opts.api_check == .on or (opts.api_check == .debug and optimize == .Debug);
62+
5963
const flags = [_][]const u8{
6064
// Standard version used in Lua Makefile
6165
"-std=gnu99",
@@ -69,12 +73,12 @@ pub fn configure(
6973
},
7074

7175
// Enable api check
72-
if (opts.api_check == .on or (opts.api_check == .debug and optimize == .Debug)) "-DLUA_USE_APICHECK" else "",
76+
if (lang == .lua55 and enable_apicheck) "-DLUA_USE_APICHECK" else "",
7377

7478
// Build as DLL for windows if shared
7579
if (target.result.os.tag == .windows and shared) "-DLUA_BUILD_AS_DLL" else "",
7680

77-
if (lua_user_h) |_| b.fmt("-DLUA_USER_H=\"{s}\"", .{user_header}) else "",
81+
if (enable_apicheck or lua_user_h != null) b.fmt("-DLUA_USER_H=\"{s}\"", .{user_header}) else "",
7882
};
7983

8084
const lua_source_files = switch (lang) {
@@ -110,8 +114,19 @@ pub fn configure(
110114
library.installHeader(upstream.path("src/luaconf.h"), "luaconf.h");
111115

112116
if (lua_user_h) |user_h| {
113-
library.root_module.addIncludePath(user_h.dirname());
114-
library.installHeader(user_h, user_header);
117+
if (enable_apicheck) {
118+
const concat = concatenateFiles(b, b.graph.host, user_h, b.path("src/user.h"), user_header);
119+
library.step.dependOn(&concat.run.step);
120+
121+
library.root_module.addIncludePath(concat.output.dirname());
122+
library.installHeader(concat.output, user_header);
123+
} else {
124+
library.root_module.addIncludePath(user_h.dirname());
125+
library.installHeader(user_h, user_header);
126+
}
127+
} else if (enable_apicheck) {
128+
library.root_module.addIncludePath(b.path("src"));
129+
library.installHeader(b.path("src/user.h"), user_header);
115130
}
116131

117132
return library;

build/utils.zig

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const std = @import("std");
33
const Build = std.Build;
44
const Step = std.Build.Step;
55

6-
const PatchFile = struct {
6+
const RunOutput = struct {
77
run: *Step.Run,
88
output: Build.LazyPath,
99
};
@@ -14,7 +14,7 @@ pub fn applyPatchToFile(
1414
file: Build.LazyPath,
1515
patch_file: Build.LazyPath,
1616
output_file: []const u8,
17-
) PatchFile {
17+
) RunOutput {
1818
const patch = b.addExecutable(.{
1919
.name = "patch",
2020
.root_module = b.createModule(.{
@@ -34,3 +34,30 @@ pub fn applyPatchToFile(
3434
.output = out,
3535
};
3636
}
37+
38+
pub fn concatenateFiles(
39+
b: *Build,
40+
target: Build.ResolvedTarget,
41+
file1: Build.LazyPath,
42+
file2: Build.LazyPath,
43+
output_file: []const u8,
44+
) RunOutput {
45+
const concatenate = b.addExecutable(.{
46+
.name = "concat",
47+
.root_module = b.createModule(.{
48+
.root_source_file = b.path("build/header_gen.zig"),
49+
.target = target,
50+
}),
51+
});
52+
53+
const concatenate_run = b.addRunArtifact(concatenate);
54+
concatenate_run.addFileArg(file1);
55+
concatenate_run.addFileArg(file2);
56+
57+
const out = concatenate_run.addOutputFileArg(output_file);
58+
59+
return .{
60+
.run = concatenate_run,
61+
.output = out,
62+
};
63+
}

src/lib.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ extern "c" fn zig_registerAssertionHandler() void;
111111
/// This function is defined in luau.cpp and ensures Zig uses the correct free when compiling luau code
112112
extern "c" fn zig_luau_free(ptr: *anyopaque) void;
113113

114+
export fn zlua_assert(ok: bool) void {
115+
std.debug.assert(ok);
116+
}
117+
114118
const Allocator = std.mem.Allocator;
115119

116120
// Types

src/user.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extern void zlua_assert(int e);
2+
3+
#define luai_apicheck(l,e) zlua_assert(e)
4+

0 commit comments

Comments
 (0)