forked from libos-nuse/frankenlibc
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
executable file
·748 lines (661 loc) · 19.3 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#!/bin/sh
write_log()
{
if [ -z ${BUILD_QUIET} ] ; then
if [ "$1" = "-n" ] ; then
SECONDS=0
/bin/echo -n "=== $1" 1>&1
return
fi
/bin/echo "$* ($SECONDS sec)" 1>&1
elif [ "${BUILD_QUIET}" = "-qq" ] ; then
if [ "$1" = "-n" ] ; then
SECONDS=0
/bin/echo -n "=== $2" 1>&3
return
fi
/bin/echo "$* ($SECONDS sec)" 1>&3
fi
}
MAKE=${MAKE-make}
RUMPOBJ=${PWD}/rumpobj
RUMP=${RUMPOBJ}/rump
RUMPSRC=${PWD}/src
OUTDIR=${PWD}/rump
NCPU=1
RUMP_KERNEL=netbsd
prefix=
export prefix
export MAKE
export RUMPSRC
export RUMPOBJ
EXTRA_AFLAGS="-Wa,--noexecstack"
EXTRA_CFLAGS="-fPIC"
if [ -n "$(${CC-cc} -v 2>&1 | grep "enable-default-pie")" ]; then
LDFLAGS_NO_PIE="-no-pie"
fi
if [ -n "$(${CC} -v 2>&1 | grep "clang")" ]; then
LDFLAGS_NO_PIE="-no-pie"
fi
TARGET=$(LC_ALL=C ${CC-cc} -v 2>&1 | sed -n 's/^Target: //p' )
case ${TARGET} in
*-linux*)
OS=linux
export HOST_CFLAGS="-fcommon"
EXTRA_CFLAGS="${EXTRA_CFLAGS} -fcommon"
if [ ${TARGET} = "aarch64-linux-gnu" ] ; then
EXTRA_CFLAGS="${EXTRA_CFLAGS} -mno-outline-atomics"
fi
;;
*-netbsd*)
OS=netbsd
;;
*-freebsd*)
OS=freebsd
FILTER="-DCAPSICUM"
;;
*-darwin*)
OS=darwin
prefix="_"
EXTRA_CFLAGS="-mmacosx-version-min=10.7.0 -Wno-unsupported-visibility -Wno-error-implicit-function-declaration"
EXTRA_CFLAGS="${EXTRA_CFLAGS} -fno-stack-check"
export HOST_CFLAGS="-Wno-error-implicit-function-declaration"
EXTRA_AFLAGS="-mmacosx-version-min=10.7.0"
;;
*)
OS=unknown
esac
HOST=$(uname -s)
case ${HOST} in
Linux)
NCPU=$(nproc 2>/dev/null || echo 1)
;;
NetBSD)
NCPU=$(sysctl -n hw.ncpu)
;;
FreeBSD)
NCPU=$(sysctl -n hw.ncpu)
;;
esac
STDJ="-j ${NCPU}"
BUILD_QUIET=""
DBG_F='-O2 -g'
helpme()
{
printf "Usage: $0 [-h] [options] [platform]\n"
printf "supported options:\n"
printf "\t-k: type of rump kernel [netbsd|linux]. default linux\n"
printf "\t-L: libraries to link eg net_netinet,net_netinet6. default all\n"
printf "\t-m: hardcode rump memory limit. default from env or unlimited\n"
printf "\t-M: thread stack size. default: 64k\n"
printf "\t-p: huge page size to use eg 2M or 1G\n"
printf "\t-r: release build, without debug settings\n"
printf "\t-s: location of source tree. default: PWD/rumpsrc\n"
printf "\t-o: location of object files. default PWD/rumpobj\n"
printf "\t-d: location of installed files. default PWD/rump\n"
printf "\t-b: location of binaries. default PWD/rump/bin\n"
printf "\tseccomp|noseccomp: select Linux seccomp (default off)\n"
printf "\texecveat: use new linux execveat call default off)\n"
printf "\tcapsicum|nocapsicum: select FreeBSD capsicum (default on)\n"
printf "\tdeterministic: make deterministic\n"
printf "\tnotests: do not run tests\n"
printf "\tnotools: do not build extra tools\n"
printf "\tclean: clean object directory first\n"
printf "Other options are passed to buildrump.sh\n"
printf "\n"
printf "Supported platforms are currently: linux, netbsd, freebsd, qemu-arm, spike\n"
exit 1
}
bytes()
{
value=$(echo "$1" | sed 's/[^0123456789].*$//g')
units=$(echo "$1" | sed 's/^[0123456789]*//g')
case "$units" in
"kb"|"k"|"KB"|"K")
value=$((${value} * 1024))
;;
"mb"|"m"|"MB"|"M")
value=$((${value} * 1048576))
;;
"gb"|"g"|"GB"|"G")
value=$((${value} * 1073741824))
;;
*)
die "Cannot understand value"
;;
esac
echo ${value}
}
abspath() {
cd "$1"
printf "$(pwd)"
}
appendvar_fs ()
{
vname="${1}"
fs="${2}"
shift 2
if [ -z "$(eval echo \${$vname})" ]; then
eval ${vname}="\${*}"
else
eval ${vname}="\"\${${vname}}"\${fs}"\${*}\""
fi
}
appendvar ()
{
vname="$1"
shift
appendvar_fs "${vname}" ' ' $*
}
. ./buildrump/subr.sh
while getopts '?b:d:F:Hhj:k:L:M:m:o:p:qrs:V:' opt; do
case "$opt" in
"b")
mkdir -p ${OPTARG}
BINDIR=$(abspath ${OPTARG})
;;
"d")
mkdir -p ${OPTARG}
OUTDIR=$(abspath ${OPTARG})
;;
"F")
ARG=${OPTARG#*=}
case ${OPTARG} in
CFLAGS\=*)
appendvar EXTRA_CFLAGS "${ARG}"
;;
AFLAGS\=*)
appendvar EXTRA_AFLAGS "${ARG}"
;;
LDFLAGS\=*)
appendvar EXTRA_LDFLAGS "${ARG}"
;;
ACFLAGS\=*)
appendvar EXTRA_CFLAGS "${ARG}"
appendvar EXTRA_AFLAGS "${ARG}"
;;
ACLFLAGS\=*)
appendvar EXTRA_CFLAGS "${ARG}"
appendvar EXTRA_AFLAGS "${ARG}"
appendvar EXTRA_LDFLAGS "${ARG}"
;;
CPPFLAGS\=*)
appendvar EXTRA_CPPFLAGS "${ARG}"
;;
DBG\=*)
appendvar F_DBG "${ARG}"
;;
CWARNFLAGS\=*)
appendvar EXTRA_CWARNFLAGS "${ARG}"
;;
*)
die Unknown flag: ${OPTARG}
;;
esac
;;
"H")
appendvar EXTRAFLAGS "-H"
;;
"h")
helpme
;;
"j")
STDJ="-j ${OPTARG}"
;;
"k")
if [ "${OPTARG}" != "netbsd" ] && [ "${OPTARG}" != "linux" ] ; then
echo ">> ERROR Unknown rump kernel type: ${OPTARG}"
helpme
fi
RUMP_KERNEL="${OPTARG}"
;;
"L")
LIBS="${OPTARG}"
;;
"M")
size=$(bytes ${OPTARG})
appendvar FRANKEN_FLAGS "-DSTACKSIZE=${size}"
;;
"m")
size=$(bytes ${OPTARG})
appendvar RUMPUSER_FLAGS "-DRUMP_MEMLIMIT=${size}"
;;
"o")
mkdir -p ${OPTARG}
RUMPOBJ=$(abspath ${OPTARG})
RUMP=${RUMPOBJ}/rump
;;
"p")
SIZE=$(bytes ${OPTARG})
HUGEPAGESIZE="-DHUGEPAGESIZE=${SIZE}"
;;
"q")
BUILD_QUIET=${BUILD_QUIET:=-}q
;;
"r")
DBG_F="-O2"
appendvar EXTRAFLAGS "-r"
;;
"s")
RUMPSRC=${OPTARG}
;;
"V")
appendvar EXTRAFLAGS "-V ${OPTARG}"
;;
"?")
helpme
;;
esac
done
shift $((${OPTIND} - 1))
_cleanup()
{
cat ${LOG_FILE} 1>&3
rm -f ${LOG_FILE}
}
if [ -z ${BUILD_QUIET} ] ; then
set -x
elif [ "${BUILD_QUIET}" = "-qq" ] ; then
LOG_FILE=$(mktemp)
exec 3>&1 1>>/dev/null 2>${LOG_FILE}
trap _cleanup EXIT
fi
if [ -z ${BINDIR+x} ]; then BINDIR=${OUTDIR}/bin; fi
for arg in "$@"; do
case ${arg} in
"clean")
${MAKE} clean
;;
"noseccomp")
;;
"seccomp")
appendvar FILTER "-DSECCOMP"
appendvar TOOLS_LDLIBS "-lseccomp"
;;
"noexecveat")
;;
"execveat")
appendvar FILTER "-DEXECVEAT"
;;
"nocapsicum")
FILTER="-DNOCAPSICUM"
;;
"capsicum")
FILTER="-DCAPSICUM"
;;
"deterministic"|"det")
DETERMINISTIC="deterministic"
;;
"test"|"tests")
RUNTESTS="test"
;;
"notest"|"notests")
RUNTESTS="notest"
;;
"tools")
MAKETOOLS="yes"
;;
"notools")
MAKETOOLS="no"
;;
*)
OS=${arg}
;;
esac
done
set -e
if [ "${OS}" = "unknown" ]; then
die "Unknown or unsupported platform"
fi
[ -f platform/${OS}/platform.sh ] && . platform/${OS}/platform.sh
[ -f rumpkernel/${RUMP_KERNEL}.sh ] && . rumpkernel/${RUMP_KERNEL}.sh
RUNTESTS="${RUNTESTS-test}"
MAKETOOLS="${MAKETOOLS-yes}"
rm -rf ${OUTDIR}
FRANKEN_CFLAGS="-std=c99 -Werror -Wextra -Wno-missing-braces -Wno-unused-parameter -Wno-missing-field-initializers"
if [ "${HOST}" = "Linux" ]; then
appendvar FRANKEN_CFLAGS "-D_GNU_SOURCE"
appendvar TOOLS_LDSTATIC "-static"
fi
if [ "${HOST}" = "FreeBSD" ]; then appendvar FRANKEN_CFLAGS "-D_BSD_SOURCE"; fi
write_log "-n" "building tools.."
CPPFLAGS="${EXTRA_CPPFLAGS} ${FILTER}" \
CFLAGS="${EXTRA_CFLAGS} ${DBG_F} ${FRANKEN_CFLAGS}" \
LDFLAGS="${EXTRA_LDFLAGS}" \
LDLIBS="${TOOLS_LDLIBS}" \
LDSTATIC="${TOOLS_LDSTATIC}" \
RUMPOBJ="${RUMPOBJ}" \
RUMP="${RUMP}" \
${MAKE} ${OS} -C tools
write_log " done"
# call buildrump.sh
write_log "-n" "building rumpkernel.."
rumpkernel_buildrump
write_log " done"
# remove libraries that are not/will not work
rm -f ${RUMP}/lib/librumpdev_ugenhc.a
rm -f ${RUMP}/lib/librumpfs_syspuffs.a
rm -f ${RUMP}/lib/librumpkern_sysproxy.a
rm -f ${RUMP}/lib/librumpnet_shmif.a
rm -f ${RUMP}/lib/librumpnet_sockin.a
rm -f ${RUMP}/lib/librumpvfs_fifofs.a
rm -f ${RUMP}/lib/librumpdev_netsmb.a
rm -f ${RUMP}/lib/librumpfs_smbfs.a
rm -f ${RUMP}/lib/librumpdev_usb.a
rm -f ${RUMP}/lib/librumpdev_ucom.a
rm -f ${RUMP}/lib/librumpdev_ulpt.a
rm -f ${RUMP}/lib/librumpdev_ubt.a
rm -f ${RUMP}/lib/librumpkern_sys_linux.a
rm -f ${RUMP}/lib/librumpdev_umass.a
# remove crypto for now as very verbose
rm -f ${RUMP}/lib/librumpkern_crypto.a
rm -f ${RUMP}/lib/librumpdev_opencrypto.a
rm -f ${RUMP}/lib/librumpdev_cgd.a
# userspace libraries to build from NetBSD base
USER_LIBS="m pthread z crypt util prop rmt ipsec"
NETBSDLIBS="${RUMPSRC}/lib/libc"
for f in ${USER_LIBS}
do
appendvar NETBSDLIBS "${RUMPSRC}/lib/lib${f}"
done
RUMPMAKE=${RUMPOBJ}/tooldir/rumpmake
# build userspace library for rumpkernel
write_log "-n" "building userspace libs.."
rumpkernel_createuserlib
write_log " done"
write_log "-n" "building userspace headers.."
# permissions set wrong
chmod -R ug+rw ${RUMP}/include/*
# install headers
${INSTALL-install} -d ${OUTDIR}/include
# install headers of userspace lib
rumpkernel_install_header
write_log " done"
write_log "-n" "building platform libs.."
CFLAGS="${EXTRA_CFLAGS} ${DBG_F} ${HUGEPAGESIZE} ${FRANKEN_CFLAGS}" \
AFLAGS="${EXTRA_AFLAGS} ${DBG_F}" \
ASFLAGS="${EXTRA_AFLAGS} ${DBG_F}" \
LDFLAGS="${EXTRA_LDFLAGS}" \
CPPFLAGS="${EXTRA_CPPFLAGS}" \
RUMPOBJ="${RUMPOBJ}" \
RUMP="${RUMP}" \
${MAKE} ${STDJ} ${OS} -C platform
# should clean up how deterministic build is done
if [ ${DETERMINSTIC-x} = "deterministic" ]
then
CFLAGS="${EXTRA_CFLAGS} ${DBG_F} ${HUGEPAGESIZE} ${FRANKEN_CFLAGS}" \
AFLAGS="${EXTRA_AFLAGS} ${DBG_F}" \
ASFLAGS="${EXTRA_AFLAGS} ${DBG_F}" \
LDFLAGS="${EXTRA_LDFLAGS}" \
CPPFLAGS="${EXTRA_CPPFLAGS}" \
RUMPOBJ="${RUMPOBJ}" \
RUMP="${RUMP}" \
${MAKE} deterministic -C platform
fi
write_log " done"
write_log "-n" "building franken libs.."
CFLAGS="${EXTRA_CFLAGS} ${DBG_F} ${HUGEPAGESIZE} ${FRANKEN_CFLAGS}" \
AFLAGS="${EXTRA_AFLAGS} ${DBG_F}" \
ASFLAGS="${EXTRA_AFLAGS} ${DBG_F}" \
LDFLAGS="${EXTRA_LDFLAGS}" \
CPPFLAGS="${EXTRA_CPPFLAGS} ${FRANKEN_FLAGS}" \
RUMPOBJ="${RUMPOBJ}" \
RUMP="${RUMP}" \
${MAKE} ${STDJ} -C franken
write_log " done"
write_log "-n" "building rumpuser libs.."
CFLAGS="${EXTRA_CFLAGS} ${DBG_F} ${FRANKEN_CFLAGS}" \
LDFLAGS="${EXTRA_LDFLAGS}" \
CPPFLAGS="${EXTRA_CPPFLAGS} ${RUMPUSER_FLAGS}" \
RUMPOBJ="${RUMPOBJ}" \
RUMP="${RUMP}" \
${MAKE} ${STDJ} -C librumpuser
write_log " done"
# build extra library
write_log "-n" "building extra files.."
rumpkernel_build_extra
write_log "done"
write_log "-n" "building special libc for cross build.."
# find which libs we should link
ALL_LIBS="${RUMP}/lib/librump.a
${RUMP}/lib/libfranken_tc.a
${RUMP}/lib/librumpdev*.a
${RUMP}/lib/librumpnet*.a
${RUMP}/lib/librumpfs*.a
${RUMP}/lib/librumpvfs*.a
${RUMP}/lib/librumpkern*.a"
if [ ! -z ${LIBS+x} ]
then
ALL_LIBS="${RUMP}/lib/librump.a ${RUMP}/lib/libfranken_tc.a"
for l in $(echo ${LIBS} | tr "," " ")
do
case ${l} in
dev_*)
appendvar ALL_LIBS "${RUMP}/lib/librumpdev.a"
;;
net_*)
appendvar ALL_LIBS "${RUMP}/lib/librumpnet.a ${RUMP}/lib/librumpnet_net.a"
;;
vfs_*)
appendvar ALL_LIBS "${RUMP}/lib/librumpvfs.a"
;;
fs_*)
appendvar ALL_LIBS "${RUMP}/lib/librumpvfs.a ${RUMP}/lib/librumpdev.a ${RUMP}/lib/librumpdev_disk.a"
;;
esac
appendvar ALL_LIBS "${RUMP}/lib/librump${l}.a"
done
fi
# for Linux case
if [ ${RUMP_KERNEL} != "netbsd" ]
then
ALL_LIBS=${RUMPOBJ}/linux/tools/lkl/liblkl.a
fi
# explode and implode
rm -rf ${RUMPOBJ}/explode
mkdir -p ${RUMPOBJ}/explode/libc
mkdir -p ${RUMPOBJ}/explode/musl
mkdir -p ${RUMPOBJ}/explode/rumpkernel
mkdir -p ${RUMPOBJ}/explode/rumpuser
mkdir -p ${RUMPOBJ}/explode/franken
mkdir -p ${RUMPOBJ}/explode/platform
(
# explode rumpkernel specific libc
rumpkernel_explode_libc
# some franken .o file names conflict with libc
cd ${RUMPOBJ}/explode/franken
${AR-ar} x ${RUMP}/lib/libfranken.a
for f in *.o
do
[ -f ../${LIBC_DIR}/$f ] && mv $f franken_$f
done
# some platform .o file names conflict with libc
cd ${RUMPOBJ}/explode/platform
${AR-ar} x ${RUMP}/lib/libplatform.a
for f in *.o
do
[ -f ../libc/$f ] && mv $f platform_$f
done
cd ${RUMPOBJ}/explode/rumpkernel
for f in ${ALL_LIBS}
do
${AR-ar} x $f
done
${CC-cc} ${EXTRA_LDFLAGS} ${LDFLAGS_NO_PIE} -nostdlib -Wl,-r *.o -o rumpkernel.o
cd ${RUMPOBJ}/explode/rumpuser
${AR-ar} x ${RUMP}/lib/librumpuser.a
cd ${RUMPOBJ}/explode
${AR-ar} cr libc.a rumpkernel/rumpkernel.o rumpuser/*.o ${LIBC_DIR}/*.o franken/*.o platform/*.o
if [ "${HOST}" = "Linux" ] && [ "${OS}" != "qemu-arm" ]; then
LDFLAGS_LIBCSO="-Wl,-e,_dlstart -nostdlib -shared"
# XXX: aarch64-gcc has an issue w/ gold
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=830540
MACHINE=$(${CC:-gcc} -dumpmachine)
if [ ${MACHINE} != "aarch64-linux-gnu" ] ; then
appendvar LDFLAGS_LIBCSO "-fuse-ld=gold"
fi
if [ "${CC}" = "arm-linux-gnueabihf-gcc" ] ; then
appendvar LDFLAGS_LIBCSO "-static"
fi
${CC-cc} $LDFLAGS_LIBCSO -o libc.so \
rumpkernel/rumpkernel.o rumpuser/*.o ${LIBC_DIR}/*.o franken/*.o \
platform/*.o ${LIBC_DIR}/*.lo -lgcc -lgcc_eh
fi
)
write_log " done"
write_log "-n" "installing from staging directory.."
# install to OUTDIR
${INSTALL-install} -d ${BINDIR} ${OUTDIR}/lib
${INSTALL-install} ${RUMP}/bin/rexec ${BINDIR}
# call rumpkernel specific routine
rumpkernel_install_extra_libs
${INSTALL-install} ${RUMP}/lib/*.o ${OUTDIR}/lib
[ -f ${RUMP}/lib/libg.a ] && ${INSTALL-install} ${RUMP}/lib/libg.a ${OUTDIR}/lib
${INSTALL-install} ${RUMPOBJ}/explode/libc.a ${OUTDIR}/lib
if [ "${HOST}" = "Linux" ] && [ "${OS}" != "qemu-arm" ]; then
${INSTALL-install} ${RUMPOBJ}/explode/libc.so ${OUTDIR}/lib
LDSO_PATHNAME="${OUTDIR}/lib/ld-frankenlibc-x86_64-linux.so.1"
WD=`pwd`; cd ${OUTDIR}/lib/; ln -sf libc.so ${LDSO_PATHNAME}; cd ${WD}
fi
# create toolchain wrappers
# select these based on compiler defs
if $(${CC-cc} -v 2>&1 | grep -q clang)
then
TOOL_PREFIX=$(basename $(ls ${RUMPOBJ}/tooldir/bin/*-clang) | \
sed -e 's/-clang//' -e "s/--netbsdelf-/-rumprun-${RUMP_KERNEL}-/" \
-e "s/--netbsd/-rumprun-${RUMP_KERNEL}/" -e "s/-eabihf//")
# possibly some will need to be filtered if compiler complains. Also de-dupe.
COMPILER_FLAGS="-fno-stack-protector -Wno-unused-command-line-argument ${EXTRA_CPPFLAGS} ${UNDEF} ${EXTRA_CFLAGS} ${EXTRA_LDSCRIPT_CC}"
appendvar COMPILER_FLAGS "-isystem ${OUTDIR}/include -nostdinc"
if [ ${OS} = "darwin" ] ; then appendvar COMPILER_FLAGS "-lcrt1.o -lc -nostdlib" ; fi
COMPILER_CXX_FLAGS="-isystem ${OUTDIR}/include/c++/v1 -D_GNU_SOURCE"
COMPILER_FLAGS="$(echo ${COMPILER_FLAGS} | sed 's/--sysroot=[^ ]*//g')"
# set up sysroot to see if it works
( cd ${OUTDIR} && ln -s . usr )
LIBGCC="$(${CC-cc} ${EXTRA_CPPFLAGS} ${EXTRA_CFLAGS} -print-libgcc-file-name)"
LIBGCCDIR="$(dirname ${LIBGCC})"
CLANG_PRINT_SYSROOT="if [ \"X\$1\" = \"X-print-sysroot\" ] ; then echo ${OUTDIR} ; exit 0; fi"
ln -s ${LIBGCC} ${OUTDIR}/lib/
ln -s ${LIBGCCDIR}/libgcc_eh.a ${OUTDIR}/lib/
if ${CC-cc} -I${OUTDIR}/include --sysroot=${OUTDIR} -static ${COMPILER_FLAGS} tests/hello.c -o /dev/null 2>/dev/null
then
# can use sysroot with clang
printf "#!/bin/sh\n\n${CLANG_PRINT_SYSROOT} \n\nexec ${CC-cc} --sysroot=${OUTDIR} -static ${COMPILER_FLAGS} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-clang
printf "#!/bin/sh\n\n${CLANG_PRINT_SYSROOT} \n\nexec ${CC-c++} --sysroot=${OUTDIR} -static ${COMPILER_CXX_FLAGS} ${COMPILER_FLAGS} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-clang++
else
# sysroot does not work with linker eg NetBSD
appendvar COMPILER_FLAGS "-I${OUTDIR}/include -L${OUTDIR}/lib -lcrt1.o -B${OUTDIR}/lib"
appendvar COMPILER_FLAGS "-nostdinc -lc -nostdlib"
# macOS can't build with -static on arm64
if [ ${OS} = "darwin" ] ; then
if [ `arch` = "i386" ] ; then
appendvar COMPILER_FLAGS "-static"
elif [ `arch` = "arm64" ] ; then
appendvar COMPILER_FLAGS "-Wl,-e,_start -framework System"
fi
fi
printf "#!/bin/sh\n\n${CLANG_PRINT_SYSROOT} \n\nexec ${CC-cc} ${COMPILER_FLAGS} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-clang
printf "#!/bin/sh\n\n${CLANG_PRINT_SYSROOT} \n\nexec ${CC-c++} ${COMPILER_CXX_FLAGS} ${COMPILER_FLAGS} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-clang++
fi
COMPILER="${TOOL_PREFIX}-clang"
( cd ${BINDIR}
ln -s ${COMPILER} ${TOOL_PREFIX}-cc
ln -s ${COMPILER} rumprun-cc
ln -s ${TOOL_PREFIX}-clang++ ${TOOL_PREFIX}-c++
ln -s ${TOOL_PREFIX}-clang++ rumprun-c++
)
else
# spec file for gcc
TOOL_PREFIX=$(basename $(ls ${RUMPOBJ}/tooldir/bin/*-gcc) | \
sed -e 's/-gcc//' -e "s/--netbsdelf-/-rumprun-${RUMP_KERNEL}-/" \
-e "s/--netbsd/-rumprun-${RUMP_KERNEL}/" -e "s/-eabihf//")
COMPILER_FLAGS="-fno-stack-protector ${EXTRA_CFLAGS}"
COMPILER_FLAGS="$(echo ${COMPILER_FLAGS} | sed 's/--sysroot=[^ ]*//g')"
[ -f ${OUTDIR}/lib/crt0.o ] && appendvar STARTFILE "${OUTDIR}/lib/crt0.o"
[ -f ${OUTDIR}/lib/crt1.o ] && appendvar STARTFILE "${OUTDIR}/lib/crt1.o"
appendvar STARTFILE "${OUTDIR}/lib/crti.o"
[ -f ${OUTDIR}/lib/crtbegin.o ] && appendvar STARTFILE "${OUTDIR}/lib/crtbegin.o"
[ -f ${OUTDIR}/lib/crtbeginT.o ] && appendvar STARTFILE "${OUTDIR}/lib/crtbeginT.o"
ENDFILE="${OUTDIR}/lib/crtend.o ${OUTDIR}/lib/crtn.o"
cat tools/spec.in | sed \
-e "s#@SYSROOT@#${OUTDIR}#g" \
-e "s#@CPPFLAGS@#${EXTRA_CPPFLAGS}#g" \
-e "s#@AFLAGS@#${EXTRA_AFLAGS}#g" \
-e "s#@CFLAGS@#${EXTRA_CFLAGS}#g" \
-e "s#@LDFLAGS@#${EXTRA_LDFLAGS}#g" \
-e "s#@LDSCRIPT@#${EXTRA_LDSCRIPT}#g" \
-e "s#@UNDEF@#${UNDEF}#g" \
-e "s#@STARTFILE@#${STARTFILE}#g" \
-e "s#@ENDFILE@#${ENDFILE}#g" \
-e "s#@LDSO@#${LDSO_PATHNAME}#g" \
-e "s/--sysroot=[^ ]*//" \
> ${OUTDIR}/lib/${TOOL_PREFIX}gcc.spec
printf "#!/bin/sh\n\nexec ${CC-cc} -specs ${OUTDIR}/lib/${TOOL_PREFIX}gcc.spec ${COMPILER_FLAGS} -nostdinc -isystem ${OUTDIR}/include \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-gcc
COMPILER="${TOOL_PREFIX}-gcc"
( cd ${BINDIR}
ln -s ${COMPILER} ${TOOL_PREFIX}-cc
ln -s ${COMPILER} rumprun-cc
)
fi
printf "#!/bin/sh\n\nexec ${AR-ar} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-ar
printf "#!/bin/sh\n\nexec ${NM-nm} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-nm
printf "#!/bin/sh\n\nexec ${LD-ld} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-ld
printf "#!/bin/sh\n\nexec ${OBJCOPY-objcopy} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-objcopy
printf "#!/bin/sh\n\nexec ${OBJDUMP-objdump} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-objdump
printf "#!/bin/sh\n\nexec ${RANLIB-ranlib} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-ranlib
printf "#!/bin/sh\n\nexec ${READELF-readelf} \"\$@\"\n" > ${BINDIR}/${TOOL_PREFIX}-readelf
chmod +x ${BINDIR}/${TOOL_PREFIX}-*
mkdir -p ${OUTDIR}/share
cat tools/toolchain.cmake.in | sed \
-e "s#@TRIPLE@#${TOOL_PREFIX}#g" \
-e "s#@BINDIR@#${BINDIR}#g" \
-e "s#@OUTDIR@#${OUTDIR}#g" \
> ${OUTDIR}/share/${TOOL_PREFIX}-toolchain.cmake
write_log " done"
write_log "-n" "testing duplicated syms.."
# test for duplicated symbols
DUPSYMS=$(nm ${OUTDIR}/lib/libc.a | grep ' T ' | sed 's/.* T //g' | sort | uniq -d )
if [ -n "${DUPSYMS}" ]
then
printf "WARNING: Duplicate symbols found:\n"
echo ${DUPSYMS}
#exit 1
fi
# install some useful applications
if [ ${MAKETOOLS} = "yes" ]
then
rumpkernel_maketools
fi
write_log " done"
if $(${CC-cc} -v 2>&1 | grep -q clang)
then
write_log "-n" "building libcxx.."
rumpkernel_install_libcxx
write_log " done"
fi
write_log "-n" "building tests.."
# Always make tests to exercise compiler
CC="${BINDIR}/${COMPILER}" \
CXX="${BINDIR}/${COMPILER}++" \
RUMPDIR="${OUTDIR}" \
RUMPOBJ="${RUMPOBJ}" \
BINDIR="${BINDIR}" \
${MAKE} ${STDJ} -C tests
# test for executable stack
if [ ${OS} != "darwin" ]
then
readelf -lW ${RUMPOBJ}/tests/hello | grep RWE 1>&2 && echo "WARNING: writeable executable section (stack?) found" 1>&2
fi
rumpkernel_build_test
write_log " done"
write_log "-n" "running tests.."
if [ ${RUNTESTS} = "test" ]
then
CC="${BINDIR}/${COMPILER}" \
CXX="${BINDIR}/${COMPILER}++" \
RUMPDIR="${OUTDIR}" \
RUMPOBJ="${RUMPOBJ}" \
BINDIR="${BINDIR}" \
${MAKE} -C tests run
fi
write_log " done"