-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·275 lines (228 loc) · 7.29 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash
usage() {
echo "usage: ${0} <arm|msp430> build_[binutils|gcc|newlib|gdb|all]"
echo "example: ./build build_all"
echo ""
echo "Builds a GNU GCC toolchain for RIOT."
echo "Will build in ~/tmp/riot-toolchain-build, and install to ~/opt/riot-toolchain."
echo "Edit to change these directories."
echo "Run like \"MAKEFLAGS=-j4 ${0} build_all\" to speed up on multicore systems."
}
if [ -z "${1}" ]; then
usage
exit 1
fi
TARGET=$1
shift
PATCHDIR=$(pwd)/patches
# mirror for binutils, gcc and gdb
: ${GNU_MIRROR:=http://ftp.gnu.org/gnu}
#
GCC_VER=10.1.0
GCC_SHA256=b6898a23844b656f1b68691c5c012036c2e694ac4b53a8918d4712ad876e7ea2
GCC_MIRROR=${GNU_MIRROR}/gcc/gcc-${GCC_VER}
BINUTILS_VER=2.34
BINUTILS_SHA256=f00b0e8803dc9bab1e2165bd568528135be734df3fabf8d0161828cd56028952
BINUTILS_MIRROR=${GNU_MIRROR}/binutils
NEWLIB_VER=3.3.0
NEWLIB_SHA256=58dd9e3eaedf519360d92d84205c3deef0b3fc286685d1c562e245914ef72c66
NEWLIB_MIRROR=https://sourceware.org/pub/newlib
GDB_VER=9.2
GDB_SHA256=360cd7ae79b776988e89d8f9a01c985d0b1fa21c767a4295e5f88cb49175c555
GDB_MIRROR=${GNU_MIRROR}/gdb
# package version number. travis sets ${GCC_VER}-${TRAVIS_BUILD_NUMBER}.
: ${PKG_VER:=${GCC_VER}}
# base directory to install compiled binaries into
: ${RT_INSTALL_PREFIX:=${HOME}/opt/riot-toolchain}
# directory to download source files and store intermediates
: ${RT_TMP_DIR:=~/tmp}
: ${RT_BUILDDIR:=${RT_TMP_DIR}/riot-toolchain-build/${TARGET}}
RT_INSTALL_DIR=${RT_INSTALL_PREFIX}/${TARGET}/${PKG_VER}
# uncomment to support multi-threaded compile
: ${MAKEFLAGS:=-j4}
export MAKEFLAGS
DOWNLOADER=wget
DOWNLOADER_OPTS="--progress=dot:giga -c"
SPACE_NEEDED=2641052
FREETMP=`df ${RT_TMP_DIR} | awk '{ if (NR == 2) print $4}'`
FILES=.
if [ `uname` = "Linux" ]; then
SHA256=sha256sum
SHA256_OPTS="-c -"
elif [ `uname` = "Darwin" ]; then
SHA256=sha256
SHA256_OPTS=""
else
echo "CAUTION: No 'sha256' tool for your host system found!"
fi
# set target specific options
case $TARGET in
msp430-elf)
;;
# arm-none-eabi)
# ;;
*)
echo "$0: unsupported target $TARGET."
exit 1
esac
build_binutils() {
echo "Building binutils..."
if [ ! -e binutils-${BINUTILS_VER}/.binutils_extracted ] ; then
rm -rf binutils-${BINUTILS_VER}
tar -xaf ${FILES}/binutils-${BINUTILS_VER}.tar.xz
touch binutils-${BINUTILS_VER}/.binutils_extracted
fi
rm -rf binutils-build && mkdir -p binutils-build && cd binutils-build
../binutils-${BINUTILS_VER}/configure \
--target=${TARGET} \
--prefix=${RT_INSTALL_DIR} \
--enable-interwork \
--enable-multilib \
--enable-lto \
--enable-gold \
--enable-plugins
make all CFLAGS="${CFLAGS}"
make install
cd ${RT_BUILDDIR}
}
build_gcc() {
echo "Building gcc..."
if [ ! -e gcc-${GCC_VER}/.gcc_extracted ] ; then
rm -Rf gcc-${GCC_VER}
tar -xaf ${FILES}/gcc-${GCC_VER}.tar.xz
touch gcc-${GCC_VER}/.gcc_extracted
fi
rm -rf gcc-build && mkdir -p gcc-build && cd gcc-build
export CFLAGS_FOR_TARGET="-g -gdwarf-2 -Os -ffunction-sections -fdata-sections"
export CXXFLAGS_FOR_TARGET="-g -gdwarf-2 -Os -ffunction-sections -fdata-sections"
../gcc-${GCC_VER}/configure \
--prefix=${RT_INSTALL_DIR} \
--target=${TARGET} \
--disable-decimal-float \
--disable-libffi \
--disable-libgomp \
--disable-libmudflap \
--disable-libquadmath \
--disable-libssp \
--disable-libstdcxx-pch \
--disable-nls \
--disable-shared \
--disable-tls \
--enable-addons \
--enable-gnu-indirect-function \
--enable-interwork \
--enable-languages=c,c++ \
--enable-lto \
--enable-multilib \
--enable-plugins \
--with-newlib \
--with-system-zlib \
--with-sysroot=${RT_INSTALL_DIR} \
${GCC_ARGS_EXTRA}
make all-gcc all-target-libgcc #all-target-libstdc++-v3
make install-strip-gcc install-target-libgcc #install-target-libstdc++-v3
cd ${RT_BUILDDIR}
}
extract_newlib() {
if [ ! -e newlib-${NEWLIB_VER}/.newlib_extracted ] ; then
echo -n "Extracting newlib..."
rm -Rf newlib-${NEWLIB_VER}
tar -xaf ${FILES}/newlib-${NEWLIB_VER}.tar.gz
( cd newlib-${NEWLIB_VER} && patch -p1 < ${PATCHDIR}/newlib-syscalls.patch )
( cd newlib-${NEWLIB_VER} && patch -p1 < ${PATCHDIR}/newlib-msp430-crt-disable-watchdog.patch )
touch newlib-${NEWLIB_VER}/.newlib_extracted
echo " Done."
fi
}
build_newlib() {
cd ${RT_BUILDDIR} &&
if [ ! -e .newlib_extracted ] ; then
extract_newlib
fi
rm -rf newlib-build && mkdir -p newlib-build && cd newlib-build
export AR_FOR_TARGET="${RT_INSTALL_DIR}/bin/${TARGET}-gcc-ar"
export NM_FOR_TARGET="${RT_INSTALL_DIR}/bin/${TARGET}-gcc-nm"
export RANLIB_FOR_TARGET="${RT_INSTALL_DIR}/bin/${TARGET}-gcc-ranlib"
export CFLAGS_FOR_TARGET="-gdwarf-2"
../newlib-${NEWLIB_VER}/configure \
--target=${TARGET} \
--prefix=${RT_INSTALL_DIR} \
--disable-newlib-fseek-optimization \
--disable-newlib-io-float \
--disable-newlib-supplied-syscalls \
--disable-newlib-unbuf-stream-opt \
--disable-newlib-wide-orient \
--disable-nls \
--disable-threads \
--enable-interwork \
--enable-lite-exit \
--enable-lto \
--enable-multilib \
--enable-newlib-global-atexit \
--enable-newlib-nano-formatted-io \
--enable-newlib-nano-malloc \
--enable-newlib-reent-small \
--enable-target-optspace
make all
make install
cd ${RT_BUILDDIR}
}
build_gdb() {
echo "Building gdb..."
if [ ! -e .gdb_extracted ] ; then
tar -xaf ${FILES}/gdb-${GDB_VER}.tar.xz
touch .gdb_extracted
fi
rm -rf gdb-build && mkdir -p gdb-build && cd gdb-build
../gdb-${GDB_VER}/configure \
--target=${TARGET} \
--prefix=${RT_INSTALL_DIR} \
--enable-interwork \
--enable-multilib \
--disable-build-docs
make all CFLAGS="-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0"
make install-strip-gdb
cd ${RT_BUILDDIR}
}
clean() {
echo "Cleaning up..."
rm -rf .gdb_extracted .newlib_extracted .gcc_extracted .binutils_extracted
rm -rf binutils-build gcc-build newlib-build gdb-build
}
export PATH=$PATH:${RT_INSTALL_DIR}/bin
download() {
download_file ${BINUTILS_MIRROR} binutils-${BINUTILS_VER}.tar.xz ${BINUTILS_SHA256}
download_file ${GCC_MIRROR} gcc-${GCC_VER}.tar.xz ${GCC_SHA256}
download_file ${NEWLIB_MIRROR} newlib-${NEWLIB_VER}.tar.gz ${NEWLIB_SHA256}
download_file ${GDB_MIRROR} gdb-${GDB_VER}.tar.xz ${GDB_SHA256}
}
download_file() {
echo "Downloading ${1}/${2}..."
${DOWNLOADER} ${DOWNLOADER_OPTS} $1/$2
echo -n "Checking SHA256 of "
echo "${3} ${2}" | ${SHA256} ${SHA256_OPTS}
}
check_space() {
echo "Checking disk space in ${RT_TMP_DIR}"
if [ $FREETMP -lt $SPACE_NEEDED ]
then
echo "Not enough available space in ${RT_TMP_DIR}. Minimum ${SPACE_NEEDED} free bytes required."
exit 1
fi
}
build_all() {
echo "Starting in ${RT_BUILDDIR}. Installing to ${RT_INSTALL_DIR}."
check_space
download
build_binutils
extract_newlib
build_gcc
build_newlib
build_gdb
echo "Build complete."
}
# Fail on any error
set -e
mkdir -p ${RT_BUILDDIR}
cd ${RT_BUILDDIR}
$*