-
Notifications
You must be signed in to change notification settings - Fork 5
/
build_rtems.sh
executable file
·345 lines (294 loc) · 8.76 KB
/
build_rtems.sh
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/bash
#
# Copyright (C) 2015 Pavel Kirienko <[email protected]>
# Distributed under the terms of the MIT license.
#
# RTEMS building script.
#
# Resources:
# http://ftp.rtems.org/pub/rtems/people/chrisj/source-builder/source-builder.html
# http://alanstechnotes.blogspot.ru/2013/03/setting-up-rtems-development.html
# http://s937484.blogspot.ru/2013/10/rtems-stm32f407-discovery-board-posted.html
#
STARTED_AT=$(date +%s)
TOPDIR=$(readlink -f $(dirname $0))
function fatal()
{
tput setaf 1
tput bold
echo "FATAL ERROR: $@"
tput sgr0
exit 1
}
function echogreen()
{
tput setaf 2
tput bold
echo "$@"
tput sgr0
}
function echoyellow()
{
tput setaf 3
tput bold
echo "$@"
tput sgr0
}
function echoblue()
{
tput setaf 4
tput bold
echo "$@"
tput sgr0
}
function usage()
{
echo "Invalid usage. Supported options:"
cat $0 | sed -n 's/^\s*--\([^)\*]*\).*)/\1/p' # Don't try this at home.
exit 1
}
#
# Parsing the arguments. Read this section for usage info.
#
CPU=
BSP=
OVERWRITE_BSP_DIR=
AMEND_BSP_DIR=
REMOVE_UNUSED_BSP=0
for i in "$@"; do
case $i in
--cpu=*)
CPU="${i#*=}"
;;
--bsp=*)
BSP="${i#*=}"
;;
--overwrite-bsp-dir=*)
OVERWRITE_BSP_DIR="${i#*=}"
;;
--amend-bsp-dir=*)
AMEND_BSP_DIR="${i#*=}"
;;
--remove-unused-bsp)
REMOVE_UNUSED_BSP=1
;;
*)
usage
;;
esac
shift
done
echoblue "CPU: $CPU"
echoblue "BSP: $BSP"
echoblue "OVERWRITE_BSP_DIR: $OVERWRITE_BSP_DIR"
echoblue "AMEND_BSP_DIR: $AMEND_BSP_DIR"
echoblue "REMOVE_UNUSED_BSP: $REMOVE_UNUSED_BSP"
echoblue "RTEMS_CONFIGURE_EXTRA_OPTIONS: $RTEMS_CONFIGURE_EXTRA_OPTIONS"
echoblue "RTEMS_RSB_EXTRA_OPTIONS: $RTEMS_RSB_EXTRA_OPTIONS"
[ -z "$CPU" ] && usage
[ -z "$BSP" ] && usage
#
# Entire process assumes that the script directory is the current directory.
#
echo "Working directory: $TOPDIR"
cd $TOPDIR || fatal "Can't cd to working directory"
#
# Initializing the submodules
#
git submodule update --init --recursive || fatal "Can't update submodules"
cd $TOPDIR
#
# Guessing the host system configuration
#
if [ -z "$PARALLEL" ]; then
export PARALLEL=$(grep -c ^processor /proc/cpuinfo)
if [ -z "$PARALLEL" ]; then
echo "Couldn't detect the number of processors, are we not on Linux?"
export PARALLEL=4
fi
echo "Parallel jobs: $PARALLEL"
fi
#
# Building the toolchain if necessary
#
TOOLCHAIN_DIR=$TOPDIR/toolchain
function does_toolchain_exist()
{
ls $TOOLCHAIN_DIR/bin/*gcc > /dev/null
}
if ! does_toolchain_exist; then
echoblue "Toolchain not found, forcing rebuild"
rm -rf $TOOLCHAIN_DIR &> /dev/null
#
# Checking the environment
#
SB_LOG_FILE=$TOPDIR/rtems-source-builder.log
function run_sb_check()
{
result=$($TOPDIR/rtems-source-builder/source-builder/sb-check --log=$SB_LOG_FILE 2>&1)
status=$?
echo "$result"
[[ $status != 0 || $result =~ "error" || $result =~ "Environment is not correctly set up" ]] && return 1
return 0
}
if ! run_sb_check; then
echo "RTEMS SB check failed"
read -p "Do you want me to install dependencies and retry? [y/N] " answer
if [[ $answer =~ (y|Y) ]]; then
if python -mplatform | grep -q Ubuntu; then
sudo apt-get build-dep binutils gcc g++ gdb unzip git python2.7-dev
else
fatal "Can't help with your distribution, sorry"
fi
else
fatal "Please fix it manually then"
fi
if ! run_sb_check; then
fatal "Sorry, I couldn't fix it."
fi
fi
echogreen "RTEMS SB check OK"
#
# Building the toolchain
#
cd $TOPDIR/rtems-source-builder/rtems
echo "Available build sets:"
../source-builder/sb-set-builder --log=$SB_LOG_FILE \
--list-bsets \
|| fatal "Can't list build sets"
set -vx
../source-builder/sb-set-builder --log=$SB_LOG_FILE \
--prefix=$TOOLCHAIN_DIR \
--macros=$TOPDIR/rsb_macros.mc \
$RTEMS_RSB_EXTRA_OPTIONS \
4.11/rtems-$CPU \
|| fatal "RSB build failed"
set +vx
echogreen "RTEMS SB succeeded"
else
echoblue "Toolchain appears to be built. To rebuild, remove $TOOLCHAIN_DIR"
fi
does_toolchain_exist || fatal "Toolchain not found"
#
# Environment configuration file
#
echoblue "Generating the environment configuration file..."
cd $TOPDIR
ENV_FILE=env.sh
rm -f $ENV_FILE # It's write protected, so explicit rm is needed
cat << EOF > $ENV_FILE
#
# This file must be sourced in order for the build system to work.
# Autogenerated by $0 at $(date). Do not edit.
#
export RTEMS_BSP=$BSP
# Toolchain path
export PATH=$TOOLCHAIN_DIR/bin:\$PATH
# Needed for applications
export RTEMS_MAKEFILE_PATH=$TOPDIR/bsps/$CPU-rtems4.11/\$RTEMS_BSP
EOF
chmod 444 $ENV_FILE # Protection against accidental changes
source $ENV_FILE || fatal "Can't source env file"
$CPU-rtems4.11-gcc -v || fatal "Toolchain test failed"
echogreen "Environment configuration file '$ENV_FILE' has been generated and sourced"
#
# Helper scripts
#
#cat << EOF > waf_configure.sh
#waf configure --rtems=$TOPDIR/bsps --rtems-tools=$TOOLCHAIN_DIR --rtems-bsps=$CPU/$BSP
#EOF
#chmod +x waf_configure.sh
#
# Copying the external BSP if specified, checking its validness
#
if [ -n "$OVERWRITE_BSP_DIR" ]; then
bsp=$(basename $OVERWRITE_BSP_DIR)
echoblue "Overwriting BSP $bsp"
OVERWRITE_BSP_DIR_DST=$TOPDIR/rtems/c/src/lib/libbsp/$CPU/$bsp
rm -rf $OVERWRITE_BSP_DIR_DST
cp -vr $OVERWRITE_BSP_DIR $OVERWRITE_BSP_DIR_DST || fatal "Can't overwrite BSP"
fi
if [ -n "$AMEND_BSP_DIR" ]; then
echoblue "Amending BSP"
\cp -vrf $AMEND_BSP_DIR $TOPDIR/rtems/c/src/lib/libbsp/$CPU || fatal "Can't amend BSP"
fi
BSP_CUSTOM_FILE=($TOPDIR/rtems/c/src/lib/libbsp/$CPU/*/make/custom/$BSP.cfg)
[[ ${#BSP_CUSTOM_FILE[@]} > 1 ]] && \
fatal "Conflicting BSP customization files found: ${BSP_CUSTOM_FILE[@]}"
[ -f "$BSP_CUSTOM_FILE" ] || fatal "BSP customization file not found"
BSP_DIR=$(readlink -f $(dirname $BSP_CUSTOM_FILE)/../..)
echo "BSP custom file: $BSP_CUSTOM_FILE"
echo "BSP directory: $BSP_DIR"
echogreen "BSP check OK"
#
# Purging BSP we don't need - the radical way of making build go faster
#
if [[ $REMOVE_UNUSED_BSP != 0 ]]; then
echoblue "Removing unused BSP..."
#
# CPU cleanup
#
for path in $TOPDIR/rtems/c/src/lib/libbsp/* \
$TOPDIR/rtems/c/src/lib/libcpu/*
do
[ -d "${path}" ] || continue # Not a directory
cpu="$(basename ${path})"
[[ "$cpu" == "$CPU" \
|| "$cpu" == "shared" \
|| "$cpu" == *.* \
]] && continue
echo "Removing CPU $cpu"
rm -rf ${path}
done
#
# BSP cleanup
#
for path in $TOPDIR/rtems/c/src/lib/libbsp/$CPU/*; do
[ -d "${path}" ] || continue
bsp_dir="$(basename ${path})"
[[ "$bsp_dir" == "$(basename $BSP_DIR)" \
|| "$bsp_dir" == "shared" \
|| "$bsp_dir" == *.* \
]] && continue
echo "Removing BSP $bsp_dir"
rm -rf ${path}
done
fi
#
# Printing BSP configuration options
#
echo "Configuration options for BSP $BSP:"
cat $BSP_DIR/configure.ac | grep RTEMS_BSPOPTS_SET
#
# Building RTEMS
#
echoblue "Bootstrapping..."
cd $TOPDIR/rtems || fatal "Can't cd to rtems"
./bootstrap || fatal "Failed to bootstrap"
echoblue "Configuring..."
echo "Extra options: $RTEMS_CONFIGURE_EXTRA_OPTIONS"
cd $TOPDIR
rm -rf rtems-build &> /dev/null
rm -rf $TOPDIR/bsps &> /dev/null
mkdir rtems-build
cd rtems-build || fatal "Can't cd to rtems-build"
set -vx
../rtems/configure --target=$CPU-rtems4.11 \
--prefix=$TOPDIR/bsps \
--enable-tests=samples \
--enable-rtemsbsp=$BSP \
--enable-posix \
--enable-languages=c,c++ \
$RTEMS_CONFIGURE_EXTRA_OPTIONS
set +vx
build_status=$?
if [[ $build_status != 0 ]]; then
fatal "Configure failed with status $build_status"
fi
echoblue "Running make install in 'rtems-build' using $PARALLEL parallel jobs..."
make -j$PARALLEL install &> $TOPDIR/rtems-make.log || fatal "Make failed, see the log for details"
echogreen "RTEMS built successfully"
#
# Final report
#
echogreen "Finished successfully in $(( ($(date +%s) - $STARTED_AT) / 60 + 1 )) minutes"