write_buildconfig_header.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python
  2. # Copyright (c) 2020 Project CHIP Authors
  3. #
  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
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  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.
  15. # Copyright 2015 The Chromium Authors. All rights reserved.
  16. #
  17. # Redistribution and use in source and binary forms, with or without
  18. # modification, are permitted provided that the following conditions are
  19. # met:
  20. #
  21. # * Redistributions of source code must retain the above copyright
  22. # notice, this list of conditions and the following disclaimer.
  23. # * Redistributions in binary form must reproduce the above
  24. # copyright notice, this list of conditions and the following disclaimer
  25. # in the documentation and/or other materials provided with the
  26. # distribution.
  27. # * Neither the name of Google Inc. nor the names of its
  28. # contributors may be used to endorse or promote products derived from
  29. # this software without specific prior written permission.
  30. #
  31. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  34. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  37. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  38. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  39. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  41. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. # This writes headers for build defines. See buildconfig_header.gni for
  43. # usage of this system as a whole.
  44. #
  45. # The parameters are passed in a response file so we don't have to worry
  46. # about command line lengths. The name of the response file is passed on the
  47. # command line.
  48. #
  49. # The format of the response file is:
  50. # [--defines <list of one or more defines values>]
  51. import optparse
  52. import os
  53. import shlex
  54. class Options:
  55. def __init__(self, output, rulename, header_guard, defines):
  56. self.output = output
  57. self.rulename = rulename
  58. self.header_guard = header_guard
  59. self.defines = defines
  60. def GetOptions():
  61. parser = optparse.OptionParser()
  62. parser.add_option('--output', help="Output header name inside --gen-dir.")
  63. parser.add_option('--rulename',
  64. help="Helpful name of build rule for including in the " +
  65. "comment at the top of the file.")
  66. parser.add_option('--gen-dir',
  67. help="Path to root of generated file directory tree.")
  68. parser.add_option('--definitions',
  69. help="Name of the response file containing the defines.")
  70. cmdline_options, cmdline_flags = parser.parse_args()
  71. # Compute header guard by replacing some chars with _ and upper-casing.
  72. header_guard = cmdline_options.output.upper()
  73. header_guard = \
  74. header_guard.replace('/', '_').replace('\\', '_').replace('.', '_')
  75. header_guard += '_'
  76. # The actual output file is inside the gen dir.
  77. output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)
  78. # Definition file in GYP is newline separated, in GN they are shell formatted.
  79. # shlex can parse both of these.
  80. with open(cmdline_options.definitions, 'r') as def_file:
  81. defs = shlex.split(def_file.read())
  82. defines_index = defs.index('--defines')
  83. # Everything after --defines are defines. true/false are remapped to 1/0,
  84. # everything else is passed through.
  85. defines = []
  86. for define in defs[defines_index + 1:]:
  87. equals_index = define.index('=')
  88. key = define[:equals_index]
  89. value = define[equals_index + 1:]
  90. # Canonicalize and validate the value.
  91. if value == 'true':
  92. value = '1'
  93. elif value == 'false':
  94. value = '0'
  95. defines.append((key, str(value)))
  96. return Options(output=output,
  97. rulename=cmdline_options.rulename,
  98. header_guard=header_guard,
  99. defines=defines)
  100. def WriteHeader(options):
  101. with open(options.output, 'w') as output_file:
  102. output_file.write("// Generated by write_buildconfig_header.py\n")
  103. if options.rulename:
  104. output_file.write('// From "' + options.rulename + '"\n')
  105. output_file.write('\n#ifndef %s\n' % options.header_guard)
  106. output_file.write('#define %s\n\n' % options.header_guard)
  107. for pair in options.defines:
  108. output_file.write('#define %s %s\n' % pair)
  109. output_file.write('\n#endif // %s\n' % options.header_guard)
  110. options = GetOptions()
  111. WriteHeader(options)