Skip to content

Commit 0e8cc09

Browse files
DarthGandalfgrobian
authored andcommitted
scripts/bootstrap-prefix: add support for Darwin Clang bootstrap path
This is a slightly modified version of 6319cbe Bug: https://bugs.gentoo.org/758167 Signed-off-by: Alexey Sokolov <[email protected]> Signed-off-by: Fabian Groffen <[email protected]>
1 parent 33c6d9a commit 0e8cc09

File tree

1 file changed

+127
-25
lines changed

1 file changed

+127
-25
lines changed

scripts/bootstrap-prefix.sh

+127-25
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,17 @@ configure_toolchain() {
220220
# this is Clang, recent enough to compile recent clang
221221
compiler_stage1+="
222222
${llvm_deps}
223-
sys-libs/libcxxabi
224-
sys-libs/libcxx
223+
sys-libs/compiler-rt
225224
sys-devel/llvm
225+
sys-devel/lld
226+
sys-devel/clang-common
226227
sys-devel/clang
227228
"
228229
CC=clang
229230
CXX=clang++
230-
# avoid going through hoops and deps for
231-
# binutils-apple, rely on the host-installed ld to
232-
# build a compiler, we'll pull in binutils-apple
233-
# from system set
234-
linker=sys-devel/native-cctools
231+
linker=
232+
[[ "${BOOTSTRAP_STAGE}" == stage2 ]] && \
233+
linker=sys-devel/lld
235234
;;
236235
*)
237236
eerror "unknown/unsupported compiler"
@@ -240,12 +239,16 @@ configure_toolchain() {
240239
esac
241240

242241
compiler="
243-
dev-libs/libffi
244242
${llvm_deps}
243+
sys-libs/compiler-rt
245244
sys-libs/libcxxabi
246245
sys-libs/libcxx
247246
sys-devel/llvm
248-
sys-devel/clang"
247+
sys-devel/lld
248+
sys-libs/llvm-libunwind
249+
sys-devel/clang-common
250+
sys-devel/clang
251+
"
249252
;;
250253
*-linux*)
251254
is-rap && einfo "Triggering Linux RAP bootstrap"
@@ -457,6 +460,8 @@ bootstrap_profile() {
457460
if [[ ${DARWIN_USE_GCC} == 1 ]] ; then
458461
# amend profile, to use gcc one
459462
profile="${profile}/gcc"
463+
elif [[ ${CHOST} == *-darwin* ]] ; then
464+
[[ "${BOOTSTRAP_STAGE}" != stage2 ]] && profile+="/clang"
460465
fi
461466

462467
[[ -n ${PROFILE_BASE}${PROFILE_VARIANT} ]] &&
@@ -1466,7 +1471,7 @@ bootstrap_stage1() {
14661471
[[ -e ${ROOT}/tmp/${x} ]] || ( cd "${ROOT}"/tmp && ln -s usr/${x} )
14671472
done
14681473

1469-
configure_toolchain
1474+
BOOTSTRAP_STAGE="stage1" configure_toolchain || return 1
14701475
export CC CXX
14711476

14721477
# default: empty = NO
@@ -1752,9 +1757,16 @@ bootstrap_stage1() {
17521757
# setup a profile for stage2
17531758
mkdir -p "${ROOT}"/tmp/etc/. || return 1
17541759
[[ -e ${ROOT}/tmp/etc/portage/make.profile ]] || \
1755-
( "${CP}" -dpR "${ROOT}"/etc/portage "${ROOT}"/tmp/etc && \
1760+
(
1761+
"${CP}" -dpR "${ROOT}"/etc/portage "${ROOT}"/tmp/etc && \
17561762
rm -f "${ROOT}"/tmp/etc/portage/make.profile && \
1757-
(ROOT="${ROOT}"/tmp PREFIX_DISABLE_RAP=yes bootstrap_profile) ) || return 1
1763+
(
1764+
ROOT="${ROOT}"/tmp \
1765+
PREFIX_DISABLE_RAP="yes" \
1766+
BOOTSTRAP_STAGE="stage2" \
1767+
bootstrap_profile
1768+
)
1769+
) || return 1
17581770

17591771
# setup portage
17601772
[[ -e ${ROOT}/tmp/usr/bin/emerge ]] || (bootstrap_portage) || return 1
@@ -1819,7 +1831,6 @@ do_emerge_pkgs() {
18191831
"-berkdb"
18201832
"-fortran" # gcc
18211833
"-gdbm"
1822-
"-libcxx"
18231834
"-nls"
18241835
"-pcre"
18251836
"-python"
@@ -1831,7 +1842,24 @@ do_emerge_pkgs() {
18311842
"clang"
18321843
"internal-glib"
18331844
)
1834-
local override_make_conf_dir="${PORTAGE_OVERRIDE_EPREFIX}${MAKE_CONF_DIR#"${ROOT}"}"
1845+
1846+
local skip_llvm_pkg_setup=
1847+
if [[ ${CHOST}:${DARWIN_USE_GCC} == *-darwin*:0 ]] ; then
1848+
# Clang-based Darwin
1849+
myuse+=(
1850+
"-binutils-plugin"
1851+
"default-compiler-rt"
1852+
"default-libcxx"
1853+
"default-lld"
1854+
)
1855+
if [[ "${BOOTSTRAP_STAGE}" == stage2 ]] ; then
1856+
myuse+=( "bootstrap-prefix" )
1857+
skip_llvm_pkg_setup="yes"
1858+
fi
1859+
fi
1860+
1861+
local override_make_conf_dir="${PORTAGE_OVERRIDE_EPREFIX}"
1862+
override_make_conf_dir+="${MAKE_CONF_DIR#"${ROOT}"}"
18351863

18361864
if [[ " ${USE} " == *" prefix-stack "* ]] &&
18371865
[[ ${PORTAGE_OVERRIDE_EPREFIX} == */tmp ]] &&
@@ -1892,6 +1920,7 @@ do_emerge_pkgs() {
18921920
PORTAGE_SYNC_STALE=0 \
18931921
FEATURES="-news ${FEATURES}" \
18941922
USE="${myuse[*]}" \
1923+
LLVM_ECLASS_SKIP_PKG_SETUP="${skip_llvm_pkg_setup}" \
18951924
"${ROOT}"/tmp/bin/python \
18961925
"${ROOT}"/tmp/usr/bin/emerge "${eopts[@]}" "${pkg}"
18971926
) || return 1
@@ -1908,7 +1937,7 @@ bootstrap_stage2() {
19081937

19091938
# Find out what toolchain packages we need, and configure LDFLAGS
19101939
# and friends.
1911-
configure_toolchain || return 1
1940+
BOOTSTRAP_STAGE="stage2" configure_toolchain || return 1
19121941
configure_cflags || return 1
19131942
export CONFIG_SHELL="${ROOT}"/tmp/bin/bash
19141943
export CC CXX
@@ -1995,6 +2024,24 @@ bootstrap_stage2() {
19952024
EOF
19962025
fi
19972026

2027+
# provide active SDK link on Darwin
2028+
if [[ ${CHOST} == *-darwin* ]] ; then
2029+
rm -f "${ROOT}"/tmp/MacOSX.sdk
2030+
( cd "${ROOT}"/tmp && ln -s ../MacOSX.sdk MacOSX.sdk )
2031+
if [[ ${DARWIN_USE_GCC} == 0 ]] ; then
2032+
# Until proper clang is installed, just redirect calls to it
2033+
# to the system's one. Libtool is here because its path is
2034+
# passed to the compiler-rt and llvm's ebuilds.
2035+
for bin in libtool clang clang++ ; do
2036+
{
2037+
echo "#!${ROOT}/tmp/bin/sh"
2038+
echo "exec ${bin}"' "$@"'
2039+
} > "${ROOT}/tmp/usr/bin/${CHOST}-${bin}"
2040+
chmod +x "${ROOT}/tmp/usr/bin/${CHOST}-${bin}"
2041+
done
2042+
fi
2043+
fi
2044+
19982045
# Build a basic compiler and portage dependencies in $ROOT/tmp.
19992046
pkgs=(
20002047
sys-devel/gnuconfig
@@ -2013,12 +2060,6 @@ bootstrap_stage2() {
20132060
sys-devel/binutils-config
20142061
)
20152062

2016-
# provide active SDK link on Darwin
2017-
if [[ ${CHOST} == *-darwin* ]] ; then
2018-
rm -f "${ROOT}"/tmp/MacOSX.sdk
2019-
( cd "${ROOT}"/tmp && ln -s ../MacOSX.sdk MacOSX.sdk )
2020-
fi
2021-
20222063
# cmake has some external dependencies which require autoconf, etc.
20232064
# unless we only build the buildtool, bug #603012
20242065
echo "dev-build/cmake -server" >> "${ROOT}"/tmp/etc/portage/package.use
@@ -2037,8 +2078,8 @@ bootstrap_stage2() {
20372078

20382079
emerge_pkgs --nodeps "${pkgs[@]}" || return 1
20392080

2040-
# Debian multiarch supported by RAP needs ld to support sysroot.
20412081
for pkg in ${linker} ; do
2082+
# Debian multiarch supported by RAP needs ld to support sysroot.
20422083
EXTRA_ECONF=$(rapx --with-sysroot=/) \
20432084
emerge_pkgs --nodeps "${pkg}" || return 1
20442085
done
@@ -2083,12 +2124,22 @@ bootstrap_stage2() {
20832124
done
20842125

20852126
if [[ ${compiler_type} == clang ]] ; then
2127+
if [[ ${CHOST} == *-darwin* ]] ; then
2128+
# Stop using host's compilers, but still need 'libtool' in PATH.
2129+
rm "${ROOT}/tmp/usr/bin/${CHOST}"-{libtool,clang,clang++}
2130+
mkdir -p "${ROOT}"/usr/bin
2131+
ln -s "${ROOT}"/tmp/usr/lib/llvm/*/bin/llvm-libtool-darwin \
2132+
"${ROOT}"/usr/bin/libtool
2133+
fi
2134+
20862135
# We use Clang as our toolchain compiler, so we need to make
20872136
# sure we actually use it
20882137
mkdir -p -- "${MAKE_CONF_DIR}"
20892138
{
20902139
echo
20912140
echo "# System compiler on $(uname) Prefix is Clang, do not remove this"
2141+
echo "AS=\"${CHOST}-clang -c\""
2142+
echo "CPP=${CHOST}-clang-cpp"
20922143
echo "CC=${CHOST}-clang"
20932144
echo "CXX=${CHOST}-clang++"
20942145
echo "OBJC=${CHOST}-clang"
@@ -2152,7 +2203,7 @@ bootstrap_stage3() {
21522203
# they stop mucking up builds.
21532204
rm -f "${ROOT}"/tmp/usr/local/bin/{,my,${CHOST}-}{gcc,g++}
21542205

2155-
configure_toolchain || return 1
2206+
BOOTSTRAP_STAGE=stage3 configure_toolchain || return 1
21562207

21572208
if [[ ${compiler_type} == clang ]] ; then
21582209
if ! type -P clang > /dev/null ; then
@@ -2205,7 +2256,6 @@ bootstrap_stage3() {
22052256
# (CBUILD, BDEPEND) and with the system being built
22062257
# (CHOST, RDEPEND). To correctly bootstrap stage3,
22072258
# PORTAGE_OVERRIDE_EPREFIX as BROOT is needed.
2208-
PREROOTPATH="${ROOT}"$(echo /{,tmp/}{usr/,}{,lib/llvm/{12,11,10}/}{s,}bin | sed "s, ,:${ROOT},g") \
22092259
EPREFIX="${ROOT}" PORTAGE_TMPDIR="${PORTAGE_TMPDIR}" \
22102260
EMERGE_LOG_DIR="${ROOT}"/var/log \
22112261
STAGE=stage3 \
@@ -2388,6 +2438,31 @@ bootstrap_stage3() {
23882438
PYTHON_COMPAT_OVERRIDE="python$(python_ver)" \
23892439
pre_emerge_pkgs --nodeps "${compiler_pkgs[@]}" || return 1
23902440

2441+
if [[ ${CHOST}:${DARWIN_USE_GCC} == *-darwin*:0 ]] ; then
2442+
# At this point our libc++abi.dylib is dynamically linked to
2443+
# /usr/lib/libc++abi.dylib. That causes issues with perl later. Force
2444+
# rebuild of sys-libs/libcxxabi to break this link.
2445+
rm -Rf "${ROOT}/var/db/pkg/sys-libs/libcxxabi"*
2446+
PYTHON_COMPAT_OVERRIDE=python$(python_ver) \
2447+
pre_emerge_pkgs --nodeps "sys-libs/libcxxabi" || return 1
2448+
2449+
# Make ${CHOST}-libtool (used by compiler-rt's and llvm's ebuild) to
2450+
# point at the correct libtool in stage3. Resolve it in runtime, to
2451+
# support llvm version upgrades.
2452+
rm -f ${ROOT}/usr/bin/${CHOST}-libtool
2453+
{
2454+
echo "#!${ROOT}/bin/sh"
2455+
echo 'exec llvm-libtool-darwin "$@"'
2456+
} > "${ROOT}"/usr/bin/${CHOST}-${bin}
2457+
2458+
# Now clang is ready, can use it instead of /usr/bin/gcc
2459+
# TODO: perhaps symlink the whole etc/portage instead?
2460+
ln -s -f "${ROOT}/etc/portage/make.profile" \
2461+
"${ROOT}/tmp/etc/portage/make.profile"
2462+
cp "${ROOT}/etc/portage/make.conf/0100_bootstrap_prefix_clang.conf" \
2463+
"${ROOT}/tmp/etc/portage/make.conf/"
2464+
fi
2465+
23912466
# Undo libgcc_s.so path of stage2
23922467
# Now we have the compiler right there
23932468
unset CC CXX CPPFLAGS LDFLAGS
@@ -2404,8 +2479,35 @@ bootstrap_stage3() {
24042479
ln -s bash "${ROOT}"/bin/sh
24052480
fi
24062481

2482+
if [[ "${compiler_type}" == clang ]] ; then
2483+
if [[ ! -e "${ROOT}"/tmp/etc/env.d/11stage3-llvm ]]; then
2484+
ln -s "${ROOT}"/etc/env.d/60llvm-* \
2485+
"${ROOT}"/tmp/etc/env.d/11stage3-llvm
2486+
fi
2487+
# Prevent usage of AppleClang aka gcc for bad packages which ignore $CC
2488+
if [[ ! -e "${ROOT}"/usr/bin/gcc ]]; then
2489+
echo "#!${ROOT}/bin/bash" > "${ROOT}"/usr/bin/gcc
2490+
echo "false ${CHOST}-clang \"\$@\"" >> "${ROOT}"/usr/bin/gcc
2491+
fi
2492+
if [[ ! -e "${ROOT}"/usr/bin/g++ ]]; then
2493+
echo "#!${ROOT}/bin/bash" > "${ROOT}"/usr/bin/g++
2494+
echo "false ${CHOST}-clang++ \"\$@\"" >> "${ROOT}"/usr/bin/g++
2495+
fi
2496+
chmod +x "${ROOT}"/usr/bin/{gcc,g++}
2497+
if [[ ${CHOST} == *-darwin* ]]; then
2498+
if [[ ! -e "${ROOT}"/usr/bin/ld ]]; then
2499+
echo "#!${ROOT}/bin/bash" > "${ROOT}"/usr/bin/ld
2500+
echo "false ld64.lld \"\$@\"" >> "${ROOT}"/usr/bin/ld
2501+
fi
2502+
chmod +x "${ROOT}"/usr/bin/ld
2503+
fi
2504+
fi
2505+
24072506
# Start using apps from the final destination Prefix
2408-
export PREROOTPATH="${ROOT}/usr/bin:${ROOT}/bin"
2507+
cat > "${ROOT}"/tmp/etc/env.d/10stage3 <<-EOF
2508+
PATH="${ROOT}/usr/bin:${ROOT}/bin"
2509+
EOF
2510+
"${ROOT}"/tmp/usr/sbin/env-update
24092511

24102512
# Get a sane bash, overwriting tmp symlinks
24112513
pre_emerge_pkgs "" "app-shells/bash" || return 1

0 commit comments

Comments
 (0)