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.
6 # This writes headers for build flags. See buildflag_header.gni for usage of
7 # this system as a whole.
9 # The parameters are passed in a response file so we don't have to worry
10 # about command line lengths. The name of the response file is passed on the
13 # The format of the response file is:
14 # [--flags <list of one or more flag values>]
23 def __init__(self, output, rulename, header_guard, flags):
25 self.rulename = rulename
26 self.header_guard = header_guard
31 parser = optparse.OptionParser()
32 parser.add_option('--output', help="Output header name inside --gen-dir.")
33 parser.add_option('--rulename',
34 help="Helpful name of build rule for including in the " +
35 "comment at the top of the file.")
36 parser.add_option('--gen-dir',
37 help="Path to root of generated file directory tree.")
38 parser.add_option('--definitions',
39 help="Name of the response file containing the flags.")
40 cmdline_options, cmdline_flags = parser.parse_args()
42 # Compute header guard by replacing some chars with _ and upper-casing.
43 header_guard = cmdline_options.output.upper()
45 header_guard.replace('/', '_').replace('\\', '_').replace('.', '_')
48 # The actual output file is inside the gen dir.
49 output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)
51 # Definition file in GYP is newline separated, in GN they are shell formatted.
52 # shlex can parse both of these.
53 with open(cmdline_options.definitions, 'r') as def_file:
54 defs = shlex.split(def_file.read())
55 flags_index = defs.index('--flags')
57 # Everything after --flags are flags. true/false are remapped to 1/0,
58 # everything else is passed through.
60 for flag in defs[flags_index + 1 :]:
61 equals_index = flag.index('=')
62 key = flag[:equals_index]
63 value = flag[equals_index + 1:]
65 # Canonicalize and validate the value.
68 elif value == 'false':
70 flags.append((key, str(value)))
72 return Options(output=output,
73 rulename=cmdline_options.rulename,
74 header_guard=header_guard,
78 def WriteHeader(options):
79 with open(options.output, 'w') as output_file:
80 output_file.write("// Generated by build/write_buildflag_header.py\n")
82 output_file.write('// From "' + options.rulename + '"\n')
84 output_file.write('\n#ifndef %s\n' % options.header_guard)
85 output_file.write('#define %s\n\n' % options.header_guard)
86 output_file.write('#include "build/buildflag.h"\n\n')
88 for pair in options.flags:
89 output_file.write('#define BUILDFLAG_INTERNAL_%s() (%s)\n' % pair)
91 output_file.write('\n#endif // %s\n' % options.header_guard)
94 options = GetOptions()