OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
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 # [--variables <list of one or more variable values] | |
15 | |
16 import optparse | |
17 import shlex | |
18 import sys | |
19 | |
20 | |
21 class Options: | |
22 def __init__(self, output, rulename, header_guard, flags, variables): | |
23 self.output = output | |
24 self.rulename = rulename | |
25 self.header_guard = header_guard | |
26 self.flags = flags | |
27 self.variables = variables | |
28 | |
29 | |
30 def GetOptions(): | |
31 parser = optparse.OptionParser() | |
32 parser.add_option('--output', help="Output header name") | |
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('--path-for-guard', | |
Mark Mentovai
2015/11/19 21:52:55
Might be nice if this accepted the base directory
brettw
2015/11/20 18:47:23
Yes, this is nicer. Changed.
| |
37 help="Path to header for basing the include guard on.") | |
38 parser.add_option('--definitions', | |
39 help="Name of the response file containing the flags and " + | |
40 "variables.") | |
41 cmdline_options, cmdline_flags = parser.parse_args() | |
42 | |
43 # Compute header guard by replacing some chars with _ and upper-casing. | |
44 header_guard = cmdline_options.path_for_guard.upper() | |
45 header_guard = \ | |
46 header_guard.replace('/', '_').replace('\\', '_').replace('.', '_') | |
Mark Mentovai
2015/11/19 21:52:56
Three replaces in a row, maybe better as an re.sub
brettw
2015/11/20 18:47:23
My brain doesn't like regular expressions and I fi
| |
47 header_guard += '_' | |
48 | |
49 # Definition file has two sections, --flags and --variables. In GYP these | |
50 # are newline separated. Otherwise these are shell formatted. | |
51 with open(cmdline_options.definitions, 'r') as def_file: | |
52 defs = shlex.split(def_file.read()) | |
53 flags_index = defs.index('--flags') | |
54 vars_index = defs.index('--variables') | |
55 | |
56 # Everything between flags and variables are flags. Values are 0/1/true/false. | |
57 flags = [] | |
58 for flag in defs[flags_index + 1 : vars_index]: | |
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 | |
Mark Mentovai
2015/11/19 21:52:55
Stick with '1' or use str(value) when you flags.ap
| |
66 elif value == 'false': | |
67 value = 0 | |
68 elif value != '0' and value != '1': | |
69 print "The flag %s should have a value or 0, 1, true, or false." % flag | |
70 print "Instead I see %s" % value | |
71 sys.exit(1) | |
72 flags.append((key, value)) | |
73 | |
74 # Everything after variables are variables. | |
75 variables = [] | |
76 for var in defs[vars_index + 1:]: | |
77 equals_index = var.index('=') | |
78 variables.append((var[:equals_index], var[equals_index + 1:])) | |
79 | |
80 return Options(output=cmdline_options.output, | |
81 rulename=cmdline_options.rulename, | |
82 header_guard=header_guard, | |
83 flags=flags, | |
84 variables=variables) | |
85 | |
86 | |
87 def WriteHeader(options): | |
88 with open(options.output, 'w') as output_file: | |
89 output_file.write("/* Generated by build/write_build_header.py */\n") | |
Mark Mentovai
2015/11/19 21:52:56
// comments like we usually use?
brettw
2015/11/20 00:02:37
We do have C files floating around for various rea
brettw
2015/11/20 18:47:23
I changed to //, we can switch to /* */ if necessa
| |
90 if options.rulename: | |
91 output_file.write("/* From " + options.rulename + " */\n") | |
Mark Mentovai
2015/11/19 21:52:56
I’m inclined to deal with jokers by scrubbing newl
brettw
2015/11/20 18:47:23
I think this is unnecessary. Invocations to this s
| |
92 | |
93 output_file.write('\n#ifndef %s\n' % options.header_guard) | |
94 output_file.write('#define %s\n\n' % options.header_guard) | |
95 output_file.write('#include "build/build_header.h"\n\n') | |
96 | |
97 for pair in options.flags: | |
98 output_file.write('#define BUILDFLAG_FLAG_VALUE_%s %s\n' % pair) | |
99 for pair in options.variables: | |
100 output_file.write('#define BUILDFLAG_VAR_VALUE_%s %s\n' % pair) | |
Mark Mentovai
2015/11/19 21:52:56
Similarly, this should make sure there are no newl
| |
101 | |
102 output_file.write('\n#endif /* %s */\n' % options.header_guard) | |
103 | |
104 | |
105 options = GetOptions() | |
106 WriteHeader(options) | |
OLD | NEW |