Skip to content

Commit 1da9836

Browse files
committed
Merge branch 'pm'
2 parents 8061d1c + b4e665b commit 1da9836

File tree

3 files changed

+72
-98
lines changed

3 files changed

+72
-98
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
zig-cache/
2+
zig-out/

build.zig

Lines changed: 66 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const std = @import("std");
22

33
const Build = std.Build;
4-
const CompileStep = std.build.CompileStep;
54

65
pub const LuaVersion = enum {
76
lua_51,
@@ -11,123 +10,92 @@ pub const LuaVersion = enum {
1110
// lua_jit,
1211
};
1312

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-
2313
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 },
3228
},
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/",
7829
};
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",
9239
};
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;
10249
const flags = [_][]const u8{
10350
// Standard version used in Lua Makefile
10451
"-std=gnu99",
10552

10653
// Define target-specific macro
107-
switch (target.os.tag) {
54+
switch (os_tag) {
10855
.linux => "-DLUA_USE_LINUX",
10956
.macos => "-DLUA_USE_MACOSX",
11057
.windows => "-DLUA_USE_WINDOWS",
11158
else => "-DLUA_USE_POSIX",
11259
},
11360

11461
// 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 "",
12363
};
124-
12564
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);
12867
}
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);
12996

130-
return lua;
97+
const test_step = b.step("test", "Run ziglua tests");
98+
test_step.dependOn(&tests.step);
13199
}
132100

133101
const lua_51_source_files = [_][]const u8{

build.zig.zon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.{
2+
.name = "ziglua",
3+
.description = "Zig bindings for the Lua C API",
4+
.version = "0.1.0",
5+
}

0 commit comments

Comments
 (0)