OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2016 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 """Helper functions for gcc_toolchain.gni wrappers.""" | |
6 | |
7 import os | |
8 import re | |
9 import subprocess | |
10 import shlex | |
11 import sys | |
12 | |
13 _BAT_PREFIX = 'cmd /c call ' | |
14 _WHITELIST_RE = re.compile( | |
15 'whitelisted_resource_(?P<resource_id>[0-9]+)') | |
agrieve
2016/07/29 01:55:21
nit: fits on prev line?
estevenson
2016/07/29 15:33:57
Done.
| |
16 | |
17 | |
18 def CommandToRun(command): | |
19 """Generates commands compatible with Windows. | |
20 | |
21 When running on a Windows host and using a toolchain whose tools are | |
22 actually wrapper scripts (i.e. .bat files on Windows) rather than binary | |
23 executables, the |command| to run has to be prefixed with this magic. | |
24 The GN toolchain definitions take care of that for when GN/Ninja is | |
25 running the tool directly. When that command is passed in to this | |
26 script, it appears as a unitary string but needs to be split up so that | |
27 just 'cmd' is the actual command given to Python's subprocess module. | |
28 | |
29 Args: | |
30 command: List containing the UNIX style |command|. | |
31 | |
32 Returns: | |
33 A list containing the Windows version of the |command|. | |
34 """ | |
35 if command[0].startswith(_BAT_PREFIX): | |
36 command = command[0].split(None, 3) + command[1:] | |
37 return command | |
38 | |
39 | |
40 def ResolveRspLinks(inputs): | |
41 """Return a list of files contained in a response file. | |
42 | |
43 Args: | |
44 inputs: A command containing rsp files. | |
45 | |
46 Returns: | |
47 A set containing the rsp file content.""" | |
48 rspfiles = [a[1:] for a in inputs if a.startswith('@')] | |
49 resolved = set() | |
50 for rspfile in rspfiles: | |
51 with open(rspfile, 'r') as f: | |
52 resolved.update(shlex.split(f.read())) | |
53 | |
54 return resolved | |
55 | |
56 | |
57 def CombineResourceWhitelists(whitelist_candidates, outfile, whitelist=True): | |
58 """Combines all whitelists for a resource file into a single whitelist. | |
59 | |
60 Args: | |
61 whitelist_candidates: List of paths to rsp files containing all targets. | |
62 outfile: Path to save the combined whitelist. | |
63 whitelist: Whether or not whitelisting is desired. | |
64 """ | |
65 if not whitelist: | |
66 open(outfile, 'w').close() | |
agrieve
2016/07/29 01:55:21
missing return. Although I think it's probably bet
estevenson
2016/07/29 15:33:57
Done.
| |
67 | |
68 whitelists = ['%s.whitelist' % candidate for candidate in whitelist_candidates | |
agrieve
2016/07/29 01:55:21
nit: better to use a generator if you're just goin
estevenson
2016/07/29 15:33:57
Done.
| |
69 if os.path.exists('%s.whitelist' % candidate)] | |
70 | |
71 resources = set() | |
72 for whitelist in whitelists: | |
73 with open(whitelist, 'r') as f: | |
74 resources.update(f.readlines()) | |
75 | |
76 with open(outfile, 'w') as f: | |
77 f.writelines(resources) | |
78 | |
79 | |
80 def ExtractResourceIdsFromPragmaWarnings(text): | |
81 """Returns set of resource IDs that are inside unknown pragma warnings. | |
82 | |
83 Args: | |
84 text: The text that will be scanned for unknown pragma warnings. | |
85 | |
86 Returns: | |
87 A set containing integers representing resource IDs. | |
88 """ | |
89 used_resources = set() | |
90 lines = text.splitlines() | |
91 for ln in lines: | |
92 match = _WHITELIST_RE.search(ln) | |
93 if match: | |
94 resource_id = int(match.group('resource_id')) | |
95 used_resources.add(resource_id) | |
96 | |
97 return used_resources | |
98 | |
99 | |
100 def CaptureCommandStderr(command): | |
101 """Returns the stderr of a command. | |
102 | |
103 Args: | |
104 args: A list containing the command and arguments. | |
105 cwd: The working directory from where the command should be made. | |
106 env: Environment variables for the new process. | |
107 """ | |
108 child = subprocess.Popen(command, stderr=subprocess.PIPE) | |
109 _, stderr = child.communicate() | |
110 return child.returncode, stderr | |
OLD | NEW |