gamemode: Fix overcomplications and @import with symlinks

This commit is contained in:
PiergiorgioZagaria 2022-07-28 10:38:17 +02:00 committed by Stephen Gutekanst
parent 38a44ea3b3
commit 4ee9ba1000
6 changed files with 13 additions and 102 deletions

View file

@ -1,13 +1,12 @@
const std = @import("std");
const builtin = @import("builtin");
pub const gpu = @import("gpu/build.zig");
const gpu_dawn = @import("gpu-dawn/build.zig");
pub const glfw = @import("glfw/build.zig");
const gpu_dawn = @import("gpu/libs/mach-gpu-dawn/build.zig");
pub const glfw = @import("gpu/libs/mach-glfw/build.zig");
pub const ecs = @import("ecs/build.zig");
const freetype = @import("freetype/build.zig");
const sysaudio = @import("sysaudio/build.zig");
const sysjs = @import("sysjs/build.zig");
const gamemode = @import("gamemode-zig/build.zig");
const Pkg = std.build.Pkg;
pub fn build(b: *std.build.Builder) void {
@ -17,7 +16,7 @@ pub fn build(b: *std.build.Builder) void {
const gpu_dawn_options = gpu_dawn.Options{
.from_source = b.option(bool, "dawn-from-source", "Build Dawn from source") orelse false,
};
const options = Options{ .gpu_dawn_options = gpu_dawn_options };
const options = gpu.Options{ .gpu_dawn_options = gpu_dawn_options };
const main_tests = b.addTestExe("mach-tests", "src/main.zig");
main_tests.setBuildMode(mode);
@ -29,7 +28,7 @@ pub fn build(b: *std.build.Builder) void {
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.run().step);
test_step.dependOn(&gpu.testStep(b, mode, target, @bitCast(gpu.Options, options)).step);
test_step.dependOn(&gpu.testStep(b, mode, target, options).step);
test_step.dependOn(&gpu_dawn.testStep(b, mode, target).step);
test_step.dependOn(&glfw.testStep(b, mode, target).step);
test_step.dependOn(&ecs.testStep(b, mode, target).step);
@ -138,21 +137,12 @@ pub fn build(b: *std.build.Builder) void {
lib.addPackage(app_pkg);
lib.addPackage(gpu.pkg);
lib.addPackage(glfw.pkg);
const gpu_options = gpu.Options{
.glfw_options = @bitCast(@import("gpu/libs/mach-glfw/build.zig").Options, options.glfw_options),
.gpu_dawn_options = @bitCast(@import("gpu/libs/mach-gpu-dawn/build.zig").Options, options.gpu_dawn_options),
};
glfw.link(b, lib, options.glfw_options);
gpu.link(b, lib, gpu_options);
gpu.link(b, lib, options);
lib.setOutputDir("./libmach/build");
lib.install();
}
pub const Options = struct {
glfw_options: glfw.Options = .{},
gpu_dawn_options: gpu_dawn.Options = .{},
};
const ExampleDefinition = struct {
name: []const u8,
packages: []const Pkg = &[_]Pkg{},
@ -228,11 +218,7 @@ pub const App = struct {
exe.addPackage(glfw.pkg);
if (target.os.tag == .linux) {
exe.addPackage(gamemode.pkg);
// TODO: choose between system lib vs buildlib?
gamemode.linkFromSystem(exe);
// gamemode.linkFromSource(b, exe, .{});
// gamemode.linkFromBinary(exe);
exe.addPackagePath("gamemode", "gamemode-zig/gamemode.zig");
}
break :blk exe;
@ -297,15 +283,10 @@ pub const App = struct {
}
}
pub fn link(app: *const App, options: Options) void {
const gpu_options = gpu.Options{
.glfw_options = @bitCast(@import("gpu/libs/mach-glfw/build.zig").Options, options.glfw_options),
.gpu_dawn_options = @bitCast(@import("gpu/libs/mach-gpu-dawn/build.zig").Options, options.gpu_dawn_options),
};
pub fn link(app: *const App, options: gpu.Options) void {
if (app.platform != .web) {
glfw.link(app.b, app.step, options.glfw_options);
gpu.link(app.b, app.step, gpu_options);
gpu.link(app.b, app.step, options);
}
}

View file

@ -1,56 +0,0 @@
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 linkFromSystem(exe: *LibExeObjStep) void {
exe.linkSystemLibrary("gamemode");
}
/// Build and link gamemode
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 ".";
}

View file

@ -1,20 +1,6 @@
#!/usr/bin/env bash
rm ./c/*
cd ./c
rm ./gamemode_client.h
wget "https://github.com/FeralInteractive/gamemode/raw/master/lib/gamemode_client.h"
wget "https://github.com/FeralInteractive/gamemode/raw/master/lib/client_impl.c"
wget "https://github.com/FeralInteractive/gamemode/raw/master/lib/client_loader.c"
cd ./common
wget "https://github.com/FeralInteractive/gamemode/raw/b11d2912e280acb87d9ad114d6c7cd8846c4ef02/common/common-helpers.c"
wget "https://github.com/FeralInteractive/gamemode/raw/b11d2912e280acb87d9ad114d6c7cd8846c4ef02/common/common-pidfds.c"
wget "https://github.com/FeralInteractive/gamemode/raw/b11d2912e280acb87d9ad114d6c7cd8846c4ef02/common/common-helpers.h"
wget "https://github.com/FeralInteractive/gamemode/raw/b11d2912e280acb87d9ad114d6c7cd8846c4ef02/common/common-pidfds.h"
cd ../..
# The output from translate-c isn't perfect, so we need this fix
zig translate-c ./c/gamemode_client.h -lc | sed "s#functor\(.*\)@alignCast\(.*\),\(.*\))#functor\1\3#" > ./src/gamemode_client.zig
rm -r ./zig-cache
zig translate-c ./gamemode_client.h -lc | sed "s#functor\(.*\)@alignCast\(.*\),\(.*\))#functor\1\3#" > ./gamemode_client.zig

View file

@ -644,7 +644,7 @@ pub inline fn gamemode_error_string() [*c]const u8 {
_ = @sizeOf(c_int);
break :blk blk_1: {
break :blk_1 if (REAL_internal_gamemode_error_string != @ptrCast(api_call_return_cstring, @alignCast(@import("std").meta.alignment(fn () callconv(.C) [*c]const u8), @intToPtr(?*anyopaque, @as(c_int, 0))))) {} else {
__assert_fail("REAL_internal_gamemode_error_string != NULL", "./c/gamemode_client.h", @bitCast(c_uint, @as(c_int, 237)), "const char *gamemode_error_string(void)");
__assert_fail("REAL_internal_gamemode_error_string != NULL", "gamemode_client.h", @bitCast(c_uint, @as(c_int, 237)), "const char *gamemode_error_string(void)");
};
};
};
@ -658,7 +658,7 @@ pub inline fn gamemode_request_start() c_int {
_ = @sizeOf(c_int);
break :blk blk_1: {
break :blk_1 if (REAL_internal_gamemode_request_start != @ptrCast(api_call_return_int, @alignCast(@import("std").meta.alignment(fn () callconv(.C) c_int), @intToPtr(?*anyopaque, @as(c_int, 0))))) {} else {
__assert_fail("REAL_internal_gamemode_request_start != NULL", "./c/gamemode_client.h", @bitCast(c_uint, @as(c_int, 263)), "int gamemode_request_start(void)");
__assert_fail("REAL_internal_gamemode_request_start != NULL", "gamemode_client.h", @bitCast(c_uint, @as(c_int, 263)), "int gamemode_request_start(void)");
};
};
};
@ -675,7 +675,7 @@ pub inline fn gamemode_request_end() c_int {
_ = @sizeOf(c_int);
break :blk blk_1: {
break :blk_1 if (REAL_internal_gamemode_request_end != @ptrCast(api_call_return_int, @alignCast(@import("std").meta.alignment(fn () callconv(.C) c_int), @intToPtr(?*anyopaque, @as(c_int, 0))))) {} else {
__assert_fail("REAL_internal_gamemode_request_end != NULL", "./c/gamemode_client.h", @bitCast(c_uint, @as(c_int, 292)), "int gamemode_request_end(void)");
__assert_fail("REAL_internal_gamemode_request_end != NULL", "gamemode_client.h", @bitCast(c_uint, @as(c_int, 292)), "int gamemode_request_end(void)");
};
};
};