Commit db94d2aa authored by Sigurd Schneider's avatar Sigurd Schneider Committed by Commit Bot

[tools] Improve locs.py to include target name in json

Change-Id: I71a97bf686e9d821b607504c4211b21a4fe8234b
Notry: true
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1545906
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: 's avatarMichael Achenbach <machenbach@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60539}
parent f5048988
...@@ -131,6 +131,12 @@ def GenerateCompileCommandsAndBuild(build_dir, compile_commands_file, out): ...@@ -131,6 +131,12 @@ def GenerateCompileCommandsAndBuild(build_dir, compile_commands_file, out):
print("Error: Specified build dir {} is not a directory.".format( print("Error: Specified build dir {} is not a directory.".format(
build_dir), file=sys.stderr) build_dir), file=sys.stderr)
exit(1) exit(1)
autoninja = "autoninja -C {}".format(build_dir)
if subprocess.call(autoninja, shell=True, stdout=out) != 0:
print("Error: Building {} failed.".format(build_dir), file=sys.stderr)
exit(1)
compile_commands_file = "{}/compile_commands.json".format(build_dir) compile_commands_file = "{}/compile_commands.json".format(build_dir)
print("Generating compile commands in {}.".format( print("Generating compile commands in {}.".format(
...@@ -143,20 +149,15 @@ def GenerateCompileCommandsAndBuild(build_dir, compile_commands_file, out): ...@@ -143,20 +149,15 @@ def GenerateCompileCommandsAndBuild(build_dir, compile_commands_file, out):
compile_commands_file, build_dir), file=sys.stderr) compile_commands_file, build_dir), file=sys.stderr)
exit(1) exit(1)
autoninja = "autoninja -C {} v8_generated_cc_files".format(build_dir)
if subprocess.call(autoninja, shell=True, stdout=out) != 0:
print("Error: Building target 'v8_generated_cc_files'"
" failed for {}.".format(build_dir), file=sys.stderr)
exit(1)
return compile_commands_file return compile_commands_file
def fmt_bytes(bytes):
if bytes > 1024*1024*1024: def fmt_bytes(num_bytes):
return int(bytes / (1024*1024)), "MB" if num_bytes > 1024*1024*1024:
elif bytes > 1024*1024: return int(num_bytes / (1024*1024)), "MB"
return int(bytes / (1024)), "kB" elif num_bytes > 1024*1024:
return int(bytes), " B" return int(num_bytes / (1024)), "kB"
return int(num_bytes), " B"
class CompilationData: class CompilationData:
...@@ -175,13 +176,15 @@ class CompilationData: ...@@ -175,13 +176,15 @@ class CompilationData:
return "{:>9,} LoC ({:>7,} {}) to {:>12,} LoC ({:>7,} {}) ({:>5.0f}x)".format( return "{:>9,} LoC ({:>7,} {}) to {:>12,} LoC ({:>7,} {}) ({:>5.0f}x)".format(
self.loc, in_bytes, in_unit, self.expanded, exp_bytes, exp_unit, self.ratio()) self.loc, in_bytes, in_unit, self.expanded, exp_bytes, exp_unit, self.ratio())
class File(CompilationData): class File(CompilationData):
def __init__(self, file, loc, in_bytes, expanded, expanded_bytes): def __init__(self, file, target, loc, in_bytes, expanded, expanded_bytes):
super().__init__(loc, in_bytes, expanded, expanded_bytes) super().__init__(loc, in_bytes, expanded, expanded_bytes)
self.file = file self.file = file
self.target = target
def to_string(self): def to_string(self):
return "{} {}".format(super().to_string(), self.file) return "{} {} {}".format(super().to_string(), self.file, self.target)
class Group(CompilationData): class Group(CompilationData):
...@@ -253,8 +256,8 @@ class Results: ...@@ -253,8 +256,8 @@ class Results:
is_tracked = True is_tracked = True
return is_tracked return is_tracked
def recordFile(self, filename, loc, in_bytes, expanded, expanded_bytes): def recordFile(self, filename, targetname, loc, in_bytes, expanded, expanded_bytes):
unit = File(filename, loc, in_bytes, expanded, expanded_bytes) unit = File(filename, targetname, loc, in_bytes, expanded, expanded_bytes)
self.units[filename] = unit self.units[filename] = unit
for group in self.groups.values(): for group in self.groups.values():
group.account(unit) group.account(unit)
...@@ -274,7 +277,7 @@ class Results: ...@@ -274,7 +277,7 @@ class Results:
class LocsEncoder(json.JSONEncoder): class LocsEncoder(json.JSONEncoder):
def default(self, o): def default(self, o):
if isinstance(o, File): if isinstance(o, File):
return {"file": o.file, "loc": o.loc, "in_bytes": o.in_bytes, return {"file": o.file, "target": o.target, "loc": o.loc, "in_bytes": o.in_bytes,
"expanded": o.expanded, "expanded_bytes": o.expanded_bytes} "expanded": o.expanded, "expanded_bytes": o.expanded_bytes}
if isinstance(o, Group): if isinstance(o, Group):
return {"name": o.name, "loc": o.loc, "in_bytes": o.in_bytes, return {"name": o.name, "loc": o.loc, "in_bytes": o.in_bytes,
...@@ -290,7 +293,8 @@ class StatusLine: ...@@ -290,7 +293,8 @@ class StatusLine:
def print(self, statusline, end="\r", file=sys.stdout): def print(self, statusline, end="\r", file=sys.stdout):
self.max_width = max(self.max_width, len(statusline)) self.max_width = max(self.max_width, len(statusline))
print("{0:<{1}}".format(statusline, self.max_width), end=end, file=file, flush=True) print("{0:<{1}}".format(statusline, self.max_width),
end=end, file=file, flush=True)
class CommandSplitter: class CommandSplitter:
...@@ -299,13 +303,12 @@ class CommandSplitter: ...@@ -299,13 +303,12 @@ class CommandSplitter:
"([^\\s]*\\s+)?(?P<clangcmd>[^\\s]*clang.*)" "([^\\s]*\\s+)?(?P<clangcmd>[^\\s]*clang.*)"
" -c (?P<infile>.*) -o (?P<outfile>.*)") " -c (?P<infile>.*) -o (?P<outfile>.*)")
def process(self, compilation_unit, temp_file_name): def process(self, compilation_unit):
cmd = self.cmd_pattern.match(compilation_unit['command']) cmd = self.cmd_pattern.match(compilation_unit['command'])
outfilename = cmd.group('outfile') + ".cc" outfilename = cmd.group('outfile')
infilename = cmd.group('infile') infilename = cmd.group('infile')
infile = Path(compilation_unit['directory']).joinpath(infilename) infile = Path(compilation_unit['directory']).joinpath(infilename)
outfile = Path(str(temp_file_name)).joinpath(outfilename) return (cmd.group('clangcmd'), infilename, infile, outfilename)
return [cmd.group('clangcmd'), infilename, infile, outfile]
def Main(): def Main():
...@@ -336,29 +339,30 @@ def Main(): ...@@ -336,29 +339,30 @@ def Main():
for i, key in enumerate(data): for i, key in enumerate(data):
if not result.track(key['file']): if not result.track(key['file']):
continue continue
status.print("[{}/{}] Counting LoCs of {}".format(i, len(data), key['file']), message = "[{}/{}] Counting LoCs of {}".format(i, len(data), key['file'])
file=out) status.print(message, file=out)
clangcmd, infilename, infile, outfile = cmd_splitter.process(key, temp) clangcmd, infilename, infile, outfilename = cmd_splitter.process(key)
outfile.parent.mkdir(parents=True, exist_ok=True)
if infile.is_file(): if infile.is_file():
clangcmd = clangcmd + " -E -P " + \ clangcmd = clangcmd + " -E -P " + \
str(infile) + " -o /dev/stdout | sed '/^\\s*$/d' | wc -lc" str(infile) + " -o /dev/stdout | sed '/^\\s*$/d' | wc -lc"
loccmd = ("cat {} | sed '\\;^\\s*//;d' | sed '\\;^/\\*;d'" loccmd = ("cat {} | sed '\\;^\\s*//;d' | sed '\\;^/\\*;d'"
" | sed '/^\\*/d' | sed '/^\\s*$/d' | wc -lc").format( " | sed '/^\\*/d' | sed '/^\\s*$/d' | wc -lc")
infile) loccmd = loccmd.format(infile)
runcmd = " {} ; {}".format(clangcmd, loccmd) runcmd = " {} ; {}".format(clangcmd, loccmd)
if ARGS['echocmd']: if ARGS['echocmd']:
print(runcmd) print(runcmd)
p = subprocess.Popen( process = subprocess.Popen(
runcmd, shell=True, cwd=key['directory'], stdout=subprocess.PIPE) runcmd, shell=True, cwd=key['directory'], stdout=subprocess.PIPE)
processes.append({'process': p, 'infile': infilename}) processes.append({'process': process, 'infile': infilename,
'outfile': outfilename})
for i, p in enumerate(processes): for i, p in enumerate(processes):
status.print("[{}/{}] Summing up {}".format( status.print("[{}/{}] Summing up {}".format(
i, len(processes), p['infile']), file=out) i, len(processes), p['infile']), file=out)
output, err = p['process'].communicate() output, _ = p['process'].communicate()
expanded, expanded_bytes, loc, in_bytes = list(map(int, output.split())) expanded, expanded_bytes, loc, in_bytes = list(map(int, output.split()))
result.recordFile(p['infile'], loc, in_bytes, expanded, expanded_bytes) result.recordFile(p['infile'], p['outfile'], loc,
in_bytes, expanded, expanded_bytes)
end = time.time() end = time.time()
if ARGS['json']: if ARGS['json']:
......
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