step: Stepargv: std.ArrayList(Arg)See also addArg and addArgs to modifying this directly
cwd: ?Build.LazyPathUse setCwd to set the initial current working directory
environ_map: ?*EnvMapOverride this field to modify the environment, or use setEnvironmentVariable
preopens: std.array_hash_map.Auto(Configuration.String, Build.LazyPath)Named files that will be provided to the parent process. See std.process.Preopens.
color: Color = .autoControls the NO_COLOR and CLICOLOR_FORCE environment variables.
disable_zig_progress: boolWhen true prevents ZIG_PROGRESS environment variable from being passed to the child process, which otherwise would be used for the child to send progress updates to the parent.
stdio: StdIoConfigures whether the Run step is considered to have side-effects, and also whether the Run step will inherit stdio streams, forwarding them to the parent process, in which case will require a global lock to prevent other steps from interfering with stdio while the subprocess associated with this Run step is running. If the Run step is determined to not have side-effects, then execution will be skipped if all output files are up-to-date and input files are unchanged.
stdin: StdInThis field must be .none if stdio is inherit. It should be only set using setStdIn.
file_inputs: std.ArrayList(std.Build.LazyPath)Additional input files that, when modified, indicate that the Run step should be re-executed. If the Run step is determined to have side-effects, the Run step is always executed when it appears in the build graph, regardless of whether these files have been modified.
rename_step_with_output_arg: boolAfter adding an output argument, this step will by default rename itself for a better display name in the build summary. This can be disabled by setting this to false.
skip_foreign_checks: boolIf this is true, a Run step which is configured to check the output of the executed binary will not fail the build if the binary cannot be executed due to being for a foreign binary to the host system which is running the build graph.
Command-line arguments such as -fqemu and -fwasmtime may affect whether a binary is detected as foreign, as well as system configuration such as Rosetta (macOS) and binfmt_misc (Linux).
If this Run step is considered to have side-effects, then this flag does nothing.
failing_to_execute_foreign_is_an_error: boolIf this is true, failing to execute a foreign binary will be considered an error. However if this is false, the step will be skipped on failure instead.
This allows for a Run step to attempt to execute a foreign binary using an external executor (such as qemu) but not fail if the executor is unavailable.
stdio_limit: std.Io.LimitIf stderr or stdout exceeds this amount, the child process is killed and the step fails.
captured_stdout: ?*CapturedStdIocaptured_stderr: ?*CapturedStdIohas_side_effects: booltest_runner_mode: bool = falseproducer: ?*Step.CompileIf this Run step was produced by a Compile step, it is tracked here.
pub fn create(owner: *std.Build, name: []const u8) *Runpub fn setName(run: *Run, name: []const u8) voidpub fn enableTestRunnerMode(run: *Run) voidpub fn addArtifactArg(run: *Run, artifact: *Step.Compile) voidDeprecated, use addArtifactArg2.
pub fn addPrefixedArtifactArg(run: *Run, prefix: []const u8, artifact: *Step.Compile) voidDeprecated, use addArtifactArg2.
pub fn addArtifactArg2(run: *Run, artifact: *Step.Compile, options: PathArgOptions) voidpub fn addOutputFileArg(run: *Run, sub_path: []const u8) std.Build.LazyPathDeprecated, use addOutputFileArg2.
pub fn addPrefixedOutputFileArg(run: *Run, prefix: []const u8, sub_path: []const u8) std.Build.LazyPathDeprecated, use addOutputFileArg2.
pub fn addOutputFileArg2(run: *Run, /// The name of the generated output file which may have zero or more path /// components. /// /// Asserted to be non-empty. sub_path: []const u8, options: PathArgOptions) std.Build.LazyPathProvides a file path as a command line argument to the command being run.
For example, a prefix of "-o" and sub_path of "output.txt" will result in the child process seeing something like this: "-ozig-cache/.../output.txt"
The child process will see a single argument, regardless of whether the prefix or sub_path have spaces.
The returned std.Build.LazyPath can be used as inputs to other APIs throughout the build system.
Related:
addFileArg - for input files given to the child processpub fn addFileArg(run: *Run, lp: std.Build.LazyPath) voidSee addFileArg2.
pub fn addPrefixedFileArg(run: *Run, prefix: []const u8, lp: std.Build.LazyPath) voidSee addFileArg2.
pub fn addFileArg2(run: *Run, lp: std.Build.LazyPath, options: PathArgOptions) voidAppends an input file to the command line arguments prepended with a string.
For example, a prefix of "-F" will result in the child process seeing something like this: "-Fexample.txt"
The child process will see a single argument, even if the prefix has spaces. Modifications to this file will be detected as a cache miss in subsequent builds, causing the child process to be re-executed.
Related:
addOutputFileArg - for files generated by the child processpub fn addFileContentArg(run: *Run, lp: std.Build.LazyPath) voidDeprecated, use addFileContentArg2.
pub fn addPrefixedFileContentArg(run: *Run, prefix: []const u8, lp: std.Build.LazyPath) voidDeprecated, use addFileContentArg2.
pub fn addFileContentArg2(run: *Run, lp: std.Build.LazyPath, options: ArgOptions) voidAppends the content of an input file to the command line arguments prepended with a string.
For example, a prefix of "-F" will result in the child process seeing something like this: "-Fmy file content"
The child process will see a single argument, even if the prefix and/or the file contain whitespace. This means that the entire file content up to EOF is rendered as one contiguous string, including escape sequences. Notably, any (trailing) newlines will show up like this: "hello,\nfile world!\n"
Modifications to the source file will be detected as a cache miss in subsequent builds, causing the child process to be re-executed.
This function may not be used to supply the first argument of a Run step.
Related:
addFileContentArg - same thing but without the prefixpub fn addOutputDirectoryArg(run: *Run, basename: []const u8) std.Build.LazyPathDeprecated, use addOutputDirectoryArg2.
pub fn addPrefixedOutputDirectoryArg(run: *Run, prefix: []const u8, basename: []const u8) std.Build.LazyPathDeprecated, use addOutputDirectoryArg2.
pub fn addOutputDirectoryArg2(run: *Run, basename: []const u8, options: PathArgOptions) std.Build.LazyPathProvides a directory path as a command line argument to the command being run. Asserts basename is not empty.
For example, a prefix of "-o" and basename of "output_dir" will result in the child process seeing something like this: "-ozig-cache/.../output_dir"
The child process will see a single argument, regardless of whether the prefix or basename have spaces.
The returned std.Build.LazyPath can be used as inputs to other APIs throughout the build system.
Related:
addDirectoryArg - for input directories given to the child processpub fn addDirectoryArg(run: *Run, lazy_directory: std.Build.LazyPath) voidDeprecated, use addDirectoryArg2.
pub fn addPrefixedDirectoryArg(run: *Run, prefix: []const u8, lazy_directory: std.Build.LazyPath) voidDeprecated, use addDirectoryArg2.
pub fn addDecoratedDirectoryArg(run: *Run, prefix: []const u8, lazy_directory: std.Build.LazyPath, suffix: []const u8) voidDeprecated, use addDirectoryArg2.
pub fn addDirectoryArg2(run: *Run, lazy_directory: std.Build.LazyPath, options: PathArgOptions) voidpub fn addDepFileOutputArg(run: *Run, basename: []const u8) std.Build.LazyPathDeprecated, use addDepFileOutputArg2.
pub fn addPrefixedDepFileOutputArg(run: *Run, prefix: []const u8, basename: []const u8) std.Build.LazyPathDeprecated, use addDepFileOutputArg2.
pub fn addDepFileOutputArg2(run: *Run, basename: []const u8, options: PathArgOptions) std.Build.LazyPathAdd a path argument to a dep file (.d) for the child process to write its discovered additional dependencies. Only one dep file argument is allowed by instance.
pub fn addArg(run: *Run, arg: []const u8) voidAppends the contents of arg, verbatim, to the command line that will be passed to the process being run.
If arg is an input file, addFileInput (or related function) must be used instead to ensure correct cache behavior.
If arg is an output file, addOutputFileArg (or related function) must be used instead to ensure correct cache behavior.
pub fn addArgs(run: *Run, args: []const []const u8) voidAppends each of args, verbatim, to the command line that will be passed to the process being run.
If any element of args is an input file, addFileInput must be used instead to ensure correct cache behavior.
If any element of args is an output file, addOutputFileArg (or related function) must be used instead to ensure correct cache behavior.
pub fn addPassthruArgs(run: *Run) voidAppends the extra arguments provided to zig build to the command line that will be passed to the process being run.
This causes the step to be considered to have side effects, disabling caching.
In the example command zig build run -- arg1 arg2, "arg1" and "arg2" will be passed to the process being run.
pub fn addThirdPartyEnabledArgDarling(run: *Run, toggle_flags: ToggleFlags) voidAppends a custom string to the command line depending on the -fdarling value passed to zig build.
pub fn addThirdPartyEnabledArgQemu(run: *Run, toggle_flags: ToggleFlags) voidAppends a custom string to the command line depending on the -fqemu value passed to zig build.
pub fn addThirdPartyEnabledArgRosetta(run: *Run, toggle_flags: ToggleFlags) voidAppends a custom string to the command line depending on the -frosetta value passed to zig build.
pub fn addThirdPartyEnabledArgWasmtime(run: *Run, toggle_flags: ToggleFlags) voidAppends a custom string to the command line depending on the -fwasmtime value passed to zig build.
pub fn addThirdPartyEnabledArgWine(run: *Run, toggle_flags: ToggleFlags) voidAppends a custom string to the command line depending on the -fwine value passed to zig build.
pub fn setStdIn(run: *Run, stdin: StdIn) voidpub fn setCwd(run: *Run, cwd: Build.LazyPath) voidpub fn clearEnvironment(run: *Run) voidpub fn getEnvMap(run: *Run) *EnvMapfn getEnvMapInternal(run: *Run) *EnvMappub fn setEnvironmentVariable(run: *Run, key: []const u8, value: []const u8) voidpub fn removeEnvironmentVariable(run: *Run, key: []const u8) voidpub fn setPreopen(run: *Run, name: []const u8, resource: Build.LazyPath) voidpub fn expectStdErrEqual(run: *Run, bytes: []const u8) voidAdds a check for exact stderr match. Does not add any other checks.
pub fn expectStdErrMatch(run: *Run, bytes: []const u8) voidpub fn expectStdOutEqual(run: *Run, bytes: []const u8) voidAdds a check for exact stdout match as well as a check for exit code 0, if there is not already an expected termination check.
pub fn expectStdOutMatch(run: *Run, bytes: []const u8) voidAdds a check for stdout match as well as a check for exit code 0, if there is not already an expected termination check.
pub fn expectExitCode(run: *Run, code: u8) voidpub fn hasTermCheck(run: Run) boolpub fn addCheck(run: *Run, new_check: StdIo.Check) voidpub fn captureStdErr(run: *Run, options: CapturedStdIo.Options) std.Build.LazyPathpub fn captureStdOut(run: *Run, options: CapturedStdIo.Options) std.Build.LazyPathpub fn addFileInput(run: *Run, file_input: std.Build.LazyPath) voidAdds an additional input files that, when modified, indicates that this Run step should be re-executed. If the Run step is determined to have side-effects, the Run step is always executed when it appears in the build graph, regardless of whether this file has been modified.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805const Run = @This();
const builtin = @import("builtin");
const std = @import("std");
const Io = std.Io;
const Build = std.Build;
const Step = std.Build.Step;
const Dir = std.Io.Dir;
const mem = std.mem;
const process = std.process;
const EnvMap = std.process.Environ.Map;
const assert = std.debug.assert;
const Path = std.Build.Cache.Path;
const Configuration = std.Build.Configuration;
pub const base_tag: Step.Tag = .run;
step: Step,
/// See also addArg and addArgs to modifying this directly
argv: std.ArrayList(Arg),
/// Use `setCwd` to set the initial current working directory
cwd: ?Build.LazyPath,
/// Override this field to modify the environment, or use setEnvironmentVariable
environ_map: ?*EnvMap,
/// Named files that will be provided to the parent process.
/// See `std.process.Preopens`.
preopens: std.array_hash_map.Auto(Configuration.String, Build.LazyPath),
/// Controls the `NO_COLOR` and `CLICOLOR_FORCE` environment variables.
color: Color = .auto,
/// When `true` prevents `ZIG_PROGRESS` environment variable from being passed
/// to the child process, which otherwise would be used for the child to send
/// progress updates to the parent.
disable_zig_progress: bool,
/// Configures whether the Run step is considered to have side-effects, and also
/// whether the Run step will inherit stdio streams, forwarding them to the
/// parent process, in which case will require a global lock to prevent other
/// steps from interfering with stdio while the subprocess associated with this
/// Run step is running.
/// If the Run step is determined to not have side-effects, then execution will
/// be skipped if all output files are up-to-date and input files are
/// unchanged.
stdio: StdIo,
/// This field must be `.none` if stdio is `inherit`.
/// It should be only set using `setStdIn`.
stdin: StdIn,
/// Additional input files that, when modified, indicate that the Run step
/// should be re-executed.
/// If the Run step is determined to have side-effects, the Run step is always
/// executed when it appears in the build graph, regardless of whether these
/// files have been modified.
file_inputs: std.ArrayList(std.Build.LazyPath),
/// After adding an output argument, this step will by default rename itself
/// for a better display name in the build summary.
/// This can be disabled by setting this to false.
rename_step_with_output_arg: bool,
/// If this is true, a Run step which is configured to check the output of the
/// executed binary will not fail the build if the binary cannot be executed
/// due to being for a foreign binary to the host system which is running the
/// build graph.
///
/// Command-line arguments such as -fqemu and -fwasmtime may affect whether a
/// binary is detected as foreign, as well as system configuration such as
/// Rosetta (macOS) and binfmt_misc (Linux).
///
/// If this Run step is considered to have side-effects, then this flag does
/// nothing.
skip_foreign_checks: bool,
/// If this is true, failing to execute a foreign binary will be considered an
/// error. However if this is false, the step will be skipped on failure instead.
///
/// This allows for a Run step to attempt to execute a foreign binary using an
/// external executor (such as qemu) but not fail if the executor is unavailable.
failing_to_execute_foreign_is_an_error: bool,
/// If stderr or stdout exceeds this amount, the child process is killed and
/// the step fails.
stdio_limit: std.Io.Limit,
captured_stdout: ?*CapturedStdIo,
captured_stderr: ?*CapturedStdIo,
has_side_effects: bool,
test_runner_mode: bool = false,
/// If this Run step was produced by a Compile step, it is tracked here.
producer: ?*Step.Compile,
pub const Color = std.Build.Configuration.Step.Run.Color;
pub const StdIn = union(enum) {
none,
bytes: []const u8,
lazy_path: std.Build.LazyPath,
};
pub const StdIo = union(enum) {
/// Whether the Run step has side-effects will be determined by whether or not one
/// of the args is an output file (added with `addOutputFileArg`).
/// If the Run step is determined to have side-effects, this is the same as `inherit`.
/// The step will fail if the subprocess crashes or returns a non-zero exit code.
infer_from_args,
/// Causes the Run step to be considered to have side-effects, and therefore
/// always execute when it appears in the build graph.
/// It also means that this step will obtain a global lock to prevent other
/// steps from running in the meantime.
/// The step will fail if the subprocess crashes or returns a non-zero exit code.
inherit,
/// Causes the Run step to be considered to *not* have side-effects. The
/// process will be re-executed if any of the input dependencies are
/// modified. The exit code and standard I/O streams will be checked for
/// certain conditions, and the step will succeed or fail based on these
/// conditions.
/// Note that an explicit check for exit code 0 needs to be added to this
/// list if such a check is desirable.
check: std.ArrayList(Check),
/// This Run step is running a zig unit test binary and will communicate
/// extra metadata over the IPC protocol.
zig_test,
pub const Check = union(enum) {
expect_stderr_exact: []const u8,
expect_stderr_match: []const u8,
expect_stdout_exact: []const u8,
expect_stdout_match: []const u8,
expect_term: process.Child.Term,
expect_stderr_snapshot: std.Build.LazyPath,
expect_stdout_snapshot: std.Build.LazyPath,
};
};
pub const Arg = union(enum) {
artifact: DecoratedArtifact,
lazy_path: DecoratedLazyPath,
decorated_directory: DecoratedLazyPath,
file_content: DecoratedFileContent,
bytes: []const u8,
output_file: *Output,
output_file_dep: *Output,
output_directory: *Output,
/// The arguments passed after "--" on the "zig build" CLI.
passthru,
enable_darling: ToggleFlags,
enable_qemu: ToggleFlags,
enable_rosetta: ToggleFlags,
enable_wasmtime: ToggleFlags,
enable_wine: ToggleFlags,
};
pub const ToggleFlags = struct {
/// The string to pass when enabled, or null to omit the arg.
enabled: ?[]const u8 = null,
/// The string to pass when disabled, or null to omit the arg.
disabled: ?[]const u8 = null,
};
pub const DecoratedArtifact = struct {
prefix: []const u8,
suffix: []const u8,
artifact: *Step.Compile,
make_absolute: bool,
};
pub const DecoratedLazyPath = struct {
prefix: []const u8,
lazy_path: std.Build.LazyPath,
suffix: []const u8,
make_absolute: bool,
};
pub const DecoratedFileContent = struct {
prefix: []const u8,
lazy_path: std.Build.LazyPath,
suffix: []const u8,
};
pub const Output = struct {
generated_file: Configuration.GeneratedFileIndex,
prefix: []const u8,
basename: []const u8,
suffix: []const u8,
make_absolute: bool,
};
pub const CapturedStdIo = struct {
generated_file: Configuration.GeneratedFileIndex,
prefix: []const u8,
basename: []const u8,
trim_whitespace: TrimWhitespace,
pub const Options = struct {
/// `null` means `stdout`/`stderr`.
basename: ?[]const u8 = null,
/// Does not affect `expectStdOutEqual`/`expectStdErrEqual`.
trim_whitespace: TrimWhitespace = .none,
};
pub const TrimWhitespace = std.Build.Configuration.Step.Run.TrimWhitespace;
};
pub fn create(owner: *std.Build, name: []const u8) *Run {
const run = owner.allocator.create(Run) catch @panic("OOM");
run.* = .{
.step = .init(.{
.tag = base_tag,
.name = name,
.owner = owner,
}),
.argv = .empty,
.cwd = null,
.environ_map = null,
.preopens = .empty,
.disable_zig_progress = false,
.stdio = .infer_from_args,
.stdin = .none,
.file_inputs = .empty,
.rename_step_with_output_arg = true,
.skip_foreign_checks = false,
.failing_to_execute_foreign_is_an_error = true,
.stdio_limit = .unlimited,
.captured_stdout = null,
.captured_stderr = null,
.has_side_effects = false,
.producer = null,
};
return run;
}
pub fn setName(run: *Run, name: []const u8) void {
run.step.name = name;
run.rename_step_with_output_arg = false;
}
pub fn enableTestRunnerMode(run: *Run) void {
if (run.test_runner_mode) return;
run.stdio = .zig_test;
run.test_runner_mode = true;
}
pub const ArgOptions = struct {
prefix: []const u8 = "",
suffix: []const u8 = "",
};
pub const PathArgOptions = struct {
prefix: []const u8 = "",
suffix: []const u8 = "",
/// Makes the path absolute before passing it to the child process. Not supported by all hosts,
/// prefer accepting relative paths when possible.
make_absolute: bool = false,
};
/// Deprecated, use `addArtifactArg2`.
pub fn addArtifactArg(run: *Run, artifact: *Step.Compile) void {
run.addArtifactArg2(artifact, .{});
}
/// Deprecated, use `addArtifactArg2`.
pub fn addPrefixedArtifactArg(run: *Run, prefix: []const u8, artifact: *Step.Compile) void {
run.addArtifactArg2(artifact, .{ .prefix = prefix });
}
pub fn addArtifactArg2(run: *Run, artifact: *Step.Compile, options: PathArgOptions) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
const prefixed_artifact: DecoratedArtifact = .{
.prefix = graph.dupeString(options.prefix),
.artifact = artifact,
.suffix = graph.dupeString(options.suffix),
.make_absolute = options.make_absolute,
};
run.argv.append(arena, .{ .artifact = prefixed_artifact }) catch @panic("OOM");
const bin_file = artifact.getEmittedBin();
bin_file.addStepDependencies(&run.step);
}
/// Deprecated, use `addOutputFileArg2`.
pub fn addOutputFileArg(run: *Run, sub_path: []const u8) std.Build.LazyPath {
return run.addOutputFileArg2(sub_path, .{});
}
/// Deprecated, use `addOutputFileArg2`.
pub fn addPrefixedOutputFileArg(
run: *Run,
prefix: []const u8,
sub_path: []const u8,
) std.Build.LazyPath {
return run.addOutputFileArg2(sub_path, .{ .prefix = prefix });
}
/// Provides a file path as a command line argument to the command being run.
///
/// For example, a prefix of "-o" and `sub_path` of "output.txt" will result in
/// the child process seeing something like this: "-ozig-cache/.../output.txt"
///
/// The child process will see a single argument, regardless of whether the
/// prefix or `sub_path` have spaces.
///
/// The returned `std.Build.LazyPath` can be used as inputs to other APIs
/// throughout the build system.
///
/// Related:
/// * `addFileArg` - for input files given to the child process
pub fn addOutputFileArg2(
run: *Run,
/// The name of the generated output file which may have zero or more path
/// components.
///
/// Asserted to be non-empty.
sub_path: []const u8,
options: PathArgOptions,
) std.Build.LazyPath {
const b = run.step.owner;
const graph = b.graph;
const arena = graph.arena;
assert(sub_path.len != 0);
const output = graph.create(Output);
output.* = .{
.prefix = graph.dupeString(options.prefix),
.basename = graph.dupeString(sub_path),
.suffix = graph.dupeString(options.suffix),
.generated_file = graph.addGeneratedFile(&run.step),
.make_absolute = options.make_absolute,
};
run.argv.append(arena, .{ .output_file = output }) catch @panic("OOM");
if (run.rename_step_with_output_arg) {
run.setName(b.fmt("{s} ({s})", .{ run.step.name, sub_path }));
}
return .{ .generated = .{ .index = output.generated_file } };
}
/// See `addFileArg2`.
pub fn addFileArg(run: *Run, lp: std.Build.LazyPath) void {
run.addFileArg2(lp, .{});
}
/// See `addFileArg2`.
pub fn addPrefixedFileArg(run: *Run, prefix: []const u8, lp: std.Build.LazyPath) void {
run.addFileArg2(lp, .{ .prefix = prefix });
}
/// Appends an input file to the command line arguments prepended with a string.
///
/// For example, a prefix of "-F" will result in the child process seeing something
/// like this: "-Fexample.txt"
///
/// The child process will see a single argument, even if the prefix has
/// spaces. Modifications to this file will be detected as a cache miss in
/// subsequent builds, causing the child process to be re-executed.
///
/// Related:
/// * `addOutputFileArg` - for files generated by the child process
pub fn addFileArg2(run: *Run, lp: std.Build.LazyPath, options: PathArgOptions) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
const prefixed_file_source: DecoratedLazyPath = .{
.prefix = graph.dupeString(options.prefix),
.lazy_path = lp.dupe(graph),
.suffix = graph.dupeString(options.suffix),
.make_absolute = options.make_absolute,
};
run.argv.append(arena, .{ .lazy_path = prefixed_file_source }) catch @panic("OOM");
lp.addStepDependencies(&run.step);
}
/// Deprecated, use `addFileContentArg2`.
pub fn addFileContentArg(run: *Run, lp: std.Build.LazyPath) void {
return run.addFileContentArg2(lp, .{});
}
/// Deprecated, use `addFileContentArg2`.
pub fn addPrefixedFileContentArg(run: *Run, prefix: []const u8, lp: std.Build.LazyPath) void {
return run.addFileContentArg2(lp, .{ .prefix = prefix });
}
/// Appends the content of an input file to the command line arguments prepended with a string.
///
/// For example, a prefix of "-F" will result in the child process seeing something
/// like this: "-Fmy file content"
///
/// The child process will see a single argument, even if the prefix and/or the file
/// contain whitespace.
/// This means that the entire file content up to EOF is rendered as one contiguous
/// string, including escape sequences. Notably, any (trailing) newlines will show up
/// like this: "hello,\nfile world!\n"
///
/// Modifications to the source file will be detected as a cache miss in subsequent
/// builds, causing the child process to be re-executed.
///
/// This function may not be used to supply the first argument of a `Run` step.
///
/// Related:
/// * `addFileContentArg` - same thing but without the prefix
pub fn addFileContentArg2(run: *Run, lp: std.Build.LazyPath, options: ArgOptions) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
// Some parts of this step's configure phase API rely on the first argument being somewhat
// transparent/readable, but the content of the file specified by `lp` remains completely
// opaque until its path can be resolved during the make phase.
if (run.argv.items.len == 0) {
@panic("'addFileContentArg'/'addPrefixedFileContentArg' cannot be first argument");
}
const file_content: DecoratedFileContent = .{
.prefix = graph.dupeString(options.prefix),
.lazy_path = lp.dupe(graph),
.suffix = graph.dupeString(options.suffix),
};
run.argv.append(arena, .{ .file_content = file_content }) catch @panic("OOM");
lp.addStepDependencies(&run.step);
}
/// Deprecated, use `addOutputDirectoryArg2`.
pub fn addOutputDirectoryArg(run: *Run, basename: []const u8) std.Build.LazyPath {
return run.addOutputDirectoryArg2(basename, .{});
}
/// Deprecated, use `addOutputDirectoryArg2`.
pub fn addPrefixedOutputDirectoryArg(
run: *Run,
prefix: []const u8,
basename: []const u8,
) std.Build.LazyPath {
return run.addOutputDirectoryArg2(basename, .{ .prefix = prefix });
}
/// Provides a directory path as a command line argument to the command being run.
/// Asserts `basename` is not empty.
///
/// For example, a prefix of "-o" and basename of "output_dir" will result in
/// the child process seeing something like this: "-ozig-cache/.../output_dir"
///
/// The child process will see a single argument, regardless of whether the
/// prefix or basename have spaces.
///
/// The returned `std.Build.LazyPath` can be used as inputs to other APIs
/// throughout the build system.
///
/// Related:
/// * `addDirectoryArg` - for input directories given to the child process
pub fn addOutputDirectoryArg2(
run: *Run,
basename: []const u8,
options: PathArgOptions,
) std.Build.LazyPath {
if (basename.len == 0) @panic("basename must not be empty");
const graph = run.step.owner.graph;
const arena = graph.arena;
const output = arena.create(Output) catch @panic("OOM");
output.* = .{
.prefix = graph.dupeString(options.prefix),
.basename = graph.dupeString(basename),
.suffix = graph.dupeString(options.suffix),
.generated_file = graph.addGeneratedFile(&run.step),
.make_absolute = options.make_absolute,
};
run.argv.append(arena, .{ .output_directory = output }) catch @panic("OOM");
if (run.rename_step_with_output_arg) {
run.setName(std.fmt.allocPrint(arena, "{s} ({s})", .{ run.step.name, basename }) catch @panic("OOM"));
}
return .{ .generated = .{ .index = output.generated_file } };
}
/// Deprecated, use `addDirectoryArg2`.
pub fn addDirectoryArg(run: *Run, lazy_directory: std.Build.LazyPath) void {
run.addDirectoryArg2(lazy_directory, .{});
}
/// Deprecated, use `addDirectoryArg2`.
pub fn addPrefixedDirectoryArg(run: *Run, prefix: []const u8, lazy_directory: std.Build.LazyPath) void {
run.addDirectoryArg2(lazy_directory, .{ .prefix = prefix });
}
/// Deprecated, use `addDirectoryArg2`.
pub fn addDecoratedDirectoryArg(
run: *Run,
prefix: []const u8,
lazy_directory: std.Build.LazyPath,
suffix: []const u8,
) void {
run.addDirectoryArg2(lazy_directory, .{ .prefix = prefix, .suffix = suffix });
}
pub fn addDirectoryArg2(
run: *Run,
lazy_directory: std.Build.LazyPath,
options: PathArgOptions,
) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
run.argv.append(arena, .{ .decorated_directory = .{
.prefix = graph.dupeString(options.prefix),
.lazy_path = lazy_directory.dupe(graph),
.suffix = graph.dupeString(options.suffix),
.make_absolute = options.make_absolute,
} }) catch @panic("OOM");
lazy_directory.addStepDependencies(&run.step);
}
/// Deprecated, use `addDepFileOutputArg2`.
pub fn addDepFileOutputArg(run: *Run, basename: []const u8) std.Build.LazyPath {
return run.addDepFileOutputArg2(basename, .{});
}
/// Deprecated, use `addDepFileOutputArg2`.
pub fn addPrefixedDepFileOutputArg(run: *Run, prefix: []const u8, basename: []const u8) std.Build.LazyPath {
return run.addDepFileOutputArg2(basename, .{ .prefix = prefix });
}
/// Add a path argument to a dep file (.d) for the child process to write its
/// discovered additional dependencies.
/// Only one dep file argument is allowed by instance.
pub fn addDepFileOutputArg2(run: *Run, basename: []const u8, options: PathArgOptions) std.Build.LazyPath {
const b = run.step.owner;
const graph = b.graph;
const arena = graph.arena;
const dep_file = arena.create(Output) catch @panic("OOM");
dep_file.* = .{
.prefix = graph.dupeString(options.prefix),
.basename = graph.dupeString(basename),
.suffix = graph.dupeString(options.suffix),
.generated_file = graph.addGeneratedFile(&run.step),
.make_absolute = options.make_absolute,
};
run.argv.append(arena, .{ .output_file_dep = dep_file }) catch @panic("OOM");
return .{ .generated = .{ .index = dep_file.generated_file } };
}
/// Appends the contents of `arg`, verbatim, to the command line that will be
/// passed to the process being run.
///
/// If `arg` is an input file, `addFileInput` (or related function) must be
/// used instead to ensure correct cache behavior.
///
/// If `arg` is an output file, `addOutputFileArg` (or related function) must
/// be used instead to ensure correct cache behavior.
pub fn addArg(run: *Run, arg: []const u8) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
run.argv.append(arena, .{ .bytes = graph.dupeString(arg) }) catch @panic("OOM");
}
/// Appends each of `args`, verbatim, to the command line that will be passed
/// to the process being run.
///
/// If any element of `args` is an input file, `addFileInput` must be used
/// instead to ensure correct cache behavior.
///
/// If any element of `args` is an output file, `addOutputFileArg` (or related
/// function) must be used instead to ensure correct cache behavior.
pub fn addArgs(run: *Run, args: []const []const u8) void {
for (args) |arg| run.addArg(arg);
}
/// Appends the extra arguments provided to `zig build` to the command line
/// that will be passed to the process being run.
///
/// This causes the step to be considered to have side effects, disabling
/// caching.
///
/// In the example command `zig build run -- arg1 arg2`, "arg1" and "arg2" will
/// be passed to the process being run.
pub fn addPassthruArgs(run: *Run) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
run.argv.append(arena, .passthru) catch @panic("OOM");
}
/// Appends a custom string to the command line depending on the `-fdarling`
/// value passed to `zig build`.
pub fn addThirdPartyEnabledArgDarling(run: *Run, toggle_flags: ToggleFlags) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
run.argv.append(arena, .{ .enable_darling = toggle_flags }) catch @panic("OOM");
}
/// Appends a custom string to the command line depending on the `-fqemu`
/// value passed to `zig build`.
pub fn addThirdPartyEnabledArgQemu(run: *Run, toggle_flags: ToggleFlags) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
run.argv.append(arena, .{ .enable_qemu = toggle_flags }) catch @panic("OOM");
}
/// Appends a custom string to the command line depending on the `-frosetta`
/// value passed to `zig build`.
pub fn addThirdPartyEnabledArgRosetta(run: *Run, toggle_flags: ToggleFlags) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
run.argv.append(arena, .{ .enable_rosetta = toggle_flags }) catch @panic("OOM");
}
/// Appends a custom string to the command line depending on the `-fwasmtime`
/// value passed to `zig build`.
pub fn addThirdPartyEnabledArgWasmtime(run: *Run, toggle_flags: ToggleFlags) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
run.argv.append(arena, .{ .enable_wasmtime = toggle_flags }) catch @panic("OOM");
}
/// Appends a custom string to the command line depending on the `-fwine`
/// value passed to `zig build`.
pub fn addThirdPartyEnabledArgWine(run: *Run, toggle_flags: ToggleFlags) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
run.argv.append(arena, .{ .enable_wine = toggle_flags }) catch @panic("OOM");
}
pub fn setStdIn(run: *Run, stdin: StdIn) void {
switch (stdin) {
.lazy_path => |lazy_path| lazy_path.addStepDependencies(&run.step),
.bytes, .none => {},
}
run.stdin = stdin;
}
pub fn setCwd(run: *Run, cwd: Build.LazyPath) void {
const graph = run.step.owner.graph;
cwd.addStepDependencies(&run.step);
run.cwd = cwd.dupe(graph);
}
pub fn clearEnvironment(run: *Run) void {
const b = run.step.owner;
const new_env_map = b.allocator.create(EnvMap) catch @panic("OOM");
new_env_map.* = .init(b.allocator);
run.environ_map = new_env_map;
}
pub fn getEnvMap(run: *Run) *EnvMap {
return getEnvMapInternal(run);
}
fn getEnvMapInternal(run: *Run) *EnvMap {
const graph = run.step.owner.graph;
const arena = graph.arena;
return run.environ_map orelse {
const cloned_map = arena.create(EnvMap) catch @panic("OOM");
cloned_map.* = graph.environ_map.clone(arena) catch @panic("OOM");
run.environ_map = cloned_map;
return cloned_map;
};
}
pub fn setEnvironmentVariable(run: *Run, key: []const u8, value: []const u8) void {
const environ_map = run.getEnvMap();
// This data structure already dupes keys and values.
environ_map.put(key, value) catch @panic("OOM");
}
pub fn removeEnvironmentVariable(run: *Run, key: []const u8) void {
_ = run.getEnvMap().swapRemove(key);
}
pub fn setPreopen(run: *Run, name: []const u8, resource: Build.LazyPath) void {
const graph = run.step.owner.graph;
const wc = &graph.wip_configuration;
const arena = graph.arena;
resource.addStepDependencies(&run.step);
run.preopens.put(
arena,
wc.addString(name) catch @panic("OOM"),
resource.dupe(graph),
) catch @panic("OOM");
}
/// Adds a check for exact stderr match. Does not add any other checks.
pub fn expectStdErrEqual(run: *Run, bytes: []const u8) void {
const graph = run.step.owner.graph;
run.addCheck(.{ .expect_stderr_exact = graph.dupeString(bytes) });
}
pub fn expectStdErrMatch(run: *Run, bytes: []const u8) void {
const graph = run.step.owner.graph;
run.addCheck(.{ .expect_stderr_match = graph.dupeString(bytes) });
}
/// Adds a check for exact stdout match as well as a check for exit code 0, if
/// there is not already an expected termination check.
pub fn expectStdOutEqual(run: *Run, bytes: []const u8) void {
const graph = run.step.owner.graph;
run.addCheck(.{ .expect_stdout_exact = graph.dupeString(bytes) });
if (!run.hasTermCheck()) run.expectExitCode(0);
}
/// Adds a check for stdout match as well as a check for exit code 0, if there
/// is not already an expected termination check.
pub fn expectStdOutMatch(run: *Run, bytes: []const u8) void {
const graph = run.step.owner.graph;
run.addCheck(.{ .expect_stdout_match = graph.dupeString(bytes) });
if (!run.hasTermCheck()) run.expectExitCode(0);
}
pub fn expectExitCode(run: *Run, code: u8) void {
const new_check: StdIo.Check = .{ .expect_term = .{ .exited = code } };
run.addCheck(new_check);
}
pub fn hasTermCheck(run: Run) bool {
for (run.stdio.check.items) |check| switch (check) {
.expect_term => return true,
else => continue,
};
return false;
}
pub fn addCheck(run: *Run, new_check: StdIo.Check) void {
const b = run.step.owner;
switch (run.stdio) {
.infer_from_args => {
run.stdio = .{ .check = .empty };
run.stdio.check.append(b.allocator, new_check) catch @panic("OOM");
},
.check => |*checks| checks.append(b.allocator, new_check) catch @panic("OOM"),
else => @panic("illegal call to addCheck: conflicting helper method calls. Suggest to directly set stdio field of Run instead"),
}
switch (new_check) {
.expect_stderr_snapshot,
.expect_stdout_snapshot,
=> |file| run.addFileInput(file),
else => {},
}
}
pub fn captureStdErr(run: *Run, options: CapturedStdIo.Options) std.Build.LazyPath {
assert(run.stdio != .inherit);
assert(run.stdio != .zig_test);
const b = run.step.owner;
const graph = b.graph;
const arena = graph.arena;
if (run.captured_stderr) |captured| return .{ .generated = .{ .index = captured.generated_file } };
const captured = arena.create(CapturedStdIo) catch @panic("OOM");
captured.* = .{
.prefix = "",
.basename = if (options.basename) |basename| graph.dupeString(basename) else "stderr",
.generated_file = graph.addGeneratedFile(&run.step),
.trim_whitespace = options.trim_whitespace,
};
run.captured_stderr = captured;
return .{ .generated = .{ .index = captured.generated_file } };
}
pub fn captureStdOut(run: *Run, options: CapturedStdIo.Options) std.Build.LazyPath {
assert(run.stdio != .inherit);
assert(run.stdio != .zig_test);
const b = run.step.owner;
const graph = b.graph;
const arena = graph.arena;
if (run.captured_stdout) |captured| return .{ .generated = .{ .index = captured.generated_file } };
const captured = arena.create(CapturedStdIo) catch @panic("OOM");
captured.* = .{
.prefix = "",
.basename = if (options.basename) |basename| graph.dupeString(basename) else "stdout",
.generated_file = graph.addGeneratedFile(&run.step),
.trim_whitespace = options.trim_whitespace,
};
run.captured_stdout = captured;
return .{ .generated = .{ .index = captured.generated_file } };
}
/// Adds an additional input files that, when modified, indicates that this Run
/// step should be re-executed.
/// If the Run step is determined to have side-effects, the Run step is always
/// executed when it appears in the build graph, regardless of whether this
/// file has been modified.
pub fn addFileInput(run: *Run, file_input: std.Build.LazyPath) void {
const graph = run.step.owner.graph;
const arena = graph.arena;
file_input.addStepDependencies(&run.step);
run.file_inputs.append(arena, file_input.dupe(graph)) catch @panic("OOM");
}