Skip to content

Commit 942afe8

Browse files
committed
feat: restructure and add Lua 5.3, 5.2, and 5.1
Restructures the source layout and adds three new versions of Lua. Documentation is still incomplete at this point, but all four supported versions are well tested.
1 parent a12e3dd commit 942afe8

File tree

198 files changed

+71909
-38
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

198 files changed

+71909
-38
lines changed

build.zig

Lines changed: 171 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,36 @@ const std = @import("std");
33
const Builder = std.build.Builder;
44
const LibExeObjStep = std.build.LibExeObjStep;
55

6+
const LuaVersion = enum {
7+
lua_51,
8+
lua_52,
9+
lua_53,
10+
lua_54,
11+
lua_jit,
12+
};
13+
14+
fn libPath(version: LuaVersion) []const u8 {
15+
return switch (version) {
16+
.lua_51 => "src/ziglua-5.1/lib.zig",
17+
.lua_52 => "src/ziglua-5.2/lib.zig",
18+
.lua_53 => "src/ziglua-5.3/lib.zig",
19+
.lua_54 => "src/ziglua-5.4/lib.zig",
20+
else => unreachable,
21+
// .lua_jit => "src/ziglua-jit/lib.zig",
22+
};
23+
}
24+
625
pub fn build(b: *Builder) void {
7-
const mode = b.standardReleaseOptions();
26+
const version = b.option(LuaVersion, "version", "lua version to test") orelse .lua_54;
827

9-
const tests = b.addTest("src/tests.zig");
10-
link(b, tests, .{ .use_apicheck = true });
11-
tests.setBuildMode(mode);
28+
const tests = b.addTest(switch (version) {
29+
.lua_51 => "src/ziglua-5.1/tests.zig",
30+
.lua_52 => "src/ziglua-5.2/tests.zig",
31+
.lua_53 => "src/ziglua-5.3/tests.zig",
32+
.lua_54 => "src/ziglua-5.4/tests.zig",
33+
else => unreachable,
34+
});
35+
link(b, tests, libPath(version), .{ .use_apicheck = true, .version = version });
1236

1337
const test_step = b.step("test", "Run ziglua library tests");
1438
test_step.dependOn(&tests.step);
@@ -21,19 +45,41 @@ fn dir() []const u8 {
2145
const Options = struct {
2246
/// Defines the macro LUA_USE_APICHECK in debug builds
2347
use_apicheck: bool = false,
48+
/// Defines the Lua version to build and link
49+
version: LuaVersion = .lua_54,
2450
};
2551

26-
pub fn link(b: *Builder, step: *LibExeObjStep, options: Options) void {
27-
const lib = buildLua(b, step, options);
52+
pub fn linkAndPackage(b: *Builder, step: *LibExeObjStep, options: Options) std.build.Pkg {
53+
const lib_path = libPath(options.version);
54+
link(b, step, lib_path, options);
55+
56+
return .{
57+
.name = "ziglua",
58+
.path = .{ .path = std.fs.path.join(b.allocator, &.{ dir(), lib_path }) catch unreachable },
59+
};
60+
}
61+
62+
// TODO: expose the link and package steps separately for advanced use cases?
63+
fn link(b: *Builder, step: *LibExeObjStep, lib_path: []const u8, options: Options) void {
64+
const lib = buildLua(b, step, lib_path, options);
2865
step.linkLibrary(lib);
2966
step.linkLibC();
3067
}
3168

32-
const lib_dir = "lib/lua-5.4.4/src/";
69+
// TODO: how to test all versions? May need a make/help script to test all
70+
// versions separately because there might be name collisions
71+
fn buildLua(b: *Builder, step: *LibExeObjStep, lib_path: []const u8, options: Options) *LibExeObjStep {
72+
const lib_dir = switch (options.version) {
73+
.lua_51 => "lib/lua-5.1.5/src/",
74+
.lua_52 => "lib/lua-5.2.4/src/",
75+
.lua_53 => "lib/lua-5.3.6/src/",
76+
.lua_54 => "lib/lua-5.4.4/src/",
77+
else => unreachable,
78+
// .lua_jit => "lib/lua-5.4.4/src/",
79+
};
3380

34-
fn buildLua(b: *Builder, step: *LibExeObjStep, options: Options) *LibExeObjStep {
35-
const lib_path = std.fs.path.join(b.allocator, &.{ dir(), "src/ziglua.zig" }) catch unreachable;
36-
const lib = b.addStaticLibrary("lua", lib_path);
81+
const absolute_lib_path = std.fs.path.join(b.allocator, &.{ dir(), lib_path }) catch unreachable;
82+
const lib = b.addStaticLibrary("lua", absolute_lib_path);
3783
lib.setBuildMode(step.build_mode);
3884
lib.setTarget(step.target);
3985

@@ -59,15 +105,127 @@ fn buildLua(b: *Builder, step: *LibExeObjStep, options: Options) *LibExeObjStep
59105
if (apicheck) "-DLUA_USE_APICHECK" else "",
60106
};
61107

62-
inline for (lua_source_files) |file| {
63-
const path = std.fs.path.join(b.allocator, &.{ dir(), lib_dir ++ file }) catch unreachable;
108+
const lua_source_files = switch (options.version) {
109+
.lua_51 => &lua_51_source_files,
110+
.lua_52 => &lua_52_source_files,
111+
.lua_53 => &lua_53_source_files,
112+
.lua_54 => &lua_54_source_files,
113+
else => unreachable,
114+
// .lua_jit => &lua_jit_source_files,
115+
};
116+
117+
for (lua_source_files) |file| {
118+
const path = std.fs.path.join(b.allocator, &.{ dir(), lib_dir, file }) catch unreachable;
64119
step.addCSourceFile(path, &flags);
65120
}
66121

67122
return lib;
68123
}
69124

70-
const lua_source_files = [_][]const u8{
125+
const lua_51_source_files = [_][]const u8 {
126+
"lapi.c",
127+
"lcode.c",
128+
"ldebug.c",
129+
"ldo.c",
130+
"ldump.c",
131+
"lfunc.c",
132+
"lgc.c",
133+
"llex.c",
134+
"lmem.c",
135+
"lobject.c",
136+
"lopcodes.c",
137+
"lparser.c",
138+
"lstate.c",
139+
"lstring.c",
140+
"ltable.c",
141+
"ltm.c",
142+
"lundump.c",
143+
"lvm.c",
144+
"lzio.c",
145+
"lauxlib.c",
146+
"lbaselib.c",
147+
"ldblib.c",
148+
"liolib.c",
149+
"lmathlib.c",
150+
"loslib.c",
151+
"ltablib.c",
152+
"lstrlib.c",
153+
"loadlib.c",
154+
"linit.c",
155+
};
156+
157+
const lua_52_source_files = [_][]const u8 {
158+
"lapi.c",
159+
"lcode.c",
160+
"lctype.c",
161+
"ldebug.c",
162+
"ldo.c",
163+
"ldump.c",
164+
"lfunc.c",
165+
"lgc.c",
166+
"llex.c",
167+
"lmem.c",
168+
"lobject.c",
169+
"lopcodes.c",
170+
"lparser.c",
171+
"lstate.c",
172+
"lstring.c",
173+
"ltable.c",
174+
"ltm.c",
175+
"lundump.c",
176+
"lvm.c",
177+
"lzio.c",
178+
"lauxlib.c",
179+
"lbaselib.c",
180+
"lbitlib.c",
181+
"lcorolib.c",
182+
"ldblib.c",
183+
"liolib.c",
184+
"lmathlib.c",
185+
"loslib.c",
186+
"lstrlib.c",
187+
"ltablib.c",
188+
"loadlib.c",
189+
"linit.c",
190+
};
191+
192+
const lua_53_source_files = [_][]const u8{
193+
"lapi.c",
194+
"lcode.c",
195+
"lctype.c",
196+
"ldebug.c",
197+
"ldo.c",
198+
"ldump.c",
199+
"lfunc.c",
200+
"lgc.c",
201+
"llex.c",
202+
"lmem.c",
203+
"lobject.c",
204+
"lopcodes.c",
205+
"lparser.c",
206+
"lstate.c",
207+
"lstring.c",
208+
"ltable.c",
209+
"ltm.c",
210+
"lundump.c",
211+
"lvm.c",
212+
"lzio.c",
213+
"lauxlib.c",
214+
"lbaselib.c",
215+
"lbitlib.c",
216+
"lcorolib.c",
217+
"ldblib.c",
218+
"liolib.c",
219+
"lmathlib.c",
220+
"loslib.c",
221+
"lstrlib.c",
222+
"ltablib.c",
223+
"lutf8lib.c",
224+
"loadlib.c",
225+
"linit.c",
226+
};
227+
228+
const lua_54_source_files = [_][]const u8{
71229
"lapi.c",
72230
"lcode.c",
73231
"lctype.c",

docs.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ Both `lua_pcall` and `lua_pcallk` are expanded to `protectedCall` and `protected
108108

109109
## Build Documentation
110110

111-
When integrating ziglua into your projects, the following three statements are required:
111+
When integrating ziglua into your projects, the following two statements are required:
112112

113113
1. `@import()` the `build.zig` file
114-
2. `addPackagePath` the ziglua api
115-
3. `ziglua.link()` the library with your executable
114+
2. `addPackage()` the ziglua api
116115

117116
Note that this _must_ be done after setting the target and build mode, otherwise ziglua will not know that information.
118117

@@ -121,15 +120,24 @@ const ziglua = @import("lib/ziglua/build.zig");
121120
122121
pub fn build(b: *Builder) void {
123122
...
124-
exe.addPackagePath("ziglua", "lib/ziglua/src/ziglua.zig");
125-
ziglua.link(b, exe, .{});
123+
exe.addPackage(ziglua.linkAndPackage(b, exe, .{}));
126124
}
127125
```
128126

129-
There is currently one option that can be passed in the third argument to `ziglua.link()`:
127+
This makes the `ziglua` package available in your project. Access with `@import("ziglua")`.
128+
129+
There are currently two options that can be passed in the third argument to `ziglua.link()`:
130130

131131
* `.use_apicheck`: defaults to **false**. When **true** defines the macro `LUA_USE_APICHECK` in debug builds. See [The C API docs](https://www.lua.org/manual/5.4/manual.html#4) for more information on this macro.
132132

133+
* `.version`: Set the Lua version to build and embed. Defaults to `.lua_54`. Possible values are `.lua_51`, `.lua_52`, `.lua_53`, and `.lua_54`.
134+
135+
For example, here is a `ziglua.linkAndPackage()` call that enables api check and embeds Lua 5.2:
136+
137+
```
138+
exe.addPackage(ziglua.linkAndPackage(b, exe, .{ .use_apicheck = true, .version = .lua_52 }));
139+
```
140+
133141
## Examples
134142

135143
Here are more thorough examples that show off the ziglua bindings in context. All examples use previously documented [`build.zig`](#build-documentation) setup.

lib/lua-5.1.5/COPYRIGHT

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Lua License
2+
-----------
3+
4+
Lua is licensed under the terms of the MIT license reproduced below.
5+
This means that Lua is free software and can be used for both academic
6+
and commercial purposes at absolutely no cost.
7+
8+
For details and rationale, see http://www.lua.org/license.html .
9+
10+
===============================================================================
11+
12+
Copyright (C) 1994-2012 Lua.org, PUC-Rio.
13+
14+
Permission is hereby granted, free of charge, to any person obtaining a copy
15+
of this software and associated documentation files (the "Software"), to deal
16+
in the Software without restriction, including without limitation the rights
17+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18+
copies of the Software, and to permit persons to whom the Software is
19+
furnished to do so, subject to the following conditions:
20+
21+
The above copyright notice and this permission notice shall be included in
22+
all copies or substantial portions of the Software.
23+
24+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30+
THE SOFTWARE.
31+
32+
===============================================================================
33+
34+
(end of COPYRIGHT)

0 commit comments

Comments
 (0)