From d256d071e0dca88c7929aa018b22b66bbbae7dbd Mon Sep 17 00:00:00 2001 From: PiergiorgioZagaria Date: Wed, 27 Jul 2022 10:24:53 +0200 Subject: [PATCH] gamemode: WIP building gamemode from source --- build.zig | 5 ++-- gamemode-zig/build.zig | 34 ++++++++++++++++++++++++---- gamemode-zig/c/common/build-config.h | 4 ++++ 3 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 gamemode-zig/c/common/build-config.h diff --git a/build.zig b/build.zig index f6a5cf85..5d43ee29 100644 --- a/build.zig +++ b/build.zig @@ -230,8 +230,9 @@ pub const App = struct { if (target.os.tag == .linux) { exe.addPackage(gamemode.pkg); // TODO: choose between system lib vs buildlib? - gamemode.link(exe); - // gamemode.buildAndLink(b, exe); + gamemode.linkFromSystem(exe); + // gamemode.linkFromSource(b, exe, .{}); + // gamemode.linkFromBinary(exe); } break :blk exe; diff --git a/gamemode-zig/build.zig b/gamemode-zig/build.zig index 256f721d..1af01996 100644 --- a/gamemode-zig/build.zig +++ b/gamemode-zig/build.zig @@ -1,32 +1,56 @@ const std = @import("std"); +const LibExeObjStep = std.build.LibExeObjStep; pub const pkg = std.build.Pkg{ .name = "gamemode", .source = .{ .path = thisDir() ++ "/src/gamemode.zig" }, }; +pub const Options = struct { + libexecdir: []const u8 = "", + sysconfdir: []const u8 = "", + gamemode_version: []const u8 = "", + + // Seems to be the only option the c files for libgamemode use + have_fn_pidfd_open: bool = true, +}; + /// Link system library gamemode -pub fn link(exe: *std.build.LibExeObjStep) void { +pub fn linkFromSystem(exe: *LibExeObjStep) void { exe.linkSystemLibrary("gamemode"); } -// TODO: to build we still need to generate a config file called build_common.h -// (see https://github.com/FeralInteractive/gamemode/blob/4dc99dff76218718763a6b07fc1900fa6d1dafd9/meson.build) line 151 - /// Build and link gamemode -pub fn buildAndLink(b: *std.build.Builder, exe: *std.build.LibExeObjStep) void { +pub fn linkFromSource(b: *std.build.Builder, exe: *LibExeObjStep, opts: Options) void { + var build_config = std.fs.createFileAbsolute((comptime thisDir()) ++ "/c/common/build-config.h", .{}) catch unreachable; + defer build_config.close(); + const writer = build_config.writer(); + writer.print( + \\#define LIBEXECDIR "{s}" + \\#define SYSCONFDIR "{s}" + \\#define GAMEMODE_VERSION "{s}" + \\#define HAVE_FN_PIDFD_OPEN {} + , .{ opts.libexecdir, opts.sysconfdir, opts.gamemode_version, @boolToInt(opts.have_fn_pidfd_open) }) catch unreachable; + const lib_common = b.addStaticLibrary("common", null); lib_common.addCSourceFiles(&.{ (comptime thisDir()) ++ "/c/common/common-helpers.c", (comptime thisDir()) ++ "/c/common/common-pidfds.c" }, &.{}); + lib_common.addIncludePath((comptime thisDir()) ++ "/c/common/"); lib_common.linkLibC(); const lib_gamemode = b.addSharedLibrarySource("gamemode", null, .unversioned); lib_gamemode.addCSourceFile((comptime thisDir()) ++ "/c/client_impl.c", &.{}); + lib_gamemode.addIncludePath((comptime thisDir()) ++ "/c/common/"); lib_gamemode.linkLibC(); + lib_gamemode.linkSystemLibrary("dbus-1"); lib_gamemode.linkLibrary(lib_common); exe.linkLibrary(lib_gamemode); } +// TODO: +/// Build from provided shared library binary +// pub fn linkFromBinary(exe: *LibExeObjStep) void {} + fn thisDir() []const u8 { return std.fs.path.dirname(@src().file) orelse "."; } diff --git a/gamemode-zig/c/common/build-config.h b/gamemode-zig/c/common/build-config.h new file mode 100644 index 00000000..e311c300 --- /dev/null +++ b/gamemode-zig/c/common/build-config.h @@ -0,0 +1,4 @@ +#define LIBEXECDIR "" +#define SYSCONFDIR "" +#define GAMEMODE_VERSION "" +#define HAVE_FN_PIDFD_OPEN 1 \ No newline at end of file