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('whitelisted_resource_(?P<resource_id>[0-9]+)') | |
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 ResolveRspLinks(inputs): | |
40 """Return a list of files contained in a response file. | |
41 | |
42 Args: | |
43 inputs: A command containing rsp files. | |
44 | |
45 Returns: | |
46 A set containing the rsp file content.""" | |
47 rspfiles = [a[1:] for a in inputs if a.startswith('@')] | |
48 resolved = set() | |
49 for rspfile in rspfiles: | |
50 with open(rspfile, 'r') as f: | |
51 resolved.update(shlex.split(f.read())) | |
52 | |
53 return resolved | |
54 | |
55 | |
56 def CombineResourceWhitelists(whitelist_candidates, outfile): | |
57 """Combines all whitelists for a resource file into a single whitelist. | |
58 | |
59 Args: | |
60 whitelist_candidates: List of paths to rsp files containing all targets. | |
61 outfile: Path to save the combined whitelist. | |
62 """ | |
63 whitelists = ('%s.whitelist' % candidate for candidate in whitelist_candidates | |
64 if os.path.exists('%s.whitelist' % candidate)) | |
65 | |
66 resources = set() | |
67 for whitelist in whitelists: | |
68 with open(whitelist, 'r') as f: | |
69 resources.update(f.readlines()) | |
70 | |
71 with open(outfile, 'w') as f: | |
72 f.writelines(resources) | |
73 | |
74 | |
75 def ExtractResourceIdsFromPragmaWarnings(text): | |
76 """Returns set of resource IDs that are inside unknown pragma warnings. | |
77 | |
78 Args: | |
79 text: The text that will be scanned for unknown pragma warnings. | |
80 | |
81 Returns: | |
82 A set containing integers representing resource IDs. | |
83 """ | |
84 used_resources = set() | |
85 lines = text.splitlines() | |
86 for ln in lines: | |
87 match = _WHITELIST_RE.search(ln) | |
88 if match: | |
89 resource_id = int(match.group('resource_id')) | |
90 used_resources.add(resource_id) | |
91 | |
92 return used_resources | |
93 | |
94 | |
95 def CaptureCommandStderr(command): | |
96 """Returns the stderr of a command. | |
97 | |
98 Args: | |
99 args: A list containing the command and arguments. | |
100 cwd: The working directory from where the command should be made. | |
101 env: Environment variables for the new process. | |
102 """ | |
103 child = subprocess.Popen(command, stderr=subprocess.PIPE) | |
104 _, stderr = child.communicate() | |
105 return child.returncode, stderr | |
OLD | NEW |