summaryrefslogtreecommitdiff
path: root/buildtools/wafsamba/samba_headers.py
blob: cca8bbeeed916ac1d4b16da3cacb583361de703a (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# specialist handling of header files for Samba

import Build, re, Task, TaskGen, shutil, sys
from samba_utils import *


def header_install_path(header, header_path):
    '''find the installation path for a header, given a header_path option'''
    if not header_path:
        return ''
    if not isinstance(header_path, list):
        return header_path
    for (p1, dir) in header_path:
        for p2 in TO_LIST(p1):
            if fnmatch.fnmatch(header, p2):
                return dir
    # default to current path
    return ''


re_header = re.compile('^\s*#\s*include[ \t]*"([^"]+)"', re.I | re.M)

# a dictionary mapping source header paths to public header paths
header_map = {}

def find_suggested_header(hpath):
    '''find a suggested header path to use'''
    base = os.path.basename(hpath)
    ret = []
    for h in header_map:
        if os.path.basename(h) == base:
            ret.append('<%s>' % header_map[h])
            ret.append('"%s"' % h)
    return ret

def create_public_header(task):
    '''create a public header from a private one, output within the build tree'''
    src = task.inputs[0].abspath(task.env)
    tgt = task.outputs[0].bldpath(task.env)

    if os.path.exists(tgt):
        os.unlink(tgt)

    relsrc = os_path_relpath(src, task.env.TOPDIR)

    infile  = open(src, mode='r')
    outfile = open(tgt, mode='w')
    linenumber = 0

    search_paths = [ '', task.env.RELPATH ]
    for i in task.env.EXTRA_INCLUDES:
        if i.startswith('#'):
            search_paths.append(i[1:])

    for line in infile:
        linenumber += 1

        # allow some straight substitutions
        if task.env.public_headers_replace and line.strip() in task.env.public_headers_replace:
            outfile.write(task.env.public_headers_replace[line.strip()] + '\n')
            continue

        # see if its an include line
        m = re_header.match(line)
        if m is None:
            outfile.write(line)
            continue

        # its an include, get the header path
        hpath = m.group(1)
        if hpath.startswith("bin/default/"):
            hpath = hpath[12:]

        # some are always allowed
        if task.env.public_headers_skip and hpath in task.env.public_headers_skip:
            outfile.write(line)
            continue

        # work out the header this refers to
        found = False
        for s in search_paths:
            p = os.path.normpath(os.path.join(s, hpath))
            if p in header_map:
                outfile.write("#include <%s>\n" % header_map[p])
                found = True
                break
        if found:
            continue

        # try to be nice to the developer by suggesting an alternative
        suggested = find_suggested_header(hpath)
        outfile.close()
        os.unlink(tgt)
        sys.stderr.write("%s:%u:Error: unable to resolve public header %s (maybe try one of %s)\n" % (
            os.path.relpath(src, os.getcwd()), linenumber, hpath, suggested))
        raise Utils.WafError("Unable to resolve header path '%s' in public header '%s' in directory %s" % (
            hpath, relsrc, task.env.RELPATH))
    infile.close()
    outfile.close()


def PUBLIC_HEADERS(bld, public_headers, header_path=None, public_headers_install=True):
    '''install some headers

    header_path may either be a string that is added to the INCLUDEDIR,
    or it can be a dictionary of wildcard patterns which map to destination
    directories relative to INCLUDEDIR
    '''
    bld.SET_BUILD_GROUP('final')
    if not bld.env.build_public_headers:
        bld.env.build_public_headers = ''

    # create the public header in the given path
    # in the build tree
    for h in TO_LIST(public_headers):
        inst_path = header_install_path(h, header_path)
        if h.find(':') != -1:
            s = h.split(":")
            h_name =  s[0]
            inst_name = s[1]
        else:
            h_name =  h
            inst_name = os.path.basename(h)
        relpath1 = os_path_relpath(bld.srcnode.abspath(), bld.curdir)
        relpath2 = os_path_relpath(bld.curdir, bld.srcnode.abspath())
        targetdir = os.path.normpath(os.path.join(relpath1, bld.env.build_public_headers, inst_path))
        if not os.path.exists(os.path.join(bld.curdir, targetdir)):
            raise Utils.WafError("missing source directory %s for public header %s" % (targetdir, inst_name))
        target = os.path.join(targetdir, inst_name)

        # the source path of the header, relative to the top of the source tree
        src_path = os.path.normpath(os.path.join(relpath2, h_name))

        # the install path of the header, relative to the public include directory
        target_path = os.path.normpath(os.path.join(inst_path, inst_name))

        header_map[src_path] = target_path

        t = bld.SAMBA_GENERATOR('HEADER_%s/%s/%s' % (relpath2, inst_path, inst_name),
                                group='prototypes',
                                rule=create_public_header,
                                source=h_name,
                                target=target)
        t.env.RELPATH = relpath2
        t.env.TOPDIR  = bld.srcnode.abspath()
        if not bld.env.public_headers_list:
            bld.env.public_headers_list = []
        bld.env.public_headers_list.append(os.path.join(inst_path, inst_name))
        if public_headers_install:
            bld.INSTALL_FILES('${INCLUDEDIR}',
                              target,
                              destname=os.path.join(inst_path, inst_name), flat=True)
Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS