@@ -135,6 +135,7 @@ pub const TestContext = struct {
135
135
extension : Extension ,
136
136
object_format : ? std.builtin.ObjectFormat = null ,
137
137
emit_h : bool = false ,
138
+ llvm_backend : bool = false ,
138
139
139
140
files : std .ArrayList (File ),
140
141
@@ -266,6 +267,21 @@ pub const TestContext = struct {
266
267
return & ctx .cases .items [ctx .cases .items .len - 1 ];
267
268
}
268
269
270
+ /// Adds a test case that uses the LLVM backend to emit an executable.
271
+ /// Currently this implies linking libc, because only then we can generate a testable executable.
272
+ pub fn exeUsingLlvmBackend (ctx : * TestContext , name : []const u8 , target : CrossTarget ) * Case {
273
+ ctx .cases .append (Case {
274
+ .name = name ,
275
+ .target = target ,
276
+ .updates = std .ArrayList (Update ).init (ctx .cases .allocator ),
277
+ .output_mode = .Exe ,
278
+ .extension = .Zig ,
279
+ .files = std .ArrayList (File ).init (ctx .cases .allocator ),
280
+ .llvm_backend = true ,
281
+ }) catch unreachable ;
282
+ return & ctx .cases .items [ctx .cases .items .len - 1 ];
283
+ }
284
+
269
285
pub fn addObj (
270
286
ctx : * TestContext ,
271
287
name : []const u8 ,
@@ -518,10 +534,30 @@ pub const TestContext = struct {
518
534
try thread_pool .init (std .testing .allocator );
519
535
defer thread_pool .deinit ();
520
536
537
+ // Use the same global cache dir for all the tests, such that we for example don't have to
538
+ // rebuild musl libc for every case (when LLVM backend is enabled).
539
+ var global_tmp = std .testing .tmpDir (.{});
540
+ defer global_tmp .cleanup ();
541
+
542
+ var cache_dir = try global_tmp .dir .makeOpenPath ("zig-cache" , .{});
543
+ defer cache_dir .close ();
544
+ const tmp_dir_path = try std .fs .path .join (std .testing .allocator , &[_ ][]const u8 { "." , "zig-cache" , "tmp" , & global_tmp .sub_path });
545
+ defer std .testing .allocator .free (tmp_dir_path );
546
+
547
+ const global_cache_directory : Compilation.Directory = .{
548
+ .handle = cache_dir ,
549
+ .path = try std .fs .path .join (std .testing .allocator , &[_ ][]const u8 { tmp_dir_path , "zig-cache" }),
550
+ };
551
+ defer std .testing .allocator .free (global_cache_directory .path .? );
552
+
521
553
for (self .cases .items ) | case | {
522
554
if (build_options .skip_non_native and case .target .getCpuArch () != std .Target .current .cpu .arch )
523
555
continue ;
524
556
557
+ // Skip tests that require LLVM backend when it is not available
558
+ if (! build_options .have_llvm and case .llvm_backend )
559
+ continue ;
560
+
525
561
var prg_node = root_node .start (case .name , case .updates .items .len );
526
562
prg_node .activate ();
527
563
defer prg_node .end ();
@@ -537,6 +573,7 @@ pub const TestContext = struct {
537
573
case ,
538
574
zig_lib_directory ,
539
575
& thread_pool ,
576
+ global_cache_directory ,
540
577
);
541
578
}
542
579
}
@@ -548,6 +585,7 @@ pub const TestContext = struct {
548
585
case : Case ,
549
586
zig_lib_directory : Compilation.Directory ,
550
587
thread_pool : * ThreadPool ,
588
+ global_cache_directory : Compilation.Directory ,
551
589
) ! void {
552
590
const target_info = try std .zig .system .NativeTargetInfo .detect (allocator , case .target );
553
591
const target = target_info .target ;
@@ -601,7 +639,7 @@ pub const TestContext = struct {
601
639
null ;
602
640
const comp = try Compilation .create (allocator , .{
603
641
.local_cache_directory = zig_cache_directory ,
604
- .global_cache_directory = zig_cache_directory ,
642
+ .global_cache_directory = global_cache_directory ,
605
643
.zig_lib_directory = zig_lib_directory ,
606
644
.thread_pool = thread_pool ,
607
645
.root_name = "test_case" ,
@@ -619,6 +657,9 @@ pub const TestContext = struct {
619
657
.object_format = case .object_format ,
620
658
.is_native_os = case .target .isNativeOs (),
621
659
.is_native_abi = case .target .isNativeAbi (),
660
+ .link_libc = case .llvm_backend ,
661
+ .use_llvm = case .llvm_backend ,
662
+ .self_exe_path = std .testing .zig_exe_path ,
622
663
});
623
664
defer comp .destroy ();
624
665
0 commit comments