aboutsummaryrefslogtreecommitdiffstats
path: root/SConstruct
blob: c41e41ef19bafbe2aba3bb5e7ea0f31d2097951e (plain) (blame)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
Help("""
=============================================
liblitmus --- The LITMUS^RT Userspace Library

There are a number of user-configurable build
variables. These can either be set on the
command line (e.g., scons ARCH=x86) or read
from a local configuration file (.config).

Run 'scons --dump-config' to see the final
build configuration.

""")

import os
(ostype, _, _, _, arch) = os.uname()

# sanity check
if ostype != 'Linux':
    print 'Error: Building liblitmus is only supported on Linux.'
    Exit(1)


# #####################################################################
# Internal configuration.
DEBUG_FLAGS  = '-Wall -g -Wdeclaration-after-statement'
API_FLAGS    = '-D_XOPEN_SOURCE=600 -D_GNU_SOURCE'
X86_32_FLAGS = '-m32'
X86_64_FLAGS = '-m64'
V9_FLAGS     = '-mcpu=v9 -m64'

SUPPORTED_ARCHS = {
    'sparc64'	: V9_FLAGS,
    'x86'	: X86_32_FLAGS,
    'x86_64'	: X86_64_FLAGS,
}

ARCH_ALIAS = {
    'i686'      : 'x86'
}

# name of the directory that has the arch headers in the Linux source
INCLUDE_ARCH = {
    'sparc64'   : 'sparc',
    'x86'       : 'x86',
    'x86_64'    : 'x86',
}

INCLUDE_DIRS = [
    # library headers
    'include/',
    # Linux kernel headers
    '${LITMUS_KERNEL}/include/',
    # Linux architecture-specific kernel headers
    '$LITMUS_KERNEL/arch/${INCLUDE_ARCH}/include'
    ]

# #####################################################################
# User configuration.

vars = Variables('.config', ARGUMENTS)

vars.AddVariables(
    PathVariable('LITMUS_KERNEL',
                 'Where to find the LITMUS^RT kernel.',
                 '../litmus2010'),

    EnumVariable('ARCH',
                 'Target architecture.',
                 arch,
                 SUPPORTED_ARCHS.keys() + ARCH_ALIAS.keys()),
)

AddOption('--dump-config',
          dest='dump',
          action='store_true',
          default=False,
          help="dump the build configuration and exit")

# #####################################################################
# Build configuration.

env  = Environment(variables = vars)

# Check what we are building for.
arch = env['ARCH']

# replace if the arch has an alternative name
if arch in ARCH_ALIAS:
    arch = ARCH_ALIAS[arch]
    env['ARCH'] = arch

# Get include directory for arch.
env['INCLUDE_ARCH'] = INCLUDE_ARCH[arch]

arch_flags = Split(SUPPORTED_ARCHS[arch])
dbg_flags  = Split(DEBUG_FLAGS)
api_flags  = Split(API_FLAGS)

# Set up environment
env.Replace(
    CC = 'gcc',
    CPPPATH = INCLUDE_DIRS,
    CCFLAGS = dbg_flags + api_flags + arch_flags,
    LINKFLAGS = arch_flags,
)

def dump_config(env):
    def dump(key):
        print "%15s = %s" % (key, env.subst("${%s}" % key))

    dump('ARCH')
    dump('LITMUS_KERNEL')
    dump('CPPPATH')
    dump('CCFLAGS')
    dump('LINKFLAGS')

if GetOption('dump'):
    print "\n"
    print "Build Configuration:"
    dump_config(env)
    print "\n"
    Exit(0)

# #####################################################################
# Build checks.

def CheckSyscallNr(context):
    context.Message('Checking for LITMUS^RT syscall numbers... ')
    nrSrc = """
#include <linux/unistd.h>
int main(int argc, char **argv)
{
  return __NR_set_rt_task_param;
}
"""
    result = context.TryLink(nrSrc, '.c')
    context.Result(result)
    return result


def abort(msg, help=None):
    print "Error: %s" % env.subst(msg)
    print "-" * 80
    print "This is the build configuration in use:"
    dump_config(env)
    if help:
        print "-" * 80
        print env.subst(help)
    print "\n"
    Exit(1)

# Check compile environment
if not (env.GetOption('clean') or env.GetOption('help')):
    print env.subst('Building ${ARCH} binaries.')
    # Check for kernel headers.
    conf = Configure(env, custom_tests = {'CheckSyscallNr' : CheckSyscallNr})

    conf.CheckCHeader('linux/unistd.h') or \
        abort("Cannot find kernel headers in '$LITMUS_KERNEL'",
              "Please ensure that LITMUS_KERNEL in .config is set to a valid path.")

    conf.CheckCHeader('litmus/rt_param.h') or \
        abort("Cannot find LITMUS^RT headers in '$LITMUS_KERNEL'",
              "Please ensure sure that the kernel in '$LITMUS_KERNEL'"
              " is a LITMUS^RT kernel.")

    conf.CheckSyscallNr() or \
        abort("The LITMUS^RT syscall numbers are not available.",
              "Please ensure sure that the kernel in '$LITMUS_KERNEL'"
              " is a LITMUS^RT kernel.")

    env = conf.Finish()

# #####################################################################
# Derived environments

# link with liblitmus
rt = env.Clone(
    LIBS     = Split('litmus rt'),
    LIBPATH  = '.'
)
rt.Append(LINKFLAGS = '-static')


# link with math lib
rtm = rt.Clone()
rtm.Append(LIBS = ['m'])

# multithreaded real-time tasks
mtrt = rt.Clone()
mtrt.Append(LINKFLAGS = '-pthread')

# #####################################################################
# Targets: liblitmus
# All the files in src/ are part of the library.
env.Library('litmus',
            ['src/kernel_iface.c', 'src/litmus.c',
             'src/syscalls.c', 'src/task.c'])

# #####################################################################
# Targets: simple tools that do not depend on liblitmus
env.Program('cycles', 'bin/cycles.c')

# #####################################################################
# Targets: tools that depend on liblitmus
rt.Program('base_task', 'bin/base_task.c')
mtrt.Program('base_mt_task', 'bin/base_mt_task.c')
rt.Program('rt_launch', ['bin/rt_launch.c', 'bin/common.c'])
rt.Program('rtspin', ['bin/rtspin.c', 'bin/common.c'])
rt.Program('release_ts', 'bin/release_ts.c')
rtm.Program('measure_syscall', 'bin/null_call.c')


# #####################################################################
# Test suite.

mkc = Builder(action = 'tests/make_catalog.py $SOURCES > $TARGET')
test = mtrt.Clone()
test.Append(BUILDERS = {'TestCatalog' : mkc})
test.Append(CPPPATH = ['tests/'])

catalog = test.TestCatalog('tests/__test_catalog.inc', Glob('tests/*.c'))
test.Program('runtests', Glob('tests/*.c'))

# #####################################################################
# Additional Help

Help("Build Variables\n")
Help("---------------\n")
Help(vars.GenerateHelpText(env))