1
1
const std = @import ("std" );
2
2
3
3
const Build = std .Build ;
4
- const CompileStep = std .build .CompileStep ;
5
4
6
5
pub const LuaVersion = enum {
7
6
lua_51 ,
@@ -11,123 +10,92 @@ pub const LuaVersion = enum {
11
10
// lua_jit,
12
11
};
13
12
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
- };
21
- }
22
-
23
13
pub fn build (b : * Build ) void {
24
- const version = b .option (LuaVersion , "version" , "lua version to test" ) orelse .lua_54 ;
25
-
26
- const tests = b .addTest (.{
27
- .root_source_file = switch (version ) {
28
- .lua_51 = > .{ .path = "src/ziglua-5.1/tests.zig" },
29
- .lua_52 = > .{ .path = "src/ziglua-5.2/tests.zig" },
30
- .lua_53 = > .{ .path = "src/ziglua-5.3/tests.zig" },
31
- .lua_54 = > .{ .path = "src/ziglua-5.4/tests.zig" },
14
+ const target = b .standardTargetOptions (.{});
15
+ const optimize = b .standardOptimizeOption (.{});
16
+ const lua_version = b .option (LuaVersion , "version" , "Lua API and library version" ) orelse .lua_54 ;
17
+ const shared = b .option (bool , "shared" , "Build shared library instead of static" ) orelse false ;
18
+
19
+ const lib_opts = .{
20
+ .name = "lua" ,
21
+ .target = target ,
22
+ .optimize = optimize ,
23
+ .version = switch (lua_version ) {
24
+ .lua_51 = > std.SemanticVersion { .major = 5 , .minor = 1 , .patch = 5 },
25
+ .lua_52 = > std.SemanticVersion { .major = 5 , .minor = 2 , .patch = 4 },
26
+ .lua_53 = > std.SemanticVersion { .major = 5 , .minor = 3 , .patch = 6 },
27
+ .lua_54 = > std.SemanticVersion { .major = 5 , .minor = 4 , .patch = 4 },
32
28
},
33
- });
34
- link (b , tests , .{ .use_apicheck = true , .version = version });
35
-
36
- const run_unit_tests = b .addRunArtifact (tests );
37
-
38
- const test_step = b .step ("test" , "Run ziglua tests" );
39
- test_step .dependOn (& run_unit_tests .step );
40
- }
41
-
42
- fn dir () []const u8 {
43
- return std .fs .path .dirname (@src ().file ) orelse "." ;
44
- }
45
-
46
- pub const Options = struct {
47
- /// Defines the macro LUA_USE_APICHECK in debug builds
48
- use_apicheck : bool = false ,
49
- /// Defines the Lua version to build and link
50
- version : LuaVersion = .lua_54 ,
51
-
52
- shared : bool = false ,
53
- };
54
-
55
- pub fn compileAndCreateModule (b : * Build , step : * CompileStep , options : Options ) * std.build.Module {
56
- link (b , step , options );
57
-
58
- const lib_path = libPath (options .version );
59
- return b .createModule (.{
60
- .source_file = .{ .path = std .fs .path .join (b .allocator , &.{ dir (), lib_path }) catch unreachable },
61
- });
62
- }
63
-
64
- // TODO: expose the link and package steps separately for advanced use cases?
65
- fn link (b : * Build , step : * CompileStep , options : Options ) void {
66
- const lua = buildLua (b , step , options );
67
- step .linkLibrary (lua );
68
- }
69
-
70
- // TODO: how to test all versions? May need a make/help script to test all
71
- // versions separately because there might be name collisions
72
- fn buildLua (b : * Build , step : * CompileStep , options : Options ) * CompileStep {
73
- const lib_dir = switch (options .version ) {
74
- .lua_51 = > "lib/lua-5.1/src/" ,
75
- .lua_52 = > "lib/lua-5.2/src/" ,
76
- .lua_53 = > "lib/lua-5.3/src/" ,
77
- .lua_54 = > "lib/lua-5.4/src/" ,
78
29
};
79
-
80
- const lua = brk : {
81
- if (options .shared ) break :brk b .addSharedLibrary (.{
82
- .name = "lua" ,
83
- .target = step .target ,
84
- .optimize = step .optimize ,
85
- });
86
-
87
- break :brk b .addStaticLibrary (.{
88
- .name = "lua" ,
89
- .target = step .target ,
90
- .optimize = step .optimize ,
91
- });
30
+ const lib = if (shared )
31
+ b .addSharedLibrary (lib_opts )
32
+ else
33
+ b .addStaticLibrary (lib_opts );
34
+ const lib_dir = switch (lua_version ) {
35
+ .lua_51 = > "lib/lua-5.1.5/src" ,
36
+ .lua_52 = > "lib/lua-5.2.4/src" ,
37
+ .lua_53 = > "lib/lua-5.3.6/src" ,
38
+ .lua_54 = > "lib/lua-5.4.4/src" ,
92
39
};
93
-
94
- lua . linkLibC ();
95
-
96
- const apicheck = step . optimize == .Debug and options . use_apicheck ;
97
-
98
- step . addIncludePath ( std . fs . path . join ( b . allocator , &.{ dir (), lib_dir }) catch unreachable ) ;
99
-
100
- const target = ( std . zig . system . NativeTargetInfo . detect ( step . target ) catch unreachable ). target ;
101
-
40
+ const lua_source_files = switch ( lua_version ) {
41
+ .lua_51 = > & lua_51_source_files ,
42
+ .lua_52 = > & lua_52_source_files ,
43
+ .lua_53 = > & lua_53_source_files ,
44
+ .lua_54 = > & lua_54_source_files ,
45
+ } ;
46
+ lib . addIncludePath ( lib_dir );
47
+ const os_tag = target . os_tag orelse
48
+ ( std . zig . system . NativeTargetInfo . detect ( target ) catch unreachable ). target . os . tag ;
102
49
const flags = [_ ][]const u8 {
103
50
// Standard version used in Lua Makefile
104
51
"-std=gnu99" ,
105
52
106
53
// Define target-specific macro
107
- switch (target . os . tag ) {
54
+ switch (os_tag ) {
108
55
.linux = > "-DLUA_USE_LINUX" ,
109
56
.macos = > "-DLUA_USE_MACOSX" ,
110
57
.windows = > "-DLUA_USE_WINDOWS" ,
111
58
else = > "-DLUA_USE_POSIX" ,
112
59
},
113
60
114
61
// Enable api check if desired
115
- if (apicheck ) "-DLUA_USE_APICHECK" else "" ,
116
- };
117
-
118
- const lua_source_files = switch (options .version ) {
119
- .lua_51 = > & lua_51_source_files ,
120
- .lua_52 = > & lua_52_source_files ,
121
- .lua_53 = > & lua_53_source_files ,
122
- .lua_54 = > & lua_54_source_files ,
62
+ if (optimize == .Debug ) "-DLUA_USE_APICHECK" else "" ,
123
63
};
124
-
125
64
for (lua_source_files ) | file | {
126
- const path = std .fs .path .join (b .allocator , &.{ dir (), lib_dir , file }) catch unreachable ;
127
- lua .addCSourceFile (path , & flags );
65
+ const path = std .fs .path .join (b .allocator , &.{ lib_dir , file }) catch unreachable ;
66
+ lib .addCSourceFile (path , & flags );
128
67
}
68
+ lib .linkLibC ();
69
+ b .installArtifact (lib );
70
+ lib .installHeader (b .pathJoin (&.{ lib_dir , "lua.h" }), "lua/lua.h" );
71
+ lib .installHeader (b .pathJoin (&.{ lib_dir , "lualib.h" }), "lua/lualib.h" );
72
+ lib .installHeader (b .pathJoin (&.{ lib_dir , "lauxlib.h" }), "lua/lauxlib.h" );
73
+
74
+ _ = b .addModule ("ziglua" , .{
75
+ .source_file = switch (lua_version ) {
76
+ .lua_51 = > .{ .path = "src/ziglua-5.1/lib.zig" },
77
+ .lua_52 = > .{ .path = "src/ziglua-5.2/lib.zig" },
78
+ .lua_53 = > .{ .path = "src/ziglua-5.3/lib.zig" },
79
+ .lua_54 = > .{ .path = "src/ziglua-5.4/lib.zig" },
80
+ },
81
+ });
82
+ // TODO: mod.addIncludePath(lib_dir);
83
+ // https://github.com/ziglang/zig/issues/14719
84
+
85
+ const tests = b .addTest (.{
86
+ .root_source_file = switch (lua_version ) {
87
+ .lua_51 = > .{ .path = "src/ziglua-5.1/tests.zig" },
88
+ .lua_52 = > .{ .path = "src/ziglua-5.2/tests.zig" },
89
+ .lua_53 = > .{ .path = "src/ziglua-5.3/tests.zig" },
90
+ .lua_54 = > .{ .path = "src/ziglua-5.4/tests.zig" },
91
+ },
92
+ .optimize = optimize ,
93
+ });
94
+ tests .addIncludePath (lib_dir );
95
+ tests .linkLibrary (lib );
129
96
130
- return lua ;
97
+ const test_step = b .step ("test" , "Run ziglua tests" );
98
+ test_step .dependOn (& tests .step );
131
99
}
132
100
133
101
const lua_51_source_files = [_ ][]const u8 {
0 commit comments