Commit 159cd528b1

Jakub Konka <kubkon@jakubkonka.com>
2021-08-02 10:04:54
Add -Denable-macos-sdk explicit flag to build.zig
This way, we can explicitly signal if a test requires the presence of macOS SDK to build. For instance, when testing our in-house MachO linker for correctly linking Objective-C, we require the presence of the SDK on the host system, and we can enforce this with `-Denable-macos-sdk` flag to `zig build test-standalone`.
1 parent 68e26a2
Changed files (5)
ci/azure/macos_script
@@ -57,7 +57,7 @@ make $JOBS install
 # TODO figure out why this causes a segmentation fault
 # release/bin/zig test ../test/behavior.zig -fno-stage1 -fLLVM -I ../test
 
-release/bin/zig build test-toolchain
+release/bin/zig build test-toolchain -Denable-macos-sdk
 release/bin/zig build test-std
 release/bin/zig build docs
 
test/standalone/objc/build.zig
@@ -1,5 +1,15 @@
 const std = @import("std");
 const Builder = std.build.Builder;
+const CrossTarget = std.zig.CrossTarget;
+
+fn isRunnableTarget(t: CrossTarget) bool {
+    // TODO I think we might be able to run this on Linux via Darling.
+    // Add a check for that here, and return true if Darling is available.
+    if (t.isNative() and t.getOsTag() == .macos)
+        return true
+    else
+        return false;
+}
 
 pub fn build(b: *Builder) void {
     const mode = b.standardReleaseOptions();
@@ -15,8 +25,12 @@ pub fn build(b: *Builder) void {
     exe.setBuildMode(mode);
     exe.setTarget(target);
     exe.linkLibC();
+    // TODO when we figure out how to ship framework stubs for cross-compilation,
+    // populate paths to the sysroot here.
     exe.linkFramework("Foundation");
 
-    const run_cmd = exe.run();
-    test_step.dependOn(&run_cmd.step);
+    if (isRunnableTarget(target)) {
+        const run_cmd = exe.run();
+        test_step.dependOn(&run_cmd.step);
+    }
 }
test/standalone.zig
@@ -38,9 +38,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
         cases.addBuildFile("test/standalone/pie/build.zig", .{});
     }
     // Try to build and run an Objective-C executable.
-    if (std.Target.current.os.tag == .macos) {
-        cases.addBuildFile("test/standalone/objc/build.zig", .{ .build_modes = true });
-    }
+    cases.addBuildFile("test/standalone/objc/build.zig", .{ .build_modes = true, .requires_macos_sdk = true });
 
     // Ensure the development tools are buildable.
     cases.add("tools/gen_spirv_spec.zig");
test/tests.zig
@@ -383,7 +383,14 @@ pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8, modes:
     return cases.step;
 }
 
-pub fn addStandaloneTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode, skip_non_native: bool, target: std.zig.CrossTarget) *build.Step {
+pub fn addStandaloneTests(
+    b: *build.Builder,
+    test_filter: ?[]const u8,
+    modes: []const Mode,
+    skip_non_native: bool,
+    enable_macos_sdk: bool,
+    target: std.zig.CrossTarget,
+) *build.Step {
     const cases = b.allocator.create(StandaloneContext) catch unreachable;
     cases.* = StandaloneContext{
         .b = b,
@@ -392,6 +399,7 @@ pub fn addStandaloneTests(b: *build.Builder, test_filter: ?[]const u8, modes: []
         .test_filter = test_filter,
         .modes = modes,
         .skip_non_native = skip_non_native,
+        .enable_macos_sdk = enable_macos_sdk,
         .target = target,
     };
 
@@ -831,6 +839,7 @@ pub const StandaloneContext = struct {
     test_filter: ?[]const u8,
     modes: []const Mode,
     skip_non_native: bool,
+    enable_macos_sdk: bool,
     target: std.zig.CrossTarget,
 
     pub fn addC(self: *StandaloneContext, root_src: []const u8) void {
@@ -841,9 +850,15 @@ pub const StandaloneContext = struct {
         self.addAllArgs(root_src, false);
     }
 
-    pub fn addBuildFile(self: *StandaloneContext, build_file: []const u8, features: struct { build_modes: bool = false, cross_targets: bool = false }) void {
+    pub fn addBuildFile(self: *StandaloneContext, build_file: []const u8, features: struct {
+        build_modes: bool = false,
+        cross_targets: bool = false,
+        requires_macos_sdk: bool = false,
+    }) void {
         const b = self.b;
 
+        if (features.requires_macos_sdk and !self.enable_macos_sdk) return;
+
         const annotated_case_name = b.fmt("build {s}", .{build_file});
         if (self.test_filter) |filter| {
             if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
build.zig
@@ -61,6 +61,7 @@ pub fn build(b: *Builder) !void {
     const omit_stage2 = b.option(bool, "omit-stage2", "Do not include stage2 behind a feature flag inside stage1") orelse false;
     const static_llvm = b.option(bool, "static-llvm", "Disable integration with system-installed LLVM, Clang, LLD, and libc++") orelse false;
     const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse (is_stage1 or static_llvm);
+    const enable_macos_sdk = b.option(bool, "enable-macos-sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse false;
     const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h");
 
     if (!skip_install_lib_files) {
@@ -340,7 +341,7 @@ pub fn build(b: *Builder) !void {
     ));
 
     toolchain_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
-    toolchain_step.dependOn(tests.addStandaloneTests(b, test_filter, modes, skip_non_native, target));
+    toolchain_step.dependOn(tests.addStandaloneTests(b, test_filter, modes, skip_non_native, enable_macos_sdk, target));
     toolchain_step.dependOn(tests.addStackTraceTests(b, test_filter, modes));
     toolchain_step.dependOn(tests.addCliTests(b, test_filter, modes));
     toolchain_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, modes));