-
Notifications
You must be signed in to change notification settings - Fork 13
/
crossdev
executable file
·1649 lines (1497 loc) · 49.5 KB
/
crossdev
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright 1999-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
CROSSDEV_VER="@CDEVPV@"
cd /
umask 0022 #159111
: ${ROOT:=/}
if [[ ${ROOT} != "/" ]] ; then
echo "Sorry, but crossdev does not support ROOT." 1>&2
exit 2
fi
EPREFIX="@GENTOO_PORTAGE_EPREFIX@"
if [[ ${EPREFIX} == "@"GENTOO_PORTAGE_EPREFIX"@" ]] ; then
EPREFIX=""
fi
EROOT=${ROOT%/}"${EPREFIX}"/
: ${PORTAGE_CONFIGROOT:=${EROOT}}
CONFIGROOT="${PORTAGE_CONFIGROOT}/etc/portage"
source "${EPREFIX}"/lib/gentoo/functions.sh || exit 1
esyslog() { :; }
die_logs() {
echo
eerror "error: $1"
shift
local log
eerror
eerror "If you file a bug, please attach the following logfiles:"
eerror "${PORT_LOGDIR}/cross-${CTARGET}-info.log"
for log in "$@" ; do
eerror "${log}"
done
exit 1
}
die() { die_logs "$*"; }
has() { [[ " ${@:2} " == *" $1 "* ]]; }
xmkdir() { mkdir "$@" || die "could not mkdir $*"; }
usage() {
local exit_status=${1:-0}
shift
cat << EOF
Usage: ${HILITE}crossdev${NORMAL} ${GOOD}[options]${NORMAL} ${BRACKET}--target TARGET${NORMAL}
Options:
${GOOD}--b, --binutils${NORMAL} ver Specify version of binutils to use
${GOOD}--g, --gcc${NORMAL} ver Specify version of gcc to use
${GOOD}--k, --kernel${NORMAL} ver Specify version of kernel headers to use
${GOOD}--l, --libc${NORMAL} ver Specify version of libc to use
Note: versions support depend atom syntaxes:
e.g. ">=2.20" "~4.6.1" "=2.13.1-r3"
${GOOD}-A, --abis${NORMAL} abis Specify ABIs to build, first one is the default
${GOOD}--host-abi abi${NORMAL} Specify the ABI of the compiler itself
${GOOD}--env${NORMAL} env Specify env settings for all packages (see below)
${GOOD}--[bdgkl]env${NORMAL} env Specify env settings for binutils/gdb/gcc/kernel/libc
Note: make sure to quote: 'VAR="some value"\nFOO="cow"'
${GOOD}--[bdgkl]cat${NORMAL} category Use binutils/gdb/gcc/kernel/libc package from category
${GOOD}--[bdgkl]pkg${NORMAL} pkg Use binutils/gdb/gcc/kernel/libc package with given name
${GOOD}-f, --force${NORMAL} I don't need a seat belt!
${GOOD}-S, --stable${NORMAL} Use latest stable versions as default
${GOOD}-C, --clean${NORMAL} target Uninstall specified target
${GOOD}-P, --portage${NORMAL} opts Options to pass to emerge (see emerge(1))
${GOOD}--with[out]-headers${NORMAL} Build C library headers before C compiler?
${GOOD}--show-fail-log${NORMAL} If the build fails, dump the failing log
Overlay Options:
${GOOD}-oS, --overlays${NORMAL} list Space delimited list of overlays to search
[default: \`portageq repositories_configuration\`]
${GOOD}-oO, --ov-output${NORMAL} path Parent directory of overlay to write crossdev package links
[default: uses repo with name 'crossdev', or
'cross-\${CTARGET}', or falls back to first
from --overlays list]
${GOOD}-ob, --ov-binutils${NORMAL} path Overlay for binutils ebuilds [default: search]
${GOOD}-od, --ov-gdb${NORMAL} path Overlay for gdb ebuilds [default: search]
${GOOD}-og, --ov-gcc${NORMAL} path Overlay for gcc ebuilds [default: search]
${GOOD}-ok, --ov-kernel${NORMAL} path Overlay for kernel ebuilds [default: search]
${GOOD}-ol, --ov-libc${NORMAL} path Overlay for C library ebuilds [default: search]
${GOOD}-ox, --ov-extra${NORMAL} path Overlay for extra packages [default: search]
Stage Options:
${GOOD}-s0, --stage0${NORMAL} Build just binutils
${GOOD}-s1, --stage1${NORMAL} Also build a bare C compiler (no C library/
C++/shared GCC libs/C++ exceptions/etc...)
${GOOD}-s2, --stage2${NORMAL} Also build kernel headers
${GOOD}-s3, --stage3${NORMAL} Also build the C library
${GOOD}-s4, --stage4${NORMAL} Also build a full compiler [default]
(shared libs GCC/various lang frontends/etc...)
External Tooling Options:
${GOOD}--show-target-cfg${NORMAL} Display target settings that crossdev will use
${GOOD}--init-target${NORMAL} Setup config/overlay/etc... files only
Extra Fun (must be run after above stages):
${GOOD}--ex-only${NORMAL} Skip the stage steps above
${GOOD}--ex-gcc${NORMAL} Build extra gcc targets (gcj/ada/etc...)
${GOOD}--ex-gdb${NORMAL} Build a cross gdb
${GOOD}--ex-pkg${NORMAL} pkg Build extra packages (may be used multiple times)
${BRACKET}Target (-t)${NORMAL} takes a tuple ${BRACKET}ARCHITECTURE-VENDOR-OS-LIBC${NORMAL}; see 'crossdev -t help'
EOF
[[ -n $* ]] && echo && eerror "Error: $*"
exit ${exit_status}
}
STAGE_BINUTILS=0
STAGE_C_ONLY=1
STAGE_C_KERNEL=2
STAGE_LIBC=3
STAGE_C_CPP=4
STAGE_DEFAULT=${STAGE_C_CPP}
STAGE_DISP=(
"binutils"
"C compiler only"
"kernel headers"
"C compiler & libc"
"C/C++ compiler"
)
parse_target() {
CTARGET=${1#cross-}
[[ -z ${CTARGET} ]] && usage 1
if [[ ${CTARGET} == "help" ]] ; then
cat <<-EOF
Target (-t) takes a tuple ARCHITECTURE-VENDOR-OS-LIBC:
Supported Architectures (ARCHITECTURE):
- alpha
- arm / armeb / aarch64
- hppa (parisc)
- ia64
- i386 / i486 / i586 / i686 (x86)
- loongarch64
- m68k
- mips / mipsel / mips64 / mips64el
- or1k
- powerpc (ppc) / powerpc64 (ppc64)
- riscv32 / riscv64
- sparc / sparc64
- s390 / s390x
- sh / sh[1-5] / sh64
- x86_64 (amd64)
Supported C Libraries (LIBC):
- glibc (gnu)
- klibc [prob wont work]
- musl
- newlib [bare metal/no operating system]
- uclibc [not all arches are ported]
Special Targets (full tuple):
- avr http://www.nongnu.org/avr-libc/
- bfin http://blackfin.uclinux.org/
- h8300 http://h8300-hms.sourceforge.net/
- mingw64 http://mingw-w64.sourceforge.net/
- mmix http://www-cs-faculty.stanford.edu/~knuth/mmix.html
- msp430 http://www.ti.com/msp430
- nds32 http://github.com/nds32
- nios2 http://www.altera.com/products/ip/processors/nios2/ni2-index.html
- xc16x http://www.infineon.com/
- ppu / spu (cell) [Cell/Playstation 3 targets]
Softfloat toolchains:
Include 'softfloat' in the 'vendor' field
e.g. armeb-softfloat-linux-uclibc powerpc-booya_softfloat-linux-gnu
EOF
exit 0
fi
# Allow for laziness
if [[ ${CTARGET} != *-* ]] ; then
# Handle aliases
local aliases=()
case ${CTARGET} in
cell) aliases=( ppu spu-elf );;
mingw64) aliases=( x86_64-w64-mingw32 );;
esac
if [[ ${#aliases[@]} -eq 1 ]] ; then
CTARGET=${aliases[0]}
elif [[ ${#aliases[@]} -gt 1 ]] ; then
einfo "The '${CTARGET}' target is really an alias for the '${aliases}' target(s)"
local alias
for alias in ${aliases} ; do
${CROSSDEV} -t ${alias} || exit 1
done
exit 0
fi
# First translate portage ARCH to actual tuple
case ${CTARGET} in
amd64) CTARGET="x86_64";;
parisc*) CTARGET=${CTARGET/parisc/hppa};;
ppc|ppc64) CTARGET=${CTARGET/ppc/powerpc};;
x86) CTARGET="i686";;
esac
# Then add common suffixes
case ${CTARGET} in
i?86|x86_64)
CTARGET="${CTARGET}-pc-linux-gnu";;
s390*)
CTARGET="${CTARGET}-ibm-linux-gnu";;
arm64*)
CTARGET="aarch${CTARGET#arm}-unknown-linux-gnu";;
armv[67]*)
CTARGET="${CTARGET}-unknown-linux-gnueabihf";;
arm*)
CTARGET="${CTARGET}-unknown-linux-gnueabi";;
aarch64*|alpha*|cris*|hppa*|ia64*|loongarch*|m68*|mips*|powerpc*|riscv*|sparc*|sh*|tile*)
CTARGET="${CTARGET}-unknown-linux-gnu";;
bfin*|h8300*|msp430*|nds32*|nios2*|spu*|xc16x*)
CTARGET="${CTARGET}-elf";;
esac
fi
# Figure out an acceptable portage ARCH for this target
case ${CTARGET} in
aarch64*) TARCH=arm64;;
alpha*) TARCH=alpha;;
arm*) TARCH=arm;;
hppa*) TARCH=hppa;;
ia64*) TARCH=ia64;;
i?86*) TARCH=x86;;
loongarch*) TARCH=loong;;
m68*) TARCH=m68k;;
mips*) TARCH=mips;;
powerpc64*) TARCH=ppc64;;
powerpc*) TARCH=ppc;;
sparc*) TARCH=sparc;;
s390*) TARCH=s390;;
sh*) TARCH=sh;;
x86_64*) TARCH=amd64;;
*) TARCH="*";;
esac
case ${CTARGET} in
*-freebsd*) TARCH="${TARCH}-fbsd";;
esac
# Now account for all the "special" architectures out there
case ${CTARGET} in
# this is a linux+ target, not microcontroller (below)
avr32*) :;;
avr*) KPKG="[none]"
LCAT="dev-embedded"
LPKG="avr-libc"
GUSE+=" -fortran -go" # doesn't work
MULTILIB_USE="yes" #377039
BUSE+=" cxx"
STAGE_DEFAULT=${STAGE_LIBC}
WITH_DEF_HEADERS="no";;
# Has no glibc support yet (or even ABI defined). Can
# only compile linux kernel:
# https://parisc.wiki.kernel.org/index.php/Userspace64
hppa64*)
STAGE_DEFAULT=${STAGE_C_ONLY}
WITH_DEF_HEADERS="no";;
ia64*)
# gcc's libgcc needs glibc headers for unwinder.
# Thus gcc-stage1 can't be built without glibc headers.
WITH_DEF_HEADERS="yes"
;;
loongarch*)
# have to bring in the experimental LoongArch patchset before
# everything is upstream
KUSE+=" experimental-loong"
LUSE+=" experimental-loong"
;;
# added in bug #609602
mmix*)
KPKG="[none]"
LPKG="newlib"
STAGE_DEFAULT=${STAGE_LIBC}
WITH_DEF_HEADERS="no";;
# due to upstream lameness, build C/C++ at first glance
*-cygwin)
GUSE_DISABLE_STAGE_1+=" -nocxx cxx";;
# these are the mingw64 targets that binutils seems to use
x86_64-*-mingw*|*-w64-mingw*)
KPKG="[none]";
LCAT="dev-util"; LPKG="mingw64-runtime"
WITH_DEF_HEADERS="yes" # gcc can't boot without headers: bug #693770
GUSE+=" -hardened" # gcc ICEs as libssp/ssp.c:186:1: internal compiler error: in seh_emit_stackalloc
;;
mingw*|*-mingw*)
# bug #584858
die "'${CTARGET}' target is not supported anymore, use i686-w64-mingw32"
;;
spu*) TARCH=ppc64;
KPKG="[none]";
LPKG="newlib";;
ppu*) TARCH=ppc64;;
mips64*-gnuabin32|mipsisa64*-gnuabin32)
[[ ${MULTILIB_ABIS} == "default" ]] && MULTILIB_ABIS="n32";;
mips64*-gnuabi64|mipsisa64*-gnuabi64|\
mips64*-gnuabin64|mipsisa64*-gnuabin64)
[[ ${MULTILIB_ABIS} == "default" ]] && MULTILIB_ABIS="n64";;
mips64*-gnuabi32|mipsisa64*-gnuabi32|\
mips64*-gnuabio32|mipsisa64*-gnuabio32)
[[ ${MULTILIB_ABIS} == "default" ]] && MULTILIB_ABIS="o32";;
esac
# Tweak packages based upon CTARGET
case ${CTARGET} in
# Normal Linux host, just diff libc
*-dietlibc) LPKG="dietlibc"; LCAT="dev-libs";;
*-gnu*) LPKG="glibc";;
*-klibc) LPKG="klibc";;
*-uclibc*) LPKG="uclibc-ng";;
*-uclinux) LPKG="uclibc-ng";;
*-musl*) LPKG="musl";;
# Windows targets
*-cygwin)
LCAT="dev-libs"; LPKG="cygwin";
KPKG="[none]";
;;
# Bare metal targets
*-newlib|*-elf|*-eabi|*-rtems*)
LPKG="newlib"
KPKG="[none]"
STAGE_DEFAULT=${STAGE_LIBC}
GUSE+=" cxx -openmp" #489798
GUSE+=" -fortran" #589672, needs syscalls
GUSE+=" -hardened" #687598, needs -fstack-check=specific support
MULTILIB_USE="yes" #407275
WITH_DEF_HEADERS="no"
;;
# Now for the BSDs ...
*-freebsd*)
LCAT="sys-freebsd" LPKG="freebsd-lib"
KPKG="[none]"
;;
esac
# Target-specific defaults (workarounds for upstream bugs). User
# can override them by passing 'USE=foo crossdev ...'. Useful when
# one wants to fix upstream bugs.
local sanitizer_support=no
# Whitelist asan on explicitly supported arches for linux.
# Broken examples:
# - musl libc
# - bare metal targets
# Untested examples:
# - *BSD
case ${CTARGET} in
# glibc targets
*-gnu*)
case ${CTARGET} in
mips64*|mipsisa64*)
# has some support code, fails to build
;;
x86_64*|i?86*|arm*|aarch64*|powerpc*)
sanitizer_support=yes
;;
esac
;;
esac
if [[ $sanitizer_support = "no" ]]; then
GUSE+=" -sanitize"
fi
local vtv_support=no
# Whitelist vtv on explicitly supported arches for linux.
# Broken examples:
# - musl libc
# - bare metal targets
# - powerpc
# Untested examples:
# - *BSD
case ${CTARGET} in
# glibc targets
*-gnu*)
case ${CTARGET} in
x86_64*|i?86*|arm*|aarch64)
vtv_support=yes
;;
esac
;;
esac
if [[ $vtv_support = "no" ]]; then
GUSE+=" -vtv"
fi
local pie_support=yes
# Blacklist rare targets that:
# 1. don't support -fPIC in compiler
# 2. have -fPIE broken
case ${CTARGET} in
# [1.]: no -fPIC flag support:
# check as '$CC -fPIC -c -x c - </dev/null'
avr*|mmix*) pie_support=no;;
# [2.] mingw32 startup code is broken: bug #644930
# at least on i686-w64-mingw32 and x86_64-w64-mingw32
mingw*|*-mingw*) pie_support=no;;
# Many bare-metal targets don't work with pie as-is
*-elf|*-eabi)
# mips can't generate freestanding PIC:
# cc1: error: position-independent code requires ‘-mabicalls’
# arm firmware packages don't expect pie-by-default:
# https://lists.gnupg.org/pipermail/gnuk-users/2018-August/000086.html
# https://github.com/gl-sergei/u2f-token/issues/14
pie_support=no
;;
esac
# Running a hardened profile on the host forces pie #831165
if [[ $pie_support = "no" ]]; then
# pie is >=gcc-6, nopie is <gcc-6
GMASK+=" pie -nopie"
GFORCE+=" nopie"
fi
local ssp_support=yes
# Blacklist rare targets that:
# 1. don't support -fstack-protector in compiler
# 2. have -fstack-protector broken
case ${CTARGET} in
# [1.]: no -fstack-protector flag support:
# check as '$CC -fstack-protector -c -x c - </dev/null'
alpha*|avr*|hppa*|ia64*|mmix*|nios2*) ssp_support=no;;
esac
# Running a hardened profile on the host forces ssp #831165
if [[ $ssp_support = "no" ]]; then
# ssp is >=gcc-6, nossp is <gcc-6
# blacklist hardened too because it'll force -fstack-clash-protection
GMASK+=" ssp hardened -nossp"
GFORCE+=" nossp"
fi
}
parse_repo_config() {
# Ugh, ini parser here we come.
# [DEFAULT]
# main-repo = gentoo
# [crossdev]
# location = /usr/local/portage
# masters = gentoo
# priority = 0
local repo_config=$(portageq repositories_configuration "${EROOT}")
local flat_config=$(echo "${repo_config}" | gawk '
function push(arr, idx, ele) {
if (idx in arr)
arr[idx][length(arr[idx]) + 1] = ele
else
arr[idx][0] = ele
}
{
if ($1 == "main-repo") {
main_repo = $NF
} else if ($1 ~ /^\[/) {
if (repo_name && loc)
push(repos, prio, repo_name ":" loc)
repo_name = gensub(/\[([^\]]*)\]/, "\\1", 1, $1)
loc = prio = ""
} else if ($1 == "priority") {
prio = $3
} else if ($1 == "location") {
loc = $3
}
}
END {
push(repos, prio, repo_name ":" loc)
print(main_repo)
asorti(repos, prios)
for (prio in prios)
for (repo in repos[prios[prio]])
print(repos[prios[prio]][repo])
}
')
MAIN_REPO_NAME=$(echo "${flat_config}" | head -1)
REPO_CONFIG=$(echo "${flat_config}" | sed 1d)
MAIN_REPO_PATH=$(echo "${REPO_CONFIG}" | sed -n "/^${MAIN_REPO_NAME}:/s,^[^:]*:,,p")
if [[ -z ${SEARCH_OVERLAYS} ]] ; then
# see if user gave us an overlay search list, otherwise
# default to whatever is configured in portage
SEARCH_OVERLAYS=$(echo "${REPO_CONFIG}" | sed -e "/^${MAIN_REPO_NAME}:/d" -e 's,^[^:]*:,,')
fi
}
# Used by --show-repo-cfg to share the parser logic with other tools.
show_repo_cfg() {
local var=$1
unset SEARCH_OVERLAYS
parse_repo_config
echo "${!var}"
exit 0
}
setup_portage_vars() {
local arch=${ARCH} arch_set=${ARCH+set}
local chost=${CHOST} chost_set=${CHOST+set}
local abi
unset ARCH CHOST
eval $(portageq envvar -v PORT_LOGDIR PORTAGE_TMPDIR ARCH CHOST)
parse_repo_config
# keep the original values, but be aware of the native ones
HARCH=${ARCH}
[[ ${arch_set} == "set" ]] && ARCH=${arch} || unset ARCH
HCHOST=${CHOST}
[[ ${chost_set} == "set" ]] && CHOST=${chost} || unset CHOST
# Collect all CHOST* visible for host. We need to make sure
# not to clobber any of native CHOSTs.
HCHOSTS=(${HCHOST})
for abi in $(portageq envvar ABI MULTILIB_ABIS DEFAULT_ABI); do
HCHOSTS+=($(portageq envvar CHOST_${abi}))
done
# uniq
HCHOSTS=(
$(printf "%s\n" "${HCHOSTS[@]}" | sort -u)
)
# see if user told us where to write things, otherwise
# set up some repos.conf magic if possible. if not,
# install our stuff to the first overlay in the list.
if [[ -z ${CROSSDEV_OVERLAY} ]] ; then
local repo_path repo_name
for repo_name in "cross-${CTARGET}" crossdev ; do
repo_path=$(echo "${REPO_CONFIG}" | sed -n "/^${repo_name}:/s,^[^:]*:,,p")
if [[ -n ${repo_path} ]] ; then
CROSSDEV_OVERLAY=${repo_path}
# if we end up installing our stuff, save the name of the repo
# for further use in metadata/layout.conf
CROSSDEV_OVERLAY_NAME=${repo_name}
break
fi
done
: ${CROSSDEV_OVERLAY:=$(echo "${SEARCH_OVERLAYS}" | head -1)}
else
# Check if there is a repos.conf entry whose
# location matches ${CROSSDEV_OVERLAY}
local repo_name conf_base i=""
repo_name="$(echo "${REPO_CONFIG}" | sed -n "\|:${CROSSDEV_OVERLAY}$|s,:.*,,p")"
if [[ -n ${repo_name} ]]; then
# Match the name specified in the corresponding repos.conf entry
CROSSDEV_OVERLAY_NAME="${repo_name}"
else
# Derive the name
: ${CROSSDEV_OVERLAY_NAME:="$(basename "${CROSSDEV_OVERLAY}")"}
# Note that there is no repos.conf entry and set up the path for the conf file
# We'll create the conf file itself in set_metadata()
conf_base="${CONFIGROOT}/repos.conf/${CROSSDEV_OVERLAY_NAME}"
while [[ -e "${conf_base}${i}.conf" ]]; do
# If there is a file collision with the conf file we target,
# we'll first append '1' before '.conf' and try.
# If that doesn't work, we'll retry with an incremented number
# Incrementing an empty string returns '1' in the shell
i=$((i + 1))
done
CROSSDEV_OVERLAY_CREATE_REPOS_CONF="${conf_base}${i}.conf"
fi
fi
# make sure we have a valid logdir
: ${PORT_LOGDIR:=${EPREFIX}/var/log/portage}
xmkdir -p "${PORT_LOGDIR}"
}
uninstall() {
local d f
setup_portage_vars
ewarn "Uninstalling target '${CTARGET}' ..."
# clean out portage config files
if [[ -d ${CROSSDEV_OVERLAY}/cross-${CTARGET} ]]; then
rm -r "${CROSSDEV_OVERLAY}"/cross-${CTARGET}
# if we remove all the package in the category,
# might as well remove the category itself
sed -e "/cross-${CTARGET}/d" \
-i "${CROSSDEV_OVERLAY}"/profiles/categories
fi
# If profiles/categories is empty, see if we can remove the output overlay entirely
# The conservative criteria for removal are as follows:
# - The profiles/categories is empty
# - metadata/layout.conf is managed by crossdev
# - The only files in the overlay are profiles/categories and metadata/layout.conf
# Otherwise, we leave the overlay alone
if [[ ! -s "${CROSSDEV_OVERLAY}"/profiles/categories ]]; then
# Check if layout.conf is managed by crossdev
if grep -qs "^${AUTOGEN_TAG}" "${CROSSDEV_OVERLAY}"/metadata/layout.conf; then
# Check that there are no other files
local i=0
while IFS="" read -d $'\0' -r; do
i=$((i + 1))
done < <(find "${CROSSDEV_OVERLAY}" -type f -print0)
# Remove the overlay if we can
[[ ${i} -eq 2 ]] && rm -r "${CROSSDEV_OVERLAY}"
fi
fi
# Remove the repos.conf entry if the output overlay is completely removed
if [[ ! -e "${CROSSDEV_OVERLAY}" ]]; then
# There is no easy way to reverse map an overlay entry
# to a particular file under repos.conf
# We'll scan every conf file for a match on repo name and
# remove if the file is managed by crossdev
while IFS="" read -d $'\0' -r f ; do
grep -qs "^${AUTOGEN_TAG}" "${f}" || continue
if grep -qs "^\[${CROSSDEV_OVERLAY_NAME}\]$" "${f}"; then
rm -f "${f}"
break
fi
done < <(find "${CONFIGROOT}"/repos.conf -type f -print0)
fi
# crossdev stopped creating 'package.keywords' in Jan 2020
for f in package.{accept_keywords,env,mask,keywords,use} profile/package.use.{force,mask} ; do
f="${CONFIGROOT}/${f}"
rm -f "${f}"/cross-${CTARGET}
rmdir "${f}" 2>/dev/null
done
rm -rf "${CONFIGROOT}"/env/cross-${CTARGET}
rmdir "${CONFIGROOT}"/env 2>/dev/null
rm -f "${EPREFIX}"/etc/revdep-rebuild/05cross-${CTARGET}
rmdir "${EPREFIX}"/etc/revdep-rebuild 2>/dev/null
# Unmerge all toolchain packages for this target.
emerge -q --rage-clean "cross-${CTARGET}/*"
# clean out known toolchain files (binutils/gcc)
for f in \
addr2line ar as c++filt dlltool dllwrap dwp embedspu \
gcc-{ar,nm,ranlib} gccbug gcov-{dump,tool} gprof ld nm \
objcopy objdump ranlib readelf size strings strip windmc windres
do
rm -f "${EPREFIX}"/usr/bin/${CTARGET}-${f}
done
rm -f "${EPREFIX}"/usr/${CTARGET}/{sys-include,usr}
rmdir "${EPREFIX}"/usr/${CTARGET}/{include/asm,include} 2>/dev/null
rm -f "${EPREFIX}"/usr/bin/${CTARGET}-{gcc,{c,g}++,cpp,gfortran,gcov}
rm -f "${EPREFIX}"/etc/env.d/{binutils,gcc}/config-${CTARGET}
# clean out files from crossdev itself
[[ -e ${EPREFIX}/var/db/pkg/cross-${CTARGET} ]] && rmdir "${EPREFIX}"/var/db/pkg/cross-${CTARGET}
rm -f "${EPREFIX}"/usr/bin/${CTARGET}-{emerge,fix-root,pkg-config} "${EPREFIX}"/usr/bin/emerge-${CTARGET}
for f in make.{conf,globals,profile} ; do
f="${EPREFIX}/usr/${CTARGET}/etc/${f}"
[[ -L ${f} ]] && rm -f ${f}
f="${EPREFIX}/usr/${CTARGET}/etc/portage/${f##*/}"
[[ -L ${f} ]] && rm -f ${f}
done
find "${EPREFIX}"/usr/share/crossdev/etc/ -type f | \
while read f ; do
f1=${f}
[[ ! -e ${f1} ]] && continue
m1=$(set -- `md5sum ${f1}`; echo $1)
f2=${EPREFIX}/usr/${CTARGET}${f#/usr/share/crossdev}
[[ ! -e ${f2} ]] && continue
m2=$(set -- `md5sum ${f2}`; echo $1)
if [[ ${m1} == ${m2} ]] ; then
rm -f ${f2}
fi
done
# clean out the sysroot, prompting the user if need be
for d in "${EPREFIX}"/usr/lib/gcc{,-lib}/${CTARGET} "${EPREFIX}"/usr/${CTARGET} ; do
if [[ ! -d ${d} ]] ; then
rm -f "${d}"
else
if [[ ${FORCE} == "no" ]] ; then
find "${d}" -type d -depth -exec rmdir {} + 2>/dev/null && continue
printf "${d}: directory still exists; remove recursively? [y/N] "
local ans
read ans
[[ ${ans} == [Yy]* ]] && rm -rf "${d}"
else
rm -rf "${d}"
fi
fi
done
}
set_withval() {
local withval varname
varname=${*#--with-}
varname=${varname#--without-}
varname=${varname%%=*}
if [[ $* == *=* ]] ; then
withval=${*#*=}
else
[[ $* == --with-* ]] && withval="yes" || withval="no"
fi
echo WITH_`echo ${varname} | tr '[:lower:]' '[:upper:]'`=\"${withval}\"
}
is_stage() { [[ ${STAGE} -ge $1 ]] ; }
is_s0() { is_stage 0 ; }
is_s1() { is_stage 1 ; }
is_s2() { is_stage 2 ; }
is_s3() { is_stage 3 ; }
is_s4() { is_stage 4 ; }
is_s5() { is_stage 5 ; }
with_headers() { [[ ${WITH_HEADERS} == "yes" ]] ; }
ex_fast() { [[ ${EX_FAST} == "yes" ]] ; }
ex_gcc() { [[ ${EX_GCC} == "yes" ]] ; }
ex_gdb() { [[ ${EX_GDB} == "yes" ]] ; }
ex_pkgs() { [[ ${#XPKGS[@]} -gt 0 ]] ; }
# For each extra package, call $@ with the X* vars set up properly.
for_each_extra_pkg() {
local pkg i
for (( i = 0; i < ${#XPKGS[@]}; ++i )) ; do
# Since the user gave us full atoms, pull them apart here.
pkg=${XPKGS[i]}
if [[ ${pkg} != */* ]]; then
usage 1 "arguments to --ex-pkg must be CATEGORY/PN"
fi
XCAT=${pkg%/*} \
XPKG=${pkg#*/} \
XVER=${XVERS[i]} \
XUSE=${XUSES[i]} \
XENV=${XENVS[i]} \
XOVL=${XOVLS[i]} \
XMASK=${XMASKS[i]} \
XFORCE=${XFORCES[i]} \
"$@"
done
}
hr() {
local c=${COLUMNS:-0}
if [[ ${c} -eq 0 ]] ; then
c=$(stty size 2> /dev/null)
[[ -z ${c} ]] \
&& c=50 \
|| c=${c##* }
fi
local ext=${1:- _ - ~ -}
local sext=${ext//?/ }
local br=$(printf "%$((c + ${#ext}))s")
local banner=${br//${sext}/${ext}}
echo "${banner:0:${c}}"
}
ver_get_op() {
local op ver=$1
[[ ${ver} == "["* ]] && return
op=${ver%%[0-9]*}
[[ -n ${op} ]] && echo "${op}"
}
ver_chop_op() {
local op ver=$1
op=$(ver_get_op "${ver}")
echo "${ver##${op}}"
}
pretty_atom() {
local mid=$1 ver=$2
printf '%s%s%s' "$(ver_get_op "${ver}")" "${mid}" "$(ver_chop_op "${ver}")"
}
##################
### setup vars ###
CROSSDEV=$0
EOPTS=
UOPTS=
TARCH=
HARCH=
HCHOST=
HCHOSTS=()
CTARGET=
MULTILIB_ABIS="default"
MULTILIB_USE=""
HOST_ABI="default"
STAGE=""
AENV=""
# Only GMASK/GFORCE are currently used
BCAT="sys-devel" ; BPKG="binutils" ; BVER="" BUSE="" BENV="" BOVL="" BMASK="" BFORCE=""
GCAT="sys-devel" ; GPKG="gcc" ; GVER="" GUSE="" GENV="" GOVL="" GMASK="" GFORCE=""
KCAT="sys-kernel" ; KPKG="linux-headers" ; KVER="" KUSE="" KENV="" KOVL="" KMASK="" KFORCE=""
LCAT="sys-libs" ; LPKG="[none]" ; LVER="" LUSE="" LENV="" LOVL="" LMASK="" LFORCE=""
DCAT="sys-devel" ; DPKG="gdb" ; DVER="" DUSE="" DENV="" DOVL="" DMASK="" DFORCE=""
XPKGS=() XVERS=() XUSES=() XENVS=() XOVLS=() XMASKS=() XFORCES=()
DEFAULT_VER="[latest]"
SEARCH_OVERLAYS=""
CROSSDEV_OVERLAY=""
CROSSDEV_OVERLAY_NAME=""
CROSSDEV_OVERLAY_CREATE_REPOS_CONF=""
AUTOGEN_TAG="# Autogenerated and managed by crossdev"
# These flags are always disabled for cross-gcc; either usually/always broken, or
# not tested, or doesn't make sense, or no one simply cares about them
GUSE_DISABLE="-boundschecking -d -gcj -gtk -libffi -mudflap -objc -objc++ -objc-gc"
# These are disabled only for stage1 gcc. Normally need libc presence.
GUSE_DISABLE_STAGE_1="${GUSE_DISABLE} -fortran -go -jit -cxx -mpx -openmp -sanitize -vtv"
# These are also disabled for stage2, but could be used later if dependencies
# are installed into ${SYSROOT}:
# - sanitize needs crypt.h: #799707
GUSE_DISABLE_STAGE_2="${GUSE_DISABLE} -sanitize"
# mingw64-runtime needs a stage2 compiler to build libraries: https://bugs.gentoo.org/751295
LUSE_DISABLE="-libraries"
# Past history of WITH_DEF_HEADERS:
# - ????-2005: "no"
# - 2005-2019: "yes": #227065 gcc-4.3+ is a pita w/out headers
# - 2019-????: "no" again: glibc does not really support headers install
# at least on riscv-*: #686248
WITH_HEADERS="COW"
WITH_DEF_HEADERS="no"
EX_FAST="no"
EX_GCC="no"
EX_GDB="no"
FORCE="no"
SET_X="no"
ACTION="install"
SHOW_FAIL_LOG="no"
SHOW_TARGET_CFG="no"
INIT_TARGET_ONLY="no"
while [[ $# -gt 0 ]] ; do
case $1 in
-V|--version) echo "crossdev-${CROSSDEV_VER}"; exit 0;;
-t|--target) shift; parse_target $1;;
--b|--binutils) shift; BVER=$1;;
--benv) shift; BENV=$1;;
-ob|--ov-binutils) shift; BOVL=$1;;
--bcat) shift; BCAT=$1;;
--bpkg) shift; BPKG=$1;;
--d|--gdb) shift; DVER=$1;;
--denv) shift; DENV=$1;;
-od|--ov-gdb) shift; DOVL=$1;;
--dcat) shift; DCAT=$1;;
--dpkg) shift; DPKG=$1;;
--g|--gcc) shift; GVER=$1;;
--genv) shift; GENV=$1;;
-og|--ov-gcc) shift; GOVL=$1;;
--gcat) shift; GCAT=$1;;
--gpkg) shift; GPKG=$1;;
--k|--kernel) shift; KVER=$1;;
--kenv) shift; KENV=$1;;
-ok|--ov-kernel) shift; KOVL=$1;;
--kcat) shift; KCAT=$1;;
--kpkg) shift; KPKG=$1;;
--l|--libc) shift; LVER=$1;;
--lenv) shift; LENV=$1;;
-ol|--ov-libc) shift; LOVL=$1;;
--lcat) shift; LCAT=$1;;
--lpkg) shift; LPKG=$1;;
-ox|--ov-extra) shift; XOVLS+=( "$1" );;
--env) shift; AENV=$1;;
-A|--abis) shift; MULTILIB_ABIS=$1;;
--host-abi) shift; HOST_ABI=$1;;
-S|--stable) DEFAULT_VER="[stable]";;
-C|--clean) shift; parse_target $1; ACTION="uninstall";;
-s?|--stage?) STAGE=${1:0-1};;
-oS|--overlays) shift; SEARCH_OVERLAYS=$1;;
-oO|--ov-output) shift; CROSSDEV_OVERLAY=$1;;
--ex-only) EX_FAST="yes";;
--ex-gcc) EX_GCC="yes";;
--ex-gdb) EX_GDB="yes";;
--ex-pkg) shift; XPKGS+=( "$1" );;
--with-*) eval $(set_withval $1);;
--without-*) eval $(set_withval $1);;
-f|--force) FORCE="yes";;
-x) SET_X="yes";;
--show-target-cfg) SHOW_TARGET_CFG="yes";;
--init-target) INIT_TARGET_ONLY="yes";;
--show-fail-log) SHOW_FAIL_LOG="yes";;
--show-repo-cfg) show_repo_cfg "$2";;
-P|--portage) UOPTS="${UOPTS} $2"; shift;;
-h|--help) usage;;
-*) eerror "UNKNOWN OPTION: '$1'" ; usage 1;;
*) parse_target $1;;
esac
shift
done
[[ ${SET_X} == "yes" ]] && set -x
case ${ACTION} in
uninstall) uninstall; exit 0;;
esac
BVER=${BVER:-${DEFAULT_VER}}
GVER=${GVER:-${DEFAULT_VER}}
KVER=${KVER:-${DEFAULT_VER}}
LVER=${LVER:-${DEFAULT_VER}}
DVER=${DVER:-${DEFAULT_VER}}
STAGE=${STAGE:-${STAGE_DEFAULT}}
[[ -z ${CTARGET} ]] && usage 1
for with in HEADERS ; do
var=WITH_${with} defvar=WITH_DEF_${with}
[[ ${!var} == "COW" ]] && eval ${var}=${!defvar}
done
# ::gentoo has no stable 'mingw64-runtime' or 'newlib'.
# Let's default to 'latest' unless user specified something else.
if [[ ${LPKG} == "mingw64-runtime" && ${LVER} == "[stable]" ]]; then
LVER="[latest]"
fi
if [[ ${LPKG} == "newlib" && ${LVER} == "[stable]" ]]; then
LVER="[latest]"
fi
show_target_cfg() {
local pkgs crosspkgs=()
pkgs=(
binutils B
gcc G
kernel K
libc L
)
ex_gdb && pkgs+=( gdb D )
if ex_pkgs ; then
show_extra_pkg() {
echo "ex_${XPKG}_category=${XCAT}"
echo "ex_${XPKG}_pn=${XPKG}"
crosspkgs+=( "ex_${XPKG}" )
}
for_each_extra_pkg show_extra_pkg
echo "extrapkgs='${crosspkgs[*]}'"
fi
echo "arch=${TARCH}"
echo "target=${CTARGET}"
echo "category=cross-${CTARGET}"
while [[ ${#pkgs[@]} -gt 0 ]] ; do
local pkg=${pkgs[0]}
local v=${pkgs[1]}
local vcat="${v}CAT"
local vpkg="${v}PKG"
local ocat="${pkg}_category"
local opkg="${pkg}_pn"
if [[ ${!vpkg} != "[none]" ]] ; then
echo "${ocat}=${!vcat}"
echo "${opkg}=${!vpkg}"
else
printf '%s=\n' "${ocat}" "${opkg}"
fi
crosspkgs+=( ${pkg} )
pkgs=( ${pkgs[@]:2} )
done
echo "crosspkgs='${crosspkgs[*]}'"
exit 0
}
[[ ${SHOW_TARGET_CFG} == "yes" ]] && show_target_cfg
setup_portage_vars
if [[ -z ${CROSSDEV_OVERLAY} ]] ; then
eerror "You need to specify an output overlay. Please use --ov-output, or consult"
eerror "https://wiki.gentoo.org/wiki/Custom_repository for more details."
exit 1
fi
if [[ ${HCHOST} == "${CTARGET}" ]] ; then
eerror "Refusing to create a cross-compiler using the same"
eerror "target name as your host utils."
exit 1
fi
for hchost in "${HCHOSTS[@]}"; do
if [[ ${hchost} == "${CTARGET}" ]] ; then
eerror "Refusing to create a cross-compiler using the same"
eerror "target name as your host utils."
eerror "Consider using sys-devel/multilib-gcc-wrapper package."
exit 1
fi
done
# grab user settings
d="${CONFIGROOT}/crossdev/${CTARGET}"
for v in MULTILIB_ABIS USE BVER GVER KVER LVER DVER STAGE CFLAGS LDFLAGS ASFLAGS ; do
if [[ -e ${d}/${v} ]] ; then
# yes, quotes are needed in this instance (export $var="...")
export ${v}="$(<"${d}"/${v})"
einfo "Restoring user setting '${v}' to '${!v}'"
fi
done
if [[ -e ${d}/env ]] ; then
einfo "Restoring generic user env settings"
source "${d}"/env
fi
# parse multilib settings until profiles are sane