From 70fae468c26175ffe38bf65fb69ca8e360c959cf Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreply.github.com> Date: Sat, 20 Jul 2024 03:34:14 +1000 Subject: [PATCH] add XDG base directory spec support --- build.zig | 19 ++++++++++++++++--- zigup.zig | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/build.zig b/build.zig index cd1cd7b..b8f4f1e 100644 --- a/build.zig +++ b/build.zig @@ -5,8 +5,17 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const use_xdg = b.option( + bool, + "use-xdg", + "Use the XDG base directory specification (default: false)", + ) orelse false; + + const build_options = b.addOptions(); + build_options.addOption(bool, "use_xdg", use_xdg); + const zigup_exe_native = blk: { - const exe = addZigupExe(b, target, optimize); + const exe = addZigupExe(b, target, optimize, build_options); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); @@ -74,13 +83,14 @@ pub fn build(b: *std.Build) !void { ci_step.dependOn(test_step); ci_step.dependOn(unzip_step); ci_step.dependOn(zip_step); - try ci(b, ci_step, test_step, host_zip_exe); + try ci(b, ci_step, test_step, host_zip_exe, build_options); } fn addZigupExe( b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode, + build_options: *std.Build.Step.Options, ) *std.Build.Step.Compile { const win32exelink_mod: ?*std.Build.Module = blk: { if (target.result.os.tag == .windows) { @@ -104,6 +114,8 @@ fn addZigupExe( .optimize = optimize, }); + exe.root_module.addOptions("build-options", build_options); + if (target.result.os.tag == .windows) { exe.root_module.addImport("win32exelink", win32exelink_mod.?); } @@ -115,6 +127,7 @@ fn ci( ci_step: *std.Build.Step, test_step: *std.Build.Step, host_zip_exe: *std.Build.Step.Compile, + build_options: *std.Build.Step.Options, ) !void { const ci_targets = [_][]const u8{ "x86_64-linux", @@ -140,7 +153,7 @@ fn ci( const optimize: std.builtin.OptimizeMode = // Compile in ReleaseSafe on Windows for faster extraction if (target.result.os.tag == .windows) .ReleaseSafe else .Debug; - const zigup_exe = addZigupExe(b, target, optimize); + const zigup_exe = addZigupExe(b, target, optimize, build_options); const zigup_exe_install = b.addInstallArtifact(zigup_exe, .{ .dest_dir = .{ .override = .{ .custom = ci_target_str } }, }); diff --git a/zigup.zig b/zigup.zig index f7a60b8..6208eca 100644 --- a/zigup.zig +++ b/zigup.zig @@ -2,6 +2,8 @@ const std = @import("std"); const builtin = @import("builtin"); const mem = std.mem; +const build_options = @import("build-options"); + const ArrayList = std.ArrayList; const Allocator = mem.Allocator; @@ -119,10 +121,15 @@ fn ignoreHttpCallback(request: []const u8) void { } fn getHomeDir() ![]const u8 { - return std.posix.getenv("HOME") orelse { + const home = std.posix.getenv("HOME") orelse { std.log.err("cannot find install directory, $HOME environment variable is not set", .{}); - return error.MissingHomeEnvironmentVariable; + return error.AlreadyReported; }; + if (!std.fs.path.isAbsolute(home)) { + std.log.err("$HOME environment variable '{s}' is not an absolute path", .{home}); + return error.AlreadyReported; + } + return home; } fn allocInstallDirString(allocator: Allocator) ![]const u8 { @@ -133,12 +140,28 @@ fn allocInstallDirString(allocator: Allocator) ![]const u8 { defer allocator.free(self_exe_dir); return std.fs.path.join(allocator, &.{ self_exe_dir, "zig" }); } - const home = try getHomeDir(); - if (!std.fs.path.isAbsolute(home)) { - std.log.err("$HOME environment variable '{s}' is not an absolute path", .{home}); - return error.BadHomeEnvironmentVariable; + + if (build_options.use_xdg) { + if (std.posix.getenv("XDG_DATA_HOME")) |xdg_data_home| { + if (xdg_data_home.len > 0) { + if (!std.fs.path.isAbsolute(xdg_data_home)) { + std.log.err( + "$XDG_DATA_HOME environment variable '{s}' is not an absolute path", + .{xdg_data_home}, + ); + return error.AlreadyReported; + } + return std.fs.path.join(allocator, &.{ xdg_data_home, "zigup" }); + } + } + const sep = .{std.fs.path.sep}; + return std.fs.path.join(allocator, &.{ + try getHomeDir(), ".local" ++ sep ++ "share" ++ sep ++ "zigup", + }); } - return std.fs.path.join(allocator, &[_][]const u8{ home, "zig" }); + return std.fs.path.join(allocator, &[_][]const u8{ + try getHomeDir(), "zig", + }); } const GetInstallDirOptions = struct { create: bool,