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 tools/tool.py x64.release | |
| 13 | |
| 14 # Generate into out.gn/foo. | |
| 15 tools/tool.py -b x64.release foo | |
| 16 | |
| 17 # Pass additional gn arguments after -- (don't use spaces within gn args). | |
| 18 tools/tool.py x64.optdebug -- use_goma=true | |
| 19 | |
| 20 # Print wrapped commands. | |
| 21 tools/tool.py x64.optdebug -v | |
| 22 | |
| 23 # Print output of wrapped commands. | |
| 24 tools/tool.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 tools/tool.py -m client.v8 -b 'V8 Linux64 - builder' | |
| 29 """ | |
| 30 | |
| 31 import argparse | |
| 32 import os | |
| 33 import subprocess | |
| 34 import sys | |
| 35 | |
| 36 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| 37 OUT_DIR = 'out.gn' | |
| 38 | |
| 39 | |
| 40 def _sanitize_nonalpha(text): | |
| 41 return ''.join(c if (c.isalnum() or c == '.') else '_' for c in text) | |
| 42 | |
| 43 | |
| 44 class GenerateGnArgs(object): | |
| 45 def __init__(self, args): | |
| 46 index = args.index('--') if '--' in args else len(args) | |
| 47 script_args = args[:index] | |
| 48 self.gn_args = args[index + 1:] | |
| 49 | |
| 50 parser = argparse.ArgumentParser( | |
| 51 description='Generator for V8\'s gn arguments. ' | |
| 52 'Additional gn args can be separated with -- ' | |
| 53 'from the arguments to this tool.') | |
| 54 parser.add_argument( | |
| 55 'outdir', nargs='?', | |
| 56 help='optional gn output directory') | |
| 57 parser.add_argument( | |
| 58 '-b', '--builder', | |
| 59 help='build configuration or builder name from mb_config.pyl') | |
| 60 parser.add_argument( | |
| 61 '-m', '--master', default='developer_default', | |
| 62 help='config group or master from mb_config.pyl') | |
| 63 parser.add_argument( | |
| 64 '-v', '--verbosity', action='count', | |
| 65 help='increase output verbosity (can be specified multiple times)') | |
| 66 self.options = parser.parse_args(script_args) | |
| 67 | |
| 68 assert (self.options.outdir or self.options.builder), ( | |
| 69 'please specify either an output directory or a builder/config name, ' | |
| 70 'e.g. x64.release') | |
| 71 | |
| 72 if not self.options.outdir: | |
| 73 # Derive output directory from builder name. | |
| 74 self.options.outdir = _sanitize_nonalpha(self.options.builder) | |
| 75 else: | |
| 76 # Also, if this should work on windows, we might need to use \ where | |
| 77 # outdir is used as path, while using / if it's used in a gn context. | |
| 78 assert not self.options.outdir.startswith('/'), ( | |
| 79 'only output directories relative to %s are supported' % OUT_DIR) | |
| 80 | |
| 81 if not self.options.builder: | |
| 82 # Derive builder from output directory. | |
| 83 self.options.builder = self.options.outdir | |
| 84 | |
| 85 def verbose_print_1(self, text): | |
| 86 if self.options.verbosity >= 1: | |
| 87 print '#################################################################' | |
| 88 print text | |
| 89 | |
| 90 def verbose_print_2(self, text): | |
| 91 if self.options.verbosity >= 2: | |
| 92 print text | |
| 93 | |
| 94 def call_cmd(self, args): | |
| 95 self.verbose_print_1(' '.join(args)) | |
| 96 try: | |
| 97 output = subprocess.check_output( | |
| 98 args=args, | |
| 99 stderr=subprocess.STDOUT, | |
| 100 ) | |
| 101 self.verbose_print_2(output) | |
| 102 except subprocess.CalledProcessError as e: | |
| 103 self.verbose_print_2(e.output) | |
| 104 raise | |
| 105 | |
| 106 def main(self): | |
| 107 # Always operate relative to the base directory for better relative-path | |
| 108 # handling. | |
| 109 self.verbose_print_1('cd ' + BASE_DIR) | |
| 110 os.chdir(BASE_DIR) | |
| 111 | |
| 112 # The directories are separated with slashes in a gn context (platform | |
| 113 # independent). | |
| 114 gn_outdir = '/'.join([OUT_DIR, self.options.outdir]) | |
| 115 | |
| 116 # Call MB to generate the basic configuration. | |
| 117 self.call_cmd([ | |
| 118 sys.executable, | |
| 119 '-u', os.path.join('tools', 'mb', 'mb.py'), | |
| 120 'gen', | |
| 121 '-f', os.path.join('infra', 'mb', 'mb_config.pyl'), | |
| 122 '-m', self.options.master, | |
| 123 '-b', self.options.builder, | |
| 124 gn_outdir, | |
| 125 ]) | |
| 126 | |
| 127 # Handle extra gn arguments. | |
| 128 if self.gn_args: | |
| 129 gn_args_path = os.path.join(OUT_DIR, self.options.outdir, 'args.gn') | |
| 130 more_gn_args = '\n'.join(self.gn_args) | |
| 131 | |
| 132 # Append extra gn arguments to the generated args.gn file. | |
| 133 self.verbose_print_1("Appending \"\"\"\n%s\n\"\"\" to %s." % ( | |
| 134 more_gn_args, os.path.abspath(gn_args_path))) | |
| 135 with open(gn_args_path, 'a') as f: | |
| 136 f.write('\n# Additional command-line args:\n') | |
| 137 f.write(more_gn_args) | |
| 138 f.write('\n') | |
| 139 | |
| 140 # Regenerate ninja files to check for errors in the additional gn args. | |
| 141 self.call_cmd(['gn', 'gen', gn_outdir]) | |
|
jochen (gone - plz use gerrit)
2016/07/12 15:09:07
i'd be happier without this call... but otherwise,
Michael Achenbach
2016/07/13 07:35:19
I thought it might be better to warn for potential
| |
| 142 return 0 | |
| 143 | |
| 144 if __name__ == "__main__": | |
| 145 gen = GenerateGnArgs(sys.argv[1:]) | |
| 146 try: | |
| 147 sys.exit(gen.main()) | |
| 148 except Exception: | |
| 149 if gen.options.verbosity < 2: | |
| 150 print ('\nHint: You can raise verbosity (-vv) to see the output of ' | |
| 151 'failed commands.\n') | |
| 152 raise | |
| OLD | NEW |