From 728a096bf2afa908d3766280a225b514792cc751 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Wed, 8 May 2024 23:15:23 -0400 Subject: [PATCH] msvc --- .github/workflows/ci.yaml | 2 +- ci/build.zig | 127 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9997ca7f3cf1..5f254330901e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -178,7 +178,7 @@ jobs: fetch-depth: 0 - name: Build and Test working-directory: ci - run: '& $Env:USERPROFILE\deps\zig-x86_64-windows-gnu-${{env.ci-zig-version}}\bin\zig.exe build -Dbuild-type=Debug -Dskip-non-native cmake-bootstrap tidy update-stage1' + run: '& $Env:USERPROFILE\deps\zig-x86_64-windows-gnu-${{env.ci-zig-version}}\bin\zig.exe build -Dbuild-type=Debug -Dskip-non-native cmake-bootstrap tidy update-stage1 msvc' x86_64-windows-release: if: github.repository_owner == 'ziglang' runs-on: [self-hosted, Windows, x86_64] diff --git a/ci/build.zig b/ci/build.zig index a8fad42d9021..a44bf5daef5b 100644 --- a/ci/build.zig +++ b/ci/build.zig @@ -553,5 +553,132 @@ pub fn build(b: *std.Build) void { run_tests_with_updated_stage4.addFileArg(b.path("../test/behavior.zig")); update_stage1_step.dependOn(&run_tests_with_updated_stage4.step); } + + if (std.zig.WindowsSdk.find(b.allocator)) |sdk| msvc: { + const win10sdk = sdk.windows10sdk orelse break :msvc; + std.debug.print("{s}\n", .{b.pathResolve(&.{ + win10sdk.path, + "Include", + win10sdk.version, + "ucrt", + })}); + + const msvc_arch = switch (host.result.cpu.arch) { + .x86 => "x86", + .x86_64 => "x64", + .arm, .armeb, .thumb, .thumbeb => "arm", + .aarch64 => "arm64", + else => break :msvc, + }; + const cl_exe = b.pathResolve(&.{ + sdk.msvc_lib_dir orelse break :msvc, + "..", + "..", + "bin", + b.fmt("Host{s}", .{msvc_arch}), + msvc_arch, + b.fmt("cl{s}", .{host_exe_file_ext}), + }); + const msvc_triple = triple: { + var msvc_target = host.result; + msvc_target.abi = .msvc; + break :triple msvc_target.linuxTriple(b.allocator) catch @panic("OOM"); + }; + + const msvc_step = b.step("msvc", "Run the behavior tests compiled with MSVC"); + + const build_msvc_behavior_tests = std.Build.Step.Run.create(b, "build msvc behavior tests"); + build_msvc_behavior_tests.step.max_rss = 213_237_760; + if (false) + build_msvc_behavior_tests.addFileArg(stage3_exe) + else + build_msvc_behavior_tests.addArg(b.cache_root.join(b.allocator, &.{ + "o", + "4aeed00bc8dFaa6ee10d8a09a88bd97b", + "build", + "stage3", + "bin", + b.fmt("zig{s}", .{host_exe_file_ext}), + }) catch @panic("OOM")); + build_msvc_behavior_tests.addArgs(&.{ + "test", + "-ofmt=c", + "--test-no-exec", + "-target", + msvc_triple, + "-lc", + }); + build_msvc_behavior_tests.addFileArg(b.path("../test/behavior.zig")); + const behavior_msvc_source = + build_msvc_behavior_tests.addPrefixedOutputFileArg("-femit-bin=", "behavior-msvc.c"); + + const build_msvc_compiler_rt = std.Build.Step.Run.create(b, "build msvc compiler-rt"); + build_msvc_compiler_rt.step.max_rss = 164_306_944; + if (false) + build_msvc_compiler_rt.addFileArg(stage3_exe) + else + build_msvc_compiler_rt.addArg(b.cache_root.join(b.allocator, &.{ + "o", + "4aeed00bc8dFaa6ee10d8a09a88bd97b", + "build", + "stage3", + "bin", + b.fmt("zig{s}", .{host_exe_file_ext}), + }) catch @panic("OOM")); + build_msvc_compiler_rt.addArgs(&.{ + "build-obj", + "-ofmt=c", + "-OReleaseSmall", + "--name", + "compiler_rt", + "-target", + msvc_triple, + "-lc", + }); + build_msvc_compiler_rt.addFileArg(b.path("../lib/compiler_rt.zig")); + const compiler_rt_msvc_source = + build_msvc_compiler_rt.addPrefixedOutputFileArg("-femit-bin=", "compiler_rt-msvc.c"); + + const build_msvc_tests = std.Build.Step.Run.create(b, "build msvc tests"); + build_msvc_tests.step.max_rss = 1; + build_msvc_tests.addArgs(&.{ cl_exe, "/W3", "/Z7" }); + build_msvc_tests.addPrefixedDirectoryArg("/I", b.path("../lib")); + build_msvc_tests.addFileArg(behavior_msvc_source); + build_msvc_tests.addFileArg(compiler_rt_msvc_source); + const msvc_tests_exe = build_msvc_tests.addPrefixedOutputFileArg( + "/Fe:", + b.fmt("behavior-msvc{s}", .{host_exe_file_ext}), + ); + // Link args must appear after all other args. + build_msvc_tests.addArgs(&.{ + "/link", + "/nologo", + "/debug", + "/subsystem:console", + "kernel32.lib", + "ntdll.lib", + "libcmt.lib", + }); + build_msvc_tests.setEnvironmentVariable("INCLUDE", std.mem.join( + b.allocator, + &.{std.fs.path.delimiter}, + &.{b.pathResolve(&.{ + win10sdk.path, + "Include", + win10sdk.version, + "ucrt", + })}, + ) catch @panic("OOM")); + if (true) build_msvc_tests.has_side_effects = true; + + const run_msvc_tests = std.Build.Step.Run.create(b, "run msvc tests"); + run_msvc_tests.step.max_rss = 2; + run_msvc_tests.addFileArg(msvc_tests_exe); + msvc_step.dependOn(&run_msvc_tests.step); + } else |err| switch (err) { + error.NotFound => {}, + + else => |e| @panic(@errorName(e)), + } } }