source: rtems-libbsd/builder.py @ 91b858d

5-freebsd-12
Last change on this file since 91b858d 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
Line 
1#
2#  Copyright (c) 2015-2016 Chris Johns <chrisj@rtems.org>. All rights reserved.
3#
4#  Copyright (c) 2009, 2017 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
37from __future__ import print_function
38
39import shutil
40import os
41import re
42import sys
43import getopt
44import filecmp
45import difflib
46import codecs
47import copy
48
49#
50# Global controls.
51#
52LIBBSD_DIR = "."
53FreeBSD_DIR = "freebsd-org"
54verboseLevel = 0
55isDryRun = False
56isDiffMode = False
57filesProcessedCount = 0
58filesProcessed = []
59filesTotal = 0
60filesTotalLines = 0
61filesTotalInserts = 0
62filesTotalDeletes = 0
63diffDetails = { }
64
65verboseInfo = 1
66verboseDetail = 2
67verboseMoreDetail = 3
68verboseDebug = 4
69
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
84def verbose(level = verboseInfo):
85    return verboseLevel >= level
86
87def changedFileSummary(statsReport = False):
88
89    global filesTotal, filesTotalLines, filesTotalInserts, filesTotalDeletes
90
91    if isDiffMode == False:
92        if verbose():
93            print('%d file(s) were changed:' % (filesProcessedCount))
94            for f in sorted(filesProcessed):
95                print(' %s' % (f))
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()))
113
114def readFile(name):
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
122def writeFile(name, contents):
123    path = os.path.dirname(name)
124    if not os.path.exists(path):
125        try:
126            os.makedirs(path)
127        except OSError as oe:
128            print('error: cannot create directory: %s: %s' % (path, oe))
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)
135
136#
137# A builder error.
138#
139class error(Exception):
140    """Base class for exceptions."""
141    def __init__(self, msg):
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
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
170#
171# This stuff needs to move to libbsd.py.
172#
173
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)
180    data = re.sub('#include <sys/unistd.h>',   '#include <rtems/bsd/sys/unistd.h>', data)
181    return data
182
183# revert fixing the include paths inside a C or .h file
184def revertFixIncludes(data):
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)
188    data = re.sub('#include <zerocopy.h>', '#include <rtems/bsd/zerocopy.h>', data)
189    data = re.sub('#include <modules.h>',  '#include <rtems/bsd/modules.h>', data)
190    return data
191
192# fix include paths inside a C or .h file
193def fixLocalIncludes(data):
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)
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':
207        print("*** " + path + " does not end in .h")
208        print("*** Move it to a C source file list")
209        sys.exit(2)
210
211def assertSourceFile(path):
212    if path[-2] != '.' or (path[-1] != 'c' and path[-1] != 'S'):
213        print("*** " + path + " does not end in .c or .S")
214        print("*** Move it to a header file list")
215        sys.exit(2)
216
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
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
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#
264class Converter(object):
265
266    def convert(self, src, dst, hasSource = True, sourceFilter = None, srcContents = None):
267
268        global filesProcessed, filesProcessedCount
269
270        if verbose(verboseDebug):
271            print("convert: filter:%s: %s -> %s" % \
272                  (['yes', 'no'][sourceFilter is None], src, dst))
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        #
278        if srcContents is None:
279            if not os.path.exists(src):
280                if hasSource:
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            #
289            srcContents = readFile(src)
290
291        if os.path.exists(dst):
292            dstContents = readFile(dst)
293        else:
294            print('warning: no destination: %s' % (dst))
295            dstContents = ''
296
297        #
298        # Filter the source.
299        #
300        if sourceFilter is not None:
301            srcContents = sourceFilter(srcContents)
302
303        #
304        # Split into a list of lines.
305        #
306        srcLines = srcContents.split(os.linesep)
307        dstLines = dstContents.split(os.linesep)
308
309        if verbose(verboseDebug):
310            print('Unified diff: %s (lines:%d)' % (src, len(srcLines)))
311
312        #
313        # Diff, note there is no line termination on each string.
314        #
315        diff = diffSource(dstLines, srcLines, src, dst)
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:
331                    writeFile(dst, srcContents)
332            else:
333                print("diff -u %s %s" % (src, dst))
334                for l in diff:
335                    print(l)
336
337class NoConverter(Converter):
338    def convert(self, src, dst, hasSource = True, sourceFilter = None):
339        return '/* EMPTY */\n'
340
341class FromFreeBSDToRTEMSHeaderConverter(Converter):
342    def sourceFilter(self, data):
343        data = fixLocalIncludes(data)
344        data = fixIncludes(data)
345        return data
346
347    def convert(self, src, dst):
348        sconverter = super(FromFreeBSDToRTEMSHeaderConverter, self)
349        sconverter.convert(src, dst, sourceFilter = self.sourceFilter)
350
351class FromFreeBSDToRTEMSUserSpaceHeaderConverter(Converter):
352    def sourceFilter(self, data):
353        data = fixIncludes(data)
354        return data
355
356    def convert(self, src, dst):
357        sconverter = super(FromFreeBSDToRTEMSUserSpaceHeaderConverter, self)
358        sconverter.convert(src, dst, sourceFilter = self.sourceFilter)
359
360class FromFreeBSDToRTEMSSourceConverter(Converter):
361    def sourceFilter(self, data):
362        data = fixLocalIncludes(data)
363        data = fixIncludes(data)
364        data = '#include <machine/rtems-bsd-kernel-space.h>\n\n' + data
365        return data
366
367    def convert(self, src, dst):
368        sconverter = super(FromFreeBSDToRTEMSSourceConverter, self)
369        sconverter.convert(src, dst, sourceFilter = self.sourceFilter)
370
371class FromFreeBSDToRTEMSUserSpaceSourceConverter(Converter):
372    def sourceFilter(self, data):
373        data = fixIncludes(data)
374        data = '#include <machine/rtems-bsd-user-space.h>\n\n' + data
375        return data
376
377    def convert(self, src, dst):
378        sconverter = super(FromFreeBSDToRTEMSUserSpaceSourceConverter, self)
379        sconverter.convert(src, dst, sourceFilter = self.sourceFilter)
380
381class FromRTEMSToFreeBSDHeaderConverter(Converter):
382    def sourceFilter(self, data):
383        data = revertFixLocalIncludes(data)
384        data = revertFixIncludes(data)
385        return data
386
387    def convert(self, src, dst):
388        sconverter = super(FromRTEMSToFreeBSDHeaderConverter, self)
389        sconverter.convert(src, dst, hasSource = False,  sourceFilter = self.sourceFilter)
390
391class FromRTEMSToFreeBSDSourceConverter(Converter):
392    def sourceFilter(self, data):
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)
395        data = revertFixLocalIncludes(data)
396        data = revertFixIncludes(data)
397        return data
398
399    def convert(self, src, dst):
400        sconverter = super(FromRTEMSToFreeBSDSourceConverter, self)
401        sconverter.convert(src, dst, hasSource = False, sourceFilter = self.sourceFilter)
402
403#
404# Compose a path based for the various parts of the source tree.
405#
406class PathComposer(object):
407    def composeOriginPath(self, path):
408        return path
409
410    def composeLibBSDPath(self, path, prefix):
411        return os.path.join(prefix, path)
412
413class FreeBSDPathComposer(PathComposer):
414    def composeOriginPath(self, path):
415        return os.path.join(FreeBSD_DIR, path)
416
417    def composeLibBSDPath(self, path, prefix):
418        return os.path.join(prefix, 'freebsd', path)
419
420class RTEMSPathComposer(PathComposer):
421    def composeOriginPath(self, path):
422        return path
423
424    def composeLibBSDPath(self, path, prefix):
425        return os.path.join(prefix, 'rtemsbsd', path)
426
427class LinuxPathComposer(PathComposer):
428    def composeOriginPath(self, path):
429        return path
430
431    def composeLibBSDPath(self, path, prefix):
432        return os.path.join(prefix, 'linux', path)
433
434class CPUDependentFreeBSDPathComposer(FreeBSDPathComposer):
435    def composeLibBSDPath(self, path, prefix):
436        path = super(CPUDependentFreeBSDPathComposer, self).composeLibBSDPath(path, prefix)
437        path = mapCPUDependentPath(path)
438        return path
439
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
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
452class TargetSourceCPUDependentPathComposer(CPUDependentFreeBSDPathComposer):
453    def __init__(self, targetCPU, sourceCPU):
454        self.targetCPU = targetCPU
455        self.sourceCPU = sourceCPU
456
457    def composeLibBSDPath(self, path, prefix):
458        path = super(TargetSourceCPUDependentPathComposer, self).composeLibBSDPath(path, prefix)
459        path = path.replace(self.sourceCPU, self.targetCPU)
460        return path
461
462class BuildSystemFragmentComposer(object):
463    def __init__(self, includes = None):
464        if type(includes) is not list:
465            self.includes = [includes]
466        else:
467            self.includes = includes
468
469    def compose(self, path):
470        return ''
471
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
505    def __init__(self, testName, fileFragments, runTest = True, netTest = False, extraLibs = []):
506        self.testName = testName
507        self.fileFragments = fileFragments
508        self.runTest = runTest
509        self.netTest = netTest
510        self.extraLibs = extraLibs
511
512    def compose(self, path):
513        return ['tests', self.testName, ('default', None)], { 'files': self.fileFragments,
514                                                              'run': self.runTest,
515                                                              'net': self.netTest,
516                                                              'libs': self.extraLibs}
517
518class TestIfHeaderComposer(TestFragementComposer):
519
520    def __init__(self, testName, headers, fileFragments, runTest = True, netTest = False, extraLibs = []):
521        if headers is not list:
522            headers = [headers]
523        self.headers = headers
524        super(TestIfHeaderComposer, self).__init__(testName, fileFragments,
525                                                   runTest = runTest, netTest = netTest,
526                                                   extraLibs = extraLibs)
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
556    def __init__(self, sym, dep, cflags = None, includes = None, build = True):
557        self.sym = sym
558        self.dep = dep
559        self.cflags, self.includes = _cflagsIncludes(cflags, includes)
560        self.build = build
561
562    def compose(self, path):
563        d = { 'file': path,
564              'sym': self.sym,
565              'dep': self.dep,
566              'build': self.build }
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
575    def __init__(self, sym, header, cflags = None, includes = None, build = True):
576        self.sym = sym
577        self.header = header
578        self.cflags, self.includes = _cflagsIncludes(cflags, includes)
579        self.build = build
580
581    def compose(self, path):
582        d = { 'file': path,
583              'sym': self.sym,
584              'header': self.header,
585              'build': self.build }
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
592#
593# File - a file in the source we move backwards and forwards.
594#
595class File(object):
596    def __init__(self, path, pathComposer,
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__))
603        self.path = path
604        self.pathComposer = pathComposer
605        self.originPath = self.pathComposer.composeOriginPath(self.path)
606        self.libbsdPath = self.pathComposer.composeLibBSDPath(self.path, LIBBSD_DIR)
607        self.forwardConverter = forwardConverter
608        self.reverseConverter = reverseConverter
609        self.buildSystemComposer = buildSystemComposer
610
611    def processSource(self, forward):
612        if forward:
613            if verbose(verboseDetail):
614                print("process source: %s => %s" % (self.originPath, self.libbsdPath))
615            self.forwardConverter.convert(self.originPath, self.libbsdPath)
616        else:
617            if verbose(verboseDetail):
618                print("process source: %s => %s converter:%s" % \
619                      (self.libbsdPath, self.originPath, self.reverseConverter.__class__.__name__))
620            self.reverseConverter.convert(self.libbsdPath, self.originPath)
621
622    def getFragment(self):
623        return self.buildSystemComposer.compose(self.pathComposer.composeLibBSDPath(self.path, ''))
624
625#
626# Module - logical group of related files we can perform actions on
627#
628class Module(object):
629    def __init__(self, manager, name, enabled = True):
630        self.manager = manager
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
641    def processSource(self, direction):
642        if verbose(verboseDetail):
643            print("process module: %s" % (self.name))
644        for f in self.files:
645            f.processSource(direction)
646        for cpu, files in self.cpuDependentSourceFiles.items():
647            for f in files:
648                f.processSource(direction)
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,
661                 pathComposer, forwardConverter, reverseConverter,
662                 assertFile, buildSystemComposer = BuildSystemFragmentComposer()):
663        files = []
664        for newFile in newFiles:
665            assertFile(newFile)
666            files += [File(newFile, pathComposer, forwardConverter,
667                           reverseConverter, buildSystemComposer)]
668        return files
669
670    def addKernelSpaceHeaderFiles(self, files):
671        self.files += self.addFiles(files,
672                                    FreeBSDPathComposer(), FromFreeBSDToRTEMSHeaderConverter(),
673                                    FromRTEMSToFreeBSDHeaderConverter(), assertHeaderOrSourceFile)
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
684    def addLinuxHeaderFiles(self, files):
685        self.files += self.addFiles(files,
686                                    PathComposer(), NoConverter(),
687                                    NoConverter(), assertHeaderFile)
688
689    def addCPUDependentFreeBSDHeaderFiles(self, files):
690        self.files += self.addFiles(files,
691                                    CPUDependentFreeBSDPathComposer(), FromFreeBSDToRTEMSHeaderConverter(),
692                                    FromRTEMSToFreeBSDHeaderConverter(), assertHeaderFile)
693
694    def addCPUDependentLinuxHeaderFiles(self, files):
695        self.files += self.addFiles(files,
696                                    CPUDependentLinuxPathComposer(), NoConverter(),
697                                    NoConverter(), assertHeaderFile)
698
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
729    def addLinuxSourceFiles(self, files, sourceFileFragmentComposer):
730        self.files += self.addFiles(files,
731                                    PathComposer(), NoConverter(),
732                                    NoConverter(), assertSourceFile,
733                                    sourceFileFragmentComposer)
734
735    def addCPUDependentFreeBSDSourceFiles(self, cpus, files, sourceFileFragmentComposer):
736        for cpu in cpus:
737            self.initCPUDependencies(cpu)
738            self.cpuDependentSourceFiles[cpu] += \
739                self.addFiles(files,
740                              CPUDependentFreeBSDPathComposer(), FromFreeBSDToRTEMSSourceConverter(),
741                              FromRTEMSToFreeBSDSourceConverter(), assertSourceFile,
742                              sourceFileFragmentComposer)
743
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
753    def addCPUDependentLinuxSourceFiles(self, cpus, files, sourceFileFragmentComposer):
754        for cpu in cpus:
755            self.initCPUDependencies(cpu)
756            self.cpuDependentSourceFiles[cpu] += \
757                self.addFiles(files,
758                              CPUDependentLinuxPathComposer(), NoConverter(),
759                              NoConverter(), assertSourceFile,
760                              sourceFileFragmentComposer)
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
770#
771# Manager - a collection of modules.
772#
773class ModuleManager(object):
774    def __init__(self):
775        self.modules = {}
776        self.generator = {}
777        self.configuration = {}
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
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 []
794
795    def addModule(self, module):
796        self.modules[module.name] = module
797
798    def processSource(self, direction):
799        if verbose(verboseDetail):
800            print("process modules:")
801        for m in sorted(self.modules):
802            self.modules[m].processSource(direction)
803
804    def setConfiguration(self, config):
805        self.configuration = copy.deepcopy(config)
806
807    def getConfiguration(self):
808        return self.configuration
809
810    def updateConfiguration(self, config):
811        self.configuration.update(config)
812
813    def setModuleConfigiuration(self):
814        mods = sorted(self.modules.keys())
815        self.configuration['modules'] = mods
816        # Enabled modules are overwritten by config file. Default to all.
817        self.configuration['modules-enabled'] = mods
818
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:
825            self.modules[m].generate()
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.