summaryrefslogtreecommitdiffstats
path: root/tools/generate_shim_headers/generate_shim_headers.py
blob: 068a2ac60c870ab0db1d47e06d34bbb93f3e68fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Generates shim headers that mirror the directory structure of bundled headers,
but just forward to the system ones.

This allows seamless compilation against system headers with no changes
to our source code.
"""


import optparse
import os.path
import sys


SHIM_TEMPLATE = """
#if defined(OFFICIAL_BUILD)
#error shim headers must not be used in official builds!
#endif

#include <%s>
"""


def GeneratorMain(argv):
  parser = optparse.OptionParser()
  parser.add_option('--headers-root')
  parser.add_option('--output-directory')
  parser.add_option('--outputs', action='store_true')
  parser.add_option('--generate', action='store_true')

  options, args = parser.parse_args(argv)

  if not options.headers_root:
    parser.error('Missing --headers-root parameter.')
  if not options.output_directory:
    parser.error('Missing --output-directory parameter.')
  if not args:
    parser.error('Missing arguments - header file names.')

  source_tree_root = os.path.abspath(
    os.path.join(os.path.dirname(__file__), '..', '..'))

  target_directory = os.path.join(
    options.output_directory,
    os.path.relpath(options.headers_root, source_tree_root))
  if options.generate and not os.path.exists(target_directory):
    os.makedirs(target_directory)
  for header_filename in args:
    if options.outputs:
      yield os.path.join(target_directory, header_filename)
    if options.generate:
      with open(os.path.join(target_directory, header_filename), 'w') as f:
        f.write(SHIM_TEMPLATE % header_filename)


def DoMain(argv):
  return '\n'.join(GeneratorMain(argv))


if __name__ == '__main__':
  DoMain(sys.argv[1:])