-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
468 lines (370 loc) · 12.7 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/bin/bash
set -eu
declare -r workdir="${PWD}"
declare -r revision="$(git rev-parse --short HEAD)"
declare -r toolchain_directory='/tmp/atar'
declare -r share_directory="${toolchain_directory}/usr/local/share/atar"
declare -r gmp_tarball='/tmp/gmp.tar.xz'
declare -r gmp_directory='/tmp/gmp-6.3.0'
declare -r mpfr_tarball='/tmp/mpfr.tar.xz'
declare -r mpfr_directory='/tmp/mpfr-4.2.1'
declare -r mpc_tarball='/tmp/mpc.tar.gz'
declare -r mpc_directory='/tmp/mpc-1.3.1'
declare -r binutils_tarball='/tmp/binutils.tar.xz'
declare -r binutils_directory='/tmp/binutils-with-gold-2.44'
declare -r lld_tarball='/tmp/lld.tar.xz'
declare gcc_directory=''
function setup_gcc_source() {
local gcc_version=''
local gcc_url=''
local gcc_tarball=''
local tgt="${1}"
declare -r tgt
if [ "${tgt}" = 'hppa-unknown-openbsd' ] || [ "${tgt}" = 'alpha-unknown-openbsd' ] || [ "${tgt}" = 'x86_64-unknown-openbsd' ] || [ "${tgt}" = 'i386-unknown-openbsd' ]; then
gcc_version='15'
gcc_directory='/tmp/gcc-master'
gcc_url='https://github.com/gcc-mirror/gcc/archive/refs/heads/master.tar.gz'
else
gcc_version='11'
gcc_directory='/tmp/gcc-11.2.0'
gcc_url='https://ftp.gnu.org/gnu/gcc/gcc-11.2.0/gcc-11.2.0.tar.xz'
fi
gcc_tarball="/tmp/gcc-${gcc_version}.tar.xz"
declare -r gcc_version
declare -r gcc_url
declare -r gcc_tarball
if ! [ -f "${gcc_tarball}" ]; then
curl \
--url "${gcc_url}" \
--retry '30' \
--retry-all-errors \
--retry-delay '0' \
--retry-max-time '0' \
--location \
--silent \
--output "${gcc_tarball}"
tar \
--directory="$(dirname "${gcc_directory}")" \
--extract \
--file="${gcc_tarball}"
fi
[ -d "${gcc_directory}/build" ] || mkdir "${gcc_directory}/build"
if ! [ -f "${gcc_directory}/patched" ]; then
if [ "${gcc_version}" = '11' ]; then
for name in "${workdir}/submodules/openbsd-ports/lang/gcc/11/patches/patch-"*; do
patch --directory="${gcc_directory}" --strip='0' --input="${name}"
done
sed --in-place 's/HAVE_IFUNC/(HAVE_IFUNC \&\& !defined(__arm__))/g' "${gcc_directory}/libatomic/libatomic_i.h"
patch --directory="${gcc_directory}" --strip='1' --input="${workdir}/patches/0001-Thats-not-openbsd.patch"
patch --directory="${gcc_directory}" --strip='1' --input="${workdir}/patches/0001-Add-libversions.patch"
elif (( gcc_version >= 15 )); then
patch --directory="${gcc_directory}" --strip='1' --input="${workdir}/patches/0001-Fix-libatomic-build-with-newer-GCC-versions.patch"
patch --directory="${gcc_directory}" --strip='1' --input="${workdir}/patches/0001-Disable-libfunc-support-for-hppa-unknown-openbsd.patch"
patch --directory="${gcc_directory}" --strip='1' --input="${workdir}/submodules/obggcc/patches/0001-Fix-libgcc-build-on-arm.patch"
patch --directory="${gcc_directory}" --strip='1' --input="${workdir}/submodules/obggcc/patches/0001-Change-the-default-language-version-for-C-compilatio.patch"
patch --directory="${gcc_directory}" --strip='1' --input="${workdir}/submodules/obggcc/patches/0001-Turn-Wimplicit-int-back-into-an-warning.patch"
patch --directory="${gcc_directory}" --strip='1' --input="${workdir}/submodules/obggcc/patches/0001-Turn-Wint-conversion-back-into-an-warning.patch"
patch --directory="${gcc_directory}" --strip='1' --input="${workdir}/submodules/obggcc/patches/0001-Revert-GCC-change-about-turning-Wimplicit-function-d.patch"
fi
touch "${gcc_directory}/patched"
fi
}
declare -r max_jobs='40'
declare -r optlto="-flto=${max_jobs} -fno-fat-lto-objects"
declare -r optfatlto="-flto=${max_jobs} -ffat-lto-objects"
declare -r optflags='-w -O2'
declare -r linkflags='-Wl,-s'
declare build_type="${1}"
if [ -z "${build_type}" ]; then
build_type='native'
fi
declare is_native='0'
if [ "${build_type}" == 'native' ]; then
is_native='1'
fi
declare CROSS_COMPILE_TRIPLET=''
if ! (( is_native )); then
source "./submodules/obggcc/toolchains/${build_type}.sh"
fi
declare -r \
build_type \
is_native
if ! [ -f "${gmp_tarball}" ]; then
curl \
--url 'https://ftp.gnu.org/gnu/gmp/gmp-6.3.0.tar.xz' \
--retry '30' \
--retry-all-errors \
--retry-delay '0' \
--retry-max-time '0' \
--location \
--silent \
--output "${gmp_tarball}"
tar \
--directory="$(dirname "${gmp_directory}")" \
--extract \
--file="${gmp_tarball}"
fi
if ! [ -f "${mpfr_tarball}" ]; then
curl \
--url 'https://ftp.gnu.org/gnu/mpfr/mpfr-4.2.1.tar.xz' \
--retry '30' \
--retry-all-errors \
--retry-delay '0' \
--retry-max-time '0' \
--location \
--silent \
--output "${mpfr_tarball}"
tar \
--directory="$(dirname "${mpfr_directory}")" \
--extract \
--file="${mpfr_tarball}"
fi
if ! [ -f "${mpc_tarball}" ]; then
curl \
--url 'https://ftp.gnu.org/gnu/mpc/mpc-1.3.1.tar.gz' \
--retry '30' \
--retry-all-errors \
--retry-delay '0' \
--retry-max-time '0' \
--location \
--silent \
--output "${mpc_tarball}"
tar \
--directory="$(dirname "${mpc_directory}")" \
--extract \
--file="${mpc_tarball}"
fi
if ! [ -f "${binutils_tarball}" ]; then
curl \
--url 'https://ftp.gnu.org/gnu/binutils/binutils-with-gold-2.44.tar.xz' \
--retry '30' \
--retry-all-errors \
--retry-delay '0' \
--retry-max-time '0' \
--location \
--silent \
--output "${binutils_tarball}"
tar \
--directory="$(dirname "${binutils_directory}")" \
--extract \
--file="${binutils_tarball}"
patch --directory="${binutils_directory}" --strip='1' --input="${workdir}/submodules/obggcc/patches/0001-Revert-gold-Use-char16_t-char32_t-instead-of-uint16_.patch"
patch --directory="${binutils_directory}" --strip='1' --input="${workdir}/submodules/obggcc/patches/0001-Disable-annoying-linker-warnings.patch"
fi
if ! [ -f "${lld_tarball}" ]; then
[ -d "${toolchain_directory}" ] || mkdir "${toolchain_directory}"
declare target="${build_type}"
if [ "${target}" = 'native' ]; then
target='x86_64-unknown-linux-gnu'
fi
curl \
--url "https://github.com/AmanoTeam/LLVM-LLD-Builds/releases/latest/download/${target}.tar.xz" \
--retry '30' \
--retry-all-errors \
--retry-delay '0' \
--retry-max-time '0' \
--location \
--silent \
--output "${lld_tarball}"
tar \
--directory="${toolchain_directory}" \
--extract \
--strip='1' \
--file="${lld_tarball}"
fi
[ -d "${gmp_directory}/build" ] || mkdir "${gmp_directory}/build"
cd "${gmp_directory}/build"
../configure \
--host="${CROSS_COMPILE_TRIPLET}" \
--prefix="${toolchain_directory}" \
--enable-shared \
--enable-static \
CFLAGS="${optflags} ${optlto}" \
CXXFLAGS="${optflags} ${optlto}" \
LDFLAGS="${linkflags} ${optlto}"
make all --silent --jobs
make install
[ -d "${mpfr_directory}/build" ] || mkdir "${mpfr_directory}/build"
cd "${mpfr_directory}/build"
../configure \
--host="${CROSS_COMPILE_TRIPLET}" \
--prefix="${toolchain_directory}" \
--with-gmp="${toolchain_directory}" \
--enable-shared \
--enable-static \
CFLAGS="${optflags} ${optlto}" \
CXXFLAGS="${optflags} ${optlto}" \
LDFLAGS="${linkflags} ${optlto}"
make all --silent --jobs
make install
[ -d "${mpc_directory}/build" ] || mkdir "${mpc_directory}/build"
cd "${mpc_directory}/build"
../configure \
--host="${CROSS_COMPILE_TRIPLET}" \
--prefix="${toolchain_directory}" \
--with-gmp="${toolchain_directory}" \
--enable-shared \
--enable-static \
CFLAGS="${optflags} ${optlto}" \
CXXFLAGS="${optflags} ${optlto}" \
LDFLAGS="${linkflags} ${optlto}"
make all --silent --jobs
make install
[ -d "${binutils_directory}/build" ] || mkdir "${binutils_directory}/build"
declare -ra targets=(
'x86_64-unknown-openbsd'
'i386-unknown-openbsd'
'alpha-unknown-openbsd'
'arm-unknown-openbsd'
'hppa-unknown-openbsd'
'aarch64-unknown-openbsd'
'powerpc-unknown-openbsd'
'powerpc64-unknown-openbsd'
'sparc64-unknown-openbsd'
'mips64-unknown-openbsd'
'mips64el-unknown-openbsd'
'riscv64-unknown-openbsd'
)
for triplet in "${targets[@]}"; do
cd "${binutils_directory}/build"
rm --force --recursive ./*
declare extra_binutils_flags=''
declare require_lld='0'
if [ "${triplet}" = 'arm-unknown-openbsd' ] || [ "${triplet}" = 'aarch64-unknown-openbsd' ]; then
require_lld='1'
fi
if (( require_lld )); then
extra_binutils_flags+='--disable-ld --disable-gold --disable-lto '
else
extra_binutils_flags+='--enable-ld --enable-gold --enable-lto '
fi
../configure \
--host="${CROSS_COMPILE_TRIPLET}" \
--target="${triplet}" \
--prefix="${toolchain_directory}" \
--disable-gprofng \
--with-static-standard-libraries \
--with-sysroot="${toolchain_directory}/${triplet}" \
${extra_binutils_flags} \
CFLAGS="${optflags} ${optlto}" \
CXXFLAGS="${optflags} ${optlto}" \
LDFLAGS="${linkflags} ${optlto}"
make all --jobs > /dev/null
make install
cd "$(mktemp --directory)"
declare sysroot_url="https://github.com/AmanoTeam/openbsd-sysroot/releases/latest/download/${triplet}.tar.xz"
declare sysroot_file="${PWD}/${triplet}.tar.xz"
declare sysroot_directory="${PWD}/${triplet}"
curl \
--url "${sysroot_url}" \
--retry '30' \
--retry-all-errors \
--retry-delay '0' \
--retry-max-time '0' \
--location \
--silent \
--output "${sysroot_file}"
tar \
--extract \
--file="${sysroot_file}"
cp --recursive "${sysroot_directory}" "${toolchain_directory}"
rm --force --recursive ./*
cd "${toolchain_directory}/bin"
ln --symbolic './ld.lld' "./${triplet}-ld.lld"
if (( require_lld )); then
ln --symbolic './ld.lld' "./${triplet}-ld"
fi
setup_gcc_source "${triplet}"
cd "${gcc_directory}/build"
rm --force --recursive ./*
declare extra_configure_flags=''
declare have_lto='0'
declare have_disable_fixincludes='0'
if [ "${triplet}" = 'hppa-unknown-openbsd' ]; then
extra_configure_flags+='--disable-libstdcxx '
fi
if [ "${triplet}" = 'hppa-unknown-openbsd' ] || [ "${triplet}" = 'alpha-unknown-openbsd' ] || [ "${triplet}" = 'x86_64-unknown-openbsd' ] || [ "${triplet}" = 'i386-unknown-openbsd' ]; then
have_disable_fixincludes='1'
fi
if [ "${triplet}" = 'hppa-unknown-openbsd' ] || [ "${triplet}" = 'alpha-unknown-openbsd' ] || [ "${triplet}" = 'x86_64-unknown-openbsd' ] || [ "${triplet}" = 'i386-unknown-openbsd' ]; then
have_lto='1'
fi
if (( have_disable_fixincludes )); then
extra_configure_flags+='--disable-fixincludes '
fi
if (( have_lto )); then
extra_configure_flags+='--enable-lto '
else
extra_configure_flags+='--disable-lto '
fi
../configure \
--host="${CROSS_COMPILE_TRIPLET}" \
--target="${triplet}" \
--prefix="${toolchain_directory}" \
--with-linker-hash-style='gnu' \
--with-gmp="${toolchain_directory}" \
--with-mpc="${toolchain_directory}" \
--with-mpfr="${toolchain_directory}" \
--with-bugurl='https://github.com/AmanoTeam/Atar/issues' \
--with-gcc-major-version-only \
--with-pkgversion="Atar v0.9-${revision}" \
--with-sysroot="${toolchain_directory}/${triplet}" \
--with-native-system-header-dir='/include' \
--enable-__cxa_atexit \
--enable-cet='auto' \
--enable-checking='release' \
--enable-gnu-indirect-function \
--enable-gnu-unique-object \
--enable-libstdcxx-backtrace \
--enable-shared \
--enable-threads='posix' \
--enable-languages='c,c++' \
--enable-cpp \
--enable-default-pie \
--enable-standard-branch-protection \
--enable-wchar_t \
--without-headers \
--disable-plugin \
--disable-libsanitizer \
--disable-bootstrap \
--disable-libgomp \
--disable-libmudflap \
--disable-libssp \
--disable-libstdcxx-pch \
--disable-multilib \
--disable-nls \
--disable-tls \
--disable-werror \
${extra_configure_flags} \
am_cv_func_iconv=no \
ac_cv_header_magic_h=no \
CFLAGS="${optflags}" \
CXXFLAGS="${optflags}" \
LDFLAGS="${linkflags}"
declare CFLAGS_FOR_TARGET="${optflags} ${linkflags}"
declare CXXFLAGS_FOR_TARGET="${optflags} ${linkflags}"
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80196#c12
if ! (( is_native )); then
CXXFLAGS_FOR_TARGET+=' -nostdinc++'
fi
LD_LIBRARY_PATH="${toolchain_directory}/lib" PATH="${PATH}:${toolchain_directory}/bin" make \
CFLAGS_FOR_TARGET="${CFLAGS_FOR_TARGET}" \
CXXFLAGS_FOR_TARGET="${CXXFLAGS_FOR_TARGET}" \
all --jobs="${max_jobs}"
make install
cd "${toolchain_directory}/${triplet}/bin"
ln --symbolic '../../bin/ld.lld' 'ld.lld'
if (( require_lld )); then
ln --symbolic '../../bin/ld.lld' 'ld'
fi
if ! (( have_disable_fixincludes )); then
rm --recursive "${toolchain_directory}/lib/gcc/${triplet}/"*"/include-fixed"
fi
patchelf --add-rpath '$ORIGIN/../../../../lib' "${toolchain_directory}/libexec/gcc/${triplet}/"*"/cc1"
patchelf --add-rpath '$ORIGIN/../../../../lib' "${toolchain_directory}/libexec/gcc/${triplet}/"*"/cc1plus"
if (( have_lto )); then
patchelf --add-rpath '$ORIGIN/../../../../lib' "${toolchain_directory}/libexec/gcc/${triplet}/"*"/lto1"
fi
done
mkdir --parent "${share_directory}"
cp --recursive "${workdir}/tools/dev/"* "${share_directory}"