Skip to content

Commit

Permalink
build: update to Zig 0.11.0
Browse files Browse the repository at this point in the history
Signed-off-by: Soc Virnyl Estela <[email protected]>
  • Loading branch information
uncomfyhalomacro authored and ifreund committed Oct 25, 2023
1 parent 562ec1e commit d9db2ce
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 77 deletions.
8 changes: 4 additions & 4 deletions .builds/alpine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ sources:
- https://github.com/ifreund/waylock
tasks:
- install_deps: |
wget -nv https://ziglang.org/download/0.10.0/zig-linux-x86_64-0.10.0.tar.xz
tar -xvf zig-linux-x86_64-0.10.0.tar.xz 1>/dev/null
sudo mv zig-linux-x86_64-0.10.0/zig /usr/bin/
sudo mv zig-linux-x86_64-0.10.0/lib /usr/lib/zig
wget -nv https://ziglang.org/download/0.11.0/zig-linux-x86_64-0.11.0.tar.xz
tar -xvf zig-linux-x86_64-0.11.0.tar.xz 1>/dev/null
sudo mv zig-linux-x86_64-0.11.0/zig /usr/bin/
sudo mv zig-linux-x86_64-0.11.0/lib /usr/lib/zig
- build: |
cd waylock
zig build -Dman-pages
Expand Down
8 changes: 4 additions & 4 deletions .builds/archlinux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ sources:
- https://github.com/ifreund/waylock
tasks:
- install_deps: |
wget -nv https://ziglang.org/download/0.10.0/zig-linux-x86_64-0.10.0.tar.xz
tar -xvf zig-linux-x86_64-0.10.0.tar.xz 1>/dev/null
sudo mv zig-linux-x86_64-0.10.0/zig /usr/bin/
sudo mv zig-linux-x86_64-0.10.0/lib /usr/lib/zig
wget -nv https://ziglang.org/download/0.11.0/zig-linux-x86_64-0.11.0.tar.xz
tar -xvf zig-linux-x86_64-0.11.0.tar.xz 1>/dev/null
sudo mv zig-linux-x86_64-0.11.0/zig /usr/bin/
sudo mv zig-linux-x86_64-0.11.0/lib /usr/lib/zig
- build: |
cd waylock
zig build -Dman-pages
Expand Down
8 changes: 4 additions & 4 deletions .builds/freebsd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ sources:
- https://gitlab.freedesktop.org/wlroots/wlroots
tasks:
- install_deps: |
wget -nv https://ziglang.org/download/0.10.0/zig-freebsd-x86_64-0.10.0.tar.xz
tar -xvf zig-freebsd-x86_64-0.10.0.tar.xz 1>/dev/null
sudo mv zig-freebsd-x86_64-0.10.0/zig /usr/bin/
sudo mv zig-freebsd-x86_64-0.10.0/lib /usr/lib/zig
wget -nv https://ziglang.org/download/0.11.0/zig-freebsd-x86_64-0.11.0.tar.xz
tar -xvf zig-freebsd-x86_64-0.11.0.tar.xz 1>/dev/null
sudo mv zig-freebsd-x86_64-0.11.0/zig /usr/bin/
sudo mv zig-freebsd-x86_64-0.11.0/lib /usr/lib/zig
- build: |
cd waylock
zig build -Dman-pages
Expand Down
14 changes: 7 additions & 7 deletions PACKAGING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ in the first place. For greatest effect, both may be used.
- `--libc my_libc.txt`: Set system libc paths for cross compilation. Run
`zig libc` to see a documented template for what this file should contain.

- Enable compiler optimizations:
- Enable compiler optimizations with `-Doptimize`:

- `-Drelease-safe`: Keep all assertions and runtime safety checks active.
- `ReleaseSafe`: Keep all assertions and runtime safety checks active.

- `-Drelease-fast`: Optimize for execution speed, disable all assertions
- `ReleaseFast`: Optimize for execution speed, disable all assertions
and runtime safety checks.

- `-Drelease-small`: Optimize for binary size, disable all assertions and
- `ReleaseSmall`: Optimize for binary size, disable all assertions and
runtime safety checks.

Please use `-Drelease-safe` when building waylock for general use. This
Please use `ReleaseSafe` when building waylock for general use. This
software is not at all demanding when it comes to CPU execution speed and the
increased safety is more than worth the binary size trade-off in my opinion.

Expand Down Expand Up @@ -107,7 +107,7 @@ install() {

Build for the host architecture and libc ABI:
```bash
DESTDIR=/foo/bar zig build -Drelease-safe -Dcpu=baseline \
DESTDIR=/foo/bar zig build -Doptimize=ReleaseSafe -Dcpu=baseline \
-Dstrip -Dpie --prefix /usr install
```

Expand All @@ -126,7 +126,7 @@ DESTDIR="/foo/bar" zig build \
--sysroot "${XBPS_CROSS_BASE}" \
--libc xbps_zig_libc.txt \
-Dtarget=aarch64-linux-musl -Dcpu=baseline \
-Drelease-safe -Dstrip -Dpie \
-Doptimize=ReleaseSafe -Dstrip -Dpie \
--prefix /usr install
```

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ git submodule update --init
To compile waylock first ensure that you have the following dependencies
installed:

- [zig](https://ziglang.org/download/) 0.10
- [zig](https://ziglang.org/download/) 0.11.0
- wayland
- wayland-protocols
- xkbcommon
Expand All @@ -38,7 +38,7 @@ installed:
Then run, for example:

```
zig build -Drelease-safe --prefix /usr install
zig build -Doptimize=ReleaseSafe --prefix /usr install
```

Note that PAM will only use configuration files in the system directory,
Expand Down
61 changes: 36 additions & 25 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
const std = @import("std");
const assert = std.debug.assert;
const zbs = std.build;
const Build = std.Build;
const Step = std.Build.Step;
const fs = std.fs;
const mem = std.mem;

const ScanProtocolsStep = @import("deps/zig-wayland/build.zig").ScanProtocolsStep;
const Scanner = @import("deps/zig-wayland/build.zig").Scanner;

/// While a waylock release is in development, this string should contain the version in
/// development with the "-dev" suffix.
/// When a release is tagged, the "-dev" suffix should be removed for the commit that gets tagged.
/// Directly after the tagged commit, the version should be bumped and the "-dev" suffix added.
const version = "0.7.0-dev";

pub fn build(b: *zbs.Builder) !void {
pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const optimize = b.standardOptimizeOption(.{});

const strip = b.option(bool, "strip", "Omit debug information") orelse false;
const pie = b.option(bool, "pie", "Build a Position Independent Executable") orelse false;
Expand All @@ -24,16 +25,25 @@ pub fn build(b: *zbs.Builder) !void {
"man-pages",
"Set to true to build man pages. Requires scdoc. Defaults to true if scdoc is found.",
) orelse scdoc_found: {
_ = b.findProgram(&[_][]const u8{"scdoc"}, &[_][]const u8{}) catch |err| switch (err) {
_ = b.findProgram(&.{"scdoc"}, &.{}) catch |err| switch (err) {
error.FileNotFound => break :scdoc_found false,
else => return err,
};
break :scdoc_found true;
};

if (man_pages) {
const scdoc_step = try ScdocStep.create(b);
try scdoc_step.install();
inline for (.{"waylock"}) |page| {
// Taken from river. The rationale is of the following:
// Workaround for https://github.com/ziglang/zig/issues/16369
// Even passing a buffer to std.Build.Step.Run appears to be racy and occasionally deadlocks.
const scdoc = b.addSystemCommand(&.{ "sh", "-c", "scdoc < doc/" ++ page ++ ".1.scd" });

scdoc.addFileArg(.{ .path = "doc/" ++ page ++ ".1.scd" });

const stdout = scdoc.captureStdOut();
b.getInstallStep().dependOn(&b.addInstallFile(stdout, "share/man/man1/" ++ page ++ ".1").step);
}
}

const install_prefix = try std.fs.path.resolve(b.allocator, &[_][]const u8{b.install_prefix});
Expand All @@ -48,12 +58,12 @@ pub fn build(b: *zbs.Builder) !void {
var ret: u8 = undefined;

const git_describe_long = b.execAllowFail(
&[_][]const u8{ "git", "-C", b.build_root, "describe", "--long" },
&.{ "git", "-C", b.build_root.path orelse ".", "describe", "--long" },
&ret,
.Inherit,
) catch break :blk version;

var it = mem.split(u8, mem.trim(u8, git_describe_long, &std.ascii.spaces), "-");
var it = mem.split(u8, mem.trim(u8, git_describe_long, &std.ascii.whitespace), "-");
_ = it.next().?; // previous tag
const commit_count = it.next().?;
const commit_hash = it.next().?;
Expand All @@ -73,7 +83,7 @@ pub fn build(b: *zbs.Builder) !void {
const options = b.addOptions();
options.addOption([]const u8, "version", full_version);

const scanner = ScanProtocolsStep.create(b);
const scanner = Scanner.create(b, .{});
scanner.addSystemProtocol("staging/ext-session-lock/ext-session-lock-v1.xml");
scanner.addSystemProtocol("staging/single-pixel-buffer/single-pixel-buffer-v1.xml");
scanner.addSystemProtocol("stable/viewporter/viewporter.xml");
Expand All @@ -85,17 +95,18 @@ pub fn build(b: *zbs.Builder) !void {
scanner.generate("wp_viewporter", 1);
scanner.generate("wp_single_pixel_buffer_manager_v1", 1);

const waylock = b.addExecutable("waylock", "src/main.zig");
waylock.setTarget(target);
waylock.setBuildMode(mode);
const waylock = b.addExecutable(.{
.name = "waylock",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
waylock.addOptions("build_options", options);

waylock.addPackage(.{
.name = "wayland",
.source = .{ .generated = &scanner.result },
});
waylock.step.dependOn(&scanner.step);
waylock.addPackagePath("xkbcommon", "deps/zig-xkbcommon/src/xkbcommon.zig");
const wayland = b.createModule(.{ .source_file = scanner.result });
waylock.addModule("wayland", wayland);
const xkbcommon = b.createModule(.{ .source_file = .{ .path = "deps/zig-xkbcommon/src/xkbcommon.zig" } });
waylock.addModule("xkbcommon", xkbcommon);
waylock.linkLibC();
waylock.linkSystemLibrary("wayland-client");
waylock.linkSystemLibrary("xkbcommon");
Expand All @@ -105,23 +116,23 @@ pub fn build(b: *zbs.Builder) !void {

waylock.strip = strip;
waylock.pie = pie;
waylock.install();
b.installArtifact(waylock);
}

const ScdocStep = struct {
builder: *zbs.Builder,
step: zbs.Step,
builder: *Build,
step: *Step,

fn create(builder: *zbs.Builder) !*ScdocStep {
fn create(builder: *Build) !*ScdocStep {
const self = try builder.allocator.create(ScdocStep);
self.* = .{
.builder = builder,
.step = zbs.Step.init(.custom, "Generate man pages", builder.allocator, make),
.step = Step.init(.custom, "Generate man pages", builder.allocator, make),
};
return self;
}

fn make(step: *zbs.Step) !void {
fn make(step: *Step) !void {
const self = @fieldParentPtr(ScdocStep, "step", step);
_ = try self.builder.exec(
&[_][]const u8{ "sh", "-c", "scdoc < doc/waylock.1.scd > doc/waylock.1" },
Expand Down
2 changes: 1 addition & 1 deletion deps/zig-xkbcommon
22 changes: 11 additions & 11 deletions src/Lock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ pub fn run(options: Options) void {
fatal("failed to read response from child authentication process: {s}", .{@errorName(err)});
};
switch (byte) {
@boolToInt(true) => {
@intFromBool(true) => {
lock.session_lock.?.unlockAndDestroy();
lock.session_lock = null;
lock.state = .exiting;
},
@boolToInt(false) => {
@intFromBool(false) => {
lock.set_color(.fail);
},
else => {
Expand Down Expand Up @@ -278,15 +278,15 @@ fn registry_listener(registry: *wl.Registry, event: wl.Registry.Event, lock: *Lo
fn registry_event(lock: *Lock, registry: *wl.Registry, event: wl.Registry.Event) !void {
switch (event) {
.global => |ev| {
if (std.cstr.cmp(ev.interface, wl.Compositor.getInterface().name) == 0) {
if (mem.orderZ(u8, ev.interface, wl.Compositor.getInterface().name) == .eq) {
// Version 4 required for wl_surface.damage_buffer
if (ev.version < 4) {
fatal("advertised wl_compositor version too old, version 4 required", .{});
}
lock.compositor = try registry.bind(ev.name, wl.Compositor, 4);
} else if (std.cstr.cmp(ev.interface, ext.SessionLockManagerV1.getInterface().name) == 0) {
} else if (mem.orderZ(u8, ev.interface, ext.SessionLockManagerV1.getInterface().name) == .eq) {
lock.session_lock_manager = try registry.bind(ev.name, ext.SessionLockManagerV1, 1);
} else if (std.cstr.cmp(ev.interface, wl.Output.getInterface().name) == 0) {
} else if (mem.orderZ(u8, ev.interface, wl.Output.getInterface().name) == .eq) {
// Version 3 required for wl_output.release
if (ev.version < 3) {
fatal("advertised wl_output version too old, version 3 required", .{});
Expand All @@ -308,7 +308,7 @@ fn registry_event(lock: *Lock, registry: *wl.Registry, event: wl.Registry.Event)
.initializing, .exiting => {},
.locking, .locked => try node.data.create_surface(),
}
} else if (std.cstr.cmp(ev.interface, wl.Seat.getInterface().name) == 0) {
} else if (mem.orderZ(u8, ev.interface, wl.Seat.getInterface().name) == .eq) {
// Version 5 required for wl_seat.release
if (ev.version < 5) {
fatal("advertised wl_seat version too old, version 5 required.", .{});
Expand All @@ -321,9 +321,9 @@ fn registry_event(lock: *Lock, registry: *wl.Registry, event: wl.Registry.Event)

node.data.init(lock, ev.name, wl_seat);
lock.seats.prepend(node);
} else if (std.cstr.cmp(ev.interface, wp.Viewporter.getInterface().name) == 0) {
} else if (mem.orderZ(u8, ev.interface, wp.Viewporter.getInterface().name) == .eq) {
lock.viewporter = try registry.bind(ev.name, wp.Viewporter, 1);
} else if (std.cstr.cmp(ev.interface, wp.SinglePixelBufferManagerV1.getInterface().name) == 0) {
} else if (mem.orderZ(u8, ev.interface, wp.SinglePixelBufferManagerV1.getInterface().name) == .eq) {
lock.buffer_manager = try registry.bind(ev.name, wp.SinglePixelBufferManagerV1, 1);
}
},
Expand Down Expand Up @@ -389,7 +389,7 @@ pub fn submit_password(lock: *Lock) void {
fn send_password_to_auth(lock: *Lock) !void {
defer lock.password.clear();
const writer = lock.auth_connection.writer();
try writer.writeIntNative(u32, @intCast(u32, lock.password.buffer.len));
try writer.writeIntNative(u32, @as(u32, @intCast(lock.password.buffer.len)));
try writer.writeAll(lock.password.buffer);
}

Expand All @@ -400,7 +400,7 @@ pub fn set_color(lock: *Lock, color: Color) void {

var it = lock.outputs.first;
while (it) |node| : (it = node.next) {
node.data.attach_buffer(lock.buffers[@enumToInt(lock.color)]);
node.data.attach_buffer(lock.buffers[@intFromEnum(lock.color)]);
}
}

Expand All @@ -424,7 +424,7 @@ fn create_buffers(
var buffers: [3]*wl.Buffer = undefined;
for ([_]Color{ .init, .input, .fail }) |color| {
const rgb = options.rgb(color);
buffers[@enumToInt(color)] = try buffer_manager.createU32RgbaBuffer(
buffers[@intFromEnum(color)] = try buffer_manager.createU32RgbaBuffer(
@as(u32, (rgb >> 16) & 0xff) * (0xffff_ffff / 0xff),
@as(u32, (rgb >> 8) & 0xff) * (0xffff_ffff / 0xff),
@as(u32, (rgb >> 0) & 0xff) * (0xffff_ffff / 0xff),
Expand Down
6 changes: 3 additions & 3 deletions src/Output.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ fn lock_surface_listener(
const lock = output.lock;
switch (event) {
.configure => |ev| {
output.width = @truncate(u31, ev.width);
output.height = @truncate(u31, ev.height);
output.width = @as(u31, @truncate(ev.width));
output.height = @as(u31, @truncate(ev.height));
output.lock_surface.?.ackConfigure(ev.serial);
output.attach_buffer(lock.buffers[@enumToInt(lock.color)]);
output.attach_buffer(lock.buffers[@intFromEnum(lock.color)]);
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Seat.zig
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn keyboard_listener(_: *wl.Keyboard, event: wl.Keyboard.Event, seat: *Seat) voi
defer os.close(ev.fd);

if (ev.format != .xkb_v1) {
log.err("unsupported keymap format {d}", .{@enumToInt(ev.format)});
log.err("unsupported keymap format {d}", .{@intFromEnum(ev.format)});
return;
}

Expand Down Expand Up @@ -155,7 +155,7 @@ fn keyboard_listener(_: *wl.Keyboard, event: wl.Keyboard.Event, seat: *Seat) voi
const keysym = xkb_state.keyGetOneSym(keycode);
if (keysym == .NoSymbol) return;

switch (@enumToInt(keysym)) {
switch (@intFromEnum(keysym)) {
xkb.Keysym.Return => {
// Ignore the attempt to submit the password if the locked event has not yet
// been received. This should be pretty much impossible to happen in practice
Expand All @@ -176,7 +176,7 @@ fn keyboard_listener(_: *wl.Keyboard, event: wl.Keyboard.Event, seat: *Seat) voi
const Component = xkb.State.Component;
const ctrl_active = xkb_state.modNameIsActive(
xkb.names.mod.ctrl,
@intToEnum(Component, Component.mods_depressed | Component.mods_latched),
@as(Component, @enumFromInt(Component.mods_depressed | Component.mods_latched)),
) == 1;

if (ctrl_active) {
Expand Down
Loading

0 comments on commit d9db2ce

Please sign in to comment.