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 ExecRecursiveMirror(self, source, dest): | |
89 """Emulation of rm -rf out && cp -af in out.""" | |
90 if os.path.exists(dest): | |
91 if os.path.isdir(dest): | |
92 def _on_error(fn, path, excinfo): | |
93 # The operation failed, possibly because the file is set to | |
94 # read-only. If that's why, make it writable and try the op again. | |
95 if not os.access(path, os.W_OK): | |
96 os.chmod(path, stat.S_IWRITE) | |
97 fn(path) | |
98 shutil.rmtree(dest, onerror=_on_error) | |
99 else: | |
100 if not os.access(dest, os.W_OK): | |
101 # Attempt to make the file writable before deleting it. | |
102 os.chmod(dest, stat.S_IWRITE) | |
103 os.unlink(dest) | |
104 | |
105 if os.path.isdir(source): | |
106 shutil.copytree(source, dest) | |
107 else: | |
108 shutil.copy2(source, dest) | |
109 | |
110 def ExecLinkWrapper(self, arch, use_separate_mspdbsrv, *args): | |
111 """Filter diagnostic output from link that looks like: | |
112 ' Creating library ui.dll.lib and object ui.dll.exp' | |
113 This happens when there are exports from the dll or exe. | |
114 """ | |
115 env = self._GetEnv(arch) | |
116 if use_separate_mspdbsrv == 'True': | |
117 self._UseSeparateMspdbsrv(env, args) | |
118 if sys.platform == 'win32': | |
119 args = list(args) # *args is a tuple by default, which is read-only. | |
120 args[0] = args[0].replace('/', '\\') | |
121 # https://docs.python.org/2/library/subprocess.html: | |
122 # "On Unix with shell=True [...] if args is a sequence, the first item | |
123 # specifies the command string, and any additional items will be treated as | |
124 # additional arguments to the shell itself. That is to say, Popen does the | |
125 # equivalent of: | |
126 # Popen(['/bin/sh', '-c', args[0], args[1], ...])" | |
127 # For that reason, since going through the shell doesn't seem necessary on | |
128 # non-Windows don't do that there. | |
129 link = subprocess.Popen(args, shell=sys.platform == 'win32', env=env, | |
130 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
131 out, _ = link.communicate() | |
132 for line in out.splitlines(): | |
133 if (not line.startswith(' Creating library ') and | |
134 not line.startswith('Generating code') and | |
135 not line.startswith('Finished generating code')): | |
136 print line | |
137 return link.returncode | |
138 | |
139 def ExecLinkWithManifests(self, arch, embed_manifest, out, ldcmd, resname, | |
140 mt, rc, intermediate_manifest, *manifests): | |
141 """A wrapper for handling creating a manifest resource and then executing | |
142 a link command.""" | |
143 # The 'normal' way to do manifests is to have link generate a manifest | |
144 # based on gathering dependencies from the object files, then merge that | |
145 # manifest with other manifests supplied as sources, convert the merged | |
146 # manifest to a resource, and then *relink*, including the compiled | |
147 # version of the manifest resource. This breaks incremental linking, and | |
148 # is generally overly complicated. Instead, we merge all the manifests | |
149 # provided (along with one that includes what would normally be in the | |
150 # linker-generated one, see msvs_emulation.py), and include that into the | |
151 # first and only link. We still tell link to generate a manifest, but we | |
152 # only use that to assert that our simpler process did not miss anything. | |
153 variables = { | |
154 'python': sys.executable, | |
155 'arch': arch, | |
156 'out': out, | |
157 'ldcmd': ldcmd, | |
158 'resname': resname, | |
159 'mt': mt, | |
160 'rc': rc, | |
161 'intermediate_manifest': intermediate_manifest, | |
162 'manifests': ' '.join(manifests), | |
163 } | |
164 add_to_ld = '' | |
165 if manifests: | |
166 subprocess.check_call( | |
167 '%(python)s tool_wrapper.py manifest-wrapper %(arch)s %(mt)s -nologo ' | |
168 '-manifest %(manifests)s -out:%(out)s.manifest' % variables) | |
169 if embed_manifest == 'True': | |
170 subprocess.check_call( | |
171 '%(python)s tool_wrapper.py manifest-to-rc %(arch)s' | |
172 '%(out)s.manifest %(out)s.manifest.rc %(resname)s' % variables) | |
173 subprocess.check_call( | |
174 '%(python)s tool_wrapper.py rc-wrapper %(arch)s %(rc)s ' | |
175 '%(out)s.manifest.rc' % variables) | |
176 add_to_ld = ' %(out)s.manifest.res' % variables | |
177 subprocess.check_call(ldcmd + add_to_ld) | |
178 | |
179 # Run mt.exe on the theoretically complete manifest we generated, merging | |
180 # it with the one the linker generated to confirm that the linker | |
181 # generated one does not add anything. This is strictly unnecessary for | |
182 # correctness, it's only to verify that e.g. /MANIFESTDEPENDENCY was not | |
183 # used in a #pragma comment. | |
184 if manifests: | |
185 # Merge the intermediate one with ours to .assert.manifest, then check | |
186 # that .assert.manifest is identical to ours. | |
187 subprocess.check_call( | |
188 '%(python)s tool_wrapper.py manifest-wrapper %(arch)s %(mt)s -nologo ' | |
189 '-manifest %(out)s.manifest %(intermediate_manifest)s ' | |
190 '-out:%(out)s.assert.manifest' % variables) | |
191 assert_manifest = '%(out)s.assert.manifest' % variables | |
192 our_manifest = '%(out)s.manifest' % variables | |
193 # Load and normalize the manifests. mt.exe sometimes removes whitespace, | |
194 # and sometimes doesn't unfortunately. | |
195 with open(our_manifest, 'rb') as our_f: | |
196 with open(assert_manifest, 'rb') as assert_f: | |
197 our_data = our_f.read().translate(None, string.whitespace) | |
198 assert_data = assert_f.read().translate(None, string.whitespace) | |
199 if our_data != assert_data: | |
200 os.unlink(out) | |
201 def dump(filename): | |
202 sys.stderr.write('%s\n-----\n' % filename) | |
203 with open(filename, 'rb') as f: | |
204 sys.stderr.write(f.read() + '\n-----\n') | |
205 dump(intermediate_manifest) | |
206 dump(our_manifest) | |
207 dump(assert_manifest) | |
208 sys.stderr.write( | |
209 'Linker generated manifest "%s" added to final manifest "%s" ' | |
210 '(result in "%s"). ' | |
211 'Were /MANIFEST switches used in #pragma statements? ' % ( | |
212 intermediate_manifest, our_manifest, assert_manifest)) | |
213 return 1 | |
214 | |
215 def ExecManifestWrapper(self, arch, *args): | |
216 """Run manifest tool with environment set. Strip out undesirable warning | |
217 (some XML blocks are recognized by the OS loader, but not the manifest | |
218 tool).""" | |
219 env = self._GetEnv(arch) | |
220 popen = subprocess.Popen(args, shell=True, env=env, | |
221 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
222 out, _ = popen.communicate() | |
223 for line in out.splitlines(): | |
224 if line and 'manifest authoring warning 81010002' not in line: | |
225 print line | |
226 return popen.returncode | |
227 | |
228 def ExecManifestToRc(self, arch, *args): | |
229 """Creates a resource file pointing a SxS assembly manifest. | |
230 |args| is tuple containing path to resource file, path to manifest file | |
231 and resource name which can be "1" (for executables) or "2" (for DLLs).""" | |
232 manifest_path, resource_path, resource_name = args | |
233 with open(resource_path, 'wb') as output: | |
234 output.write('#include <windows.h>\n%s RT_MANIFEST "%s"' % ( | |
235 resource_name, | |
236 os.path.abspath(manifest_path).replace('\\', '/'))) | |
237 | |
238 def ExecMidlWrapper(self, arch, outdir, tlb, h, dlldata, iid, proxy, idl, | |
239 *flags): | |
240 """Filter noisy filenames output from MIDL compile step that isn't | |
241 quietable via command line flags. | |
242 """ | |
243 args = ['midl', '/nologo'] + list(flags) + [ | |
244 '/out', outdir, | |
245 '/tlb', tlb, | |
246 '/h', h, | |
247 '/dlldata', dlldata, | |
248 '/iid', iid, | |
249 '/proxy', proxy, | |
250 idl] | |
251 env = self._GetEnv(arch) | |
252 popen = subprocess.Popen(args, shell=True, env=env, | |
253 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
254 out, _ = popen.communicate() | |
255 # Filter junk out of stdout, and write filtered versions. Output we want | |
256 # to filter is pairs of lines that look like this: | |
257 # Processing C:\Program Files (x86)\Microsoft SDKs\...\include\objidl.idl | |
258 # objidl.idl | |
259 lines = out.splitlines() | |
260 prefixes = ('Processing ', '64 bit Processing ') | |
261 processing = set(os.path.basename(x) | |
262 for x in lines if x.startswith(prefixes)) | |
263 for line in lines: | |
264 if not line.startswith(prefixes) and line not in processing: | |
265 print line | |
266 return popen.returncode | |
267 | |
268 def ExecAsmWrapper(self, arch, *args): | |
269 """Filter logo banner from invocations of asm.exe.""" | |
270 env = self._GetEnv(arch) | |
271 popen = subprocess.Popen(args, shell=True, env=env, | |
272 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
273 out, _ = popen.communicate() | |
274 for line in out.splitlines(): | |
275 # Split to avoid triggering license checks: | |
276 if (not line.startswith('Copy' + 'right (C' + | |
277 ') Microsoft Corporation') and | |
278 not line.startswith('Microsoft (R) Macro Assembler') and | |
279 not line.startswith(' Assembling: ') and | |
280 line): | |
281 print line | |
282 return popen.returncode | |
283 | |
284 def ExecRcWrapper(self, arch, *args): | |
285 """Filter logo banner from invocations of rc.exe. Older versions of RC | |
286 don't support the /nologo flag.""" | |
287 env = self._GetEnv(arch) | |
288 popen = subprocess.Popen(args, shell=True, env=env, | |
289 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
290 out, _ = popen.communicate() | |
291 for line in out.splitlines(): | |
292 if (not line.startswith('Microsoft (R) Windows (R) Resource Compiler') and | |
293 not line.startswith('Copy' + 'right (C' + | |
294 ') Microsoft Corporation') and | |
295 line): | |
296 print line | |
297 return popen.returncode | |
298 | |
299 def ExecActionWrapper(self, arch, rspfile, *dir): | |
300 """Runs an action command line from a response file using the environment | |
301 for |arch|. If |dir| is supplied, use that as the working directory.""" | |
302 env = self._GetEnv(arch) | |
303 # TODO(scottmg): This is a temporary hack to get some specific variables | |
304 # through to actions that are set after GN-time. http://crbug.com/333738. | |
305 for k, v in os.environ.iteritems(): | |
306 if k not in env: | |
307 env[k] = v | |
308 args = open(rspfile).read() | |
309 dir = dir[0] if dir else None | |
310 return subprocess.call(args, shell=True, env=env, cwd=dir) | |
311 | |
312 def ExecClCompile(self, project_dir, selected_files): | |
313 """Executed by msvs-ninja projects when the 'ClCompile' target is used to | |
314 build selected C/C++ files.""" | |
315 project_dir = os.path.relpath(project_dir, BASE_DIR) | |
316 selected_files = selected_files.split(';') | |
317 ninja_targets = [os.path.join(project_dir, filename) + '^^' | |
318 for filename in selected_files] | |
319 cmd = ['ninja.exe'] | |
320 cmd.extend(ninja_targets) | |
321 return subprocess.call(cmd, shell=True, cwd=BASE_DIR) | |
322 | |
323 if __name__ == '__main__': | |
324 sys.exit(main(sys.argv[1:])) | |
OLD | NEW |