Skip to content

Commit

Permalink
use known-folders for install dir by default
Browse files Browse the repository at this point in the history
A new build option `default-dir` is provided to achieve the original
behaviour prior to this commit by building with

    zig build -Ddefault-dir=zig
  • Loading branch information
dweiller committed Jul 15, 2024
1 parent 461dd1d commit c131dae
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
35 changes: 32 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,27 @@ pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const default_dir = b.option(
[]const u8,
"default-dir",
"Use a static default install directory",
);

const build_options = b.addOptions();
build_options.addOption(bool, "use_known_folders", default_dir == null);
if (default_dir) |dir| {
build_options.addOption([]const u8, "default_dir", dir);
}

const known_folders = if (default_dir != null)
null
else if (b.lazyDependency("known_folders", .{})) |pkg|
pkg.module("known-folders")
else
null;

const zigup_exe_native = blk: {
const exe = addZigupExe(b, target, optimize);
const exe = addZigupExe(b, target, optimize, build_options, known_folders);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
Expand Down Expand Up @@ -74,13 +93,15 @@ 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, known_folders);
}

fn addZigupExe(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.Mode,
build_options: *std.Build.Step.Options,
known_folders: ?*std.Build.Module,
) *std.Build.Step.Compile {
const win32exelink_mod: ?*std.Build.Module = blk: {
if (target.result.os.tag == .windows) {
Expand All @@ -104,6 +125,12 @@ fn addZigupExe(
.optimize = optimize,
});

exe.root_module.addOptions("build-options", build_options);

if (known_folders) |mod| {
exe.root_module.addImport("known-folders", mod);
}

if (target.result.os.tag == .windows) {
exe.root_module.addImport("win32exelink", win32exelink_mod.?);
}
Expand All @@ -115,6 +142,8 @@ 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,
known_folders: ?*std.Build.Module,
) !void {
const ci_targets = [_][]const u8{
"x86_64-linux",
Expand All @@ -140,7 +169,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, known_folders);
const zigup_exe_install = b.addInstallArtifact(zigup_exe, .{
.dest_dir = .{ .override = .{ .custom = ci_target_str } },
});
Expand Down
7 changes: 7 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
.{
.name = "zigup",
.version = "0.0.1",
.dependencies = .{
.known_folders = .{
.url = "https://github.com/ziglibs/known-folders/archive/47076c6b11214a218e9244471d8762310820911a.tar.gz",
.hash = "12209d2738a2e1dbd3781c2e5f01a2ea877dcfeea53efdfa1913247297d328e6b207",
.lazy = true,
},
},

.paths = .{
"LICENSE",
Expand Down
16 changes: 14 additions & 2 deletions zigup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const std = @import("std");
const builtin = @import("builtin");
const mem = std.mem;

const build_options = @import("build-options");
const known_folders = @import("known-folders");

const ArrayList = std.ArrayList;
const Allocator = mem.Allocator;

Expand Down Expand Up @@ -126,19 +129,28 @@ fn getHomeDir() ![]const u8 {
}

fn allocInstallDirString(allocator: Allocator) ![]const u8 {
if (build_options.use_known_folders) {
if (try known_folders.getPath(allocator, .data)) |path| {
defer allocator.free(path);
return std.fs.path.join(allocator, &.{ path, "zigup" });
} else {
std.log.err("Could not find install directory", .{});
return error.AlreadyReported;
}
}
// TODO: maybe support ZIG_INSTALL_DIR environment variable?
// TODO: maybe support a file on the filesystem to configure install dir?
if (builtin.os.tag == .windows) {
const self_exe_dir = try std.fs.selfExeDirPathAlloc(allocator);
defer allocator.free(self_exe_dir);
return std.fs.path.join(allocator, &.{ self_exe_dir, "zig" });
return std.fs.path.join(allocator, &.{ self_exe_dir, build_options.default_dir });
}
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;
}
return std.fs.path.join(allocator, &[_][]const u8{ home, "zig" });
return std.fs.path.join(allocator, &[_][]const u8{ home, build_options.default_dir });
}
const GetInstallDirOptions = struct {
create: bool,
Expand Down

0 comments on commit c131dae

Please sign in to comment.