[bug#77653,2/4] gnu: Add wasm32-wasi-clang-runtime.

Message ID 20250408195830.2084-2-ian@retrospec.tv
State New
Headers
Series Add WASM toolchain, wasi-libc, and browser WASM sandbox support |

Commit Message

Ian Eure April 8, 2025, 7:58 p.m. UTC
* gnu/packages/wasm.scm (wasm32-wasi-clang-runtime): New variable.

Change-Id: Ib0465fdc86086451782d533380a8966cdde6dc1e
---
 gnu/packages/wasm.scm | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
  

Comments

Z572 April 9, 2025, 2:58 a.m. UTC | #1
Ian Eure <ian@retrospec.tv> writes:

> * gnu/packages/wasm.scm (wasm32-wasi-clang-runtime): New variable.
>
> Change-Id: Ib0465fdc86086451782d533380a8966cdde6dc1e
> ---
>  gnu/packages/wasm.scm | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
>
> diff --git a/gnu/packages/wasm.scm b/gnu/packages/wasm.scm
> index 060e1e420f..53ea31f4a7 100644
> --- a/gnu/packages/wasm.scm
> +++ b/gnu/packages/wasm.scm
> @@ -76,3 +76,33 @@ (define-public wasi-libc
>                license:bsd-2
>                ;; For wasi-libc and musl-libc.
>                license:expat))))
> +
> +(define-public wasm32-wasi-clang-runtime
> +  (package (inherit clang-runtime-16)

Please add a line break

(package
  (inherit clang-runtime-16)
   ....
)
   

> +    (native-inputs
> +     (list clang-16
> +           wasi-libc))

Should wasi-libc be placed in inputs?

> +    (inputs (list llvm-16))
> +    (arguments
> +     (list
> +      #:build-type "Release"
> +      #:tests? #f
> +      ;; Stripping binaries breaks wasm linking, resulting in the following
> +      ;; error: "archive has no index; run ranlib to add one".
> +      #:strip-binaries? #f
> +      #:configure-flags
> +      #~(list "-DCMAKE_C_COMPILER=clang"
> +              "-DCMAKE_C_COMPILER_TARGET=wasm32-wasi"
> +              (string-append
> +               "-DCMAKE_SYSROOT=" #$wasi-libc "/wasm32-wasi")

(this-package-native-input "wasi-libc")

> +              (string-append
> +               "-DCMAKE_C_FLAGS=-I " #$wasi-libc "/wasm32-wasi/include")

(this-package-native-input "wasi-libc")

> +
> +              "-DCOMPILER_RT_OS_DIR=wasi"
> +
> +              "-DCOMPILER_RT_BAREMETAL_BUILD=On"
> +              "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=On"
> +
> +              ;; WASM only needs libclang_rt.builtins-wasm32.a from
> +              ;; compiler-rt.
> +              "../source/compiler-rt/lib/builtins")))))
  
Maxim Cournoyer April 25, 2025, 8:05 a.m. UTC | #2
Hi,

Ian Eure <ian@retrospec.tv> writes:

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

[...]

> +(define-public wasm32-wasi-clang-runtime
> +  (package (inherit clang-runtime-16)

This reminds me that this should be documented, but when creating
variants (packages issued from the same source but with different build
flags, say), you must use package/inherit for the grafts machinery to
work correctly.

> +    (native-inputs
> +     (list clang-16
> +           wasi-libc))

I'd format these on one line, like you did below, since there are less
than 5 and it fits.

> +    (inputs (list llvm-16))
> +    (arguments
> +     (list
> +      #:build-type "Release"
> +      #:tests? #f

Need an explanatory comment, e.g.:   ;no test suite

> +      ;; Stripping binaries breaks wasm linking, resulting in the following
> +      ;; error: "archive has no index; run ranlib to add one".
> +      #:strip-binaries? #f
> +      #:configure-flags
> +      #~(list "-DCMAKE_C_COMPILER=clang"
> +              "-DCMAKE_C_COMPILER_TARGET=wasm32-wasi"
> +              (string-append
> +               "-DCMAKE_SYSROOT=" #$wasi-libc "/wasm32-wasi")
> +              (string-append
> +               "-DCMAKE_C_FLAGS=-I " #$wasi-libc "/wasm32-wasi/include")

As Z572 mentioned, this shouldn't reference the packages directly, to be
input-rewrite friendly.
  
Maxim Cournoyer April 25, 2025, 8:09 a.m. UTC | #3
Hi,

Ian Eure <ian@retrospec.tv> writes:

[...]

> +(define-public wasm32-wasi-clang-runtime
> +  (package (inherit clang-runtime-16)

I forgot the most important bit: inheritance cannot happen between
different modules, as it uses direct (non-delayed nor thunked)
references to the package objects and would introduce module circular
dependencies, which are surprising and not fun to hunt down.  See (info
"(guix) Cyclic Module Dependencies") for the explanation/guidelines.
  

Patch

diff --git a/gnu/packages/wasm.scm b/gnu/packages/wasm.scm
index 060e1e420f..53ea31f4a7 100644
--- a/gnu/packages/wasm.scm
+++ b/gnu/packages/wasm.scm
@@ -76,3 +76,33 @@  (define-public wasi-libc
               license:bsd-2
               ;; For wasi-libc and musl-libc.
               license:expat))))
+
+(define-public wasm32-wasi-clang-runtime
+  (package (inherit clang-runtime-16)
+    (native-inputs
+     (list clang-16
+           wasi-libc))
+    (inputs (list llvm-16))
+    (arguments
+     (list
+      #:build-type "Release"
+      #:tests? #f
+      ;; Stripping binaries breaks wasm linking, resulting in the following
+      ;; error: "archive has no index; run ranlib to add one".
+      #:strip-binaries? #f
+      #:configure-flags
+      #~(list "-DCMAKE_C_COMPILER=clang"
+              "-DCMAKE_C_COMPILER_TARGET=wasm32-wasi"
+              (string-append
+               "-DCMAKE_SYSROOT=" #$wasi-libc "/wasm32-wasi")
+              (string-append
+               "-DCMAKE_C_FLAGS=-I " #$wasi-libc "/wasm32-wasi/include")
+
+              "-DCOMPILER_RT_OS_DIR=wasi"
+
+              "-DCOMPILER_RT_BAREMETAL_BUILD=On"
+              "-DCOMPILER_RT_DEFAULT_TARGET_ONLY=On"
+
+              ;; WASM only needs libclang_rt.builtins-wasm32.a from
+              ;; compiler-rt.
+              "../source/compiler-rt/lib/builtins")))))