From d36fdab8d7ebc3dc18fddf6fd70af765b2717bd5 Mon Sep 17 00:00:00 2001
Patch-Source: https://github.com/dotnet/arcade/pull/11608
From: Antoine Martin <dev@ayakael.net>
Date: Thu, 6 Apr 2023 18:39:03 -0400
Subject: [PATCH 1/1] properly set toolchain for crossbuilding on Alpine Linux

---
 eng/common/cross/toolchain.cmake      |  19 +-
 3 files changed, 325 insertions(+), 2 deletions(-)

diff --git a/src/runtime/eng/common/cross/toolchain.cmake b/src/runtime/eng/common/cross/toolchain.cmake
index 51f30e53dd4..b1369a2aa37 100644
--- a/src/runtime/eng/common/cross/toolchain.cmake
+++ b/src/runtime/eng/common/cross/toolchain.cmake
@@ -45,10 +45,18 @@ elseif(TARGET_ARCH_NAME STREQUAL "arm64")
   endif()
 elseif(TARGET_ARCH_NAME STREQUAL "s390x")
   set(CMAKE_SYSTEM_PROCESSOR s390x)
-  set(TOOLCHAIN "s390x-linux-gnu")
+  if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/s390x-alpine-linux-musl)
+    set(TOOLCHAIN "s390x-alpine-linux-musl")
+  else()
+    set(TOOLCHAIN "s390x-linux-gnu")
+  endif()
 elseif(TARGET_ARCH_NAME STREQUAL "x86")
   set(CMAKE_SYSTEM_PROCESSOR i686)
-  set(TOOLCHAIN "i686-linux-gnu")
+  if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/i586-alpine-linux-musl)
+    set(TOOLCHAIN "i586-alpine-linux-musl")
+  else()
+    set(TOOLCHAIN "i686-linux-gnu")
+  endif()
 elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
   set(CMAKE_SYSTEM_PROCESSOR "x86_64")
   set(triple "x86_64-unknown-freebsd11")
@@ -173,6 +181,10 @@ elseif(TARGET_ARCH_NAME STREQUAL "arm64")
     add_toolchain_linker_flag("-Wl,--rpath-link=${CROSS_ROOTFS}/usr/lib64/gcc/${TIZEN_TOOLCHAIN}")
   endif()
 elseif(TARGET_ARCH_NAME STREQUAL "x86")
+  if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/i586-alpine-linux-musl)
+    add_toolchain_linker_flag("--target=${TOOLCHAIN}")
+    add_toolchain_linker_flag("-Wl,--rpath-link=${CROSS_ROOTFS}/usr/lib/gcc/${TOOLCHAIN}")
+  endif()
   add_toolchain_linker_flag(-m32)
 elseif(ILLUMOS)
   add_toolchain_linker_flag("-L${CROSS_ROOTFS}/lib/amd64")
@@ -204,6 +216,9 @@ if(TARGET_ARCH_NAME MATCHES "^(arm|armel)$")
     add_compile_options(-mfloat-abi=softfp)
   endif()
 elseif(TARGET_ARCH_NAME STREQUAL "x86")
+  if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/i586-alpine-linux-musl)
+    add_compile_options(--target=${TOOLCHAIN})
+  endif()
   add_compile_options(-m32)
   add_compile_options(-Wno-error=unused-command-line-argument)
 endif()
