1#!/usr/bin/env python
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.
5
6# This writes headers for build flags. See buildflag_header.gni for usage of
7# this system as a whole.
8#
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
11# command line.
12#
13# The format of the response file is:
14#    [--flags <list of one or more flag values>]
15
16import optparse
17import os
18import shlex
19import sys
20
21
22class Options:
23  def __init__(self, output, rulename, header_guard, flags):
24    self.output = output
25    self.rulename = rulename
26    self.header_guard = header_guard
27    self.flags = flags
28
29
30def GetOptions():
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()
41
42  # Compute header guard by replacing some chars with _ and upper-casing.
43  header_guard = cmdline_options.output.upper()
44  header_guard = \
45      header_guard.replace('/', '_').replace('\\', '_').replace('.', '_')
46  header_guard += '_'
47
48  # The actual output file is inside the gen dir.
49  output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)
50
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')
56
57  # Everything after --flags are flags. true/false are remapped to 1/0,
58  # everything else is passed through.
59  flags = []
60  for flag in defs[flags_index + 1 :]:
61    equals_index = flag.index('=')
62    key = flag[:equals_index]
63    value = flag[equals_index + 1:]
64
65    # Canonicalize and validate the value.
66    if value == 'true':
67      value = '1'
68    elif value == 'false':
69      value = '0'
70    flags.append((key, str(value)))
71
72  return Options(output=output,
73                 rulename=cmdline_options.rulename,
74                 header_guard=header_guard,
75                 flags=flags)
76
77
78def WriteHeader(options):
79  with open(options.output, 'w') as output_file:
80    output_file.write("// Generated by build/write_buildflag_header.py\n")
81    if options.rulename:
82      output_file.write('// From "' + options.rulename + '"\n')
83
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')
87
88    for pair in options.flags:
89      output_file.write('#define BUILDFLAG_INTERNAL_%s() (%s)\n' % pair)
90
91    output_file.write('\n#endif  // %s\n' % options.header_guard)
92
93
94if os.name == 'nt':
95  major, minor, build, platform, service_pack = sys.getwindowsversion()
96  # Windows 10 will be 6.2 on Python 2 and 10.0 on Python 3. This check
97  # handles both.
98  if major < 6 or (major == 6 and minor < 2):
99    raise Exception(
100        'Unsupported OS. Building Chromium requires Windows 10. %s detected.' %
101        str(sys.getwindowsversion()))
102options = GetOptions()
103WriteHeader(options)
104