OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 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 |
| 5 """Utility functions for Windows builds. |
| 6 |
| 7 This file is copied to the build directory as part of toolchain setup and |
| 8 is used to set up calls to tools used by the build that need wrappers. |
| 9 """ |
| 10 |
| 11 import os |
| 12 import re |
| 13 import shutil |
| 14 import subprocess |
| 15 import stat |
| 16 import string |
| 17 import sys |
| 18 |
| 19 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 20 |
| 21 # A regex matching an argument corresponding to the output filename passed to |
| 22 # link.exe. |
| 23 _LINK_EXE_OUT_ARG = re.compile('/OUT:(?P<out>.+)$', re.IGNORECASE) |
| 24 |
| 25 def main(args): |
| 26 executor = WinTool() |
| 27 exit_code = executor.Dispatch(args) |
| 28 if exit_code is not None: |
| 29 sys.exit(exit_code) |
| 30 |
| 31 |
| 32 class WinTool(object): |
| 33 """This class performs all the Windows tooling steps. The methods can either |
| 34 be executed directly, or dispatched from an argument list.""" |
| 35 |
| 36 def _UseSeparateMspdbsrv(self, env, args): |
| 37 """Allows to use a unique instance of mspdbsrv.exe per linker instead of a |
| 38 shared one.""" |
| 39 if len(args) < 1: |
| 40 raise Exception("Not enough arguments") |
| 41 |
| 42 if args[0] != 'link.exe': |
| 43 return |
| 44 |
| 45 # Use the output filename passed to the linker to generate an endpoint name |
| 46 # for mspdbsrv.exe. |
| 47 endpoint_name = None |
| 48 for arg in args: |
| 49 m = _LINK_EXE_OUT_ARG.match(arg) |
| 50 if m: |
| 51 endpoint_name = re.sub(r'\W+', '', |
| 52 '%s_%d' % (m.group('out'), os.getpid())) |
| 53 break |
| 54 |
| 55 if endpoint_name is None: |
| 56 return |
| 57 |
| 58 # Adds the appropriate environment variable. This will be read by link.exe |
| 59 # to know which instance of mspdbsrv.exe it should connect to (if it's |
| 60 # not set then the default endpoint is used). |
| 61 env['_MSPDBSRV_ENDPOINT_'] = endpoint_name |
| 62 |
| 63 def Dispatch(self, args): |
| 64 """Dispatches a string command to a method.""" |
| 65 if len(args) < 1: |
| 66 raise Exception("Not enough arguments") |
| 67 |
| 68 method = "Exec%s" % self._CommandifyName(args[0]) |
| 69 return getattr(self, method)(*args[1:]) |
| 70 |
| 71 def _CommandifyName(self, name_string): |
| 72 """Transforms a tool name like recursive-mirror to RecursiveMirror.""" |
| 73 return name_string.title().replace('-', '') |
| 74 |
| 75 def _GetEnv(self, arch): |
| 76 """Gets the saved environment from a file for a given architecture.""" |
| 77 # The environment is saved as an "environment block" (see CreateProcess |
| 78 # and msvs_emulation for details). We convert to a dict here. |
| 79 # Drop last 2 NULs, one for list terminator, one for trailing vs. separator. |
| 80 pairs = open(arch).read()[:-2].split('\0') |
| 81 kvs = [item.split('=', 1) for item in pairs] |
| 82 return dict(kvs) |
| 83 |
| 84 def ExecStamp(self, path): |
| 85 """Simple stamp command.""" |
| 86 open(path, 'w').close() |
| 87 |
| 88 def ExecDeleteFile(self, path): |
| 89 """Simple file delete command.""" |
| 90 if os.path.exists(path): |
| 91 os.unlink(path) |
| 92 |
| 93 def ExecRecursiveMirror(self, source, dest): |
| 94 """Emulation of rm -rf out && cp -af in out.""" |
| 95 if os.path.exists(dest): |
| 96 if os.path.isdir(dest): |
| 97 def _on_error(fn, path, dummy_excinfo): |
| 98 # The operation failed, possibly because the file is set to |
| 99 # read-only. If that's why, make it writable and try the op again. |
| 100 if not os.access(path, os.W_OK): |
| 101 os.chmod(path, stat.S_IWRITE) |
| 102 fn(path) |
| 103 shutil.rmtree(dest, onerror=_on_error) |
| 104 else: |
| 105 if not os.access(dest, os.W_OK): |
| 106 # Attempt to make the file writable before deleting it. |
| 107 os.chmod(dest, stat.S_IWRITE) |
| 108 os.unlink(dest) |
| 109 |
| 110 if os.path.isdir(source): |
| 111 shutil.copytree(source, dest) |
| 112 else: |
| 113 shutil.copy2(source, dest) |
| 114 # Try to diagnose crbug.com/741603 |
| 115 if not os.path.exists(dest): |
| 116 raise Exception("Copying of %s to %s failed" % (source, dest)) |
| 117 |
| 118 def ExecLinkWrapper(self, arch, use_separate_mspdbsrv, *args): |
| 119 """Filter diagnostic output from link that looks like: |
| 120 ' Creating library ui.dll.lib and object ui.dll.exp' |
| 121 This happens when there are exports from the dll or exe. |
| 122 """ |
| 123 env = self._GetEnv(arch) |
| 124 if use_separate_mspdbsrv == 'True': |
| 125 self._UseSeparateMspdbsrv(env, args) |
| 126 if sys.platform == 'win32': |
| 127 args = list(args) # *args is a tuple by default, which is read-only. |
| 128 args[0] = args[0].replace('/', '\\') |
| 129 # https://docs.python.org/2/library/subprocess.html: |
| 130 # "On Unix with shell=True [...] if args is a sequence, the first item |
| 131 # specifies the command string, and any additional items will be treated as |
| 132 # additional arguments to the shell itself. That is to say, Popen does the |
| 133 # equivalent of: |
| 134 # Popen(['/bin/sh', '-c', args[0], args[1], ...])" |
| 135 # For that reason, since going through the shell doesn't seem necessary on |
| 136 # non-Windows don't do that there. |
| 137 link = subprocess.Popen(args, shell=sys.platform == 'win32', env=env, |
| 138 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 139 # Read output one line at a time as it shows up to avoid OOM failures when |
| 140 # GBs of output is produced. |
| 141 for line in link.stdout: |
| 142 if (not line.startswith(' Creating library ') and |
| 143 not line.startswith('Generating code') and |
| 144 not line.startswith('Finished generating code')): |
| 145 print line, |
| 146 return link.wait() |
| 147 |
| 148 def ExecMidlWrapper(self, arch, outdir, tlb, h, dlldata, iid, proxy, idl, |
| 149 *flags): |
| 150 """Filter noisy filenames output from MIDL compile step that isn't |
| 151 quietable via command line flags. |
| 152 """ |
| 153 args = ['midl', '/nologo'] + list(flags) + [ |
| 154 '/out', outdir, |
| 155 '/tlb', tlb, |
| 156 '/h', h, |
| 157 '/dlldata', dlldata, |
| 158 '/iid', iid, |
| 159 '/proxy', proxy, |
| 160 idl] |
| 161 env = self._GetEnv(arch) |
| 162 popen = subprocess.Popen(args, shell=True, env=env, |
| 163 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 164 out, _ = popen.communicate() |
| 165 # Filter junk out of stdout, and write filtered versions. Output we want |
| 166 # to filter is pairs of lines that look like this: |
| 167 # Processing C:\Program Files (x86)\Microsoft SDKs\...\include\objidl.idl |
| 168 # objidl.idl |
| 169 lines = out.splitlines() |
| 170 prefixes = ('Processing ', '64 bit Processing ') |
| 171 processing = set(os.path.basename(x) |
| 172 for x in lines if x.startswith(prefixes)) |
| 173 for line in lines: |
| 174 if not line.startswith(prefixes) and line not in processing: |
| 175 print line |
| 176 return popen.returncode |
| 177 |
| 178 def ExecAsmWrapper(self, arch, *args): |
| 179 """Filter logo banner from invocations of asm.exe.""" |
| 180 env = self._GetEnv(arch) |
| 181 popen = subprocess.Popen(args, shell=True, env=env, |
| 182 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 183 out, _ = popen.communicate() |
| 184 for line in out.splitlines(): |
| 185 # Split to avoid triggering license checks: |
| 186 if (not line.startswith('Copy' + 'right (C' + |
| 187 ') Microsoft Corporation') and |
| 188 not line.startswith('Microsoft (R) Macro Assembler') and |
| 189 not line.startswith(' Assembling: ') and |
| 190 line): |
| 191 print line |
| 192 return popen.returncode |
| 193 |
| 194 def ExecRcWrapper(self, arch, *args): |
| 195 """Filter logo banner from invocations of rc.exe. Older versions of RC |
| 196 don't support the /nologo flag.""" |
| 197 env = self._GetEnv(arch) |
| 198 popen = subprocess.Popen(args, shell=True, env=env, |
| 199 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 200 out, _ = popen.communicate() |
| 201 for line in out.splitlines(): |
| 202 if (not line.startswith('Microsoft (R) Windows (R) Resource Compiler') and |
| 203 not line.startswith('Copy' + 'right (C' + |
| 204 ') Microsoft Corporation') and |
| 205 line): |
| 206 print line |
| 207 return popen.returncode |
| 208 |
| 209 def ExecActionWrapper(self, arch, rspfile, *dirname): |
| 210 """Runs an action command line from a response file using the environment |
| 211 for |arch|. If |dirname| is supplied, use that as the working directory.""" |
| 212 env = self._GetEnv(arch) |
| 213 # TODO(scottmg): This is a temporary hack to get some specific variables |
| 214 # through to actions that are set after GN-time. http://crbug.com/333738. |
| 215 for k, v in os.environ.iteritems(): |
| 216 if k not in env: |
| 217 env[k] = v |
| 218 args = open(rspfile).read() |
| 219 dirname = dirname[0] if dirname else None |
| 220 return subprocess.call(args, shell=True, env=env, cwd=dirname) |
| 221 |
| 222 |
| 223 if __name__ == '__main__': |
| 224 sys.exit(main(sys.argv[1:])) |
OLD | NEW |