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 sys | |
11 | |
12 PRAGMA_WARNING = '[-Wunknown-pragmas]' | |
agrieve
2016/07/27 01:42:27
nit: Prefix with _
estevenson
2016/07/28 21:56:46
Done.
| |
13 PRAGMA_WARNING_LINE_COUNT = 9 | |
14 BAT_PREFIX = 'cmd /c call ' | |
15 | |
16 | |
17 def CommandToRun(command): | |
18 """Generates commands compatible with Windows. | |
19 | |
20 When running on a Windows host and using a toolchain whose tools are | |
21 actually wrapper scripts (i.e. .bat files on Windows) rather than binary | |
22 executables, the |command| to run has to be prefixed with this magic. | |
23 The GN toolchain definitions take care of that for when GN/Ninja is | |
24 running the tool directly. When that command is passed in to this | |
25 script, it appears as a unitary string but needs to be split up so that | |
26 just 'cmd' is the actual command given to Python's subprocess module. | |
27 | |
28 Args: | |
29 command: List containing the UNIX style |command|. | |
30 | |
31 Returns: | |
32 A list containing the Windows version of the |command|. | |
33 """ | |
34 if command[0].startswith(BAT_PREFIX): | |
35 command = command[0].split(None, 3) + command[1:] | |
36 return command | |
37 | |
38 | |
39 def CombineWhitelists(rspfile, outfile): | |
agrieve
2016/07/27 01:42:28
nit: CombineWhitelists -> CombineResourceWhitelist
estevenson
2016/07/28 21:56:45
Done.
| |
40 """Combines all whitelists for a resource file into a single whitelist. | |
41 | |
42 Args: | |
43 rspfile: Path to the resource file containing all targets. | |
44 outfile: Path to save the combined whitelist. | |
45 """ | |
46 with open(rspfile, 'r') as f: | |
47 tokens = f.read().split() | |
agrieve
2016/07/27 01:42:27
nit: split won't properly handle filenames with sp
estevenson
2016/07/28 21:56:45
Done.
| |
48 whitelists = ['%s.whitelist' % token for token in tokens | |
agrieve
2016/07/27 01:42:27
nit: unindent
estevenson
2016/07/28 21:56:46
Done.
| |
49 if os.path.exists('%s.whitelist' % token)] | |
50 | |
51 resources = set() | |
52 for whitelist in whitelists: | |
53 with open(whitelist, 'r') as f: | |
54 resources.update(f.readlines()) | |
55 | |
56 with open(outfile, 'w') as f: | |
57 f.writelines(resources) | |
58 | |
59 | |
60 def GetResourceIdsInPragmaWarnings(text): | |
agrieve
2016/07/27 01:42:27
nit: never prefix a non-trivial function with "Get
estevenson
2016/07/28 21:56:46
Done.
| |
61 """Returns set of resource IDs that are inside unknown pragma warnings. | |
62 | |
63 Args: | |
64 text: The text that will be scanned for unknown pragma warnings. | |
65 | |
66 Returns: | |
67 A set containing integers representing resource IDs. | |
68 """ | |
69 used_resources = set() | |
70 lines = text.splitlines() | |
71 unknown_pragma_warning_pattern = re.compile( | |
agrieve
2016/07/27 01:42:27
nit: Make this a constant at the top of the file.
estevenson
2016/07/28 21:56:46
Done.
| |
72 'whitelisted_resource_(?P<resource_id>[0-9]+)') | |
73 for ln in lines: | |
74 match = unknown_pragma_warning_pattern.search(ln) | |
75 if match: | |
76 resource_id = int(match.group('resource_id')) | |
77 used_resources.add(resource_id) | |
78 | |
79 return used_resources | |
80 | |
81 | |
82 def _FilterStderr(text, warning, warning_size): | |
83 """Removes warnings from compilation output. | |
84 | |
85 Args: | |
86 text: A String containing warnings. | |
87 warning: A string that identifies the warning type to surpress. | |
88 warning_size: The number of lines a warning corresponding to the given | |
89 |warning| occupies. | |
90 | |
91 Returns: | |
92 A string containing the filtered text. | |
93 """ | |
94 # TODO(estevenson): Find a cleaner, less error prone way to do this. | |
agrieve
2016/07/27 01:42:27
Yep, ideally, this should be combined with the abo
estevenson
2016/07/28 21:56:46
We discussed this, there doesn't seem to be a clea
| |
95 lines = text.splitlines() | |
96 warning_groups = [range(lineno, lineno + warning_size) | |
97 for lineno, line in enumerate(lines) | |
98 if warning in line] | |
99 warning_lines = [line for group in warning_groups for line in group] | |
100 filtered_lines = [line for i, line in enumerate(lines) | |
101 if i not in warning_lines] | |
102 filtered_text = '\n'.join(line for line in filtered_lines) | |
103 return filtered_text | |
104 | |
105 | |
106 def FilterUnknownPragmas(stderr): | |
agrieve
2016/07/27 01:42:27
Note: We only want to filter warnings that are cau
estevenson
2016/07/28 21:56:45
Done.
| |
107 """Removes unknown pragma warnings from stderr. | |
108 | |
109 Args: | |
110 stderr: Stderr output captured from a compilation command. | |
111 | |
112 Returns: | |
113 A string containing the filtered stderr. | |
114 """ | |
115 return _FilterStderr(stderr, PRAGMA_WARNING, PRAGMA_WARNING_LINE_COUNT) | |
116 | |
117 | |
118 def GetCommandOutput(args, cwd=None, env=None): | |
agrieve
2016/07/27 01:42:28
nit: remove optional params since they are not use
estevenson
2016/07/28 21:56:46
Done.
| |
119 """Returns the output of a command. | |
120 | |
121 Args: | |
122 args: A list containing the command and arguments. | |
123 cwd: The working directory from where the command should be made. | |
124 env: Environment variables for the new process. | |
125 """ | |
126 if not cwd: | |
127 cwd = os.getcwd() | |
128 | |
129 child = subprocess.Popen(args, | |
130 stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env=env) | |
agrieve
2016/07/27 01:42:28
There's no need to pipe stdout since we're not fil
estevenson
2016/07/28 21:56:46
Done.
| |
131 stdout, stderr = child.communicate() | |
132 return child.returncode, stdout, stderr | |
OLD | NEW |