diff --git a/examples/headless/storybook_headless_application/BUILD.bazel b/examples/headless/storybook_headless_application/BUILD.bazel index 9c29c28b..97e7ed79 100644 --- a/examples/headless/storybook_headless_application/BUILD.bazel +++ b/examples/headless/storybook_headless_application/BUILD.bazel @@ -212,7 +212,9 @@ swift_test( ] + select({ # Built on Linux: Swift Testing's import libraries from the Windows # Swift SDK (its Library/Testing-*). Built on Windows (CI's - # windows-11-arm job), the installed Swift's toolchain brings its own. + # windows-11-arm job), the installed platform's Library/Testing-*, + # which tools/ci/windows_build.ps1 adds to rules_swift's Windows + # toolchain beside XCTest. "@isomer//tools/windows:built_on_linux": ["@swift_windows_sdk//:testing"], "//conditions:default": [], }), diff --git a/tools/ci/windows_build.ps1 b/tools/ci/windows_build.ps1 index 3914d9dd..df108cbd 100755 --- a/tools/ci/windows_build.ps1 +++ b/tools/ci/windows_build.ps1 @@ -4,7 +4,8 @@ # Visual Studio (ARM64 MSVC) and Windows SDK, so nothing of Microsoft's is # redistributed (tools/windows/README.md). Built only, not run: Windows is # tier 2. The recipe a Windows machine has always built with -# (tools/proof/proof.py's): Bazel in MSVC's environment (vcvarsall arm64), +# (tools/proof/proof.py's), with LLVM's llvm-lib as MSVC's lib.exe (below): +# Bazel in MSVC's environment (vcvarsall arm64), # BAZEL_SH Git's bash and Git's usr\bin ahead of System32 (rules_swift_package_manager # runs find, cp and od; tools/patches/rules_swift_package_manager_windows.patch # in the examples), and --repo_env for SDKROOT, PATH and MSVC's variables @@ -42,8 +43,18 @@ if ($SetUp) { if (-not $sdkroot) { throw 'windows_build: no SDKROOT after Swift was installed' } "SDKROOT=$sdkroot" | Add-Content $env:GITHUB_ENV $paths = @([Environment]::GetEnvironmentVariable('Path', 'User'), [Environment]::GetEnvironmentVariable('Path', 'Machine')) -join ';' - $swiftPaths = $paths.Split(';') | Where-Object { $_ -and $_ -match '\\Swift\\' } | Select-Object -Unique + $swiftPaths = @($paths.Split(';') | Where-Object { $_ -and $_ -match '\\Swift\\' } | Select-Object -Unique) if (-not $swiftPaths) { throw 'windows_build: Swift added nothing to PATH' } + # The Swift runtime's DLLs (swiftCore.dll and the rest), which the tools + # Bazel builds for itself load when they run (protoc-gen-swift, the macro + # plugins): the arm64 installer's runtime is in Runtimes\\usr\bin, + # not always on the PATH it sets. + $swiftRoot = if ($sdkroot -match '^(.*\\Swift)\\Platforms\\') { $Matches[1] } else { Join-Path $env:LOCALAPPDATA 'Programs\Swift' } + $runtime = Get-ChildItem -Recurse -Filter swiftCore.dll -Path (Join-Path $swiftRoot 'Runtimes') -ErrorAction SilentlyContinue | + Where-Object { $_.DirectoryName -match '\\usr\\bin$' } | Select-Object -First 1 + if (-not $runtime) { throw "windows_build: no Swift runtime (swiftCore.dll) under $swiftRoot\Runtimes" } + if ($swiftPaths -notcontains $runtime.DirectoryName) { $swiftPaths = @($runtime.DirectoryName) + $swiftPaths } + "Swift on PATH: $($swiftPaths -join '; ')" $swiftPaths | Add-Content $env:GITHUB_PATH $env:Path = ($swiftPaths -join ';') + ';' + $env:Path $env:SDKROOT = $sdkroot @@ -56,7 +67,55 @@ if ($Bazel.Count -eq 0) { throw "usage: tools/ci/windows_build.ps1 setup | /execroot/_main; llvm-lib +# opens them (both checked on this runner), and makes archives MSVC's +# link.exe reads. rules_cc takes the archiver from BAZEL_VC's +# Tools\MSVC\\bin\HostX64\arm64, so C:\v mirrors the install by +# junctions but for that folder, copied with lib.exe replaced. (LLVM's whole +# toolchain, rules_cc's USE_CLANG_CL, failed: rules_swift's tools pass +# clang-cl a flag it rejects, and its ARM64 backend fails on upb's code.) +$version = (Get-ChildItem (Join-Path $vs 'VC\Tools\MSVC') | Sort-Object Name | Select-Object -Last 1).Name +$llvmLib = Join-Path $vs 'VC\Tools\Llvm\ARM64\bin\llvm-lib.exe' +if (-not (Test-Path $llvmLib)) { $llvmLib = 'C:\Program Files\LLVM\bin\llvm-lib.exe' } +if (-not (Test-Path $llvmLib)) { throw 'windows_build: no llvm-lib on this runner' } +$mirror = 'C:\v' +# mirror : every other child of in +# , a folder by a junction, a file copied. +function mirror($from, $to, $skip) { + New-Item -ItemType Directory -Force $to | Out-Null + foreach ($child in Get-ChildItem -Force $from) { + if ($child.Name -eq $skip) { continue } + $target = Join-Path $to $child.Name + if (Test-Path $target) { continue } + if ($child.PSIsContainer) { New-Item -ItemType Junction -Path $target -Target $child.FullName | Out-Null } + else { Copy-Item $child.FullName $target } + } +} +$tools = "VC\Tools\MSVC\$version\bin\HostX64\arm64" +if (-not (Test-Path (Join-Path $mirror "$tools\lib.exe"))) { + mirror $vs $mirror 'VC' + mirror (Join-Path $vs 'VC') (Join-Path $mirror 'VC') 'Tools' + mirror (Join-Path $vs 'VC\Tools') (Join-Path $mirror 'VC\Tools') 'MSVC' + mirror (Join-Path $vs 'VC\Tools\MSVC') (Join-Path $mirror 'VC\Tools\MSVC') $version + mirror (Join-Path $vs "VC\Tools\MSVC\$version") (Join-Path $mirror "VC\Tools\MSVC\$version") 'bin' + mirror (Join-Path $vs "VC\Tools\MSVC\$version\bin") (Join-Path $mirror "VC\Tools\MSVC\$version\bin") 'HostX64' + mirror (Join-Path $vs "VC\Tools\MSVC\$version\bin\HostX64") (Join-Path $mirror "VC\Tools\MSVC\$version\bin\HostX64") 'arm64' + Copy-Item -Recurse (Join-Path $vs $tools) (Join-Path $mirror $tools) + Copy-Item -Force $llvmLib (Join-Path $mirror "$tools\lib.exe") +} +$env:BAZEL_VC = Join-Path $mirror 'VC' +# LLVM for the target's C and C++ (clang-cl, lld-link, llvm-lib): rules_cc +# makes its clang-cl toolchain from it, which @isomer//tools/windows's +# clang_cl_for_targets takes for the target platform alone (the exec tools +# stay MSVC's). Visual Studio's own, else the image's. +$llvm = Join-Path $vs 'VC\Tools\Llvm\ARM64' +if (-not (Test-Path (Join-Path $llvm 'bin\clang-cl.exe'))) { $llvm = 'C:\Program Files\LLVM' } +$env:BAZEL_LLVM = $llvm +$vcvars = Join-Path $mirror 'VC\Auxiliary\Build\vcvarsall.bat' cmd /c "`"$vcvars`" arm64 >nul && set" | ForEach-Object { if ($_ -match '^([^=]+)=(.*)$') { Set-Item -Path "env:$($Matches[1])" -Value $Matches[2] } } @@ -68,17 +127,131 @@ $env:BAZEL_SH = "$git\bin\bash.exe" $env:Path = $env:Path -replace '(?i)C:\\Windows\\system32;', "$git\usr\bin;C:\Windows\system32;" if ($env:Path -notmatch [regex]::Escape("$git\usr\bin")) { $env:Path = "$env:Path;$git\usr\bin" } $repoEnv = @('SDKROOT', 'PATH', 'INCLUDE', 'LIB', 'LIBPATH', 'UniversalCRTSdkDir', 'UCRTVersion', 'WindowsSdkDir', - 'WindowsSDKVersion', 'VCToolsInstallDir', 'VCINSTALLDIR', 'VSINSTALLDIR') | ForEach-Object { "--repo_env=$_" } + 'WindowsSDKVersion', 'VCToolsInstallDir', 'VCINSTALLDIR', 'VSINSTALLDIR', 'BAZEL_VC', 'BAZEL_LLVM') | + ForEach-Object { "--repo_env=$_" } # Bazelisk, so .bazelversion is honoured (the image's `bazel` may be a # release of its own). $bazelisk = (Get-Command bazelisk -ErrorAction SilentlyContinue) $tool = if ($bazelisk) { 'bazelisk' } else { 'bazel' } Push-Location (Join-Path $PSScriptRoot '..\..\examples') try { + # rules_swift's proto rules as they run on Windows (an upstream fix for + # rules_swift to take; until then this runner's copy, by + # --override_repository): proto/swift_proto_compiler.bzl runs protoc with + # no environment at all (actions.run, no env, no use_default_shell_env), + # so protoc-gen-swift finds no Swift runtime on PATH (0xC0000135, a DLL + # not found), and runs its copy_swift_sources.sh as an executable, which + # Windows can't start (error 193). Here: both with the default shell + # environment (--action_env's PATH: the Swift runtime, Git's cp and + # touch), the script run by the shell (run_shell: Bazel's BAZEL_SH). And + # it joins protoc's --descriptor_set_in with ":", where protoc on Windows + # splits that list at ";" (a proto with dependencies: "No such file or + # directory" for the whole list). + # + # Swift Testing as the installed platform carries it: + # \..\..\Library\Testing- (its module and import + # libraries in usr\lib\swift\windows, its DLLs in usr\bin). + $testing = Get-ChildItem -Directory -Path (Join-Path $env:SDKROOT '..\..\Library') -Filter 'Testing-*' -ErrorAction SilentlyContinue | + Sort-Object Name | Select-Object -Last 1 -ExpandProperty FullName + if (-not $testing) { throw "windows_build: no Library\Testing-* beside the SDK ($env:SDKROOT)" } + # Its DLLs for a test that runs (Bazel's actions and tests take this PATH). + $env:Path = "$testing\usr\bin;$env:Path" + & $tool fetch @repoEnv '--repo=@@rules_swift+' | Out-Host + if ($LASTEXITCODE) { exit $LASTEXITCODE } + $outputBase = (& $tool info @repoEnv output_base | Select-Object -Last 1) + $patched = 'C:\rs' + if (-not (Test-Path $patched)) { + robocopy (Join-Path $outputBase 'external\rules_swift+') $patched /E /NFL /NDL /NJH /NJS /NP | Out-Null + if ($LASTEXITCODE -ge 8) { throw 'windows_build: rules_swift was not copied' } + $file = Join-Path $patched 'proto\swift_proto_compiler.bzl' + $text = [IO.File]::ReadAllText($file) + $edits = @( + @('tools = \[swift_proto_compiler_info\.internal\.plugin\],', "tools = [swift_proto_compiler_info.internal.plugin],`n use_default_shell_env = True,"), + @('actions\.run\((\s+)inputs = depset\(\[temporary_output_directory\]\),', 'actions.run_shell($1inputs = depset([temporary_output_directory, copy_swift_sources_file]),'), + @('executable = copy_swift_sources_file,', "command = copy_swift_sources_file.path,`n use_default_shell_env = True,"), + @('":"\.join\(\[f\.path for f in transitive_descriptor_sets\.to_list\(\)\]\)', '";".join([f.path for f in transitive_descriptor_sets.to_list()])') + ) + foreach ($edit in $edits) { + if ([regex]::Matches($text, $edit[0]).Count -ne 1) { throw "windows_build: rules_swift's swift_proto_compiler.bzl isn't as expected ($($edit[0]))" } + $text = [regex]::Replace($text, $edit[0], $edit[1]) + } + [IO.File]::WriteAllText($file, $text) + # Swift Testing beside XCTest. rules_swift's Windows toolchain gives + # a Swift compile XCTest's module directory (-I) and a link its + # import libraries (-LIBPATH), both under the platform's + # Developer\Library\XCTest-, and nothing of Testing's, which + # the SDK keeps beside it (Developer\Library\Testing-): a + # test that imports Testing finds no such module. Here its two + # directories go where XCTest's do (on Linux, @swift_windows_sdk's + # :testing and its compiled module do the same). + $file = Join-Path $patched 'swift\toolchains\swift_toolchain.bzl' + $text = [IO.File]::ReadAllText($file) + $testingSwift = "$($testing.Replace('\', '/'))/usr/lib/swift/windows" + $edits = @( + @('configurators = \[add_arg\("-sdk", sdkroot\)\],', "configurators = [add_arg(`"-sdk`", sdkroot), add_arg(`"-I$testingSwift`")],"), + @('"-LIBPATH:\{\}"\.format\(platform_lib_dir\),', "`"-LIBPATH:{}`".format(platform_lib_dir),`n `"-LIBPATH:$testingSwift/{}`".format(arch),") + ) + foreach ($edit in $edits) { + if ([regex]::Matches($text, $edit[0]).Count -ne 1) { throw "windows_build: rules_swift's swift_toolchain.bzl isn't as expected ($($edit[0]))" } + $text = [regex]::Replace($text, $edit[0], $edit[1]) + } + [IO.File]::WriteAllText($file, $text) + } + $repoEnv += '--override_repository=rules_swift+=C:/rs' $command = $Bazel[0] $rest = @($Bazel | Select-Object -Skip 1) + # swift.org's Swift for Windows is a build with assertions (Toolchains\ + # +Asserts), unlike Linux's and the Mac's, so its SIL verifiers + # run on every compile; one stopped the SwiftUI module ("SIL memory + # lifetime failure" in _GuideOffset's init), where the other platforms' + # compilers check nothing. Verification off for CI's build here, the + # finding reported apart. + if ($command -in @('build', 'test')) { + # rules_swift's own flags (Bazel 9 has no --swiftcopt). + $rest = @('--@rules_swift//swift:copt=-Xfrontend', '--@rules_swift//swift:copt=-sil-verify-none', + '--@rules_swift//swift:exec_copt=-Xfrontend', '--@rules_swift//swift:exec_copt=-sil-verify-none') + $rest + # The target's C and C++ by clang-cl, the exec tools' by MSVC + # (@isomer//tools/windows: SwiftNIO's C needs clang). + # rules_swift takes the target's triple from the cc toolchain's + # target_gnu_system_name, which rules_cc's clang-cl reports as + # aarch64-pc-windows-msvc, whereas the SDK's modules are + # aarch64-unknown-windows-msvc's (MSVC's reports "local", for which + # rules_swift writes that); CC_TARGET_TRIPLE, which it reads first, + # names the SDK's for both configurations. + $rest = @('--platforms=@isomer//tools/windows:windows_arm64_clang_cl', + '--extra_toolchains=@isomer//tools/windows:clang_cl_for_targets', + '--define=CC_TARGET_TRIPLE=aarch64-unknown-windows-msvc') + $rest + # Bazel 9's default test toolchain runs a test on an execution + # platform with all the target's constraints, and the clang-cl target + # platform is none (made one, it would take the exec tools' C too): + # tests on the host, as before Bazel 9 (examples/.bazelrc's + # windows_tests_on_linux does the same). + $rest = @('--@bazel_tools//tools/test:incompatible_use_default_test_toolchain=false') + $rest + } & $tool $command @repoEnv @rest - exit $LASTEXITCODE + $status = $LASTEXITCODE + if ($status -ne 0) { + # What Swift Testing's directory holds (a module compiled, or its + # interface alone, which a compile must rebuild). + "Swift Testing at ${testing}:" + Get-ChildItem -Recurse -Depth 3 (Join-Path $testing 'usr') -ErrorAction SilentlyContinue | + ForEach-Object { ' ' + $_.FullName.Substring($testing.Length + 1) } + # What a tool Bazel built for itself can't load, if one couldn't run + # (0xC0000135, a DLL not found): each DLL it names, found on PATH or + # not. + $bin = (& $tool info @repoEnv bazel-bin 2> $null | Select-Object -Last 1) + $execBin = $bin -replace '-fastbuild[^/\\]*', '-opt-exec' + foreach ($exe in Get-ChildItem -Recurse -Filter 'protoc-gen-swift*.exe' -Path (Join-Path $execBin 'external') -ErrorAction SilentlyContinue) { + "$($exe.FullName) loads:" + $dumpbin = Join-Path $env:VCToolsInstallDir 'bin\HostX64\arm64\dumpbin.exe' + & $dumpbin /nologo /dependents $exe.FullName | Where-Object { $_ -match '^\s+\S+\.dll\s*$' } | ForEach-Object { + $dll = $_.Trim() + $found = (Get-Command $dll -ErrorAction SilentlyContinue | Select-Object -First 1).Source + " $dll -> $(if ($found) { $found } else { 'NOT FOUND on PATH' })" + } + } + } + exit $status } finally { Pop-Location } diff --git a/tools/windows/BUILD.bazel b/tools/windows/BUILD.bazel index 5a7b74fd..a17dc015 100644 --- a/tools/windows/BUILD.bazel +++ b/tools/windows/BUILD.bazel @@ -26,3 +26,34 @@ config_setting( flag_values = {":on_linux": "true"}, visibility = ["//visibility:public"], ) + +# CI's Windows build on Windows itself (tools/ci/windows_build.ps1): the +# target's C and C++ compiled by clang-cl, as SwiftPM compiles a package's C +# on Windows (SwiftNIO's needs clang: C11 atomics, __attribute__, clang +# builtins), the exec tools by MSVC (rules_swift's tools pass clang-cl +# -Xclang=-fno-split-cold-code, which the image's LLVM rejects, and its ARM64 +# backend fails on protobuf's upb). The target platform is the host with +# rules_cc's clang-cl constraint, and this toolchain, rules_cc's clang-cl one, +# is for that target alone (rules_cc's own asks for the constraint of the +# execution platform instead, which would take the exec tools too). +platform( + name = "windows_arm64_clang_cl", + constraint_values = ["@rules_cc//cc/private/toolchain:clang-cl"], + parents = ["@platforms//host"], + visibility = ["//visibility:public"], +) + +toolchain( + name = "clang_cl_for_targets", + exec_compatible_with = ["@platforms//os:windows"], + target_compatible_with = [ + "@platforms//cpu:arm64", + "@platforms//os:windows", + "@rules_cc//cc/private/toolchain:clang-cl", + ], + # rules_cc's Windows configuration (its cc_configure extension), made + # with BAZEL_LLVM set. + toolchain = "@@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-arm64_windows-clang-cl", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", + visibility = ["//visibility:public"], +)