Files
ghostty/pkg/gtk4-layer-shell/build.zig
Chris Marchesi e8525c0fd9 Update to Zig 0.16.0
This commit represents the majority of the work necessary to upgrade
Ghostty to use Zig 0.16.0.

Key parts:

* In addition to its previous responsibilities, the global state now
  houses state for global I/O implementations and the process
  environment. It is now also utilized in the main application along
  with the C library. Where necessary, global state is isolated from key
  parts of the implementation (e.g., in libghostty subsystems), and it's
  expected that this list will grow.

* We currently manage our own C translation layer where necessary. In
  these cases, cImport has been removed in favor of the new external
  translate-c package. Due to fixes that have needed be made to properly
  translate the dependencies that were swapped out, as mentioned, we
  have had to backport fixes from the current translate-c package (and
  the upstream Arocc dependency). We will host this ourselves until Zig
  0.17.0 is released with these fixes.

* Where necessary (only a small number of cases), some stdlib code from
  0.15.2 (and even from 0.17.0) has been taken, adopted, and vendored in
  lib/compat.

Co-authored-by: Leah Amelia Chen <hi@pluie.me>
2026-07-21 12:35:05 -07:00

148 lines
5.0 KiB
Zig

const std = @import("std");
const version = @import("build.zig.zon").version;
const dynamic_link_opts: std.Build.Module.LinkSystemLibraryOptions = .{
.preferred_link_mode = .dynamic,
.search_strategy = .mode_first,
};
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Zig API
const module = b.addModule("gtk4-layer-shell", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
translate: {
const translate_c = b.lazyImport(@This(), "translate_c") orelse break :translate;
const translate_c_dep = b.lazyDependency("translate_c", .{}) orelse break :translate;
const Translator = translate_c.Translator;
const link_system_libs_full: [2]Translator.LinkSystemLib = .{
.{ .name = "gtk4", .options = dynamic_link_opts },
.{ .name = "gtk4-layer-shell-0", .options = dynamic_link_opts },
};
const headers = Translator.init(translate_c_dep, .{
.c_source_file = b.addWriteFiles().add("c.h",
\\#include <gtk4-layer-shell.h>
),
.target = target,
.optimize = optimize,
.link_system_libs = if (b.systemIntegrationOption("gtk4-layer-shell", .{}))
&link_system_libs_full
else
link_system_libs_full[0..1],
});
module.addImport("c", headers.mod);
}
if (!b.systemIntegrationOption("gtk4-layer-shell", .{})) {
_ = try buildLib(b, module, .{
.target = target,
.optimize = optimize,
});
}
}
fn buildLib(b: *std.Build, module: *std.Build.Module, options: anytype) !*std.Build.Step.Compile {
const lib_version = try std.SemanticVersion.parse(version);
const target = options.target;
const optimize = options.optimize;
// Shared library
const lib = b.addLibrary(.{
.name = "gtk4-layer-shell",
.linkage = .dynamic,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
b.installArtifact(lib);
// We need to call both lazy dependencies to tell Zig we need both
const upstream_ = b.lazyDependency("gtk4_layer_shell", .{});
const wayland_protocols_ = b.lazyDependency("wayland_protocols", .{});
const upstream = upstream_ orelse return lib;
const wayland_protocols = wayland_protocols_ orelse return lib;
lib.root_module.addIncludePath(upstream.path("include"));
lib.root_module.addIncludePath(upstream.path("src"));
module.addIncludePath(upstream.path("include"));
// GTK
lib.root_module.linkSystemLibrary("gtk4", dynamic_link_opts);
// Wayland headers and source files
{
const protocols = [_]struct { []const u8, std.Build.LazyPath }{
.{
"wlr-layer-shell-unstable-v1",
upstream.path("protocol/wlr-layer-shell-unstable-v1.xml"),
},
.{
"xdg-shell",
wayland_protocols.path("stable/xdg-shell/xdg-shell.xml"),
},
// Even though we don't use session lock, we still need its headers
.{
"ext-session-lock-v1",
wayland_protocols.path("staging/ext-session-lock/ext-session-lock-v1.xml"),
},
};
const wf = b.addWriteFiles();
for (protocols) |protocol| {
const name, const xml = protocol;
const header_scanner = b.addSystemCommand(&.{ "wayland-scanner", "client-header" });
header_scanner.addFileArg(xml);
_ = wf.addCopyFile(
header_scanner.addOutputFileArg(name),
b.fmt("{s}-client.h", .{name}),
);
const source_scanner = b.addSystemCommand(&.{ "wayland-scanner", "private-code" });
source_scanner.addFileArg(xml);
const source = source_scanner.addOutputFileArg(b.fmt("{s}.c", .{name}));
lib.root_module.addCSourceFile(.{ .file = source });
}
lib.root_module.addIncludePath(wf.getDirectory());
}
lib.installHeadersDirectory(
upstream.path("include"),
"",
.{ .include_extensions = &.{".h"} },
);
// Certain files relating to session lock were removed as we don't use them
const srcs: []const []const u8 = &.{
"gtk4-layer-shell.c",
"layer-surface.c",
"libwayland-shim.c",
"registry.c",
"stolen-from-libwayland.c",
"stubbed-surface.c",
"xdg-surface-server.c",
};
lib.root_module.addCSourceFiles(.{
.root = upstream.path("src"),
.files = srcs,
.flags = &.{
b.fmt("-DGTK_LAYER_SHELL_MAJOR={}", .{lib_version.major}),
b.fmt("-DGTK_LAYER_SHELL_MINOR={}", .{lib_version.minor}),
b.fmt("-DGTK_LAYER_SHELL_MICRO={}", .{lib_version.patch}),
},
});
return lib;
}