rely on modules for build options

This commit is contained in:
Carmen 2025-09-24 02:42:56 +02:00
parent d6254e1434
commit e725cba50c
No known key found for this signature in database

229
build.zig
View file

@ -8,9 +8,6 @@ const version = std.SemanticVersion{
.minor = 4, .minor = 4,
.patch = 8, .patch = 8,
}; };
const lib_name = "lua";
const exe_name = lib_name ++ "_exe";
const compiler_name = "luac";
pub fn build(b: *Build) !void { pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
@ -25,150 +22,139 @@ pub fn build(b: *Build) !void {
const lua_src = b.dependency("lua", .{}); const lua_src = b.dependency("lua", .{});
const lib = b.addLibrary(.{ const static = b.addModule("staticlib", .{
.name = lib_name,
.linkage = .static,
.root_module = b.createModule(.{
.link_libc = true, .link_libc = true,
.optimize = optimize, .optimize = optimize,
.target = target, .target = target,
}),
}); });
const shared = if (build_shared) static.addCSourceFiles(.{
b.addLibrary(.{ .root = lua_src.path("src"),
.name = lib_name ++ "54", .files = &base_src,
.linkage = .dynamic, .flags = &cflags,
.root_module = b.createModule(.{ });
const shared = b.addModule("sharedlib", .{
.link_libc = true, .link_libc = true,
.optimize = optimize, .optimize = optimize,
.target = target, .target = target,
.strip = if (target.result.os.tag == .windows) true else null, .strip = if (target.result.os.tag == .windows) true else null,
}),
})
else
null;
const exe = b.addExecutable(.{
.name = exe_name,
.root_module = b.createModule(.{
.link_libc = true,
.optimize = optimize,
.target = target,
}),
}); });
const exec = b.addExecutable(.{ shared.addCSourceFiles(.{
.name = compiler_name,
.root_module = b.createModule(.{
.link_libc = true,
.optimize = optimize,
.target = target,
}),
});
if (!target.result.isMinGW()) {
lib.linkSystemLibrary("m");
exe.linkSystemLibrary("m");
exec.linkSystemLibrary("m");
}
const build_targets = [_]?*Build.Step.Compile{
lib,
exe,
exec,
shared,
};
// Common compile flags
for (&build_targets) |tr| {
if (tr == null)
continue;
const t = tr.?;
t.linkLibC();
t.addIncludePath(lua_src.path("src"));
switch (target.result.os.tag) {
.aix => {
t.root_module.addCMacro("LUA_USE_POSIX", "");
t.root_module.addCMacro("LUA_USE_DLOPEN", "");
t.linkSystemLibrary("dl");
},
.freebsd, .netbsd, .openbsd => {
t.root_module.addCMacro("LUA_USE_LINUX", "");
t.root_module.addCMacro("LUA_USE_READLINE", "");
t.addIncludePath(.{ .cwd_relative = "/usr/include/edit" });
t.linkSystemLibrary("edit");
},
.ios => {
t.root_module.addCMacro("LUA_USE_IOS", "");
},
.linux => {
t.root_module.addCMacro("LUA_USE_LINUX", "");
t.linkSystemLibrary("dl");
if (use_readline.?) {
t.root_module.addCMacro("LUA_USE_READLINE", "");
t.linkSystemLibrary("readline");
}
},
.macos => {
t.root_module.addCMacro("LUA_USE_MACOSX", "");
t.root_module.addCMacro("LUA_USE_READLINE", "");
t.linkSystemLibrary("readline");
},
.solaris => {
t.root_module.addCMacro("LUA_USE_POSIX", "");
t.root_module.addCMacro("LUA_USE_DLOPEN", "");
t.root_module.addCMacro("_REENTRANT", "");
t.linkSystemLibrary("dl");
},
else => {},
}
}
if (target.result.isMinGW()) {
lib.root_module.addCMacro("LUA_BUILD_AS_DLL", "");
exe.root_module.addCMacro("LUA_BUILD_AS_DLL", "");
}
if (shared) |s| {
s.addCSourceFiles(.{
.root = lua_src.path("src"), .root = lua_src.path("src"),
.files = &base_src, .files = &base_src,
.flags = &cflags, .flags = &cflags,
}); });
s.installHeadersDirectory( const exe = b.addModule("interpreter", .{
lua_src.path("src"), .link_libc = true,
"", .optimize = optimize,
.{ .include_extensions = &lua_inc }, .target = target,
);
}
lib.addCSourceFiles(.{
.root = lua_src.path("src"),
.files = &base_src,
.flags = &cflags,
}); });
lib.installHeadersDirectory(
lua_src.path("src"),
"",
.{ .include_extensions = &lua_inc },
);
exe.addCSourceFile(.{ exe.addCSourceFile(.{
.file = lua_src.path("src/lua.c"), .file = lua_src.path("src/lua.c"),
.flags = &cflags, .flags = &cflags,
}); });
const exec = b.addModule("compiler", .{
.link_libc = true,
.optimize = optimize,
.target = target,
});
exec.addCSourceFile(.{ exec.addCSourceFile(.{
.file = lua_src.path("src/luac.c"), .file = lua_src.path("src/luac.c"),
.flags = &cflags, .flags = &cflags,
}); });
if (shared) |s| { // Common compile flags
exe.linkLibrary(s); for (&[_]*Build.Module{ static, shared, exe, exec }) |mod| {
b.installArtifact(s); const link = mod != shared;
} else {
exe.linkLibrary(lib); mod.addIncludePath(lua_src.path("src"));
b.installArtifact(lib); if (link and !target.result.isMinGW())
mod.linkSystemLibrary("m", .{});
switch (target.result.os.tag) {
.aix => {
mod.addCMacro("LUA_USE_POSIX", "");
mod.addCMacro("LUA_USE_DLOPEN", "");
if (link)
mod.linkSystemLibrary("dl", .{});
},
.freebsd, .netbsd, .openbsd => {
mod.addCMacro("LUA_USE_LINUX", "");
mod.addCMacro("LUA_USE_READLINE", "");
mod.addIncludePath(.{ .cwd_relative = "/usr/include/edit" });
if (link)
mod.linkSystemLibrary("edit", .{});
},
.ios => {
mod.addCMacro("LUA_USE_IOS", "");
},
.linux => {
mod.addCMacro("LUA_USE_LINUX", "");
if (link)
mod.linkSystemLibrary("dl", .{});
if (use_readline.?) {
mod.addCMacro("LUA_USE_READLINE", "");
if (link)
mod.linkSystemLibrary("readline", .{});
}
},
.macos => {
mod.addCMacro("LUA_USE_MACOSX", "");
mod.addCMacro("LUA_USE_READLINE", "");
if (link)
mod.linkSystemLibrary("readline", .{});
},
.solaris => {
mod.addCMacro("LUA_USE_POSIX", "");
mod.addCMacro("LUA_USE_DLOPEN", "");
mod.addCMacro("_REENTRANT", "");
if (link)
mod.linkSystemLibrary("dl", .{});
},
else => {},
}
}
if (target.result.isMinGW()) {
static.addCMacro("LUA_BUILD_AS_DLL", "");
exe.addCMacro("LUA_BUILD_AS_DLL", "");
} }
b.installArtifact(exe); const sharedlib = b.addLibrary(.{
exec.linkLibrary(lib); .name = "lua" ++ &[_]u8{
b.installArtifact(exec); '0' + version.major, '0' + version.minor,
},
.root_module = shared,
.version = version,
.linkage = .dynamic,
});
b.installArtifact(sharedlib);
const staticlib =
b.addLibrary(.{
.name = "lua",
.root_module = static,
.version = version,
.linkage = .static,
});
b.installArtifact(staticlib);
exe.linkLibrary(if (build_shared) sharedlib else staticlib);
const lua_exe = b.addExecutable(.{
.name = "lua_exe",
.version = version,
.root_module = exe,
});
b.installArtifact(lua_exe);
exec.linkLibrary(staticlib);
const luac_exe = b.addExecutable(.{
.name = "luac",
.version = version,
.root_module = exe,
});
b.installArtifact(luac_exe);
b.installDirectory(.{ b.installDirectory(.{
.source_dir = lua_src.path("doc"), .source_dir = lua_src.path("doc"),
@ -178,7 +164,8 @@ pub fn build(b: *Build) !void {
}); });
const run_step = b.step("run", "run lua interpreter"); const run_step = b.step("run", "run lua interpreter");
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(lua_exe);
if (b.args) |args| run_cmd.addArgs(args);
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
const unpack_step = b.step("unpack", "unpack source"); const unpack_step = b.step("unpack", "unpack source");
const unpack_cmd = b.addInstallDirectory(.{ const unpack_cmd = b.addInstallDirectory(.{