source: rtems-libbsd/waf_generator.py @ 8b10210

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 8b10210 was e9aa9537, checked in by Chris Johns <chrisj@…>, on 05/20/15 at 11:59:15

waf: Add warnings and auto-regen options.

  • Property mode set to 100755
File size: 17.8 KB
Line 
1#
2#  Copyright (c) 2015 Chris Johns <chrisj@rtems.org>. All rights reserved.
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
35import os
36import tempfile
37
38import builder
39
40trace = False
41
42data = { }
43
44def _add_files(name, files):
45    if type(files) is not list:
46        files = [files]
47    if name not in data:
48        data[name] = []
49    data[name] += files
50
51class SourceFileFragmentComposer(builder.BuildSystemFragmentComposer):
52
53    def __init__(self, cflags = "default"):
54        self.cflags = cflags
55
56    def compose(self, path):
57        return ['sources', self.cflags], [path]
58
59class TestFragementComposer(builder.BuildSystemFragmentComposer):
60
61    def __init__(self, testName, fileFragments, runTest = True, netTest = False):
62        self.testName = testName
63        self.fileFragments = fileFragments
64        self.runTest = runTest
65        self.netTest = netTest
66
67    def compose(self, path):
68        return ['tests', self.testName], { 'files': self.fileFragments,
69                                           'run': self.runTest,
70                                           'net': self.netTest }
71
72class KVMSymbolsFragmentComposer(builder.BuildSystemFragmentComposer):
73
74    def compose(self, path):
75        return ['KVMSymbols', 'files'], [path]
76
77class RPCGENFragmentComposer(builder.BuildSystemFragmentComposer):
78
79    def compose(self, path):
80        return ['RPCGen', 'files'], [path]
81
82class RouteKeywordsFragmentComposer(builder.BuildSystemFragmentComposer):
83
84    def compose(self, path):
85        return ['RouteKeywords', 'files'], [path]
86
87class LexFragmentComposer(builder.BuildSystemFragmentComposer):
88
89    def __init__(self, sym, dep):
90        self.sym = sym
91        self.dep = dep
92
93    def compose(self, path):
94        return ['lex', path], { 'file': path,
95                                'sym': self.sym,
96                                'dep': self.dep }
97
98class YaccFragmentComposer(builder.BuildSystemFragmentComposer):
99
100    def __init__(self, sym, header):
101        self.sym = sym
102        self.header = header
103
104    def compose(self, path):
105        return ['yacc', path], { 'file': path,
106                                 'sym': self.sym,
107                                 'header': self.header }
108
109# Module Manager - Collection of Modules
110class ModuleManager(builder.ModuleManager):
111
112    def restart(self):
113        self.script = ''
114
115    def add(self, line = ''):
116        self.script += line + os.linesep
117
118    def write(self):
119        try:
120            out = tempfile.NamedTemporaryFile(delete = False)
121            out.write(self.script)
122            out.close()
123            wscript = builder.RTEMS_DIR + '/wscript'
124            builder.processIfDifferent(out.name, wscript, "wscript")
125        finally:
126            try:
127                os.remove(out.name)
128            except:
129                pass
130
131    def setGenerators(self):
132        self.generator['convert'] = builder.Converter
133        self.generator['no-convert'] = builder.NoConverter
134
135        self.generator['file'] = builder.File
136
137        self.generator['path'] = builder.PathComposer
138        self.generator['freebsd-path'] = builder.FreeBSDPathComposer
139        self.generator['rtems-path'] = builder.RTEMSPathComposer
140        self.generator['cpu-path'] = builder.CPUDependentPathComposer
141        self.generator['target-src-cpu--path'] = builder.TargetSourceCPUDependentPathComposer
142
143        self.generator['source'] = SourceFileFragmentComposer
144        self.generator['test'] = TestFragementComposer
145        self.generator['kvm-symbols'] = KVMSymbolsFragmentComposer
146        self.generator['rpc-gen'] = RPCGENFragmentComposer
147        self.generator['route-keywords'] = RouteKeywordsFragmentComposer
148        self.generator['lex'] = LexFragmentComposer
149        self.generator['yacc'] = YaccFragmentComposer
150
151    def generate(self):
152
153        def _source_list(lhs, files, append = False):
154            if append:
155                adder = '+'
156                adder_space = ' '
157            else:
158                adder = ''
159                adder_space = ''
160            ll = len(lhs)
161            if len(files) == 1:
162                self.add('%s %s= [%r]' % (lhs, adder, files[0]))
163            elif len(files) == 2:
164                self.add('%s %s= [%r,' % (lhs, adder, files[0]))
165                self.add('%s %s   %r]' % (' ' * ll, adder_space, files[-1]))
166            elif len(files) > 0:
167                self.add('%s %s= [%r,' % (lhs, adder, files[0]))
168                for f in files[1:-1]:
169                    self.add('%s %s   %r,' % (' ' * ll, adder_space, f))
170                self.add('%s %s   %r]' % (' ' * ll, adder_space, files[-1]))
171
172        def _data_insert(data, cpu, frag):
173            #
174            # The default handler returns an empty string. Skip it.
175            #
176            if type(frag) is not str:
177                d = data
178                for p in frag[0]:
179                    if p not in d:
180                        d[p] = {}
181                    d = d[p]
182                if type(frag[1]) is list:
183                    if cpu not in d:
184                        d[cpu] = []
185                    d[cpu] += frag[1]
186                else:
187                    d[cpu] = frag[1]
188
189        data = { }
190
191        for mn in self.getModules():
192            m = self[mn]
193            if m.conditionalOn == "none":
194                for f in m.files:
195                    _data_insert(data, 'all', f.getFragment())
196            for cpu, files in sorted(m.cpuDependentSourceFiles.items()):
197                for f in files:
198                    _data_insert(data, cpu, f.getFragment())
199
200        if trace:
201            import pprint
202            pprint.pprint(data)
203
204        self.restart()
205
206        self.add('#')
207        self.add('# RTEMS Project (https://www.rtems.org)')
208        self.add('#')
209        self.add('# Generated waf script. Do not edit, run ./freebsd-to-rtems.py -m')
210        self.add('#')
211        self.add('# To use see README.waf shipped with this file.')
212        self.add('#')
213        self.add('')
214        self.add('try:')
215        self.add('    import rtems_waf.rtems as rtems')
216        self.add('except:')
217        self.add('    print "error: no rtems_waf git submodule; see README.waf"')
218        self.add('    import sys')
219        self.add('    sys.exit(1)')
220        self.add('')
221        self.add('def init(ctx):')
222        self.add('    rtems.init(ctx)')
223        self.add('')
224        self.add('def options(opt):')
225        self.add('    rtems.options(opt)')
226        self.add('    opt.add_option("--enable-auto-regen",')
227        self.add('                   action = "store_true",')
228        self.add('                   default = False,')
229        self.add('                   dest = "auto_regen",')
230        self.add('                   help = "Enable auto-regeneration of LEX, RPC and YACC files.")')
231        self.add('    opt.add_option("--enable-warnings",')
232        self.add('                   action = "store_true",')
233        self.add('                   default = False,')
234        self.add('                   dest = "warnings",')
235        self.add('                   help = "Enable all warnings. The default is quiet builds.")')
236        self.add('')
237        self.add('def configure(conf):')
238        self.add('    if conf.options.auto_regen:')
239        self.add('        conf.find_program("lex", mandatory = True)')
240        self.add('        conf.find_program("rpcgen", mandatory = True)')
241        self.add('        conf.find_program("yacc", mandatory = True)')
242        self.add('    conf.env.AUTO_REGEN = conf.options.auto_regen')
243        self.add('    conf.env.WARNINGS = conf.options.warnings')
244        self.add('    rtems.configure(conf)')
245        self.add('    if rtems.check_networking(conf):')
246        self.add('        conf.fatal("RTEMS kernel contains the old network support; configure RTEMS with --disable-networking")')
247        self.add('')
248        self.add('def build(bld):')
249        self.add('    rtems.build(bld)')
250        self.add('')
251        self.add('    # C/C++ flags')
252        self.add('    common_flags = []')
253        for f in builder.common_flags():
254            self.add('    common_flags += ["%s"]' % (f))
255        self.add('    if bld.env.WARNINGS:')
256        for f in builder.common_warnings():
257            self.add('        common_flags += ["%s"]' % (f))
258        self.add('    else:')
259        for f in builder.common_no_warnings():
260            self.add('        common_flags += ["%s"]' % (f))
261        self.add('    cflags = %r + common_flags' % (builder.cflags()))
262        self.add('    cxxflags = %r + common_flags' % (builder.cxxflags()))
263        self.add('')
264        self.add('    # Include paths')
265        self.add('    includes = []')
266        for i in builder.includes():
267            self.add('    includes += ["%s"]' % (i[2:]))
268        self.add('    for i in %r:' % (builder.cpu_includes()))
269        self.add('        includes += ["%s" % (i[2:].replace("@CPU@", bld.get_env()["RTEMS_ARCH"]))]')
270        self.add('')
271        self.add('    # Support dummy PIC IRQ includes')
272        self.add('    if bld.get_env()["RTEMS_ARCH"] not in ("arm", "i386", "lm32", "mips", "powerpc", "sparc", "m68k"):')
273        self.add('        includes += ["rtems-dummy-pic-irq/include"]')
274        self.add('')
275
276        self.add('    # Collect the libbsd uses')
277        self.add('    libbsd_use = []')
278        self.add('')
279
280        #
281        # Add the specific rule based builders for generating files.
282        #
283        if 'KVMSymbols' in data:
284            kvmsymbols = data['KVMSymbols']
285            self.add('    # KVM Symbols')
286            self.add('    if bld.env.AUTO_REGEN:')
287            self.add('        bld(target = "%s",' % (kvmsymbols['files']['all'][0]))
288            self.add('            source = "rtemsbsd/rtems/generate_kvm_symbols",')
289            self.add('            rule = "./${SRC} > ${TGT}")')
290            self.add('    bld.objects(target = "kvmsymbols",')
291            self.add('                features = "c",')
292            self.add('                cflags = cflags,')
293            self.add('                includes = includes,')
294            self.add('                source = "%s")' % (kvmsymbols['files']['all'][0]))
295            self.add('    libbsd_use += ["kvmsymbols"]')
296            self.add('')
297
298        if 'RPCGen' in data:
299            rpcgen = data['RPCGen']
300            rpcname = rpcgen['files']['all'][0][:-2]
301            self.add('    # RPC Generation')
302            self.add('    if bld.env.AUTO_REGEN:')
303            self.add('        bld(target = "%s.h",' % (rpcname))
304            self.add('            source = "%s.x",' % (rpcname))
305            self.add('            rule = "${RPCGEN} -h -o ${TGT} ${SRC}")')
306            self.add('')
307
308        if 'RouteKeywords' in data:
309            routekw = data['RouteKeywords']
310            rkwname = routekw['files']['all'][0]
311            self.add('    # Route keywords')
312            self.add('    if bld.env.AUTO_REGEN:')
313            self.add('        rkw_rule = "cat ${SRC} | ' + \
314                     'awk \'BEGIN { r = 0 } { if (NF == 1) ' + \
315                     'printf \\"#define\\\\tK_%%s\\\\t%%d\\\\n\\\\t{\\\\\\"%%s\\\\\\", K_%%s},\\\\n\\", ' + \
316                     'toupper($1), ++r, $1, toupper($1)}\' > ${TGT}"')
317            self.add('        bld(target = "%s.h",' % (rkwname))
318            self.add('            source = "%s",' % (rkwname))
319            self.add('            rule = rkw_rule)')
320            self.add('')
321
322        if 'lex' in data:
323            lexes = data['lex']
324            self.add('    # Lex')
325            for l in lexes:
326                lex = lexes[l]['all']
327                self.add('    if bld.env.AUTO_REGEN:')
328                self.add('        bld(target = "%s.c",' % (lex['file'][:-2]))
329                self.add('            source = "%s",' % (lex['file']))
330                self.add('            rule = "${LEX} -P %s -t ${SRC} | ' % (lex['sym']) + \
331                         'sed -e \'/YY_BUF_SIZE/s/16384/1024/\' > ${TGT}")')
332                self.add('    bld.objects(target = "lex_%s",' % (lex['sym']))
333                self.add('                features = "c",')
334                self.add('                cflags = cflags,')
335                self.add('                includes = includes,')
336                self.add('                source = "%s.c")' % (lex['file'][:-2]))
337                self.add('    libbsd_use += ["lex_%s"]' % (lex['sym']))
338                self.add('')
339
340        if 'yacc' in data:
341            yaccs = data['yacc']
342            self.add('    # Yacc')
343            for y in yaccs:
344                yacc = yaccs[y]['all']
345                yacc_file = yacc['file']
346                yacc_sym = yacc['sym']
347                yacc_header = '%s/%s' % (os.path.dirname(yacc_file), yacc['header'])
348                self.add('    if bld.env.AUTO_REGEN:')
349                self.add('        bld(target = "%s.c",' % (yacc_file[:-2]))
350                self.add('            source = "%s",' % (yacc_file))
351                self.add('            rule = "${YACC} -b %s -d -p %s ${SRC} && ' % (yacc_sym, yacc_sym) + \
352                         'sed -e \'/YY_BUF_SIZE/s/16384/1024/\' < %s.tab.c > ${TGT} && ' % (yacc_sym) + \
353                         'rm -f %s.tab.c && mv %s.tab.h %s")' % (yacc_sym, yacc_sym, yacc_header))
354                self.add('    bld.objects(target = "yacc_%s",' % (yacc_sym))
355                self.add('                features = "c",')
356                self.add('                cflags = cflags,')
357                self.add('                includes = includes,')
358                self.add('                source = "%s.c")' % (yacc_file[:-2]))
359                self.add('    libbsd_use += ["yacc_%s"]' % (yacc_sym))
360            self.add('')
361
362        #
363        # We have 'm' different sets of flags and there can be 'n' cpus
364        # specific files for those flags.
365        #
366        objs = 0
367        self.add('    # Objects built with different CFLAGS')
368        for cflags in sorted(data['sources']):
369            if cflags is not 'default':
370                objs += 1
371                _source_list('    objs%02d_source' % objs, sorted(data['sources'][cflags]['all']))
372                archs = sorted(data['sources'][cflags])
373                for arch in archs:
374                    if arch is not 'all':
375                        self.add('    if bld.get_env()["RTEMS_ARCH"] == "%s":' % arch)
376                        _source_list('        objs%02d_source' % objs,
377                                     sorted(data['sources'][cflags][arch]),
378                                     append = True)
379                defines = [d[2:] for d in cflags.split(' ')]
380                self.add('    bld.objects(target = "objs%02d",' % (objs))
381                self.add('                features = "c",')
382                self.add('                cflags = cflags,')
383                self.add('                includes = includes,')
384                self.add('                defines = %r,' % (defines))
385                self.add('                source = objs%02d_source)' % objs)
386                self.add('    libbsd_use += ["objs%02d"]' % (objs))
387                self.add('')
388
389        #
390        # We hold the 'default' cflags set of files to the end to create the
391        # static library with.
392        #
393        _source_list('    source', sorted(data['sources']['default']['all']))
394        archs = sorted(data['sources']['default'])
395        for arch in archs:
396            if arch is not 'all':
397                self.add('    if bld.get_env()["RTEMS_ARCH"] == "%s":' % arch)
398                _source_list('        source',
399                             sorted(data['sources']['default'][arch]),
400                             append = True)
401        self.add('    bld.stlib(target = "bsd",')
402        self.add('              features = "c cxx",')
403        self.add('              cflags = cflags,')
404        self.add('              cxxflags = cxxflags,')
405        self.add('              includes = includes,')
406        self.add('              source = source,')
407        self.add('              use = libbsd_use)')
408        self.add('')
409
410        self.add('    # Tests')
411        tests = data['tests']
412        for test_name in tests:
413            files = ['testsuite/%s/%s.c' % (test_name, f) for f in  data['tests'][test_name]['all']['files']]
414            _source_list('    test_%s' % (test_name), sorted(files))
415            self.add('    bld.program(target = "%s",' % (test_name))
416            self.add('                features = "cprogram",')
417            self.add('                cflags = cflags,')
418            self.add('                includes = includes,')
419            self.add('                source = test_%s,' % (test_name))
420            self.add('                use = ["bsd"],')
421            self.add('                lib = ["m", "z"])')
422            self.add('')
423
424        self.write()
Note: See TracBrowser for help on using the repository browser.