shared builds on linux

This commit is contained in:
Jacob Stannix 2024-08-15 14:06:54 -06:00
parent 5727c0bc04
commit ee2e87371c

View file

@ -25,16 +25,18 @@ pub fn build(b: *Build) !void {
const lua_src = b.dependency("lua", .{}); const lua_src = b.dependency("lua", .{});
const lib = if (!build_shared) const lib =
b.addStaticLibrary(artifactOptions( b.addStaticLibrary(artifactOptions(
.{ .shared = false }, .{ .shared = false },
.{ .target = target, .optimize = optimize }, .{ .target = target, .optimize = optimize },
)) ));
else const shared = if (build_shared)
b.addSharedLibrary(artifactOptions( b.addSharedLibrary(artifactOptions(
.{ .shared = true }, .{ .shared = true },
.{ .target = target, .optimize = optimize }, .{ .target = target, .optimize = optimize },
)); ))
else
null;
const exe = b.addExecutable(artifactOptions(.exe, .{ const exe = b.addExecutable(artifactOptions(.exe, .{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
@ -46,16 +48,21 @@ pub fn build(b: *Build) !void {
if (!target.result.isMinGW()) { if (!target.result.isMinGW()) {
lib.linkSystemLibrary("m"); lib.linkSystemLibrary("m");
exe.linkSystemLibrary("m"); exe.linkSystemLibrary("m");
exec.linkSystemLibrary("m");
} }
exec.linkSystemLibrary("m"); const build_targets = [_]?*Build.Step.Compile{
const build_targets = [_]*Build.Step.Compile{
lib, lib,
exe, exe,
exec, exec,
shared,
}; };
// Common compile flags // Common compile flags
for (&build_targets) |t| { for (&build_targets) |tr| {
if (tr == null)
continue;
const t = tr.?;
t.linkLibC(); t.linkLibC();
t.addIncludePath(lua_src.path("src"));
switch (target.result.os.tag) { switch (target.result.os.tag) {
.aix => { .aix => {
t.defineCMacro("LUA_USE_POSIX", null); t.defineCMacro("LUA_USE_POSIX", null);
@ -97,8 +104,20 @@ pub fn build(b: *Build) !void {
lib.defineCMacro("LUA_BUILD_AS_DLL", null); lib.defineCMacro("LUA_BUILD_AS_DLL", null);
exe.defineCMacro("LUA_BUILD_AS_DLL", null); exe.defineCMacro("LUA_BUILD_AS_DLL", null);
} }
if (shared) |s| {
s.addCSourceFiles(.{
.root = lua_src.path("src"),
.files = &base_src,
.flags = &cflags,
});
s.installHeadersDirectory(
lua_src.path("src"),
"",
.{ .include_extensions = &lua_inc },
);
}
lib.addIncludePath(lua_src.path("src"));
lib.addCSourceFiles(.{ lib.addCSourceFiles(.{
.root = lua_src.path("src"), .root = lua_src.path("src"),
.files = &base_src, .files = &base_src,
@ -121,10 +140,20 @@ pub fn build(b: *Build) !void {
.flags = &cflags, .flags = &cflags,
}); });
exe.linkLibrary(lib); // if (build_shared) {
// exe.addRPath(.{ .cwd_relative = b.getInstallPath(.{ .lib = {} }, "") });
// exec.addRPath(.{ .cwd_relative = b.getInstallPath(.{ .lib = {} }, "") });
// }
if (shared) |s| {
exe.linkLibrary(s);
b.installArtifact(s);
} else {
exe.linkLibrary(lib);
b.installArtifact(lib);
}
exec.linkLibrary(lib); exec.linkLibrary(lib);
b.installArtifact(lib);
b.installArtifact(exe); b.installArtifact(exe);
b.installArtifact(exec); b.installArtifact(exec);
b.installDirectory(.{ b.installDirectory(.{
@ -137,6 +166,13 @@ 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(exe);
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
const unpack_step = b.step("unpack", "unpack source");
const unpack_cmd = b.addInstallDirectory(.{
.source_dir = lua_src.path(""),
.install_dir = .prefix,
.install_subdir = "",
});
unpack_step.dependOn(&unpack_cmd.step);
} }
const ArtifactTarget = union(enum) { const ArtifactTarget = union(enum) {
// True if shared options // True if shared options
@ -159,6 +195,12 @@ fn artifactOptions(comptime options: ArtifactTarget, opts: ArtifactTargetOptions
return switch (options) { return switch (options) {
.shared => |shared| if (shared) blk: { .shared => |shared| if (shared) blk: {
switch (t) { switch (t) {
.windows => break :blk .{
.name = lib_name ++ "54",
.target = opts.target,
.optimize = opts.optimize,
.strip = true,
},
else => break :blk .{ else => break :blk .{
.name = lib_name, .name = lib_name,
.target = opts.target, .target = opts.target,
@ -192,6 +234,7 @@ fn artifactOptions(comptime options: ArtifactTarget, opts: ArtifactTargetOptions
} }
const cflags = [_][]const u8{ const cflags = [_][]const u8{
"-std=gnu99",
"-Wall", "-Wall",
"-Wextra", "-Wextra",
}; };