Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 # Copyright (c) 2012 Google Inc. All rights reserved. | 3 # Copyright (c) 2012 Google Inc. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Utility functions for Windows builds. | 7 """Utility functions for Windows builds. |
| 8 | 8 |
| 9 These functions are executed via gyp-win-tool when using the ninja generator. | 9 These functions are executed via gyp-win-tool when using the ninja generator. |
| 10 """ | 10 """ |
| 11 | 11 |
| 12 import os | 12 import os |
| 13 import re | |
| 13 import shutil | 14 import shutil |
| 14 import subprocess | 15 import subprocess |
| 15 import sys | 16 import sys |
| 16 | 17 |
| 17 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | 18 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 18 | 19 |
| 20 # A regex matching an argument corresponding to a PDB filename passed as an | |
| 21 # argument to link.exe. | |
| 22 _LINK_EXE_PDB_ARG = re.compile('/PDB:(?P<pdb>.+\.exe\.pdb)', re.IGNORECASE) | |
| 23 _MSPDBSRV_ENDPOINT_ENV_VAR = '_MSPDBSRV_ENDPOINT_' | |
| 19 | 24 |
| 20 def main(args): | 25 def main(args): |
| 21 executor = WinTool() | 26 executor = WinTool() |
| 22 exit_code = executor.Dispatch(args) | 27 exit_code = executor.Dispatch(args) |
| 23 if exit_code is not None: | 28 if exit_code is not None: |
| 24 sys.exit(exit_code) | 29 sys.exit(exit_code) |
| 25 | 30 |
| 26 | 31 |
| 27 class WinTool(object): | 32 class WinTool(object): |
| 28 """This class performs all the Windows tooling steps. The methods can either | 33 """This class performs all the Windows tooling steps. The methods can either |
| 29 be executed directly, or dispatched from an argument list.""" | 34 be executed directly, or dispatched from an argument list.""" |
| 30 | 35 |
| 36 class MsPdbSrvHelper(object): | |
| 37 """This is an helper class to use a unique instance of mspdbsrv.exe for the | |
| 38 linkers linking an .exe target. This helps to reduce the memory pressure one | |
| 39 the shared instance of mspdbsrv.exe.""" | |
| 40 | |
| 41 def __init__(self, env, args): | |
| 42 self.env = env | |
| 43 self.args = args | |
| 44 | |
| 45 def __enter__(self): | |
| 46 self._MaybeStartMsPdbSrv() | |
| 47 | |
| 48 def __exit__(self, type, value, traceback): | |
| 49 if _MSPDBSRV_ENDPOINT_ENV_VAR in self.env: | |
| 50 self._StopMsPdbSrv() | |
| 51 | |
| 52 def _MaybeStartMsPdbSrv(self): | |
| 53 """Starts an unique instance of mspdbsrv.exe if the given arguments are | |
| 54 those of a linker linking a .exe target.""" | |
| 55 if not os.environ.get('GYP_USE_SEPARATE_MSPDBSRV'): | |
| 56 return | |
| 57 | |
| 58 if len(self.args) < 1: | |
| 59 raise Exception("Not enough arguments") | |
| 60 | |
| 61 if self.args[0] != 'link.exe': | |
| 62 return | |
| 63 | |
| 64 # Checks if this linker produces a PDB for an .exe target. If so use the | |
| 65 # name of this PDB to generate an endpoint name for mspdbsrv.exe. | |
| 66 endpoint_name = None | |
| 67 for arg in self.args: | |
| 68 m = _LINK_EXE_PDB_ARG.match(arg) | |
| 69 if m: | |
| 70 endpoint_name = '%s_%d' % (m.group('pdb'), os.getpid()) | |
| 71 break | |
| 72 | |
| 73 if endpoint_name is None: | |
| 74 return | |
| 75 | |
| 76 # Adds the appropriate environment variable. This will be read by link.exe | |
| 77 # to know which instance of mspdbsrv.exe it should connect to (if it's | |
| 78 # not set then the default endpoint is used). | |
| 79 self.env[_MSPDBSRV_ENDPOINT_ENV_VAR] = endpoint_name | |
| 80 | |
| 81 # Starts the mspdbsrv.exe instance manually. We could specify the endpoint | |
| 82 # in the command line but it's redundant with setting it in the | |
| 83 # environment block. Starting this process manually allows us to wait for | |
| 84 # its completion once the link step is finished. This is required to | |
| 85 # respect the dependencies between the different targets. | |
| 86 mspdbsrv_args = ('mspdbsrv.exe', '-start', '-spawn') | |
| 87 subprocess.Popen(mspdbsrv_args, shell=True, env=self.env) | |
| 88 | |
| 89 def _StopMsPdbSrv(self): | |
| 90 """Stop the instance of mspdbsrv.exe that has been started by this | |
| 91 helper.""" | |
| 92 mspdbsrv_stop_args = ('mspdbsrv.exe', '-stop') | |
| 93 mspdbsrv_stop_process = subprocess.call(mspdbsrv_stop_args, | |
| 94 shell=True, | |
| 95 env=self.env) | |
| 96 | |
| 31 def Dispatch(self, args): | 97 def Dispatch(self, args): |
| 32 """Dispatches a string command to a method.""" | 98 """Dispatches a string command to a method.""" |
| 33 if len(args) < 1: | 99 if len(args) < 1: |
| 34 raise Exception("Not enough arguments") | 100 raise Exception("Not enough arguments") |
| 35 | 101 |
| 36 method = "Exec%s" % self._CommandifyName(args[0]) | 102 method = "Exec%s" % self._CommandifyName(args[0]) |
| 37 return getattr(self, method)(*args[1:]) | 103 return getattr(self, method)(*args[1:]) |
| 38 | 104 |
| 39 def _CommandifyName(self, name_string): | 105 def _CommandifyName(self, name_string): |
| 40 """Transforms a tool name like recursive-mirror to RecursiveMirror.""" | 106 """Transforms a tool name like recursive-mirror to RecursiveMirror.""" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 64 shutil.copytree(source, dest) | 130 shutil.copytree(source, dest) |
| 65 else: | 131 else: |
| 66 shutil.copy2(source, dest) | 132 shutil.copy2(source, dest) |
| 67 | 133 |
| 68 def ExecLinkWrapper(self, arch, *args): | 134 def ExecLinkWrapper(self, arch, *args): |
| 69 """Filter diagnostic output from link that looks like: | 135 """Filter diagnostic output from link that looks like: |
| 70 ' Creating library ui.dll.lib and object ui.dll.exp' | 136 ' Creating library ui.dll.lib and object ui.dll.exp' |
| 71 This happens when there are exports from the dll or exe. | 137 This happens when there are exports from the dll or exe. |
| 72 """ | 138 """ |
| 73 env = self._GetEnv(arch) | 139 env = self._GetEnv(arch) |
| 74 popen = subprocess.Popen(args, shell=True, env=env, | 140 with WinTool.MsPdbSrvHelper(env, args): |
| 75 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 141 link_wrapper = subprocess.Popen(args, |
|
scottmg
2013/11/27 20:00:44
if you want to change this, it shouldn't be "link_
Sébastien Marchand
2013/11/27 20:29:55
Done.
| |
| 76 out, _ = popen.communicate() | 142 shell=True, |
| 77 for line in out.splitlines(): | 143 env=env, |
| 78 if not line.startswith(' Creating library '): | 144 stdout=subprocess.PIPE, |
| 79 print line | 145 stderr=subprocess.STDOUT) |
| 80 return popen.returncode | 146 out, _ = link_wrapper.communicate() |
| 147 for line in out.splitlines(): | |
| 148 if not line.startswith(' Creating library '): | |
| 149 print line | |
| 150 return link_wrapper.returncode | |
| 81 | 151 |
| 82 def ExecManifestWrapper(self, arch, *args): | 152 def ExecManifestWrapper(self, arch, *args): |
| 83 """Run manifest tool with environment set. Strip out undesirable warning | 153 """Run manifest tool with environment set. Strip out undesirable warning |
| 84 (some XML blocks are recognized by the OS loader, but not the manifest | 154 (some XML blocks are recognized by the OS loader, but not the manifest |
| 85 tool).""" | 155 tool).""" |
| 86 env = self._GetEnv(arch) | 156 env = self._GetEnv(arch) |
| 87 popen = subprocess.Popen(args, shell=True, env=env, | 157 popen = subprocess.Popen(args, shell=True, env=env, |
| 88 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 158 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 89 out, _ = popen.communicate() | 159 out, _ = popen.communicate() |
| 90 for line in out.splitlines(): | 160 for line in out.splitlines(): |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 line): | 231 line): |
| 162 print line | 232 print line |
| 163 return popen.returncode | 233 return popen.returncode |
| 164 | 234 |
| 165 def ExecActionWrapper(self, arch, rspfile, *dir): | 235 def ExecActionWrapper(self, arch, rspfile, *dir): |
| 166 """Runs an action command line from a response file using the environment | 236 """Runs an action command line from a response file using the environment |
| 167 for |arch|. If |dir| is supplied, use that as the working directory.""" | 237 for |arch|. If |dir| is supplied, use that as the working directory.""" |
| 168 env = self._GetEnv(arch) | 238 env = self._GetEnv(arch) |
| 169 args = open(rspfile).read() | 239 args = open(rspfile).read() |
| 170 dir = dir[0] if dir else None | 240 dir = dir[0] if dir else None |
| 171 popen = subprocess.Popen(args, shell=True, env=env, cwd=dir) | 241 return subprocess.call(args, shell=True, env=env, cwd=dir) |
| 172 popen.wait() | |
| 173 return popen.returncode | |
| 174 | 242 |
| 175 if __name__ == '__main__': | 243 if __name__ == '__main__': |
| 176 sys.exit(main(sys.argv[1:])) | 244 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |