@@ -3,12 +3,36 @@ const std = @import("std");
3
3
const Builder = std .build .Builder ;
4
4
const LibExeObjStep = std .build .LibExeObjStep ;
5
5
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
+
6
25
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 ;
8
27
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 });
12
36
13
37
const test_step = b .step ("test" , "Run ziglua library tests" );
14
38
test_step .dependOn (& tests .step );
@@ -21,19 +45,41 @@ fn dir() []const u8 {
21
45
const Options = struct {
22
46
/// Defines the macro LUA_USE_APICHECK in debug builds
23
47
use_apicheck : bool = false ,
48
+ /// Defines the Lua version to build and link
49
+ version : LuaVersion = .lua_54 ,
24
50
};
25
51
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 );
28
65
step .linkLibrary (lib );
29
66
step .linkLibC ();
30
67
}
31
68
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
+ };
33
80
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 );
37
83
lib .setBuildMode (step .build_mode );
38
84
lib .setTarget (step .target );
39
85
@@ -59,15 +105,127 @@ fn buildLua(b: *Builder, step: *LibExeObjStep, options: Options) *LibExeObjStep
59
105
if (apicheck ) "-DLUA_USE_APICHECK" else "" ,
60
106
};
61
107
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 ;
64
119
step .addCSourceFile (path , & flags );
65
120
}
66
121
67
122
return lib ;
68
123
}
69
124
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 {
71
229
"lapi.c" ,
72
230
"lcode.c" ,
73
231
"lctype.c" ,
0 commit comments