From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1681375-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 8192E158042
	for <garchives@archives.gentoo.org>; Fri, 18 Oct 2024 14:01:59 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 1F5AFE081E;
	Fri, 18 Oct 2024 14:01:56 +0000 (UTC)
Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id CC1E0E081E
	for <gentoo-commits@lists.gentoo.org>; Fri, 18 Oct 2024 14:01:55 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id CAD6F343079
	for <gentoo-commits@lists.gentoo.org>; Fri, 18 Oct 2024 14:01:40 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 3089D1D0A
	for <gentoo-commits@lists.gentoo.org>; Fri, 18 Oct 2024 14:01:39 +0000 (UTC)
From: "Fabian Groffen" <grobian@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Fabian Groffen" <grobian@gentoo.org>
Message-ID: <1729260050.0e8cc099b8d696b2354e8705ce83bcf317ed9a7a.grobian@gentoo>
Subject: [gentoo-commits] repo/proj/prefix:master commit in: scripts/
X-VCS-Repository: repo/proj/prefix
X-VCS-Files: scripts/bootstrap-prefix.sh
X-VCS-Directories: scripts/
X-VCS-Committer: grobian
X-VCS-Committer-Name: Fabian Groffen
X-VCS-Revision: 0e8cc099b8d696b2354e8705ce83bcf317ed9a7a
X-VCS-Branch: master
Date: Fri, 18 Oct 2024 14:01:39 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: bbeb8093-a64e-4743-8939-785857fa3c68
X-Archives-Hash: 31c2602e6f142b6c2d918bd5111a002f

commit:     0e8cc099b8d696b2354e8705ce83bcf317ed9a7a
Author:     Alexey Sokolov <alexey+gentoo <AT> asokolov <DOT> org>
AuthorDate: Fri Oct 18 14:00:50 2024 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Fri Oct 18 14:00:50 2024 +0000
URL:        https://gitweb.gentoo.org/repo/proj/prefix.git/commit/?id=0e8cc099

scripts/bootstrap-prefix: add support for Darwin Clang bootstrap path

This is a slightly modified version of
https://github.com/gentoo/prefix/pull/34/commits/6319cbe1d15c3831bd4a2e5b52c4367abc68747d

Bug: https://bugs.gentoo.org/758167
Signed-off-by: Alexey Sokolov <alexey+gentoo <AT> asokolov.org>
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 scripts/bootstrap-prefix.sh | 152 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 127 insertions(+), 25 deletions(-)

diff --git a/scripts/bootstrap-prefix.sh b/scripts/bootstrap-prefix.sh
index fdbe73e17e..86b7860595 100755
--- a/scripts/bootstrap-prefix.sh
+++ b/scripts/bootstrap-prefix.sh
@@ -220,18 +220,17 @@ configure_toolchain() {
 					# this is Clang, recent enough to compile recent clang
 					compiler_stage1+="
 						${llvm_deps}
-						sys-libs/libcxxabi
-						sys-libs/libcxx
+						sys-libs/compiler-rt
 						sys-devel/llvm
+						sys-devel/lld
+						sys-devel/clang-common
 						sys-devel/clang
 					"
 					CC=clang
 					CXX=clang++
-					# avoid going through hoops and deps for
-					# binutils-apple, rely on the host-installed ld to
-					# build a compiler, we'll pull in binutils-apple
-					# from system set
-					linker=sys-devel/native-cctools
+					linker=
+					[[ "${BOOTSTRAP_STAGE}" == stage2 ]] && \
+						linker=sys-devel/lld
 					;;
 				*)
 					eerror "unknown/unsupported compiler"
@@ -240,12 +239,16 @@ configure_toolchain() {
 			esac
 
 			compiler="
-				dev-libs/libffi
 				${llvm_deps}
+				sys-libs/compiler-rt
 				sys-libs/libcxxabi
 				sys-libs/libcxx
 				sys-devel/llvm
-				sys-devel/clang"
+				sys-devel/lld
+				sys-libs/llvm-libunwind
+				sys-devel/clang-common
+				sys-devel/clang
+			"
 			;;
 		*-linux*)
 			is-rap && einfo "Triggering Linux RAP bootstrap"
@@ -457,6 +460,8 @@ bootstrap_profile() {
 	if [[ ${DARWIN_USE_GCC} == 1 ]] ; then
 		# amend profile, to use gcc one
 		profile="${profile}/gcc"
+	elif [[ ${CHOST} == *-darwin* ]] ; then
+		[[ "${BOOTSTRAP_STAGE}" != stage2 ]] && profile+="/clang"
 	fi
 
 	[[ -n ${PROFILE_BASE}${PROFILE_VARIANT} ]] &&
@@ -1466,7 +1471,7 @@ bootstrap_stage1() {
 		[[ -e ${ROOT}/tmp/${x} ]] || ( cd "${ROOT}"/tmp && ln -s usr/${x} )
 	done
 
-	configure_toolchain
+	BOOTSTRAP_STAGE="stage1" configure_toolchain || return 1
 	export CC CXX
 
 	# default: empty = NO
@@ -1752,9 +1757,16 @@ bootstrap_stage1() {
 	# setup a profile for stage2
 	mkdir -p "${ROOT}"/tmp/etc/. || return 1
 	[[ -e ${ROOT}/tmp/etc/portage/make.profile ]] || \
-		(	"${CP}" -dpR "${ROOT}"/etc/portage "${ROOT}"/tmp/etc && \
+		(
+			"${CP}" -dpR "${ROOT}"/etc/portage "${ROOT}"/tmp/etc && \
 			rm -f "${ROOT}"/tmp/etc/portage/make.profile && \
-			(ROOT="${ROOT}"/tmp PREFIX_DISABLE_RAP=yes bootstrap_profile) ) || return 1
+			(
+				ROOT="${ROOT}"/tmp \
+				PREFIX_DISABLE_RAP="yes" \
+				BOOTSTRAP_STAGE="stage2" \
+				bootstrap_profile
+			)
+		) || return 1
 
 	# setup portage
 	[[ -e ${ROOT}/tmp/usr/bin/emerge ]] || (bootstrap_portage) || return 1
@@ -1819,7 +1831,6 @@ do_emerge_pkgs() {
 			"-berkdb"
 			"-fortran"            # gcc
 			"-gdbm"
-			"-libcxx"
 			"-nls"
 			"-pcre"
 			"-python"
@@ -1831,7 +1842,24 @@ do_emerge_pkgs() {
 			"clang"
 			"internal-glib"
 		)
-		local override_make_conf_dir="${PORTAGE_OVERRIDE_EPREFIX}${MAKE_CONF_DIR#"${ROOT}"}"
+
+		local skip_llvm_pkg_setup=
+		if [[ ${CHOST}:${DARWIN_USE_GCC} == *-darwin*:0 ]] ; then
+			# Clang-based Darwin
+			myuse+=(
+				"-binutils-plugin"
+				"default-compiler-rt"
+				"default-libcxx"
+				"default-lld"
+			)
+			if [[ "${BOOTSTRAP_STAGE}" == stage2 ]] ; then
+				myuse+=( "bootstrap-prefix" )
+				skip_llvm_pkg_setup="yes"
+			fi
+		fi
+
+		local override_make_conf_dir="${PORTAGE_OVERRIDE_EPREFIX}"
+		override_make_conf_dir+="${MAKE_CONF_DIR#"${ROOT}"}"
 
 		if [[ " ${USE} " == *" prefix-stack "* ]] &&
 		   [[ ${PORTAGE_OVERRIDE_EPREFIX} == */tmp ]] &&
@@ -1892,6 +1920,7 @@ do_emerge_pkgs() {
 			PORTAGE_SYNC_STALE=0 \
 			FEATURES="-news ${FEATURES}" \
 			USE="${myuse[*]}" \
+			LLVM_ECLASS_SKIP_PKG_SETUP="${skip_llvm_pkg_setup}" \
 			"${ROOT}"/tmp/bin/python \
 			"${ROOT}"/tmp/usr/bin/emerge "${eopts[@]}" "${pkg}"
 		) || return 1
@@ -1908,7 +1937,7 @@ bootstrap_stage2() {
 
 	# Find out what toolchain packages we need, and configure LDFLAGS
 	# and friends.
-	configure_toolchain || return 1
+	BOOTSTRAP_STAGE="stage2" configure_toolchain || return 1
 	configure_cflags || return 1
 	export CONFIG_SHELL="${ROOT}"/tmp/bin/bash
 	export CC CXX
@@ -1995,6 +2024,24 @@ bootstrap_stage2() {
 		EOF
 	fi
 
+	# provide active SDK link on Darwin
+	if [[ ${CHOST} == *-darwin* ]] ; then
+		rm -f "${ROOT}"/tmp/MacOSX.sdk
+		( cd "${ROOT}"/tmp && ln -s ../MacOSX.sdk MacOSX.sdk )
+		if [[ ${DARWIN_USE_GCC} == 0 ]] ; then
+			# Until proper clang is installed, just redirect calls to it
+			# to the system's one. Libtool is here because its path is
+			# passed to the compiler-rt and llvm's ebuilds.
+			for bin in libtool clang clang++ ; do
+				{
+					echo "#!${ROOT}/tmp/bin/sh"
+					echo "exec ${bin}"' "$@"'
+				} > "${ROOT}/tmp/usr/bin/${CHOST}-${bin}"
+				chmod +x "${ROOT}/tmp/usr/bin/${CHOST}-${bin}"
+			done
+		fi
+	fi
+
 	# Build a basic compiler and portage dependencies in $ROOT/tmp.
 	pkgs=(
 		sys-devel/gnuconfig
@@ -2013,12 +2060,6 @@ bootstrap_stage2() {
 		sys-devel/binutils-config
 	)
 
-	# provide active SDK link on Darwin
-	if [[ ${CHOST} == *-darwin* ]] ; then
-		rm -f "${ROOT}"/tmp/MacOSX.sdk
-		( cd "${ROOT}"/tmp && ln -s ../MacOSX.sdk MacOSX.sdk )
-	fi
-
 	# cmake has some external dependencies which require autoconf, etc.
 	# unless we only build the buildtool, bug #603012
 	echo "dev-build/cmake -server" >> "${ROOT}"/tmp/etc/portage/package.use
@@ -2037,8 +2078,8 @@ bootstrap_stage2() {
 
 	emerge_pkgs --nodeps "${pkgs[@]}" || return 1
 
-	# Debian multiarch supported by RAP needs ld to support sysroot.
 	for pkg in ${linker} ; do
+		# Debian multiarch supported by RAP needs ld to support sysroot.
 		EXTRA_ECONF=$(rapx --with-sysroot=/) \
 		emerge_pkgs --nodeps "${pkg}" || return 1
 	done
@@ -2083,12 +2124,22 @@ bootstrap_stage2() {
 	done
 
 	if [[ ${compiler_type} == clang ]] ; then
+		if [[ ${CHOST} == *-darwin* ]] ; then
+			# Stop using host's compilers, but still need 'libtool' in PATH.
+			rm "${ROOT}/tmp/usr/bin/${CHOST}"-{libtool,clang,clang++}
+			mkdir -p "${ROOT}"/usr/bin
+			ln -s "${ROOT}"/tmp/usr/lib/llvm/*/bin/llvm-libtool-darwin \
+				"${ROOT}"/usr/bin/libtool
+		fi
+
 		# We use Clang as our toolchain compiler, so we need to make
 		# sure we actually use it
 		mkdir -p -- "${MAKE_CONF_DIR}"
 		{
 			echo
 			echo "# System compiler on $(uname) Prefix is Clang, do not remove this"
+			echo "AS=\"${CHOST}-clang -c\""
+			echo "CPP=${CHOST}-clang-cpp"
 			echo "CC=${CHOST}-clang"
 			echo "CXX=${CHOST}-clang++"
 			echo "OBJC=${CHOST}-clang"
@@ -2152,7 +2203,7 @@ bootstrap_stage3() {
 	# they stop mucking up builds.
 	rm -f "${ROOT}"/tmp/usr/local/bin/{,my,${CHOST}-}{gcc,g++}
 
-	configure_toolchain || return 1
+	BOOTSTRAP_STAGE=stage3 configure_toolchain || return 1
 
 	if [[ ${compiler_type} == clang ]] ; then
 		if ! type -P clang > /dev/null ; then
@@ -2205,7 +2256,6 @@ bootstrap_stage3() {
 		# (CBUILD, BDEPEND) and with the system being built
 		# (CHOST, RDEPEND).  To correctly bootstrap stage3,
 		# PORTAGE_OVERRIDE_EPREFIX as BROOT is needed.
-		PREROOTPATH="${ROOT}"$(echo /{,tmp/}{usr/,}{,lib/llvm/{12,11,10}/}{s,}bin | sed "s, ,:${ROOT},g") \
 		EPREFIX="${ROOT}" PORTAGE_TMPDIR="${PORTAGE_TMPDIR}" \
 		EMERGE_LOG_DIR="${ROOT}"/var/log \
 		STAGE=stage3 \
@@ -2388,6 +2438,31 @@ bootstrap_stage3() {
 	PYTHON_COMPAT_OVERRIDE="python$(python_ver)" \
 	pre_emerge_pkgs --nodeps "${compiler_pkgs[@]}" || return 1
 
+	if [[ ${CHOST}:${DARWIN_USE_GCC} == *-darwin*:0 ]] ; then
+		# At this point our libc++abi.dylib is dynamically linked to
+		# /usr/lib/libc++abi.dylib. That causes issues with perl later. Force
+		# rebuild of sys-libs/libcxxabi to break this link.
+		rm -Rf "${ROOT}/var/db/pkg/sys-libs/libcxxabi"*
+		PYTHON_COMPAT_OVERRIDE=python$(python_ver) \
+			pre_emerge_pkgs --nodeps "sys-libs/libcxxabi" || return 1
+
+		# Make ${CHOST}-libtool (used by compiler-rt's and llvm's ebuild) to
+		# point at the correct libtool in stage3. Resolve it in runtime, to
+		# support llvm version upgrades.
+		rm -f ${ROOT}/usr/bin/${CHOST}-libtool
+		{
+			echo "#!${ROOT}/bin/sh"
+			echo 'exec llvm-libtool-darwin "$@"'
+		} > "${ROOT}"/usr/bin/${CHOST}-${bin}
+
+		# Now clang is ready, can use it instead of /usr/bin/gcc
+		# TODO: perhaps symlink the whole etc/portage instead?
+		ln -s -f "${ROOT}/etc/portage/make.profile" \
+			"${ROOT}/tmp/etc/portage/make.profile"
+		cp "${ROOT}/etc/portage/make.conf/0100_bootstrap_prefix_clang.conf" \
+			"${ROOT}/tmp/etc/portage/make.conf/"
+	fi
+
 	# Undo libgcc_s.so path of stage2
 	# Now we have the compiler right there
 	unset CC CXX CPPFLAGS LDFLAGS
@@ -2404,8 +2479,35 @@ bootstrap_stage3() {
 		ln -s bash "${ROOT}"/bin/sh
 	fi
 
+	if [[ "${compiler_type}" == clang ]] ; then
+		if [[ ! -e "${ROOT}"/tmp/etc/env.d/11stage3-llvm ]]; then
+			ln -s "${ROOT}"/etc/env.d/60llvm-* \
+				"${ROOT}"/tmp/etc/env.d/11stage3-llvm
+		fi
+		# Prevent usage of AppleClang aka gcc for bad packages which ignore $CC
+		if [[ ! -e "${ROOT}"/usr/bin/gcc ]]; then
+			echo "#!${ROOT}/bin/bash" > "${ROOT}"/usr/bin/gcc
+			echo "false ${CHOST}-clang \"\$@\"" >> "${ROOT}"/usr/bin/gcc
+		fi
+		if [[ ! -e "${ROOT}"/usr/bin/g++ ]]; then
+			echo "#!${ROOT}/bin/bash" > "${ROOT}"/usr/bin/g++
+			echo "false ${CHOST}-clang++ \"\$@\"" >> "${ROOT}"/usr/bin/g++
+		fi
+		chmod +x "${ROOT}"/usr/bin/{gcc,g++}
+		if [[ ${CHOST} == *-darwin* ]]; then
+			if [[ ! -e "${ROOT}"/usr/bin/ld ]]; then
+				echo "#!${ROOT}/bin/bash" > "${ROOT}"/usr/bin/ld
+				echo "false ld64.lld \"\$@\"" >> "${ROOT}"/usr/bin/ld
+			fi
+			chmod +x "${ROOT}"/usr/bin/ld
+		fi
+	fi
+	
 	# Start using apps from the final destination Prefix
-	export PREROOTPATH="${ROOT}/usr/bin:${ROOT}/bin"
+	cat > "${ROOT}"/tmp/etc/env.d/10stage3 <<-EOF
+		PATH="${ROOT}/usr/bin:${ROOT}/bin"
+	EOF
+	"${ROOT}"/tmp/usr/sbin/env-update
 
 	# Get a sane bash, overwriting tmp symlinks
 	pre_emerge_pkgs "" "app-shells/bash" || return 1