-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.sh
executable file
·254 lines (209 loc) · 6.32 KB
/
build.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
#!/usr/bin/env bash
# shellcheck source-path=SCRIPTDIR/..
#
# Shell script for building Phoenix-RTOS based firmware
#
# Main builder
#
# Copyright 2018, 2019 Phoenix Systems
# Author: Kaja Swat, Aleksander Kaminski, Pawel Pisarczyk
#
set -e
ORIG_ENV="$(env)"
# Colon-separated list of dirs to overlay the default rootFS.
# It can be overwritten by build.project scripts.
ROOTFS_OVERLAYS=""
source ./phoenix-rtos-build/build.subr
# set all static output dirs before sourcing target/project build script
PREFIX_PROJECT="$(pwd)"
_TARGET_FOR_HOST_BUILD="host-generic-pc"
PREFIX_BUILD="$PREFIX_PROJECT/_build/$TARGET"
PREFIX_BUILD_HOST="$PREFIX_PROJECT/_build/$_TARGET_FOR_HOST_BUILD"
PREFIX_FS="$PREFIX_PROJECT/_fs/$TARGET"
PREFIX_BOOT="$PREFIX_PROJECT/_boot/$TARGET"
PREFIX_PROG="$PREFIX_BUILD/prog/"
PREFIX_PROG_STRIPPED="$PREFIX_BUILD/prog.stripped/"
PREFIX_A="$PREFIX_BUILD/lib/"
PREFIX_H="$PREFIX_BUILD/include/"
PREFIX_SYSROOT="" # empty by default (use toolchain sysroot)
PLO_SCRIPT_DIR="$PREFIX_BUILD/plo-scripts"
PREFIX_ROOTFS="$PREFIX_FS/root/"
export TARGET TARGET_FAMILY TARGET_SUBFAMILY TARGET_PROJECT PROJECT_PATH PREFIX_PROJECT PREFIX_BUILD\
PREFIX_BUILD_HOST PREFIX_FS PREFIX_BOOT PREFIX_PROG PREFIX_PROG_STRIPPED PREFIX_A\
PREFIX_H PREFIX_ROOTFS CROSS CFLAGS CXXFLAGS LDFLAGS CC LD AR AS MAKEFLAGS DEVICE_FLAGS PLO_SCRIPT_DIR\
PREFIX_SYSROOT LIBPHOENIX_DEVEL_MODE
source ./build.project
# Some makefiles add "$PROJECT_PATH/" to their include path so it has to be set
[ -z "$PROJECT_PATH" ] && b_die "PROJECT_PATH is not set (or is empty)"
: "${PREFIX_ROOTSKEL:="$PREFIX_PROJECT/_fs/root-skel/"}"
# LIBPHOENIX_DEVEL_MODE:
# - if enabled (y): use project-specific sysroot (PREFIX_SYSROOT) and install kernel headers + compile/install libphoenix
# - if disabled (n or not set): use toolchain sysroot, don't install kernel headers, don't compile libphoenix
#TODO: change default value to 'n' when the toolchain-supplied sysroot will be stable enough
: "${LIBPHOENIX_DEVEL_MODE:=y}"
if [ "$LIBPHOENIX_DEVEL_MODE" = "y" ]; then
PREFIX_SYSROOT="$PREFIX_BUILD/sysroot"
fi
# Default project's overlay directory, it does not have to exist.
ROOTFS_OVERLAYS="$PROJECT_PATH/rootfs-overlay:${ROOTFS_OVERLAYS}"
CC=${CROSS}gcc
AS=${CROSS}as
LD=${CROSS}ld
AR=${CROSS}ar
MAKEFLAGS="--no-print-directory -j 9"
# export flags for ports - call make only after all necessary env variables are already set
EXPORT_CFLAGS="$(make -f phoenix-rtos-build/Makefile.common export-cflags)"
EXPORT_LDFLAGS="$(make -f phoenix-rtos-build/Makefile.common export-ldflags)"
# export STRIP path & args for stripping binaries
EXPORT_STRIP="$(make -f phoenix-rtos-build/Makefile.common export-strip)"
export EXPORT_CFLAGS EXPORT_LDFLAGS EXPORT_STRIP
#
# Parse command line
#
if [ $# -lt 1 ]; then
echo "Build options should be specified!"
echo "Usage: build.sh [clean] [all] [host] [fs] [core] [test] [ports] [project] [image]";
exit 1;
fi
B_CLEAN="n"
B_FS="n"
B_CORE="n"
B_HOST="n"
B_PORTS="n"
B_PROJECT="n"
B_IMAGE="n"
B_TEST="n"
# GA CI passes all params as quoted first param - split on ' ' if necessary
ARGS=("$@")
[ "$#" -eq 1 ] && read -ra ARGS <<< "$1"
for arg in "${ARGS[@]}"; do
case "$arg"
in
clean)
B_CLEAN="y";;
fs)
B_FS="y";;
core)
B_CORE="y";;
host)
B_HOST="y";;
test|tests)
B_TEST="y";;
ports)
B_PORTS="y";;
project)
B_PROJECT="y";;
image)
B_IMAGE="y";;
all)
B_FS="y"; B_CORE="y"; B_HOST="y"; B_PORTS="y"; B_PROJECT="y"; B_IMAGE="y";;
*)
echo "Unknown build option: \"$arg\"."
exit 1;;
esac;
done
#
# Clean if requested
#
if [ "$B_CLEAN" = "y" ]; then
b_log "Cleaning build dirs"
rm -rf "$PREFIX_BUILD" "$PREFIX_BUILD_HOST"
rm -rf "$PREFIX_FS"
rm -rf "$PREFIX_BOOT"
fi
#
# Prepare
#
mkdir -p "$PREFIX_BUILD"
mkdir -p "$PREFIX_BUILD_HOST"
mkdir -p "$PREFIX_BOOT"
mkdir -p "$PREFIX_PROG" "$PREFIX_PROG_STRIPPED"
if [ -n "$PREFIX_SYSROOT" ]; then
mkdir -p "$PREFIX_SYSROOT" "$PREFIX_SYSROOT/include" "$PREFIX_SYSROOT/usr"
# libc includes needs to be accessible by ${SYSROOT}/usr/local/include for C++ headers to work
(cd "$PREFIX_SYSROOT/usr" && ln -sfn . local)
# see sysroot-setup.mk for next steps in sysroot setup
fi
if declare -f "b_prepare" > /dev/null; then
b_prepare
fi
if command -v git > /dev/null && [ -a ".git" ]; then
echo " $(git rev-parse HEAD) $(basename "$(git rev-parse --show-toplevel)") ($(git describe --always --dirty))" > "${PREFIX_BUILD}/git-version"
git submodule status --recursive >> "${PREFIX_BUILD}/git-version"
else
echo "not available" > "${PREFIX_BUILD}/git-version"
fi
#
# Preparing filesystem
#
if [ "${B_FS}" = "y" ] && [ -d "${PREFIX_ROOTSKEL}" ]; then
b_log "Preparing filesystem"
mkdir -p "${PREFIX_ROOTFS}"
cp -a "${PREFIX_ROOTSKEL}/." "${PREFIX_ROOTFS}"
mkdir -p "$PREFIX_ROOTFS/"{dev,etc,local,data,mnt,tmp,var,usr}
# ROOTFS_OVERLAYS contains colon-separated path
(
IFS=:
for path in $ROOTFS_OVERLAYS; do
if [ -d "$path" ]; then
echo "Applying overlay: $path"
cp -a "${path}/." "${PREFIX_ROOTFS}"
else
echo "Not existing rootfs overlay: $path"
fi
done
)
b_log "Saving git-version"
install -m 664 "${PREFIX_BUILD}/git-version" "$PREFIX_FS/root/etc"
fi
#
# Build host tools
#
if [ "${B_HOST}" = "y" ]; then
if [ "$TARGET" != "$_TARGET_FOR_HOST_BUILD" ]; then
# if not already building for host - re-exec with clean env
(env "$ORIG_ENV" NOSAN=1 TARGET=$_TARGET_FOR_HOST_BUILD ./phoenix-rtos-build/build.sh host)
else
source ./phoenix-rtos-build/build-host-tools.sh
fi
fi
# always install hostutils if they are present
b_log "Installing hostutils"
HOSTUTILS=(metaelf phoenixd psdisk psu syspagen mcxisp mkrofs)
for tool in "${HOSTUTILS[@]}"; do
toolfile="$PREFIX_BUILD_HOST/prog.stripped/$tool"
[ -e "$toolfile" ] && cp -a "$toolfile" "$PREFIX_BOOT"
done
# use per-project compile_commands.json compilation database
COMPILE_DB_FNAME="compile_commands.json"
ln -sf "$PREFIX_BUILD/$COMPILE_DB_FNAME" "$COMPILE_DB_FNAME"
#
# Build core part
#
if [ "${B_CORE}" = "y" ]; then
"./phoenix-rtos-build/build-core-${TARGET_FAMILY}-${TARGET_SUBFAMILY}.sh"
fi
#
# Build test part
#
if [ "${B_TEST}" = "y" ]; then
b_build_test
fi
#
# Build ports
#
if [ "${B_PORTS}" = "y" ] && [ -d phoenix-rtos-ports ]; then
./phoenix-rtos-ports/build.sh
fi
#
# Build project part
#
if [ "${B_PROJECT}" = "y" ]; then
b_build
fi
#
# Build final filesystems
#
if [ "${B_IMAGE}" = "y" ]; then
b_image
fi