unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Ian Eure <ian@retrospec.tv>
To: 67512@debbugs.gnu.org
Cc: Ian Eure <ian@retrospec.tv>
Subject: [bug#67512] [PATCH v3 3/4] gnu: Add wasm packages.
Date: Sun, 11 Feb 2024 15:49:43 -0800	[thread overview]
Message-ID: <20240211234948.7568-3-ian@retrospec.tv> (raw)
In-Reply-To: <cover.1701186634.git.ian@retrospec.tv>

* gnu/packages/wasm.scm (wasi-libc): New variable.
* gnu/packages/wasm.scm (wasm32-wasi-clang-runtime): New variable.
* gnu/packages/wasm.scm (wasm32-wasi-clang): New variable.
* gnu/packages/wasm.scm (wasm32-wasi-libcxx): New variable.
* gnu/packages/wasm.scm (wasm32-wasi-clang-toolchain): New variable.
---
 gnu/packages/wasm.scm | 273 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 273 insertions(+)
 create mode 100644 gnu/packages/wasm.scm

diff --git a/gnu/packages/wasm.scm b/gnu/packages/wasm.scm
new file mode 100644
index 0000000000..05d247f333
--- /dev/null
+++ b/gnu/packages/wasm.scm
@@ -0,0 +1,273 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2022-2023 Pierre Langlois <pierre.langlois@gmx.com>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu packages wasm)
+  #:use-module (guix base32)
+  #:use-module (guix gexp)
+  #:use-module (guix packages)
+  #:use-module ((guix licenses) #:prefix license:)
+  #:use-module (guix utils)
+  #:use-module (guix git-download)
+  #:use-module (guix build-system cmake)
+  #:use-module (guix build-system gnu)
+  #:use-module (guix build-system trivial)
+  #:use-module (gnu packages bash)
+  #:use-module (gnu packages llvm)
+  #:use-module (gnu packages python))
+
+(define-public wasi-libc
+  (package
+    (name "wasi-libc")
+    (version "sdk-19")
+    (source
+     (origin
+       (method git-fetch)
+       (uri (git-reference
+             (url "https://github.com/WebAssembly/wasi-libc")
+             (commit (string-append "wasi-" version))
+             (recursive? #t)))
+       (file-name (git-file-name name version))
+       (sha256
+        (base32 "0bnpz8wk9wiic938296gxp4vz820bvpi1w41jksjzz5552hql169"))))
+    (build-system gnu-build-system)
+    (native-inputs (list clang-15))
+    (arguments
+     (list
+      #:tests? #f ;No test suite
+      ;; Firefox uses wasm2c to compile WebAssembly to C code, and it
+      ;; does not support the memory.copy opcode.
+      ;; See https://bugzilla.mozilla.org/show_bug.cgi?id=1773200#c4
+      #:make-flags ''("BULK_MEMORY_SOURCES=")
+      #:phases #~(modify-phases %standard-phases
+                   (delete 'configure)
+                   (add-before 'build 'set-sysroot-include
+                     (lambda _
+                       (setenv "C_INCLUDE_PATH"
+                               (string-append (getcwd) "/sysroot/include"))))
+                   (add-before 'install 'set-install-dir
+                     (lambda _
+                       (setenv "INSTALL_DIR"
+                               (string-append #$output "/wasm32-wasi")))))))
+    (home-page "https://wasi.dev")
+    (synopsis "WASI libc implementation for WebAssembly")
+    (description
+     "WASI Libc is a libc for WebAssembly programs built on top of WASI
+system calls.  It provides a wide array of POSIX-compatible C APIs, including
+support for standard I/O, file I/O, filesystem manipulation, memory
+management, time, string, environment variables, program startup, and many
+other APIs.")
+    (license (list
+              ;; For wasi-libc, with LLVM exceptions
+              license:asl2.0
+              ;; For malloc.c.
+              license:cc0
+              ;; For cloudlibc.
+              license:bsd-2
+              ;; For wasi-libc and musl-libc.
+              license:expat))))
+
+(define-public wasm32-wasi-clang-runtime
+  (package (inherit clang-runtime-15)
+    (native-inputs
+     (list clang-15
+           wasi-libc))
+    (inputs (list llvm-15))
+    (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")))))
+
+;; FIXME: Ideally we wouldn't need to build a separate compiler because clang
+;; can support multiple targets at runtime.  However Guix patches the default
+;; clang with a specific clang-runtime package.  It would be good to improve
+;; upstream Guix's support for cross-compiling with clang.
+
+(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-15 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))))))))
+
+(define-public wasm32-wasi-libcxx
+  (package
+    (name "wasm32-wasi-libcxx")
+    (version (package-version llvm-15))
+    (source
+     (llvm-monorepo version))
+    (build-system cmake-build-system)
+    (arguments
+     (list
+      #:configure-flags #~(list (string-append "-S ../source/runtimes")
+
+                                "-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi"
+
+                                (string-append "-DCMAKE_SYSROOT="
+                                               #$wasi-libc "/wasm32-wasi")
+
+                                (string-append "-DCMAKE_INCLUDE_PATH="
+                                               #$wasi-libc
+                                               "/wasm32-wasi/include")
+
+                                (string-append "-DCMAKE_STAGING_PREFIX="
+                                               #$output "/wasm32-wasi")
+
+                                "-DCMAKE_C_COMPILER=clang"
+                                "-DCMAKE_C_COMPILER_WORKS=ON"
+                                "-DCMAKE_CXX_COMPILER=clang++"
+                                "-DCMAKE_CXX_COMPILER_WORKS=ON"
+                                "-DCMAKE_C_COMPILER_TARGET=wasm32-wasi"
+                                "-DCMAKE_CXX_COMPILER_TARGET=wasm32-wasi"
+
+                                "-DLIBCXX_LIBDIR_SUFFIX=/wasm32-wasi"
+
+                                "-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
+                                "-DLIBCXX_ENABLE_SHARED=OFF"
+                                "-DLIBCXX_ENABLE_THREADS=OFF"
+                                "-DLIBCXX_ENABLE_FILESYSTEM=OFF"
+
+                                "-DLIBCXXABI_LIBDIR_SUFFIX=/wasm32-wasi"
+
+                                "-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
+                                "-DLIBCXXABI_ENABLE_SHARED=OFF"
+                                "-DLIBCXXABI_ENABLE_THREADS=OFF"
+                                "-DLIBCXXABI_ENABLE_FILESYSTEM=OFF")
+      #:tests? #f
+      #:phases #~(modify-phases %standard-phases
+                   (add-after 'set-paths 'adjust-CPLUS_INCLUDE_PATH
+                     (lambda _
+                       (setenv "CPLUS_INCLUDE_PATH"
+                               (string-append #$wasi-libc
+                                              "/wasm32-wasi/include:"
+                                              (getenv "CPLUS_INCLUDE_PATH"))))))))
+    (native-inputs (list lld python wasm32-wasi-clang))
+    (inputs (list wasi-libc))
+    (home-page "https://libcxx.llvm.org")
+    (synopsis "C++ standard library for WebAssembly")
+    (description
+     "This package provides an implementation of the C++ standard library for
+use with Clang, targeting C++11, C++14 and above.  This package targets
+WebAssembly with WASI.")
+    (license license:expat)))
+
+(define-public wasm32-wasi-clang-toolchain
+  (package
+    (name "wasm32-wasi-clang-toolchain")
+    (version (package-version wasm32-wasi-clang))
+    (source
+     #f)
+    (build-system trivial-build-system)
+    (arguments
+     (list
+      #:builder (with-imported-modules '((guix build union)
+                                         (guix build utils))
+                                       #~(begin
+                                           (use-modules (guix build union)
+                                                        (guix build utils))
+                                           (union-build #$output
+                                                        (list #$wasm32-wasi-clang-runtime
+                                                              #$wasi-libc
+                                                              #$wasm32-wasi-libcxx))
+                                           (mkdir-p (string-append #$output
+                                                                   "/bin"))
+
+                                           ;; We provide clang and clang++ via a wrapped program that sets
+                                           ;; include paths correctly so that it does not include paths from
+                                           ;; the host.
+                                           
+                                           ;; FIXME: Review how we can provide better support for
+                                           ;; cross-compiling with clang in Guix, maybe adding support for
+                                           ;; the CROSS_C_INCLUDE_PATH and CROSS_CPLUS_INCLUDE_PATH
+                                           ;; environment variables like GCC.
+                                           
+                                           (for-each (lambda (bin)
+                                                       (symlink (string-append #$wasm32-wasi-clang
+                                                                 bin)
+                                                                (string-append #$output
+                                                                 bin))
+                                                       (wrap-program (string-append #$output
+                                                                      bin)
+                                                         #:sh (string-append #$bash-minimal
+                                                               "/bin/bash")
+                                                         `("C_INCLUDE_PATH"
+                                                           ":" =
+                                                           (,(string-append #$output
+                                                              "/wasm32-wasi/include")))
+                                                         `("CPLUS_INCLUDE_PATH"
+                                                           ":" =
+                                                           ;; Make sure inclure/c++/v1 comes first for #include_next
+                                                           ;; to work.
+                                                           (,(string-append #$output
+                                                              "/wasm32-wasi/include/c++/v1") ,
+                                                            (string-append #$output
+                                                             "/wasm32-wasi/include")))))
+                                                     '("/bin/clang"
+                                                       "/bin/clang++"))
+
+                                           (symlink (string-append #$lld
+                                                     "/bin/wasm-ld")
+                                                    (string-append #$output
+                                                     "/bin/wasm-ld"))))))
+    (inputs (list bash-minimal
+                  lld
+                  wasi-libc
+                  wasm32-wasi-clang
+                  wasm32-wasi-clang-runtime
+                  wasm32-wasi-libcxx))
+    (license (cons (package-license wasm32-wasi-clang)
+                   (package-license wasi-libc)))
+    (home-page "https://clang.llvm.org")
+    (synopsis
+     "Complete Clang toolchain for C/C++ development, for WebAssembly.")
+    (description
+     "This package provides a complete Clang toolchain for C/C++
+development targeting WebAssembly with WASI.  This includes Clang, as well as
+libc, libc++ and wasm-ld.")))
-- 
2.39.2





  parent reply	other threads:[~2024-02-11 23:51 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-28 20:09 [bug#67512] [PATCH 0/5] Add LibreWolf Ian Eure
2023-11-28 20:13 ` [bug#67512] [PATCH 1/5] gnu: icu4c: Update to 73.1 Ian Eure
2023-11-28 20:13 ` [bug#67512] [PATCH 2/5] gnu: Add icu4c-71 Ian Eure
2023-11-28 20:13 ` [bug#67512] [PATCH 3/5] gnu: node: Switch to icu4c-71 Ian Eure
2023-11-28 20:13 ` [bug#67512] [PATCH 4/5] gnu: Add wasm packages Ian Eure
2023-11-28 20:13 ` [bug#67512] [PATCH 5/5] gnu: Add librewolf Ian Eure
2023-12-10 21:57 ` [bug#67512] [PATCH 0/5] Add LibreWolf Ludovic Courtès
2023-12-10 22:28   ` Ian Eure
2023-12-14 18:39     ` Ludovic Courtès
2024-01-13 17:53 ` [bug#67512] [PATCH 5/5] gnu: Add librewolf Herman Rimm via Guix-patches via
2024-01-28 21:23   ` Ian Eure
2024-01-28 22:51     ` Ian Eure
2024-02-02 18:44     ` Herman Rimm via Guix-patches via
2024-02-06 23:29       ` Ian Eure
2024-02-07 10:06         ` Clément Lassieur
2024-01-17 15:43 ` [bug#67512] [PATCH 0/5] Add LibreWolf Clément Lassieur
2024-02-17 16:06   ` Ian Eure
2024-02-11 19:20 ` [bug#67512] [PATCH v2 0/4] Add Librewolf Ian Eure
2024-02-11 20:22   ` Clément Lassieur
2024-02-11 23:23     ` Ian Eure
2024-02-11 23:32       ` Clément Lassieur
2024-02-11 19:20 ` [bug#67512] [PATCH v2 1/4] gnu: Move icu4c-73 from gnuzilla to icu4c Ian Eure
2024-02-11 21:40   ` Mark H Weaver
2024-02-11 22:14   ` Clément Lassieur
2024-02-11 23:23     ` Ian Eure
2024-02-11 23:34       ` Clément Lassieur
2024-02-11 19:20 ` [bug#67512] [PATCH v2 2/4] gnu: nss: Update to 3.97 Ian Eure
2024-02-11 19:20 ` [bug#67512] [PATCH v2 3/4] gnu: Add wasm packages Ian Eure
2024-02-11 19:20 ` [bug#67512] [PATCH v2 4/4] gnu: Add librewolf Ian Eure
2024-02-11 23:49 ` [bug#67512] [PATCH v3 1/4] gnu: Move icu4c-73 from gnuzilla to icu4c Ian Eure
2024-02-11 23:49 ` [bug#67512] [PATCH v3 2/4] gnu: nss: Update to 3.97 Ian Eure
2024-02-11 23:49 ` Ian Eure [this message]
2024-02-11 23:49 ` [bug#67512] [PATCH v3 4/4] gnu: Add librewolf Ian Eure
2024-02-13 20:34 ` [bug#67512] [PATCH v4 0/4] Add LibreWolf Ian Eure
2024-02-13 20:34   ` [bug#67512] [PATCH v4 1/4] gnu: Move icu4c-73 from gnuzilla to icu4c Ian Eure
2024-02-14 18:05     ` Mark H Weaver
2024-02-16 14:02       ` Clément Lassieur
2024-02-17 16:07         ` Ian Eure
2024-02-21 11:49           ` Clément Lassieur
2024-02-22  1:07             ` Ian Eure
2024-02-22 10:21               ` Clément Lassieur
2024-02-22 14:34                 ` Mark H Weaver
2024-02-23  0:56                   ` Ian Eure
2024-02-13 20:34   ` [bug#67512] [PATCH v4 2/4] gnu: nss: Update to 3.97 Ian Eure
2024-02-13 20:34   ` [bug#67512] [PATCH v4 3/4] gnu: Add wasm packages Ian Eure
2024-02-13 22:41     ` Liliana Marie Prikler
2024-02-13 23:22       ` Ian Eure
2024-02-16 14:29         ` Clément Lassieur
2024-02-16 21:48           ` Mark H Weaver
2024-02-17 16:09           ` Ian Eure
2024-02-21  2:00             ` Clément Lassieur
2024-02-21  2:18               ` Ian Eure
2024-02-21  5:20                 ` Liliana Marie Prikler
2024-02-21 11:45                   ` Clément Lassieur
2024-02-22  0:07                     ` Ian Eure
2024-02-22 10:19                       ` Clément Lassieur
2024-03-09 21:20                         ` Ian Eure
2024-02-21 14:32               ` Mark H Weaver
2024-02-13 20:34   ` [bug#67512] [PATCH v4 4/4] gnu: Add librewolf Ian Eure
2024-02-25  0:53 ` [bug#67512] [PATCH v5 0/2] Add LibreWolf Ian Eure
2024-02-25  0:53   ` [bug#67512] [PATCH v5 1/2] gnu: nss: Update to 3.97 Ian Eure
2024-02-25  0:53   ` [bug#67512] [PATCH v5 2/2] gnu: Add librewolf Ian Eure
2024-03-13  9:07 ` [bug#67512] Feedback for Librewolf package Romain Garbage
2024-03-15 14:05   ` Ian Eure
2024-03-29 22:34 ` [bug#67512] [PATCH v6 0/2] Add LibreWolf Ian Eure
2024-03-29 22:34   ` [bug#67512] [PATCH v6 1/2] gnu: nss: Update to 3.98 Ian Eure
2024-03-29 22:34   ` [bug#67512] [PATCH v6 2/2] gnu: Add librewolf Ian Eure
2024-04-06 15:04 ` [bug#67512] [PATCH v7 0/3] Add LibreWolf Ian Eure
2024-04-06 15:04   ` [bug#67512] [PATCH v7 1/3] gnu: Add nss-3.98 Ian Eure
2024-04-06 15:04   ` [bug#67512] [PATCH v7 2/3] gnu: Add nss-certs-3.98 Ian Eure
2024-04-06 15:04   ` [bug#67512] [PATCH v7 3/3] gnu: Add librewolf Ian Eure
2024-04-12 13:11   ` bug#67512: [PATCH v7 0/3] Add LibreWolf Andrew Tropin via Guix-patches via
2024-04-27 10:46     ` [bug#67512] " Clément Lassieur
     [not found]       ` <87r0eqww7t.fsf@meson>
2024-04-27 18:21         ` Ian Eure
2024-04-12 11:32 ` [bug#67512] [PATCH 0/5] " Sharlatan Hellseher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240211234948.7568-3-ian@retrospec.tv \
    --to=ian@retrospec.tv \
    --cc=67512@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).