All Projects → WebGHC → Wasm Cross

WebGHC / Wasm Cross

Nix expressions for cross compiling to WebAssembly

Labels

Projects that are alternatives of or similar to Wasm Cross

Nix Linter
Linter for the Nix expression language
Stars: ✭ 100 (-18.03%)
Mutual labels:  nix
Nixos Config
My NixOS configuration files. (This public mirror is not updated anymore.)
Stars: ✭ 110 (-9.84%)
Mutual labels:  nix
Nix Emacs Ci
Emacs installations for continuous integration
Stars: ✭ 114 (-6.56%)
Mutual labels:  nix
Cached Nix Shell
Instant startup time for nix-shell
Stars: ✭ 103 (-15.57%)
Mutual labels:  nix
Nix Haskell Monorepo
Pragmatic tutorial on how to use nix with a haskell monorepo
Stars: ✭ 111 (-9.02%)
Mutual labels:  nix
Nix Deploy
Deploy software or an entire NixOS system configuration to another NixOS system
Stars: ✭ 111 (-9.02%)
Mutual labels:  nix
Nixcloud Webservices
This nixpkgs extension, called nixcloud-webservices, focuses on ease of deployment of web-related technologies.
Stars: ✭ 98 (-19.67%)
Mutual labels:  nix
Hie Nix
Nix packages for Haskell IDE Engine
Stars: ✭ 118 (-3.28%)
Mutual labels:  nix
Yarn2nix
Generate nix expressions from a yarn.lock file [maintainer=???]
Stars: ✭ 110 (-9.84%)
Mutual labels:  nix
Iohk Ops
NixOps deployment configuration for IOHK devops
Stars: ✭ 113 (-7.38%)
Mutual labels:  nix
Nix Tree
Interactively browse the dependency graph of your Nix derivations.
Stars: ✭ 104 (-14.75%)
Mutual labels:  nix
Powerline Rs
GitLab: https://gitlab.com/jD91mZM2/powerline-rs
Stars: ✭ 108 (-11.48%)
Mutual labels:  nix
Nix Config
My NixOS configuration
Stars: ✭ 112 (-8.2%)
Mutual labels:  nix
Nixos Config
Mirror of https://code.balsoft.ru/balsoft/nixos-config
Stars: ✭ 100 (-18.03%)
Mutual labels:  nix
Docker
Dockerfiles to package Nix in a minimal docker container
Stars: ✭ 114 (-6.56%)
Mutual labels:  nix
Styx
Static site generator in Nix expression language.
Stars: ✭ 99 (-18.85%)
Mutual labels:  nix
Nixwrt
Build images for embedded MIPS SoCs using NixPkgs (experimental)
Stars: ✭ 111 (-9.02%)
Mutual labels:  nix
Nixgl
A wrapper tool for nix OpenGL application
Stars: ✭ 120 (-1.64%)
Mutual labels:  nix
Mbp Nixos
Instructions and scripts related to getting NixOS running on a newer generation MBP
Stars: ✭ 115 (-5.74%)
Mutual labels:  nix
Flake Utils
Pure Nix flake utility functions
Stars: ✭ 113 (-7.38%)
Mutual labels:  nix

wasm-cross

wasm-cross is a toolchain for cross compiling C and Haskell to WebAssembly, using the WebGHC and LLVM

The generated WebAssembly binary can be run on a browser using the webabi library.

It currently provides a minimal ABI, sufficient to run the Haskell code using the jsaddle library with jsaddle-wasm

Quick start

Setup nix cache

  • If you're using linux, a nix binary cache is available at https://nixcache.webghc.org with a public key of:

    hydra.webghc.org-1:knW30Yb8EXYxmUZKEl0Vc6t2BDjAUQ5kfC1BKJ9qEG8=

  • When using reflex-platform you may want to add its cache as well: https://nixcache.reflex-frp.org

    ryantrinkle.com-1:JJiAKaRv9mWgpVAz8dwewnZe0AzzEAzPkagE9SP5NWI=

Build an example app

git clone [email protected]:WebGHC/wasm-cross.git
cd wasm-cross;

Reflex TodoMVC

nix-build release.nix -A examples.wasm.reflex-todomvc

Miso 2048

nix-build release.nix -A examples.wasm._2048

Nix based build with TemplateHaskell support

Template Haskell

For Template Haskell support the save/load-splices infrastructure is used. This was initially created to support the arm cross-compilation, but it works equally well for WebGHC.

This works by first compiling the code for x86/64 arch, and then using the splices generated by it in the wasm compilation. It needs the reflex-platform's nix wizardry to achieve this. It works well for the straightforward use cases of Template Haskell, but may break for more complicated uses.

Note: use of reflex is not required to use reflex-platform, it should work well for Miso too.

In order to support TemplateHaskell the reflex-platform and nix based build is necessary.

See the example JSaddle app and the example Reflex app

Cabal based build without TemplateHaskell support

If you dont need TemplateHaskell support, then you can use only cabal to do the builds, which works much faster as it can do builds incrementally.

The reflex-platform is used here as it provides an easier to use interface for creating a nix-shell with all the dependencies necessary for a cabal project.

Get the latest reflex-platform from https://github.com/reflex-frp/reflex-platform/

  • For a JSaddle app, add the jsaddle-wasm in your executable's cabal file, and put the -threaded option in if !arch(wasm32) block.

Note: reflex-dom library from v0.7 onwards already includes jsaddle-wasm dependency, so it is not necessary to modify anything.

TODO: add instructions for miso and reflex-dom < 0.7

  • Use the work-on script from reflex-platform to obtain a shell with WebGHC
<reflex-platform>/scripts/work-on wasm ./.

Then do the cabal configure with following options, followed by cabal build

cabal configure --configure-option=--host=wasm32-unknown-unknown-wasm --with-ghc=wasm32-unknown-unknown-wasm-ghc --with-ghc-pkg=wasm32-unknown-unknown-wasm-ghc-pkg  --with-gcc=wasm32-unknown-unknown-wasm-cc --with-ld=wasm32-unknown-unknown-wasm-ld --with-ar=wasm32-unknown-unknown-wasm-ar --with-hsc2hs=wasm32-unknown-unknown-wasm-hsc2hs
cabal build

Limitations / Known issues

  • The -threaded option is not supported with WebGHC. You will have to put to modify the cabal file to exclude this option like this:
  if !arch(wasm32)
    ghc-options: -threaded
  • The -Werror option is known to cause compilation failure as it converts the clang warnings to errors. This is a temporary issue, as it should be possible to get rid of clang warnings altogether.

Other details

The C toolchain is made up of Clang / LLVM, LLD, and a fork of musl.

The Haskell compilation is done using WebGHC, which is a fork of GHC

GHC cross compilers are built from a fork using the Nix infrastructure largely developed by John Ericson (@Ericson2314), producing a working haskellPackages.

webabi is the "kernel" to support musl's syscalls.

Notes

  • We have forked nixpkgs just to support the wasm32-unknown-unknown-wasm compilation target, as this support cannot be provided by overlays. All other changes needed to nixpkgs, which cannot be done by overlays, have already been upstreamed.

    The choice of target triple is still under debate, with wasm32-unknown-unknown or wasm32-unknown-unknown-webghc being other candidates.

  • The wasm-cross contains some code for nodejs support, but unfortunately it has not been maintained. If there is interest in running the executables outside the browser environment, then this could be resurrected with a little effort.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].