1#!/usr/local/bin/python3.8
2
3"""Make a header that lists other headers.
4usage: make_all_header.py header_name.h include_prefix headers
5"""
6
7import sys
8import glob
9import os
10
11sys.path.append(os.path.split(sys.argv[0])[0])
12import python_tools
13
14# Treat an open file as UTF8-encoded, regardless of the locale
15if sys.version_info[0] >= 3:
16    def open_utf8(fname):
17        return open(fname, encoding='UTF8')
18else:
19    open_utf8 = open
20
21
22def _add_includes(headers, output):
23    for g in headers:
24        name = os.path.split(g)[1]
25        output.append("#include <%s/" % sys.argv[2] + name + ">")
26
27
28includepath = sys.argv[1][sys.argv[1].find("include") + len("include") + 1:]
29
30output = ["""/**
31 *  \\file %s
32 *  \\brief Include all non-deprecated headers in %s.
33 *
34 *  Copyright 2007-2021 IMP Inventors. All rights reserved.
35 */
36""" % (includepath, includepath[:-2].replace('/', '.'))]
37guard = includepath.replace(
38    "/",
39    "_").replace("\\",
40                 "_").replace(".",
41                              "_").upper()
42output.append("#ifndef %s" % guard)
43output.append("#define %s" % guard)
44
45for h in sys.argv[3:]:
46    if not h.endswith(".h"):
47        pat = os.path.join(h, "*.h")
48        orig_h = sorted(glob.glob(pat))
49        allh = []
50        deprecated_allh = []
51        for h in orig_h:
52            if 'DEPRECATED_HEADER' in open_utf8(h).read():
53                deprecated_allh.append(h)
54            else:
55                allh.append(h)
56    else:
57        deprecated_allh = []
58        allh = [h]
59    _add_includes(allh, output)
60
61    if deprecated_allh:
62        # SWIG needs all headers (for now)
63        output.append("#ifdef IMP_SWIG_WRAPPER")
64        _add_includes(deprecated_allh, output)
65        output.append("#endif")
66
67output.append("#endif /* %s */" % guard)
68python_tools.rewrite(sys.argv[1], "\n".join(output) + "\n")
69