| # Copyright 2020 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| """The `windws_sdk` module provides safe functions to access a semi-hermetic Windows SDK |
| installation. |
| |
| Available only to Google-run bots. |
| """ |
| |
| from contextlib import contextmanager |
| |
| from recipe_engine import recipe_api |
| |
| WINDOWS_SDK_CIPD_PACKAGE = "fuchsia_internal/third_party/sdk/windows" |
| WINDOWS_SDK_CIPD_VERSION = ( |
| "lock_hash:514ca2a9e98bd48b7233d3b31d66676c07758e2acf437583d74e29421e6711fd" |
| ) |
| |
| |
| class WindowsSDKApi(recipe_api.RecipeApi): |
| """API for using Windows SDK distributed via CIPD.""" |
| |
| def __init__(self, props, *args, **kwargs): |
| super().__init__(*args, **kwargs) |
| |
| self._sdk_dir = props.sdk_dir |
| self._cipd_package = props.cipd_package or WINDOWS_SDK_CIPD_PACKAGE |
| self._cipd_version = props.cipd_version or WINDOWS_SDK_CIPD_VERSION |
| |
| @property |
| def sdk_dir(self): |
| assert self._sdk_dir |
| return self._sdk_dir |
| |
| @contextmanager |
| def __call__(self): |
| """Setups the Windows SDK environment. |
| |
| This call is a no-op on non-Windows platforms. |
| |
| Raises: |
| StepFailure or InfraFailure. |
| """ |
| if not self.m.platform.is_win: |
| yield |
| return |
| |
| try: |
| with self.m.context(infra_steps=True): |
| if not self._sdk_dir: |
| self._sdk_dir = self.ensure_sdk() |
| with self.m.context(**self._sdk_env(self._sdk_dir)): |
| yield |
| finally: |
| if not self.m.runtime.in_global_shutdown: |
| # cl.exe automatically starts background mspdbsrv.exe daemon which |
| # needs to be manually stopped so Swarming can tidy up after itself. |
| self.m.step( |
| "taskkill mspdbsrv", |
| ["taskkill.exe", "/f", "/t", "/im", "mspdbsrv.exe"], |
| ok_ret=(0, 128), |
| ) |
| |
| def ensure_sdk(self): |
| """Ensures the Windows SDK CIPD package is installed. |
| |
| Returns the directory where the SDK package has been installed. |
| """ |
| sdk_dir = self.m.path["cache"].join("windows_sdk") |
| pkgs = self.m.cipd.EnsureFile() |
| pkgs.add_package(self._cipd_package, self._cipd_version) |
| self.m.cipd.ensure(sdk_dir, pkgs) |
| return sdk_dir |
| |
| def _sdk_env(self, sdk_dir): |
| """Constructs the environment for the SDK. |
| |
| Returns environment and environment prefixes. |
| |
| Args: |
| sdk_dir (path): Path to a directory containing the SDK. |
| """ |
| env = {} |
| env_prefixes = {} |
| |
| # Load .../win_sdk/bin/SetEnv.${arch}.json to extract the required |
| # environment. It contains a dict that looks like this: |
| # { |
| # "env": { |
| # "VAR": [["..", "..", "x"], ["..", "..", "y"]], |
| # ... |
| # } |
| # } |
| # All these environment variables need to be added to the environment |
| # for the compiler and linker to work. |
| filename = "SetEnv.%s.json" % {32: "x86", 64: "x64"}[self.m.platform.bits] |
| data = self.m.file.read_json( |
| f"read {filename}", |
| sdk_dir.join("Windows Kits", "10", "bin", filename), |
| test_data={ |
| "env": { |
| "PATH": [["Windows Kits", "10", "bin", "x64"]], |
| "VSINSTALLDIR": [[".\\"]], |
| }, |
| }, |
| ).get("env") |
| for key in data: |
| results = [] |
| for value in data[key]: |
| results.append(f"{sdk_dir.join(*value)}") |
| |
| # PATH is special-cased because we don't want to overwrite other things |
| # like C:\Windows\System32. Others are replacements because prepending |
| # doesn't necessarily makes sense, like VSINSTALLDIR. |
| if key.lower() == "path": |
| env_prefixes[key] = results |
| else: |
| env[key] = ";".join(results) |
| |
| return {"env": env, "env_prefixes": env_prefixes} |