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 try: 33 with self.m.context(infra_steps=True): 34 sdk_dir = self._ensure_sdk() 35 with self.m.context(**self._sdk_env(sdk_dir)): 36 yield 37 finally: 38 # cl.exe automatically starts background mspdbsrv.exe daemon which 39 # needs to be manually stopped so Swarming can tidy up after itself. 40 self.m.step('taskkill mspdbsrv', 41 ['taskkill.exe', '/f', '/t', '/im', 'mspdbsrv.exe']) 42 43 def _ensure_sdk(self): 44 """Ensures the Windows SDK CIPD package is installed. 45 46 Returns the directory where the SDK package has been installed. 47 48 Args: 49 path (path): Path to a directory. 50 version (str): CIPD instance ID, tag or ref. 51 """ 52 sdk_dir = self.m.path['cache'].join('windows_sdk') 53 pkgs = self.m.cipd.EnsureFile() 54 pkgs.add_package(self._sdk_package, self._sdk_version) 55 self.m.cipd.ensure(sdk_dir, pkgs) 56 return sdk_dir 57 58 def _sdk_env(self, sdk_dir): 59 """Constructs the environment for the SDK. 60 61 Returns environment and environment prefixes. 62 63 Args: 64 sdk_dir (path): Path to a directory containing the SDK. 65 """ 66 env = {} 67 env_prefixes = {} 68 69 # Load .../win_sdk/bin/SetEnv.${arch}.json to extract the required 70 # environment. It contains a dict that looks like this: 71 # { 72 # "env": { 73 # "VAR": [["..", "..", "x"], ["..", "..", "y"]], 74 # ... 75 # } 76 # } 77 # All these environment variables need to be added to the environment 78 # for the compiler and linker to work. 79 filename = 'SetEnv.%s.json' % {32: 'x86', 64: 'x64'}[self.m.platform.bits] 80 step_result = self.m.json.read( 81 'read %s' % filename, 82 sdk_dir.join('win_sdk', 'bin', filename), 83 step_test_data=lambda: self.m.json.test_api.output({ 84 'env': { 85 'PATH': [['..', '..', 'win_sdk', 'bin', 'x64']], 86 'VSINSTALLDIR': [['..', '..\\']],},})) 87 data = step_result.json.output.get('env') 88 for key in data: 89 # recipes' Path() does not like .., ., \, or /, so this is cumbersome. 90 # What we want to do is: 91 # [sdk_bin_dir.join(*e) for e in env[k]] 92 # Instead do that badly, and rely (but verify) on the fact that the paths 93 # are all specified relative to the root, but specified relative to 94 # win_sdk/bin (i.e. everything starts with "../../".) 95 results = [] 96 for value in data[key]: 97 assert value[0] == '..' and (value[1] == '..' or value[1] == '..\\') 98 results.append('%s' % sdk_dir.join(*value[2:])) 99 100 # PATH is special-cased because we don't want to overwrite other things 101 # like C:\Windows\System32. Others are replacements because prepending 102 # doesn't necessarily makes sense, like VSINSTALLDIR. 103 if key.lower() == 'path': 104 env_prefixes[key] = results 105 else: 106 env[key] = ';'.join(results) 107 108 return {'env': env, 'env_prefixes': env_prefixes} 109