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')
36
37 options, args = parser.parse_args(argv)
38
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.')
45
46 source_tree_root = os.path.abspath(
47 os.path.join(os.path.dirname(__file__), '..'))
48
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)
55
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 header_path = os.path.join(target_directory, header_filename)
69 header_dir = os.path.dirname(header_path)
70 if not os.path.exists(header_dir):
71 os.makedirs(header_dir)
72 with open(header_path, 'w') as f:
73 f.write(SHIM_TEMPLATE)
74
75 if options.define:
76 for define in options.define:
77 key, value = define.split('=', 1)
78
79
80 f.write('#pragma push_macro("%s")\n' % key)
81 f.write('#undef %s\n' % key)
82 f.write('#define %s %s\n' % (key, value))
83
84 if include_before:
85 for header in include_before.split(':'):
86 f.write('#include %s\n' % header)
87
88 include_target = options.prefix + header_filename
89 if options.use_include_next:
90 f.write('#include_next <%s>\n' % include_target)
91 else:
92 f.write('#include <%s>\n' % include_target)
93
94 if include_after:
95 for header in include_after.split(':'):
96 f.write('#include %s\n' % header)
97
98 if options.define:
99 for define in options.define:
100 key, value = define.split('=', 1)
101
102
103 f.write('#pragma pop_macro("%s")\n' % key)
104
105