Skip to content

Commit

Permalink
Rename LuaVersion fields and link lua to the module
Browse files Browse the repository at this point in the history
This links the compiled lua library to the ziglua module so it doesn't
need to be linked manually in other projects.
  • Loading branch information
Nathan Craddock committed Jan 9, 2024
1 parent cfd1f59 commit e20056a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
50 changes: 24 additions & 26 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const Build = std.Build;
const Step = std.Build.Step;

pub const LuaVersion = enum {
@"lua5.1",
@"lua5.2",
@"lua5.3",
@"lua5.4",
lua51,
lua52,
lua53,
lua54,
luau,
};

Expand All @@ -18,44 +18,43 @@ pub fn build(b: *Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const lua_version = b.option(LuaVersion, "version", "Lua API and library version") orelse .@"lua5.4";
const lua_version = b.option(LuaVersion, "version", "Lua API and library version") orelse .lua54;
const shared = b.option(bool, "shared", "Build shared library instead of static") orelse false;

const upstream = b.dependency(@tagName(lua_version), .{});

// Zig module
const ziglua = b.addModule("ziglua", .{
.root_source_file = switch (lua_version) {
.@"lua5.1" => .{ .path = "src/ziglua-5.1/lib.zig" },
.@"lua5.2" => .{ .path = "src/ziglua-5.2/lib.zig" },
.@"lua5.3" => .{ .path = "src/ziglua-5.3/lib.zig" },
.@"lua5.4" => .{ .path = "src/ziglua-5.4/lib.zig" },
.lua51 => .{ .path = "src/ziglua-5.1/lib.zig" },
.lua52 => .{ .path = "src/ziglua-5.2/lib.zig" },
.lua53 => .{ .path = "src/ziglua-5.3/lib.zig" },
.lua54 => .{ .path = "src/ziglua-5.4/lib.zig" },
.luau => .{ .path = "src/zigluau/lib.zig" },
},
});
ziglua.addIncludePath(upstream.path("src"));

const lib = switch (lua_version) {
else => buildLua(b, target, optimize, upstream, lua_version, shared),
.luau => buildLuau(b, target, optimize, shared),
else => buildLua(b, target, optimize, upstream, lua_version, shared),
};

b.installArtifact(lib);
ziglua.addIncludePath(upstream.path("src"));
ziglua.linkLibrary(lib);

// Tests
const tests = b.addTest(.{
.root_source_file = switch (lua_version) {
.@"lua5.1" => .{ .path = "src/ziglua-5.1/tests.zig" },
.@"lua5.2" => .{ .path = "src/ziglua-5.2/tests.zig" },
.@"lua5.3" => .{ .path = "src/ziglua-5.3/tests.zig" },
.@"lua5.4" => .{ .path = "src/ziglua-5.4/tests.zig" },
.lua51 => .{ .path = "src/ziglua-5.1/tests.zig" },
.lua52 => .{ .path = "src/ziglua-5.2/tests.zig" },
.lua53 => .{ .path = "src/ziglua-5.3/tests.zig" },
.lua54 => .{ .path = "src/ziglua-5.4/tests.zig" },
.luau => .{ .path = "src/zigluau/tests.zig" },
},
.target = target,
.optimize = optimize,
});
tests.root_module.addImport("ziglua", ziglua);
tests.linkLibrary(lib);

const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run ziglua tests");
Expand All @@ -75,7 +74,6 @@ pub fn build(b: *Build) void {
.optimize = optimize,
});
exe.root_module.addImport("ziglua", ziglua);
exe.linkLibrary(lib);

const artifact = b.addInstallArtifact(exe, .{});
const exe_step = b.step(b.fmt("install-example-{s}", .{example[0]}), b.fmt("Install {s} example", .{example[0]}));
Expand All @@ -97,10 +95,10 @@ fn buildLua(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.Optim
.target = target,
.optimize = optimize,
.version = switch (lua_version) {
.@"lua5.1" => std.SemanticVersion{ .major = 5, .minor = 1, .patch = 5 },
.@"lua5.2" => std.SemanticVersion{ .major = 5, .minor = 2, .patch = 4 },
.@"lua5.3" => std.SemanticVersion{ .major = 5, .minor = 3, .patch = 6 },
.@"lua5.4" => std.SemanticVersion{ .major = 5, .minor = 4, .patch = 6 },
.lua51 => std.SemanticVersion{ .major = 5, .minor = 1, .patch = 5 },
.lua52 => std.SemanticVersion{ .major = 5, .minor = 2, .patch = 4 },
.lua53 => std.SemanticVersion{ .major = 5, .minor = 3, .patch = 6 },
.lua54 => std.SemanticVersion{ .major = 5, .minor = 4, .patch = 6 },
else => unreachable,
},
};
Expand Down Expand Up @@ -128,10 +126,10 @@ fn buildLua(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.Optim
};

const lua_source_files = switch (lua_version) {
.@"lua5.1" => &lua_51_source_files,
.@"lua5.2" => &lua_52_source_files,
.@"lua5.3" => &lua_53_source_files,
.@"lua5.4" => &lua_54_source_files,
.lua51 => &lua_51_source_files,
.lua52 => &lua_52_source_files,
.lua53 => &lua_53_source_files,
.lua54 => &lua_54_source_files,
else => unreachable,
};
for (lua_source_files) |file| {
Expand Down
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
.paths = .{ "" },

.dependencies = .{
.@"lua5.1" = .{
.lua51 = .{
.url = "https://www.lua.org/ftp/lua-5.1.5.tar.gz",
.hash = "1220089572fb380fb4679b16421fc53851a8226bcebc9ce44463a0f4ada5c9bd737f",
},

.@"lua5.2" = .{
.lua52 = .{
.url = "https://www.lua.org/ftp/lua-5.2.4.tar.gz",
.hash = "1220d5b2b39738f0644d9ed5b7431973f1a16b937ef86d4cf85887ef3e9fda7a3379",
},

.@"lua5.3" = .{
.lua53 = .{
.url = "https://www.lua.org/ftp/lua-5.3.6.tar.gz",
.hash = "1220937a223531ef6b3fea8f653dc135310b0e84805e7efa148870191f5ab915c828",
},

.@"lua5.4" = .{
.lua54 = .{
.url = "https://www.lua.org/ftp/lua-5.4.6.tar.gz",
.hash = "1220f93ada1fa077ab096bf88a5b159ad421dbf6a478edec78ddb186d0c21d3476d9",
},
Expand Down
8 changes: 4 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.PHONY: docs

test:
zig build test --summary failures -Dversion=lua_51
zig build test --summary failures -Dversion=lua_52
zig build test --summary failures -Dversion=lua_53
zig build test --summary failures -Dversion=lua_54
zig build test --summary failures -Dversion=lua51
zig build test --summary failures -Dversion=lua52
zig build test --summary failures -Dversion=lua53
zig build test --summary failures -Dversion=lua54
zig build test --summary failures -Dversion=luau

zig build install-example-interpreter
Expand Down

0 comments on commit e20056a

Please sign in to comment.