[314be23] | 1 | # |
---|
[97c5024a] | 2 | # Copyright (c) 2015-2016 Chris Johns <chrisj@rtems.org>. All rights reserved. |
---|
[314be23] | 3 | # |
---|
| 4 | # Copyright (c) 2009-2015 embedded brains GmbH. All rights reserved. |
---|
| 5 | # |
---|
| 6 | # embedded brains GmbH |
---|
| 7 | # Dornierstr. 4 |
---|
| 8 | # 82178 Puchheim |
---|
| 9 | # Germany |
---|
| 10 | # <info@embedded-brains.de> |
---|
| 11 | # |
---|
| 12 | # Copyright (c) 2012 OAR Corporation. All rights reserved. |
---|
| 13 | # |
---|
| 14 | # Redistribution and use in source and binary forms, with or without |
---|
| 15 | # modification, are permitted provided that the following conditions |
---|
| 16 | # are met: |
---|
| 17 | # 1. Redistributions of source code must retain the above copyright |
---|
| 18 | # notice, this list of conditions and the following disclaimer. |
---|
| 19 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
| 20 | # notice, this list of conditions and the following disclaimer in the |
---|
| 21 | # documentation and/or other materials provided with the distribution. |
---|
| 22 | # |
---|
| 23 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 24 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 25 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 26 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
| 27 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
| 28 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
| 29 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
| 30 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
| 31 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 32 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
| 33 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 34 | |
---|
| 35 | # FreeBSD: http://svn.freebsd.org/base/releng/8.2/sys (revision 222485) |
---|
| 36 | |
---|
[97c5024a] | 37 | from __future__ import print_function |
---|
| 38 | |
---|
[314be23] | 39 | import shutil |
---|
| 40 | import os |
---|
| 41 | import re |
---|
| 42 | import sys |
---|
| 43 | import getopt |
---|
| 44 | import filecmp |
---|
| 45 | import difflib |
---|
[f1fcdba] | 46 | import codecs |
---|
[314be23] | 47 | |
---|
| 48 | # |
---|
| 49 | # Global controls. |
---|
| 50 | # |
---|
| 51 | RTEMS_DIR = "." |
---|
| 52 | FreeBSD_DIR = "freebsd-org" |
---|
[f1fcdba] | 53 | verboseLevel = 0 |
---|
[314be23] | 54 | isDryRun = False |
---|
| 55 | isDiffMode = False |
---|
[97c5024a] | 56 | filesProcessedCount = 0 |
---|
| 57 | filesProcessed = [] |
---|
[f9798ad] | 58 | filesTotal = 0 |
---|
| 59 | filesTotalLines = 0 |
---|
| 60 | filesTotalInserts = 0 |
---|
| 61 | filesTotalDeletes = 0 |
---|
| 62 | diffDetails = { } |
---|
[97c5024a] | 63 | |
---|
[f1fcdba] | 64 | verboseInfo = 1 |
---|
| 65 | verboseDetail = 2 |
---|
| 66 | verboseMoreDetail = 3 |
---|
| 67 | verboseDebug = 4 |
---|
| 68 | |
---|
| 69 | def verbose(level = verboseInfo): |
---|
| 70 | return verboseLevel >= level |
---|
| 71 | |
---|
[f9798ad] | 72 | def changedFileSummary(statsReport = False): |
---|
| 73 | |
---|
| 74 | global filesTotal, filesTotalLines, filesTotalInserts, filesTotalDeletes |
---|
| 75 | |
---|
[97c5024a] | 76 | if isDiffMode == False: |
---|
[f1fcdba] | 77 | if verbose(): |
---|
[f9798ad] | 78 | print('%d file(s) were changed:' % (filesProcessedCount)) |
---|
[f1fcdba] | 79 | for f in sorted(filesProcessed): |
---|
| 80 | print(' %s' % (f)) |
---|
[f9798ad] | 81 | else: |
---|
| 82 | print('%d file(s) were changed.' % (filesProcessedCount)) |
---|
| 83 | if statsReport: |
---|
| 84 | print('Stats Report:') |
---|
| 85 | transparent = filesTotal - len(diffDetails) |
---|
| 86 | changes = filesTotalInserts + filesTotalDeletes |
---|
| 87 | opacity = (float(changes) / (filesTotalLines + changes)) * 100.0 |
---|
| 88 | print(' Total File(s):%d Unchanged:%d (%.1f%%) Changed:%d' \ |
---|
| 89 | ' Opacity:%5.1f%% Lines:%d Edits:%d (+):%d (-):%d' % \ |
---|
| 90 | (filesTotal, transparent, (float(transparent) / filesTotal) * 100.0, len(diffDetails), \ |
---|
| 91 | opacity, filesTotalLines, changes, filesTotalInserts, filesTotalDeletes)) |
---|
| 92 | # |
---|
| 93 | # Sort by opacity. |
---|
| 94 | # |
---|
| 95 | ordered_diffs = sorted(diffDetails.items(), key = lambda diff: diff[1].opacity, reverse = True) |
---|
| 96 | for f in ordered_diffs: |
---|
| 97 | print(' %s' % (diffDetails[f[0]].status())) |
---|
[f1fcdba] | 98 | |
---|
[5338089] | 99 | def readFile(name): |
---|
[f1fcdba] | 100 | try: |
---|
| 101 | contents = codecs.open(name, mode = 'r', encoding = 'utf-8', errors = 'ignore').read() |
---|
| 102 | except UnicodeDecodeError as ude: |
---|
| 103 | print('error: reading: %s: %s' % (name, ude)) |
---|
| 104 | sys.exit(1) |
---|
| 105 | return contents |
---|
| 106 | |
---|
[5338089] | 107 | def writeFile(name, contents): |
---|
[f1fcdba] | 108 | path = os.path.dirname(name) |
---|
| 109 | if not os.path.exists(path): |
---|
| 110 | try: |
---|
| 111 | os.makedirs(path) |
---|
| 112 | except OSError as oe: |
---|
[5338089] | 113 | print('error: cannot create directory: %s: %s' % (path, oe)) |
---|
[f1fcdba] | 114 | sys.exit(1) |
---|
| 115 | try: |
---|
| 116 | codecs.open(name, mode = 'w', encoding = 'utf-8', errors = 'ignore').write(contents) |
---|
| 117 | except UnicodeDecodeError as ude: |
---|
| 118 | print('error: write: %s: %s' % (name, ude)) |
---|
| 119 | sys.exit(1) |
---|
[314be23] | 120 | |
---|
[f1fcdba] | 121 | # |
---|
| 122 | # A builder error. |
---|
| 123 | # |
---|
[5ba6949] | 124 | class error(Exception): |
---|
| 125 | """Base class for exceptions.""" |
---|
[f1fcdba] | 126 | def __init__(self, msg): |
---|
[5ba6949] | 127 | self.msg = 'error: %s' % (msg) |
---|
| 128 | def set_output(self, msg): |
---|
| 129 | self.msg = msg |
---|
| 130 | def __str__(self): |
---|
| 131 | return self.msg |
---|
| 132 | |
---|
[f9798ad] | 133 | # |
---|
| 134 | # Diff Record |
---|
| 135 | # |
---|
| 136 | class diffRecord: |
---|
| 137 | def __init__(self, src, dst, orig, diff, inserts, deletes): |
---|
| 138 | self.src = src |
---|
| 139 | self.dst = dst |
---|
| 140 | self.orig = orig |
---|
| 141 | self.diff = diff |
---|
| 142 | self.lines = len(orig) |
---|
| 143 | self.inserts = inserts |
---|
| 144 | self.deletes = deletes |
---|
| 145 | self.changes = inserts + deletes |
---|
| 146 | self.opacity = (float(self.changes) / (self.lines + self.changes)) * 100.0 |
---|
| 147 | |
---|
| 148 | def __repr__(self): |
---|
| 149 | return self.src |
---|
| 150 | |
---|
| 151 | def status(self): |
---|
| 152 | return 'opacity:%5.1f%% edits:%4d (+):%-4d (-):%-4d %s' % \ |
---|
| 153 | (self.opacity, self.changes, self.inserts, self.deletes, self.src) |
---|
| 154 | |
---|
[f1fcdba] | 155 | # |
---|
| 156 | # This stuff needs to move to libbsd.py. |
---|
| 157 | # |
---|
[5338089] | 158 | def commonFlags(): |
---|
[d18c643] | 159 | return ['-g', |
---|
[5ba6949] | 160 | '-fno-strict-aliasing', |
---|
| 161 | '-ffreestanding', |
---|
| 162 | '-fno-common'] |
---|
| 163 | |
---|
[5338089] | 164 | def commonWarnings(): |
---|
[5ba6949] | 165 | return ['-Wall', |
---|
[34545ad] | 166 | '-Wno-format', |
---|
| 167 | '-Wno-pointer-sign'] |
---|
[5ba6949] | 168 | |
---|
[5338089] | 169 | def commonNoWarnings(): |
---|
[e9aa9537] | 170 | return ['-w'] |
---|
[5ba6949] | 171 | |
---|
| 172 | def includes(): |
---|
| 173 | return ['-Irtemsbsd/include', |
---|
| 174 | '-Ifreebsd/sys', |
---|
| 175 | '-Ifreebsd/sys/contrib/pf', |
---|
[c40e45b] | 176 | '-Ifreebsd/sys/net', |
---|
[5ba6949] | 177 | '-Ifreebsd/include', |
---|
[5c3223f] | 178 | '-Ifreebsd/lib', |
---|
[ecda181] | 179 | '-Ifreebsd/lib/libbsdstat', |
---|
[5ba6949] | 180 | '-Ifreebsd/lib/libc/include', |
---|
| 181 | '-Ifreebsd/lib/libc/isc/include', |
---|
| 182 | '-Ifreebsd/lib/libc/resolv', |
---|
| 183 | '-Ifreebsd/lib/libutil', |
---|
| 184 | '-Ifreebsd/lib/libkvm', |
---|
| 185 | '-Ifreebsd/lib/libmemstat', |
---|
| 186 | '-Ifreebsd/lib/libipsec', |
---|
[5c3223f] | 187 | '-Ifreebsd/contrib/expat/lib', |
---|
[8440506] | 188 | '-Ifreebsd/contrib/libpcap', |
---|
[c40e45b] | 189 | '-Ifreebsd/contrib/libxo', |
---|
[5ba6949] | 190 | '-Irtemsbsd/sys', |
---|
| 191 | '-ImDNSResponder/mDNSCore', |
---|
| 192 | '-ImDNSResponder/mDNSShared', |
---|
| 193 | '-ImDNSResponder/mDNSPosix', |
---|
| 194 | '-Itestsuite/include'] |
---|
| 195 | |
---|
[307b324] | 196 | def buildInclude(): |
---|
| 197 | """ Returns the path where headers will be copied during build. """ |
---|
| 198 | return 'build-include' |
---|
| 199 | |
---|
[5338089] | 200 | def cpuIncludes(): |
---|
[5ba6949] | 201 | return ['-Irtemsbsd/@CPU@/include', |
---|
| 202 | '-Ifreebsd/sys/@CPU@/include'] |
---|
| 203 | |
---|
| 204 | def cflags(): |
---|
[2a5d001] | 205 | return [] |
---|
[5ba6949] | 206 | |
---|
| 207 | def cxxflags(): |
---|
[2a5d001] | 208 | return [] |
---|
[5ba6949] | 209 | |
---|
[5338089] | 210 | def headerPaths(): |
---|
[307b324] | 211 | """ Returns a list of information about what header files should be |
---|
| 212 | installed. |
---|
| 213 | |
---|
| 214 | The list is also used to find headers with a local path that doesn't match |
---|
| 215 | it's dest path. Due to the difference in the path name such files are |
---|
| 216 | problematic during the build if they are included using their later |
---|
| 217 | installation path (dest path) name. Therefore they are copied into a |
---|
| 218 | sub-directory of the build path so that they can be included with their |
---|
| 219 | normal installation path. """ |
---|
| 220 | |
---|
[0768880] | 221 | # local path wildcard dest path |
---|
[8d2dbd3] | 222 | return [('rtemsbsd/include', '**/*.h', ''), |
---|
[0768880] | 223 | ('rtemsbsd/mghttpd', 'mongoose.h', 'mghttpd'), |
---|
[8d2dbd3] | 224 | ('freebsd/include', '**/*.h', ''), |
---|
| 225 | ('freebsd/sys/bsm', '**/*.h', 'bsm'), |
---|
| 226 | ('freebsd/sys/cam', '**/*.h', 'cam'), |
---|
| 227 | ('freebsd/sys/net', '**/*.h', 'net'), |
---|
| 228 | ('freebsd/sys/net80211', '**/*.h', 'net80211'), |
---|
| 229 | ('freebsd/sys/netinet', '**/*.h', 'netinet'), |
---|
| 230 | ('freebsd/sys/netinet6', '**/*.h', 'netinet6'), |
---|
| 231 | ('freebsd/sys/netipsec', '**/*.h', 'netipsec'), |
---|
[79ec6b9] | 232 | ('freebsd/contrib/libpcap', '*.h', ''), |
---|
| 233 | ('freebsd/contrib/libpcap/pcap', '*.h', 'pcap'), |
---|
[1f7037d] | 234 | ('freebsd/crypto/openssl', '*.h', 'openssl'), |
---|
| 235 | ('freebsd/crypto/openssl/crypto', '*.h', 'openssl'), |
---|
| 236 | ('freebsd/crypto/openssl/ssl', '(ssl|kssl|ssl2).h', 'openssl'), |
---|
| 237 | ('freebsd/crypto/openssl/crypto/aes', 'aes.h', 'openssl'), |
---|
| 238 | ('freebsd/crypto/openssl/crypto/err', 'err.h', 'openssl'), |
---|
| 239 | ('freebsd/crypto/openssl/crypto/bio', '*.h', 'openssl'), |
---|
| 240 | ('freebsd/crypto/openssl/crypto/dsa', '*.h', 'openssl'), |
---|
| 241 | ('freebsd/crypto/openssl/ssl', '*.h', 'openssl'), |
---|
| 242 | ('freebsd/crypto/openssl/crypto/bn', 'bn.h', 'openssl'), |
---|
| 243 | ('freebsd/crypto/openssl/crypto/x509', 'x509.h', 'openssl'), |
---|
| 244 | ('freebsd/crypto/openssl/crypto/cast', 'cast.h', 'openssl'), |
---|
| 245 | ('freebsd/crypto/openssl/crypto/lhash', 'lhash.h', 'openssl'), |
---|
| 246 | ('freebsd/crypto/openssl/crypto/ecdh', 'ecdh.h', 'openssl'), |
---|
| 247 | ('freebsd/crypto/openssl/crypto/ecdsa', 'ecdsa.h', 'openssl'), |
---|
| 248 | ('freebsd/crypto/openssl/crypto/idea', 'idea.h', 'openssl'), |
---|
| 249 | ('freebsd/crypto/openssl/crypto/mdc2', 'mdc2.h', 'openssl'), |
---|
| 250 | ('freebsd/crypto/openssl/crypto/md4', 'md4.h', 'openssl'), |
---|
| 251 | ('freebsd/crypto/openssl/crypto/md5', 'md5.h', 'openssl'), |
---|
| 252 | ('freebsd/crypto/openssl/crypto/rc2', 'rc2.h', 'openssl'), |
---|
| 253 | ('freebsd/crypto/openssl/crypto/rc4', 'rc4.h', 'openssl'), |
---|
| 254 | ('freebsd/crypto/openssl/crypto/ripemd','ripemd.h', 'openssl'), |
---|
| 255 | ('freebsd/crypto/openssl/crypto/seed', 'seed.h', 'openssl'), |
---|
| 256 | ('freebsd/crypto/openssl/crypto/sha', 'sha.h', 'openssl'), |
---|
| 257 | ('freebsd/crypto/openssl/crypto/x509v3','x509v3.h', 'openssl'), |
---|
| 258 | ('freebsd/crypto/openssl/crypto/x509', 'x509_vfy.h', 'openssl'), |
---|
| 259 | ('freebsd/crypto/openssl/crypto/buffer','buffer.h', 'openssl'), |
---|
| 260 | ('freebsd/crypto/openssl/crypto/comp', 'comp.h', 'openssl'), |
---|
| 261 | ('freebsd/crypto/openssl/crypto/asn1', 'asn1_mac.h', 'openssl'), |
---|
| 262 | ('freebsd/crypto/openssl/crypto/pem', '(pem|pem2).h', 'openssl'), |
---|
| 263 | ('freebsd/crypto/openssl/crypto/rsa', 'rsa.h', 'openssl'), |
---|
| 264 | ('freebsd/crypto/openssl/crypto/evp', 'evp.h', 'openssl'), |
---|
| 265 | ('freebsd/crypto/openssl/crypto/ec', 'ec.h', 'openssl'), |
---|
| 266 | ('freebsd/crypto/openssl/crypto/engine', 'engine.h', 'openssl'), |
---|
| 267 | ('freebsd/crypto/openssl/crypto/pkcs7', 'pkcs7.h', 'openssl'), |
---|
| 268 | ('freebsd/crypto/openssl/crypto/hmac', 'hmac.h', 'openssl'), |
---|
| 269 | ('freebsd/crypto/openssl/crypto/pqueue', 'pqueue.h', 'openssl'), |
---|
| 270 | ('freebsd/crypto/openssl/crypto/ocsp', 'ocsp.h', 'openssl'), |
---|
| 271 | ('freebsd/crypto/openssl/crypto/rand', 'rand.h', 'openssl'), |
---|
| 272 | ('freebsd/crypto/openssl/crypto/srp', 'srp.h', 'openssl'), |
---|
| 273 | ('freebsd/crypto/openssl/crypto/dh', 'dh.h', 'openssl'), |
---|
| 274 | ('freebsd/crypto/openssl/crypto/dso', 'dso.h', 'openssl'), |
---|
| 275 | ('freebsd/crypto/openssl/crypto/krb5', 'krb5_asn.h', 'openssl'), |
---|
| 276 | ('freebsd/crypto/openssl/crypto/cms', 'cms.h', 'openssl'), |
---|
| 277 | ('freebsd/crypto/openssl/crypto/txt_db', 'txt_db.h', 'openssl'), |
---|
| 278 | ('freebsd/crypto/openssl/crypto/ts', 'ts.h', 'openssl'), |
---|
| 279 | ('freebsd/crypto/openssl/crypto/modes', 'modes.h', 'openssl'), |
---|
| 280 | ('freebsd/crypto/openssl/crypto/pkcs12', 'pkcs12.h', 'openssl'), |
---|
| 281 | ('freebsd/crypto/openssl/crypto/bf', 'blowfish.h', 'openssl'), |
---|
| 282 | ('freebsd/crypto/openssl/crypto/cmac', 'cmac.h', 'openssl'), |
---|
| 283 | ('freebsd/crypto/openssl/crypto/asn1', '(asn1|asn1t).h', 'openssl'), |
---|
| 284 | ('freebsd/crypto/openssl/crypto/camellia', 'camellia.h', 'openssl'), |
---|
| 285 | ('freebsd/crypto/openssl/crypto/objects', '(objects|obj_mac).h', 'openssl'), |
---|
| 286 | ('freebsd/crypto/openssl/crypto/conf', '(conf|conf_api).h', 'openssl'), |
---|
| 287 | ('freebsd/crypto/openssl/crypto/des', '(des|des_old).h', 'openssl'), |
---|
| 288 | ('freebsd/crypto/openssl/crypto/ui', '(ui_compat|ui).h', 'openssl'), |
---|
| 289 | ('freebsd/crypto/openssl/crypto/whrlpool', 'whrlpool.h', 'openssl'), |
---|
| 290 | ('freebsd/crypto/openssl/crypto/stack', '(stack|safestack).h', 'openssl'), |
---|
| 291 | ('freebsd/crypto/openssl/crypto', '(opensslconf|opensslv|crypto).h', 'openssl'), |
---|
[8d2dbd3] | 292 | ('freebsd/sys/rpc', '**/*.h', 'rpc'), |
---|
| 293 | ('freebsd/sys/sys', '**/*.h', 'sys'), |
---|
| 294 | ('freebsd/sys/vm', '**/*.h', 'vm'), |
---|
| 295 | ('freebsd/sys/dev/mii', '**/*.h', 'dev/mii'), |
---|
[0768880] | 296 | ('mDNSResponder/mDNSCore', 'mDNSDebug.h', ''), |
---|
| 297 | ('mDNSResponder/mDNSCore', 'mDNSEmbeddedAPI.h', ''), |
---|
| 298 | ('mDNSResponder/mDNSShared', 'dns_sd.h', ''), |
---|
| 299 | ('mDNSResponder/mDNSPosix', 'mDNSPosix.h', '')] |
---|
| 300 | |
---|
[314be23] | 301 | # Move target dependent files under a machine directory |
---|
| 302 | def mapCPUDependentPath(path): |
---|
| 303 | return path.replace("include/", "include/machine/") |
---|
| 304 | |
---|
| 305 | def fixIncludes(data): |
---|
| 306 | data = re.sub('#include <sys/resource.h>', '#include <rtems/bsd/sys/resource.h>', data) |
---|
[f1fcdba] | 307 | data = re.sub('#include <sys/unistd.h>', '#include <rtems/bsd/sys/unistd.h>', data) |
---|
[314be23] | 308 | return data |
---|
| 309 | |
---|
| 310 | # revert fixing the include paths inside a C or .h file |
---|
| 311 | def revertFixIncludes(data): |
---|
[f1fcdba] | 312 | data = re.sub('#include <rtems/bsd/', '#include <', data) |
---|
| 313 | data = re.sub('#include <util.h>', '#include <rtems/bsd/util.h>', data) |
---|
| 314 | data = re.sub('#include <bsd.h>', '#include <rtems/bsd/bsd.h>', data) |
---|
[314be23] | 315 | data = re.sub('#include <zerocopy.h>', '#include <rtems/bsd/zerocopy.h>', data) |
---|
| 316 | return data |
---|
| 317 | |
---|
| 318 | # fix include paths inside a C or .h file |
---|
| 319 | def fixLocalIncludes(data): |
---|
[f1fcdba] | 320 | data = re.sub('#include "opt_([^"]*)"', '#include <rtems/bsd/local/opt_\\1>', data) |
---|
| 321 | data = re.sub('#include "([^"]*)_if.h"', '#include <rtems/bsd/local/\\1_if.h>', data) |
---|
[314be23] | 322 | data = re.sub('#include "miidevs([^"]*)"', '#include <rtems/bsd/local/miidevs\\1>', data) |
---|
| 323 | data = re.sub('#include "usbdevs([^"]*)"', '#include <rtems/bsd/local/usbdevs\\1>', data) |
---|
| 324 | return data |
---|
| 325 | |
---|
| 326 | # revert fixing the include paths inside a C or .h file |
---|
| 327 | def revertFixLocalIncludes(data): |
---|
| 328 | data = re.sub('#include <rtems/bsd/local/([^>]*)>', '#include "\\1"', data) |
---|
| 329 | return data |
---|
| 330 | |
---|
| 331 | def assertHeaderFile(path): |
---|
| 332 | if path[-2] != '.' or path[-1] != 'h': |
---|
[97c5024a] | 333 | print("*** " + path + " does not end in .h") |
---|
| 334 | print("*** Move it to a C source file list") |
---|
[314be23] | 335 | sys.exit(2) |
---|
| 336 | |
---|
| 337 | def assertSourceFile(path): |
---|
| 338 | if path[-2] != '.' or (path[-1] != 'c' and path[-1] != 'S'): |
---|
[97c5024a] | 339 | print("*** " + path + " does not end in .c") |
---|
| 340 | print("*** Move it to a header file list") |
---|
[314be23] | 341 | sys.exit(2) |
---|
| 342 | |
---|
[f9798ad] | 343 | def diffSource(dstLines, srcLines, src, dst): |
---|
| 344 | global filesTotal, filesTotalLines, filesTotalInserts, filesTotalDeletes |
---|
| 345 | # |
---|
| 346 | # Diff, note there is no line termination on each string. Expand the |
---|
| 347 | # generator to list because the generator is not reusable. |
---|
| 348 | # |
---|
| 349 | diff = list(difflib.unified_diff(dstLines, |
---|
| 350 | srcLines, |
---|
| 351 | fromfile = src, |
---|
| 352 | tofile = dst, |
---|
| 353 | n = 5, |
---|
| 354 | lineterm = '')) |
---|
| 355 | inserts = 0 |
---|
| 356 | deletes = 0 |
---|
| 357 | if len(diff) > 0: |
---|
| 358 | if src in diffDetails and \ |
---|
| 359 | diffDetails[src].dst != dst and diffDetails[src].diff != diff: |
---|
| 360 | raise error('repeated diff of file different: src:%s dst:%s' % (src, dst)) |
---|
| 361 | for l in diff: |
---|
| 362 | if l[0] == '-': |
---|
| 363 | deletes += 1 |
---|
| 364 | elif l[0] == '+': |
---|
| 365 | inserts += 1 |
---|
| 366 | diffDetails[src] = diffRecord(src, dst, srcLines, diff, inserts, deletes) |
---|
| 367 | |
---|
| 368 | # |
---|
| 369 | # Count the total files, lines and the level of changes. |
---|
| 370 | # |
---|
| 371 | filesTotal += 1 |
---|
| 372 | filesTotalLines += len(srcLines) |
---|
| 373 | filesTotalInserts += inserts |
---|
| 374 | filesTotalDeletes += deletes |
---|
| 375 | |
---|
| 376 | return diff |
---|
| 377 | |
---|
[f1fcdba] | 378 | # |
---|
| 379 | # Converters provide a way to alter the various types of code. The conversion |
---|
| 380 | # process filters a file as it is copies from the source path to the |
---|
| 381 | # destination path. Specialised versions are provided for different types of |
---|
| 382 | # source. |
---|
| 383 | # |
---|
[314be23] | 384 | class Converter(object): |
---|
| 385 | |
---|
[5338089] | 386 | def convert(self, src, dst, hasSource = True, sourceFilter = None, srcContents = None): |
---|
[f1fcdba] | 387 | |
---|
| 388 | global filesProcessed, filesProcessedCount |
---|
| 389 | |
---|
| 390 | if verbose(verboseDebug): |
---|
| 391 | print("convert: filter:%s: %s -> %s" % \ |
---|
[5338089] | 392 | (['yes', 'no'][sourceFilter is None], src, dst)) |
---|
[f1fcdba] | 393 | |
---|
| 394 | # |
---|
| 395 | # If there is no source raise an error if we expect source else print a |
---|
| 396 | # warning and do not try and convert. |
---|
| 397 | # |
---|
[5338089] | 398 | if srcContents is None: |
---|
[f1fcdba] | 399 | if not os.path.exists(src): |
---|
[5338089] | 400 | if hasSource: |
---|
[f1fcdba] | 401 | raise error('source not found: %s' % (src)) |
---|
| 402 | else: |
---|
| 403 | print('warning: no source: %s' % (src)) |
---|
| 404 | return |
---|
| 405 | |
---|
| 406 | # |
---|
| 407 | # Files read as a single string if not passed in. |
---|
| 408 | # |
---|
[5338089] | 409 | srcContents = readFile(src) |
---|
[f1fcdba] | 410 | |
---|
| 411 | if os.path.exists(dst): |
---|
[5338089] | 412 | dstContents = readFile(dst) |
---|
[f1fcdba] | 413 | else: |
---|
| 414 | print('warning: no destination: %s' % (dst)) |
---|
[5338089] | 415 | dstContents = '' |
---|
[f1fcdba] | 416 | |
---|
| 417 | # |
---|
| 418 | # Filter the source. |
---|
| 419 | # |
---|
[5338089] | 420 | if sourceFilter is not None: |
---|
| 421 | srcContents = sourceFilter(srcContents) |
---|
[f1fcdba] | 422 | |
---|
| 423 | # |
---|
| 424 | # Split into a list of lines. |
---|
| 425 | # |
---|
[5338089] | 426 | srcLines = srcContents.split(os.linesep) |
---|
| 427 | dstLines = dstContents.split(os.linesep) |
---|
[f1fcdba] | 428 | |
---|
| 429 | if verbose(verboseDebug): |
---|
[5338089] | 430 | print('Unified diff: %s (lines:%d)' % (src, len(srcLines))) |
---|
[f1fcdba] | 431 | |
---|
| 432 | # |
---|
[f9798ad] | 433 | # Diff, note there is no line termination on each string. |
---|
[f1fcdba] | 434 | # |
---|
[f9798ad] | 435 | diff = diffSource(dstLines, srcLines, src, dst) |
---|
[f1fcdba] | 436 | |
---|
| 437 | # |
---|
| 438 | # The diff list is empty if the files are the same. |
---|
| 439 | # |
---|
| 440 | if len(diff) > 0: |
---|
| 441 | |
---|
| 442 | if verbose(verboseDebug): |
---|
| 443 | print('Unified diff length: %d' % len(diff)) |
---|
| 444 | |
---|
| 445 | filesProcessed += [dst] |
---|
| 446 | filesProcessedCount += 1 |
---|
| 447 | if isDiffMode == False: |
---|
| 448 | if verbose(verboseDetail): |
---|
| 449 | print("UPDATE: %s -> %s" % (src, dst)) |
---|
| 450 | if isDryRun == False: |
---|
[5338089] | 451 | writeFile(dst, srcContents) |
---|
[f1fcdba] | 452 | else: |
---|
| 453 | print("diff -u %s %s" % (src, dst)) |
---|
| 454 | for l in diff: |
---|
| 455 | print(l) |
---|
[314be23] | 456 | |
---|
| 457 | class NoConverter(Converter): |
---|
[5338089] | 458 | def convert(self, src, dst, hasSource = True, sourceFilter = None): |
---|
[314be23] | 459 | return '/* EMPTY */\n' |
---|
| 460 | |
---|
| 461 | class FromFreeBSDToRTEMSHeaderConverter(Converter): |
---|
[5338089] | 462 | def sourceFilter(self, data): |
---|
[314be23] | 463 | data = fixLocalIncludes(data) |
---|
| 464 | data = fixIncludes(data) |
---|
| 465 | return data |
---|
| 466 | |
---|
[f1fcdba] | 467 | def convert(self, src, dst): |
---|
| 468 | sconverter = super(FromFreeBSDToRTEMSHeaderConverter, self) |
---|
[5338089] | 469 | sconverter.convert(src, dst, sourceFilter = self.sourceFilter) |
---|
[f1fcdba] | 470 | |
---|
[314be23] | 471 | class FromFreeBSDToRTEMSUserSpaceHeaderConverter(Converter): |
---|
[5338089] | 472 | def sourceFilter(self, data): |
---|
[314be23] | 473 | data = fixIncludes(data) |
---|
| 474 | return data |
---|
| 475 | |
---|
[f1fcdba] | 476 | def convert(self, src, dst): |
---|
| 477 | sconverter = super(FromFreeBSDToRTEMSUserSpaceHeaderConverter, self) |
---|
[5338089] | 478 | sconverter.convert(src, dst, sourceFilter = self.sourceFilter) |
---|
[f1fcdba] | 479 | |
---|
[314be23] | 480 | class FromFreeBSDToRTEMSSourceConverter(Converter): |
---|
[5338089] | 481 | def sourceFilter(self, data): |
---|
[314be23] | 482 | data = fixLocalIncludes(data) |
---|
| 483 | data = fixIncludes(data) |
---|
| 484 | data = '#include <machine/rtems-bsd-kernel-space.h>\n\n' + data |
---|
| 485 | return data |
---|
| 486 | |
---|
[f1fcdba] | 487 | def convert(self, src, dst): |
---|
| 488 | sconverter = super(FromFreeBSDToRTEMSSourceConverter, self) |
---|
[5338089] | 489 | sconverter.convert(src, dst, sourceFilter = self.sourceFilter) |
---|
[f1fcdba] | 490 | |
---|
[314be23] | 491 | class FromFreeBSDToRTEMSUserSpaceSourceConverter(Converter): |
---|
[5338089] | 492 | def sourceFilter(self, data): |
---|
[314be23] | 493 | data = fixIncludes(data) |
---|
| 494 | data = '#include <machine/rtems-bsd-user-space.h>\n\n' + data |
---|
| 495 | return data |
---|
| 496 | |
---|
[f1fcdba] | 497 | def convert(self, src, dst): |
---|
| 498 | sconverter = super(FromFreeBSDToRTEMSUserSpaceSourceConverter, self) |
---|
[5338089] | 499 | sconverter.convert(src, dst, sourceFilter = self.sourceFilter) |
---|
[f1fcdba] | 500 | |
---|
[314be23] | 501 | class FromRTEMSToFreeBSDHeaderConverter(Converter): |
---|
[5338089] | 502 | def sourceFilter(self, data): |
---|
[314be23] | 503 | data = revertFixLocalIncludes(data) |
---|
| 504 | data = revertFixIncludes(data) |
---|
| 505 | return data |
---|
| 506 | |
---|
[f1fcdba] | 507 | def convert(self, src, dst): |
---|
| 508 | sconverter = super(FromRTEMSToFreeBSDHeaderConverter, self) |
---|
[5338089] | 509 | sconverter.convert(src, dst, hasSource = False, sourceFilter = self.sourceFilter) |
---|
[f1fcdba] | 510 | |
---|
[314be23] | 511 | class FromRTEMSToFreeBSDSourceConverter(Converter): |
---|
[5338089] | 512 | def sourceFilter(self, data): |
---|
[314be23] | 513 | data = re.sub('#include <machine/rtems-bsd-kernel-space.h>\n\n', '', data) |
---|
| 514 | data = re.sub('#include <machine/rtems-bsd-user-space.h>\n\n', '', data) |
---|
[95a12a2] | 515 | data = revertFixLocalIncludes(data) |
---|
| 516 | data = revertFixIncludes(data) |
---|
[314be23] | 517 | return data |
---|
| 518 | |
---|
[f1fcdba] | 519 | def convert(self, src, dst): |
---|
| 520 | sconverter = super(FromRTEMSToFreeBSDSourceConverter, self) |
---|
[5338089] | 521 | sconverter.convert(src, dst, hasSource = False, sourceFilter = self.sourceFilter) |
---|
[f1fcdba] | 522 | |
---|
| 523 | # |
---|
| 524 | # Compose a path based for the various parts of the source tree. |
---|
| 525 | # |
---|
[314be23] | 526 | class PathComposer(object): |
---|
| 527 | def composeFreeBSDPath(self, path): |
---|
| 528 | return path |
---|
| 529 | |
---|
| 530 | def composeRTEMSPath(self, path, prefix): |
---|
[f1fcdba] | 531 | return os.path.join(prefix, path) |
---|
[314be23] | 532 | |
---|
| 533 | class FreeBSDPathComposer(PathComposer): |
---|
| 534 | def composeFreeBSDPath(self, path): |
---|
[f1fcdba] | 535 | return os.path.join(FreeBSD_DIR, path) |
---|
[314be23] | 536 | |
---|
| 537 | def composeRTEMSPath(self, path, prefix): |
---|
[f1fcdba] | 538 | return os.path.join(prefix, 'freebsd', path) |
---|
[314be23] | 539 | |
---|
| 540 | class RTEMSPathComposer(PathComposer): |
---|
| 541 | def composeFreeBSDPath(self, path): |
---|
| 542 | return path |
---|
| 543 | |
---|
| 544 | def composeRTEMSPath(self, path, prefix): |
---|
[f1fcdba] | 545 | return os.path.join(prefix, 'rtemsbsd', path) |
---|
[314be23] | 546 | |
---|
| 547 | class CPUDependentPathComposer(FreeBSDPathComposer): |
---|
| 548 | def composeRTEMSPath(self, path, prefix): |
---|
| 549 | path = super(CPUDependentPathComposer, self).composeRTEMSPath(path, prefix) |
---|
| 550 | path = mapCPUDependentPath(path) |
---|
| 551 | return path |
---|
| 552 | |
---|
| 553 | class TargetSourceCPUDependentPathComposer(CPUDependentPathComposer): |
---|
| 554 | def __init__(self, targetCPU, sourceCPU): |
---|
| 555 | self.targetCPU = targetCPU |
---|
| 556 | self.sourceCPU = sourceCPU |
---|
| 557 | |
---|
| 558 | def composeRTEMSPath(self, path, prefix): |
---|
| 559 | path = super(TargetSourceCPUDependentPathComposer, self).composeRTEMSPath(path, prefix) |
---|
| 560 | path = path.replace(self.sourceCPU, self.targetCPU) |
---|
| 561 | return path |
---|
| 562 | |
---|
| 563 | class BuildSystemFragmentComposer(object): |
---|
[8440506] | 564 | def __init__(self, includes = None): |
---|
| 565 | if type(includes) is not list: |
---|
| 566 | self.includes = [includes] |
---|
| 567 | else: |
---|
| 568 | self.includes = includes |
---|
| 569 | |
---|
[314be23] | 570 | def compose(self, path): |
---|
| 571 | return '' |
---|
| 572 | |
---|
[f1fcdba] | 573 | # |
---|
| 574 | # File - a file in the source we move backwards and forwards. |
---|
| 575 | # |
---|
[314be23] | 576 | class File(object): |
---|
| 577 | def __init__(self, path, pathComposer, |
---|
[f1fcdba] | 578 | forwardConverter, reverseConverter, buildSystemComposer): |
---|
| 579 | if verbose(verboseMoreDetail): |
---|
| 580 | print("FILE: %-50s F:%-45s R:%-45s" % \ |
---|
| 581 | (path, |
---|
| 582 | forwardConverter.__class__.__name__, |
---|
| 583 | reverseConverter.__class__.__name__)) |
---|
[314be23] | 584 | self.path = path |
---|
| 585 | self.pathComposer = pathComposer |
---|
[f1fcdba] | 586 | self.freebsdPath = self.pathComposer.composeFreeBSDPath(self.path) |
---|
| 587 | self.rtemsPath = self.pathComposer.composeRTEMSPath(self.path, RTEMS_DIR) |
---|
| 588 | self.forwardConverter = forwardConverter |
---|
| 589 | self.reverseConverter = reverseConverter |
---|
[314be23] | 590 | self.buildSystemComposer = buildSystemComposer |
---|
| 591 | |
---|
[f1fcdba] | 592 | def processSource(self, forward): |
---|
| 593 | if forward: |
---|
| 594 | if verbose(verboseDetail): |
---|
| 595 | print("process source: %s => %s" % (self.freebsdPath, self.rtemsPath)) |
---|
| 596 | self.forwardConverter.convert(self.freebsdPath, self.rtemsPath) |
---|
| 597 | else: |
---|
| 598 | if verbose(verboseDetail): |
---|
| 599 | print("process source: %s => %s converter:%s" % \ |
---|
| 600 | (self.rtemsPath, self.freebsdPath, self.reverseConverter.__class__.__name__)) |
---|
| 601 | self.reverseConverter.convert(self.rtemsPath, self.freebsdPath) |
---|
[314be23] | 602 | |
---|
| 603 | def getFragment(self): |
---|
| 604 | return self.buildSystemComposer.compose(self.pathComposer.composeRTEMSPath(self.path, '')) |
---|
| 605 | |
---|
[f1fcdba] | 606 | # |
---|
[314be23] | 607 | # Module - logical group of related files we can perform actions on |
---|
[f1fcdba] | 608 | # |
---|
[314be23] | 609 | class Module: |
---|
| 610 | def __init__(self, name): |
---|
| 611 | self.name = name |
---|
| 612 | self.conditionalOn = "none" |
---|
| 613 | self.files = [] |
---|
| 614 | self.cpuDependentSourceFiles = {} |
---|
| 615 | self.dependencies = [] |
---|
| 616 | |
---|
| 617 | def initCPUDependencies(self, cpu): |
---|
| 618 | if cpu not in self.cpuDependentSourceFiles: |
---|
| 619 | self.cpuDependentSourceFiles[cpu] = [] |
---|
| 620 | |
---|
[f1fcdba] | 621 | def processSource(self, direction): |
---|
| 622 | if verbose(verboseDetail): |
---|
| 623 | print("process module: %s" % (self.name)) |
---|
[314be23] | 624 | for f in self.files: |
---|
[f1fcdba] | 625 | f.processSource(direction) |
---|
[314be23] | 626 | for cpu, files in self.cpuDependentSourceFiles.items(): |
---|
| 627 | for f in files: |
---|
[f1fcdba] | 628 | f.processSource(direction) |
---|
[314be23] | 629 | |
---|
| 630 | def addFiles(self, newFiles, buildSystemComposer = BuildSystemFragmentComposer()): |
---|
| 631 | files = [] |
---|
| 632 | for newFile in newFiles: |
---|
| 633 | assertFile(newFile) |
---|
| 634 | files += [File(newFile, composers, buildSystemComposer)] |
---|
| 635 | return files |
---|
| 636 | |
---|
| 637 | def addFile(self, f): |
---|
| 638 | self.files += [f] |
---|
| 639 | |
---|
| 640 | def addFiles(self, newFiles, |
---|
| 641 | pathComposer, fromFreeBSDToRTEMSConverter, fromRTEMSToFreeBSDConverter, |
---|
| 642 | assertFile, buildSystemComposer = BuildSystemFragmentComposer()): |
---|
| 643 | files = [] |
---|
| 644 | for newFile in newFiles: |
---|
| 645 | assertFile(newFile) |
---|
| 646 | files += [File(newFile, pathComposer, fromFreeBSDToRTEMSConverter, |
---|
| 647 | fromRTEMSToFreeBSDConverter, buildSystemComposer)] |
---|
| 648 | return files |
---|
| 649 | |
---|
| 650 | def addKernelSpaceHeaderFiles(self, files): |
---|
| 651 | self.files += self.addFiles(files, |
---|
| 652 | FreeBSDPathComposer(), FromFreeBSDToRTEMSHeaderConverter(), |
---|
| 653 | FromRTEMSToFreeBSDHeaderConverter(), assertHeaderFile) |
---|
| 654 | |
---|
| 655 | def addUserSpaceHeaderFiles(self, files): |
---|
| 656 | self.files += self.addFiles(files, |
---|
| 657 | FreeBSDPathComposer(), FromFreeBSDToRTEMSUserSpaceHeaderConverter(), |
---|
| 658 | FromRTEMSToFreeBSDHeaderConverter(), assertHeaderFile) |
---|
| 659 | |
---|
| 660 | def addRTEMSHeaderFiles(self, files): |
---|
| 661 | self.files += self.addFiles(files, RTEMSPathComposer(), |
---|
| 662 | NoConverter(), NoConverter(), assertHeaderFile) |
---|
| 663 | |
---|
| 664 | def addCPUDependentHeaderFiles(self, files): |
---|
| 665 | self.files += self.addFiles(files, |
---|
| 666 | CPUDependentPathComposer(), FromFreeBSDToRTEMSHeaderConverter(), |
---|
| 667 | FromRTEMSToFreeBSDHeaderConverter(), assertHeaderFile) |
---|
| 668 | |
---|
| 669 | def addTargetSourceCPUDependentHeaderFiles(self, targetCPUs, sourceCPU, files): |
---|
| 670 | for cpu in targetCPUs: |
---|
| 671 | self.files += self.addFiles(files, |
---|
| 672 | TargetSourceCPUDependentPathComposer(cpu, sourceCPU), |
---|
| 673 | FromFreeBSDToRTEMSHeaderConverter(), |
---|
| 674 | NoConverter(), assertHeaderFile) |
---|
| 675 | |
---|
| 676 | def addSourceFiles(self, files, sourceFileFragmentComposer): |
---|
| 677 | self.files += self.addFiles(files, |
---|
| 678 | PathComposer(), NoConverter(), NoConverter(), assertSourceFile, |
---|
| 679 | sourceFileFragmentComposer) |
---|
| 680 | |
---|
| 681 | def addKernelSpaceSourceFiles(self, files, sourceFileFragmentComposer): |
---|
| 682 | self.files += self.addFiles(files, |
---|
| 683 | FreeBSDPathComposer(), FromFreeBSDToRTEMSSourceConverter(), |
---|
| 684 | FromRTEMSToFreeBSDSourceConverter(), assertSourceFile, |
---|
| 685 | sourceFileFragmentComposer) |
---|
| 686 | |
---|
| 687 | def addUserSpaceSourceFiles(self, files, sourceFileFragmentComposer): |
---|
| 688 | self.files += self.addFiles(files, |
---|
| 689 | FreeBSDPathComposer(), |
---|
| 690 | FromFreeBSDToRTEMSUserSpaceSourceConverter(), |
---|
| 691 | FromRTEMSToFreeBSDSourceConverter(), assertSourceFile, |
---|
| 692 | sourceFileFragmentComposer) |
---|
| 693 | |
---|
| 694 | def addRTEMSSourceFiles(self, files, sourceFileFragmentComposer): |
---|
| 695 | self.files += self.addFiles(files, |
---|
| 696 | RTEMSPathComposer(), NoConverter(), NoConverter(), |
---|
| 697 | assertSourceFile, sourceFileFragmentComposer) |
---|
| 698 | |
---|
[40f7d5c] | 699 | def addCPUDependentSourceFiles(self, cpus, files, sourceFileFragmentComposer): |
---|
| 700 | for cpu in cpus: |
---|
[314be23] | 701 | self.initCPUDependencies(cpu) |
---|
| 702 | self.cpuDependentSourceFiles[cpu] += \ |
---|
| 703 | self.addFiles(files, |
---|
[40f7d5c] | 704 | CPUDependentPathComposer(), FromFreeBSDToRTEMSSourceConverter(), |
---|
| 705 | FromRTEMSToFreeBSDSourceConverter(), assertSourceFile, |
---|
| 706 | sourceFileFragmentComposer) |
---|
[314be23] | 707 | |
---|
| 708 | def addTest(self, testFragementComposer): |
---|
| 709 | self.files += [File(testFragementComposer.testName, |
---|
| 710 | PathComposer(), NoConverter(), NoConverter(), |
---|
| 711 | testFragementComposer)] |
---|
| 712 | |
---|
| 713 | def addDependency(self, dep): |
---|
| 714 | self.dependencies += [dep] |
---|
| 715 | |
---|
[f1fcdba] | 716 | # |
---|
| 717 | # Manager - a collection of modules. |
---|
| 718 | # |
---|
[314be23] | 719 | class ModuleManager: |
---|
| 720 | def __init__(self): |
---|
| 721 | self.modules = {} |
---|
| 722 | self.generator = {} |
---|
| 723 | self.setGenerators() |
---|
| 724 | |
---|
| 725 | def __getitem__(self, key): |
---|
| 726 | if key not in self.modules: |
---|
| 727 | raise KeyError('module %s not found' % (key)) |
---|
| 728 | return self.modules[key] |
---|
| 729 | |
---|
| 730 | def getModules(self): |
---|
| 731 | return sorted(self.modules.keys()) |
---|
| 732 | |
---|
| 733 | def addModule(self, module): |
---|
| 734 | self.modules[module.name] = module |
---|
| 735 | |
---|
[f1fcdba] | 736 | def processSource(self, direction): |
---|
| 737 | if verbose(verboseDetail): |
---|
| 738 | print("process modules:") |
---|
[314be23] | 739 | for m in sorted(self.modules): |
---|
[f1fcdba] | 740 | self.modules[m].processSource(direction) |
---|