From 0a568a04a6c144cea625b5de2fbbea6bcfe89e3d Mon Sep 17 00:00:00 2001
From: Roman Shaposhnik <rvs@zededa.com>
Date: Fri, 21 May 2021 14:17:45 -0700
Subject: [PATCH] Accounting for gcc requiring explicit -latomic in
 bootstrap.sh

On some architectures rograms which use C11 or C++11 atomics,
like #include <atomic>, generally must link against -latomic
explicitly.

For now the check is only against riscv64, but the list may grow.

Signed-off-by: Roman Shaposhnik <rvs@zededa.com>
---
 scripts/bootstrap.sh | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh
index acc45f5cdbd8..7cad6feb2beb 100755
--- a/scripts/bootstrap.sh
+++ b/scripts/bootstrap.sh
@@ -94,6 +94,16 @@ msg "Cross building base system"
 # Implicit dependencies for early targets
 EXTRADEPENDS_TARGET="libgcc libstdc++ musl-dev"
 
+# On a few architectures like riscv64 we need to account for
+# gcc requiring -ltomic to be set explicitly if a C[++]11 program
+# uses atomics (e.g. #include <atomic>):
+# https://github.com/riscv/riscv-gnu-toolchain/issues/183#issuecomment-253721765
+# The reason gcc itself is needed is because .so is in that package,
+# not in libatomic.
+if [ "$TARGET_ARCH" = "riscv64" ]; then
+	NEEDS_LIBATOMIC="yes"
+fi
+
 # ordered cross-build
 for PKG in fortify-headers linux-headers musl libc-dev pkgconf zlib \
 	   openssl ca-certificates libmd \
@@ -111,7 +121,10 @@ for PKG in fortify-headers linux-headers musl libc-dev pkgconf zlib \
 	   libxml2 pax-utils llvm11 community/rust \
 	   $KERNEL_PKG ; do
 
-	EXTRADEPENDS_TARGET="$EXTRADEPENDS_TARGET" \
+	if [ "$NEEDS_LIBATOMIC" = "yes" ]; then
+		EXTRADEPENDS_BUILD="libatomic gcc-$TARGET_ARCH g++-$TARGET_ARCH"
+	fi
+	EXTRADEPENDS_TARGET="$EXTRADEPENDS_TARGET"  EXTRADEPENDS_BUILD="$EXTRADEPENDS_BUILD" \
 	CHOST=$TARGET_ARCH BOOTSTRAP=bootimage APKBUILD=$(apkbuildname $PKG) abuild -r
 
 	case "$PKG" in
@@ -119,6 +132,11 @@ for PKG in fortify-headers linux-headers musl libc-dev pkgconf zlib \
 		# Additional implicit dependencies once built
 		EXTRADEPENDS_TARGET="$EXTRADEPENDS_TARGET $PKG"
 		;;
+	gcc)
+		if [ "$NEEDS_LIBATOMIC" = "yes" ]; then
+			EXTRADEPENDS_TARGET="libatomic gcc $EXTRADEPENDS_TARGET"
+		fi
+		;;
 	build-base)
 		# After build-base, that alone is sufficient dependency in the target
 		EXTRADEPENDS_TARGET="busybox $PKG"
-- 
GitLab