source: rtems-libbsd/builder.py @ f949b43

5-freebsd-12
Last change on this file since f949b43 was dd60daa, checked in by Sebastian Huber <sebastian.huber@…>, on Aug 9, 2018 at 10:59:55 AM

Allow *.c as kernel space header files

This is a workaround for the FreeBSD kernel space source file

sys/opencrypto/xform.c

which includes a bunch of *.c files.

Update #3472.

  • Property mode set to 100755
File size: 31.4 KB
RevLine 
[314be23]1#
[97c5024a]2#  Copyright (c) 2015-2016 Chris Johns <chrisj@rtems.org>. All rights reserved.
[314be23]3#
[fa4ec51]4#  Copyright (c) 2009, 2017 embedded brains GmbH.  All rights reserved.
[314be23]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]37from __future__ import print_function
38
[314be23]39import shutil
40import os
41import re
42import sys
43import getopt
44import filecmp
45import difflib
[f1fcdba]46import codecs
[854427b]47import copy
[314be23]48
49#
50# Global controls.
51#
[fa4ec51]52LIBBSD_DIR = "."
[314be23]53FreeBSD_DIR = "freebsd-org"
[f1fcdba]54verboseLevel = 0
[314be23]55isDryRun = False
56isDiffMode = False
[97c5024a]57filesProcessedCount = 0
58filesProcessed = []
[f9798ad]59filesTotal = 0
60filesTotalLines = 0
61filesTotalInserts = 0
62filesTotalDeletes = 0
63diffDetails = { }
[97c5024a]64
[f1fcdba]65verboseInfo = 1
66verboseDetail = 2
67verboseMoreDetail = 3
68verboseDebug = 4
69
[a6a6d54]70def _cflagsIncludes(cflags, includes):
71    if type(cflags) is not list:
72        if cflags is not None:
73            _cflags = cflags.split(' ')
74        else:
75            _cflags = [None]
76    else:
77        _cflags = cflags
78    if type(includes) is not list:
79        _includes = [includes]
80    else:
81        _includes = includes
82    return _cflags, _includes
83
[f1fcdba]84def verbose(level = verboseInfo):
85    return verboseLevel >= level
86
[f9798ad]87def changedFileSummary(statsReport = False):
88
89    global filesTotal, filesTotalLines, filesTotalInserts, filesTotalDeletes
90
[97c5024a]91    if isDiffMode == False:
[f1fcdba]92        if verbose():
[f9798ad]93            print('%d file(s) were changed:' % (filesProcessedCount))
[f1fcdba]94            for f in sorted(filesProcessed):
95                print(' %s' % (f))
[f9798ad]96        else:
97            print('%d file(s) were changed.' % (filesProcessedCount))
98    if statsReport:
99        print('Stats Report:')
100        transparent = filesTotal - len(diffDetails)
101        changes = filesTotalInserts + filesTotalDeletes
102        opacity = (float(changes) / (filesTotalLines + changes)) * 100.0
103        print(' Total File(s):%d  Unchanged:%d (%.1f%%)  Changed:%d' \
104              '   Opacity:%5.1f%% Lines:%d Edits:%d (+):%d (-):%d'  % \
105              (filesTotal, transparent, (float(transparent) / filesTotal) * 100.0, len(diffDetails), \
106               opacity, filesTotalLines, changes, filesTotalInserts, filesTotalDeletes))
107        #
108        # Sort by opacity.
109        #
110        ordered_diffs = sorted(diffDetails.items(), key = lambda diff: diff[1].opacity, reverse = True)
111        for f in ordered_diffs:
112            print(%s' % (diffDetails[f[0]].status()))
[f1fcdba]113
[5338089]114def readFile(name):
[f1fcdba]115    try:
116        contents = codecs.open(name, mode = 'r', encoding = 'utf-8', errors = 'ignore').read()
117    except UnicodeDecodeError as ude:
118        print('error: reading: %s: %s' % (name, ude))
119        sys.exit(1)
120    return contents
121
[5338089]122def writeFile(name, contents):
[f1fcdba]123    path = os.path.dirname(name)
124    if not os.path.exists(path):
125        try:
126            os.makedirs(path)
127        except OSError as oe:
[5338089]128            print('error: cannot create directory: %s: %s' % (path, oe))
[f1fcdba]129            sys.exit(1)
130    try:
131        codecs.open(name, mode = 'w',  encoding = 'utf-8', errors = 'ignore').write(contents)
132    except UnicodeDecodeError as ude:
133        print('error: write: %s: %s' % (name, ude))
134        sys.exit(1)
[314be23]135
[f1fcdba]136#
137# A builder error.
138#
[5ba6949]139class error(Exception):
140    """Base class for exceptions."""
[f1fcdba]141    def __init__(self, msg):
[5ba6949]142        self.msg = 'error: %s' % (msg)
143    def set_output(self, msg):
144        self.msg = msg
145    def __str__(self):
146        return self.msg
147
[f9798ad]148#
149# Diff Record
150#
151class diffRecord:
152    def __init__(self, src, dst, orig, diff, inserts, deletes):
153        self.src = src
154        self.dst = dst
155        self.orig = orig
156        self.diff = diff
157        self.lines = len(orig)
158        self.inserts = inserts
159        self.deletes = deletes
160        self.changes = inserts + deletes
161        self.opacity = (float(self.changes) / (self.lines + self.changes)) * 100.0
162
163    def __repr__(self):
164        return self.src
165
166    def status(self):
167        return 'opacity:%5.1f%% edits:%4d (+):%-4d (-):%-4d %s' % \
168            (self.opacity, self.changes, self.inserts, self.deletes, self.src)
169
[f1fcdba]170#
171# This stuff needs to move to libbsd.py.
172#
[0768880]173
[314be23]174# Move target dependent files under a machine directory
175def mapCPUDependentPath(path):
176  return path.replace("include/", "include/machine/")
177
178def fixIncludes(data):
179    data = re.sub('#include <sys/resource.h>', '#include <rtems/bsd/sys/resource.h>', data)
[f1fcdba]180    data = re.sub('#include <sys/unistd.h>',   '#include <rtems/bsd/sys/unistd.h>', data)
[314be23]181    return data
182
183# revert fixing the include paths inside a C or .h file
184def revertFixIncludes(data):
[f1fcdba]185    data = re.sub('#include <rtems/bsd/',  '#include <', data)
186    data = re.sub('#include <util.h>',     '#include <rtems/bsd/util.h>', data)
187    data = re.sub('#include <bsd.h>',      '#include <rtems/bsd/bsd.h>', data)
[314be23]188    data = re.sub('#include <zerocopy.h>', '#include <rtems/bsd/zerocopy.h>', data)
[338f300]189    data = re.sub('#include <modules.h>',  '#include <rtems/bsd/modules.h>', data)
[314be23]190    return data
191
192# fix include paths inside a C or .h file
193def fixLocalIncludes(data):
[f1fcdba]194    data = re.sub('#include "opt_([^"]*)"',    '#include <rtems/bsd/local/opt_\\1>', data)
195    data = re.sub('#include "([^"]*)_if.h"',   '#include <rtems/bsd/local/\\1_if.h>', data)
[314be23]196    data = re.sub('#include "miidevs([^"]*)"', '#include <rtems/bsd/local/miidevs\\1>', data)
197    data = re.sub('#include "usbdevs([^"]*)"', '#include <rtems/bsd/local/usbdevs\\1>', data)
198    return data
199
200# revert fixing the include paths inside a C or .h file
201def revertFixLocalIncludes(data):
202    data = re.sub('#include <rtems/bsd/local/([^>]*)>', '#include "\\1"', data)
203    return data
204
205def assertHeaderFile(path):
206    if path[-2] != '.' or path[-1] != 'h':
[97c5024a]207        print("*** " + path + " does not end in .h")
208        print("*** Move it to a C source file list")
[314be23]209        sys.exit(2)
210
211def assertSourceFile(path):
212    if path[-2] != '.' or (path[-1] != 'c' and path[-1] != 'S'):
[dd60daa]213        print("*** " + path + " does not end in .c or .S")
[97c5024a]214        print("*** Move it to a header file list")
[314be23]215        sys.exit(2)
216
[dd60daa]217def assertHeaderOrSourceFile(path):
218    if path[-2] != '.' or (path[-1] != 'h' and path[-1] != 'c'):
219        print("*** " + path + " does not end in .h or .c")
220        print("*** Move it to another list")
221        sys.exit(2)
222
[f9798ad]223def diffSource(dstLines, srcLines, src, dst):
224    global filesTotal, filesTotalLines, filesTotalInserts, filesTotalDeletes
225    #
226    # Diff, note there is no line termination on each string.  Expand the
227    # generator to list because the generator is not reusable.
228    #
229    diff = list(difflib.unified_diff(dstLines,
230                                     srcLines,
231                                     fromfile = src,
232                                     tofile = dst,
233                                     n = 5,
234                                     lineterm = ''))
235    inserts = 0
236    deletes = 0
237    if len(diff) > 0:
238        if src in diffDetails and \
239           diffDetails[src].dst != dst and diffDetails[src].diff != diff:
240            raise error('repeated diff of file different: src:%s dst:%s' % (src, dst))
241        for l in diff:
242            if l[0] == '-':
243                deletes += 1
244            elif l[0] == '+':
245                inserts += 1
246        diffDetails[src] = diffRecord(src, dst, srcLines, diff, inserts, deletes)
247
248    #
249    # Count the total files, lines and the level of changes.
250    #
251    filesTotal += 1
252    filesTotalLines += len(srcLines)
253    filesTotalInserts += inserts
254    filesTotalDeletes += deletes
255
256    return diff
257
[f1fcdba]258#
259# Converters provide a way to alter the various types of code. The conversion
260# process filters a file as it is copies from the source path to the
261# destination path. Specialised versions are provided for different types of
262# source.
263#
[314be23]264class Converter(object):
265
[5338089]266    def convert(self, src, dst, hasSource = True, sourceFilter = None, srcContents = None):
[f1fcdba]267
268        global filesProcessed, filesProcessedCount
269
270        if verbose(verboseDebug):
271            print("convert: filter:%s: %s -> %s" % \
[5338089]272                  (['yes', 'no'][sourceFilter is None], src, dst))
[f1fcdba]273
274        #
275        # If there is no source raise an error if we expect source else print a
276        # warning and do not try and convert.
277        #
[5338089]278        if srcContents is None:
[f1fcdba]279            if not os.path.exists(src):
[5338089]280                if hasSource:
[f1fcdba]281                    raise error('source not found: %s' % (src))
282                else:
283                    print('warning: no source: %s' % (src))
284                    return
285
286            #
287            # Files read as a single string if not passed in.
288            #
[5338089]289            srcContents = readFile(src)
[f1fcdba]290
291        if os.path.exists(dst):
[5338089]292            dstContents = readFile(dst)
[f1fcdba]293        else:
294            print('warning: no destination: %s' % (dst))
[5338089]295            dstContents = ''
[f1fcdba]296
297        #
298        # Filter the source.
299        #
[5338089]300        if sourceFilter is not None:
301            srcContents = sourceFilter(srcContents)
[f1fcdba]302
303        #
304        # Split into a list of lines.
305        #
[5338089]306        srcLines = srcContents.split(os.linesep)
307        dstLines = dstContents.split(os.linesep)
[f1fcdba]308
309        if verbose(verboseDebug):
[5338089]310            print('Unified diff: %s (lines:%d)' % (src, len(srcLines)))
[f1fcdba]311
312        #
[f9798ad]313        # Diff, note there is no line termination on each string.
[f1fcdba]314        #
[f9798ad]315        diff = diffSource(dstLines, srcLines, src, dst)
[f1fcdba]316
317        #
318        # The diff list is empty if the files are the same.
319        #
320        if len(diff) > 0:
321
322            if verbose(verboseDebug):
323                print('Unified diff length: %d' % len(diff))
324
325            filesProcessed += [dst]
326            filesProcessedCount += 1
327            if isDiffMode == False:
328                if verbose(verboseDetail):
329                    print("UPDATE: %s -> %s" % (src, dst))
330                if isDryRun == False:
[5338089]331                    writeFile(dst, srcContents)
[f1fcdba]332            else:
333                print("diff -u %s %s" % (src, dst))
334                for l in diff:
335                    print(l)
[314be23]336
337class NoConverter(Converter):
[5338089]338    def convert(self, src, dst, hasSource = True, sourceFilter = None):
[314be23]339        return '/* EMPTY */\n'
340
341class FromFreeBSDToRTEMSHeaderConverter(Converter):
[5338089]342    def sourceFilter(self, data):
[314be23]343        data = fixLocalIncludes(data)
344        data = fixIncludes(data)
345        return data
346
[f1fcdba]347    def convert(self, src, dst):
348        sconverter = super(FromFreeBSDToRTEMSHeaderConverter, self)
[5338089]349        sconverter.convert(src, dst, sourceFilter = self.sourceFilter)
[f1fcdba]350
[314be23]351class FromFreeBSDToRTEMSUserSpaceHeaderConverter(Converter):
[5338089]352    def sourceFilter(self, data):
[314be23]353        data = fixIncludes(data)
354        return data
355
[f1fcdba]356    def convert(self, src, dst):
357        sconverter = super(FromFreeBSDToRTEMSUserSpaceHeaderConverter, self)
[5338089]358        sconverter.convert(src, dst, sourceFilter = self.sourceFilter)
[f1fcdba]359
[314be23]360class FromFreeBSDToRTEMSSourceConverter(Converter):
[5338089]361    def sourceFilter(self, data):
[314be23]362        data = fixLocalIncludes(data)
363        data = fixIncludes(data)
364        data = '#include <machine/rtems-bsd-kernel-space.h>\n\n' + data
365        return data
366
[f1fcdba]367    def convert(self, src, dst):
368        sconverter = super(FromFreeBSDToRTEMSSourceConverter, self)
[5338089]369        sconverter.convert(src, dst, sourceFilter = self.sourceFilter)
[f1fcdba]370
[314be23]371class FromFreeBSDToRTEMSUserSpaceSourceConverter(Converter):
[5338089]372    def sourceFilter(self, data):
[314be23]373        data = fixIncludes(data)
374        data = '#include <machine/rtems-bsd-user-space.h>\n\n' + data
375        return data
376
[f1fcdba]377    def convert(self, src, dst):
378        sconverter = super(FromFreeBSDToRTEMSUserSpaceSourceConverter, self)
[5338089]379        sconverter.convert(src, dst, sourceFilter = self.sourceFilter)
[f1fcdba]380
[314be23]381class FromRTEMSToFreeBSDHeaderConverter(Converter):
[5338089]382    def sourceFilter(self, data):
[314be23]383        data = revertFixLocalIncludes(data)
384        data = revertFixIncludes(data)
385        return data
386
[f1fcdba]387    def convert(self, src, dst):
388        sconverter = super(FromRTEMSToFreeBSDHeaderConverter, self)
[5338089]389        sconverter.convert(src, dst, hasSource = False,  sourceFilter = self.sourceFilter)
[f1fcdba]390
[314be23]391class FromRTEMSToFreeBSDSourceConverter(Converter):
[5338089]392    def sourceFilter(self, data):
[314be23]393        data = re.sub('#include <machine/rtems-bsd-kernel-space.h>\n\n', '', data)
394        data = re.sub('#include <machine/rtems-bsd-user-space.h>\n\n', '', data)
[95a12a2]395        data = revertFixLocalIncludes(data)
396        data = revertFixIncludes(data)
[314be23]397        return data
398
[f1fcdba]399    def convert(self, src, dst):
400        sconverter = super(FromRTEMSToFreeBSDSourceConverter, self)
[5338089]401        sconverter.convert(src, dst, hasSource = False, sourceFilter = self.sourceFilter)
[f1fcdba]402
403#
404# Compose a path based for the various parts of the source tree.
405#
[314be23]406class PathComposer(object):
[fa4ec51]407    def composeOriginPath(self, path):
[314be23]408        return path
409
[fa4ec51]410    def composeLibBSDPath(self, path, prefix):
[f1fcdba]411        return os.path.join(prefix, path)
[314be23]412
413class FreeBSDPathComposer(PathComposer):
[fa4ec51]414    def composeOriginPath(self, path):
[f1fcdba]415        return os.path.join(FreeBSD_DIR, path)
[314be23]416
[fa4ec51]417    def composeLibBSDPath(self, path, prefix):
[f1fcdba]418        return os.path.join(prefix, 'freebsd', path)
[314be23]419
420class RTEMSPathComposer(PathComposer):
[fa4ec51]421    def composeOriginPath(self, path):
[314be23]422        return path
423
[fa4ec51]424    def composeLibBSDPath(self, path, prefix):
[f1fcdba]425        return os.path.join(prefix, 'rtemsbsd', path)
[314be23]426
[23d6e50]427class LinuxPathComposer(PathComposer):
428    def composeOriginPath(self, path):
[445d59e]429        return path
[23d6e50]430
431    def composeLibBSDPath(self, path, prefix):
432        return os.path.join(prefix, 'linux', path)
433
[fa4ec51]434class CPUDependentFreeBSDPathComposer(FreeBSDPathComposer):
435    def composeLibBSDPath(self, path, prefix):
436        path = super(CPUDependentFreeBSDPathComposer, self).composeLibBSDPath(path, prefix)
[314be23]437        path = mapCPUDependentPath(path)
438        return path
439
[238e275]440class CPUDependentRTEMSPathComposer(RTEMSPathComposer):
441    def composeLibBSDPath(self, path, prefix):
442        path = super(CPUDependentRTEMSPathComposer, self).composeLibBSDPath(path, prefix)
443        path = mapCPUDependentPath(path)
444        return path
445
[23d6e50]446class CPUDependentLinuxPathComposer(LinuxPathComposer):
447    def composeLibBSDPath(self, path, prefix):
448        path = super(CPUDependentLinuxPathComposer, self).composeLibBSDPath(path, prefix)
449        path = mapCPUDependentPath(path)
450        return path
451
[fa4ec51]452class TargetSourceCPUDependentPathComposer(CPUDependentFreeBSDPathComposer):
[314be23]453    def __init__(self, targetCPU, sourceCPU):
454        self.targetCPU = targetCPU
455        self.sourceCPU = sourceCPU
456
[fa4ec51]457    def composeLibBSDPath(self, path, prefix):
458        path = super(TargetSourceCPUDependentPathComposer, self).composeLibBSDPath(path, prefix)
[314be23]459        path = path.replace(self.sourceCPU, self.targetCPU)
460        return path
461
462class BuildSystemFragmentComposer(object):
[8440506]463    def __init__(self, includes = None):
464        if type(includes) is not list:
465            self.includes = [includes]
466        else:
467            self.includes = includes
468
[314be23]469    def compose(self, path):
470        return ''
471
[a6a6d54]472class SourceFileFragmentComposer(BuildSystemFragmentComposer):
473
474    def __init__(self, cflags = "default", includes = None):
475        self.cflags, self.includes = _cflagsIncludes(cflags, includes)
476
477    def compose(self, path):
478        if None in self.includes:
479            flags = self.cflags
480        else:
481            flags = self.cflags + self.includes
482        return ['sources', flags, ('default', None)], [path], self.cflags, self.includes
483
484class SourceFileIfHeaderComposer(SourceFileFragmentComposer):
485
486    def __init__(self, headers, cflags = "default", includes = None):
487        if headers is not list:
488            headers = [headers]
489        self.headers = headers
490        super(SourceFileIfHeaderComposer, self).__init__(cflags = cflags, includes = includes)
491
492    def compose(self, path):
493        r = SourceFileFragmentComposer.compose(self, path)
494        define_keys = ''
495        for h in self.headers:
496            h = h.upper()
497            for c in '\/-.':
498                h = h.replace(c, '_')
499            define_keys += ' ' + h
500        r[0][2] = (define_keys.strip(), self.headers)
501        return r
502
503class TestFragementComposer(BuildSystemFragmentComposer):
504
[dd35ec5]505    def __init__(self, testName, fileFragments, runTest = True, netTest = False, extraLibs = []):
[a6a6d54]506        self.testName = testName
507        self.fileFragments = fileFragments
508        self.runTest = runTest
509        self.netTest = netTest
[dd35ec5]510        self.extraLibs = extraLibs
[a6a6d54]511
512    def compose(self, path):
513        return ['tests', self.testName, ('default', None)], { 'files': self.fileFragments,
514                                                              'run': self.runTest,
[dd35ec5]515                                                              'net': self.netTest,
516                                                              'libs': self.extraLibs}
[a6a6d54]517
518class TestIfHeaderComposer(TestFragementComposer):
519
[dd35ec5]520    def __init__(self, testName, headers, fileFragments, runTest = True, netTest = False, extraLibs = []):
[a6a6d54]521        if headers is not list:
522            headers = [headers]
523        self.headers = headers
524        super(TestIfHeaderComposer, self).__init__(testName, fileFragments,
[dd35ec5]525                                                   runTest = runTest, netTest = netTest,
526                                                   extraLibs = extraLibs)
[a6a6d54]527
528    def compose(self, path):
529        r = TestFragementComposer.compose(self, path)
530        define_keys = ''
531        for h in self.headers:
532            h = h.upper()
533            for c in '\/-.':
534                h = h.replace(c, '_')
535            define_keys += ' ' + h
536        r[0][2] = (define_keys.strip(), self.headers)
537        return r
538
539class KVMSymbolsFragmentComposer(BuildSystemFragmentComposer):
540
541    def compose(self, path):
542        return ['KVMSymbols', 'files', ('default', None)], [path], self.includes
543
544class RPCGENFragmentComposer(BuildSystemFragmentComposer):
545
546    def compose(self, path):
547        return ['RPCGen', 'files', ('default', None)], [path]
548
549class RouteKeywordsFragmentComposer(BuildSystemFragmentComposer):
550
551    def compose(self, path):
552        return ['RouteKeywords', 'files', ('default', None)], [path]
553
554class LexFragmentComposer(BuildSystemFragmentComposer):
555
[01855a5]556    def __init__(self, sym, dep, cflags = None, includes = None, build = True):
[a6a6d54]557        self.sym = sym
558        self.dep = dep
559        self.cflags, self.includes = _cflagsIncludes(cflags, includes)
[01855a5]560        self.build = build
[a6a6d54]561
562    def compose(self, path):
563        d = { 'file': path,
564              'sym': self.sym,
[01855a5]565              'dep': self.dep,
566              'build': self.build }
[a6a6d54]567        if None not in self.cflags:
568            d['cflags'] = self.cflags
569        if None not in self.includes:
570            d['includes'] = self.includes
571        return ['lex', path, ('default', None)], d
572
573class YaccFragmentComposer(BuildSystemFragmentComposer):
574
[01855a5]575    def __init__(self, sym, header, cflags = None, includes = None, build = True):
[a6a6d54]576        self.sym = sym
577        self.header = header
578        self.cflags, self.includes = _cflagsIncludes(cflags, includes)
[01855a5]579        self.build = build
[a6a6d54]580
581    def compose(self, path):
582        d = { 'file': path,
583              'sym': self.sym,
[01855a5]584              'header': self.header,
585              'build': self.build }
[a6a6d54]586        if None not in self.cflags:
587            d['cflags'] = self.cflags
588        if None not in self.includes:
589            d['includes'] = self.includes
590        return ['yacc', path, ('default', None)], d
591
[f1fcdba]592#
593# File - a file in the source we move backwards and forwards.
594#
[314be23]595class File(object):
596    def __init__(self, path, pathComposer,
[f1fcdba]597                 forwardConverter, reverseConverter, buildSystemComposer):
598        if verbose(verboseMoreDetail):
599            print("FILE: %-50s F:%-45s R:%-45s" % \
600                  (path,
601                   forwardConverter.__class__.__name__,
602                   reverseConverter.__class__.__name__))
[314be23]603        self.path = path
604        self.pathComposer = pathComposer
[fa4ec51]605        self.originPath = self.pathComposer.composeOriginPath(self.path)
606        self.libbsdPath = self.pathComposer.composeLibBSDPath(self.path, LIBBSD_DIR)
[f1fcdba]607        self.forwardConverter = forwardConverter
608        self.reverseConverter = reverseConverter
[314be23]609        self.buildSystemComposer = buildSystemComposer
610
[f1fcdba]611    def processSource(self, forward):
612        if forward:
613            if verbose(verboseDetail):
[fa4ec51]614                print("process source: %s => %s" % (self.originPath, self.libbsdPath))
615            self.forwardConverter.convert(self.originPath, self.libbsdPath)
[f1fcdba]616        else:
617            if verbose(verboseDetail):
618                print("process source: %s => %s converter:%s" % \
[fa4ec51]619                      (self.libbsdPath, self.originPath, self.reverseConverter.__class__.__name__))
620            self.reverseConverter.convert(self.libbsdPath, self.originPath)
[314be23]621
622    def getFragment(self):
[fa4ec51]623        return self.buildSystemComposer.compose(self.pathComposer.composeLibBSDPath(self.path, ''))
[314be23]624
[f1fcdba]625#
[314be23]626# Module - logical group of related files we can perform actions on
[f1fcdba]627#
[f7a09b5]628class Module(object):
[d797c5d]629    def __init__(self, manager, name, enabled = True):
630        self.manager = manager
[314be23]631        self.name = name
632        self.conditionalOn = "none"
633        self.files = []
634        self.cpuDependentSourceFiles = {}
635        self.dependencies = []
636
637    def initCPUDependencies(self, cpu):
638        if cpu not in self.cpuDependentSourceFiles:
639            self.cpuDependentSourceFiles[cpu] = []
640
[f1fcdba]641    def processSource(self, direction):
642        if verbose(verboseDetail):
643            print("process module: %s" % (self.name))
[314be23]644        for f in self.files:
[f1fcdba]645            f.processSource(direction)
[314be23]646        for cpu, files in self.cpuDependentSourceFiles.items():
647            for f in files:
[f1fcdba]648                f.processSource(direction)
[314be23]649
650    def addFiles(self, newFiles, buildSystemComposer = BuildSystemFragmentComposer()):
651        files = []
652        for newFile in newFiles:
653            assertFile(newFile)
654            files += [File(newFile, composers, buildSystemComposer)]
655        return files
656
657    def addFile(self, f):
658        self.files += [f]
659
660    def addFiles(self, newFiles,
[fa4ec51]661                 pathComposer, forwardConverter, reverseConverter,
[314be23]662                 assertFile, buildSystemComposer = BuildSystemFragmentComposer()):
663        files = []
664        for newFile in newFiles:
665            assertFile(newFile)
[fa4ec51]666            files += [File(newFile, pathComposer, forwardConverter,
667                           reverseConverter, buildSystemComposer)]
[314be23]668        return files
669
670    def addKernelSpaceHeaderFiles(self, files):
671        self.files += self.addFiles(files,
672                                    FreeBSDPathComposer(), FromFreeBSDToRTEMSHeaderConverter(),
[dd60daa]673                                    FromRTEMSToFreeBSDHeaderConverter(), assertHeaderOrSourceFile)
[314be23]674
675    def addUserSpaceHeaderFiles(self, files):
676        self.files += self.addFiles(files,
677                                    FreeBSDPathComposer(), FromFreeBSDToRTEMSUserSpaceHeaderConverter(),
678                                    FromRTEMSToFreeBSDHeaderConverter(), assertHeaderFile)
679
680    def addRTEMSHeaderFiles(self, files):
681        self.files += self.addFiles(files, RTEMSPathComposer(),
682                                    NoConverter(), NoConverter(), assertHeaderFile)
683
[23d6e50]684    def addLinuxHeaderFiles(self, files):
685        self.files += self.addFiles(files,
[445d59e]686                                    PathComposer(), NoConverter(),
687                                    NoConverter(), assertHeaderFile)
[23d6e50]688
[fa4ec51]689    def addCPUDependentFreeBSDHeaderFiles(self, files):
[314be23]690        self.files += self.addFiles(files,
[fa4ec51]691                                    CPUDependentFreeBSDPathComposer(), FromFreeBSDToRTEMSHeaderConverter(),
[314be23]692                                    FromRTEMSToFreeBSDHeaderConverter(), assertHeaderFile)
693
[23d6e50]694    def addCPUDependentLinuxHeaderFiles(self, files):
695        self.files += self.addFiles(files,
[445d59e]696                                    CPUDependentLinuxPathComposer(), NoConverter(),
697                                    NoConverter(), assertHeaderFile)
[23d6e50]698
[314be23]699    def addTargetSourceCPUDependentHeaderFiles(self, targetCPUs, sourceCPU, files):
700        for cpu in targetCPUs:
701            self.files += self.addFiles(files,
702                                        TargetSourceCPUDependentPathComposer(cpu, sourceCPU),
703                                        FromFreeBSDToRTEMSHeaderConverter(),
704                                        NoConverter(), assertHeaderFile)
705
706    def addSourceFiles(self, files, sourceFileFragmentComposer):
707        self.files += self.addFiles(files,
708                                    PathComposer(), NoConverter(), NoConverter(), assertSourceFile,
709                                    sourceFileFragmentComposer)
710
711    def addKernelSpaceSourceFiles(self, files, sourceFileFragmentComposer):
712        self.files += self.addFiles(files,
713                                    FreeBSDPathComposer(), FromFreeBSDToRTEMSSourceConverter(),
714                                    FromRTEMSToFreeBSDSourceConverter(), assertSourceFile,
715                                    sourceFileFragmentComposer)
716
717    def addUserSpaceSourceFiles(self, files, sourceFileFragmentComposer):
718        self.files += self.addFiles(files,
719                                    FreeBSDPathComposer(),
720                                    FromFreeBSDToRTEMSUserSpaceSourceConverter(),
721                                    FromRTEMSToFreeBSDSourceConverter(), assertSourceFile,
722                                    sourceFileFragmentComposer)
723
724    def addRTEMSSourceFiles(self, files, sourceFileFragmentComposer):
725        self.files += self.addFiles(files,
726                                    RTEMSPathComposer(), NoConverter(), NoConverter(),
727                                    assertSourceFile, sourceFileFragmentComposer)
728
[23d6e50]729    def addLinuxSourceFiles(self, files, sourceFileFragmentComposer):
730        self.files += self.addFiles(files,
[445d59e]731                                    PathComposer(), NoConverter(),
732                                    NoConverter(), assertSourceFile,
[23d6e50]733                                    sourceFileFragmentComposer)
734
[fa4ec51]735    def addCPUDependentFreeBSDSourceFiles(self, cpus, files, sourceFileFragmentComposer):
[40f7d5c]736        for cpu in cpus:
[314be23]737            self.initCPUDependencies(cpu)
738            self.cpuDependentSourceFiles[cpu] += \
739                self.addFiles(files,
[fa4ec51]740                              CPUDependentFreeBSDPathComposer(), FromFreeBSDToRTEMSSourceConverter(),
[40f7d5c]741                              FromRTEMSToFreeBSDSourceConverter(), assertSourceFile,
742                              sourceFileFragmentComposer)
[23d6e50]743
[238e275]744    def addCPUDependentRTEMSSourceFiles(self, cpus, files, sourceFileFragmentComposer):
745        for cpu in cpus:
746            self.initCPUDependencies(cpu)
747            self.cpuDependentSourceFiles[cpu] += \
748                self.addFiles(files,
749                              CPUDependentRTEMSPathComposer(), NoConverter(),
750                              NoConverter(), assertSourceFile,
751                              sourceFileFragmentComposer)
752
[23d6e50]753    def addCPUDependentLinuxSourceFiles(self, cpus, files, sourceFileFragmentComposer):
754        for cpu in cpus:
755            self.initCPUDependencies(cpu)
756            self.cpuDependentSourceFiles[cpu] += \
757                self.addFiles(files,
[445d59e]758                              CPUDependentLinuxPathComposer(), NoConverter(),
759                              NoConverter(), assertSourceFile,
[23d6e50]760                              sourceFileFragmentComposer)
[314be23]761
762    def addTest(self, testFragementComposer):
763        self.files += [File(testFragementComposer.testName,
764                            PathComposer(), NoConverter(), NoConverter(),
765                            testFragementComposer)]
766
767    def addDependency(self, dep):
768        self.dependencies += [dep]
769
[f1fcdba]770#
771# Manager - a collection of modules.
772#
[f7a09b5]773class ModuleManager(object):
[314be23]774    def __init__(self):
775        self.modules = {}
776        self.generator = {}
[f7a09b5]777        self.configuration = {}
[314be23]778        self.setGenerators()
779
780    def __getitem__(self, key):
781        if key not in self.modules:
782            raise KeyError('module %s not found' % (key))
783        return self.modules[key]
784
[d797c5d]785    def getAllModules(self):
786        if 'modules' in self.configuration:
787            return self.configuration['modules']
788        return []
789
790    def getEnabledModules(self):
791        if 'modules-enabled' in self.configuration:
792            return self.configuration['modules-enabled']
793        return []
[314be23]794
795    def addModule(self, module):
796        self.modules[module.name] = module
797
[f1fcdba]798    def processSource(self, direction):
799        if verbose(verboseDetail):
800            print("process modules:")
[314be23]801        for m in sorted(self.modules):
[f1fcdba]802            self.modules[m].processSource(direction)
[f7a09b5]803
804    def setConfiguration(self, config):
[854427b]805        self.configuration = copy.deepcopy(config)
[f7a09b5]806
807    def getConfiguration(self):
808        return self.configuration
[d797c5d]809
[854427b]810    def updateConfiguration(self, config):
811        self.configuration.update(config)
812
[d797c5d]813    def setModuleConfigiuration(self):
814        mods = sorted(self.modules.keys())
815        self.configuration['modules'] = mods
[854427b]816        # Enabled modules are overwritten by config file. Default to all.
817        self.configuration['modules-enabled'] = mods
[d797c5d]818
[fedd993]819    def generateBuild(self, only_enabled=True):
820        modules_to_process = self.getEnabledModules()
821        # Used for copy between FreeBSD and RTEMS
822        if only_enabled == False:
823            modules_to_process = self.getAllModules()
824        for m in modules_to_process:
[d797c5d]825            self.modules[m].generate()
[a6a6d54]826
827    def setGenerators(self):
828        self.generator['convert'] = Converter
829        self.generator['no-convert'] = NoConverter
830        self.generator['from-FreeBSD-to-RTEMS-UserSpaceSourceConverter'] = FromFreeBSDToRTEMSUserSpaceSourceConverter
831        self.generator['from-RTEMS-To-FreeBSD-SourceConverter'] = FromRTEMSToFreeBSDSourceConverter
832        self.generator['buildSystemFragmentComposer'] = BuildSystemFragmentComposer
833
834        self.generator['file'] = File
835
836        self.generator['path'] = PathComposer
837        self.generator['freebsd-path'] = FreeBSDPathComposer
838        self.generator['rtems-path'] = RTEMSPathComposer
839        self.generator['cpu-path'] = CPUDependentFreeBSDPathComposer
840        self.generator['target-src-cpu--path'] = TargetSourceCPUDependentPathComposer
841
842        self.generator['source'] = SourceFileFragmentComposer
843        self.generator['test'] = TestFragementComposer
844        self.generator['kvm-symbols'] = KVMSymbolsFragmentComposer
845        self.generator['rpc-gen'] = RPCGENFragmentComposer
846        self.generator['route-keywords'] = RouteKeywordsFragmentComposer
847        self.generator['lex'] = LexFragmentComposer
848        self.generator['yacc'] = YaccFragmentComposer
849
850        self.generator['source-if-header'] = SourceFileIfHeaderComposer
851        self.generator['test-if-header'] = TestIfHeaderComposer
Note: See TracBrowser for help on using the repository browser.