-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtoolchain.sh
350 lines (249 loc) · 6.96 KB
/
toolchain.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
346
347
348
349
350
#!/bin/bash
if [ -f variables.sh ]; then
. variables.sh
fi
. ghost.sh
#
# === Ghost Toolchain setup script ===
#
# This script attempts to download, patch and build the Ghost-specific toolchain
# so that your host system is capable of building binaries for Ghost.
#
# The following pre-requirements must be installed on your system:
#
# gcc g++ nasm make texinfo flex bison
# libmpfr-dev libgmp-dev libmpc-dev autoconf pkg-config
# grub-pc-bin xorriso
#
# The build will fail if any of these requirements are not present.
# To modify parameters, check the "variables.sh.template".
#
echo ""
printf "\e[44mGhost Toolchain setup script\e[0m\n"
echo ""
echo "Target architecture: $TARGET"
echo "Toolchain base: $TOOLCHAIN_BASE"
echo ""
# Archive sources & patch versions
GCC_ARCHIVE=https://ftp.gnu.org/gnu/gcc/gcc-12.2.0/gcc-12.2.0.tar.gz
GCC_PATCH=patches/toolchain/gcc-12.2.0-ghost-1.0.patch
GCC_UNPACKED=gcc-12.2.0
BINUTILS_ARCHIVE=https://ftp.gnu.org/gnu/binutils/binutils-2.39.tar.gz
BINUTILS_PATCH=patches/toolchain/binutils-2.39-ghost-1.0.patch
BINUTILS_UNPACKED=binutils-2.39
# Add toolchain bin folder to PATH
PATH=$PATH:$TOOLCHAIN_BASE/bin
# Default definitions
with REQUIRED_AUTOCONF "autoconf (GNU Autoconf) 2.69"
with AUTOMAKE automake
with AUTOCONF autoconf
with HOST_CXX g++
# Parse parameters
STEP_DOWNLOAD=1
STEP_CLEAN=0
STEP_UNPACK=1
STEP_PATCH=1
STEP_BUILD_BINUTILS=1
STEP_BUILD_GCC=1
for var in "$@"; do
if [ $var == "--skip-archives" ]; then
STEP_DOWNLOAD=0
STEP_UNPACK=0
STEP_PATCH=0
elif [ $var == "--skip-download" ]; then
STEP_DOWNLOAD=0
elif [ $var == "--skip-unpack" ]; then
STEP_UNPACK=0
elif [ $var == "--skip-patch" ]; then
STEP_PATCH=0
elif [ $var == "--skip-build-binutils" ]; then
STEP_BUILD_BINUTILS=0
elif [ $var == "--skip-build-gcc" ]; then
STEP_BUILD_GCC=0
elif [ $var == "--clean" ]; then
STEP_CLEAN=1
elif [ $var == "--help" ]; then
echo "Run this script to build a cross toolchain for your host system."
echo "The following flags are available:"
echo
echo " --skip-download Skips the downloading of binutils/gcc archives"
echo " --skip-unpack Skips the unpacking of said archives"
echo " --skip-patch Skips the patching of the unpacked sources"
echo " --skip-build-binutils Skips building binutils"
echo " --skip-build-gcc Skips building gcc"
echo " --clean Cleans everything"
echo " --help Prints this help screen"
echo
echo "To specify a different path for your TOOLCHAIN_BASE/SYSROOT,"
echo "copy 'variables.sh.template' to 'variables.sh' and use export"
echo "to set the respective variables."
echo
exit
else
echo "unknown parameter: $var"
exit
fi
done
pushd() {
command pushd "$@" > /dev/null
}
popd() {
command popd "$@" > /dev/null
}
echo "Checking tools"
requireTool patch
requireTool curl
requireTool $AUTOCONF
# Check version of autoconf
echo " $REQUIRED_AUTOCONF"
$AUTOCONF --version | grep -q "$REQUIRED_AUTOCONF"
if [[ $? != 0 ]]; then
echo " -> wrong autoconf version:"
echo " $($AUTOCONF --version | sed -n 1p)"
exit
fi
# Clean if necessary
if [ $STEP_CLEAN == 1 ]; then
rm -rf temp
fi
# Create temp
echo "Creating temporary work directory"
mkdir -p temp
# Download archives if necessary
if [ $STEP_DOWNLOAD == 1 ]; then
echo "Downloading archive files"
echo " gcc"
curl $GCC_ARCHIVE -o temp/gcc.tar.gz -k
echo " binutils"
curl $BINUTILS_ARCHIVE -o temp/binutils.tar.gz -k
else
echo "Skipping file download"
fi
# Unpack archives
if [ $STEP_UNPACK == 1 ]; then
echo "Unpacking archives"
tar -xf temp/gcc.tar.gz -C temp
failOnError
tar -xf temp/binutils.tar.gz -C temp
failOnError
else
echo "Skipping unpacking"
fi
# Apply patches
if [ $STEP_PATCH == 1 ]; then
echo "Patching GCC"
patch -d temp/$GCC_UNPACKED -p 1 < $GCC_PATCH >>temp/patching.log 2>&1
pushd temp/$GCC_UNPACKED/libstdc++-v3
echo "Updating autoconf in libstdc++-v3"
$AUTOCONF
popd
echo "Patching binutils"
patch -d temp/$BINUTILS_UNPACKED -p 1 < $BINUTILS_PATCH >>temp/patching.log 2>&1
else
echo "Skipping patching"
fi
# Build tools
echo "Building 'changes' tool"
pushd tools/changes
CC=$HOST_CXX $SH build.sh all >>ghost-build.log 2>&1
popd
echo "Building 'ramdisk-writer' tool"
pushd tools/ramdisk-writer
CC=$HOST_CXX $SH build.sh all >>ghost-build.log 2>&1
popd
echo "Installing 'pkg-config' wrapper"
pushd tools/pkg-config
$SH build.sh >>ghost-build.log 2>&1
popd
# Install headers
echo "Installing libc and libapi headers"
pushd libc
$SH build.sh install-headers >>ghost-build.log 2>&1
popd
pushd libapi
$SH build.sh install-headers >>ghost-build.log 2>&1
popd
# Build binutils
if [ $STEP_BUILD_BINUTILS == 1 ]; then
echo "Building binutils"
mkdir -p temp/build-binutils
pushd temp/build-binutils
echo " Configuring"
../$BINUTILS_UNPACKED/configure --target=$TARGET --prefix=$TOOLCHAIN_BASE --disable-nls --enable-shared --disable-werror --with-sysroot=$SYSROOT >>ghost-build.log 2>&1
failOnError
echo " Building"
make all -j8 >>ghost-build.log 2>&1
failOnError
echo " Installing"
make install >>ghost-build.log 2>&1
failOnError
popd
else
echo "Skipping build of binutils"
fi
# Build gcc
if [ $STEP_BUILD_GCC == 1 ]; then
echo "Building gcc"
mkdir -p temp/build-gcc
pushd temp/build-gcc
echo " Configuration"
../$GCC_UNPACKED/configure --target=$TARGET --prefix=$TOOLCHAIN_BASE --disable-nls --enable-languages=c,c++ --enable-shared --with-sysroot=$SYSROOT $BUILD_GCC_ADDITIONAL_FLAGS >>ghost-build.log 2>&1
failOnError
echo " Building core"
make all-gcc -j8 >>ghost-build.log 2>&1
failOnError
echo " Installing core"
make install-gcc >>ghost-build.log 2>&1
failOnError
popd
else
echo "Skipping build of GCC"
fi
# Build libc static
echo "Building libc static"
pushd libc
$SH build.sh clean static >>ghost-build.log 2>&1
failOnError
popd
# Build libapi
echo "Building libapi static"
pushd libapi
$SH build.sh clean static >>ghost-build.log 2>&1
failOnError
popd
# Build libgcc
echo "Building target GCC libraries"
pushd temp/build-gcc
echo " Building target libgcc"
make all-target-libgcc -j8 >>ghost-build.log 2>&1
failOnError
echo " Installing target libgcc"
make install-target-libgcc >>ghost-build.log 2>&1
failOnError
echo " Copying artifacts to system/lib"
cp "$TOOLCHAIN_BASE/$TARGET/lib/libgcc_s.so.1" "$SYSROOT/system/lib/libgcc_s.so.1"
failOnError
popd
# Build libc shared
echo "Building libc shared"
pushd libc
$SH build.sh shared >>ghost-build.log 2>&1
failOnError
popd
# Build libapi shared
echo "Building libapi shared"
pushd libapi
$SH build.sh shared >>ghost-build.log 2>&1
failOnError
popd
# Build libstdc++-v3
pushd temp/build-gcc
echo " Building libstdc++-v3"
make all-target-libstdc++-v3 -j8 >>ghost-build.log 2>&1
failOnError
echo " Installing libstdc++-v3"
make install-target-libstdc++-v3 >>ghost-build.log 2>&1
failOnError
popd
# Finished
echo "Toolchain successfully built"