Skip to content

Commit 32deb26

Browse files
committed
build: use an explicit list of supported oses
This simplifies both figuring our `enable` for commands and also the future system abstraction.
1 parent ada498c commit 32deb26

File tree

7 files changed

+57
-27
lines changed

7 files changed

+57
-27
lines changed

build.zig

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
const supported_oses: []const std.Target.Os.Tag = &.{
2-
.linux,
3-
.macos,
4-
.windows,
5-
};
6-
71
pub fn build(b: *std.Build) !void {
82
const optimize = b.standardOptimizeOption(.{});
93

@@ -28,9 +22,9 @@ pub fn build(b: *std.Build) !void {
2822
{
2923
const target = b.standardTargetOptions(.{});
3024

31-
if (std.mem.indexOfScalar(std.Target.Os.Tag, supported_oses, target.result.os.tag) == null) {
25+
const coreutils_target = CoreutilsTarget.fromOsTag(target.result.os.tag) orelse {
3226
std.debug.panic("unsupported target OS {s}", .{@tagName(target.result.os.tag)});
33-
}
27+
};
3428

3529
const coreutils_exe = b.addExecutable(.{
3630
.name = "zig-coreutils",
@@ -39,6 +33,7 @@ pub fn build(b: *std.Build) !void {
3933
target,
4034
optimize,
4135
trace,
36+
coreutils_target,
4237
options_module,
4338
),
4439
});
@@ -71,17 +66,17 @@ pub fn build(b: *std.Build) !void {
7166
const check_step = b.step("check", "");
7267
const test_step = b.step("test", "Run the tests for all targets");
7368

74-
for (supported_oses) |os_tag| {
75-
const target = b.resolveTargetQuery(.{ .os_tag = os_tag });
76-
const is_native_target = target.result.os.tag == builtin.os.tag;
69+
for (std.meta.tags(CoreutilsTarget)) |coreutils_target| {
70+
const target = b.resolveTargetQuery(.{ .os_tag = coreutils_target.osTag() });
7771

7872
try createTestAndCheckSteps(
7973
b,
8074
target,
8175
optimize,
8276
trace,
77+
coreutils_target,
8378
options_module,
84-
is_native_target,
79+
target.result.os.tag == builtin.os.tag,
8580
coverage,
8681
test_step,
8782
check_step,
@@ -96,6 +91,7 @@ fn createRootModule(
9691
target: std.Build.ResolvedTarget,
9792
optimize: std.builtin.OptimizeMode,
9893
trace: bool,
94+
coreutils_target: CoreutilsTarget,
9995
options_module: *std.Build.Module,
10096
) *std.Build.Module {
10197
const tracy_dep = b.dependency("tracy", .{
@@ -118,6 +114,10 @@ fn createRootModule(
118114
coreutils_module.addImport("tracy_impl", tracy_dep.module("tracy_impl_disabled"));
119115
}
120116

117+
const target_options = b.addOptions();
118+
target_options.addOption(CoreutilsTarget, "target_os", coreutils_target);
119+
coreutils_module.addImport("target_os", target_options.createModule());
120+
121121
return coreutils_module;
122122
}
123123

@@ -126,6 +126,7 @@ fn createTestAndCheckSteps(
126126
target: std.Build.ResolvedTarget,
127127
optimize: std.builtin.OptimizeMode,
128128
trace: bool,
129+
coreutils_target: CoreutilsTarget,
129130
options_module: *std.Build.Module,
130131
is_native_target: bool,
131132
coverage: bool,
@@ -138,6 +139,7 @@ fn createTestAndCheckSteps(
138139
target,
139140
optimize,
140141
trace,
142+
coreutils_target,
141143
options_module,
142144
);
143145

@@ -197,6 +199,7 @@ fn createTestAndCheckSteps(
197199
target,
198200
optimize,
199201
trace,
202+
coreutils_target,
200203
options_module,
201204
),
202205
});
@@ -207,6 +210,7 @@ fn createTestAndCheckSteps(
207210
target,
208211
optimize,
209212
trace,
213+
coreutils_target,
210214
options_module,
211215
),
212216
});
@@ -216,6 +220,40 @@ fn createTestAndCheckSteps(
216220
}
217221
}
218222

223+
// Having our own enum rather than using std.Target.Os.Tag simplifies the system abstraction.
224+
const CoreutilsTarget = enum {
225+
linux,
226+
macos,
227+
windows,
228+
229+
fn osTag(self: CoreutilsTarget) std.Target.Os.Tag {
230+
return switch (self) {
231+
.linux => .linux,
232+
.macos => .macos,
233+
.windows => .windows,
234+
};
235+
}
236+
237+
pub fn fromOsTag(tag: std.Target.Os.Tag) ?CoreutilsTarget {
238+
return switch (tag) {
239+
.linux => .linux,
240+
.macos => .macos,
241+
.windows => .windows,
242+
else => null,
243+
};
244+
}
245+
246+
const os_tags = blk: {
247+
const tags = std.meta.tags(CoreutilsTarget);
248+
249+
var zig_os_tags: [tags.len]std.Target.Os.Tag = undefined;
250+
for (tags, 0..) |tag, i| {
251+
zig_os_tags[i] = tag.osTag();
252+
}
253+
break :blk zig_os_tags;
254+
};
255+
};
256+
219257
/// Gets the version string.
220258
fn getVersionString(b: *std.Build, base_semantic_version: std.SemanticVersion, root_path: []const u8) ![]const u8 {
221259
const version_string = b.fmt(

src/commands/groups.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
// SPDX-FileCopyrightText: 2025 Lee Cannon <[email protected]>
33

44
/// Is this command enabled for the current target?
5-
pub const enabled: bool = blk: {
6-
const builtin = @import("builtin");
7-
break :blk builtin.os.tag == .linux; // TODO: support other OSes
8-
};
5+
pub const enabled: bool = shared.target_os == .linux; // TODO: support other OSes
96

107
pub const command: Command = .{
118
.name = "groups",

src/commands/nproc.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
// SPDX-FileCopyrightText: 2024 Leon Henrik Plickat
44

55
/// Is this command enabled for the current target?
6-
pub const enabled: bool = blk: {
7-
const builtin = @import("builtin");
8-
break :blk builtin.os.tag == .linux; // TODO: support other OSes
9-
};
6+
pub const enabled: bool = shared.target_os == .linux; // TODO: support other OSes
107

118
pub const command: Command = .{
129
.name = "nproc",

src/commands/template.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-FileCopyrightText: 2025 Lee Cannon <[email protected]>
33

44
/// Is this command enabled for the current target?
5-
pub const enabled: bool = true; // USE BUILTIN TO DETERMINE IF THE COMMAND IS ENABLED FOR THE CURRENT TARGET
5+
pub const enabled: bool = true; // USE `shared.target_os` TO DETERMINE IF THE COMMAND IS ENABLED FOR THE CURRENT TARGET
66

77
pub const command: Command = .{
88
.name = "template", // CHANGE THIS

src/commands/whoami.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
// SPDX-FileCopyrightText: 2025 Lee Cannon <[email protected]>
33

44
/// Is this command enabled for the current target?
5-
pub const enabled: bool = blk: {
6-
const builtin = @import("builtin");
7-
break :blk builtin.os.tag == .linux; // TODO: support other OSes
8-
};
5+
pub const enabled: bool = shared.target_os == .linux;
96

107
pub const command: Command = .{
118
.name = "whoami",

src/commands/yes.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const impl = struct {
4040
const string = try getString(allocator, args);
4141
defer if (shared.free_on_close) string.deinit(allocator);
4242

43-
if (builtin.is_test) {
43+
if (@import("builtin").is_test) {
4444
// to allow this command to be tested
4545

4646
for (0..10) |_| {
@@ -98,6 +98,5 @@ const Command = @import("../Command.zig");
9898
const IO = @import("../IO.zig");
9999
const shared = @import("../shared.zig");
100100

101-
const builtin = @import("builtin");
102101
const std = @import("std");
103102
const tracy = @import("tracy");

src/shared.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: MIT
22
// SPDX-FileCopyrightText: 2025 Lee Cannon <[email protected]>
33

4+
pub const target_os = @import("target_os").target_os;
5+
46
pub const base_version_string = "zig-coreutils " ++ options.version ++ "\nMIT License Copyright (c) 2025 Lee Cannon\n";
57
pub const version_string = "{NAME} - " ++ base_version_string;
68
pub const is_debug_or_test = builtin.is_test or builtin.mode == .Debug;

0 commit comments

Comments
 (0)