summaryrefslogtreecommitdiff
path: root/nongnu/packages/wasm.scm
blob: 910622f5a7fb90c87a915551b220bb2d7ae2aa47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2022 Pierre Langlois <pierre.langlois@gmx.com>
;;;
;;; This file is not 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 (nongnu packages wasm)
  #: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 (gnu packages llvm)
  #:use-module (gnu packages python))

(define-public wasi-libc
  (let ((commit "ad5133410f66b93a2381db5b542aad5e0964db96")
        (revision "1"))
    (package
      (name "wasi-libc")
      (version (git-version "0.1-alpha" revision commit))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url "https://github.com/WebAssembly/wasi-libc")
                      (commit commit)
                      (recursive? #t)))
                (file-name (git-file-name name version))
                (sha256
                 (base32
                  "146jamq2q24vxjfpcwlqj84wzc80cbpbg0ns2wimyvzbanah48j6"))))
      (build-system gnu-build-system)
      (native-inputs (list clang-13))
      (arguments
       (list #:tests? #f ;No test suite
             #: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-13)
    (native-inputs
     (list clang-13
           wasi-libc))
    (inputs (list llvm-13))
    (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.
              (string-append "../compiler-rt-"
                             #$(package-version clang-runtime-13)
                             ".src/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-public wasm32-wasi-clang
  (let ((base (clang-from-llvm llvm-13 wasm32-wasi-clang-runtime
                               "0zp1p6syii5iajm8v2c207s80arv00yz5ckfwimn5dng0sxiqqax")))
    (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 "13.0.0")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/llvm/llvm-project")
             (commit (string-append "llvmorg-" version))))
       (file-name (git-file-name name version))
       (sha256
        (base32
         "0cjl0vssi4y2g4nfr710fb6cdhxmn5r0vis15sf088zsc5zydfhw"))))
    (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)))