1
1
const std = @import ("std" );
2
2
const builtin = @import ("builtin" );
3
- const Step = std .Build .Step ;
3
+ const Build = std .Build ;
4
+ const Step = Build .Step ;
4
5
const fs = std .fs ;
5
6
const mem = std .mem ;
6
7
const process = std .process ;
@@ -19,10 +20,8 @@ step: Step,
19
20
/// See also addArg and addArgs to modifying this directly
20
21
argv : ArrayList (Arg ),
21
22
22
- /// Set this to modify the current working directory
23
- /// TODO change this to a Build.Cache.Directory to better integrate with
24
- /// future child process cwd API.
25
- cwd : ? []const u8 ,
23
+ /// Use `setCwd` to set the initial current working directory
24
+ cwd : ? Build.LazyPath ,
26
25
27
26
/// Override this field to modify the environment, or use setEnvironmentVariable
28
27
env_map : ? * EnvMap ,
@@ -287,6 +286,11 @@ pub fn setStdIn(self: *Run, stdin: StdIn) void {
287
286
self .stdin = stdin ;
288
287
}
289
288
289
+ pub fn setCwd (self : * Run , cwd : Build.LazyPath ) void {
290
+ cwd .addStepDependencies (& self .step );
291
+ self .cwd = cwd ;
292
+ }
293
+
290
294
pub fn clearEnvironment (self : * Run ) void {
291
295
const b = self .step .owner ;
292
296
const new_env_map = b .allocator .create (EnvMap ) catch @panic ("OOM" );
@@ -650,8 +654,10 @@ fn runCommand(
650
654
const b = step .owner ;
651
655
const arena = b .allocator ;
652
656
653
- try step .handleChildProcUnsupported (self .cwd , argv );
654
- try Step .handleVerbose2 (step .owner , self .cwd , self .env_map , argv );
657
+ const cwd : ? []const u8 = if (self .cwd ) | lazy_cwd | lazy_cwd .getPath (b ) else null ;
658
+
659
+ try step .handleChildProcUnsupported (cwd , argv );
660
+ try Step .handleVerbose2 (step .owner , cwd , self .env_map , argv );
655
661
656
662
const allow_skip = switch (self .stdio ) {
657
663
.check , .zig_test = > self .skip_foreign_checks ,
@@ -778,7 +784,7 @@ fn runCommand(
778
784
self .addPathForDynLibs (exe );
779
785
}
780
786
781
- try Step .handleVerbose2 (step .owner , self . cwd , self .env_map , interp_argv .items );
787
+ try Step .handleVerbose2 (step .owner , cwd , self .env_map , interp_argv .items );
782
788
783
789
break :term spawnChildAndCollect (self , interp_argv .items , has_side_effects , prog_node ) catch | e | {
784
790
if (! self .failing_to_execute_foreign_is_an_error ) return error .MakeSkipped ;
@@ -848,7 +854,7 @@ fn runCommand(
848
854
, .{
849
855
expected_bytes ,
850
856
result .stdio .stderr .? ,
851
- try Step .allocPrintCmd (arena , self . cwd , final_argv ),
857
+ try Step .allocPrintCmd (arena , cwd , final_argv ),
852
858
});
853
859
}
854
860
},
@@ -865,7 +871,7 @@ fn runCommand(
865
871
, .{
866
872
match ,
867
873
result .stdio .stderr .? ,
868
- try Step .allocPrintCmd (arena , self . cwd , final_argv ),
874
+ try Step .allocPrintCmd (arena , cwd , final_argv ),
869
875
});
870
876
}
871
877
},
@@ -882,7 +888,7 @@ fn runCommand(
882
888
, .{
883
889
expected_bytes ,
884
890
result .stdio .stdout .? ,
885
- try Step .allocPrintCmd (arena , self . cwd , final_argv ),
891
+ try Step .allocPrintCmd (arena , cwd , final_argv ),
886
892
});
887
893
}
888
894
},
@@ -899,7 +905,7 @@ fn runCommand(
899
905
, .{
900
906
match ,
901
907
result .stdio .stdout .? ,
902
- try Step .allocPrintCmd (arena , self . cwd , final_argv ),
908
+ try Step .allocPrintCmd (arena , cwd , final_argv ),
903
909
});
904
910
}
905
911
},
@@ -908,7 +914,7 @@ fn runCommand(
908
914
return step .fail ("the following command {} (expected {}):\n {s}" , .{
909
915
fmtTerm (result .term ),
910
916
fmtTerm (expected_term ),
911
- try Step .allocPrintCmd (arena , self . cwd , final_argv ),
917
+ try Step .allocPrintCmd (arena , cwd , final_argv ),
912
918
});
913
919
}
914
920
},
@@ -929,18 +935,18 @@ fn runCommand(
929
935
prefix ,
930
936
fmtTerm (result .term ),
931
937
fmtTerm (expected_term ),
932
- try Step .allocPrintCmd (arena , self . cwd , final_argv ),
938
+ try Step .allocPrintCmd (arena , cwd , final_argv ),
933
939
});
934
940
}
935
941
if (! result .stdio .test_results .isSuccess ()) {
936
942
return step .fail (
937
943
"{s}the following test command failed:\n {s}" ,
938
- .{ prefix , try Step .allocPrintCmd (arena , self . cwd , final_argv ) },
944
+ .{ prefix , try Step .allocPrintCmd (arena , cwd , final_argv ) },
939
945
);
940
946
}
941
947
},
942
948
else = > {
943
- try step .handleChildProcessTerm (result .term , self . cwd , final_argv );
949
+ try step .handleChildProcessTerm (result .term , cwd , final_argv );
944
950
},
945
951
}
946
952
}
@@ -963,8 +969,8 @@ fn spawnChildAndCollect(
963
969
const arena = b .allocator ;
964
970
965
971
var child = std .process .Child .init (argv , arena );
966
- if (self .cwd ) | cwd | {
967
- child .cwd = b . pathFromRoot ( cwd );
972
+ if (self .cwd ) | lazy_cwd | {
973
+ child .cwd = lazy_cwd . getPath ( b );
968
974
} else {
969
975
child .cwd = b .build_root .path ;
970
976
child .cwd_dir = b .build_root .handle ;
0 commit comments