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