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