1# Copyright 2018 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from contextlib import contextmanager 6 7from recipe_engine import recipe_api 8 9 10class WindowsSDKApi(recipe_api.RecipeApi): 11 """API for using Windows SDK distributed via CIPD.""" 12 13 def __init__(self, sdk_properties, *args, **kwargs): 14 super(WindowsSDKApi, self).__init__(*args, **kwargs) 15 16 self._sdk_package = sdk_properties['sdk_package'] 17 self._sdk_version = sdk_properties['sdk_version'] 18 19 @contextmanager 20 def __call__(self): 21 """Setups the Windows SDK environment. 22 23 This call is a no-op on non-Windows platforms. 24 25 Raises: 26 StepFailure or InfraFailure. 27 """ 28 if not self.m.platform.is_win: 29 yield 30 return 31 32 with self.m.context(infra_steps=True): 33 sdk_dir = self._ensure_sdk() 34 35 with self.m.context(**self._sdk_env(sdk_dir)): 36 try: 37 yield 38 finally: 39 # cl.exe automatically starts background mspdbsrv.exe daemon which 40 # needs to be manually stopped so Swarming can tidy up after itself. 41 self.m.step('taskkill mspdbsrv', 42 ['taskkill.exe', '/f', '/t', '/im', 'mspdbsrv.exe']) 43 44 def _ensure_sdk(self): 45 """Ensures the Windows SDK CIPD package is installed. 46 47 Returns the directory where the SDK package has been installed. 48 49 Args: 50 path (path): Path to a directory. 51 version (str): CIPD instance ID, tag or ref. 52 """ 53 sdk_dir = self.m.path['cache'].join('windows_sdk') 54 pkgs = self.m.cipd.EnsureFile() 55 pkgs.add_package(self._sdk_package, self._sdk_version) 56 self.m.cipd.ensure(sdk_dir, pkgs) 57 return sdk_dir 58 59 def _sdk_env(self, sdk_dir): 60 """Constructs the environment for the SDK. 61 62 Returns environment and environment prefixes. 63 64 Args: 65 sdk_dir (path): Path to a directory containing the SDK. 66 """ 67 env = {} 68 env_prefixes = {} 69 70 # Load .../Windows Kits/10/bin/SetEnv.${arch}.json to extract the required 71 # environment. It contains a dict that looks like this: 72 # { 73 # "env": { 74 # "VAR": [["x"], ["y"]], 75 # ... 76 # } 77 # } 78 # All these environment variables need to be added to the environment 79 # for the compiler and linker to work. 80 filename = 'SetEnv.%s.json' % {32: 'x86', 64: 'x64'}[self.m.platform.bits] 81 step_result = self.m.json.read( 82 'read %s' % filename, 83 sdk_dir.join('Windows Kits', '10', 'bin', filename), 84 step_test_data=lambda: self.m.json.test_api.output({ 85 'env': { 86 'PATH': [['Windows Kits', '10', 'bin', '10.0.19041.0', 'x64']], 87 'VSINSTALLDIR': [['.\\']], 88 }, 89 })) 90 data = step_result.json.output.get('env') 91 for key in data: 92 results = ['%s' % sdk_dir.join(*e) for e in data[key]] 93 94 # PATH is special-cased because we don't want to overwrite other things 95 # like C:\Windows\System32. Others are replacements because prepending 96 # doesn't necessarily makes sense, like VSINSTALLDIR. 97 if key.lower() == 'path': 98 env_prefixes[key] = results 99 else: 100 env[key] = ';'.join(results) 101 102 return {'env': env, 'env_prefixes': env_prefixes} 103