From 7eea38c838c9d839e9efea0e6d9f79034e99fa4f 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.
---
 compiler/GHC.hs | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/compiler/GHC.hs b/compiler/GHC.hs
index c173eac75f4..0ef89a79220 100644
--- a/compiler/GHC.hs
+++ b/compiler/GHC.hs
@@ -717,11 +717,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