generate_shim_headers.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 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. """
  6. Generates shim headers that mirror the directory structure of bundled headers,
  7. but just forward to the system ones.
  8. This allows seamless compilation against system headers with no changes
  9. to our source code.
  10. """
  11. import optparse
  12. import os.path
  13. import sys
  14. SHIM_TEMPLATE = """
  15. #if defined(OFFICIAL_BUILD)
  16. #error shim headers must not be used in official builds!
  17. #endif
  18. """
  19. def GeneratorMain(argv):
  20. parser = optparse.OptionParser()
  21. parser.add_option('--headers-root', action='append')
  22. parser.add_option('--define', action='append')
  23. parser.add_option('--output-directory')
  24. parser.add_option('--prefix', default='')
  25. parser.add_option('--use-include-next', action='store_true')
  26. parser.add_option('--outputs', action='store_true')
  27. parser.add_option('--generate', action='store_true')
  28. options, args = parser.parse_args(argv)
  29. if not options.headers_root:
  30. parser.error('Missing --headers-root parameter.')
  31. if not options.output_directory:
  32. parser.error('Missing --output-directory parameter.')
  33. if not args:
  34. parser.error('Missing arguments - header file names.')
  35. source_tree_root = os.path.abspath(
  36. os.path.join(os.path.dirname(__file__), '..', '..'))
  37. for root in options.headers_root:
  38. target_directory = os.path.join(
  39. options.output_directory,
  40. os.path.relpath(root, source_tree_root))
  41. if options.generate and not os.path.exists(target_directory):
  42. os.makedirs(target_directory)
  43. for header_spec in args:
  44. if ';' in header_spec:
  45. (header_filename,
  46. include_before,
  47. include_after) = header_spec.split(';', 2)
  48. else:
  49. header_filename = header_spec
  50. include_before = ''
  51. include_after = ''
  52. if options.outputs:
  53. yield os.path.join(target_directory, header_filename)
  54. if options.generate:
  55. header_path = os.path.join(target_directory, header_filename)
  56. header_dir = os.path.dirname(header_path)
  57. if not os.path.exists(header_dir):
  58. os.makedirs(header_dir)
  59. with open(header_path, 'w') as f:
  60. f.write(SHIM_TEMPLATE)
  61. if options.define:
  62. for define in options.define:
  63. key, value = define.split('=', 1)
  64. # This non-standard push_macro extension is supported
  65. # by compilers we support (GCC, clang).
  66. f.write('#pragma push_macro("%s")\n' % key)
  67. f.write('#undef %s\n' % key)
  68. f.write('#define %s %s\n' % (key, value))
  69. if include_before:
  70. for header in include_before.split(':'):
  71. f.write('#include %s\n' % header)
  72. include_target = options.prefix + header_filename
  73. if options.use_include_next:
  74. f.write('#include_next <%s>\n' % include_target)
  75. else:
  76. f.write('#include <%s>\n' % include_target)
  77. if include_after:
  78. for header in include_after.split(':'):
  79. f.write('#include %s\n' % header)
  80. if options.define:
  81. for define in options.define:
  82. key, value = define.split('=', 1)
  83. # This non-standard pop_macro extension is supported
  84. # by compilers we support (GCC, clang).
  85. f.write('#pragma pop_macro("%s")\n' % key)
  86. def DoMain(argv):
  87. return '\n'.join(GeneratorMain(argv))
  88. if __name__ == '__main__':
  89. DoMain(sys.argv[1:])