Support Zig 0.13.0 and setting default TTY at build time (#632)

* feat: support zig `0.13.0`

* 12 compatible

* update clap

* feat: add default tty to build option

* little fix

* update `zigini`
This commit is contained in:
jinzhongjia 2024-07-13 04:40:01 +08:00 committed by GitHub
parent 5f8fbe381c
commit e4abf79ad5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 34 additions and 17 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
.idea/
zig-cache/
zig-out/
valgrind.log
valgrind.log
.zig-cache

View File

@ -1,13 +1,30 @@
const std = @import("std");
const builtin = @import("builtin");
const min_zig_string = "0.12.0";
const current_zig = builtin.zig_version;
// Implementing zig version detection through compile time
comptime {
const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable;
if (current_zig.order(min_zig) == .lt) {
@compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig }));
}
}
const ly_version = std.SemanticVersion{ .major = 1, .minor = 1, .patch = 0 };
var dest_directory: []const u8 = undefined;
var data_directory: []const u8 = undefined;
var default_tty: u8 = undefined;
var exe_name: []const u8 = undefined;
const ProgressNode = if (current_zig.minor == 12) *std.Progress.Node else std.Progress.Node;
pub fn build(b: *std.Build) !void {
dest_directory = b.option([]const u8, "dest_directory", "Specify a destination directory for installation") orelse "";
data_directory = b.option([]const u8, "data_directory", "Specify a default data directory (default is /etc/ly). This path gets embedded into the binary") orelse "/etc/ly";
default_tty = b.option(u8, "default_tty", "set default TTY") orelse 2;
exe_name = b.option([]const u8, "name", "Specify installed executable file name (default is ly)") orelse "ly";
const bin_directory = try b.allocator.dupe(u8, data_directory);
@ -20,12 +37,14 @@ pub fn build(b: *std.Build) !void {
build_options.addOption([]const u8, "version", version_str);
build_options.addOption(u8, "tty", default_tty);
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "ly",
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
@ -38,14 +57,14 @@ pub fn build(b: *std.Build) !void {
const clap = b.dependency("clap", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("clap", clap.module("clap"));
exe.addIncludePath(.{ .path = "include" });
exe.addIncludePath(b.path("include"));
exe.linkSystemLibrary("pam");
exe.linkSystemLibrary("xcb");
exe.linkLibC();
// HACK: Only fails with ReleaseSafe, so we'll override it.
const translate_c = b.addTranslateC(.{
.root_source_file = .{ .path = "include/termbox2.h" },
.root_source_file = b.path("include/termbox2.h"),
.target = target,
.optimize = if (optimize == .ReleaseSafe) .ReleaseFast else optimize,
});
@ -94,8 +113,7 @@ pub fn build(b: *std.Build) !void {
pub fn ExeInstaller(install_conf: bool) type {
return struct {
pub fn make(step: *std.Build.Step, progress: *std.Progress.Node) !void {
_ = progress;
pub fn make(step: *std.Build.Step, _: ProgressNode) !void {
try install_ly(step.owner.allocator, install_conf);
}
};
@ -108,8 +126,7 @@ const InitSystem = enum {
};
pub fn ServiceInstaller(comptime init_system: InitSystem) type {
return struct {
pub fn make(step: *std.Build.Step, progress: *std.Progress.Node) !void {
_ = progress;
pub fn make(step: *std.Build.Step, _: ProgressNode) !void {
const allocator = step.owner.allocator;
switch (init_system) {
.Openrc => {
@ -217,8 +234,7 @@ fn install_ly(allocator: std.mem.Allocator, install_config: bool) !void {
}
}
pub fn uninstallall(step: *std.Build.Step, progress: *std.Progress.Node) !void {
_ = progress;
pub fn uninstallall(step: *std.Build.Step, _: ProgressNode) !void {
try std.fs.cwd().deleteTree(data_directory);
const allocator = step.owner.allocator;

View File

@ -4,12 +4,12 @@
.minimum_zig_version = "0.12.0",
.dependencies = .{
.clap = .{
.url = "https://github.com/Hejsil/zig-clap/archive/8c98e6404b22aafc0184e999d8f068b81cc22fa1.tar.gz",
.hash = "122014e73fd712190e109950837b97f6143f02d7e2b6986e1db70b6f4aadb5ba6a0d",
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz",
.hash = "122062d301a203d003547b414237229b09a7980095061697349f8bef41be9c30266b",
},
.zigini = .{
.url = "https://github.com/Kawaii-Ash/zigini/archive/ce1f322482099db058f5d9fdd05fbfa255d79723.tar.gz",
.hash = "1220e7a99793a0430e0a7c0b938cb3c98321035bc297e21cd0e2413cf740b4923b9f",
.url = "https://github.com/Kawaii-Ash/zigini/archive/refs/tags/0.2.2.tar.gz",
.hash = "1220afda2f3258cd0bb042dd3c2d5a35069ce1785c11325e65f136c5220013b36d00",
},
},
.paths = .{""},

View File

@ -42,7 +42,7 @@ sleep_cmd: ?[]const u8 = null,
sleep_key: []const u8 = "F3",
term_reset_cmd: [:0]const u8 = "/usr/bin/tput reset",
term_restore_cursor_cmd: []const u8 = "/usr/bin/tput cnorm",
tty: u8 = 2,
tty: u8 = build_options.tty,
vi_mode: bool = false,
wayland_cmd: []const u8 = build_options.data_directory ++ "/wsetup.sh",
waylandsessions: []const u8 = "/usr/share/wayland-sessions",

View File

@ -498,7 +498,7 @@ pub fn main() !void {
run = false;
} else if (pressed_key == sleep_key) {
if (config.sleep_cmd) |sleep_cmd| {
var sleep = std.ChildProcess.init(&[_][]const u8{ "/bin/sh", "-c", sleep_cmd }, allocator);
var sleep = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", sleep_cmd }, allocator);
_ = sleep.spawnAndWait() catch .{};
}
} else if (pressed_key == brightness_down_key and unistd.access(&config.brightnessctl[0], unistd.X_OK) == 0) brightness_change: {
@ -619,7 +619,7 @@ pub fn main() !void {
update = true;
var restore_cursor = std.ChildProcess.init(&[_][]const u8{ "/bin/sh", "-c", config.term_restore_cursor_cmd }, allocator);
var restore_cursor = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", config.term_restore_cursor_cmd }, allocator);
_ = restore_cursor.spawnAndWait() catch .{};
},
else => {