Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /build /fix_gn_headers.py
blob: 5111b5db4d4ccfb5603d45e435cf2800df07180d [file] [log] [blame] [edit]
#!/usr/bin/env python3
# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Fix header files missing in GN.
This script takes the missing header files from check_gn_headers.py, and
try to fix them by adding them to the GN files.
Manual cleaning up is likely required afterwards.
"""
import argparse
import os
import re
import subprocess
import sys
defGitGrep(pattern):
p= subprocess.Popen(
['git','grep','-En', pattern,'--','*.gn','*.gni'],
stdout=subprocess.PIPE)
out, _= p.communicate()
return out, p.returncode
defValidMatches(basename, cc, grep_lines):
"""Filter out 'git grep' matches with header files already."""
matches=[]
for linein grep_lines:
gnfile, linenr, contents= line.split(':')
linenr= int(linenr)
new= re.sub(cc, basename, contents)
lines= open(gnfile).read().splitlines()
assert contentsin lines[linenr-1]
# Skip if it's already there. It could be before or after the match.
if lines[linenr]== new:
continue
if lines[linenr-2]== new:
continue
print(' ', gnfile, linenr, new)
matches.append((gnfile, linenr, new))
return matches
defAddHeadersNextToCC(headers, skip_ambiguous=True):
"""Add header files next to the corresponding .cc files in GN files.
When skip_ambiguous is True, skip if multiple .cc files are found.
Returns unhandled headers.
Manual cleaning up is likely required, especially if not skip_ambiguous.
"""
edits={}
unhandled=[]
for filenamein headers:
filename= filename.strip()
ifnot(filename.endswith('.h')or filename.endswith('.hh')):
continue
basename= os.path.basename(filename)
print(filename)
cc= r'\b'+ os.path.splitext(basename)[0]+ r'\.(cc|cpp|mm)\b'
out, returncode=GitGrep('(/|")'+ cc+'"')
if returncode!=0ornot out:
unhandled.append(filename)
continue
matches=ValidMatches(basename, cc, out.splitlines())
if len(matches)==0:
continue
if len(matches)>1:
print('\n[WARNING] Ambiguous matching for', filename)
for iin enumerate(matches,1):
print('%d: %s'%(i[0], i[1]))
print()
if skip_ambiguous:
continue
picked= raw_input('Pick the matches ("2,3" for multiple): ')
try:
matches=[matches[int(i)-1]for iin picked.split(',')]
except(ValueError,IndexError):
continue
for matchin matches:
gnfile, linenr, new= match
print(' ', gnfile, linenr, new)
edits.setdefault(gnfile,{})[linenr]= new
for gnfilein edits:
lines= open(gnfile).read().splitlines()
for lin sorted(edits[gnfile].keys(), reverse=True):
lines.insert(l, edits[gnfile][l])
open(gnfile,'w').write('\n'.join(lines)+'\n')
return unhandled
defAddHeadersToSources(headers, skip_ambiguous=True):
"""Add header files to the sources list in the first GN file.
The target GN file is the first one up the parent directories.
This usually does the wrong thing for _test files if the test and the main
target are in the same .gn file.
When skip_ambiguous is True, skip if multiple sources arrays are found.
"git cl format" afterwards is required. Manually cleaning up duplicated items
is likely required.
"""
for filenamein headers:
filename= filename.strip()
print(filename)
dirname= os.path.dirname(filename)
whilenot os.path.exists(os.path.join(dirname,'BUILD.gn')):
dirname= os.path.dirname(dirname)
rel= filename[len(dirname)+1:]
gnfile= os.path.join(dirname,'BUILD.gn')
lines= open(gnfile).read().splitlines()
matched=[ifor i, lin enumerate(lines)if' sources = ['in l]
if skip_ambiguousand len(matched)>1:
print('[WARNING] Multiple sources in', gnfile)
continue
if len(matched)<1:
continue
print(' ', gnfile, rel)
index= matched[0]
lines.insert(index+1,'"%s",'% rel)
open(gnfile,'w').write('\n'.join(lines)+'\n')
defRemoveHeader(headers, skip_ambiguous=True):
"""Remove non-existing headers in GN files.
When skip_ambiguous is True, skip if multiple matches are found.
"""
edits={}
unhandled=[]
for filenamein headers:
filename= filename.strip()
ifnot(filename.endswith('.h')or filename.endswith('.hh')):
continue
basename= os.path.basename(filename)
print(filename)
out, returncode=GitGrep('(/|")'+ basename+'"')
if returncode!=0ornot out:
unhandled.append(filename)
print(' Not found')
continue
grep_lines= out.splitlines()
matches=[]
for linein grep_lines:
gnfile, linenr, contents= line.split(':')
print(' ', gnfile, linenr, contents)
linenr= int(linenr)
lines= open(gnfile).read().splitlines()
assert contentsin lines[linenr-1]
matches.append((gnfile, linenr, contents))
if len(matches)==0:
continue
if len(matches)>1:
print('\n[WARNING] Ambiguous matching for', filename)
for iin enumerate(matches,1):
print('%d: %s'%(i[0], i[1]))
print()
if skip_ambiguous:
continue
picked= raw_input('Pick the matches ("2,3" for multiple): ')
try:
matches=[matches[int(i)-1]for iin picked.split(',')]
except(ValueError,IndexError):
continue
for matchin matches:
gnfile, linenr, contents= match
print(' ', gnfile, linenr, contents)
edits.setdefault(gnfile, set()).add(linenr)
for gnfilein edits:
lines= open(gnfile).read().splitlines()
for lin sorted(edits[gnfile], reverse=True):
lines.pop(l-1)
open(gnfile,'w').write('\n'.join(lines)+'\n')
return unhandled
def main():
parser= argparse.ArgumentParser()
parser.add_argument('input_file', help="missing or non-existing headers, "
"output of check_gn_headers.py")
parser.add_argument('--prefix',
help="only handle path name with this prefix")
parser.add_argument('--remove', action='store_true',
help="treat input_file as non-existing headers")
args, _extras= parser.parse_known_args()
headers= open(args.input_file).readlines()
if args.prefix:
headers=[ifor iin headersif i.startswith(args.prefix)]
if args.remove:
RemoveHeader(headers,False)
else:
unhandled=AddHeadersNextToCC(headers)
AddHeadersToSources(unhandled)
if __name__=='__main__':
sys.exit(main())

[8]ページ先頭

©2009-2025 Movatter.jp