import optparse
import os
import shlex
import sys
class Options:
def __init__(self, output, rulename, header_guard, flags):
self.output = output
self.rulename = rulename
self.header_guard = header_guard
self.flags = flags
def GetOptions():
parser = optparse.OptionParser()
parser.add_option('--output', help="Output header name inside --gen-dir.")
parser.add_option('--rulename',
help="Helpful name of build rule for including in the " +
"comment at the top of the file.")
parser.add_option('--gen-dir',
help="Path to root of generated file directory tree.")
parser.add_option('--definitions',
help="Name of the response file containing the flags.")
cmdline_options, cmdline_flags = parser.parse_args()
header_guard = cmdline_options.output.upper()
header_guard = \
header_guard.replace('/', '_').replace('\\', '_').replace('.', '_')
header_guard += '_'
output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)
with open(cmdline_options.definitions, 'r') as def_file:
defs = shlex.split(def_file.read())
flags_index = defs.index('--flags')
flags = []
for flag in defs[flags_index + 1 :]:
equals_index = flag.index('=')
key = flag[:equals_index]
value = flag[equals_index + 1:]
if value == 'true':
value = '1'
elif value == 'false':
value = '0'
flags.append((key, str(value)))
return Options(output=output,
rulename=cmdline_options.rulename,
header_guard=header_guard,
flags=flags)
def WriteHeader(options):
with open(options.output, 'w') as output_file:
output_file.write("// Generated by build/write_buildflag_header.py\n")
if options.rulename:
output_file.write('// From "' + options.rulename + '"\n')
output_file.write('\n#ifndef %s\n' % options.header_guard)
output_file.write('#define %s\n\n' % options.header_guard)
output_file.write('#include "build/buildflag.h"\n\n')
for pair in options.flags:
output_file.write('#define BUILDFLAG_INTERNAL_%s() (%s)\n' % pair)
output_file.write('\n#endif // %s\n' % options.header_guard)
if os.name == 'nt':
major, minor, build, platform, service_pack = sys.getwindowsversion()
if major < 6 or (major == 6 and minor < 2):
raise Exception(
'Unsupported OS. Building Chromium requires Windows 10. %s detected.' %
str(sys.getwindowsversion()))
options = GetOptions()
WriteHeader(options)