write_buildflag_header.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. # This writes headers for build flags. See buildflag_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. import optparse
  15. import os
  16. import re
  17. import shlex
  18. class Options:
  19. def __init__(self, output, rulename, header_guard, flags):
  20. self.output = output
  21. self.rulename = rulename
  22. self.header_guard = header_guard
  23. self.flags = flags
  24. def GetOptions():
  25. parser = optparse.OptionParser()
  26. parser.add_option('--output', help="Output header name inside --gen-dir.")
  27. parser.add_option('--rulename',
  28. help="Helpful name of build rule for including in the " +
  29. "comment at the top of the file.")
  30. parser.add_option('--gen-dir',
  31. help="Path to root of generated file directory tree.")
  32. parser.add_option('--definitions',
  33. help="Name of the response file containing the flags.")
  34. cmdline_options, cmdline_flags = parser.parse_args()
  35. # Compute a valid C++ header guard by replacing non valid chars with '_',
  36. # upper-casing everything and prepending '_' if first symbol is digit.
  37. header_guard = cmdline_options.output.upper()
  38. if header_guard[0].isdigit():
  39. header_guard = '_' + header_guard
  40. header_guard = re.sub('[^\w]', '_', header_guard)
  41. header_guard += '_'
  42. # The actual output file is inside the gen dir.
  43. output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)
  44. # Definition file in GYP is newline separated, in GN they are shell formatted.
  45. # shlex can parse both of these.
  46. with open(cmdline_options.definitions, 'r') as def_file:
  47. defs = shlex.split(def_file.read())
  48. flags_index = defs.index('--flags')
  49. # Everything after --flags are flags. true/false are remapped to 1/0,
  50. # everything else is passed through.
  51. flags = []
  52. for flag in defs[flags_index + 1 :]:
  53. equals_index = flag.index('=')
  54. key = flag[:equals_index]
  55. value = flag[equals_index + 1:]
  56. # Canonicalize and validate the value.
  57. if value == 'true':
  58. value = '1'
  59. elif value == 'false':
  60. value = '0'
  61. flags.append((key, str(value)))
  62. return Options(output=output,
  63. rulename=cmdline_options.rulename,
  64. header_guard=header_guard,
  65. flags=flags)
  66. def WriteHeader(options):
  67. with open(options.output, 'w') as output_file:
  68. output_file.write("// Generated by build/write_buildflag_header.py\n")
  69. if options.rulename:
  70. output_file.write('// From "' + options.rulename + '"\n')
  71. output_file.write('\n#ifndef %s\n' % options.header_guard)
  72. output_file.write('#define %s\n\n' % options.header_guard)
  73. output_file.write('#include "build/buildflag.h" // IWYU pragma: export\n\n')
  74. for pair in options.flags:
  75. output_file.write('#define BUILDFLAG_INTERNAL_%s() (%s)\n' % pair)
  76. output_file.write('\n#endif // %s\n' % options.header_guard)
  77. options = GetOptions()
  78. WriteHeader(options)