2 # Copyright (c) 2020 Project CHIP Authors
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Copyright 2015 The Chromium Authors. All rights reserved.
18 # Redistribution and use in source and binary forms, with or without
19 # modification, are permitted provided that the following conditions are
22 # * Redistributions of source code must retain the above copyright
23 # notice, this list of conditions and the following disclaimer.
24 # * Redistributions in binary form must reproduce the above
25 # copyright notice, this list of conditions and the following disclaimer
26 # in the documentation and/or other materials provided with the
28 # * Neither the name of Google Inc. nor the names of its
29 # contributors may be used to endorse or promote products derived from
30 # this software without specific prior written permission.
32 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 # This writes headers for build defines. See buildconfig_header.gni for
45 # usage of this system as a whole.
47 # The parameters are passed in a response file so we don't have to worry
48 # about command line lengths. The name of the response file is passed on the
51 # The format of the response file is:
52 # [--defines <list of one or more defines values>]
60 def __init__(self, output, rulename, header_guard, defines):
62 self.rulename = rulename
63 self.header_guard = header_guard
64 self.defines = defines
68 parser = optparse.OptionParser()
69 parser.add_option('--output', help="Output header name inside --gen-dir.")
70 parser.add_option('--rulename',
71 help="Helpful name of build rule for including in the " +
72 "comment at the top of the file.")
73 parser.add_option('--gen-dir',
74 help="Path to root of generated file directory tree.")
75 parser.add_option('--definitions',
76 help="Name of the response file containing the defines.")
77 cmdline_options, cmdline_flags = parser.parse_args()
79 # Compute header guard by replacing some chars with _ and upper-casing.
80 header_guard = cmdline_options.output.upper()
82 header_guard.replace('/', '_').replace('\\', '_').replace('.', '_')
85 # The actual output file is inside the gen dir.
86 output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)
88 # Definition file in GYP is newline separated, in GN they are shell formatted.
89 # shlex can parse both of these.
90 with open(cmdline_options.definitions, 'r') as def_file:
91 defs = shlex.split(def_file.read())
92 defines_index = defs.index('--defines')
94 # Everything after --defines are defines. true/false are remapped to 1/0,
95 # everything else is passed through.
97 for define in defs[defines_index + 1 :]:
98 equals_index = define.index('=')
99 key = define[:equals_index]
100 value = define[equals_index + 1:]
102 # Canonicalize and validate the value.
105 elif value == 'false':
107 defines.append((key, str(value)))
109 return Options(output=output,
110 rulename=cmdline_options.rulename,
111 header_guard=header_guard,
115 def WriteHeader(options):
116 with open(options.output, 'w') as output_file:
117 output_file.write("// Generated by write_buildconfig_header.py\n")
119 output_file.write('// From "' + options.rulename + '"\n')
121 output_file.write('\n#ifndef %s\n' % options.header_guard)
122 output_file.write('#define %s\n\n' % options.header_guard)
124 for pair in options.defines:
125 output_file.write('#define %s %s\n' % pair)
127 output_file.write('\n#endif // %s\n' % options.header_guard)
130 options = GetOptions()