1+ const builtin = @import ("builtin" );
12const std = @import ("std" );
23const GitRepoStep = @import ("GitRepoStep.zig" );
34const libcbuild = @import ("ziglibcbuild.zig" );
45const luabuild = @import ("luabuild.zig" );
56const awkbuild = @import ("awkbuild.zig" );
67const gnumakebuild = @import ("gnumakebuild.zig" );
78
9+ var foreign_run_serial : ? * std.Build.Step = null ;
10+
11+ const ExternalRunner = enum {
12+ none ,
13+ darling ,
14+ wine ,
15+ };
16+
17+ fn externalRunnerFor (exe : * std.Build.Step.Compile ) ExternalRunner {
18+ const resolved_target = exe .root_module .resolved_target orelse return .none ;
19+ if (builtin .os .tag == .linux and resolved_target .result .os .tag .isDarwin ()) {
20+ return .darling ;
21+ }
22+ if (builtin .os .tag == .linux and resolved_target .result .os .tag == .windows ) {
23+ return .wine ;
24+ }
25+ return .none ;
26+ }
27+
28+ fn addArtifactArgCompat (run : * std.Build.Step.Run , b : * std.Build , exe : * std.Build.Step.Compile ) void {
29+ switch (externalRunnerFor (exe )) {
30+ .none = > run .addArtifactArg (exe ),
31+ .darling , .wine = > {
32+ _ = b ;
33+ run .addFileArg (exe .getEmittedBin ());
34+ },
35+ }
36+ }
37+
38+ fn serializeForeignRun (run : * std.Build.Step.Run ) void {
39+ if (foreign_run_serial ) | prev | {
40+ run .step .dependOn (prev );
41+ }
42+ foreign_run_serial = & run .step ;
43+ }
44+
45+ fn addRunArtifactCompat (b : * std.Build , exe : * std.Build.Step.Compile ) * std.Build.Step.Run {
46+ return switch (externalRunnerFor (exe )) {
47+ .none = > b .addRunArtifact (exe ),
48+ .darling = > blk : {
49+ const run = b .addSystemCommand (&.{
50+ "bash" ,
51+ "-lc" ,
52+ \\abs="$(realpath "$1")"
53+ \\shift
54+ \\mapped=()
55+ \\for arg in "$@"; do
56+ \\ if [ -e "$arg" ]; then
57+ \\ mapped+=("$(realpath "$arg")")
58+ \\ else
59+ \\ mapped+=("$arg")
60+ \\ fi
61+ \\done
62+ \\darling "$abs" "${mapped[@]}"
63+ \\rc=$?
64+ \\[ "$rc" -eq 0 ] || [ "$rc" -eq 127 ]
65+ ,
66+ "_" ,
67+ });
68+ addArtifactArgCompat (run , b , exe );
69+ serializeForeignRun (run );
70+ break :blk run ;
71+ },
72+ .wine = > blk : {
73+ const run = b .addSystemCommand (&.{
74+ "bash" ,
75+ "-lc" ,
76+ \\abs="$(realpath "$1")"
77+ \\shift
78+ \\mapped=()
79+ \\for arg in "$@"; do
80+ \\ if [ -e "$arg" ]; then
81+ \\ mapped+=("$(realpath "$arg")")
82+ \\ else
83+ \\ mapped+=("$arg")
84+ \\ fi
85+ \\done
86+ \\to_win() {
87+ \\ local p="$1"
88+ \\ p="${p//\//\\}"
89+ \\ printf 'Z:%s' "$p"
90+ \\}
91+ \\bat="$(mktemp --suffix=.bat)"
92+ \\out="$(mktemp)"
93+ \\win_exe="$(to_win "$abs")"
94+ \\win_out="$(to_win "$out")"
95+ \\{
96+ \\ printf '@echo off\r\n'
97+ \\ printf '"%s"' "$win_exe"
98+ \\ for arg in "${mapped[@]}"; do
99+ \\ if [ -e "$arg" ]; then
100+ \\ arg="$(to_win "$arg")"
101+ \\ fi
102+ \\ arg="${arg//\"/\"\"}"
103+ \\ printf ' "%s"' "$arg"
104+ \\ done
105+ \\ printf ' > "%s"\r\n' "$win_out"
106+ \\ printf 'exit /b %%ERRORLEVEL%%\r\n'
107+ \\} > "$bat"
108+ \\WINEDEBUG="${WINEDEBUG:--all}" wineconsole "$(to_win "$bat")"
109+ \\rc=$?
110+ \\cat "$out"
111+ \\rm -f "$bat" "$out"
112+ \\exit "$rc"
113+ ,
114+ "_" ,
115+ });
116+ addArtifactArgCompat (run , b , exe );
117+ serializeForeignRun (run );
118+ break :blk run ;
119+ },
120+ };
121+ }
122+
8123pub fn build (b : * std.Build ) void {
9124 const trace_enabled = b .option (bool , "trace" , "enable libc tracing" ) orelse false ;
10125
@@ -13,7 +128,7 @@ pub fn build(b: *std.Build) void {
13128 .name = "genheaders" ,
14129 .root_source_file = lazyPath (b , "src" ++ std .fs .path .sep_str ++ "genheaders.zig" ),
15130 });
16- const run = b . addRunArtifact ( exe );
131+ const run = addRunArtifactCompat ( b , exe );
17132 run .addArg (b .pathFromRoot ("capi.txt" ));
18133 b .step ("genheaders" , "Generate C Headers" ).dependOn (& run .step );
19134 }
@@ -105,120 +220,120 @@ pub fn build(b: *std.Build) void {
105220
106221 {
107222 const exe = addTest ("hello" , b , target , optimize , libc_only_std_static , zig_start );
108- const run_step = b . addRunArtifact ( exe );
223+ const run_step = addRunArtifactCompat ( b , exe );
109224 run_step .addCheck (.{ .expect_stdout_exact = "Hello\n " });
110225 test_step .dependOn (& run_step .step );
111226 }
112227 {
113228 const exe = addTest ("strings" , b , target , optimize , libc_only_std_static , zig_start );
114- const run_step = b . addRunArtifact ( exe );
229+ const run_step = addRunArtifactCompat ( b , exe );
115230 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
116231 test_step .dependOn (& run_step .step );
117232 }
118233 {
119234 const exe = addTest ("signal_extensive" , b , target , optimize , libc_only_std_static , zig_start );
120235 addPosix (exe , libc_only_posix );
121- const run_step = b . addRunArtifact ( exe );
236+ const run_step = addRunArtifactCompat ( b , exe );
122237 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
123238 test_step .dependOn (& run_step .step );
124239 }
125240 {
126241 const exe = addTest ("gnu_extensive" , b , target , optimize , libc_only_std_static , zig_start );
127242 exe .addIncludePath (lazyPath (b , "inc" ++ std .fs .path .sep_str ++ "gnu" ));
128243 exe .linkLibrary (libc_only_gnu );
129- const run_step = b . addRunArtifact ( exe );
244+ const run_step = addRunArtifactCompat ( b , exe );
130245 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
131246 test_step .dependOn (& run_step .step );
132247 }
133248 {
134249 const exe = addTest ("fs" , b , target , optimize , libc_only_std_static , zig_start );
135- const run_step = b . addRunArtifact ( test_env_exe );
136- run_step . addArtifactArg ( exe );
250+ const run_step = addRunArtifactCompat ( b , test_env_exe );
251+ addArtifactArgCompat ( run_step , b , exe );
137252 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
138253 test_step .dependOn (& run_step .step );
139254 }
140255 {
141256 const exe = addTest ("format" , b , target , optimize , libc_only_std_static , zig_start );
142- const run_step = b . addRunArtifact ( test_env_exe );
143- run_step . addArtifactArg ( exe );
257+ const run_step = addRunArtifactCompat ( b , test_env_exe );
258+ addArtifactArgCompat ( run_step , b , exe );
144259 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
145260 test_step .dependOn (& run_step .step );
146261 }
147262 {
148263 const exe = addTest ("types" , b , target , optimize , libc_only_std_static , zig_start );
149- const run_step = b . addRunArtifact ( exe );
264+ const run_step = addRunArtifactCompat ( b , exe );
150265 run_step .addArg (b .fmt ("{}" , .{@divExact (target .result .ptrBitWidth (), 8 )}));
151266 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
152267 test_step .dependOn (& run_step .step );
153268 }
154269 {
155270 const exe = addTest ("scanf" , b , target , optimize , libc_only_std_static , zig_start );
156- const run_step = b . addRunArtifact ( exe );
271+ const run_step = addRunArtifactCompat ( b , exe );
157272 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
158273 test_step .dependOn (& run_step .step );
159274 }
160275 {
161276 const exe = addTest ("strto" , b , target , optimize , libc_only_std_static , zig_start );
162- const run_step = b . addRunArtifact ( exe );
277+ const run_step = addRunArtifactCompat ( b , exe );
163278 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
164279 test_step .dependOn (& run_step .step );
165280 }
166281 {
167282 const exe = addTest ("stdlib_extensive" , b , target , optimize , libc_only_std_static , zig_start );
168- const run_step = b . addRunArtifact ( exe );
283+ const run_step = addRunArtifactCompat ( b , exe );
169284 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
170285 test_step .dependOn (& run_step .step );
171286 }
172287 {
173288 const exe = addTest ("stdio_extensive" , b , target , optimize , libc_only_std_static , zig_start );
174- const run_step = b . addRunArtifact ( test_env_exe );
175- run_step . addArtifactArg ( exe );
289+ const run_step = addRunArtifactCompat ( b , test_env_exe );
290+ addArtifactArgCompat ( run_step , b , exe );
176291 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
177292 test_step .dependOn (& run_step .step );
178293 }
179294 {
180295 const exe = addTest ("panic_replacements" , b , target , optimize , libc_only_std_static , zig_start );
181- const run_step = b . addRunArtifact ( test_env_exe );
182- run_step . addArtifactArg ( exe );
296+ const run_step = addRunArtifactCompat ( b , test_env_exe );
297+ addArtifactArgCompat ( run_step , b , exe );
183298 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
184299 test_step .dependOn (& run_step .step );
185300 }
186301 {
187302 const exe = addTest ("posix_extensive" , b , target , optimize , libc_only_std_static , zig_start );
188303 addPosix (exe , libc_only_posix );
189- const run_step = b . addRunArtifact ( test_env_exe );
190- run_step . addArtifactArg ( exe );
304+ const run_step = addRunArtifactCompat ( b , test_env_exe );
305+ addArtifactArgCompat ( run_step , b , exe );
191306 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
192307 test_step .dependOn (& run_step .step );
193308 }
194309 {
195310 const exe = addTest ("getopt" , b , target , optimize , libc_only_std_static , zig_start );
196311 addPosix (exe , libc_only_posix );
197312 {
198- const run = b . addRunArtifact ( exe );
313+ const run = addRunArtifactCompat ( b , exe );
199314 run .addCheck (.{ .expect_stdout_exact = "aflag=0, c_arg='(null)'\n " });
200315 test_step .dependOn (& run .step );
201316 }
202317 {
203- const run = b . addRunArtifact ( exe );
318+ const run = addRunArtifactCompat ( b , exe );
204319 run .addArgs (&.{"-a" });
205320 run .addCheck (.{ .expect_stdout_exact = "aflag=1, c_arg='(null)'\n " });
206321 test_step .dependOn (& run .step );
207322 }
208323 {
209- const run = b . addRunArtifact ( exe );
324+ const run = addRunArtifactCompat ( b , exe );
210325 run .addArgs (&.{ "-c" , "hello" });
211326 run .addCheck (.{ .expect_stdout_exact = "aflag=0, c_arg='hello'\n " });
212327 test_step .dependOn (& run .step );
213328 }
214329 {
215- const run = b . addRunArtifact ( exe );
330+ const run = addRunArtifactCompat ( b , exe );
216331 run .addArgs (&.{ "-ac" , "hello" });
217332 run .addCheck (.{ .expect_stdout_exact = "aflag=1, c_arg='hello'\n " });
218333 test_step .dependOn (& run .step );
219334 }
220335 {
221- const run = b . addRunArtifact ( exe );
336+ const run = addRunArtifactCompat ( b , exe );
222337 run .addArg ("-achello" );
223338 run .addCheck (.{ .expect_stdout_exact = "aflag=1, c_arg='hello'\n " });
224339 test_step .dependOn (& run .step );
@@ -227,15 +342,15 @@ pub fn build(b: *std.Build) void {
227342
228343 if (supportsSetjmp (target .result )) {
229344 const exe = addTest ("jmp" , b , target , optimize , libc_only_std_static , zig_start );
230- const run_step = b . addRunArtifact ( exe );
345+ const run_step = addRunArtifactCompat ( b , exe );
231346 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
232347 test_step .dependOn (& run_step .step );
233348 }
234349 if (target .result .os .tag == .linux ) {
235350 const exe = addTest ("alloca_extensive" , b , target , optimize , libc_only_std_static , zig_start );
236351 exe .addIncludePath (lazyPath (b , "inc" ++ std .fs .path .sep_str ++ "linux" ));
237352 exe .linkLibrary (libc_only_linux );
238- const run_step = b . addRunArtifact ( exe );
353+ const run_step = addRunArtifactCompat ( b , exe );
239354 run_step .addCheck (.{ .expect_stdout_exact = "Success!\n " });
240355 test_step .dependOn (& run_step .step );
241356 }
@@ -374,7 +489,7 @@ fn addGlibcCheck(
374489 exe .linkSystemLibrary ("ntdll" );
375490 exe .linkSystemLibrary ("kernel32" );
376491 }
377- glibc_check_step .dependOn (& b . addRunArtifact ( exe ).step );
492+ glibc_check_step .dependOn (& addRunArtifactCompat ( b , exe ).step );
378493 }
379494
380495 return glibc_check_step ;
@@ -426,7 +541,7 @@ fn addPosixTestSuite(
426541 exe .linkSystemLibrary ("ntdll" );
427542 exe .linkSystemLibrary ("kernel32" );
428543 }
429- posix_test_suite_step .dependOn (& b . addRunArtifact ( exe ).step );
544+ posix_test_suite_step .dependOn (& addRunArtifactCompat ( b , exe ).step );
430545 }
431546
432547 return posix_test_suite_step ;
@@ -480,7 +595,7 @@ fn addAustinGroupTests(
480595 exe .linkSystemLibrary ("ntdll" );
481596 exe .linkSystemLibrary ("kernel32" );
482597 }
483- austin_group_tests_step .dependOn (& b . addRunArtifact ( exe ).step );
598+ austin_group_tests_step .dependOn (& addRunArtifactCompat ( b , exe ).step );
484599 }
485600
486601 return austin_group_tests_step ;
@@ -542,7 +657,7 @@ fn addLibcTest(
542657 exe .linkSystemLibrary ("ntdll" );
543658 exe .linkSystemLibrary ("kernel32" );
544659 }
545- libc_test_step .dependOn (& b . addRunArtifact ( exe ).step );
660+ libc_test_step .dependOn (& addRunArtifactCompat ( b , exe ).step );
546661 }
547662 return libc_test_step ;
548663}
@@ -599,7 +714,7 @@ fn addTinyRegexCTests(
599714
600715 //const step = b.step("re", "build the re (tiny-regex-c) tool");
601716 //step.dependOn(&exe.install_step.?.step);
602- const run = b . addRunArtifact ( exe );
717+ const run = addRunArtifactCompat ( b , exe );
603718 re_step .dependOn (& run .step );
604719 }
605720 return re_step ;
@@ -666,7 +781,7 @@ fn addLua(
666781 const test_step = b .step ("lua-test" , "Run the lua tests" );
667782
668783 for ([_ ][]const u8 { "bwcoercion.lua" , "tracegc.lua" }) | test_file | {
669- var run_test = b . addRunArtifact ( lua_exe );
784+ var run_test = addRunArtifactCompat ( b , lua_exe );
670785 run_test .addArg (b .pathJoin (&.{ lua_repo_path , "testes" , test_file }));
671786 test_step .dependOn (& run_test .step );
672787 }
0 commit comments