|
| 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 | +This file is copied to the build directory as part of toolchain setup and |
| 7 | +is used to set up calls to tools used by the build that need wrappers. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import print_function |
| 11 | + |
| 12 | +import os |
| 13 | +import re |
| 14 | +import shutil |
| 15 | +import subprocess |
| 16 | +import stat |
| 17 | +import sys |
| 18 | + |
| 19 | + |
| 20 | +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 21 | + |
| 22 | +# A regex matching an argument corresponding to the output filename passed to |
| 23 | +# link.exe. |
| 24 | +_LINK_EXE_OUT_ARG = re.compile('/OUT:(?P<out>.+)$', re.IGNORECASE) |
| 25 | + |
| 26 | +def main(args): |
| 27 | + exit_code = WinTool().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 ExecDeleteFile(self, path): |
| 85 | + """Simple file delete command.""" |
| 86 | + if os.path.exists(path): |
| 87 | + os.unlink(path) |
| 88 | + |
| 89 | + def ExecRecursiveMirror(self, source, dest): |
| 90 | + """Emulation of rm -rf out && cp -af in out.""" |
| 91 | + if os.path.exists(dest): |
| 92 | + if os.path.isdir(dest): |
| 93 | + def _on_error(fn, path, dummy_excinfo): |
| 94 | + # The operation failed, possibly because the file is set to |
| 95 | + # read-only. If that's why, make it writable and try the op again. |
| 96 | + if not os.access(path, os.W_OK): |
| 97 | + os.chmod(path, stat.S_IWRITE) |
| 98 | + fn(path) |
| 99 | + shutil.rmtree(dest, onerror=_on_error) |
| 100 | + else: |
| 101 | + if not os.access(dest, os.W_OK): |
| 102 | + # Attempt to make the file writable before deleting it. |
| 103 | + os.chmod(dest, stat.S_IWRITE) |
| 104 | + os.unlink(dest) |
| 105 | + |
| 106 | + if os.path.isdir(source): |
| 107 | + shutil.copytree(source, dest) |
| 108 | + else: |
| 109 | + shutil.copy2(source, dest) |
| 110 | + # Try to diagnose crbug.com/741603 |
| 111 | + if not os.path.exists(dest): |
| 112 | + raise Exception("Copying of %s to %s failed" % (source, dest)) |
| 113 | + |
| 114 | + def ExecLinkWrapper(self, arch, use_separate_mspdbsrv, *args): |
| 115 | + """Filter diagnostic output from link that looks like: |
| 116 | + ' Creating library ui.dll.lib and object ui.dll.exp' |
| 117 | + This happens when there are exports from the dll or exe. |
| 118 | + """ |
| 119 | + env = self._GetEnv(arch) |
| 120 | + if use_separate_mspdbsrv == 'True': |
| 121 | + self._UseSeparateMspdbsrv(env, args) |
| 122 | + if sys.platform == 'win32': |
| 123 | + args = list(args) # *args is a tuple by default, which is read-only. |
| 124 | + args[0] = args[0].replace('/', '\\') |
| 125 | + # https://docs.python.org/2/library/subprocess.html: |
| 126 | + # "On Unix with shell=True [...] if args is a sequence, the first item |
| 127 | + # specifies the command string, and any additional items will be treated as |
| 128 | + # additional arguments to the shell itself. That is to say, Popen does the |
| 129 | + # equivalent of: |
| 130 | + # Popen(['/bin/sh', '-c', args[0], args[1], ...])" |
| 131 | + # For that reason, since going through the shell doesn't seem necessary on |
| 132 | + # non-Windows don't do that there. |
| 133 | + pe_name = None |
| 134 | + for arg in args: |
| 135 | + m = _LINK_EXE_OUT_ARG.match(arg) |
| 136 | + if m: |
| 137 | + pe_name = m.group('out') |
| 138 | + link = subprocess.Popen(args, shell=sys.platform == 'win32', env=env, |
| 139 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 140 | + # Read output one line at a time as it shows up to avoid OOM failures when |
| 141 | + # GBs of output is produced. |
| 142 | + for line in link.stdout: |
| 143 | + if (not line.startswith(b' Creating library ') |
| 144 | + and not line.startswith(b'Generating code') |
| 145 | + and not line.startswith(b'Finished generating code')): |
| 146 | + print(line) |
| 147 | + return link.wait() |
| 148 | + |
| 149 | + def ExecAsmWrapper(self, arch, *args): |
| 150 | + """Filter logo banner from invocations of asm.exe.""" |
| 151 | + env = self._GetEnv(arch) |
| 152 | + if sys.platform == 'win32': |
| 153 | + # Windows ARM64 uses clang-cl as assembler which has '/' as path |
| 154 | + # separator, convert it to '\\' when running on Windows. |
| 155 | + args = list(args) # *args is a tuple by default, which is read-only |
| 156 | + args[0] = args[0].replace('/', '\\') |
| 157 | + popen = subprocess.Popen(args, shell=True, env=env, |
| 158 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 159 | + out, _ = popen.communicate() |
| 160 | + for line in out.decode('utf8').splitlines(): |
| 161 | + if not line.startswith(' Assembling: '): |
| 162 | + print(line) |
| 163 | + return popen.returncode |
| 164 | + |
| 165 | + def ExecRcWrapper(self, arch, *args): |
| 166 | + """Filter logo banner from invocations of rc.exe. Older versions of RC |
| 167 | + don't support the /nologo flag.""" |
| 168 | + env = self._GetEnv(arch) |
| 169 | + popen = subprocess.Popen(args, shell=True, env=env, |
| 170 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 171 | + out, _ = popen.communicate() |
| 172 | + for line in out.splitlines(): |
| 173 | + if (not line.startswith(b'Microsoft (R) Windows (R) Resource Compiler') and |
| 174 | + not line.startswith(b'Copyright (C) Microsoft Corporation') and line): |
| 175 | + print(line) |
| 176 | + return popen.returncode |
| 177 | + |
| 178 | + def ExecActionWrapper(self, arch, rspfile, *dirname): |
| 179 | + """Runs an action command line from a response file using the environment |
| 180 | + for |arch|. If |dirname| is supplied, use that as the working directory.""" |
| 181 | + env = self._GetEnv(arch) |
| 182 | + # TODO(scottmg): This is a temporary hack to get some specific variables |
| 183 | + # through to actions that are set after GN-time. http://crbug.com/333738. |
| 184 | + for k, v in os.environ.items(): |
| 185 | + if k not in env: |
| 186 | + env[k] = v |
| 187 | + args = open(rspfile).read() |
| 188 | + dirname = dirname[0] if dirname else None |
| 189 | + return subprocess.call(args, shell=True, env=env, cwd=dirname) |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == '__main__': |
| 193 | + sys.exit(main(sys.argv[1:])) |
0 commit comments