1- const supported_oses : []const std.Target.Os.Tag = &.{
2- .linux ,
3- .macos ,
4- .windows ,
5- };
6-
71pub 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.
220258fn getVersionString (b : * std.Build , base_semantic_version : std.SemanticVersion , root_path : []const u8 ) ! []const u8 {
221259 const version_string = b .fmt (
0 commit comments