Subject: [bug#77653] [PATCH 3/4] gnu: Add wasm32-wasi-clang.

From: Ian Eure <[email-masked]>
Date: Tue, 8 Apr 2025 12:58:28 -0700

* gnu/packages/wasm.scm (wasm32-wasi-clang): New variable.

Change-Id: Ibf1df3e93929442a8eed29eea20b2f83b491f3b3
---
 gnu/packages/wasm.scm | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/gnu/packages/wasm.scm b/gnu/packages/wasm.scm
index 53ea31f4a7..447687d009 100644
--- a/gnu/packages/wasm.scm
+++ b/gnu/packages/wasm.scm
@@ -24,7 +24,8 @@ (define-module (gnu packages wasm)
   #:use-module (guix gexp)
   #:use-module (guix git-download)
   #:use-module ((guix licenses) #:prefix license:)
-  #:use-module (guix packages))
+  #:use-module (guix packages)
+  #:use-module (guix utils))
 
 (define-public wasi-libc
   (package
@@ -106,3 +107,29 @@ (define-public wasm32-wasi-clang-runtime
               ;; WASM only needs libclang_rt.builtins-wasm32.a from
               ;; compiler-rt.
               "../source/compiler-rt/lib/builtins")))))
+
+;; Although Clang can be built to support multiple compilation targets, Guix
+;; builds only target the native architecture, so we have to build a build a
+;; new toolchain that to target WASM.
+
+(define clang-from-llvm (@@ (gnu packages llvm) clang-from-llvm))
+(define llvm-monorepo (@@ (gnu packages llvm) llvm-monorepo))
+
+(define-public wasm32-wasi-clang
+  (let ((base (clang-from-llvm llvm-16 wasm32-wasi-clang-runtime)))
+    (package (inherit base)
+      (name "wasm32-wasi-clang")
+      (inputs
+       (modify-inputs (package-inputs base)
+         (prepend wasi-libc)))
+      (arguments
+       (substitute-keyword-arguments (package-arguments base)
+         ((#:configure-flags flags)
+          #~(list "-DCLANG_INCLUDE_TESTS=True"
+                  ;; Use a sane default include directory.
+                  (string-append "-DC_INCLUDE_DIRS="
+                                 #$wasi-libc
+                                 "/wasm32-wasi/include")))
+         ((#:phases phases)
+          `(modify-phases ,phases
+             (delete 'symlink-cfi_ignorelist))))))))
-- 
2.49.0