OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
4 # | |
5 # Use of this source code is governed by a BSD-style license | |
6 # that can be found in the LICENSE file in the root of the source | |
7 # tree. An additional intellectual property rights grant can be found | |
8 # in the file PATENTS. All contributing project authors may | |
9 # be found in the AUTHORS file in the root of the source tree. | |
10 | |
11 | |
12 """Given the output of -t commands from a ninja build for a gyp and GN generated | |
13 build, report on differences between the command lines.""" | |
14 | |
15 | |
16 import os | |
17 import shlex | |
18 import subprocess | |
19 import sys | |
20 | |
21 | |
22 # Must be in the checkout root directory. | |
23 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
24 os.chdir(BASE_DIR) | |
25 | |
26 | |
27 g_total_differences = 0 | |
kjellander_webrtc
2016/08/19 12:16:57
Add the following comment to this line to ignore t
| |
28 | |
29 | |
30 def FilterChromium(filename): | |
31 return filename.replace('chromium/src/', '') | |
32 | |
33 | |
34 def FindAndRemoveArgWithValue(command_line, argname): | |
35 """Given a command line as a list, remove and return the value of an option | |
36 that takes a value as a separate entry. | |
37 | |
38 Modifies |command_line| in place. | |
39 """ | |
40 if argname not in command_line: | |
41 return '' | |
42 location = command_line.index(argname) | |
43 value = command_line[location + 1] | |
44 command_line[location:location + 2] = [] | |
45 return value | |
46 | |
47 | |
48 def MergeSpacedArgs(command_line, argname): | |
49 """Combine all arguments |argname| with their values, separated by a space.""" | |
50 i = 0 | |
51 result = [] | |
52 while i < len(command_line): | |
53 arg = command_line[i] | |
54 if arg == argname: | |
55 result.append(arg + ' ' + command_line[i + 1]) | |
56 i += 1 | |
57 else: | |
58 result.append(arg) | |
59 i += 1 | |
60 return result | |
61 | |
62 | |
63 def NormalizeSymbolArguments(command_line): | |
64 """Normalize -g arguments. | |
65 | |
66 If there's no -g args, it's equivalent to -g0. -g2 is equivalent to -g. | |
67 Modifies |command_line| in place. | |
68 """ | |
69 # Strip -g0 if there's no symbols. | |
70 have_some_symbols = False | |
71 for x in command_line: | |
72 if x.startswith('-g') and x != '-g0': | |
73 have_some_symbols = True | |
74 if not have_some_symbols and '-g0' in command_line: | |
75 command_line.remove('-g0') | |
76 | |
77 # Rename -g2 to -g. | |
78 if '-g2' in command_line: | |
79 command_line[command_line.index('-g2')] = '-g' | |
80 | |
81 | |
82 def GetFlags(lines, build_dir): | |
83 """Turn a list of command lines into a semi-structured dict.""" | |
84 is_win = sys.platform == 'win32' | |
85 flags_by_output = {} | |
86 for line in lines: | |
87 command_line = shlex.split(line.strip(), posix=not is_win)[1:] | |
88 | |
89 output_name = FindAndRemoveArgWithValue(command_line, '-o') | |
90 dep_name = FindAndRemoveArgWithValue(command_line, '-MF') | |
91 | |
92 NormalizeSymbolArguments(command_line) | |
93 | |
94 command_line = MergeSpacedArgs(command_line, '-Xclang') | |
95 | |
96 cc_file = [x for x in command_line if x.endswith('.cc') or | |
97 x.endswith('.c') or | |
98 x.endswith('.cpp')] | |
99 | |
100 if len(cc_file) == 0 and 'stamp' not in line: | |
101 cc_file = ["LINKING"] | |
102 | |
103 if len(cc_file) != 1: | |
104 print 'Skipping %s' % command_line | |
105 continue | |
106 assert len(cc_file) == 1 | |
107 | |
108 if is_win: | |
109 rsp_file = [x for x in command_line if x.endswith('.rsp')] | |
110 assert len(rsp_file) <= 1 | |
111 if rsp_file: | |
112 rsp_file = os.path.join(build_dir, rsp_file[0][1:]) | |
113 with open(rsp_file, "r") as open_rsp_file: | |
114 command_line = shlex.split(open_rsp_file, posix=False) | |
115 | |
116 defines = [x for x in command_line if x.startswith('-D')] | |
117 include_dirs = [x for x in command_line if x.startswith('-I')] | |
118 dash_f = [x for x in command_line if x.startswith('-f')] | |
119 warnings = \ | |
120 [x for x in command_line if x.startswith('/wd' if is_win else '-W')] | |
121 others = [x for x in command_line if x not in defines and \ | |
122 x not in include_dirs and \ | |
123 x not in dash_f and \ | |
124 x not in warnings and \ | |
125 x not in cc_file] | |
126 | |
127 for index, value in enumerate(include_dirs): | |
128 if value == '-Igen': | |
129 continue | |
130 path = value[2:] | |
131 if not os.path.isabs(path): | |
132 path = os.path.join(build_dir, path) | |
133 include_dirs[index] = '-I' + os.path.normpath(path) | |
134 | |
135 # GYP supports paths above the source root like <(DEPTH)/../foo while such | |
136 # paths are unsupported by gn. But gn allows to use system-absolute paths | |
137 # instead (paths that start with single '/'). Normalize all paths. | |
138 cc_file = [os.path.normpath(os.path.join(build_dir, cc_file[0]))] | |
139 | |
140 # Filter for libFindBadConstructs.so having a relative path in one and | |
141 # absolute path in the other. | |
142 others_filtered = [] | |
143 for x in others: | |
144 if x.startswith('-Xclang ') and x.endswith('libFindBadConstructs.so'): | |
145 others_filtered.append( | |
146 '-Xclang ' + | |
147 os.path.join(os.getcwd(), | |
148 os.path.normpath( | |
149 os.path.join('out/gn_flags', x.split(' ', 1)[1])))) | |
150 elif x.startswith('-B'): | |
151 others_filtered.append( | |
152 '-B' + | |
153 os.path.join(os.getcwd(), | |
154 os.path.normpath(os.path.join('out/gn_flags', x[2:])))) | |
155 else: | |
156 others_filtered.append(x) | |
157 others = others_filtered | |
158 | |
159 flags_by_output[cc_file[0]] = { | |
160 'output': output_name, | |
161 'depname': dep_name, | |
162 'defines': sorted(defines), | |
163 'include_dirs': sorted(include_dirs), # TODO(scottmg): This is wrong. | |
164 'dash_f': sorted(dash_f), | |
165 'warnings': sorted(warnings), | |
166 'other': sorted(others), | |
167 } | |
168 return flags_by_output | |
169 | |
170 | |
171 def CompareLists(gyp, gn, name, dont_care_gyp=None, dont_care_gn=None): | |
172 """Return a report of any differences between gyp and gn lists, ignoring | |
173 anything in |dont_care_{gyp|gn}| respectively.""" | |
174 global g_total_differences | |
175 if not dont_care_gyp: | |
176 dont_care_gyp = [] | |
177 if not dont_care_gn: | |
178 dont_care_gn = [] | |
179 output = '' | |
180 if gyp[name] != gn[name]: | |
181 gyp_set = set(gyp[name]) | |
182 gn_set = set(gn[name]) | |
183 missing_in_gyp = gyp_set - gn_set | |
184 missing_in_gn = gn_set - gyp_set | |
185 missing_in_gyp -= set(dont_care_gyp) | |
186 missing_in_gn -= set(dont_care_gn) | |
187 if missing_in_gyp or missing_in_gn: | |
188 output += ' %s differ:\n' % name | |
189 if missing_in_gyp: | |
190 output += ' In gyp, but not in GN:\n %s' % '\n '.join( | |
191 sorted(missing_in_gyp)) + '\n' | |
192 g_total_differences += len(missing_in_gyp) | |
193 if missing_in_gn: | |
194 output += ' In GN, but not in gyp:\n %s' % '\n '.join( | |
195 sorted(missing_in_gn)) + '\n\n' | |
196 g_total_differences += len(missing_in_gn) | |
197 return output | |
198 | |
199 | |
200 def Run(command_line): | |
201 """Run |command_line| as a subprocess and return stdout. Raises on error.""" | |
202 try: | |
203 return subprocess.check_output(command_line, shell=True) | |
204 except subprocess.CalledProcessError as e: | |
205 # Rescue the output we got until the exception happened. | |
206 print '#### Stdout: ####################################################' | |
207 print e.output | |
208 print '#################################################################' | |
209 raise | |
210 | |
211 | |
212 def main(): | |
213 if len(sys.argv) < 4: | |
214 print ('usage: %s gn_outdir gyp_outdir gn_target ' | |
215 '[gyp_target1, gyp_target2, ...]' % __file__) | |
216 return 1 | |
217 | |
218 if len(sys.argv) == 4: | |
219 sys.argv.append(sys.argv[3]) | |
220 gn_out_dir = sys.argv[1] | |
221 print >> sys.stderr, 'Expecting gn outdir in %s...' % gn_out_dir | |
222 gn = Run('ninja -C %s -t commands %s' % (gn_out_dir, sys.argv[3])) | |
223 if sys.platform == 'win32': | |
224 # On Windows flags are stored in .rsp files which are created during build. | |
225 print >> sys.stderr, 'Building in %s...' % gn_out_dir | |
226 Run('ninja -C %s -d keeprsp %s' % (gn_out_dir, sys.argv[3])) | |
227 | |
228 gyp_out_dir = sys.argv[2] | |
229 print >> sys.stderr, 'Expecting gyp outdir in %s...' % gyp_out_dir | |
230 gyp = Run('ninja -C %s -t commands %s' % | |
231 (gyp_out_dir, " ".join(sys.argv[4:]))) | |
232 if sys.platform == 'win32': | |
233 # On Windows flags are stored in .rsp files which are created during build. | |
234 print >> sys.stderr, 'Building in %s...' % gyp_out_dir | |
235 Run('ninja -C %s -d keeprsp %s' % (gyp_out_dir, " ".join(sys.argv[4:]))) | |
236 | |
237 all_gyp_flags = GetFlags(gyp.splitlines(), | |
238 os.path.join(os.getcwd(), gyp_out_dir)) | |
239 all_gn_flags = GetFlags(gn.splitlines(), | |
240 os.path.join(os.getcwd(), gn_out_dir)) | |
241 | |
242 all_gyp_flags = dict((FilterChromium(filename), value) | |
243 for filename, value in all_gyp_flags.items()) | |
244 all_gn_flags = dict((FilterChromium(filename), value) | |
245 for filename, value in all_gn_flags.items()) | |
246 | |
247 gyp_files = set(all_gyp_flags.keys()) | |
248 gn_files = set(all_gn_flags.keys()) | |
249 | |
250 different_source_list = gyp_files != gn_files | |
251 if different_source_list: | |
252 print 'Different set of sources files:' | |
253 print ' In gyp, not in GN:\n %s' % '\n '.join( | |
254 sorted(gyp_files - gn_files)) | |
255 print ' In GN, not in gyp:\n %s' % '\n '.join( | |
256 sorted(gn_files - gyp_files)) | |
257 print '\nNote that flags will only be compared for files in both sets.\n' | |
258 file_list = gyp_files & gn_files | |
259 files_with_given_differences = {} | |
260 for filename in sorted(file_list): | |
261 gyp_flags = all_gyp_flags[filename] | |
262 gn_flags = all_gn_flags[filename] | |
263 differences = CompareLists(gyp_flags, gn_flags, 'dash_f') | |
264 differences += CompareLists(gyp_flags, gn_flags, 'defines') | |
265 differences += CompareLists(gyp_flags, gn_flags, 'include_dirs', | |
266 ['-I%s' % os.path.dirname(BASE_DIR)]) | |
267 differences += CompareLists(gyp_flags, gn_flags, 'warnings', | |
268 # More conservative warnings in GN we consider to be OK. | |
269 dont_care_gyp=[ | |
270 '/wd4091', # 'keyword' : ignored on left of 'type' when no variable | |
271 # is declared. | |
272 '/wd4456', # Declaration hides previous local declaration. | |
273 '/wd4457', # Declaration hides function parameter. | |
274 '/wd4458', # Declaration hides class member. | |
275 '/wd4459', # Declaration hides global declaration. | |
276 '/wd4702', # Unreachable code. | |
277 '/wd4800', # Forcing value to bool 'true' or 'false'. | |
278 '/wd4838', # Conversion from 'type' to 'type' requires a narrowing | |
279 # conversion. | |
280 ] if sys.platform == 'win32' else None, | |
281 dont_care_gn=[ | |
282 '-Wendif-labels', | |
283 '-Wextra', | |
284 '-Wsign-compare', | |
285 ] if not sys.platform == 'win32' else None) | |
286 differences += CompareLists(gyp_flags, gn_flags, 'other') | |
287 if differences: | |
288 files_with_given_differences.setdefault(differences, []).append(filename) | |
289 | |
290 for diff, files in files_with_given_differences.iteritems(): | |
291 print '\n'.join(sorted(files)) | |
292 print diff | |
293 | |
294 print 'Total differences:', g_total_differences | |
295 # TODO(scottmg): Return failure on difference once we're closer to identical. | |
296 return 0 | |
297 | |
298 | |
299 if __name__ == '__main__': | |
300 sys.exit(main()) | |
OLD | NEW |