dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 6 | """MB - the Meta-Build wrapper around GN. |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 7 | |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 8 | MB is a wrapper script for GN that can be used to generate build files |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 9 | for sets of canned configurations and analyze them. |
| 10 | """ |
| 11 | |
| 12 | from __future__ import print_function |
| 13 | |
| 14 | import argparse |
| 15 | import ast |
dpranke | c3441d1 | 2015-06-23 23:01:35 | [diff] [blame] | 16 | import errno |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 17 | import json |
| 18 | import os |
dpranke | 68d1cb18 | 2015-09-17 23:30:00 | [diff] [blame] | 19 | import pipes |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 20 | import platform |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 21 | import pprint |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame] | 22 | import re |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 23 | import shutil |
| 24 | import sys |
| 25 | import subprocess |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 26 | import tempfile |
dpranke | bbe6d467 | 2016-04-19 06:56:57 | [diff] [blame] | 27 | import traceback |
dpranke | 867bcf4a | 2016-03-14 22:28:32 | [diff] [blame] | 28 | import urllib2 |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 29 | import zipfile |
dpranke | 867bcf4a | 2016-03-14 22:28:32 | [diff] [blame] | 30 | |
| 31 | from collections import OrderedDict |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 32 | |
dpranke | eca4a78 | 2016-04-14 01:42:38 | [diff] [blame] | 33 | CHROMIUM_SRC_DIR = os.path.dirname(os.path.dirname(os.path.dirname( |
| 34 | os.path.abspath(__file__)))) |
| 35 | sys.path = [os.path.join(CHROMIUM_SRC_DIR, 'build')] + sys.path |
| 36 | |
| 37 | import gn_helpers |
| 38 | |
Karen Qian | 92ffd1a | 2019-09-11 01:09:23 | [diff] [blame] | 39 | def PruneVirtualEnv(): |
| 40 | # Set by VirtualEnv, no need to keep it. |
| 41 | os.environ.pop('VIRTUAL_ENV', None) |
| 42 | |
| 43 | # Set by VPython, if scripts want it back they have to set it explicitly. |
| 44 | os.environ.pop('PYTHONNOUSERSITE', None) |
| 45 | |
| 46 | # Look for "activate_this.py" in this path, which is installed by VirtualEnv. |
| 47 | # This mechanism is used by vpython as well to sanitize VirtualEnvs from |
| 48 | # $PATH. |
| 49 | os.environ['PATH'] = os.pathsep.join([ |
| 50 | p for p in os.environ.get('PATH', '').split(os.pathsep) |
| 51 | if not os.path.isfile(os.path.join(p, 'activate_this.py')) |
| 52 | ]) |
| 53 | |
dpranke | eca4a78 | 2016-04-14 01:42:38 | [diff] [blame] | 54 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 55 | def main(args): |
Karen Qian | 92ffd1a | 2019-09-11 01:09:23 | [diff] [blame] | 56 | # Prune all evidence of VPython/VirtualEnv out of the environment. This means |
| 57 | # that we 'unwrap' vpython VirtualEnv path/env manipulation. Invocations of |
| 58 | # `python` from GN should never inherit the gn.py's own VirtualEnv. This also |
| 59 | # helps to ensure that generated ninja files do not reference python.exe from |
| 60 | # the VirtualEnv generated from depot_tools' own .vpython file (or lack |
| 61 | # thereof), but instead reference the default python from the PATH. |
| 62 | PruneVirtualEnv() |
| 63 | |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 64 | mbw = MetaBuildWrapper() |
dpranke | 255085e | 2016-03-16 05:23:59 | [diff] [blame] | 65 | return mbw.Main(args) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 66 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 67 | class MetaBuildWrapper(object): |
| 68 | def __init__(self): |
dpranke | eca4a78 | 2016-04-14 01:42:38 | [diff] [blame] | 69 | self.chromium_src_dir = CHROMIUM_SRC_DIR |
| 70 | self.default_config = os.path.join(self.chromium_src_dir, 'tools', 'mb', |
| 71 | 'mb_config.pyl') |
kjellander | 902bcb6 | 2016-10-26 06:20:50 | [diff] [blame] | 72 | self.default_isolate_map = os.path.join(self.chromium_src_dir, 'testing', |
| 73 | 'buildbot', 'gn_isolate_map.pyl') |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 74 | self.executable = sys.executable |
dpranke | d1fba48 | 2015-04-14 20:54:51 | [diff] [blame] | 75 | self.platform = sys.platform |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 76 | self.sep = os.sep |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 77 | self.args = argparse.Namespace() |
| 78 | self.configs = {} |
| 79 | self.masters = {} |
| 80 | self.mixins = {} |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 81 | |
dpranke | 255085e | 2016-03-16 05:23:59 | [diff] [blame] | 82 | def Main(self, args): |
| 83 | self.ParseArgs(args) |
| 84 | try: |
| 85 | ret = self.args.func() |
| 86 | if ret: |
| 87 | self.DumpInputFiles() |
| 88 | return ret |
| 89 | except KeyboardInterrupt: |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 90 | self.Print('interrupted, exiting') |
dpranke | 255085e | 2016-03-16 05:23:59 | [diff] [blame] | 91 | return 130 |
dpranke | bbe6d467 | 2016-04-19 06:56:57 | [diff] [blame] | 92 | except Exception: |
dpranke | 255085e | 2016-03-16 05:23:59 | [diff] [blame] | 93 | self.DumpInputFiles() |
dpranke | bbe6d467 | 2016-04-19 06:56:57 | [diff] [blame] | 94 | s = traceback.format_exc() |
| 95 | for l in s.splitlines(): |
| 96 | self.Print(l) |
dpranke | 255085e | 2016-03-16 05:23:59 | [diff] [blame] | 97 | return 1 |
| 98 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 99 | def ParseArgs(self, argv): |
| 100 | def AddCommonOptions(subp): |
| 101 | subp.add_argument('-b', '--builder', |
| 102 | help='builder name to look up config from') |
| 103 | subp.add_argument('-m', '--master', |
| 104 | help='master name to look up config from') |
| 105 | subp.add_argument('-c', '--config', |
| 106 | help='configuration to analyze') |
shenghuazhang | 804b2154 | 2016-10-11 02:06:49 | [diff] [blame] | 107 | subp.add_argument('--phase', |
| 108 | help='optional phase name (used when builders ' |
| 109 | 'do multiple compiles with different ' |
| 110 | 'arguments in a single build)') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 111 | subp.add_argument('-f', '--config-file', metavar='PATH', |
| 112 | default=self.default_config, |
| 113 | help='path to config file ' |
kjellander | 902bcb6 | 2016-10-26 06:20:50 | [diff] [blame] | 114 | '(default is %(default)s)') |
| 115 | subp.add_argument('-i', '--isolate-map-file', metavar='PATH', |
kjellander | 902bcb6 | 2016-10-26 06:20:50 | [diff] [blame] | 116 | help='path to isolate map file ' |
Zhiling Huang | 6695846 | 2018-02-03 00:28:20 | [diff] [blame] | 117 | '(default is %(default)s)', |
| 118 | default=[], |
| 119 | action='append', |
| 120 | dest='isolate_map_files') |
dpranke | d0c138b | 2016-04-13 18:28:47 | [diff] [blame] | 121 | subp.add_argument('-g', '--goma-dir', |
| 122 | help='path to goma directory') |
agrieve | 41d21a7 | 2016-04-14 18:02:26 | [diff] [blame] | 123 | subp.add_argument('--android-version-code', |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 124 | help='Sets GN arg android_default_version_code') |
agrieve | 41d21a7 | 2016-04-14 18:02:26 | [diff] [blame] | 125 | subp.add_argument('--android-version-name', |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 126 | help='Sets GN arg android_default_version_name') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 127 | subp.add_argument('-n', '--dryrun', action='store_true', |
| 128 | help='Do a dry run (i.e., do nothing, just print ' |
| 129 | 'the commands that will run)') |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 130 | subp.add_argument('-v', '--verbose', action='store_true', |
| 131 | help='verbose logging') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 132 | |
Stephen Martinis | b40a685 | 2019-07-23 01:48:30 | [diff] [blame] | 133 | parser = argparse.ArgumentParser( |
| 134 | prog='mb', description='mb (meta-build) is a python wrapper around GN. ' |
| 135 | 'See the user guide in ' |
| 136 | '//tools/mb/docs/user_guide.md for detailed usage ' |
| 137 | 'instructions.') |
| 138 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 139 | subps = parser.add_subparsers() |
| 140 | |
| 141 | subp = subps.add_parser('analyze', |
Stephen Martinis | 239c35a | 2019-07-22 19:34:40 | [diff] [blame] | 142 | description='Analyze whether changes to a set of ' |
| 143 | 'files will cause a set of binaries to ' |
| 144 | 'be rebuilt.') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 145 | AddCommonOptions(subp) |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 146 | subp.add_argument('path', |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 147 | help='path build was generated into.') |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 148 | subp.add_argument('input_path', |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 149 | help='path to a file containing the input arguments ' |
| 150 | 'as a JSON object.') |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 151 | subp.add_argument('output_path', |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 152 | help='path to a file containing the output arguments ' |
| 153 | 'as a JSON object.') |
Debrian Figueroa | ae51d0d | 2019-07-22 18:04:11 | [diff] [blame] | 154 | subp.add_argument('--json-output', |
Debrian Figueroa | ae58223 | 2019-07-17 01:54:45 | [diff] [blame] | 155 | help='Write errors to json.output') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 156 | subp.set_defaults(func=self.CmdAnalyze) |
| 157 | |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 158 | subp = subps.add_parser('export', |
Stephen Martinis | 239c35a | 2019-07-22 19:34:40 | [diff] [blame] | 159 | description='Print out the expanded configuration ' |
| 160 | 'for each builder as a JSON object.') |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 161 | subp.add_argument('-f', '--config-file', metavar='PATH', |
| 162 | default=self.default_config, |
kjellander | 902bcb6 | 2016-10-26 06:20:50 | [diff] [blame] | 163 | help='path to config file (default is %(default)s)') |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 164 | subp.add_argument('-g', '--goma-dir', |
| 165 | help='path to goma directory') |
| 166 | subp.set_defaults(func=self.CmdExport) |
| 167 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 168 | subp = subps.add_parser('gen', |
Stephen Martinis | 239c35a | 2019-07-22 19:34:40 | [diff] [blame] | 169 | description='Generate a new set of build files.') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 170 | AddCommonOptions(subp) |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 171 | subp.add_argument('--swarming-targets-file', |
Erik Chen | b706824 | 2019-11-27 21:52:04 | [diff] [blame^] | 172 | help='generates runtime dependencies for targets listed ' |
| 173 | 'in file as .isolate and .isolated.gen.json files. ' |
| 174 | 'Targets should be listed by name, separated by ' |
| 175 | 'newline.') |
Debrian Figueroa | ae51d0d | 2019-07-22 18:04:11 | [diff] [blame] | 176 | subp.add_argument('--json-output', |
Debrian Figueroa | ae58223 | 2019-07-17 01:54:45 | [diff] [blame] | 177 | help='Write errors to json.output') |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 178 | subp.add_argument('path', |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 179 | help='path to generate build into') |
| 180 | subp.set_defaults(func=self.CmdGen) |
| 181 | |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 182 | subp = subps.add_parser('isolate-everything', |
Stephen Martinis | 239c35a | 2019-07-22 19:34:40 | [diff] [blame] | 183 | description='Generates a .isolate for all targets. ' |
| 184 | 'Requires that mb.py gen has already ' |
| 185 | 'been run.') |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 186 | AddCommonOptions(subp) |
| 187 | subp.set_defaults(func=self.CmdIsolateEverything) |
| 188 | subp.add_argument('path', |
| 189 | help='path build was generated into') |
| 190 | |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 191 | subp = subps.add_parser('isolate', |
Stephen Martinis | 239c35a | 2019-07-22 19:34:40 | [diff] [blame] | 192 | description='Generate the .isolate files for a ' |
| 193 | 'given binary.') |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 194 | AddCommonOptions(subp) |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 195 | subp.add_argument('--no-build', dest='build', default=True, |
| 196 | action='store_false', |
| 197 | help='Do not build, just isolate') |
| 198 | subp.add_argument('-j', '--jobs', type=int, |
| 199 | help='Number of jobs to pass to ninja') |
| 200 | subp.add_argument('path', |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 201 | help='path build was generated into') |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 202 | subp.add_argument('target', |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 203 | help='ninja target to generate the isolate for') |
| 204 | subp.set_defaults(func=self.CmdIsolate) |
| 205 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 206 | subp = subps.add_parser('lookup', |
Stephen Martinis | 239c35a | 2019-07-22 19:34:40 | [diff] [blame] | 207 | description='Look up the command for a given ' |
| 208 | 'config or builder.') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 209 | AddCommonOptions(subp) |
Garrett Beaty | b6cee04 | 2019-04-22 18:42:09 | [diff] [blame] | 210 | subp.add_argument('--quiet', default=False, action='store_true', |
| 211 | help='Print out just the arguments, ' |
| 212 | 'do not emulate the output of the gen subcommand.') |
| 213 | subp.add_argument('--recursive', default=False, action='store_true', |
| 214 | help='Lookup arguments from imported files, ' |
| 215 | 'implies --quiet') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 216 | subp.set_defaults(func=self.CmdLookup) |
| 217 | |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 218 | subp = subps.add_parser('try', |
| 219 | description='Try your change on a remote builder') |
| 220 | AddCommonOptions(subp) |
| 221 | subp.add_argument('target', |
| 222 | help='ninja target to build and run') |
Stephen Martinis | 3016084b | 2019-11-20 20:26:22 | [diff] [blame] | 223 | subp.add_argument('--force', default=False, action='store_true', |
| 224 | help='Force the job to run. Ignores local checkout state;' |
| 225 | ' by default, the tool doesn\'t trigger jobs if there are' |
| 226 | ' local changes which are not present on Gerrit.') |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 227 | subp.set_defaults(func=self.CmdTry) |
| 228 | |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 229 | subp = subps.add_parser( |
Stephen Martinis | 239c35a | 2019-07-22 19:34:40 | [diff] [blame] | 230 | 'run', formatter_class=argparse.RawDescriptionHelpFormatter) |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 231 | subp.description = ( |
| 232 | 'Build, isolate, and run the given binary with the command line\n' |
| 233 | 'listed in the isolate. You may pass extra arguments after the\n' |
| 234 | 'target; use "--" if the extra arguments need to include switches.\n' |
| 235 | '\n' |
| 236 | 'Examples:\n' |
| 237 | '\n' |
| 238 | ' % tools/mb/mb.py run -m chromium.linux -b "Linux Builder" \\\n' |
| 239 | ' //out/Default content_browsertests\n' |
| 240 | '\n' |
| 241 | ' % tools/mb/mb.py run out/Default content_browsertests\n' |
| 242 | '\n' |
| 243 | ' % tools/mb/mb.py run out/Default content_browsertests -- \\\n' |
| 244 | ' --test-launcher-retry-limit=0' |
| 245 | '\n' |
| 246 | ) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 247 | AddCommonOptions(subp) |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 248 | subp.add_argument('-j', '--jobs', type=int, |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 249 | help='Number of jobs to pass to ninja') |
| 250 | subp.add_argument('--no-build', dest='build', default=True, |
| 251 | action='store_false', |
| 252 | help='Do not build, just isolate and run') |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 253 | subp.add_argument('path', |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 254 | help=('path to generate build into (or use).' |
| 255 | ' This can be either a regular path or a ' |
| 256 | 'GN-style source-relative path like ' |
| 257 | '//out/Default.')) |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 258 | subp.add_argument('-s', '--swarmed', action='store_true', |
| 259 | help='Run under swarming with the default dimensions') |
| 260 | subp.add_argument('-d', '--dimension', default=[], action='append', nargs=2, |
| 261 | dest='dimensions', metavar='FOO bar', |
| 262 | help='dimension to filter on') |
| 263 | subp.add_argument('--no-default-dimensions', action='store_false', |
| 264 | dest='default_dimensions', default=True, |
| 265 | help='Do not automatically add dimensions to the task') |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 266 | subp.add_argument('target', |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 267 | help='ninja target to build and run') |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 268 | subp.add_argument('extra_args', nargs='*', |
| 269 | help=('extra args to pass to the isolate to run. Use ' |
| 270 | '"--" as the first arg if you need to pass ' |
| 271 | 'switches')) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 272 | subp.set_defaults(func=self.CmdRun) |
| 273 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 274 | subp = subps.add_parser('validate', |
Stephen Martinis | 239c35a | 2019-07-22 19:34:40 | [diff] [blame] | 275 | description='Validate the config file.') |
dpranke | a5a77ca | 2015-07-16 23:24:17 | [diff] [blame] | 276 | subp.add_argument('-f', '--config-file', metavar='PATH', |
| 277 | default=self.default_config, |
kjellander | 902bcb6 | 2016-10-26 06:20:50 | [diff] [blame] | 278 | help='path to config file (default is %(default)s)') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 279 | subp.set_defaults(func=self.CmdValidate) |
| 280 | |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 281 | subp = subps.add_parser('zip', |
Stephen Martinis | 239c35a | 2019-07-22 19:34:40 | [diff] [blame] | 282 | description='Generate a .zip containing the files ' |
| 283 | 'needed for a given binary.') |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 284 | AddCommonOptions(subp) |
| 285 | subp.add_argument('--no-build', dest='build', default=True, |
| 286 | action='store_false', |
| 287 | help='Do not build, just isolate') |
| 288 | subp.add_argument('-j', '--jobs', type=int, |
| 289 | help='Number of jobs to pass to ninja') |
| 290 | subp.add_argument('path', |
| 291 | help='path build was generated into') |
| 292 | subp.add_argument('target', |
| 293 | help='ninja target to generate the isolate for') |
| 294 | subp.add_argument('zip_path', |
| 295 | help='path to zip file to create') |
| 296 | subp.set_defaults(func=self.CmdZip) |
| 297 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 298 | subp = subps.add_parser('help', |
| 299 | help='Get help on a subcommand.') |
| 300 | subp.add_argument(nargs='?', action='store', dest='subcommand', |
| 301 | help='The command to get help for.') |
| 302 | subp.set_defaults(func=self.CmdHelp) |
| 303 | |
| 304 | self.args = parser.parse_args(argv) |
| 305 | |
dpranke | b2be10a | 2016-02-22 17:11:00 | [diff] [blame] | 306 | def DumpInputFiles(self): |
| 307 | |
dpranke | f7b7eb7a | 2016-03-28 22:42:59 | [diff] [blame] | 308 | def DumpContentsOfFilePassedTo(arg_name, path): |
dpranke | b2be10a | 2016-02-22 17:11:00 | [diff] [blame] | 309 | if path and self.Exists(path): |
dpranke | f7b7eb7a | 2016-03-28 22:42:59 | [diff] [blame] | 310 | self.Print("\n# To recreate the file passed to %s:" % arg_name) |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 311 | self.Print("%% cat > %s <<EOF" % path) |
dpranke | b2be10a | 2016-02-22 17:11:00 | [diff] [blame] | 312 | contents = self.ReadFile(path) |
dpranke | f7b7eb7a | 2016-03-28 22:42:59 | [diff] [blame] | 313 | self.Print(contents) |
| 314 | self.Print("EOF\n%\n") |
dpranke | b2be10a | 2016-02-22 17:11:00 | [diff] [blame] | 315 | |
dpranke | f7b7eb7a | 2016-03-28 22:42:59 | [diff] [blame] | 316 | if getattr(self.args, 'input_path', None): |
| 317 | DumpContentsOfFilePassedTo( |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 318 | 'argv[0] (input_path)', self.args.input_path) |
dpranke | f7b7eb7a | 2016-03-28 22:42:59 | [diff] [blame] | 319 | if getattr(self.args, 'swarming_targets_file', None): |
| 320 | DumpContentsOfFilePassedTo( |
| 321 | '--swarming-targets-file', self.args.swarming_targets_file) |
dpranke | b2be10a | 2016-02-22 17:11:00 | [diff] [blame] | 322 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 323 | def CmdAnalyze(self): |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 324 | vals = self.Lookup() |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 325 | return self.RunGNAnalyze(vals) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 326 | |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 327 | def CmdExport(self): |
| 328 | self.ReadConfigFile() |
| 329 | obj = {} |
| 330 | for master, builders in self.masters.items(): |
| 331 | obj[master] = {} |
| 332 | for builder in builders: |
| 333 | config = self.masters[master][builder] |
| 334 | if not config: |
| 335 | continue |
| 336 | |
shenghuazhang | 804b2154 | 2016-10-11 02:06:49 | [diff] [blame] | 337 | if isinstance(config, dict): |
| 338 | args = {k: self.FlattenConfig(v)['gn_args'] |
| 339 | for k, v in config.items()} |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 340 | elif config.startswith('//'): |
| 341 | args = config |
| 342 | else: |
| 343 | args = self.FlattenConfig(config)['gn_args'] |
| 344 | if 'error' in args: |
| 345 | continue |
| 346 | |
| 347 | obj[master][builder] = args |
| 348 | |
| 349 | # Dump object and trim trailing whitespace. |
| 350 | s = '\n'.join(l.rstrip() for l in |
| 351 | json.dumps(obj, sort_keys=True, indent=2).splitlines()) |
| 352 | self.Print(s) |
| 353 | return 0 |
| 354 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 355 | def CmdGen(self): |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 356 | vals = self.Lookup() |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 357 | return self.RunGNGen(vals) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 358 | |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 359 | def CmdIsolateEverything(self): |
| 360 | vals = self.Lookup() |
| 361 | return self.RunGNGenAllIsolates(vals) |
| 362 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 363 | def CmdHelp(self): |
| 364 | if self.args.subcommand: |
| 365 | self.ParseArgs([self.args.subcommand, '--help']) |
| 366 | else: |
| 367 | self.ParseArgs(['--help']) |
| 368 | |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 369 | def CmdIsolate(self): |
| 370 | vals = self.GetConfig() |
| 371 | if not vals: |
| 372 | return 1 |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 373 | if self.args.build: |
| 374 | ret = self.Build(self.args.target) |
| 375 | if ret: |
| 376 | return ret |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 377 | return self.RunGNIsolate(vals) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 378 | |
| 379 | def CmdLookup(self): |
| 380 | vals = self.Lookup() |
Garrett Beaty | b6cee04 | 2019-04-22 18:42:09 | [diff] [blame] | 381 | gn_args = self.GNArgs(vals, expand_imports=self.args.recursive) |
| 382 | if self.args.quiet or self.args.recursive: |
| 383 | self.Print(gn_args, end='') |
| 384 | else: |
| 385 | cmd = self.GNCmd('gen', '_path_') |
| 386 | self.Print('\nWriting """\\\n%s""" to _path_/args.gn.\n' % gn_args) |
| 387 | env = None |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 388 | |
Garrett Beaty | b6cee04 | 2019-04-22 18:42:09 | [diff] [blame] | 389 | self.PrintCmd(cmd, env) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 390 | return 0 |
| 391 | |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 392 | def CmdTry(self): |
Stephen Martinis | 9388ffc | 2019-10-19 00:15:08 | [diff] [blame] | 393 | ninja_target = self.args.target |
| 394 | if ninja_target.startswith('//'): |
Stephen Martinis | 3016084b | 2019-11-20 20:26:22 | [diff] [blame] | 395 | self.Print("Expected a ninja target like base_unittests, got %s" % ( |
| 396 | ninja_target)) |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 397 | return 1 |
| 398 | |
Stephen Martinis | 3016084b | 2019-11-20 20:26:22 | [diff] [blame] | 399 | _, out, _ = self.Run(['git', 'cl', 'diff', '--stat'], force_verbose=False) |
| 400 | if out: |
| 401 | self.Print("Your checkout appears to local changes which are not uploaded" |
| 402 | " to Gerrit. Changes must be committed and uploaded to Gerrit" |
| 403 | " to be tested using this tool.") |
| 404 | if not self.args.force: |
| 405 | return 1 |
| 406 | |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 407 | json_path = self.PathJoin(self.chromium_src_dir, 'out.json') |
| 408 | try: |
| 409 | ret, out, err = self.Run( |
| 410 | ['git', 'cl', 'issue', '--json=out.json'], force_verbose=False) |
| 411 | if ret != 0: |
| 412 | self.Print( |
| 413 | "Unable to fetch current issue. Output and error:\n%s\n%s" % ( |
| 414 | out, err |
| 415 | )) |
| 416 | return ret |
| 417 | with open(json_path) as f: |
| 418 | issue_data = json.load(f) |
| 419 | finally: |
| 420 | if self.Exists(json_path): |
| 421 | os.unlink(json_path) |
| 422 | |
| 423 | if not issue_data['issue']: |
| 424 | self.Print("Missing issue data. Upload your CL to Gerrit and try again.") |
| 425 | return 1 |
| 426 | |
| 427 | def run_cmd(previous_res, cmd): |
| 428 | res, out, err = self.Run(cmd, force_verbose=False, stdin=previous_res) |
| 429 | if res != 0: |
| 430 | self.Print("Err while running", cmd) |
| 431 | self.Print("Output", out) |
| 432 | raise Exception(err) |
| 433 | return out |
| 434 | |
| 435 | result = LedResult(None, run_cmd).then( |
| 436 | # TODO(martiniss): maybe don't always assume the bucket? |
| 437 | 'led', 'get-builder', 'luci.chromium.try:%s' % self.args.builder).then( |
| 438 | 'led', 'edit', '-r', 'chromium_trybot_experimental', |
Stephen Martinis | 9388ffc | 2019-10-19 00:15:08 | [diff] [blame] | 439 | '-p', 'tests=["%s"]' % ninja_target).then( |
Stephen Martinis | 24dbfb2 | 2019-10-21 21:30:03 | [diff] [blame] | 440 | 'led', 'edit-system', '--tag=purpose:user-debug-mb-try').then( |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 441 | 'led', 'edit-cr-cl', issue_data['issue_url']).then( |
| 442 | 'led', 'launch').result |
| 443 | |
| 444 | swarming_data = json.loads(result)['swarming'] |
| 445 | self.Print("Launched task at https://%s/task?id=%s" % ( |
| 446 | swarming_data['host_name'], swarming_data['task_id'])) |
| 447 | |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 448 | def CmdRun(self): |
| 449 | vals = self.GetConfig() |
| 450 | if not vals: |
| 451 | return 1 |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 452 | if self.args.build: |
Dirk Pranke | 5f22a82 | 2019-05-23 22:55:25 | [diff] [blame] | 453 | self.Print('') |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 454 | ret = self.Build(self.args.target) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 455 | if ret: |
| 456 | return ret |
Dirk Pranke | 5f22a82 | 2019-05-23 22:55:25 | [diff] [blame] | 457 | |
| 458 | self.Print('') |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 459 | ret = self.RunGNIsolate(vals) |
| 460 | if ret: |
| 461 | return ret |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 462 | |
Dirk Pranke | 5f22a82 | 2019-05-23 22:55:25 | [diff] [blame] | 463 | self.Print('') |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 464 | if self.args.swarmed: |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 465 | return self._RunUnderSwarming(self.args.path, self.args.target) |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 466 | else: |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 467 | return self._RunLocallyIsolated(self.args.path, self.args.target) |
| 468 | |
| 469 | def CmdZip(self): |
Yun Liu | c0f2f73 | 2019-09-18 17:06:31 | [diff] [blame] | 470 | ret = self.CmdIsolate() |
| 471 | if ret: |
| 472 | return ret |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 473 | |
Yun Liu | c0f2f73 | 2019-09-18 17:06:31 | [diff] [blame] | 474 | zip_dir = None |
| 475 | try: |
| 476 | zip_dir = self.TempDir() |
| 477 | remap_cmd = [ |
| 478 | self.executable, |
| 479 | self.PathJoin(self.chromium_src_dir, 'tools', 'swarming_client', |
| 480 | 'isolate.py'), 'remap', '--collapse_symlinks', '-s', |
| 481 | self.PathJoin(self.args.path, self.args.target + '.isolated'), '-o', |
| 482 | zip_dir |
| 483 | ] |
| 484 | self.Run(remap_cmd) |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 485 | |
Yun Liu | c0f2f73 | 2019-09-18 17:06:31 | [diff] [blame] | 486 | zip_path = self.args.zip_path |
| 487 | with zipfile.ZipFile( |
| 488 | zip_path, 'w', zipfile.ZIP_DEFLATED, allowZip64=True) as fp: |
| 489 | for root, _, files in os.walk(zip_dir): |
| 490 | for filename in files: |
| 491 | path = self.PathJoin(root, filename) |
| 492 | fp.write(path, self.RelPath(path, zip_dir)) |
| 493 | finally: |
| 494 | if zip_dir: |
| 495 | self.RemoveDirectory(zip_dir) |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 496 | |
Robert Iannucci | 5a9d75f6 | 2018-03-02 05:28:20 | [diff] [blame] | 497 | @staticmethod |
| 498 | def _AddBaseSoftware(cmd): |
| 499 | # HACK(iannucci): These packages SHOULD NOT BE HERE. |
| 500 | # Remove method once Swarming Pool Task Templates are implemented. |
| 501 | # crbug.com/812428 |
| 502 | |
| 503 | # Add in required base software. This should be kept in sync with the |
John Budorick | 9d917537 | 2019-04-01 19:04:24 | [diff] [blame] | 504 | # `chromium_swarming` recipe module in build.git. All references to |
| 505 | # `swarming_module` below are purely due to this. |
Robert Iannucci | 5a9d75f6 | 2018-03-02 05:28:20 | [diff] [blame] | 506 | cipd_packages = [ |
| 507 | ('infra/python/cpython/${platform}', |
smut | 22dcd68e | 2019-06-25 23:33:27 | [diff] [blame] | 508 | 'version:2.7.15.chromium14'), |
Robert Iannucci | 5a9d75f6 | 2018-03-02 05:28:20 | [diff] [blame] | 509 | ('infra/tools/luci/logdog/butler/${platform}', |
| 510 | 'git_revision:e1abc57be62d198b5c2f487bfb2fa2d2eb0e867c'), |
| 511 | ('infra/tools/luci/vpython-native/${platform}', |
Andrii Shyshkalov | b35c4cb | 2019-10-24 03:16:24 | [diff] [blame] | 512 | 'git_revision:e317c7d2c17d4c3460ee37524dfce4e1dee4306a'), |
Robert Iannucci | 5a9d75f6 | 2018-03-02 05:28:20 | [diff] [blame] | 513 | ('infra/tools/luci/vpython/${platform}', |
Andrii Shyshkalov | b35c4cb | 2019-10-24 03:16:24 | [diff] [blame] | 514 | 'git_revision:e317c7d2c17d4c3460ee37524dfce4e1dee4306a'), |
Robert Iannucci | 5a9d75f6 | 2018-03-02 05:28:20 | [diff] [blame] | 515 | ] |
| 516 | for pkg, vers in cipd_packages: |
| 517 | cmd.append('--cipd-package=.swarming_module:%s:%s' % (pkg, vers)) |
| 518 | |
| 519 | # Add packages to $PATH |
| 520 | cmd.extend([ |
| 521 | '--env-prefix=PATH', '.swarming_module', |
| 522 | '--env-prefix=PATH', '.swarming_module/bin', |
| 523 | ]) |
| 524 | |
| 525 | # Add cache directives for vpython. |
| 526 | vpython_cache_path = '.swarming_module_cache/vpython' |
| 527 | cmd.extend([ |
| 528 | '--named-cache=swarming_module_cache_vpython', vpython_cache_path, |
| 529 | '--env-prefix=VPYTHON_VIRTUALENV_ROOT', vpython_cache_path, |
| 530 | ]) |
| 531 | |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 532 | def _RunUnderSwarming(self, build_dir, target): |
Marc-Antoine Ruel | 559cc473 | 2019-03-19 22:20:46 | [diff] [blame] | 533 | isolate_server = 'isolateserver.appspot.com' |
| 534 | namespace = 'default-gzip' |
| 535 | swarming_server = 'chromium-swarm.appspot.com' |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 536 | # TODO(dpranke): Look up the information for the target in |
| 537 | # the //testing/buildbot.json file, if possible, so that we |
| 538 | # can determine the isolate target, command line, and additional |
| 539 | # swarming parameters, if possible. |
| 540 | # |
| 541 | # TODO(dpranke): Also, add support for sharding and merging results. |
| 542 | dimensions = [] |
| 543 | for k, v in self._DefaultDimensions() + self.args.dimensions: |
| 544 | dimensions += ['-d', k, v] |
| 545 | |
| 546 | cmd = [ |
| 547 | self.executable, |
| 548 | self.PathJoin('tools', 'swarming_client', 'isolate.py'), |
| 549 | 'archive', |
Marc-Antoine Ruel | 559cc473 | 2019-03-19 22:20:46 | [diff] [blame] | 550 | '-s', self.ToSrcRelPath('%s/%s.isolated' % (build_dir, target)), |
| 551 | '-I', isolate_server, |
| 552 | '--namespace', namespace, |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 553 | ] |
Dirk Pranke | 5f22a82 | 2019-05-23 22:55:25 | [diff] [blame] | 554 | |
| 555 | # Talking to the isolateserver may fail because we're not logged in. |
| 556 | # We trap the command explicitly and rewrite the error output so that |
| 557 | # the error message is actually correct for a Chromium check out. |
| 558 | self.PrintCmd(cmd, env=None) |
| 559 | ret, out, err = self.Run(cmd, force_verbose=False) |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 560 | if ret: |
Dirk Pranke | 5f22a82 | 2019-05-23 22:55:25 | [diff] [blame] | 561 | self.Print(' -> returned %d' % ret) |
| 562 | if out: |
| 563 | self.Print(out, end='') |
| 564 | if err: |
| 565 | # The swarming client will return an exit code of 2 (via |
| 566 | # argparse.ArgumentParser.error()) and print a message to indicate |
| 567 | # that auth failed, so we have to parse the message to check. |
| 568 | if (ret == 2 and 'Please login to' in err): |
| 569 | err = err.replace(' auth.py', ' tools/swarming_client/auth.py') |
| 570 | self.Print(err, end='', file=sys.stderr) |
| 571 | |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 572 | return ret |
| 573 | |
| 574 | isolated_hash = out.splitlines()[0].split()[0] |
| 575 | cmd = [ |
| 576 | self.executable, |
| 577 | self.PathJoin('tools', 'swarming_client', 'swarming.py'), |
| 578 | 'run', |
| 579 | '-s', isolated_hash, |
Marc-Antoine Ruel | 559cc473 | 2019-03-19 22:20:46 | [diff] [blame] | 580 | '-I', isolate_server, |
| 581 | '--namespace', namespace, |
| 582 | '-S', swarming_server, |
Stephen Martinis | 43ab303 | 2019-09-11 20:07:41 | [diff] [blame] | 583 | '--tags=purpose:user-debug-mb', |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 584 | ] + dimensions |
Robert Iannucci | 5a9d75f6 | 2018-03-02 05:28:20 | [diff] [blame] | 585 | self._AddBaseSoftware(cmd) |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 586 | if self.args.extra_args: |
| 587 | cmd += ['--'] + self.args.extra_args |
Dirk Pranke | 5f22a82 | 2019-05-23 22:55:25 | [diff] [blame] | 588 | self.Print('') |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 589 | ret, _, _ = self.Run(cmd, force_verbose=True, buffer_output=False) |
| 590 | return ret |
| 591 | |
| 592 | def _RunLocallyIsolated(self, build_dir, target): |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 593 | cmd = [ |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 594 | self.executable, |
| 595 | self.PathJoin('tools', 'swarming_client', 'isolate.py'), |
| 596 | 'run', |
| 597 | '-s', |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 598 | self.ToSrcRelPath('%s/%s.isolated' % (build_dir, target)), |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 599 | ] |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 600 | if self.args.extra_args: |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 601 | cmd += ['--'] + self.args.extra_args |
| 602 | ret, _, _ = self.Run(cmd, force_verbose=True, buffer_output=False) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 603 | return ret |
| 604 | |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 605 | def _DefaultDimensions(self): |
| 606 | if not self.args.default_dimensions: |
| 607 | return [] |
| 608 | |
| 609 | # This code is naive and just picks reasonable defaults per platform. |
| 610 | if self.platform == 'darwin': |
Mike Meade | d12fd0f | 2018-04-10 01:02:40 | [diff] [blame] | 611 | os_dim = ('os', 'Mac-10.13') |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 612 | elif self.platform.startswith('linux'): |
Takuto Ikuta | 169663b | 2019-08-05 16:21:32 | [diff] [blame] | 613 | os_dim = ('os', 'Ubuntu-16.04') |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 614 | elif self.platform == 'win32': |
Mike Meade | d12fd0f | 2018-04-10 01:02:40 | [diff] [blame] | 615 | os_dim = ('os', 'Windows-10') |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 616 | else: |
| 617 | raise MBErr('unrecognized platform string "%s"' % self.platform) |
| 618 | |
John Budorick | 9cf2d4c6 | 2019-11-11 23:56:12 | [diff] [blame] | 619 | return [('pool', 'chromium.tests'), |
Dirk Pranke | 8cb6aa78 | 2017-12-16 02:31:33 | [diff] [blame] | 620 | ('cpu', 'x86-64'), |
| 621 | os_dim] |
| 622 | |
dpranke | 0cafc16 | 2016-03-19 00:41:10 | [diff] [blame] | 623 | def CmdValidate(self, print_ok=True): |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 624 | errs = [] |
| 625 | |
| 626 | # Read the file to make sure it parses. |
| 627 | self.ReadConfigFile() |
| 628 | |
dpranke | 3be0014 | 2016-03-17 22:46:04 | [diff] [blame] | 629 | # Build a list of all of the configs referenced by builders. |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 630 | all_configs = {} |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 631 | for master in self.masters: |
dpranke | 3be0014 | 2016-03-17 22:46:04 | [diff] [blame] | 632 | for config in self.masters[master].values(): |
shenghuazhang | 804b2154 | 2016-10-11 02:06:49 | [diff] [blame] | 633 | if isinstance(config, dict): |
| 634 | for c in config.values(): |
dpranke | b9380a1 | 2016-07-21 21:44:09 | [diff] [blame] | 635 | all_configs[c] = master |
| 636 | else: |
| 637 | all_configs[config] = master |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 638 | |
dpranke | 9dd5e25 | 2016-04-14 04:23:09 | [diff] [blame] | 639 | # Check that every referenced args file or config actually exists. |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 640 | for config, loc in all_configs.items(): |
dpranke | 9dd5e25 | 2016-04-14 04:23:09 | [diff] [blame] | 641 | if config.startswith('//'): |
| 642 | if not self.Exists(self.ToAbsPath(config)): |
| 643 | errs.append('Unknown args file "%s" referenced from "%s".' % |
| 644 | (config, loc)) |
| 645 | elif not config in self.configs: |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 646 | errs.append('Unknown config "%s" referenced from "%s".' % |
| 647 | (config, loc)) |
| 648 | |
| 649 | # Check that every actual config is actually referenced. |
| 650 | for config in self.configs: |
| 651 | if not config in all_configs: |
| 652 | errs.append('Unused config "%s".' % config) |
| 653 | |
| 654 | # Figure out the whole list of mixins, and check that every mixin |
| 655 | # listed by a config or another mixin actually exists. |
| 656 | referenced_mixins = set() |
| 657 | for config, mixins in self.configs.items(): |
| 658 | for mixin in mixins: |
| 659 | if not mixin in self.mixins: |
| 660 | errs.append('Unknown mixin "%s" referenced by config "%s".' % |
| 661 | (mixin, config)) |
| 662 | referenced_mixins.add(mixin) |
| 663 | |
| 664 | for mixin in self.mixins: |
| 665 | for sub_mixin in self.mixins[mixin].get('mixins', []): |
| 666 | if not sub_mixin in self.mixins: |
| 667 | errs.append('Unknown mixin "%s" referenced by mixin "%s".' % |
| 668 | (sub_mixin, mixin)) |
| 669 | referenced_mixins.add(sub_mixin) |
| 670 | |
| 671 | # Check that every mixin defined is actually referenced somewhere. |
| 672 | for mixin in self.mixins: |
| 673 | if not mixin in referenced_mixins: |
| 674 | errs.append('Unreferenced mixin "%s".' % mixin) |
| 675 | |
dpranke | 255085e | 2016-03-16 05:23:59 | [diff] [blame] | 676 | # If we're checking the Chromium config, check that the 'chromium' bots |
| 677 | # which build public artifacts do not include the chrome_with_codecs mixin. |
| 678 | if self.args.config_file == self.default_config: |
| 679 | if 'chromium' in self.masters: |
| 680 | for builder in self.masters['chromium']: |
| 681 | config = self.masters['chromium'][builder] |
| 682 | def RecurseMixins(current_mixin): |
| 683 | if current_mixin == 'chrome_with_codecs': |
| 684 | errs.append('Public artifact builder "%s" can not contain the ' |
| 685 | '"chrome_with_codecs" mixin.' % builder) |
| 686 | return |
| 687 | if not 'mixins' in self.mixins[current_mixin]: |
| 688 | return |
| 689 | for mixin in self.mixins[current_mixin]['mixins']: |
| 690 | RecurseMixins(mixin) |
dalecurtis | 56fd27e | 2016-03-09 23:06:41 | [diff] [blame] | 691 | |
dpranke | 255085e | 2016-03-16 05:23:59 | [diff] [blame] | 692 | for mixin in self.configs[config]: |
| 693 | RecurseMixins(mixin) |
| 694 | else: |
| 695 | errs.append('Missing "chromium" master. Please update this ' |
| 696 | 'proprietary codecs check with the name of the master ' |
| 697 | 'responsible for public build artifacts.') |
dalecurtis | 56fd27e | 2016-03-09 23:06:41 | [diff] [blame] | 698 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 699 | if errs: |
dpranke | 4323c8063 | 2015-08-10 22:53:54 | [diff] [blame] | 700 | raise MBErr(('mb config file %s has problems:' % self.args.config_file) + |
dpranke | a3326787 | 2015-08-12 15:45:17 | [diff] [blame] | 701 | '\n ' + '\n '.join(errs)) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 702 | |
dpranke | 0cafc16 | 2016-03-19 00:41:10 | [diff] [blame] | 703 | if print_ok: |
| 704 | self.Print('mb config file %s looks ok.' % self.args.config_file) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 705 | return 0 |
| 706 | |
| 707 | def GetConfig(self): |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 708 | build_dir = self.args.path |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 709 | |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 710 | vals = self.DefaultVals() |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 711 | if self.args.builder or self.args.master or self.args.config: |
| 712 | vals = self.Lookup() |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 713 | # Re-run gn gen in order to ensure the config is consistent with the |
| 714 | # build dir. |
| 715 | self.RunGNGen(vals) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 716 | return vals |
| 717 | |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 718 | toolchain_path = self.PathJoin(self.ToAbsPath(build_dir), |
| 719 | 'toolchain.ninja') |
| 720 | if not self.Exists(toolchain_path): |
| 721 | self.Print('Must either specify a path to an existing GN build dir ' |
| 722 | 'or pass in a -m/-b pair or a -c flag to specify the ' |
| 723 | 'configuration') |
| 724 | return {} |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 725 | |
Dirk Pranke | d181a1a | 2017-12-14 01:47:11 | [diff] [blame] | 726 | vals['gn_args'] = self.GNArgsFromDir(build_dir) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 727 | return vals |
| 728 | |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 729 | def GNArgsFromDir(self, build_dir): |
brucedawson | ecc0c1cd | 2016-06-02 18:24:58 | [diff] [blame] | 730 | args_contents = "" |
| 731 | gn_args_path = self.PathJoin(self.ToAbsPath(build_dir), 'args.gn') |
| 732 | if self.Exists(gn_args_path): |
| 733 | args_contents = self.ReadFile(gn_args_path) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 734 | gn_args = [] |
| 735 | for l in args_contents.splitlines(): |
| 736 | fields = l.split(' ') |
| 737 | name = fields[0] |
| 738 | val = ' '.join(fields[2:]) |
| 739 | gn_args.append('%s=%s' % (name, val)) |
| 740 | |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 741 | return ' '.join(gn_args) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 742 | |
| 743 | def Lookup(self): |
Erik Chen | 238f4ac | 2019-04-12 19:02:50 | [diff] [blame] | 744 | vals = self.ReadIOSBotConfig() |
| 745 | if not vals: |
| 746 | self.ReadConfigFile() |
| 747 | config = self.ConfigFromArgs() |
| 748 | if config.startswith('//'): |
| 749 | if not self.Exists(self.ToAbsPath(config)): |
| 750 | raise MBErr('args file "%s" not found' % config) |
| 751 | vals = self.DefaultVals() |
| 752 | vals['args_file'] = config |
| 753 | else: |
| 754 | if not config in self.configs: |
| 755 | raise MBErr('Config "%s" not found in %s' % |
| 756 | (config, self.args.config_file)) |
| 757 | vals = self.FlattenConfig(config) |
| 758 | return vals |
| 759 | |
| 760 | def ReadIOSBotConfig(self): |
| 761 | if not self.args.master or not self.args.builder: |
| 762 | return {} |
| 763 | path = self.PathJoin(self.chromium_src_dir, 'ios', 'build', 'bots', |
| 764 | self.args.master, self.args.builder + '.json') |
| 765 | if not self.Exists(path): |
| 766 | return {} |
| 767 | |
| 768 | contents = json.loads(self.ReadFile(path)) |
| 769 | gn_args = ' '.join(contents.get('gn_args', [])) |
| 770 | |
| 771 | vals = self.DefaultVals() |
| 772 | vals['gn_args'] = gn_args |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 773 | return vals |
dpranke | e0f486f | 2015-11-19 23:42:00 | [diff] [blame] | 774 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 775 | def ReadConfigFile(self): |
| 776 | if not self.Exists(self.args.config_file): |
| 777 | raise MBErr('config file not found at %s' % self.args.config_file) |
| 778 | |
| 779 | try: |
| 780 | contents = ast.literal_eval(self.ReadFile(self.args.config_file)) |
| 781 | except SyntaxError as e: |
| 782 | raise MBErr('Failed to parse config file "%s": %s' % |
| 783 | (self.args.config_file, e)) |
| 784 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 785 | self.configs = contents['configs'] |
| 786 | self.masters = contents['masters'] |
| 787 | self.mixins = contents['mixins'] |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 788 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 789 | def ReadIsolateMap(self): |
Zhiling Huang | 6695846 | 2018-02-03 00:28:20 | [diff] [blame] | 790 | if not self.args.isolate_map_files: |
| 791 | self.args.isolate_map_files = [self.default_isolate_map] |
| 792 | |
| 793 | for f in self.args.isolate_map_files: |
| 794 | if not self.Exists(f): |
| 795 | raise MBErr('isolate map file not found at %s' % f) |
| 796 | isolate_maps = {} |
| 797 | for isolate_map in self.args.isolate_map_files: |
| 798 | try: |
| 799 | isolate_map = ast.literal_eval(self.ReadFile(isolate_map)) |
| 800 | duplicates = set(isolate_map).intersection(isolate_maps) |
| 801 | if duplicates: |
| 802 | raise MBErr( |
| 803 | 'Duplicate targets in isolate map files: %s.' % |
| 804 | ', '.join(duplicates)) |
| 805 | isolate_maps.update(isolate_map) |
| 806 | except SyntaxError as e: |
| 807 | raise MBErr( |
| 808 | 'Failed to parse isolate map file "%s": %s' % (isolate_map, e)) |
| 809 | return isolate_maps |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 810 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 811 | def ConfigFromArgs(self): |
| 812 | if self.args.config: |
| 813 | if self.args.master or self.args.builder: |
| 814 | raise MBErr('Can not specific both -c/--config and -m/--master or ' |
| 815 | '-b/--builder') |
| 816 | |
| 817 | return self.args.config |
| 818 | |
| 819 | if not self.args.master or not self.args.builder: |
| 820 | raise MBErr('Must specify either -c/--config or ' |
| 821 | '(-m/--master and -b/--builder)') |
| 822 | |
| 823 | if not self.args.master in self.masters: |
| 824 | raise MBErr('Master name "%s" not found in "%s"' % |
| 825 | (self.args.master, self.args.config_file)) |
| 826 | |
| 827 | if not self.args.builder in self.masters[self.args.master]: |
| 828 | raise MBErr('Builder name "%s" not found under masters[%s] in "%s"' % |
| 829 | (self.args.builder, self.args.master, self.args.config_file)) |
| 830 | |
dpranke | b9380a1 | 2016-07-21 21:44:09 | [diff] [blame] | 831 | config = self.masters[self.args.master][self.args.builder] |
shenghuazhang | 804b2154 | 2016-10-11 02:06:49 | [diff] [blame] | 832 | if isinstance(config, dict): |
dpranke | b9380a1 | 2016-07-21 21:44:09 | [diff] [blame] | 833 | if self.args.phase is None: |
| 834 | raise MBErr('Must specify a build --phase for %s on %s' % |
| 835 | (self.args.builder, self.args.master)) |
shenghuazhang | 804b2154 | 2016-10-11 02:06:49 | [diff] [blame] | 836 | phase = str(self.args.phase) |
| 837 | if phase not in config: |
| 838 | raise MBErr('Phase %s doesn\'t exist for %s on %s' % |
dpranke | b9380a1 | 2016-07-21 21:44:09 | [diff] [blame] | 839 | (phase, self.args.builder, self.args.master)) |
shenghuazhang | 804b2154 | 2016-10-11 02:06:49 | [diff] [blame] | 840 | return config[phase] |
dpranke | b9380a1 | 2016-07-21 21:44:09 | [diff] [blame] | 841 | |
| 842 | if self.args.phase is not None: |
| 843 | raise MBErr('Must not specify a build --phase for %s on %s' % |
| 844 | (self.args.builder, self.args.master)) |
| 845 | return config |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 846 | |
| 847 | def FlattenConfig(self, config): |
| 848 | mixins = self.configs[config] |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 849 | vals = self.DefaultVals() |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 850 | |
| 851 | visited = [] |
| 852 | self.FlattenMixins(mixins, vals, visited) |
| 853 | return vals |
| 854 | |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 855 | def DefaultVals(self): |
| 856 | return { |
| 857 | 'args_file': '', |
| 858 | 'cros_passthrough': False, |
| 859 | 'gn_args': '', |
dpranke | f37aebb9 | 2016-09-23 01:14:49 | [diff] [blame] | 860 | } |
| 861 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 862 | def FlattenMixins(self, mixins, vals, visited): |
| 863 | for m in mixins: |
| 864 | if m not in self.mixins: |
| 865 | raise MBErr('Unknown mixin "%s"' % m) |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 866 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 867 | visited.append(m) |
| 868 | |
| 869 | mixin_vals = self.mixins[m] |
dpranke | 73ed0d6 | 2016-04-25 19:18:34 | [diff] [blame] | 870 | |
| 871 | if 'cros_passthrough' in mixin_vals: |
| 872 | vals['cros_passthrough'] = mixin_vals['cros_passthrough'] |
Dirk Pranke | 6b99f07 | 2017-04-05 00:58:30 | [diff] [blame] | 873 | if 'args_file' in mixin_vals: |
| 874 | if vals['args_file']: |
Yun Liu | c0f2f73 | 2019-09-18 17:06:31 | [diff] [blame] | 875 | raise MBErr('args_file specified multiple times in mixins ' |
| 876 | 'for %s on %s' % (self.args.builder, self.args.master)) |
Dirk Pranke | 6b99f07 | 2017-04-05 00:58:30 | [diff] [blame] | 877 | vals['args_file'] = mixin_vals['args_file'] |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 878 | if 'gn_args' in mixin_vals: |
| 879 | if vals['gn_args']: |
| 880 | vals['gn_args'] += ' ' + mixin_vals['gn_args'] |
| 881 | else: |
| 882 | vals['gn_args'] = mixin_vals['gn_args'] |
dpranke | 73ed0d6 | 2016-04-25 19:18:34 | [diff] [blame] | 883 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 884 | if 'mixins' in mixin_vals: |
| 885 | self.FlattenMixins(mixin_vals['mixins'], vals, visited) |
| 886 | return vals |
| 887 | |
Takuto Ikuta | 9dffd7e | 2018-09-05 01:04:00 | [diff] [blame] | 888 | def RunGNGen(self, vals, compute_inputs_for_analyze=False, check=True): |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 889 | build_dir = self.args.path |
Dirk Pranke | 0fd41bcd | 2015-06-19 00:05:50 | [diff] [blame] | 890 | |
Takuto Ikuta | 9dffd7e | 2018-09-05 01:04:00 | [diff] [blame] | 891 | if check: |
| 892 | cmd = self.GNCmd('gen', build_dir, '--check') |
| 893 | else: |
| 894 | cmd = self.GNCmd('gen', build_dir) |
dpranke | eca4a78 | 2016-04-14 01:42:38 | [diff] [blame] | 895 | gn_args = self.GNArgs(vals) |
Andrew Grieve | 0bb79bb | 2018-06-27 03:14:09 | [diff] [blame] | 896 | if compute_inputs_for_analyze: |
| 897 | gn_args += ' compute_inputs_for_analyze=true' |
dpranke | eca4a78 | 2016-04-14 01:42:38 | [diff] [blame] | 898 | |
| 899 | # Since GN hasn't run yet, the build directory may not even exist. |
| 900 | self.MaybeMakeDirectory(self.ToAbsPath(build_dir)) |
| 901 | |
| 902 | gn_args_path = self.ToAbsPath(build_dir, 'args.gn') |
dpranke | 4ff8b9f | 2016-04-15 03:07:54 | [diff] [blame] | 903 | self.WriteFile(gn_args_path, gn_args, force_verbose=True) |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 904 | |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 905 | if getattr(self.args, 'swarming_targets_file', None): |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 906 | # We need GN to generate the list of runtime dependencies for |
| 907 | # the compile targets listed (one per line) in the file so |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 908 | # we can run them via swarming. We use gn_isolate_map.pyl to convert |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 909 | # the compile targets to the matching GN labels. |
dpranke | b2be10a | 2016-02-22 17:11:00 | [diff] [blame] | 910 | path = self.args.swarming_targets_file |
| 911 | if not self.Exists(path): |
| 912 | self.WriteFailureAndRaise('"%s" does not exist' % path, |
| 913 | output_path=None) |
| 914 | contents = self.ReadFile(path) |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 915 | isolate_targets = set(contents.splitlines()) |
dpranke | b2be10a | 2016-02-22 17:11:00 | [diff] [blame] | 916 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 917 | isolate_map = self.ReadIsolateMap() |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 918 | self.RemovePossiblyStaleRuntimeDepsFiles(vals, isolate_targets, |
| 919 | isolate_map, build_dir) |
| 920 | |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 921 | err, labels = self.MapTargetsToLabels(isolate_map, isolate_targets) |
dpranke | b2be10a | 2016-02-22 17:11:00 | [diff] [blame] | 922 | if err: |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 923 | raise MBErr(err) |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 924 | |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 925 | gn_runtime_deps_path = self.ToAbsPath(build_dir, 'runtime_deps') |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 926 | self.WriteFile(gn_runtime_deps_path, '\n'.join(labels) + '\n') |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 927 | cmd.append('--runtime-deps-list-file=%s' % gn_runtime_deps_path) |
| 928 | |
Debrian Figueroa | ae58223 | 2019-07-17 01:54:45 | [diff] [blame] | 929 | ret, output, _ = self.Run(cmd) |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 930 | if ret: |
Debrian Figueroa | ae51d0d | 2019-07-22 18:04:11 | [diff] [blame] | 931 | if self.args.json_output: |
Debrian Figueroa | ae58223 | 2019-07-17 01:54:45 | [diff] [blame] | 932 | # write errors to json.output |
| 933 | self.WriteJSON({'output': output}, self.args.json_output) |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 934 | # If `gn gen` failed, we should exit early rather than trying to |
| 935 | # generate isolates. Run() will have already logged any error output. |
| 936 | self.Print('GN gen failed: %d' % ret) |
| 937 | return ret |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 938 | |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 939 | if getattr(self.args, 'swarming_targets_file', None): |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 940 | ret = self.GenerateIsolates(vals, isolate_targets, isolate_map, build_dir) |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 941 | |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 942 | return ret |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 943 | |
| 944 | def RunGNGenAllIsolates(self, vals): |
| 945 | """ |
| 946 | This command generates all .isolate files. |
| 947 | |
| 948 | This command assumes that "mb.py gen" has already been run, as it relies on |
| 949 | "gn ls" to fetch all gn targets. If uses that output, combined with the |
| 950 | isolate_map, to determine all isolates that can be generated for the current |
| 951 | gn configuration. |
| 952 | """ |
| 953 | build_dir = self.args.path |
| 954 | ret, output, _ = self.Run(self.GNCmd('ls', build_dir), |
| 955 | force_verbose=False) |
| 956 | if ret: |
Yun Liu | c0f2f73 | 2019-09-18 17:06:31 | [diff] [blame] | 957 | # If `gn ls` failed, we should exit early rather than trying to |
| 958 | # generate isolates. |
| 959 | self.Print('GN ls failed: %d' % ret) |
| 960 | return ret |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 961 | |
| 962 | # Create a reverse map from isolate label to isolate dict. |
| 963 | isolate_map = self.ReadIsolateMap() |
| 964 | isolate_dict_map = {} |
| 965 | for key, isolate_dict in isolate_map.iteritems(): |
| 966 | isolate_dict_map[isolate_dict['label']] = isolate_dict |
| 967 | isolate_dict_map[isolate_dict['label']]['isolate_key'] = key |
| 968 | |
| 969 | runtime_deps = [] |
| 970 | |
| 971 | isolate_targets = [] |
| 972 | # For every GN target, look up the isolate dict. |
| 973 | for line in output.splitlines(): |
| 974 | target = line.strip() |
| 975 | if target in isolate_dict_map: |
| 976 | if isolate_dict_map[target]['type'] == 'additional_compile_target': |
| 977 | # By definition, additional_compile_targets are not tests, so we |
| 978 | # shouldn't generate isolates for them. |
| 979 | continue |
| 980 | |
| 981 | isolate_targets.append(isolate_dict_map[target]['isolate_key']) |
| 982 | runtime_deps.append(target) |
| 983 | |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 984 | self.RemovePossiblyStaleRuntimeDepsFiles(vals, isolate_targets, |
| 985 | isolate_map, build_dir) |
| 986 | |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 987 | gn_runtime_deps_path = self.ToAbsPath(build_dir, 'runtime_deps') |
| 988 | self.WriteFile(gn_runtime_deps_path, '\n'.join(runtime_deps) + '\n') |
| 989 | cmd = self.GNCmd('gen', build_dir) |
| 990 | cmd.append('--runtime-deps-list-file=%s' % gn_runtime_deps_path) |
| 991 | self.Run(cmd) |
| 992 | |
| 993 | return self.GenerateIsolates(vals, isolate_targets, isolate_map, build_dir) |
| 994 | |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 995 | def RemovePossiblyStaleRuntimeDepsFiles(self, vals, targets, isolate_map, |
| 996 | build_dir): |
| 997 | # TODO(crbug.com/932700): Because `gn gen --runtime-deps-list-file` |
| 998 | # puts the runtime_deps file in different locations based on the actual |
| 999 | # type of a target, we may end up with multiple possible runtime_deps |
| 1000 | # files in a given build directory, where some of the entries might be |
| 1001 | # stale (since we might be reusing an existing build directory). |
| 1002 | # |
| 1003 | # We need to be able to get the right one reliably; you might think |
| 1004 | # we can just pick the newest file, but because GN won't update timestamps |
| 1005 | # if the contents of the files change, an older runtime_deps |
| 1006 | # file might actually be the one we should use over a newer one (see |
| 1007 | # crbug.com/932387 for a more complete explanation and example). |
| 1008 | # |
| 1009 | # In order to avoid this, we need to delete any possible runtime_deps |
| 1010 | # files *prior* to running GN. As long as the files aren't actually |
| 1011 | # needed during the build, this hopefully will not cause unnecessary |
| 1012 | # build work, and so it should be safe. |
| 1013 | # |
| 1014 | # Ultimately, we should just make sure we get the runtime_deps files |
| 1015 | # in predictable locations so we don't have this issue at all, and |
| 1016 | # that's what crbug.com/932700 is for. |
| 1017 | possible_rpaths = self.PossibleRuntimeDepsPaths(vals, targets, isolate_map) |
| 1018 | for rpaths in possible_rpaths.values(): |
| 1019 | for rpath in rpaths: |
| 1020 | path = self.ToAbsPath(build_dir, rpath) |
| 1021 | if self.Exists(path): |
| 1022 | self.RemoveFile(path) |
| 1023 | |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 1024 | def GenerateIsolates(self, vals, ninja_targets, isolate_map, build_dir): |
| 1025 | """ |
| 1026 | Generates isolates for a list of ninja targets. |
| 1027 | |
| 1028 | Ninja targets are transformed to GN targets via isolate_map. |
| 1029 | |
| 1030 | This function assumes that a previous invocation of "mb.py gen" has |
| 1031 | generated runtime deps for all targets. |
| 1032 | """ |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1033 | possible_rpaths = self.PossibleRuntimeDepsPaths(vals, ninja_targets, |
| 1034 | isolate_map) |
| 1035 | |
| 1036 | for target, rpaths in possible_rpaths.items(): |
| 1037 | # TODO(crbug.com/932700): We don't know where each .runtime_deps |
| 1038 | # file might be, but assuming we called |
| 1039 | # RemovePossiblyStaleRuntimeDepsFiles prior to calling `gn gen`, |
| 1040 | # there should only be one file. |
| 1041 | found_one = False |
| 1042 | path_to_use = None |
| 1043 | for r in rpaths: |
| 1044 | path = self.ToAbsPath(build_dir, r) |
| 1045 | if self.Exists(path): |
| 1046 | if found_one: |
| 1047 | raise MBErr('Found more than one of %s' % ', '.join(rpaths)) |
| 1048 | path_to_use = path |
| 1049 | found_one = True |
| 1050 | |
| 1051 | if not found_one: |
| 1052 | raise MBErr('Did not find any of %s' % ', '.join(rpaths)) |
| 1053 | |
| 1054 | command, extra_files = self.GetIsolateCommand(target, vals) |
| 1055 | runtime_deps = self.ReadFile(path_to_use).splitlines() |
| 1056 | |
| 1057 | canonical_target = target.replace(':','_').replace('/','_') |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1058 | ret = self.WriteIsolateFiles(build_dir, command, canonical_target, |
| 1059 | runtime_deps, vals, extra_files) |
| 1060 | if ret: |
| 1061 | return ret |
| 1062 | return 0 |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1063 | |
| 1064 | def PossibleRuntimeDepsPaths(self, vals, ninja_targets, isolate_map): |
| 1065 | """Returns a map of targets to possible .runtime_deps paths. |
| 1066 | |
| 1067 | Each ninja target maps on to a GN label, but depending on the type |
| 1068 | of the GN target, `gn gen --runtime-deps-list-file` will write |
| 1069 | the .runtime_deps files into different locations. Unfortunately, in |
| 1070 | some cases we don't actually know which of multiple locations will |
| 1071 | actually be used, so we return all plausible candidates. |
| 1072 | |
| 1073 | The paths that are returned are relative to the build directory. |
| 1074 | """ |
| 1075 | |
jbudorick | e3c4f95e | 2016-04-28 23:17:38 | [diff] [blame] | 1076 | android = 'target_os="android"' in vals['gn_args'] |
Dirk Pranke | 26de05aec | 2019-04-03 19:18:38 | [diff] [blame] | 1077 | ios = 'target_os="ios"' in vals['gn_args'] |
Kevin Marshall | f35fa5f | 2018-01-29 19:24:42 | [diff] [blame] | 1078 | fuchsia = 'target_os="fuchsia"' in vals['gn_args'] |
Nico Weber | d94b71a | 2018-02-22 22:00:30 | [diff] [blame] | 1079 | win = self.platform == 'win32' or 'target_os="win"' in vals['gn_args'] |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1080 | possible_runtime_deps_rpaths = {} |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 1081 | for target in ninja_targets: |
John Budorick | 39f1496 | 2019-04-11 23:03:20 | [diff] [blame] | 1082 | target_type = isolate_map[target]['type'] |
| 1083 | label = isolate_map[target]['label'] |
| 1084 | stamp_runtime_deps = 'obj/%s.stamp.runtime_deps' % label.replace(':', '/') |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 1085 | # TODO(https://crbug.com/876065): 'official_tests' use |
| 1086 | # type='additional_compile_target' to isolate tests. This is not the |
| 1087 | # intended use for 'additional_compile_target'. |
John Budorick | 39f1496 | 2019-04-11 23:03:20 | [diff] [blame] | 1088 | if (target_type == 'additional_compile_target' and |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 1089 | target != 'official_tests'): |
| 1090 | # By definition, additional_compile_targets are not tests, so we |
| 1091 | # shouldn't generate isolates for them. |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1092 | raise MBErr('Cannot generate isolate for %s since it is an ' |
| 1093 | 'additional_compile_target.' % target) |
John Budorick | 39f1496 | 2019-04-11 23:03:20 | [diff] [blame] | 1094 | elif fuchsia or ios or target_type == 'generated_script': |
| 1095 | # iOS and Fuchsia targets end up as groups. |
| 1096 | # generated_script targets are always actions. |
| 1097 | rpaths = [stamp_runtime_deps] |
Erik Chen | 42df41d | 2018-08-21 17:13:31 | [diff] [blame] | 1098 | elif android: |
jbudorick | e3c4f95e | 2016-04-28 23:17:38 | [diff] [blame] | 1099 | # Android targets may be either android_apk or executable. The former |
jbudorick | 91c8a601 | 2016-01-29 23:20:02 | [diff] [blame] | 1100 | # will result in runtime_deps associated with the stamp file, while the |
| 1101 | # latter will result in runtime_deps associated with the executable. |
Abhishek Arya | 2f5f734 | 2018-06-13 16:59:44 | [diff] [blame] | 1102 | label = isolate_map[target]['label'] |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1103 | rpaths = [ |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1104 | target + '.runtime_deps', |
John Budorick | 39f1496 | 2019-04-11 23:03:20 | [diff] [blame] | 1105 | stamp_runtime_deps] |
| 1106 | elif (target_type == 'script' or |
| 1107 | target_type == 'fuzzer' or |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1108 | isolate_map[target].get('label_type') == 'group'): |
dpranke | 6abd865 | 2015-08-28 03:21:11 | [diff] [blame] | 1109 | # For script targets, the build target is usually a group, |
| 1110 | # for which gn generates the runtime_deps next to the stamp file |
eyaich | 82d5ac94 | 2016-11-03 12:13:49 | [diff] [blame] | 1111 | # for the label, which lives under the obj/ directory, but it may |
| 1112 | # also be an executable. |
Abhishek Arya | 2f5f734 | 2018-06-13 16:59:44 | [diff] [blame] | 1113 | label = isolate_map[target]['label'] |
John Budorick | 39f1496 | 2019-04-11 23:03:20 | [diff] [blame] | 1114 | rpaths = [stamp_runtime_deps] |
Nico Weber | d94b71a | 2018-02-22 22:00:30 | [diff] [blame] | 1115 | if win: |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1116 | rpaths += [ target + '.exe.runtime_deps' ] |
eyaich | 82d5ac94 | 2016-11-03 12:13:49 | [diff] [blame] | 1117 | else: |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1118 | rpaths += [ target + '.runtime_deps' ] |
Nico Weber | d94b71a | 2018-02-22 22:00:30 | [diff] [blame] | 1119 | elif win: |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1120 | rpaths = [target + '.exe.runtime_deps'] |
dpranke | 34bd39d | 2015-06-24 02:36:52 | [diff] [blame] | 1121 | else: |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1122 | rpaths = [target + '.runtime_deps'] |
jbudorick | 91c8a601 | 2016-01-29 23:20:02 | [diff] [blame] | 1123 | |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1124 | possible_runtime_deps_rpaths[target] = rpaths |
Dirk Pranke | b3b725c | 2019-02-16 02:18:41 | [diff] [blame] | 1125 | |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1126 | return possible_runtime_deps_rpaths |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1127 | |
| 1128 | def RunGNIsolate(self, vals): |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 1129 | target = self.args.target |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1130 | isolate_map = self.ReadIsolateMap() |
| 1131 | err, labels = self.MapTargetsToLabels(isolate_map, [target]) |
| 1132 | if err: |
| 1133 | raise MBErr(err) |
Dirk Pranke | 7a7e9b6 | 2019-02-17 01:46:25 | [diff] [blame] | 1134 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1135 | label = labels[0] |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1136 | |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 1137 | build_dir = self.args.path |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1138 | command, extra_files = self.GetIsolateCommand(target, vals) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1139 | |
dpranke | eca4a78 | 2016-04-14 01:42:38 | [diff] [blame] | 1140 | cmd = self.GNCmd('desc', build_dir, label, 'runtime_deps') |
dpranke | 40da020 | 2016-02-13 05:05:20 | [diff] [blame] | 1141 | ret, out, _ = self.Call(cmd) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1142 | if ret: |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 1143 | if out: |
| 1144 | self.Print(out) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1145 | return ret |
| 1146 | |
| 1147 | runtime_deps = out.splitlines() |
| 1148 | |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1149 | ret = self.WriteIsolateFiles(build_dir, command, target, runtime_deps, vals, |
| 1150 | extra_files) |
| 1151 | if ret: |
| 1152 | return ret |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1153 | |
| 1154 | ret, _, _ = self.Run([ |
| 1155 | self.executable, |
| 1156 | self.PathJoin('tools', 'swarming_client', 'isolate.py'), |
| 1157 | 'check', |
| 1158 | '-i', |
| 1159 | self.ToSrcRelPath('%s/%s.isolate' % (build_dir, target)), |
| 1160 | '-s', |
| 1161 | self.ToSrcRelPath('%s/%s.isolated' % (build_dir, target))], |
| 1162 | buffer_output=False) |
dpranke | d5b2b943 | 2015-06-23 16:55:30 | [diff] [blame] | 1163 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1164 | return ret |
| 1165 | |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1166 | def WriteIsolateFiles(self, build_dir, command, target, runtime_deps, vals, |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1167 | extra_files): |
| 1168 | isolate_path = self.ToAbsPath(build_dir, target + '.isolate') |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1169 | files = sorted(set(runtime_deps + extra_files)) |
| 1170 | |
| 1171 | # Complain if any file is a directory that's inside the build directory, |
| 1172 | # since that makes incremental builds incorrect. See |
| 1173 | # https://crbug.com/912946 |
| 1174 | is_android = 'target_os="android"' in vals['gn_args'] |
| 1175 | is_cros = ('target_os="chromeos"' in vals['gn_args'] or |
| 1176 | vals.get('cros_passthrough', False)) |
| 1177 | is_mac = self.platform == 'darwin' |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1178 | is_msan = 'is_msan=true' in vals['gn_args'] |
| 1179 | |
| 1180 | err = '' |
| 1181 | for f in files: |
| 1182 | # Skip a few configs that need extra cleanup for now. |
| 1183 | # TODO(https://crbug.com/912946): Fix everything on all platforms and |
| 1184 | # enable check everywhere. |
Nico Weber | d9886b9 | 2019-09-10 17:52:17 | [diff] [blame] | 1185 | if is_android: |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1186 | break |
| 1187 | |
| 1188 | # Skip a few existing violations that need to be cleaned up. Each of |
| 1189 | # these will lead to incorrect incremental builds if their directory |
| 1190 | # contents change. Do not add to this list. |
| 1191 | # TODO(https://crbug.com/912946): Remove this if statement. |
Nico Weber | 8989582 | 2019-08-27 18:59:03 | [diff] [blame] | 1192 | if ((is_msan and f == 'instrumented_libraries_prebuilt/') or |
Clifford Cheng | e124482 | 2019-08-27 17:26:55 | [diff] [blame] | 1193 | f == 'mr_extension/' or # https://crbug.com/997947 |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1194 | f == 'locales/' or |
| 1195 | f.startswith('nacl_test_data/') or |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1196 | f.startswith('ppapi_nacl_tests_libs/') or |
Nico Weber | d9886b9 | 2019-09-10 17:52:17 | [diff] [blame] | 1197 | (is_cros and f in ( # https://crbug.com/1002509 |
| 1198 | 'chromevox_test_data/', |
| 1199 | 'gen/ui/file_manager/file_manager/', |
| 1200 | 'resources/chromeos/', |
Anastasia Helfinstein | 4bb71955 | 2019-11-21 19:02:51 | [diff] [blame] | 1201 | 'resources/chromeos/accessibility/autoclick/', |
| 1202 | 'resources/chromeos/accessibility/chromevox/', |
| 1203 | 'resources/chromeos/accessibility/select_to_speak/', |
| 1204 | 'test_data/chrome/browser/resources/chromeos/accessibility/' |
| 1205 | 'autoclick/', |
| 1206 | 'test_data/chrome/browser/resources/chromeos/accessibility/' |
| 1207 | 'chromevox/', |
| 1208 | 'test_data/chrome/browser/resources/chromeos/accessibility/' |
| 1209 | 'select_to_speak/', |
Nico Weber | d9886b9 | 2019-09-10 17:52:17 | [diff] [blame] | 1210 | )) or |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1211 | (is_mac and f in ( # https://crbug.com/1000667 |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1212 | 'AlertNotificationService.xpc/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1213 | 'Chromium Framework.framework/', |
| 1214 | 'Chromium Helper.app/', |
| 1215 | 'Chromium.app/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1216 | 'Content Shell.app/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1217 | 'Google Chrome Framework.framework/', |
| 1218 | 'Google Chrome Helper (GPU).app/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1219 | 'Google Chrome Helper (Plugin).app/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1220 | 'Google Chrome Helper (Renderer).app/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1221 | 'Google Chrome Helper.app/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1222 | 'Google Chrome.app/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1223 | 'blink_deprecated_test_plugin.plugin/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1224 | 'blink_test_plugin.plugin/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1225 | 'corb_test_plugin.plugin/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1226 | 'obj/tools/grit/brotli_mac_asan_workaround/', |
| 1227 | 'power_saver_test_plugin.plugin/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1228 | 'ppapi_tests.plugin/', |
Nico Weber | 5eee452 | 2019-09-05 23:28:05 | [diff] [blame] | 1229 | 'ui_unittests Framework.framework/', |
| 1230 | ))): |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1231 | continue |
| 1232 | |
Nico Weber | 24e54f99 | 2019-08-26 14:33:32 | [diff] [blame] | 1233 | # This runs before the build, so we can't use isdir(f). But |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1234 | # isolate.py luckily requires data directories to end with '/', so we |
Nico Weber | 24e54f99 | 2019-08-26 14:33:32 | [diff] [blame] | 1235 | # can check for that. |
Nico Weber | 57dbc995 | 2019-09-04 13:33:58 | [diff] [blame] | 1236 | if not f.startswith('../../') and f.endswith('/'): |
Nico Weber | 24e54f99 | 2019-08-26 14:33:32 | [diff] [blame] | 1237 | # Don't use self.PathJoin() -- all involved paths consistently use |
| 1238 | # forward slashes, so don't add one single backslash on Windows. |
| 1239 | err += '\n' + build_dir + '/' + f |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1240 | |
| 1241 | if err: |
| 1242 | self.Print('error: gn `data` items may not list generated directories; ' |
Nico Weber | 24e54f99 | 2019-08-26 14:33:32 | [diff] [blame] | 1243 | 'list files in directory instead for:' + err) |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1244 | return 1 |
| 1245 | |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1246 | self.WriteFile(isolate_path, |
| 1247 | pprint.pformat({ |
| 1248 | 'variables': { |
| 1249 | 'command': command, |
Nico Weber | 0fd01676 | 2019-08-25 14:48:14 | [diff] [blame] | 1250 | 'files': files, |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1251 | } |
| 1252 | }) + '\n') |
| 1253 | |
| 1254 | self.WriteJSON( |
| 1255 | { |
| 1256 | 'args': [ |
| 1257 | '--isolated', |
| 1258 | self.ToSrcRelPath('%s/%s.isolated' % (build_dir, target)), |
| 1259 | '--isolate', |
| 1260 | self.ToSrcRelPath('%s/%s.isolate' % (build_dir, target)), |
| 1261 | ], |
| 1262 | 'dir': self.chromium_src_dir, |
| 1263 | 'version': 1, |
| 1264 | }, |
| 1265 | isolate_path + 'd.gen.json', |
| 1266 | ) |
| 1267 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1268 | def MapTargetsToLabels(self, isolate_map, targets): |
| 1269 | labels = [] |
| 1270 | err = '' |
| 1271 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1272 | for target in targets: |
| 1273 | if target == 'all': |
| 1274 | labels.append(target) |
| 1275 | elif target.startswith('//'): |
| 1276 | labels.append(target) |
| 1277 | else: |
| 1278 | if target in isolate_map: |
thakis | 024d6f3 | 2017-05-16 23:21:42 | [diff] [blame] | 1279 | if isolate_map[target]['type'] == 'unknown': |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1280 | err += ('test target "%s" type is unknown\n' % target) |
| 1281 | else: |
thakis | 024d6f3 | 2017-05-16 23:21:42 | [diff] [blame] | 1282 | labels.append(isolate_map[target]['label']) |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1283 | else: |
| 1284 | err += ('target "%s" not found in ' |
| 1285 | '//testing/buildbot/gn_isolate_map.pyl\n' % target) |
| 1286 | |
| 1287 | return err, labels |
| 1288 | |
dpranke | eca4a78 | 2016-04-14 01:42:38 | [diff] [blame] | 1289 | def GNCmd(self, subcommand, path, *args): |
Xiaoqian Dai | 8962649 | 2018-06-28 17:07:46 | [diff] [blame] | 1290 | if self.platform == 'linux2': |
| 1291 | subdir, exe = 'linux64', 'gn' |
| 1292 | elif self.platform == 'darwin': |
| 1293 | subdir, exe = 'mac', 'gn' |
John Barboza | a1a12ef | 2018-07-11 13:51:25 | [diff] [blame] | 1294 | elif self.platform == 'aix6': |
| 1295 | subdir, exe = 'aix', 'gn' |
Xiaoqian Dai | 8962649 | 2018-06-28 17:07:46 | [diff] [blame] | 1296 | else: |
| 1297 | subdir, exe = 'win', 'gn.exe' |
| 1298 | |
| 1299 | gn_path = self.PathJoin(self.chromium_src_dir, 'buildtools', subdir, exe) |
dpranke | 10118bf | 2016-09-16 23:16:08 | [diff] [blame] | 1300 | return [gn_path, subcommand, path] + list(args) |
dpranke | 9aba8b21 | 2016-09-16 22:52:52 | [diff] [blame] | 1301 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1302 | |
Garrett Beaty | b6cee04 | 2019-04-22 18:42:09 | [diff] [blame] | 1303 | def GNArgs(self, vals, expand_imports=False): |
dpranke | 73ed0d6 | 2016-04-25 19:18:34 | [diff] [blame] | 1304 | if vals['cros_passthrough']: |
| 1305 | if not 'GN_ARGS' in os.environ: |
| 1306 | raise MBErr('MB is expecting GN_ARGS to be in the environment') |
| 1307 | gn_args = os.environ['GN_ARGS'] |
dpranke | 4026018 | 2016-04-27 04:45:16 | [diff] [blame] | 1308 | if not re.search('target_os.*=.*"chromeos"', gn_args): |
dpranke | 39f3be0 | 2016-04-27 04:07:30 | [diff] [blame] | 1309 | raise MBErr('GN_ARGS is missing target_os = "chromeos": (GN_ARGS=%s)' % |
dpranke | 73ed0d6 | 2016-04-25 19:18:34 | [diff] [blame] | 1310 | gn_args) |
Ben Pastene | 74ad5377 | 2018-07-19 17:21:35 | [diff] [blame] | 1311 | if vals['gn_args']: |
| 1312 | gn_args += ' ' + vals['gn_args'] |
dpranke | 73ed0d6 | 2016-04-25 19:18:34 | [diff] [blame] | 1313 | else: |
| 1314 | gn_args = vals['gn_args'] |
| 1315 | |
dpranke | d0c138b | 2016-04-13 18:28:47 | [diff] [blame] | 1316 | if self.args.goma_dir: |
| 1317 | gn_args += ' goma_dir="%s"' % self.args.goma_dir |
dpranke | eca4a78 | 2016-04-14 01:42:38 | [diff] [blame] | 1318 | |
agrieve | 41d21a7 | 2016-04-14 18:02:26 | [diff] [blame] | 1319 | android_version_code = self.args.android_version_code |
| 1320 | if android_version_code: |
| 1321 | gn_args += ' android_default_version_code="%s"' % android_version_code |
| 1322 | |
| 1323 | android_version_name = self.args.android_version_name |
| 1324 | if android_version_name: |
| 1325 | gn_args += ' android_default_version_name="%s"' % android_version_name |
| 1326 | |
Garrett Beaty | b6cee04 | 2019-04-22 18:42:09 | [diff] [blame] | 1327 | args_gn_lines = [] |
| 1328 | parsed_gn_args = {} |
dpranke | eca4a78 | 2016-04-14 01:42:38 | [diff] [blame] | 1329 | |
Ben Pastene | 65ccf613 | 2018-11-08 00:47:59 | [diff] [blame] | 1330 | # If we're using the Simple Chrome SDK, add a comment at the top that |
| 1331 | # points to the doc. This must happen after the gn_helpers.ToGNString() |
| 1332 | # call above since gn_helpers strips comments. |
| 1333 | if vals['cros_passthrough']: |
Garrett Beaty | b6cee04 | 2019-04-22 18:42:09 | [diff] [blame] | 1334 | args_gn_lines.extend([ |
Ben Pastene | 65ccf613 | 2018-11-08 00:47:59 | [diff] [blame] | 1335 | '# These args are generated via the Simple Chrome SDK. See the link', |
| 1336 | '# below for more details:', |
| 1337 | '# https://chromium.googlesource.com/chromiumos/docs/+/master/simple_chrome_workflow.md', # pylint: disable=line-too-long |
Garrett Beaty | b6cee04 | 2019-04-22 18:42:09 | [diff] [blame] | 1338 | ]) |
Ben Pastene | 65ccf613 | 2018-11-08 00:47:59 | [diff] [blame] | 1339 | |
dpranke | 9dd5e25 | 2016-04-14 04:23:09 | [diff] [blame] | 1340 | args_file = vals.get('args_file', None) |
| 1341 | if args_file: |
Garrett Beaty | b6cee04 | 2019-04-22 18:42:09 | [diff] [blame] | 1342 | if expand_imports: |
| 1343 | content = self.ReadFile(self.ToAbsPath(args_file)) |
| 1344 | parsed_gn_args = gn_helpers.FromGNArgs(content) |
| 1345 | else: |
| 1346 | args_gn_lines.append('import("%s")' % args_file) |
| 1347 | |
| 1348 | # Canonicalize the arg string into a sorted, newline-separated list |
| 1349 | # of key-value pairs, and de-dup the keys if need be so that only |
| 1350 | # the last instance of each arg is listed. |
| 1351 | parsed_gn_args.update(gn_helpers.FromGNArgs(gn_args)) |
| 1352 | args_gn_lines.append(gn_helpers.ToGNString(parsed_gn_args)) |
| 1353 | |
| 1354 | return '\n'.join(args_gn_lines) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1355 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1356 | def GetIsolateCommand(self, target, vals): |
kylechar | 50abf5a | 2016-11-29 16:03:07 | [diff] [blame] | 1357 | isolate_map = self.ReadIsolateMap() |
| 1358 | |
Scott Graham | 3be4b416 | 2017-09-12 00:41:41 | [diff] [blame] | 1359 | is_android = 'target_os="android"' in vals['gn_args'] |
| 1360 | is_fuchsia = 'target_os="fuchsia"' in vals['gn_args'] |
Caleb Raitto | f983d10 | 2019-06-21 23:05:02 | [diff] [blame] | 1361 | is_cros = 'target_os="chromeos"' in vals['gn_args'] |
Nico Weber | a7bc1cb | 2019-06-15 17:42:39 | [diff] [blame] | 1362 | is_simplechrome = vals.get('cros_passthrough', False) |
| 1363 | is_mac = self.platform == 'darwin' |
Nico Weber | d94b71a | 2018-02-22 22:00:30 | [diff] [blame] | 1364 | is_win = self.platform == 'win32' or 'target_os="win"' in vals['gn_args'] |
jbudorick | e842873 | 2016-02-02 02:17:06 | [diff] [blame] | 1365 | |
kylechar | 3970568 | 2017-01-19 14:37:23 | [diff] [blame] | 1366 | # This should be true if tests with type='windowed_test_launcher' are |
| 1367 | # expected to run using xvfb. For example, Linux Desktop, X11 CrOS and |
msisov | aea5273 | 2017-03-21 08:08:08 | [diff] [blame] | 1368 | # Ozone CrOS builds. Note that one Ozone build can be used to run differen |
| 1369 | # backends. Currently, tests are executed for the headless and X11 backends |
| 1370 | # and both can run under Xvfb. |
| 1371 | # TODO(tonikitoo,msisov,fwang): Find a way to run tests for the Wayland |
| 1372 | # backend. |
Scott Graham | 3be4b416 | 2017-09-12 00:41:41 | [diff] [blame] | 1373 | use_xvfb = self.platform == 'linux2' and not is_android and not is_fuchsia |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1374 | |
| 1375 | asan = 'is_asan=true' in vals['gn_args'] |
| 1376 | msan = 'is_msan=true' in vals['gn_args'] |
| 1377 | tsan = 'is_tsan=true' in vals['gn_args'] |
pcc | 46233c2 | 2017-06-20 22:11:41 | [diff] [blame] | 1378 | cfi_diag = 'use_cfi_diag=true' in vals['gn_args'] |
Yun Liu | 5764e0dc | 2019-10-24 01:50:22 | [diff] [blame] | 1379 | clang_coverage = 'use_clang_coverage=true' in vals['gn_args'] |
Yun Liu | c0f2f73 | 2019-09-18 17:06:31 | [diff] [blame] | 1380 | java_coverage = 'use_jacoco_coverage=true' in vals['gn_args'] |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1381 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1382 | test_type = isolate_map[target]['type'] |
Brian Sheedy | 234580e5 | 2019-09-10 17:42:51 | [diff] [blame] | 1383 | use_python3 = isolate_map[target].get('use_python3', False) |
dpranke | fe0d35e | 2016-02-05 02:43:59 | [diff] [blame] | 1384 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1385 | executable = isolate_map[target].get('executable', target) |
bsheedy | 9c16ed6 | 2019-04-10 20:32:11 | [diff] [blame] | 1386 | executable_suffix = isolate_map[target].get( |
| 1387 | 'executable_suffix', '.exe' if is_win else '') |
dpranke | fe0d35e | 2016-02-05 02:43:59 | [diff] [blame] | 1388 | |
Brian Sheedy | 234580e5 | 2019-09-10 17:42:51 | [diff] [blame] | 1389 | if use_python3: |
| 1390 | cmdline = [ 'vpython3' ] |
| 1391 | extra_files = [ '../../.vpython3' ] |
| 1392 | else: |
| 1393 | cmdline = [ 'vpython' ] |
| 1394 | extra_files = [ '../../.vpython' ] |
| 1395 | extra_files += [ |
Andrii Shyshkalov | c158e010 | 2018-01-10 05:52:00 | [diff] [blame] | 1396 | '../../testing/test_env.py', |
| 1397 | ] |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1398 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1399 | if test_type == 'nontest': |
| 1400 | self.WriteFailureAndRaise('We should not be isolating %s.' % target, |
| 1401 | output_path=None) |
| 1402 | |
John Budorick | 93e88ac8 | 2019-04-12 18:39:11 | [diff] [blame] | 1403 | if test_type == 'generated_script': |
Ben Pastene | cb0fb41 | 2019-06-11 02:31:54 | [diff] [blame] | 1404 | script = isolate_map[target]['script'] |
| 1405 | if self.platform == 'win32': |
| 1406 | script += '.bat' |
Brian Sheedy | 234580e5 | 2019-09-10 17:42:51 | [diff] [blame] | 1407 | cmdline += [ |
John Budorick | 93e88ac8 | 2019-04-12 18:39:11 | [diff] [blame] | 1408 | '../../testing/test_env.py', |
Ben Pastene | cb0fb41 | 2019-06-11 02:31:54 | [diff] [blame] | 1409 | script, |
John Budorick | 93e88ac8 | 2019-04-12 18:39:11 | [diff] [blame] | 1410 | ] |
| 1411 | elif test_type == 'fuzzer': |
Brian Sheedy | 234580e5 | 2019-09-10 17:42:51 | [diff] [blame] | 1412 | cmdline += [ |
Roberto Carrillo | 1460da85 | 2018-12-14 17:10:39 | [diff] [blame] | 1413 | '../../testing/test_env.py', |
| 1414 | '../../tools/code_coverage/run_fuzz_target.py', |
| 1415 | '--fuzzer', './' + target, |
| 1416 | '--output-dir', '${ISOLATED_OUTDIR}', |
| 1417 | '--timeout', '3600'] |
| 1418 | elif is_android and test_type != "script": |
John Budorick | 8c420304 | 2019-03-19 17:22:01 | [diff] [blame] | 1419 | if asan: |
John Budorick | 31cdce6 | 2019-04-03 20:56:11 | [diff] [blame] | 1420 | cmdline += [os.path.join('bin', 'run_with_asan'), '--'] |
John Budorick | 8c420304 | 2019-03-19 17:22:01 | [diff] [blame] | 1421 | cmdline += [ |
John Budorick | fb97a85 | 2017-12-20 20:10:19 | [diff] [blame] | 1422 | '../../testing/test_env.py', |
hzl | 9b15df5 | 2017-03-23 23:43:04 | [diff] [blame] | 1423 | '../../build/android/test_wrapper/logdog_wrapper.py', |
| 1424 | '--target', target, |
hzl | 9ae1445 | 2017-04-04 23:38:02 | [diff] [blame] | 1425 | '--logdog-bin-cmd', '../../bin/logdog_butler', |
hzl | fc66094f | 2017-05-18 00:50:48 | [diff] [blame] | 1426 | '--store-tombstones'] |
Yun Liu | 5764e0dc | 2019-10-24 01:50:22 | [diff] [blame] | 1427 | if clang_coverage or java_coverage: |
Yun Liu | 7cef107 | 2019-06-27 21:22:19 | [diff] [blame] | 1428 | cmdline += ['--coverage-dir', '${ISOLATED_OUTDIR}'] |
Scott Graham | 3be4b416 | 2017-09-12 00:41:41 | [diff] [blame] | 1429 | elif is_fuchsia and test_type != 'script': |
Brian Sheedy | 234580e5 | 2019-09-10 17:42:51 | [diff] [blame] | 1430 | cmdline += [ |
John Budorick | fb97a85 | 2017-12-20 20:10:19 | [diff] [blame] | 1431 | '../../testing/test_env.py', |
| 1432 | os.path.join('bin', 'run_%s' % target), |
Wez | 9d5c0b5 | 2018-12-04 00:53:44 | [diff] [blame] | 1433 | '--test-launcher-bot-mode', |
Sergey Ulanov | d851243b | 2019-06-25 00:33:47 | [diff] [blame] | 1434 | '--system-log-file', '${ISOLATED_OUTDIR}/system_log' |
John Budorick | fb97a85 | 2017-12-20 20:10:19 | [diff] [blame] | 1435 | ] |
Benjamin Pastene | 3bce864e | 2018-04-14 01:16:32 | [diff] [blame] | 1436 | elif is_simplechrome and test_type != 'script': |
Brian Sheedy | 234580e5 | 2019-09-10 17:42:51 | [diff] [blame] | 1437 | cmdline += [ |
Benjamin Pastene | 3bce864e | 2018-04-14 01:16:32 | [diff] [blame] | 1438 | '../../testing/test_env.py', |
| 1439 | os.path.join('bin', 'run_%s' % target), |
| 1440 | ] |
kylechar | 3970568 | 2017-01-19 14:37:23 | [diff] [blame] | 1441 | elif use_xvfb and test_type == 'windowed_test_launcher': |
Andrii Shyshkalov | c158e010 | 2018-01-10 05:52:00 | [diff] [blame] | 1442 | extra_files.append('../../testing/xvfb.py') |
Brian Sheedy | 234580e5 | 2019-09-10 17:42:51 | [diff] [blame] | 1443 | cmdline += [ |
Nico Weber | a7bc1cb | 2019-06-15 17:42:39 | [diff] [blame] | 1444 | '../../testing/xvfb.py', |
| 1445 | './' + str(executable) + executable_suffix, |
| 1446 | '--test-launcher-bot-mode', |
| 1447 | '--asan=%d' % asan, |
| 1448 | # Enable lsan when asan is enabled except on Windows where LSAN isn't |
| 1449 | # supported. |
| 1450 | # TODO(https://crbug.com/948939): Enable on Mac once things pass. |
Caleb Raitto | f983d10 | 2019-06-21 23:05:02 | [diff] [blame] | 1451 | # TODO(https://crbug.com/974478): Enable on ChromeOS once things pass. |
| 1452 | '--lsan=%d' % (asan and not is_mac and not is_win and not is_cros), |
Nico Weber | a7bc1cb | 2019-06-15 17:42:39 | [diff] [blame] | 1453 | '--msan=%d' % msan, |
| 1454 | '--tsan=%d' % tsan, |
| 1455 | '--cfi-diag=%d' % cfi_diag, |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 1456 | ] |
| 1457 | elif test_type in ('windowed_test_launcher', 'console_test_launcher'): |
Brian Sheedy | 234580e5 | 2019-09-10 17:42:51 | [diff] [blame] | 1458 | cmdline += [ |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 1459 | '../../testing/test_env.py', |
dpranke | fe0d35e | 2016-02-05 02:43:59 | [diff] [blame] | 1460 | './' + str(executable) + executable_suffix, |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1461 | '--test-launcher-bot-mode', |
| 1462 | '--asan=%d' % asan, |
Caleb Raitto | 1fb2cc9e | 2019-06-14 01:04:23 | [diff] [blame] | 1463 | # Enable lsan when asan is enabled except on Windows where LSAN isn't |
| 1464 | # supported. |
Nico Weber | a7bc1cb | 2019-06-15 17:42:39 | [diff] [blame] | 1465 | # TODO(https://crbug.com/948939): Enable on Mac once things pass. |
Caleb Raitto | f983d10 | 2019-06-21 23:05:02 | [diff] [blame] | 1466 | # TODO(https://crbug.com/974478): Enable on ChromeOS once things pass. |
| 1467 | '--lsan=%d' % (asan and not is_mac and not is_win and not is_cros), |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1468 | '--msan=%d' % msan, |
| 1469 | '--tsan=%d' % tsan, |
pcc | 46233c2 | 2017-06-20 22:11:41 | [diff] [blame] | 1470 | '--cfi-diag=%d' % cfi_diag, |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 1471 | ] |
dpranke | 6abd865 | 2015-08-28 03:21:11 | [diff] [blame] | 1472 | elif test_type == 'script': |
Ben Pastene | 4534c39e | 2019-07-08 22:55:34 | [diff] [blame] | 1473 | # If we're testing a CrOS simplechrome build, assume we need to prepare a |
| 1474 | # DUT for testing. So prepend the command to run with the test wrapper. |
Ben Pastene | 8ab6954d | 2018-05-04 04:08:24 | [diff] [blame] | 1475 | if is_simplechrome: |
Ben Pastene | 908863c | 2019-07-25 16:20:03 | [diff] [blame] | 1476 | cmdline = [ |
| 1477 | os.path.join('bin', 'cros_test_wrapper'), |
| 1478 | '--logs-dir=${ISOLATED_OUTDIR}', |
| 1479 | ] |
Ben Pastene | 8ab6954d | 2018-05-04 04:08:24 | [diff] [blame] | 1480 | cmdline += [ |
dpranke | 6abd865 | 2015-08-28 03:21:11 | [diff] [blame] | 1481 | '../../testing/test_env.py', |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1482 | '../../' + self.ToSrcRelPath(isolate_map[target]['script']) |
dpranke | fe0d35e | 2016-02-05 02:43:59 | [diff] [blame] | 1483 | ] |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 1484 | elif test_type in ('raw', 'additional_compile_target'): |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 1485 | cmdline = [ |
| 1486 | './' + str(target) + executable_suffix, |
dpranke | fe0d35e | 2016-02-05 02:43:59 | [diff] [blame] | 1487 | ] |
dpranke | a55584f1 | 2015-07-22 00:52:47 | [diff] [blame] | 1488 | else: |
| 1489 | self.WriteFailureAndRaise('No command line for %s found (test type %s).' |
| 1490 | % (target, test_type), output_path=None) |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1491 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1492 | cmdline += isolate_map[target].get('args', []) |
dpranke | fe0d35e | 2016-02-05 02:43:59 | [diff] [blame] | 1493 | |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1494 | return cmdline, extra_files |
| 1495 | |
dpranke | 74559b5 | 2015-06-10 21:20:39 | [diff] [blame] | 1496 | def ToAbsPath(self, build_path, *comps): |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 1497 | return self.PathJoin(self.chromium_src_dir, |
| 1498 | self.ToSrcRelPath(build_path), |
| 1499 | *comps) |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1500 | |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 1501 | def ToSrcRelPath(self, path): |
| 1502 | """Returns a relative path from the top of the repo.""" |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 1503 | if path.startswith('//'): |
| 1504 | return path[2:].replace('/', self.sep) |
| 1505 | return self.RelPath(path, self.chromium_src_dir) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1506 | |
Dirk Pranke | 0fd41bcd | 2015-06-19 00:05:50 | [diff] [blame] | 1507 | def RunGNAnalyze(self, vals): |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1508 | # Analyze runs before 'gn gen' now, so we need to run gn gen |
Dirk Pranke | 0fd41bcd | 2015-06-19 00:05:50 | [diff] [blame] | 1509 | # in order to ensure that we have a build directory. |
Takuto Ikuta | 9dffd7e | 2018-09-05 01:04:00 | [diff] [blame] | 1510 | ret = self.RunGNGen(vals, compute_inputs_for_analyze=True, check=False) |
Dirk Pranke | 0fd41bcd | 2015-06-19 00:05:50 | [diff] [blame] | 1511 | if ret: |
| 1512 | return ret |
| 1513 | |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 1514 | build_path = self.args.path |
| 1515 | input_path = self.args.input_path |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1516 | gn_input_path = input_path + '.gn' |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 1517 | output_path = self.args.output_path |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1518 | gn_output_path = output_path + '.gn' |
| 1519 | |
dpranke | 7837fc36 | 2015-11-19 03:54:16 | [diff] [blame] | 1520 | inp = self.ReadInputJSON(['files', 'test_targets', |
| 1521 | 'additional_compile_targets']) |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 1522 | if self.args.verbose: |
| 1523 | self.Print() |
| 1524 | self.Print('analyze input:') |
| 1525 | self.PrintJSON(inp) |
| 1526 | self.Print() |
| 1527 | |
dpranke | 7673466 | 2015-04-16 02:17:50 | [diff] [blame] | 1528 | |
dpranke | 7c5f614d | 2015-07-22 23:43:39 | [diff] [blame] | 1529 | # This shouldn't normally happen, but could due to unusual race conditions, |
| 1530 | # like a try job that gets scheduled before a patch lands but runs after |
| 1531 | # the patch has landed. |
| 1532 | if not inp['files']: |
| 1533 | self.Print('Warning: No files modified in patch, bailing out early.') |
dpranke | 7837fc36 | 2015-11-19 03:54:16 | [diff] [blame] | 1534 | self.WriteJSON({ |
| 1535 | 'status': 'No dependency', |
| 1536 | 'compile_targets': [], |
| 1537 | 'test_targets': [], |
| 1538 | }, output_path) |
dpranke | 7c5f614d | 2015-07-22 23:43:39 | [diff] [blame] | 1539 | return 0 |
| 1540 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1541 | gn_inp = {} |
dpranke | b7b183f | 2017-04-24 23:50:16 | [diff] [blame] | 1542 | gn_inp['files'] = ['//' + f for f in inp['files'] if not f.startswith('//')] |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 1543 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1544 | isolate_map = self.ReadIsolateMap() |
| 1545 | err, gn_inp['additional_compile_targets'] = self.MapTargetsToLabels( |
| 1546 | isolate_map, inp['additional_compile_targets']) |
| 1547 | if err: |
| 1548 | raise MBErr(err) |
| 1549 | |
| 1550 | err, gn_inp['test_targets'] = self.MapTargetsToLabels( |
| 1551 | isolate_map, inp['test_targets']) |
| 1552 | if err: |
| 1553 | raise MBErr(err) |
| 1554 | labels_to_targets = {} |
| 1555 | for i, label in enumerate(gn_inp['test_targets']): |
| 1556 | labels_to_targets[label] = inp['test_targets'][i] |
| 1557 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 1558 | try: |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1559 | self.WriteJSON(gn_inp, gn_input_path) |
| 1560 | cmd = self.GNCmd('analyze', build_path, gn_input_path, gn_output_path) |
Debrian Figueroa | ae51d0d | 2019-07-22 18:04:11 | [diff] [blame] | 1561 | ret, output, _ = self.Run(cmd, force_verbose=True) |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1562 | if ret: |
Debrian Figueroa | ae51d0d | 2019-07-22 18:04:11 | [diff] [blame] | 1563 | if self.args.json_output: |
| 1564 | # write errors to json.output |
| 1565 | self.WriteJSON({'output': output}, self.args.json_output) |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1566 | return ret |
dpranke | 067d014 | 2015-05-14 22:52:45 | [diff] [blame] | 1567 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1568 | gn_outp_str = self.ReadFile(gn_output_path) |
| 1569 | try: |
| 1570 | gn_outp = json.loads(gn_outp_str) |
| 1571 | except Exception as e: |
| 1572 | self.Print("Failed to parse the JSON string GN returned: %s\n%s" |
| 1573 | % (repr(gn_outp_str), str(e))) |
| 1574 | raise |
| 1575 | |
| 1576 | outp = {} |
| 1577 | if 'status' in gn_outp: |
| 1578 | outp['status'] = gn_outp['status'] |
| 1579 | if 'error' in gn_outp: |
| 1580 | outp['error'] = gn_outp['error'] |
| 1581 | if 'invalid_targets' in gn_outp: |
| 1582 | outp['invalid_targets'] = gn_outp['invalid_targets'] |
| 1583 | if 'compile_targets' in gn_outp: |
Dirk Pranke | 4516507 | 2017-11-08 04:57:49 | [diff] [blame] | 1584 | all_input_compile_targets = sorted( |
| 1585 | set(inp['test_targets'] + inp['additional_compile_targets'])) |
| 1586 | |
| 1587 | # If we're building 'all', we can throw away the rest of the targets |
| 1588 | # since they're redundant. |
dpranke | 385a310 | 2016-09-20 22:04:08 | [diff] [blame] | 1589 | if 'all' in gn_outp['compile_targets']: |
| 1590 | outp['compile_targets'] = ['all'] |
| 1591 | else: |
Dirk Pranke | 4516507 | 2017-11-08 04:57:49 | [diff] [blame] | 1592 | outp['compile_targets'] = gn_outp['compile_targets'] |
| 1593 | |
| 1594 | # crbug.com/736215: When GN returns targets back, for targets in |
| 1595 | # the default toolchain, GN will have generated a phony ninja |
| 1596 | # target matching the label, and so we can safely (and easily) |
| 1597 | # transform any GN label into the matching ninja target. For |
| 1598 | # targets in other toolchains, though, GN doesn't generate the |
| 1599 | # phony targets, and we don't know how to turn the labels into |
| 1600 | # compile targets. In this case, we also conservatively give up |
| 1601 | # and build everything. Probably the right thing to do here is |
| 1602 | # to have GN return the compile targets directly. |
| 1603 | if any("(" in target for target in outp['compile_targets']): |
| 1604 | self.Print('WARNING: targets with non-default toolchains were ' |
| 1605 | 'found, building everything instead.') |
| 1606 | outp['compile_targets'] = all_input_compile_targets |
| 1607 | else: |
dpranke | 385a310 | 2016-09-20 22:04:08 | [diff] [blame] | 1608 | outp['compile_targets'] = [ |
Dirk Pranke | 4516507 | 2017-11-08 04:57:49 | [diff] [blame] | 1609 | label.replace('//', '') for label in outp['compile_targets']] |
| 1610 | |
| 1611 | # Windows has a maximum command line length of 8k; even Linux |
| 1612 | # maxes out at 128k; if analyze returns a *really long* list of |
| 1613 | # targets, we just give up and conservatively build everything instead. |
| 1614 | # Probably the right thing here is for ninja to support response |
| 1615 | # files as input on the command line |
| 1616 | # (see https://github.com/ninja-build/ninja/issues/1355). |
| 1617 | if len(' '.join(outp['compile_targets'])) > 7*1024: |
| 1618 | self.Print('WARNING: Too many compile targets were affected.') |
| 1619 | self.Print('WARNING: Building everything instead to avoid ' |
| 1620 | 'command-line length issues.') |
| 1621 | outp['compile_targets'] = all_input_compile_targets |
| 1622 | |
| 1623 | |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1624 | if 'test_targets' in gn_outp: |
| 1625 | outp['test_targets'] = [ |
| 1626 | labels_to_targets[label] for label in gn_outp['test_targets']] |
| 1627 | |
| 1628 | if self.args.verbose: |
| 1629 | self.Print() |
| 1630 | self.Print('analyze output:') |
| 1631 | self.PrintJSON(outp) |
| 1632 | self.Print() |
| 1633 | |
| 1634 | self.WriteJSON(outp, output_path) |
| 1635 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 1636 | finally: |
dpranke | cb4a2e24 | 2016-09-19 01:13:14 | [diff] [blame] | 1637 | if self.Exists(gn_input_path): |
| 1638 | self.RemoveFile(gn_input_path) |
| 1639 | if self.Exists(gn_output_path): |
| 1640 | self.RemoveFile(gn_output_path) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1641 | |
| 1642 | return 0 |
| 1643 | |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1644 | def ReadInputJSON(self, required_keys): |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 1645 | path = self.args.input_path |
| 1646 | output_path = self.args.output_path |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1647 | if not self.Exists(path): |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 1648 | self.WriteFailureAndRaise('"%s" does not exist' % path, output_path) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1649 | |
| 1650 | try: |
| 1651 | inp = json.loads(self.ReadFile(path)) |
| 1652 | except Exception as e: |
| 1653 | self.WriteFailureAndRaise('Failed to read JSON input from "%s": %s' % |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 1654 | (path, e), output_path) |
dpranke | d811358 | 2015-06-05 20:08:25 | [diff] [blame] | 1655 | |
| 1656 | for k in required_keys: |
| 1657 | if not k in inp: |
| 1658 | self.WriteFailureAndRaise('input file is missing a "%s" key' % k, |
| 1659 | output_path) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1660 | |
| 1661 | return inp |
| 1662 | |
dpranke | d5b2b943 | 2015-06-23 16:55:30 | [diff] [blame] | 1663 | def WriteFailureAndRaise(self, msg, output_path): |
| 1664 | if output_path: |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 1665 | self.WriteJSON({'error': msg}, output_path, force_verbose=True) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1666 | raise MBErr(msg) |
| 1667 | |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 1668 | def WriteJSON(self, obj, path, force_verbose=False): |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 1669 | try: |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 1670 | self.WriteFile(path, json.dumps(obj, indent=2, sort_keys=True) + '\n', |
| 1671 | force_verbose=force_verbose) |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 1672 | except Exception as e: |
| 1673 | raise MBErr('Error %s writing to the output path "%s"' % |
| 1674 | (e, path)) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1675 | |
aneeshm | de50f47 | 2016-04-01 01:13:10 | [diff] [blame] | 1676 | def CheckCompile(self, master, builder): |
| 1677 | url_template = self.args.url_template + '/{builder}/builds/_all?as_text=1' |
| 1678 | url = urllib2.quote(url_template.format(master=master, builder=builder), |
| 1679 | safe=':/()?=') |
| 1680 | try: |
| 1681 | builds = json.loads(self.Fetch(url)) |
| 1682 | except Exception as e: |
| 1683 | return str(e) |
| 1684 | successes = sorted( |
| 1685 | [int(x) for x in builds.keys() if "text" in builds[x] and |
| 1686 | cmp(builds[x]["text"][:2], ["build", "successful"]) == 0], |
| 1687 | reverse=True) |
| 1688 | if not successes: |
| 1689 | return "no successful builds" |
| 1690 | build = builds[str(successes[0])] |
| 1691 | step_names = set([step["name"] for step in build["steps"]]) |
| 1692 | compile_indicators = set(["compile", "compile (with patch)", "analyze"]) |
| 1693 | if compile_indicators & step_names: |
| 1694 | return "compiles" |
| 1695 | return "does not compile" |
| 1696 | |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame] | 1697 | def PrintCmd(self, cmd, env): |
| 1698 | if self.platform == 'win32': |
| 1699 | env_prefix = 'set ' |
| 1700 | env_quoter = QuoteForSet |
| 1701 | shell_quoter = QuoteForCmd |
| 1702 | else: |
| 1703 | env_prefix = '' |
| 1704 | env_quoter = pipes.quote |
| 1705 | shell_quoter = pipes.quote |
| 1706 | |
| 1707 | def print_env(var): |
| 1708 | if env and var in env: |
| 1709 | self.Print('%s%s=%s' % (env_prefix, var, env_quoter(env[var]))) |
| 1710 | |
dpranke | ec07926 | 2016-06-07 02:21:20 | [diff] [blame] | 1711 | print_env('LLVM_FORCE_HEAD_REVISION') |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame] | 1712 | |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 1713 | if cmd[0] == self.executable: |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1714 | cmd = ['python'] + cmd[1:] |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame] | 1715 | self.Print(*[shell_quoter(arg) for arg in cmd]) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1716 | |
dpranke | cda0033 | 2015-04-11 04:18:32 | [diff] [blame] | 1717 | def PrintJSON(self, obj): |
| 1718 | self.Print(json.dumps(obj, indent=2, sort_keys=True)) |
| 1719 | |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1720 | def Build(self, target): |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 1721 | build_dir = self.ToSrcRelPath(self.args.path) |
Mike Meade | 9c100ff | 2018-03-30 23:09:38 | [diff] [blame] | 1722 | if self.platform == 'win32': |
| 1723 | # On Windows use the batch script since there is no exe |
| 1724 | ninja_cmd = ['autoninja.bat', '-C', build_dir] |
| 1725 | else: |
| 1726 | ninja_cmd = ['autoninja', '-C', build_dir] |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1727 | if self.args.jobs: |
| 1728 | ninja_cmd.extend(['-j', '%d' % self.args.jobs]) |
| 1729 | ninja_cmd.append(target) |
Dirk Pranke | 5f22a82 | 2019-05-23 22:55:25 | [diff] [blame] | 1730 | ret, _, _ = self.Run(ninja_cmd, buffer_output=False) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1731 | return ret |
| 1732 | |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 1733 | def Run(self, cmd, env=None, force_verbose=True, buffer_output=True, |
| 1734 | stdin=None): |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1735 | # This function largely exists so it can be overridden for testing. |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 1736 | if self.args.dryrun or self.args.verbose or force_verbose: |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame] | 1737 | self.PrintCmd(cmd, env) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1738 | if self.args.dryrun: |
| 1739 | return 0, '', '' |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 1740 | |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 1741 | ret, out, err = self.Call(cmd, env=env, buffer_output=buffer_output, |
| 1742 | stdin=stdin) |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 1743 | if self.args.verbose or force_verbose: |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1744 | if ret: |
| 1745 | self.Print(' -> returned %d' % ret) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1746 | if out: |
Debrian Figueroa | ae58223 | 2019-07-17 01:54:45 | [diff] [blame] | 1747 | # This is the error seen on the logs |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 1748 | self.Print(out, end='') |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1749 | if err: |
dpranke | ee5b51f6 | 2015-04-09 00:03:22 | [diff] [blame] | 1750 | self.Print(err, end='', file=sys.stderr) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1751 | return ret, out, err |
| 1752 | |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 1753 | def Call(self, cmd, env=None, buffer_output=True, stdin=None): |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1754 | if buffer_output: |
| 1755 | p = subprocess.Popen(cmd, shell=False, cwd=self.chromium_src_dir, |
| 1756 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 1757 | env=env, stdin=subprocess.PIPE) |
| 1758 | out, err = p.communicate(input=stdin) |
dpranke | 751516a | 2015-10-03 01:11:34 | [diff] [blame] | 1759 | else: |
| 1760 | p = subprocess.Popen(cmd, shell=False, cwd=self.chromium_src_dir, |
| 1761 | env=env) |
| 1762 | p.wait() |
| 1763 | out = err = '' |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1764 | return p.returncode, out, err |
| 1765 | |
| 1766 | def ExpandUser(self, path): |
| 1767 | # This function largely exists so it can be overridden for testing. |
| 1768 | return os.path.expanduser(path) |
| 1769 | |
| 1770 | def Exists(self, path): |
| 1771 | # This function largely exists so it can be overridden for testing. |
| 1772 | return os.path.exists(path) |
| 1773 | |
dpranke | 867bcf4a | 2016-03-14 22:28:32 | [diff] [blame] | 1774 | def Fetch(self, url): |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 1775 | # This function largely exists so it can be overridden for testing. |
dpranke | 867bcf4a | 2016-03-14 22:28:32 | [diff] [blame] | 1776 | f = urllib2.urlopen(url) |
| 1777 | contents = f.read() |
| 1778 | f.close() |
| 1779 | return contents |
| 1780 | |
dpranke | c3441d1 | 2015-06-23 23:01:35 | [diff] [blame] | 1781 | def MaybeMakeDirectory(self, path): |
| 1782 | try: |
| 1783 | os.makedirs(path) |
| 1784 | except OSError, e: |
| 1785 | if e.errno != errno.EEXIST: |
| 1786 | raise |
| 1787 | |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 1788 | def PathJoin(self, *comps): |
| 1789 | # This function largely exists so it can be overriden for testing. |
| 1790 | return os.path.join(*comps) |
| 1791 | |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 1792 | def Print(self, *args, **kwargs): |
| 1793 | # This function largely exists so it can be overridden for testing. |
| 1794 | print(*args, **kwargs) |
aneeshm | de50f47 | 2016-04-01 01:13:10 | [diff] [blame] | 1795 | if kwargs.get('stream', sys.stdout) == sys.stdout: |
| 1796 | sys.stdout.flush() |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 1797 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1798 | def ReadFile(self, path): |
| 1799 | # This function largely exists so it can be overriden for testing. |
| 1800 | with open(path) as fp: |
| 1801 | return fp.read() |
| 1802 | |
dpranke | 030d7a6d | 2016-03-26 17:23:50 | [diff] [blame] | 1803 | def RelPath(self, path, start='.'): |
| 1804 | # This function largely exists so it can be overriden for testing. |
| 1805 | return os.path.relpath(path, start) |
| 1806 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 1807 | def RemoveFile(self, path): |
| 1808 | # This function largely exists so it can be overriden for testing. |
| 1809 | os.remove(path) |
| 1810 | |
dpranke | c161aa9 | 2015-09-14 20:21:13 | [diff] [blame] | 1811 | def RemoveDirectory(self, abs_path): |
dpranke | 8c2cfd3 | 2015-09-17 20:12:33 | [diff] [blame] | 1812 | if self.platform == 'win32': |
dpranke | c161aa9 | 2015-09-14 20:21:13 | [diff] [blame] | 1813 | # In other places in chromium, we often have to retry this command |
| 1814 | # because we're worried about other processes still holding on to |
| 1815 | # file handles, but when MB is invoked, it will be early enough in the |
| 1816 | # build that their should be no other processes to interfere. We |
| 1817 | # can change this if need be. |
| 1818 | self.Run(['cmd.exe', '/c', 'rmdir', '/q', '/s', abs_path]) |
| 1819 | else: |
| 1820 | shutil.rmtree(abs_path, ignore_errors=True) |
| 1821 | |
Dirk Pranke | f24e6b2 | 2018-03-27 20:12:30 | [diff] [blame] | 1822 | def TempDir(self): |
| 1823 | # This function largely exists so it can be overriden for testing. |
| 1824 | return tempfile.mkdtemp(prefix='mb_') |
| 1825 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 1826 | def TempFile(self, mode='w'): |
| 1827 | # This function largely exists so it can be overriden for testing. |
| 1828 | return tempfile.NamedTemporaryFile(mode=mode, delete=False) |
| 1829 | |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 1830 | def WriteFile(self, path, contents, force_verbose=False): |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1831 | # This function largely exists so it can be overriden for testing. |
dpranke | e0547cd | 2015-09-15 01:27:40 | [diff] [blame] | 1832 | if self.args.dryrun or self.args.verbose or force_verbose: |
dpranke | d5b2b943 | 2015-06-23 16:55:30 | [diff] [blame] | 1833 | self.Print('\nWriting """\\\n%s""" to %s.\n' % (contents, path)) |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1834 | with open(path, 'w') as fp: |
| 1835 | return fp.write(contents) |
| 1836 | |
dpranke | f61de2f | 2015-05-14 04:09:56 | [diff] [blame] | 1837 | |
Stephen Martinis | cd37701 | 2019-10-18 17:40:46 | [diff] [blame] | 1838 | class LedResult(object): |
| 1839 | """Holds the result of a led operation. Can be chained using |then|.""" |
| 1840 | |
| 1841 | def __init__(self, result, run_cmd): |
| 1842 | self._result = result |
| 1843 | self._run_cmd = run_cmd |
| 1844 | |
| 1845 | @property |
| 1846 | def result(self): |
| 1847 | """The mutable result data of the previous led call as decoded JSON.""" |
| 1848 | return self._result |
| 1849 | |
| 1850 | def then(self, *cmd): |
| 1851 | """Invoke led, passing it the current `result` data as input. |
| 1852 | |
| 1853 | Returns another LedResult object with the output of the command. |
| 1854 | """ |
| 1855 | return self.__class__( |
| 1856 | self._run_cmd(self._result, cmd), self._run_cmd) |
| 1857 | |
| 1858 | |
| 1859 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1860 | class MBErr(Exception): |
| 1861 | pass |
| 1862 | |
| 1863 | |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame] | 1864 | # See http://goo.gl/l5NPDW and http://goo.gl/4Diozm for the painful |
| 1865 | # details of this next section, which handles escaping command lines |
| 1866 | # so that they can be copied and pasted into a cmd window. |
| 1867 | UNSAFE_FOR_SET = set('^<>&|') |
| 1868 | UNSAFE_FOR_CMD = UNSAFE_FOR_SET.union(set('()%')) |
| 1869 | ALL_META_CHARS = UNSAFE_FOR_CMD.union(set('"')) |
| 1870 | |
| 1871 | |
| 1872 | def QuoteForSet(arg): |
| 1873 | if any(a in UNSAFE_FOR_SET for a in arg): |
| 1874 | arg = ''.join('^' + a if a in UNSAFE_FOR_SET else a for a in arg) |
| 1875 | return arg |
| 1876 | |
| 1877 | |
| 1878 | def QuoteForCmd(arg): |
| 1879 | # First, escape the arg so that CommandLineToArgvW will parse it properly. |
dpranke | 3cec199c | 2015-09-22 23:29:02 | [diff] [blame] | 1880 | if arg == '' or ' ' in arg or '"' in arg: |
| 1881 | quote_re = re.compile(r'(\\*)"') |
| 1882 | arg = '"%s"' % (quote_re.sub(lambda mo: 2 * mo.group(1) + '\\"', arg)) |
| 1883 | |
| 1884 | # Then check to see if the arg contains any metacharacters other than |
| 1885 | # double quotes; if it does, quote everything (including the double |
| 1886 | # quotes) for safety. |
| 1887 | if any(a in UNSAFE_FOR_CMD for a in arg): |
| 1888 | arg = ''.join('^' + a if a in ALL_META_CHARS else a for a in arg) |
| 1889 | return arg |
| 1890 | |
| 1891 | |
dpranke | fe460231 | 2015-04-08 16:20:35 | [diff] [blame] | 1892 | if __name__ == '__main__': |
dpranke | 255085e | 2016-03-16 05:23:59 | [diff] [blame] | 1893 | sys.exit(main(sys.argv[1:])) |