From patchwork Sun Feb 11 19:20:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ian Eure X-Patchwork-Id: 23146 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 4C15C27BBEA; Sun, 11 Feb 2024 19:26:58 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,URIBL_BLOCKED,URIBL_SBL_A autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 7EFF127BBE2 for ; Sun, 11 Feb 2024 19:26:56 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rZFTU-0007Sc-5m; Sun, 11 Feb 2024 14:26:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rZFTS-0007SM-HX for guix-patches@gnu.org; Sun, 11 Feb 2024 14:26:46 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rZFTS-0002NM-9E for guix-patches@gnu.org; Sun, 11 Feb 2024 14:26:46 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rZFTi-00064W-JE for guix-patches@gnu.org; Sun, 11 Feb 2024 14:27:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#67512] [PATCH v2 3/4] gnu: Add wasm packages. References: In-Reply-To: Resent-From: Ian Eure Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 11 Feb 2024 19:27:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67512 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 67512@debbugs.gnu.org Cc: Ian Eure Received: via spool by 67512-submit@debbugs.gnu.org id=B67512.170767959223250 (code B ref 67512); Sun, 11 Feb 2024 19:27:02 +0000 Received: (at 67512) by debbugs.gnu.org; 11 Feb 2024 19:26:32 +0000 Received: from localhost ([127.0.0.1]:41626 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZFTD-00062v-OV for submit@debbugs.gnu.org; Sun, 11 Feb 2024 14:26:32 -0500 Received: from fout2-smtp.messagingengine.com ([103.168.172.145]:43097) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZFT6-00061y-Q0 for 67512@debbugs.gnu.org; Sun, 11 Feb 2024 14:26:27 -0500 Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.nyi.internal (Postfix) with ESMTP id E09CA13800BB; Sun, 11 Feb 2024 14:20:42 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Sun, 11 Feb 2024 14:20:42 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=retrospec.tv; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:message-id:mime-version:reply-to :subject:subject:to:to; s=fm2; t=1707679242; x=1707765642; bh=oB C0nCRtRxlGrAQi9DWyfMS5etQoFM0ANvepl8rXmd8=; b=bfoh59XUsJk68YyZRd mCJzlFO4wPAuN/yDbN9rPILpZ6DXE8PXKVI6tHScCig9IayrOiC2UkfdjsqkD9ZS tmVgbjd8jyXxHW4T2FsVoHA8vl2P1D7bYeBwTq/aY3oaEhKjqGG5bTE5A4oi8AeL Q4F4H7w9cpcS7YJ409v1x0pVzD50BTRxgmzCWTQ8qqOp6VY1PAOkTOors8+cATd9 k4/XRxcFxFaYo3G0Is6x8mIkEMFrnD6SMcok7GA0xnsqVvhq+ZUVlBVaj5MiHoFZ bYuez1H5nO2ZQJG9ulvuQ1aBmW/2oyabNlRuyKcmT5airQk7sttSN3GccNlzTy2v SRIA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:message-id:mime-version:reply-to:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1707679242; x=1707765642; bh=oBC0nCRtRxlGr AQi9DWyfMS5etQoFM0ANvepl8rXmd8=; b=dm2I0yi6ABycCedZytWexzV2Hw8oL shdcejVeIDfs4ATjztWulq6zIMIn4qiBKs26grWl7qvaJ5ppONoT6bykZExi6cTl Zx+QFnaK/hUslhsff0y/enSQuzTtf4gTDsa/uKVxENnS/NpVsIGpDoVv97p0CA+h xIbXxmDONYu6Cx4zY0gJOub41FOPfnrg3ShDQxyE4v0Xr4vrIJwV6E9FFMtQjXg6 Oe+IiJD/NdIj9TPnW54y4v32Tvp+xuB019vC6uvyEr7vKi5Q5Zh+A7MVwEeKVKRa 7YbdUBhzAE1n9+92OVqP6QRUMQ6WwVepPWl8TvJLrzc9ku24VsisXzosw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddugdduvdeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvfevufffkffogggtgfesthekre dtredtjeenucfhrhhomhepkfgrnhcugfhurhgvuceoihgrnhesrhgvthhrohhsphgvtgdr thhvqeenucggtffrrghtthgvrhhnpeehteevieevffevtdefvedujeekheeigeegteevff etffevjedtudegudegvdffkeenucffohhmrghinhepghhnuhdrohhrghdpghhithhhuhgs rdgtohhmpdhmohiiihhllhgrrdhorhhgpdifrghsihdruggvvhdplhhlvhhmrdhorhhgne cuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepihgrnhes rhgvthhrohhsphgvtgdrthhv X-ME-Proxy: Feedback-ID: id9014242:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 11 Feb 2024 14:20:42 -0500 (EST) From: Ian Eure Date: Sun, 11 Feb 2024 11:20:26 -0800 Message-ID: <20240211192031.32284-4-ian@retrospec.tv> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * 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 +;;; +;;; 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 . + +(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.")))