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.
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
21 #if defined(OFFICIAL_BUILD)
22 #error shim headers must not be used in official builds!
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('--use-include-next', action
='store_true')
33 parser
.add_option('--outputs', action
='store_true')
34 parser
.add_option('--generate', action
='store_true')
36 options
, args
= parser
.parse_args(argv
)
38 if not options
.headers_root
:
39 parser
.error('Missing --headers-root parameter.')
40 if not options
.output_directory
:
41 parser
.error('Missing --output-directory parameter.')
43 parser
.error('Missing arguments - header file names.')
45 source_tree_root
= os
.path
.abspath(
46 os
.path
.join(os
.path
.dirname(__file__
), '..', '..'))
48 for root
in options
.headers_root
:
49 target_directory
= os
.path
.join(
50 options
.output_directory
,
51 os
.path
.relpath(root
, source_tree_root
))
52 if options
.generate
and not os
.path
.exists(target_directory
):
53 os
.makedirs(target_directory
)
55 for header_spec
in args
:
56 if ';' in header_spec
:
59 include_after
) = header_spec
.split(';', 2)
61 header_filename
= header_spec
65 yield os
.path
.join(target_directory
, header_filename
)
67 with
open(os
.path
.join(target_directory
, header_filename
), 'w') as f
:
68 f
.write(SHIM_TEMPLATE
)
71 for define
in options
.define
:
72 key
, value
= define
.split('=', 1)
73 # This non-standard push_macro extension is supported
74 # by compilers we support (GCC, clang).
75 f
.write('#pragma push_macro("%s")\n' % key
)
76 f
.write('#undef %s\n' % key
)
77 f
.write('#define %s %s\n' % (key
, value
))
80 for header
in include_before
.split(':'):
81 f
.write('#include %s\n' % header
)
83 if options
.use_include_next
:
84 f
.write('#include_next <%s>\n' % header_filename
)
86 f
.write('#include <%s>\n' % header_filename
)
89 for header
in include_after
.split(':'):
90 f
.write('#include %s\n' % header
)
93 for define
in options
.define
:
94 key
, value
= define
.split('=', 1)
95 # This non-standard pop_macro extension is supported
96 # by compilers we support (GCC, clang).
97 f
.write('#pragma pop_macro("%s")\n' % key
)
101 return '\n'.join(GeneratorMain(argv
))
104 if __name__
== '__main__':