Commit a237428c authored by rhalavati's avatar rhalavati Committed by Commit bot

Clang scripts updates.

Two updates are proposed for clang scripts:
An optional parameter is added to run_tool.py called (tool-param) that is directly passed to the tool that is run.
An optional parameter is added to generate_win_compdb.py to specify the build directory. Its default value is out/Debug so that it won't change the previous behavior.

BUG=670072

Review-Url: https://codereview.chromium.org/2542563002
Cr-Commit-Position: refs/heads/master@{#435532}
parent cfb28f09
...@@ -11,6 +11,7 @@ which also confuses clang. This script generates a compile DB that should mostly ...@@ -11,6 +11,7 @@ which also confuses clang. This script generates a compile DB that should mostly
work until clang tooling can be improved upstream. work until clang tooling can be improved upstream.
""" """
import argparse
import os import os
import re import re
import json import json
...@@ -60,10 +61,18 @@ def _ProcessEntry(e): ...@@ -60,10 +61,18 @@ def _ProcessEntry(e):
def main(argv): def main(argv):
# Parse argument
parser = argparse.ArgumentParser()
parser.add_argument(
'build_path',
nargs='?',
help='Path to build directory',
default='out/Debug')
args = parser.parse_args()
# First, generate the compile database. # First, generate the compile database.
print 'Generating compile DB with ninja...' print 'Generating compile DB with ninja...'
compile_db_as_json = subprocess.check_output(shlex.split( compile_db_as_json = subprocess.check_output(shlex.split(
'ninja -C out/Debug -t compdb cc cxx objc objcxx')) 'ninja -C %s -t compdb cc cxx objc objcxx' % args.build_path))
compile_db = json.loads(compile_db_as_json) compile_db = json.loads(compile_db_as_json)
print 'Read in %d entries from the compile db' % len(compile_db) print 'Read in %d entries from the compile db' % len(compile_db)
...@@ -74,7 +83,7 @@ def main(argv): ...@@ -74,7 +83,7 @@ def main(argv):
compile_db = [e for e in compile_db if '_nacl.cc.pdb' not in e['command'] compile_db = [e for e in compile_db if '_nacl.cc.pdb' not in e['command']
and '_nacl_win64.cc.pdb' not in e['command']] and '_nacl_win64.cc.pdb' not in e['command']]
print 'Filtered out %d entries...' % (original_length - len(compile_db)) print 'Filtered out %d entries...' % (original_length - len(compile_db))
f = file('out/Debug/compile_commands.json', 'w') f = file('%s/compile_commands.json' % args.build_path, 'w')
f.write(json.dumps(compile_db, indent=2)) f.write(json.dumps(compile_db, indent=2))
print 'Done!' print 'Done!'
......
...@@ -114,7 +114,7 @@ def _ExtractEditsFromStdout(build_directory, stdout): ...@@ -114,7 +114,7 @@ def _ExtractEditsFromStdout(build_directory, stdout):
return edits return edits
def _ExecuteTool(toolname, build_directory, filename): def _ExecuteTool(toolname, tool_args, build_directory, filename):
"""Executes the tool. """Executes the tool.
This is defined outside the class so it can be pickled for the multiprocessing This is defined outside the class so it can be pickled for the multiprocessing
...@@ -122,6 +122,7 @@ def _ExecuteTool(toolname, build_directory, filename): ...@@ -122,6 +122,7 @@ def _ExecuteTool(toolname, build_directory, filename):
Args: Args:
toolname: Path to the tool to execute. toolname: Path to the tool to execute.
tool_args: Arguments to be passed to the tool. Can be None.
build_directory: Directory that contains the compile database. build_directory: Directory that contains the compile database.
filename: The file to run the tool over. filename: The file to run the tool over.
...@@ -135,10 +136,11 @@ def _ExecuteTool(toolname, build_directory, filename): ...@@ -135,10 +136,11 @@ def _ExecuteTool(toolname, build_directory, filename):
Otherwise, the filename and the output from stderr are associated with the Otherwise, the filename and the output from stderr are associated with the
keys "filename" and "stderr" respectively. keys "filename" and "stderr" respectively.
""" """
args = [toolname, '-p', build_directory, filename]
if (tool_args):
args.extend(tool_args)
command = subprocess.Popen( command = subprocess.Popen(
(toolname, '-p', build_directory, filename), args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = command.communicate() stdout, stderr = command.communicate()
if command.returncode != 0: if command.returncode != 0:
return {'status': False, 'filename': filename, 'stderr': stderr} return {'status': False, 'filename': filename, 'stderr': stderr}
...@@ -150,15 +152,17 @@ def _ExecuteTool(toolname, build_directory, filename): ...@@ -150,15 +152,17 @@ def _ExecuteTool(toolname, build_directory, filename):
class _CompilerDispatcher(object): class _CompilerDispatcher(object):
"""Multiprocessing controller for running clang tools in parallel.""" """Multiprocessing controller for running clang tools in parallel."""
def __init__(self, toolname, build_directory, filenames): def __init__(self, toolname, tool_args, build_directory, filenames):
"""Initializer method. """Initializer method.
Args: Args:
toolname: Path to the tool to execute. toolname: Path to the tool to execute.
tool_args: Arguments to be passed to the tool. Can be None.
build_directory: Directory that contains the compile database. build_directory: Directory that contains the compile database.
filenames: The files to run the tool over. filenames: The files to run the tool over.
""" """
self.__toolname = toolname self.__toolname = toolname
self.__tool_args = tool_args
self.__build_directory = build_directory self.__build_directory = build_directory
self.__filenames = filenames self.__filenames = filenames
self.__success_count = 0 self.__success_count = 0
...@@ -178,8 +182,9 @@ class _CompilerDispatcher(object): ...@@ -178,8 +182,9 @@ class _CompilerDispatcher(object):
"""Does the grunt work.""" """Does the grunt work."""
pool = multiprocessing.Pool() pool = multiprocessing.Pool()
result_iterator = pool.imap_unordered( result_iterator = pool.imap_unordered(
functools.partial(_ExecuteTool, self.__toolname, functools.partial(_ExecuteTool, self.__toolname, self.__tool_args,
self.__build_directory), self.__filenames) self.__build_directory),
self.__filenames)
for result in result_iterator: for result in result_iterator:
self.__ProcessResult(result) self.__ProcessResult(result)
sys.stdout.write('\n') sys.stdout.write('\n')
...@@ -299,6 +304,9 @@ def main(): ...@@ -299,6 +304,9 @@ def main():
'path_filter', 'path_filter',
nargs='*', nargs='*',
help='optional paths to filter what files the tool is run on') help='optional paths to filter what files the tool is run on')
parser.add_argument(
'--tool-args', nargs='*',
help='optional arguments passed to the tool')
args = parser.parse_args() args = parser.parse_args()
os.environ['PATH'] = '%s%s%s' % ( os.environ['PATH'] = '%s%s%s' % (
...@@ -321,7 +329,8 @@ def main(): ...@@ -321,7 +329,8 @@ def main():
source_filenames = [f source_filenames = [f
for f in filenames for f in filenames
if os.path.splitext(f)[1] in extensions] if os.path.splitext(f)[1] in extensions]
dispatcher = _CompilerDispatcher(args.tool, args.compile_database, dispatcher = _CompilerDispatcher(args.tool, args.tool_args,
args.compile_database,
source_filenames) source_filenames)
dispatcher.Run() dispatcher.Run()
# Filter out edits to files that aren't in the git repository, since it's not # Filter out edits to files that aren't in the git repository, since it's not
...@@ -334,4 +343,4 @@ def main(): ...@@ -334,4 +343,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment