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 | |
|
jochen (gone - plz use gerrit)
2016/07/18 11:28:29
shouldn't that be something like -b 'V8 Linux64 -
Michael Achenbach
2016/07/21 13:13:29
No. All our standard configs have their own shortc
| |
| 16 | |
| 17 # Pass additional gn arguments after -- (don't use spaces within gn args). | |
| 18 v8gen.py x64.optdebug -- use_goma=true | |
|
jochen (gone - plz use gerrit)
2016/07/18 11:28:29
use_goma would be -g - maybe use another example h
Michael Achenbach
2016/07/21 13:13:29
I'll update this example with another flag than go
| |
| 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) | |
|
vogelheim
2016/07/18 18:28:26
I think I'd find an re-based version more readable
Michael Achenbach
2016/07/21 13:13:29
Not sure if I understand what you mean by re-based
vogelheim
2016/07/21 13:19:54
Sorry for being unclear.
What I meant: Based on r
| |
| 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 self._options = self._parse_arguments(args[:index]) | |
| 51 self._gn_args = args[index + 1:] | |
| 52 | |
| 53 def _parse_arguments(self, args): | |
| 54 parser = argparse.ArgumentParser( | |
| 55 description='Generator for V8\'s gn arguments. ' | |
| 56 'Additional gn args can be separated with -- ' | |
| 57 'from the arguments to this tool (don\'t use spaces or ' | |
| 58 'quote arguments like "use_goma = true").') | |
| 59 parser.add_argument( | |
| 60 'outdir', nargs='?', | |
| 61 help='optional gn output directory') | |
| 62 parser.add_argument( | |
| 63 '-b', '--builder', | |
| 64 help='build configuration or builder name from mb_config.pyl') | |
| 65 parser.add_argument( | |
| 66 '-m', '--master', default='developer_default', | |
| 67 help='config group or master from mb_config.pyl') | |
| 68 parser.add_argument( | |
| 69 '-p', '--pedantic', action='store_true', | |
| 70 help='run gn over command-line gn args to catch errors early') | |
| 71 parser.add_argument( | |
| 72 '-v', '--verbosity', action='count', | |
| 73 help='increase output verbosity (can be specified multiple times)') | |
| 74 | |
| 75 goma = parser.add_mutually_exclusive_group() | |
| 76 goma.add_argument( | |
| 77 '-g' , '--goma', | |
| 78 action='store_true', default=None, dest='goma', | |
| 79 help='use goma') | |
| 80 goma.add_argument( | |
| 81 '--nogoma', '--no-goma', | |
| 82 action='store_false', default=None, dest='goma', | |
| 83 help='don\'t use goma') | |
| 84 | |
| 85 options = parser.parse_args(args) | |
| 86 | |
| 87 if not options.outdir and not options.builder: | |
| 88 parser.error('please specify either an output directory or ' | |
| 89 'a builder/config name (-b), e.g. x64.release') | |
| 90 | |
| 91 if not options.outdir: | |
| 92 # Derive output directory from builder name. | |
| 93 options.outdir = _sanitize_nonalpha(options.builder) | |
| 94 else: | |
| 95 # Also, if this should work on windows, we might need to use \ where | |
| 96 # outdir is used as path, while using / if it's used in a gn context. | |
| 97 if options.outdir.startswith('/'): | |
| 98 parser.error( | |
| 99 'only output directories relative to %s are supported' % OUT_DIR) | |
| 100 | |
| 101 if not options.builder: | |
| 102 # Derive builder from output directory. | |
| 103 options.builder = options.outdir | |
| 104 | |
| 105 return options | |
| 106 | |
| 107 def verbose_print_1(self, text): | |
| 108 if self._options.verbosity >= 1: | |
| 109 print '#' * 80 | |
| 110 print text | |
| 111 | |
| 112 def verbose_print_2(self, text): | |
| 113 if self._options.verbosity >= 2: | |
| 114 indent = ' ' * 2 | |
| 115 for l in text.splitlines(): | |
| 116 print indent + l | |
| 117 | |
| 118 def _call_cmd(self, args): | |
| 119 self.verbose_print_1(' '.join(args)) | |
| 120 try: | |
| 121 output = subprocess.check_output( | |
| 122 args=args, | |
| 123 stderr=subprocess.STDOUT, | |
| 124 ) | |
| 125 self.verbose_print_2(output) | |
| 126 except subprocess.CalledProcessError as e: | |
| 127 self.verbose_print_2(e.output) | |
| 128 raise | |
| 129 | |
| 130 def _find_work_dir(self, path): | |
| 131 """Find the closest v8 root to `path`.""" | |
| 132 if os.path.exists(os.path.join(path, 'tools', 'dev', 'v8gen.py')): | |
| 133 # Approximate the v8 root dir by a folder where this script exists | |
| 134 # in the expected place. | |
| 135 return path | |
| 136 elif os.path.dirname(path) == path: | |
| 137 raise Exception( | |
| 138 'This appears to not be called from a recent v8 checkout') | |
| 139 else: | |
| 140 return self._find_work_dir(os.path.dirname(path)) | |
| 141 | |
| 142 @property | |
| 143 def _goma_dir(self): | |
| 144 return os.path.normpath(os.environ.get('GOMA_DIR') or GOMA_DEFAULT) | |
| 145 | |
| 146 @property | |
| 147 def _need_goma_dir(self): | |
| 148 return self._goma_dir != GOMA_DEFAULT | |
| 149 | |
| 150 @property | |
| 151 def _use_goma(self): | |
| 152 if self._options.goma is None: | |
| 153 # Auto-detect. | |
| 154 return os.path.exists(self._goma_dir) and os.path.isdir(self._goma_dir) | |
| 155 else: | |
| 156 return self._options.goma | |
| 157 | |
| 158 @property | |
| 159 def _goma_args(self): | |
| 160 """Gn args for using goma.""" | |
| 161 # Specify goma args if we want to use goma and if goma isn't specified | |
| 162 # via command line already. The command-line always has precedence over | |
| 163 # any other specification. | |
| 164 if (self._use_goma and | |
| 165 not any(re.match(r'use_goma\s*=.*', x) for x in self._gn_args)): | |
| 166 if self._need_goma_dir: | |
| 167 return 'use_goma=true\ngoma_dir="%s"' % self._goma_dir | |
| 168 else: | |
| 169 return 'use_goma=true' | |
| 170 else: | |
| 171 return '' | |
| 172 | |
| 173 def _append_gn_args(self, type, gn_args_path, more_gn_args): | |
| 174 """Append extra gn arguments to the generated args.gn file.""" | |
| 175 if not more_gn_args: | |
| 176 return False | |
| 177 self.verbose_print_1('Appending """\n%s\n""" to %s.' % ( | |
| 178 more_gn_args, os.path.abspath(gn_args_path))) | |
| 179 with open(gn_args_path, 'a') as f: | |
| 180 f.write('\n# Additional %s args:\n' % type) | |
| 181 f.write(more_gn_args) | |
| 182 f.write('\n') | |
| 183 return True | |
| 184 | |
| 185 def main(self): | |
| 186 # Always operate relative to the base directory for better relative-path | |
| 187 # handling. This script can be used in any v8 checkout. | |
| 188 workdir = self._find_work_dir(os.getcwd()) | |
| 189 if workdir != os.getcwd(): | |
| 190 self.verbose_print_1('cd ' + workdir) | |
| 191 os.chdir(workdir) | |
| 192 | |
| 193 # The directories are separated with slashes in a gn context (platform | |
| 194 # independent). | |
| 195 gn_outdir = '/'.join([OUT_DIR, self._options.outdir]) | |
| 196 | |
| 197 # Call MB to generate the basic configuration. | |
| 198 self._call_cmd([ | |
| 199 sys.executable, | |
| 200 '-u', os.path.join('tools', 'mb', 'mb.py'), | |
| 201 'gen', | |
| 202 '-f', os.path.join('infra', 'mb', 'mb_config.pyl'), | |
| 203 '-m', self._options.master, | |
| 204 '-b', self._options.builder, | |
| 205 gn_outdir, | |
| 206 ]) | |
| 207 | |
| 208 # Handle extra gn arguments. | |
| 209 gn_args_path = os.path.join(OUT_DIR, self._options.outdir, 'args.gn') | |
| 210 | |
| 211 # Append command-line args. | |
| 212 modified = self._append_gn_args( | |
| 213 'command-line', gn_args_path, '\n'.join(self._gn_args)) | |
| 214 | |
| 215 # Append goma args. | |
| 216 # TODO(machenbach): We currently can't remove existing goma args from the | |
| 217 # original config. E.g. to build like a bot that uses goma, but switch | |
| 218 # goma off. | |
| 219 modified |= self._append_gn_args( | |
| 220 'goma', gn_args_path, self._goma_args) | |
| 221 | |
| 222 # Regenerate ninja files to check for errors in the additional gn args. | |
| 223 if modified and self._options.pedantic: | |
| 224 self._call_cmd(['gn', 'gen', gn_outdir]) | |
| 225 return 0 | |
| 226 | |
| 227 if __name__ == "__main__": | |
| 228 gen = GenerateGnArgs(sys.argv[1:]) | |
| 229 try: | |
| 230 sys.exit(gen.main()) | |
| 231 except Exception: | |
| 232 if gen._options.verbosity < 2: | |
| 233 print ('\nHint: You can raise verbosity (-vv) to see the output of ' | |
| 234 'failed commands.\n') | |
| 235 raise | |
| OLD | NEW |