| 1 | # specialist handling of header files for Samba
|
|---|
| 2 |
|
|---|
| 3 | import Build, re, Task, TaskGen, shutil, sys, Logs
|
|---|
| 4 | from samba_utils import *
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 | def header_install_path(header, header_path):
|
|---|
| 8 | '''find the installation path for a header, given a header_path option'''
|
|---|
| 9 | if not header_path:
|
|---|
| 10 | return ''
|
|---|
| 11 | if not isinstance(header_path, list):
|
|---|
| 12 | return header_path
|
|---|
| 13 | for (p1, dir) in header_path:
|
|---|
| 14 | for p2 in TO_LIST(p1):
|
|---|
| 15 | if fnmatch.fnmatch(header, p2):
|
|---|
| 16 | return dir
|
|---|
| 17 | # default to current path
|
|---|
| 18 | return ''
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | re_header = re.compile('^\s*#\s*include[ \t]*"([^"]+)"', re.I | re.M)
|
|---|
| 22 |
|
|---|
| 23 | # a dictionary mapping source header paths to public header paths
|
|---|
| 24 | header_map = {}
|
|---|
| 25 |
|
|---|
| 26 | def find_suggested_header(hpath):
|
|---|
| 27 | '''find a suggested header path to use'''
|
|---|
| 28 | base = os.path.basename(hpath)
|
|---|
| 29 | ret = []
|
|---|
| 30 | for h in header_map:
|
|---|
| 31 | if os.path.basename(h) == base:
|
|---|
| 32 | ret.append('<%s>' % header_map[h])
|
|---|
| 33 | ret.append('"%s"' % h)
|
|---|
| 34 | return ret
|
|---|
| 35 |
|
|---|
| 36 | def create_public_header(task):
|
|---|
| 37 | '''create a public header from a private one, output within the build tree'''
|
|---|
| 38 | src = task.inputs[0].abspath(task.env)
|
|---|
| 39 | tgt = task.outputs[0].bldpath(task.env)
|
|---|
| 40 |
|
|---|
| 41 | if os.path.exists(tgt):
|
|---|
| 42 | os.unlink(tgt)
|
|---|
| 43 |
|
|---|
| 44 | relsrc = os_path_relpath(src, task.env.TOPDIR)
|
|---|
| 45 |
|
|---|
| 46 | infile = open(src, mode='r')
|
|---|
| 47 | outfile = open(tgt, mode='w')
|
|---|
| 48 | linenumber = 0
|
|---|
| 49 |
|
|---|
| 50 | search_paths = [ '', task.env.RELPATH ]
|
|---|
| 51 | for i in task.env.EXTRA_INCLUDES:
|
|---|
| 52 | if i.startswith('#'):
|
|---|
| 53 | search_paths.append(i[1:])
|
|---|
| 54 |
|
|---|
| 55 | for line in infile:
|
|---|
| 56 | linenumber += 1
|
|---|
| 57 |
|
|---|
| 58 | # allow some straight substitutions
|
|---|
| 59 | if task.env.public_headers_replace and line.strip() in task.env.public_headers_replace:
|
|---|
| 60 | outfile.write(task.env.public_headers_replace[line.strip()] + '\n')
|
|---|
| 61 | continue
|
|---|
| 62 |
|
|---|
| 63 | # see if its an include line
|
|---|
| 64 | m = re_header.match(line)
|
|---|
| 65 | if m is None:
|
|---|
| 66 | outfile.write(line)
|
|---|
| 67 | continue
|
|---|
| 68 |
|
|---|
| 69 | # its an include, get the header path
|
|---|
| 70 | hpath = m.group(1)
|
|---|
| 71 | if hpath.startswith("bin/default/"):
|
|---|
| 72 | hpath = hpath[12:]
|
|---|
| 73 |
|
|---|
| 74 | # some are always allowed
|
|---|
| 75 | if task.env.public_headers_skip and hpath in task.env.public_headers_skip:
|
|---|
| 76 | outfile.write(line)
|
|---|
| 77 | continue
|
|---|
| 78 |
|
|---|
| 79 | # work out the header this refers to
|
|---|
| 80 | found = False
|
|---|
| 81 | for s in search_paths:
|
|---|
| 82 | p = os.path.normpath(os.path.join(s, hpath))
|
|---|
| 83 | if p in header_map:
|
|---|
| 84 | outfile.write("#include <%s>\n" % header_map[p])
|
|---|
| 85 | found = True
|
|---|
| 86 | break
|
|---|
| 87 | if found:
|
|---|
| 88 | continue
|
|---|
| 89 |
|
|---|
| 90 | if task.env.public_headers_allow_broken:
|
|---|
| 91 | Logs.warn("Broken public header include '%s' in '%s'" % (hpath, relsrc))
|
|---|
| 92 | outfile.write(line)
|
|---|
| 93 | continue
|
|---|
| 94 |
|
|---|
| 95 | # try to be nice to the developer by suggesting an alternative
|
|---|
| 96 | suggested = find_suggested_header(hpath)
|
|---|
| 97 | outfile.close()
|
|---|
| 98 | os.unlink(tgt)
|
|---|
| 99 | sys.stderr.write("%s:%u:Error: unable to resolve public header %s (maybe try one of %s)\n" % (
|
|---|
| 100 | os.path.relpath(src, os.getcwd()), linenumber, hpath, suggested))
|
|---|
| 101 | raise Utils.WafError("Unable to resolve header path '%s' in public header '%s' in directory %s" % (
|
|---|
| 102 | hpath, relsrc, task.env.RELPATH))
|
|---|
| 103 | infile.close()
|
|---|
| 104 | outfile.close()
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 | def public_headers_simple(bld, public_headers, header_path=None, public_headers_install=True):
|
|---|
| 108 | '''install some headers - simple version, no munging needed
|
|---|
| 109 | '''
|
|---|
| 110 | if not public_headers_install:
|
|---|
| 111 | return
|
|---|
| 112 | for h in TO_LIST(public_headers):
|
|---|
| 113 | inst_path = header_install_path(h, header_path)
|
|---|
| 114 | if h.find(':') != -1:
|
|---|
| 115 | s = h.split(":")
|
|---|
| 116 | h_name = s[0]
|
|---|
| 117 | inst_name = s[1]
|
|---|
| 118 | else:
|
|---|
| 119 | h_name = h
|
|---|
| 120 | inst_name = os.path.basename(h)
|
|---|
| 121 | bld.INSTALL_FILES('${INCLUDEDIR}', h_name, destname=inst_name)
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 | def PUBLIC_HEADERS(bld, public_headers, header_path=None, public_headers_install=True):
|
|---|
| 126 | '''install some headers
|
|---|
| 127 |
|
|---|
| 128 | header_path may either be a string that is added to the INCLUDEDIR,
|
|---|
| 129 | or it can be a dictionary of wildcard patterns which map to destination
|
|---|
| 130 | directories relative to INCLUDEDIR
|
|---|
| 131 | '''
|
|---|
| 132 | bld.SET_BUILD_GROUP('final')
|
|---|
| 133 |
|
|---|
| 134 | if not bld.env.build_public_headers:
|
|---|
| 135 | # in this case no header munging neeeded. Used for tdb, talloc etc
|
|---|
| 136 | public_headers_simple(bld, public_headers, header_path=header_path,
|
|---|
| 137 | public_headers_install=public_headers_install)
|
|---|
| 138 | return
|
|---|
| 139 |
|
|---|
| 140 | # create the public header in the given path
|
|---|
| 141 | # in the build tree
|
|---|
| 142 | for h in TO_LIST(public_headers):
|
|---|
| 143 | inst_path = header_install_path(h, header_path)
|
|---|
| 144 | if h.find(':') != -1:
|
|---|
| 145 | s = h.split(":")
|
|---|
| 146 | h_name = s[0]
|
|---|
| 147 | inst_name = s[1]
|
|---|
| 148 | else:
|
|---|
| 149 | h_name = h
|
|---|
| 150 | inst_name = os.path.basename(h)
|
|---|
| 151 | relpath1 = os_path_relpath(bld.srcnode.abspath(), bld.curdir)
|
|---|
| 152 | relpath2 = os_path_relpath(bld.curdir, bld.srcnode.abspath())
|
|---|
| 153 | targetdir = os.path.normpath(os.path.join(relpath1, bld.env.build_public_headers, inst_path))
|
|---|
| 154 | if not os.path.exists(os.path.join(bld.curdir, targetdir)):
|
|---|
| 155 | raise Utils.WafError("missing source directory %s for public header %s" % (targetdir, inst_name))
|
|---|
| 156 | target = os.path.join(targetdir, inst_name)
|
|---|
| 157 |
|
|---|
| 158 | # the source path of the header, relative to the top of the source tree
|
|---|
| 159 | src_path = os.path.normpath(os.path.join(relpath2, h_name))
|
|---|
| 160 |
|
|---|
| 161 | # the install path of the header, relative to the public include directory
|
|---|
| 162 | target_path = os.path.normpath(os.path.join(inst_path, inst_name))
|
|---|
| 163 |
|
|---|
| 164 | header_map[src_path] = target_path
|
|---|
| 165 |
|
|---|
| 166 | t = bld.SAMBA_GENERATOR('HEADER_%s/%s/%s' % (relpath2, inst_path, inst_name),
|
|---|
| 167 | group='headers',
|
|---|
| 168 | rule=create_public_header,
|
|---|
| 169 | source=h_name,
|
|---|
| 170 | target=target)
|
|---|
| 171 | t.env.RELPATH = relpath2
|
|---|
| 172 | t.env.TOPDIR = bld.srcnode.abspath()
|
|---|
| 173 | if not bld.env.public_headers_list:
|
|---|
| 174 | bld.env.public_headers_list = []
|
|---|
| 175 | bld.env.public_headers_list.append(os.path.join(inst_path, inst_name))
|
|---|
| 176 | if public_headers_install:
|
|---|
| 177 | bld.INSTALL_FILES('${INCLUDEDIR}',
|
|---|
| 178 | target,
|
|---|
| 179 | destname=os.path.join(inst_path, inst_name), flat=True)
|
|---|
| 180 | Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
|
|---|