Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2016 the V8 project 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 | |
| 6 """Script to generate V8's gn arguments based on common developer defaults | |
| 7 or builder configurations. | |
| 8 | |
| 9 Examples: | |
| 10 | |
| 11 # Generate the x64.release config in out.gn/x64.release. | |
| 12 v8gen.py x64.release | |
| 13 | |
| 14 # Generate into out.gn/foo. | |
| 15 v8gen.py -b x64.release foo | |
| 16 | |
| 17 # Pass additional gn arguments after -- (don't use spaces within gn args). | |
| 18 v8gen.py x64.optdebug -- use_goma=true | |
| 19 | |
| 20 # Print wrapped commands. | |
| 21 v8gen.py x64.optdebug -v | |
| 22 | |
| 23 # Print output of wrapped commands. | |
| 24 v8gen.py x64.optdebug -vv | |
| 25 | |
| 26 # Generate gn arguments of builder 'V8 Linux64 - builder' from master | |
| 27 # 'client.v8' into out.gn/V8_Linux64___builder. | |
| 28 v8gen.py -m client.v8 -b 'V8 Linux64 - builder' | |
| 29 """ | |
| 30 | |
| 31 import argparse | |
| 32 import os | |
| 33 import re | |
| 34 import subprocess | |
| 35 import sys | |
| 36 | |
| 37 GOMA_DEFAULT = os.path.join(os.path.expanduser("~"), 'goma') | |
| 38 OUT_DIR = 'out.gn' | |
| 39 | |
| 40 | |
| 41 def _sanitize_nonalpha(text): | |
| 42 return ''.join(c if (c.isalnum() or c == '.') else '_' for c in text) | |
| 43 | |
| 44 | |
| 45 class GenerateGnArgs(object): | |
| 46 def __init__(self, args): | |
| 47 # Split args into this script's arguments and gn args passed to the | |
| 48 # wrapped gn. | |
| 49 index = args.index('--') if '--' in args else len(args) | |
| 50 script_args = args[:index] | |
| 51 self.gn_args = args[index + 1:] | |
|
tandrii(chromium)
2016/07/14 12:45:28
here and also for self.options: since you are mark
Michael Achenbach
2016/07/15 14:18:09
Done.
| |
| 52 | |
| 53 parser = argparse.ArgumentParser( | |
|
tandrii(chromium)
2016/07/14 12:45:28
i'd put all code from here to end of __init__ into
Michael Achenbach
2016/07/15 14:18:09
Done.
| |
| 54 description='Generator for V8\'s gn arguments. ' | |
| 55 'Additional gn args can be separated with -- ' | |
| 56 'from the arguments to this tool (don\'t use spaces or ' | |
| 57 'quote arguments like "use_goma = true").') | |
| 58 parser.add_argument( | |
| 59 'outdir', nargs='?', | |
| 60 help='optional gn output directory') | |
| 61 parser.add_argument( | |
| 62 '-b', '--builder', | |
| 63 help='build configuration or builder name from mb_config.pyl') | |
| 64 parser.add_argument( | |
| 65 '-m', '--master', default='developer_default', | |
| 66 help='config group or master from mb_config.pyl') | |
| 67 parser.add_argument( | |
| 68 '-p', '--pedantic', action='store_true', | |
| 69 help='run gn over command-line gn args to catch errors early') | |
| 70 parser.add_argument( | |
| 71 '-v', '--verbosity', action='count', | |
| 72 help='increase output verbosity (can be specified multiple times)') | |
| 73 | |
| 74 goma = parser.add_mutually_exclusive_group() | |
| 75 goma.add_argument( | |
| 76 '-g' , '--goma', | |
| 77 action='store_true', default=None, dest='goma', | |
| 78 help='use goma') | |
| 79 goma.add_argument( | |
| 80 '--nogoma', '--no-goma', | |
| 81 action='store_false', default=None, dest='goma', | |
| 82 help='don\'t use goma') | |
| 83 | |
| 84 self.options = parser.parse_args(script_args) | |
| 85 | |
| 86 assert (self.options.outdir or self.options.builder), ( | |
|
tandrii(chromium)
2016/07/14 12:45:29
if ...:
parser.error(...) # quits with ret code
Michael Achenbach
2016/07/15 14:18:09
Done.
| |
| 87 'please specify either an output directory or a builder/config name, ' | |
| 88 'e.g. x64.release') | |
| 89 | |
| 90 if not self.options.outdir: | |
| 91 # Derive output directory from builder name. | |
| 92 self.options.outdir = _sanitize_nonalpha(self.options.builder) | |
| 93 else: | |
| 94 # Also, if this should work on windows, we might need to use \ where | |
| 95 # outdir is used as path, while using / if it's used in a gn context. | |
| 96 assert not self.options.outdir.startswith('/'), ( | |
| 97 'only output directories relative to %s are supported' % OUT_DIR) | |
|
tandrii(chromium)
2016/07/14 12:45:29
ditto, use parser.error()
Michael Achenbach
2016/07/15 14:18:10
Done.
| |
| 98 | |
| 99 if not self.options.builder: | |
| 100 # Derive builder from output directory. | |
| 101 self.options.builder = self.options.outdir | |
| 102 | |
| 103 def verbose_print_1(self, text): | |
| 104 if self.options.verbosity >= 1: | |
| 105 print '#################################################################' | |
|
tandrii(chromium)
2016/07/14 12:45:29
ah, so verbose_print_1 means a section title?
then
Michael Achenbach
2016/07/15 14:18:09
Done.
| |
| 106 print text | |
| 107 | |
| 108 def verbose_print_2(self, text): | |
| 109 if self.options.verbosity >= 2: | |
| 110 print text | |
|
tandrii(chromium)
2016/07/14 12:45:29
after reading the whole class, I see that print-1
Michael Achenbach
2016/07/15 14:18:09
Done. But keeping the ### as it is even more reada
| |
| 111 | |
| 112 def call_cmd(self, args): | |
| 113 self.verbose_print_1(' '.join(args)) | |
| 114 try: | |
| 115 output = subprocess.check_output( | |
| 116 args=args, | |
| 117 stderr=subprocess.STDOUT, | |
| 118 ) | |
| 119 self.verbose_print_2(output) | |
| 120 except subprocess.CalledProcessError as e: | |
| 121 self.verbose_print_2(e.output) | |
| 122 raise | |
| 123 | |
| 124 def _find_work_dir(self, path): | |
| 125 """Find the closest v8 root to 'path'.""" | |
|
tandrii(chromium)
2016/07/14 12:45:29
nit: I think `path` is proper way to mark vars, bu
Michael Achenbach
2016/07/15 14:18:09
Done.
| |
| 126 if os.path.exists(os.path.join(path, 'tools', 'dev', 'v8gen.py')): | |
| 127 # Approximate the v8 root dir by a folder where this script exists | |
| 128 # in the expected place. | |
| 129 return path | |
| 130 elif os.path.dirname(path) == path: | |
| 131 raise Exception( | |
| 132 'This appears to not be called from a recent v8 checkout') | |
| 133 else: | |
| 134 return self._find_work_dir(os.path.dirname(path)) | |
| 135 | |
| 136 @property | |
| 137 def _goma_dir(self): | |
| 138 return os.path.normpath(os.environ.get('GOMA_DIR') or GOMA_DEFAULT) | |
| 139 | |
| 140 @property | |
| 141 def _need_goma_dir(self): | |
| 142 return self._goma_dir != GOMA_DEFAULT | |
| 143 | |
| 144 @property | |
| 145 def _use_goma(self): | |
| 146 if self.options.goma is None: | |
| 147 # Auto-detect. | |
| 148 return os.path.exists(self._goma_dir) and os.path.isdir(self._goma_dir) | |
| 149 else: | |
| 150 return self.options.goma | |
| 151 | |
| 152 @property | |
| 153 def _goma_args(self): | |
| 154 """Gn args for using goma.""" | |
| 155 # Specify goma args if we want to use goma and if goma isn't specified | |
| 156 # via command line already. The command-line always has precedence over | |
| 157 # any other specification. | |
| 158 if (self._use_goma and | |
| 159 not [x for x in self.gn_args if re.match(r'use_goma\s*=.*', x)]): | |
|
tandrii(chromium)
2016/07/14 12:45:29
maybe
all(not re.match(r'use_goma\s*=.*', x) for
Michael Achenbach
2016/07/15 14:18:09
Done.
| |
| 160 if self._need_goma_dir: | |
| 161 return 'use_goma=true\ngoma_dir="%s"' % self._goma_dir | |
| 162 else: | |
| 163 return 'use_goma=true' | |
| 164 else: | |
| 165 return '' | |
| 166 | |
| 167 def _append_gn_args(self, type, gn_args_path, more_gn_args): | |
| 168 """Append extra gn arguments to the generated args.gn file.""" | |
| 169 if not more_gn_args: | |
| 170 return False | |
| 171 self.verbose_print_1("Appending \"\"\"\n%s\n\"\"\" to %s." % ( | |
|
tandrii(chromium)
2016/07/14 12:45:28
use singular quotes and string would be easier to
Michael Achenbach
2016/07/15 14:18:09
Weird, I'm using single quotes in the rest of the
tandrii(chromium)
2016/07/15 14:37:25
yep, i wondered too :)
| |
| 172 more_gn_args, os.path.abspath(gn_args_path))) | |
| 173 with open(gn_args_path, 'a') as f: | |
| 174 f.write('\n# Additional %s args:\n' % type) | |
| 175 f.write(more_gn_args) | |
| 176 f.write('\n') | |
| 177 return True | |
| 178 | |
| 179 def main(self): | |
| 180 # Always operate relative to the base directory for better relative-path | |
| 181 # handling. This script can be used in any v8 checkout. | |
| 182 workdir = self._find_work_dir(os.getcwd()) | |
| 183 if workdir != os.getcwd(): | |
| 184 self.verbose_print_1('cd ' + workdir) | |
| 185 os.chdir(workdir) | |
| 186 | |
| 187 # The directories are separated with slashes in a gn context (platform | |
| 188 # independent). | |
| 189 gn_outdir = '/'.join([OUT_DIR, self.options.outdir]) | |
| 190 | |
| 191 # Call MB to generate the basic configuration. | |
| 192 self.call_cmd([ | |
| 193 sys.executable, | |
| 194 '-u', os.path.join('tools', 'mb', 'mb.py'), | |
| 195 'gen', | |
| 196 '-f', os.path.join('infra', 'mb', 'mb_config.pyl'), | |
| 197 '-m', self.options.master, | |
| 198 '-b', self.options.builder, | |
| 199 gn_outdir, | |
| 200 ]) | |
| 201 | |
| 202 # Handle extra gn arguments. | |
| 203 gn_args_path = os.path.join(OUT_DIR, self.options.outdir, 'args.gn') | |
| 204 more_gn_args = '\n'.join(self.gn_args) | |
|
tandrii(chromium)
2016/07/15 14:37:25
good, i didn't notice.
| |
| 205 | |
| 206 # Append command-line args. | |
| 207 modified = self._append_gn_args( | |
| 208 'command-line', gn_args_path, '\n'.join(self.gn_args)) | |
| 209 | |
| 210 # Append goma args. | |
| 211 # TODO(machenbach): We currently can't remove existing goma args from the | |
| 212 # original config. E.g. to build like a bot that uses goma, but switch | |
| 213 # goma off. | |
| 214 modified |= self._append_gn_args( | |
| 215 'goma', gn_args_path, self._goma_args) | |
| 216 | |
| 217 # Regenerate ninja files to check for errors in the additional gn args. | |
| 218 if modified and self.options.pedantic: | |
| 219 self.call_cmd(['gn', 'gen', gn_outdir]) | |
| 220 return 0 | |
| 221 | |
| 222 if __name__ == "__main__": | |
| 223 gen = GenerateGnArgs(sys.argv[1:]) | |
| 224 try: | |
| 225 sys.exit(gen.main()) | |
| 226 except Exception: | |
| 227 if gen.options.verbosity < 2: | |
| 228 print ('\nHint: You can raise verbosity (-vv) to see the output of ' | |
| 229 'failed commands.\n') | |
| 230 raise | |
| OLD | NEW |