1#!/usr/bin/env python
2#
3# Copyright 2013 the V8 project authors. All rights reserved.
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9#       notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11#       copyright notice, this list of conditions and the following
12#       disclaimer in the documentation and/or other materials provided
13#       with the distribution.
14#     * Neither the name of Google Inc. nor the names of its
15#       contributors may be used to endorse or promote products derived
16#       from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30"""
31Generates shim headers that mirror the directory structure of bundled headers,
32but just forward to the system ones.
33
34This allows seamless compilation against system headers with no changes
35to our source code.
36"""
37
38
39import optparse
40import os.path
41import sys
42
43
44def GeneratorMain(argv):
45  parser = optparse.OptionParser()
46  parser.add_option('--headers-root', action='append')
47  parser.add_option('--define', action='append')
48  parser.add_option('--output-directory')
49  parser.add_option('--prefix', default='')
50  parser.add_option('--use-include-next', action='store_true')
51  parser.add_option('--outputs', action='store_true')
52  parser.add_option('--generate', action='store_true')
53
54  options, args = parser.parse_args(argv)
55
56  if not options.headers_root:
57    parser.error('Missing --headers-root parameter.')
58  if not options.output_directory:
59    parser.error('Missing --output-directory parameter.')
60  if not args:
61    parser.error('Missing arguments - header file names.')
62
63  source_tree_root = os.path.abspath(
64    os.path.join(os.path.dirname(__file__), '..', '..'))
65
66  for root in options.headers_root:
67    target_directory = os.path.join(
68      options.output_directory,
69      os.path.relpath(root, source_tree_root))
70    if options.generate and not os.path.exists(target_directory):
71      os.makedirs(target_directory)
72
73    for header_spec in args:
74      if ';' in header_spec:
75        (header_filename,
76         include_before,
77         include_after) = header_spec.split(';', 2)
78      else:
79        header_filename = header_spec
80        include_before = ''
81        include_after = ''
82      if options.outputs:
83        yield os.path.join(target_directory, header_filename)
84      if options.generate:
85        with open(os.path.join(target_directory, header_filename), 'w') as f:
86          if options.define:
87            for define in options.define:
88              key, value = define.split('=', 1)
89              # This non-standard push_macro extension is supported
90              # by compilers we support (GCC, clang).
91              f.write('#pragma push_macro("%s")\n' % key)
92              f.write('#undef %s\n' % key)
93              f.write('#define %s %s\n' % (key, value))
94
95          if include_before:
96            for header in include_before.split(':'):
97              f.write('#include %s\n' % header)
98
99          include_target = options.prefix + header_filename
100          if options.use_include_next:
101            f.write('#include_next <%s>\n' % include_target)
102          else:
103            f.write('#include <%s>\n' % include_target)
104
105          if include_after:
106            for header in include_after.split(':'):
107              f.write('#include %s\n' % header)
108
109          if options.define:
110            for define in options.define:
111              key, value = define.split('=', 1)
112              # This non-standard pop_macro extension is supported
113              # by compilers we support (GCC, clang).
114              f.write('#pragma pop_macro("%s")\n' % key)
115
116
117def DoMain(argv):
118  return '\n'.join(GeneratorMain(argv))
119
120
121if __name__ == '__main__':
122  DoMain(sys.argv[1:])
123