Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
|
Mark Mentovai
2015/11/24 14:33:33
#!/usr/bin/env python
# Copyright 2015 …
(or wha
Mark Mentovai
2015/11/24 14:33:33
This ought to be executable.
brettw
2015/11/24 23:36:01
Done.
| |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 # This writes headers for build flags. See build_header.gni for usage of | |
| 6 # this system as a whole. | |
| 7 # | |
| 8 # The parameters are passed in a response file so we don't have to worry | |
| 9 # about command line lengths. The name of the response file is passed on the | |
| 10 # command line. | |
| 11 # | |
| 12 # The format of the response file is: | |
| 13 # [--flags <list of one or more flag values>] | |
| 14 | |
| 15 import optparse | |
| 16 import os | |
| 17 import shlex | |
| 18 import sys | |
| 19 | |
| 20 | |
| 21 class Options: | |
| 22 def __init__(self, output, rulename, header_guard, flags): | |
| 23 self.output = output | |
| 24 self.rulename = rulename | |
| 25 self.header_guard = header_guard | |
| 26 self.flags = flags | |
| 27 | |
| 28 | |
| 29 def GetOptions(): | |
| 30 parser = optparse.OptionParser() | |
| 31 parser.add_option('--output', help="Output header name inside --gen-dir.") | |
| 32 parser.add_option('--rulename', | |
| 33 help="Helpful name of build rule for including in the " + | |
| 34 "comment at the top of the file.") | |
| 35 parser.add_option('--gen-dir', | |
| 36 help="Path to root of generated file directory tree.") | |
| 37 parser.add_option('--definitions', | |
| 38 help="Name of the response file containing the flags.") | |
| 39 cmdline_options, cmdline_flags = parser.parse_args() | |
| 40 | |
| 41 # Compute header guard by replacing some chars with _ and upper-casing. | |
| 42 header_guard = cmdline_options.output.upper() | |
| 43 header_guard = \ | |
| 44 header_guard.replace('/', '_').replace('\\', '_').replace('.', '_') | |
| 45 header_guard += '_' | |
| 46 | |
| 47 # The actual output file is inside the gen dir. | |
| 48 output = os.path.join(cmdline_options.gen_dir, cmdline_options.output) | |
| 49 | |
| 50 # Definition file in GYP is newline separated, in GN they are shell formatted. | |
| 51 # shlex can parse both of these. | |
| 52 with open(cmdline_options.definitions, 'r') as def_file: | |
| 53 defs = shlex.split(def_file.read()) | |
| 54 flags_index = defs.index('--flags') | |
| 55 | |
| 56 # Everything after --flags are flags. Values are 0/1/true/false. | |
|
Mark Mentovai
2015/11/24 14:33:33
Revise to say that false and true are mapped to 0
brettw
2015/11/24 23:36:01
Done.
| |
| 57 flags = [] | |
| 58 for flag in defs[flags_index + 1 :]: | |
| 59 equals_index = flag.index('=') | |
| 60 key = flag[:equals_index] | |
| 61 value = flag[equals_index + 1:] | |
| 62 | |
| 63 # Canonicalize and validate the value. | |
| 64 if value == 'true': | |
| 65 value = '1' | |
| 66 elif value == 'false': | |
| 67 value = '0' | |
| 68 flags.append((key, str(value))) | |
| 69 | |
| 70 return Options(output=output, | |
| 71 rulename=cmdline_options.rulename, | |
| 72 header_guard=header_guard, | |
| 73 flags=flags) | |
| 74 | |
| 75 | |
| 76 def WriteHeader(options): | |
| 77 with open(options.output, 'w') as output_file: | |
| 78 output_file.write("// Generated by build/write_build_header.py\n") | |
| 79 if options.rulename: | |
| 80 output_file.write('// From "' + options.rulename + '"\n') | |
| 81 | |
| 82 output_file.write('\n#ifndef %s\n' % options.header_guard) | |
| 83 output_file.write('#define %s\n\n' % options.header_guard) | |
| 84 output_file.write('#include "build/build_header.h"\n\n') | |
| 85 | |
| 86 for pair in options.flags: | |
| 87 output_file.write('#define BUILDFLAG_INTERNAL_%s() (%s)\n' % pair) | |
| 88 | |
| 89 output_file.write('\n#endif // %s\n' % options.header_guard) | |
| 90 | |
| 91 | |
| 92 options = GetOptions() | |
| 93 WriteHeader(options) | |
| OLD | NEW |