Index: tools/mb/mb.py |
diff --git a/tools/mb/mb.py b/tools/mb/mb.py |
index 2711bf1fc89b795f97ab0264225a5b8cb7f6772f..e54763c0370b0c2fff818d36e2fdb9550c539a91 100755 |
--- a/tools/mb/mb.py |
+++ b/tools/mb/mb.py |
@@ -61,7 +61,7 @@ |
self.DumpInputFiles() |
return ret |
except KeyboardInterrupt: |
- self.Print('interrupted, exiting') |
+ self.Print('interrupted, exiting', stream=sys.stderr) |
return 130 |
except Exception: |
self.DumpInputFiles() |
@@ -229,7 +229,7 @@ |
def DumpContentsOfFilePassedTo(arg_name, path): |
if path and self.Exists(path): |
self.Print("\n# To recreate the file passed to %s:" % arg_name) |
- self.Print("%% cat > %s <<EOF" % path) |
+ self.Print("%% cat > %s <<EOF)" % path) |
contents = self.ReadFile(path) |
self.Print(contents) |
self.Print("EOF\n%\n") |
@@ -643,10 +643,6 @@ |
self.masters = contents['masters'] |
self.mixins = contents['mixins'] |
- def ReadIsolateMap(self): |
- return ast.literal_eval(self.ReadFile(self.PathJoin( |
- self.chromium_src_dir, 'testing', 'buildbot', 'gn_isolate_map.pyl'))) |
- |
def ConfigFromArgs(self): |
if self.args.config: |
if self.args.master or self.args.builder: |
@@ -774,7 +770,7 @@ |
if getattr(self.args, 'swarming_targets_file', None): |
# We need GN to generate the list of runtime dependencies for |
# the compile targets listed (one per line) in the file so |
- # we can run them via swarming. We use gn_isolate_map.pyl to convert |
+ # we can run them via swarming. We use ninja_to_gn.pyl to convert |
# the compile targets to the matching GN labels. |
path = self.args.swarming_targets_file |
if not self.Exists(path): |
@@ -782,14 +778,25 @@ |
output_path=None) |
contents = self.ReadFile(path) |
swarming_targets = set(contents.splitlines()) |
- |
- isolate_map = self.ReadIsolateMap() |
- err, labels = self.MapTargetsToLabels(isolate_map, swarming_targets) |
+ gn_isolate_map = ast.literal_eval(self.ReadFile(self.PathJoin( |
+ self.chromium_src_dir, 'testing', 'buildbot', 'gn_isolate_map.pyl'))) |
+ gn_labels = [] |
+ err = '' |
+ for target in swarming_targets: |
+ target_name = self.GNTargetName(target) |
+ if not target_name in gn_isolate_map: |
+ err += ('test target "%s" not found\n' % target_name) |
+ elif gn_isolate_map[target_name]['type'] == 'unknown': |
+ err += ('test target "%s" type is unknown\n' % target_name) |
+ else: |
+ gn_labels.append(gn_isolate_map[target_name]['label']) |
+ |
if err: |
- raise MBErr(err) |
+ raise MBErr('Error: Failed to match swarming targets to %s:\n%s' % |
+ ('//testing/buildbot/gn_isolate_map.pyl', err)) |
gn_runtime_deps_path = self.ToAbsPath(build_dir, 'runtime_deps') |
- self.WriteFile(gn_runtime_deps_path, '\n'.join(labels) + '\n') |
+ self.WriteFile(gn_runtime_deps_path, '\n'.join(gn_labels) + '\n') |
cmd.append('--runtime-deps-list-file=%s' % gn_runtime_deps_path) |
ret, _, _ = self.Run(cmd) |
@@ -805,21 +812,22 @@ |
# Android targets may be either android_apk or executable. The former |
# will result in runtime_deps associated with the stamp file, while the |
# latter will result in runtime_deps associated with the executable. |
- label = isolate_map[target]['label'] |
+ target_name = self.GNTargetName(target) |
+ label = gn_isolate_map[target_name]['label'] |
runtime_deps_targets = [ |
- target + '.runtime_deps', |
+ target_name + '.runtime_deps', |
'obj/%s.stamp.runtime_deps' % label.replace(':', '/')] |
- elif isolate_map[target]['type'] == 'gpu_browser_test': |
+ elif gn_isolate_map[target]['type'] == 'gpu_browser_test': |
if self.platform == 'win32': |
runtime_deps_targets = ['browser_tests.exe.runtime_deps'] |
else: |
runtime_deps_targets = ['browser_tests.runtime_deps'] |
- elif (isolate_map[target]['type'] == 'script' or |
- isolate_map[target].get('label_type') == 'group'): |
+ elif (gn_isolate_map[target]['type'] == 'script' or |
+ gn_isolate_map[target].get('label_type') == 'group'): |
# For script targets, the build target is usually a group, |
# for which gn generates the runtime_deps next to the stamp file |
# for the label, which lives under the obj/ directory. |
- label = isolate_map[target]['label'] |
+ label = gn_isolate_map[target]['label'] |
runtime_deps_targets = [ |
'obj/%s.stamp.runtime_deps' % label.replace(':', '/')] |
elif self.platform == 'win32': |
@@ -835,7 +843,8 @@ |
raise MBErr('did not generate any of %s' % |
', '.join(runtime_deps_targets)) |
- command, extra_files = self.GetIsolateCommand(target, vals) |
+ command, extra_files = self.GetIsolateCommand(target, vals, |
+ gn_isolate_map) |
runtime_deps = self.ReadFile(runtime_deps_path).splitlines() |
@@ -845,16 +854,15 @@ |
return 0 |
def RunGNIsolate(self, vals): |
+ gn_isolate_map = ast.literal_eval(self.ReadFile(self.PathJoin( |
+ self.chromium_src_dir, 'testing', 'buildbot', 'gn_isolate_map.pyl'))) |
+ |
+ build_dir = self.args.path[0] |
target = self.args.target[0] |
- isolate_map = self.ReadIsolateMap() |
- err, labels = self.MapTargetsToLabels(isolate_map, [target]) |
- if err: |
- raise MBErr(err) |
- label = labels[0] |
- |
- build_dir = self.args.path[0] |
- command, extra_files = self.GetIsolateCommand(target, vals) |
- |
+ target_name = self.GNTargetName(target) |
+ command, extra_files = self.GetIsolateCommand(target, vals, gn_isolate_map) |
+ |
+ label = gn_isolate_map[target_name]['label'] |
cmd = self.GNCmd('desc', build_dir, label, 'runtime_deps') |
ret, out, _ = self.Call(cmd) |
if ret: |
@@ -904,27 +912,6 @@ |
isolate_path + 'd.gen.json', |
) |
- def MapTargetsToLabels(self, isolate_map, targets): |
- labels = [] |
- err = '' |
- for target in targets: |
- if target == 'all': |
- labels.append(target) |
- elif not target in isolate_map: |
- non_run_target = target[:-4] |
- if target.endswith('_run') and non_run_target in isolate_map: |
- labels.append(isolate_map[non_run_target]['label'] + '_run') |
- elif target.startswith('//'): |
- labels.append(target) |
- else: |
- err += ('target "%s" not found in ' |
- '//testing/buildbot/gn_isolate_map.pyl\n' % target) |
- elif isolate_map[target]['type'] == 'unknown': |
- err += ('test target "%s" type is unknown\n' % target) |
- else: |
- labels.append(isolate_map[target]['label']) |
- return err, labels |
- |
def GNCmd(self, subcommand, path, *args): |
if self.platform == 'linux2': |
subdir, exe = 'linux64', 'gn' |
@@ -934,8 +921,8 @@ |
subdir, exe = 'win', 'gn.exe' |
gn_path = self.PathJoin(self.chromium_src_dir, 'buildtools', subdir, exe) |
+ |
return [gn_path, subcommand, path] + list(args) |
- |
def GNArgs(self, vals): |
if vals['cros_passthrough']: |
@@ -1001,7 +988,7 @@ |
return ret |
- def GetIsolateCommand(self, target, vals): |
+ def GetIsolateCommand(self, target, vals, gn_isolate_map): |
android = 'target_os="android"' in vals['gn_args'] |
# This needs to mirror the settings in //build/config/ui.gni: |
@@ -1014,18 +1001,14 @@ |
msan = 'is_msan=true' in vals['gn_args'] |
tsan = 'is_tsan=true' in vals['gn_args'] |
- isolate_map = self.ReadIsolateMap() |
- test_type = isolate_map[target]['type'] |
- |
- executable = isolate_map[target].get('executable', target) |
+ target_name = self.GNTargetName(target) |
+ test_type = gn_isolate_map[target_name]['type'] |
+ |
+ executable = gn_isolate_map[target_name].get('executable', target_name) |
executable_suffix = '.exe' if self.platform == 'win32' else '' |
cmdline = [] |
extra_files = [] |
- |
- if test_type == 'nontest': |
- self.WriteFailureAndRaise('We should not be isolating %s.' % target, |
- output_path=None) |
if android and test_type != "script": |
logdog_command = [ |
@@ -1038,7 +1021,7 @@ |
'--name', 'unified_logcats', |
] |
test_cmdline = [ |
- self.PathJoin('bin', 'run_%s' % target), |
+ self.PathJoin('bin', 'run_%s' % target_name), |
'--logcat-output-file', '${ISOLATED_OUTDIR}/logcats', |
'--target-devices-file', '${SWARMING_BOT_FILE}', |
'-v' |
@@ -1078,7 +1061,7 @@ |
extra_files = [ |
'../../testing/test_env.py' |
] |
- gtest_filter = isolate_map[target]['gtest_filter'] |
+ gtest_filter = gn_isolate_map[target]['gtest_filter'] |
cmdline = [ |
'../../testing/test_env.py', |
'./browser_tests' + executable_suffix, |
@@ -1093,7 +1076,7 @@ |
] |
cmdline = [ |
'../../testing/test_env.py', |
- '../../' + self.ToSrcRelPath(isolate_map[target]['script']) |
+ '../../' + self.ToSrcRelPath(gn_isolate_map[target]['script']) |
] |
elif test_type in ('raw'): |
extra_files = [] |
@@ -1105,7 +1088,7 @@ |
self.WriteFailureAndRaise('No command line for %s found (test type %s).' |
% (target, test_type), output_path=None) |
- cmdline += isolate_map[target].get('args', []) |
+ cmdline += gn_isolate_map[target_name].get('args', []) |
return cmdline, extra_files |
@@ -1201,17 +1184,11 @@ |
return cmd, env |
def RunGNAnalyze(self, vals): |
- # Analyze runs before 'gn gen' now, so we need to run gn gen |
+ # analyze runs before 'gn gen' now, so we need to run gn gen |
# in order to ensure that we have a build directory. |
ret = self.RunGNGen(vals) |
if ret: |
return ret |
- |
- build_path = self.args.path[0] |
- input_path = self.args.input_path[0] |
- gn_input_path = input_path + '.gn' |
- output_path = self.args.output_path[0] |
- gn_output_path = output_path + '.gn' |
inp = self.ReadInputJSON(['files', 'test_targets', |
'additional_compile_targets']) |
@@ -1221,6 +1198,26 @@ |
self.PrintJSON(inp) |
self.Print() |
+ # TODO(crbug.com/555273) - currently GN treats targets and |
+ # additional_compile_targets identically since we can't tell the |
+ # difference between a target that is a group in GN and one that isn't. |
+ # We should eventually fix this and treat the two types differently. |
+ targets = (set(inp['test_targets']) | |
+ set(inp['additional_compile_targets'])) |
+ |
+ output_path = self.args.output_path[0] |
+ |
+ # Bail out early if a GN file was modified, since 'gn refs' won't know |
+ # what to do about it. Also, bail out early if 'all' was asked for, |
+ # since we can't deal with it yet. |
+ if (any(f.endswith('.gn') or f.endswith('.gni') for f in inp['files']) or |
+ 'all' in targets): |
+ self.WriteJSON({ |
+ 'status': 'Found dependency (all)', |
+ 'compile_targets': sorted(targets), |
+ 'test_targets': sorted(targets & set(inp['test_targets'])), |
+ }, output_path) |
+ return 0 |
# This shouldn't normally happen, but could due to unusual race conditions, |
# like a try job that gets scheduled before a patch lands but runs after |
@@ -1234,65 +1231,68 @@ |
}, output_path) |
return 0 |
- gn_inp = {} |
- gn_inp['files'] = ['//' + f for f in inp['files'] if not f.startswith('//')] |
- |
- isolate_map = self.ReadIsolateMap() |
- err, gn_inp['additional_compile_targets'] = self.MapTargetsToLabels( |
- isolate_map, inp['additional_compile_targets']) |
- if err: |
- raise MBErr(err) |
- |
- err, gn_inp['test_targets'] = self.MapTargetsToLabels( |
- isolate_map, inp['test_targets']) |
- if err: |
- raise MBErr(err) |
- labels_to_targets = {} |
- for i, label in enumerate(gn_inp['test_targets']): |
- labels_to_targets[label] = inp['test_targets'][i] |
- |
+ ret = 0 |
+ response_file = self.TempFile() |
+ response_file.write('\n'.join(inp['files']) + '\n') |
+ response_file.close() |
+ |
+ matching_targets = set() |
try: |
- self.WriteJSON(gn_inp, gn_input_path) |
- cmd = self.GNCmd('analyze', build_path, gn_input_path, gn_output_path) |
- ret, _, _ = self.Run(cmd, force_verbose=True) |
- if ret: |
- return ret |
- |
- gn_outp_str = self.ReadFile(gn_output_path) |
- try: |
- gn_outp = json.loads(gn_outp_str) |
- except Exception as e: |
- self.Print("Failed to parse the JSON string GN returned: %s\n%s" |
- % (repr(gn_outp_str), str(e))) |
- raise |
- |
- outp = {} |
- if 'status' in gn_outp: |
- outp['status'] = gn_outp['status'] |
- if 'error' in gn_outp: |
- outp['error'] = gn_outp['error'] |
- if 'invalid_targets' in gn_outp: |
- outp['invalid_targets'] = gn_outp['invalid_targets'] |
- if 'compile_targets' in gn_outp: |
- outp['compile_targets'] = [ |
- label.replace('//', '') for label in gn_outp['compile_targets']] |
- if 'test_targets' in gn_outp: |
- outp['test_targets'] = [ |
- labels_to_targets[label] for label in gn_outp['test_targets']] |
- |
- if self.args.verbose: |
- self.Print() |
- self.Print('analyze output:') |
- self.PrintJSON(outp) |
- self.Print() |
- |
- self.WriteJSON(outp, output_path) |
- |
+ cmd = self.GNCmd('refs', |
+ self.args.path[0], |
+ '@%s' % response_file.name, |
+ '--all', |
+ '--as=output') |
+ ret, out, _ = self.Run(cmd, force_verbose=False) |
+ if ret and not 'The input matches no targets' in out: |
+ self.WriteFailureAndRaise('gn refs returned %d: %s' % (ret, out), |
+ output_path) |
+ build_dir = self.ToSrcRelPath(self.args.path[0]) + self.sep |
+ for output in out.splitlines(): |
+ build_output = output.replace(build_dir, '') |
+ if build_output in targets: |
+ matching_targets.add(build_output) |
+ |
+ cmd = self.GNCmd('refs', |
+ self.args.path[0], |
+ '@%s' % response_file.name, |
+ '--all') |
+ ret, out, _ = self.Run(cmd, force_verbose=False) |
+ if ret and not 'The input matches no targets' in out: |
+ self.WriteFailureAndRaise('gn refs returned %d: %s' % (ret, out), |
+ output_path) |
+ for label in out.splitlines(): |
+ build_target = label[2:] |
+ # We want to accept 'chrome/android:chrome_public_apk' and |
+ # just 'chrome_public_apk'. This may result in too many targets |
+ # getting built, but we can adjust that later if need be. |
+ for input_target in targets: |
+ if (input_target == build_target or |
+ build_target.endswith(':' + input_target)): |
+ matching_targets.add(input_target) |
finally: |
- if self.Exists(gn_input_path): |
- self.RemoveFile(gn_input_path) |
- if self.Exists(gn_output_path): |
- self.RemoveFile(gn_output_path) |
+ self.RemoveFile(response_file.name) |
+ |
+ if matching_targets: |
+ self.WriteJSON({ |
+ 'status': 'Found dependency', |
+ 'compile_targets': sorted(matching_targets), |
+ 'test_targets': sorted(matching_targets & |
+ set(inp['test_targets'])), |
+ }, output_path) |
+ else: |
+ self.WriteJSON({ |
+ 'status': 'No dependency', |
+ 'compile_targets': [], |
+ 'test_targets': [], |
+ }, output_path) |
+ |
+ if self.args.verbose: |
+ outp = json.loads(self.ReadFile(output_path)) |
+ self.Print() |
+ self.Print('analyze output:') |
+ self.PrintJSON(outp) |
+ self.Print() |
return 0 |
@@ -1375,6 +1375,9 @@ |
def PrintJSON(self, obj): |
self.Print(json.dumps(obj, indent=2, sort_keys=True)) |
+ def GNTargetName(self, target): |
+ return target |
+ |
def Build(self, target): |
build_dir = self.ToSrcRelPath(self.args.path[0]) |
ninja_cmd = ['ninja', '-C', build_dir] |