summaryrefslogtreecommitdiff
path: root/wscript
blob: 453b39a8b985f0c114f9a2ab30d166220722eaa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#
# RTEMS Project (https://www.rtems.org/)
#
# Copyright (c) 2015-2016 Chris Johns <chrisj@rtems.org>. All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#
# RTEMS LibBSD is a transparent source build of the FreeBSD kernel source for RTEMS.
#
# To use see README.waf shipped with this file.
#

from __future__ import print_function

rtems_version = "5"

try:
    import rtems_waf.rtems as rtems
except:
    print("error: no rtems_waf git submodule; see README.waf")
    import sys
    sys.exit(1)

#import libbsd_waf
import libbsd
import waf_libbsd
import os.path
import runpy
import sys
try:
    import configparser
except ImportError:
    import ConfigParser as configparser
import waflib.Options

builders = {}

BUILDSET_DIR = "buildset"
BUILDSET_DEFAULT = "buildset/base.ini"

def load_ini(conf, f):
    ini = configparser.ConfigParser()
    ini.read(f)
    if not ini.has_section('general'):
        conf.fatal("'{}' is missing a general section.".format(f))
    if not ini.has_option('general', 'name'):
        conf.fatal("'{}' is missing a general/name.".format(f))
    if ini.has_option('general', 'extends'):
        extends = ini.get('general', 'extends')
        extendfile = None
        if os.path.isfile(extends):
            extendfile = extends
        elif os.path.isfile(os.path.join(BUILDSET_DIR, extends)):
            extendfile = os.path.join(BUILDSET_DIR, extends)
        else:
            conf.fatal("'{}': Invalid file given for general/extends:'{}'"
                .format(f, extends))
        base = load_ini(conf, extendfile)
        for s in ini.sections():
            if not base.has_section(s):
                base.add_section(s)
            for o in ini.options(s):
                val = ini.get(s, o)
                base.set(s, o, val)
        ini = base
    return ini

def load_config(conf, f):
    ini = load_ini(conf, f)
    config = {}

    config['name'] = ini.get('general', 'name')

    config['modules-enabled'] = []
    mods = []
    if ini.has_section('modules-enabled'):
        mods = ini.options('modules-enabled')
    for mod in mods:
        if ini.getboolean('modules-enabled', mod):
            config['modules-enabled'].append(mod)
    return config

def check_buildsets(ctx, buildset_opt):
    buildsets = []
    if buildset_opt == []:
        buildset_opt.append(BUILDSET_DEFAULT)
    for bs in buildset_opt:
        if os.path.isdir(bs):
            for f in os.listdir(bs):
                if f[-4:] == ".ini":
                    buildsets += [os.path.join(bs,f)]
        else:
            for f in bs.split(','):
                buildsets += [f]
    return buildsets

def post_init(ctx, env, context_classes):
    buildsets = check_buildsets(ctx, env.options['buildset'])
    bsnames = []

    global builders

    for bs in buildsets:
        # create builder objects
        builder = waf_libbsd.Builder()
        libbsd.load(builder)
        bsconfig = load_config(ctx, bs)
        bsname = bsconfig['name']
        bsnames += [bsname]
        builder.updateConfiguration(bsconfig)
        builder.generate(rtems_version)
        builders[bsname]=builder

        # Update the contextes for build variants
        for y in context_classes:
            newcmd = y.cmd + '-' + bsname
            newvariant = y.variant + '-' + bsname
            class context(y):
                cmd = newcmd
                variant = newvariant
                libbsd_buildset_name = bsname

    # Transform the commands to per build variant commands
    commands = []
    for cmd in waflib.Options.commands:
        if cmd.startswith(('build', 'clean', 'install')):
            for bsname in bsnames:
                commands += [cmd + '-' + bsname]
        else:
            commands += [cmd]
    waflib.Options.commands = commands

def init(ctx):
    rtems.init(ctx, version = rtems_version, long_commands = True,
               post_init = post_init)

def options(opt):
    rtems.options(opt)
    opt.add_option("--enable-auto-regen",
                   action = "store_true",
                   default = False,
                   dest = "auto_regen",
                   help = "Enable auto-regeneration of LEX, RPC and YACC files.")
    opt.add_option("--enable-warnings",
                   action = "store_true",
                   default = False,
                   dest = "warnings",
                   help = "Enable all warnings. The default is quiet builds.")
    opt.add_option("--net-test-config",
                   default = "config.inc",
                   dest = "net_config",
                   help = "Network test configuration.")
    opt.add_option("--freebsd-options",
                   action = "store",
                   default = "",
                   dest = "freebsd_options",
                   help = "Set FreeBSD options (developer option).")
    opt.add_option("--optimization",
                   action = "store",
                   default = "2",
                   dest = "optimization",
                   help = "Set optimization level to OPTIMIZATION (-On compiler flag). Default is 2 (-O2).")
    opt.add_option("--buildset",
                   action = "append",
                   default = [],
                   dest = "buildset",
                   help = "Select build sets to build. If set to a directory, all .py file in this directory will be used.")

def bsp_configure(conf, arch_bsp):
    conf.check(header_name = "dlfcn.h", features = "c")
    conf.check(header_name = "rtems/pci.h", features = "c", mandatory = False)
    if not rtems.check_posix(conf):
        conf.fatal("RTEMS kernel POSIX support is disabled; configure RTEMS with --enable-posix")
    if rtems.check_networking(conf):
        conf.fatal("RTEMS kernel contains the old network support; configure RTEMS with --disable-networking")
    env = conf.env.derive()
    for builder in builders:
        ab = conf.env.RTEMS_ARCH_BSP
        variant = ab + "-" + builder
        conf.msg('Configure variant: ', variant)
        conf.setenv(variant, env)
        builders[builder].bsp_configure(conf, arch_bsp)
        conf.setenv(ab)

def configure(conf):
    if conf.options.auto_regen:
        conf.find_program("lex", mandatory = True)
        conf.find_program("rpcgen", mandatory = True)
        conf.find_program("yacc", mandatory = True)
    conf.env.AUTO_REGEN = conf.options.auto_regen
    conf.env.WARNINGS = conf.options.warnings
    conf.env.NET_CONFIG = conf.options.net_config
    conf.env.FREEBSD_OPTIONS =conf.options.freebsd_options
    conf.env.OPTIMIZATION = conf.options.optimization
    rtems.configure(conf, bsp_configure)

def build(bld):
    rtems.build(bld)
    builders[bld.libbsd_buildset_name].build(bld)