From 482c74e756cfc82de88d715c93e0268acdb8bfd5 Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Fri, 21 Feb 2025 20:38:00 +0000
Subject: [PATCH] driver: fix wasm backend sysroot lookup logic when -flto is
 passed

For the wasm target, the driver calls `wasm32-wasi-clang
--print-search-dirs` and finds the sysroot directory that contains
libc.so etc, then passes the directory path to dyld. However, when GHC
is configured with -flto as a part of C/C++ compiler flags, the clang
driver would insert a llvm-lto specific directory in the
--print-search-dirs output and the driver didn't take that into
account. This patch fixes it and always selects the non-lto sysroot
directory to be passed to dyld. This is one small step towards
supporting building all cbits with lto for wasm.

(cherry picked from commit 7eea38c838c9d839e9efea0e6d9f79034e99fa4f)
(cherry picked from commit d842f75048c98436d5daec870a63062475e9386f)
---
 compiler/GHC.hs | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/compiler/GHC.hs b/compiler/GHC.hs
index 129f8ecb078..994db7ee9f6 100644
--- a/compiler/GHC.hs
+++ b/compiler/GHC.hs
@@ -689,11 +689,7 @@ setTopSessionDynFlags dflags = do
 #if defined(wasm32_HOST_ARCH)
         let libdir = sorry "cannot spawn child process on wasm"
 #else
-        libdir <- liftIO $ do
-          libdirs <- Loader.getGccSearchDirectory logger dflags "libraries"
-          case libdirs of
-            [_, libdir] -> pure libdir
-            _ -> panic "corrupted wasi-sdk installation"
+        libdir <- liftIO $ last <$> Loader.getGccSearchDirectory logger dflags "libraries"
 #endif
         let profiled = ways dflags `hasWay` WayProf
             way_tag = if profiled then "_p" else ""
-- 
GitLab