-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_stuff.sh
executable file
·253 lines (215 loc) · 8.19 KB
/
build_stuff.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
#!/bin/bash
# Central build script for downloading and compiling a GCC cross-compiler
# for various platforms. Mainly tested with ARM and AARCH64 systems.
# Exit function that prints a message before exiting
die() {
echo "Error: $1"
exit 1
}
# Check if stage is not built
# Used to skip already build stages by checking the .buildstages file
# created by commiting a stage
is_stage_not_built() {
local stage_name=$1
grep -q "$stage_name" .buildstages
local res=$?
if [ "$res" == "0" ]; then
return 1
else
return 0
fi
}
# Commit a stage
# Stage will be recorded in the .buildstages file
commit_stage() {
local stage_name=$1
sed -i "/$stage_name/d" .buildstages
echo "$stage_name" >> .buildstages
}
# Change shell title
print_title() {
local title=$1
echo -e '\033]2;'$title'\007'
}
CWD=$(pwd)
[ ! -f "variables" ] && die "'variables' file missing"
. variables
[ -z "${TOOLS_FILE}" ] && die "TOOLS_FILE file missing"
TOOLS_FILE=$(realpath ${TOOLS_FILE})
[ -z "${PREFIX}" ] && die "Empty prefix"
mkdir -p ${PREFIX} || die "Could not create PREFIX directory"
PREFIX=$(realpath $PREFIX)
IMAGE_PREFIX=${PREFIX}/${TARGET_ARCH}
BIN_PREFIX=${PREFIX}/bin
SYSROOT_DIR=
SYSROOT_PREFIX=${IMAGE_PREFIX}/${SYSROOT_DIR}
PATH=${BIN_PREFIX}:$PATH
mkdir -p ${WORK_DIR}
cd ${WORK_DIR}
# This stage downloads the dependencies found in the 'tools' file
touch .downloaded
while read line; do
name=$(echo $line | tr -s ' ' | cut -d ' ' -f 1)
url=$(echo $line | tr -s ' ' | cut -d ' ' -f 2)
package=${url##*/}
if grep -q "$name" .downloaded; then
echo "$name is already downloaded"
continue
else
wget $url
[ "$?" != 0 ] && die "Could not download $package from $url"
echo "Unpacking $package..."
tar -xf $package
echo "Done."
echo $name >> .downloaded
rm -f $package
fi
done < ${TOOLS_FILE}
echo "Running build stages..."
touch .buildstages
# This stage applies any patch scripts that are present in the current
# working directory
echo "Applying post-download patches.."
if is_stage_not_built "applying-patches"; then
print_title "Post-download patches"
cd $CWD
for patch_file in patch-*.sh; do
if [ -x $patch_file ]; then
./$patch_file || die "Could not apply post-download patch '${patch_file}'"
fi
done
cd ${WORK_DIR}
commit_stage "applying-patches"
else
echo "Patches already applied"
fi
# this stage sets up the links to the GCC dependencies that was downloaded
# in a previous step
if is_stage_not_built "gcc-links"; then
print_title "GCC dependencies links"
echo "Setting up GCC dependecies links..."
cd gcc-*
ln -s ../mpfr-* mpfr
ln -s ../gmp-* gmp
ln -s ../mpc-* mpc
ln -s ../isl-* isl
ln -s ../cloog-* cloog
cd ..
echo "done."
commit_stage "gcc-links"
fi
echo "Building Binutils.."
# This stage builds GNU Binutils which are the programs for manipulating the
# binary object files created by the compiler (linker ld, assembler as, and more..)
if is_stage_not_built "built-binutils"; then
print_title "Building binutils"
mkdir -p build-binutils
cd build-binutils
../binutils-*/configure --prefix= --with-sysroot=$SYSROOT_PREFIX --target=${TARGET_ARCH} ${BINUTILS_OPTS} || die "Could not configure binutils"
make configure-host || die "configure host"
make LDFLAGS="-all-static" -j4 || die "building binutils"
make install-strip DESTDIR=${PREFIX} || die "Binutils build failed"
cd ..
commit_stage "built-binutils"
echo "built binutils."
else
echo "Already built binutils"
fi
echo "Installing Linux headers.."
# The compiler needs to know how to handle system calls to the Linux kernel so
# it needs the headers of some version of the Linux kernel to do this. This stage
# installs the relevant headers in the install prefix.
if is_stage_not_built "install-linux-headers"; then
print_title "Linux headers"
make -C linux-* ARCH=${LINUX_ARCH} INSTALL_HDR_PATH=${SYSROOT_PREFIX}/usr headers_install || die "Could not install linux headers"
echo "Installed Linux headers."
commit_stage "install-linux-headers"
else
echo "Already installed Linux headers."
fi
echo "Installing GCC cross compiler.."
# This stage compiles the actual cross compiler, so it can be used to compile
# the standard libraries (C and C++) in a later stage.
if is_stage_not_built "cross-compiler"; then
print_title "GCC stage 1"
mkdir -p build-gcc
cd build-gcc
../gcc-*/configure --prefix= --with-sysroot=/${TARGET_ARCH}/${SYSROOT_DIR} --with-build-sysroot=${SYSROOT_PREFIX} --target=${TARGET_ARCH} ${GCC_OPTS} || die "Could not configure cross compiler"
make -j4 all-gcc && make install-gcc DESTDIR=${PREFIX} || die "Could not install cross compiler"
cd ..
echo "Installed GCC cross compiler."
commit_stage "cross-compiler"
else
echo "Already install GCC cross compiler."
fi
echo "Installing standard C headers and startup files.."
# This stage configures the glibc sources, creates and install glibc headers,
# and builds and install the crt files which are C RunTime files (basically, the
# C code that initialize a main-function in a C program)
if is_stage_not_built "std-c-headers-and-runtime"; then
print_title "libc headers and C runtime"
mkdir -p build-glibc
cd build-glibc
../glibc-*/configure --prefix=/usr --with-sysroot=/${TARGET_ARCH}/${SYSROOT_DIR} --build=$MACHTYPE --host=${TARGET_ARCH} --target=${TARGET_ARCH} ${GLIBC_OPTS} libc_cv_forced_unwind=yes || die "Could not configure glibc"
make install-bootstrap-headers=yes install-headers DESTDIR=${SYSROOT_PREFIX} || die "Could not standard C headers and startup files"
mkdir -p ${SYSROOT_PREFIX}/usr/lib
make -j4 csu/subdir_lib && install csu/crt1.o csu/crti.o csu/crtn.o ${SYSROOT_PREFIX}/usr/lib || die "Could not install C Runtime files"
${TARGET_ARCH}-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o ${SYSROOT_PREFIX}/usr/lib/libc.so || die "Could not create startup files"
touch ${SYSROOT_PREFIX}/usr/include/gnu/stubs.h
cd ..
commit_stage "std-c-headers-and-runtime"
echo "Installed standard C and startup files."
else
echo "Already installed standard C and startup files."
fi
echo "Installing compiler support library.."
# This stage builds support libraries for the cross-compiler. These libraries
# contain C++ exeception handler code amoungst other this.
if is_stage_not_built "compiler-support-lib"; then
print_title "Libgcc"
make -C build-gcc -j4 all-target-libgcc || die "Could not compile libgcc"
make -C build-gcc install-target-libgcc DESTDIR=${PREFIX} || die "Could not install libgcc"
commit_stage "compiler-support-lib"
echo "Installed compiler support library."
else
echo "Already installed compiler support library"
fi
echo "Installing standard C library"
# Finally we build the glibc fully
if is_stage_not_built "std-c-lib"; then
print_title "Glibc"
make -C build-glibc CFLAGS="-O2 ${GLIBC_CFLAGS}" -j4 || die "Could not build glibc"
make -C build-glibc install DESTDIR=${SYSROOT_PREFIX} || die "Could not install standard C library"
echo "Installed standard C library"
commit_stage "std-c-lib"
else
echo "Already installed standard C library"
fi
echo "Installing standard C++ library"
# This stage builds the C++ library
if is_stage_not_built "std-c++-lib"; then
print_title "GCC stage 2 (final)"
make -C build-gcc -j4 || die "Could not build GCC stage 2"
make -C build-gcc install DESTDIR=${PREFIX} || die "Could not install standard C++ library"
echo "Installed standard C++ library."
commit_stage "std-c++-lib"
else
echo "Already installed standard C++ library"
fi
# This stage applies any patch scripts that are present in the current
# working directory
echo "Applying post-build patches.."
if is_stage_not_built "applying-post-patches"; then
print_title "Post-build patches"
cd $CWD
for patch_file in post-patch-*.sh; do
if [ -x $patch_file ]; then
./$patch_file || die "Could not apply post-build patch '${patch_file}'"
fi
done
cd $WORK_DIR
commit_stage "applying-post-patches"
else
echo "Post-build patches already applied"
fi
cd $CWD