2

How can Haskell be compiled to Javascript using GHCJS and Haskell Stack?

I have the current version of Haksell Stack 1.5.1.

I can initialize a GHCJS project with this command: stack new ghcjsSetup ghcjs

Once in the project directory I try following the Stack documentation for GHCJS.

I create a stack.yaml file and edit it to match the example from the documentation so that is looks like this.

resolver: lts-7.19
compiler: ghcjs-0.2.1.9007019_ghc-8.0.1
compiler-check: match-exact

setup-info:
  ghcjs:
    source:
      ghcjs-0.2.1.9007019_ghc-8.0.1:
        url: http://ghcjs.tolysz.org/ghc-8.0-2017-02-05-lts-7.19-9007019.tar.gz
        sha1: d2cfc25f9cda32a25a87d9af68891b2186ee52f9

Following the documentation further I try to install and boot GHCJS with this command:

stack setup

This command produces an output which ends with an error.

Preparing to install GHCJS to an isolated location.
This will not interfere with any system-level installation.
Preparing to download ghcjs-0.2.1.9007019_ghc-8.0.1 ...
Already downloaded.
Unpacking GHCJS into /home/u/.stack/programs/x86_64-linux/ghcjs-0.2.1.9007019_ghc-8.0.1/src/ ...
Setting up GHCJS build environment
Installing GHCJS (this will take a long time) ...
ghcjs-0.2.1.9007019: configure (lib + exe)
Package ghcjs uses a custom Cabal build, but does not use a custom-setup stanza
Using the explicit setup deps approach based on configuration
Strongly recommend fixing the package's cabal file
[1 of 2] Compiling Main             ( /home/u/.stack/programs/x86_64-linux/ghcjs-0.2.1.9007019_ghc-8.0.1/src/Setup.hs, /home/u/.stack/programs/x86_64-linux/ghcjs-0.2.1.9007019_ghc-8.0.1/src/.stack-work/dist/x86_64-linux/Cabal-1.24.0.0/setup/Main.o )
[2 of 2] Compiling StackSetupShim   ( /home/u/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs, /home/u/.stack/programs/x86_64-linux/ghcjs-0.2.1.9007019_ghc-8.0.1/src/.stack-work/dist/x86_64-linux/Cabal-1.24.0.0/setup/StackSetupShim.o )
Linking /home/u/.stack/programs/x86_64-linux/ghcjs-0.2.1.9007019_ghc-8.0.1/src/.stack-work/dist/x86_64-linux/Cabal-1.24.0.0/setup/setup ...
Configuring ghcjs-0.2.1.9007019...
ghcjs-0.2.1.9007019: build (lib + exe)
Preprocessing library ghcjs-0.2.1.9007019...

/home/u/.stack/programs/x86_64-linux/ghcjs-0.2.1.9007019_ghc-8.0.1/src/src/Compiler/JMacro/Base.hs:1:55: warning:
    -XOverlappingInstances is deprecated: instead use per-instance pragmas OVERLAPPING/OVERLAPPABLE/OVERLAPS

/home/u/.stack/programs/x86_64-linux/ghcjs-0.2.1.9007019_ghc-8.0.1/src/src/Compiler/JMacro/QQ.hs:1:55: warning:
    -XOverlappingInstances is deprecated: instead use per-instance pragmas OVERLAPPING/OVERLAPPABLE/OVERLAPS
[ 1 of 50] Compiling GHCJS.Prim.TH.Serialized ( lib/ghcjs-prim/GHCJS/Prim/TH/Serialized.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/GHCJS/Prim/TH/Serialized.o )

Because it is too long to post here I have excluded the middle of the output. Which includes lots of warning messages.

[49 of 50] Compiling Compiler.Program ( src/Compiler/Program.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/Compiler/Program.o )
[50 of 50] Compiling GHCJS            ( src/GHCJS.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/GHCJS.o )
Preprocessing executable 'ghcjs' for ghcjs-0.2.1.9007019...
[1 of 1] Compiling Main             ( src-bin/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/ghcjs/ghcjs-tmp/Main.o )
Linking .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/ghcjs/ghcjs ...
Preprocessing executable 'ghcjs-boot' for ghcjs-0.2.1.9007019...
[1 of 1] Compiling Main             ( src-bin/Boot.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/ghcjs-boot/ghcjs-boot-tmp/Main.o )

/home/u/.stack/programs/x86_64-linux/ghcjs-0.2.1.9007019_ghc-8.0.1/src/src-bin/Boot.hs:690:1: warning: [-Wtabs]
    Tab character found here, and in three further locations.
    Please use spaces instead.
Linking .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/ghcjs-boot/ghcjs-boot ...
Preprocessing executable 'ghcjs-pkg' for ghcjs-0.2.1.9007019...
[1 of 1] Compiling Main             ( src-bin/Pkg.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/ghcjs-pkg/ghcjs-pkg-tmp/Main.o )
Linking .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/ghcjs-pkg/ghcjs-pkg ...
/usr/bin/ld: cannot find -ltinfo
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)

--  While building package ghcjs-0.2.1.9007019 using:
      /home/u/.stack/programs/x86_64-linux/ghcjs-0.2.1.9007019_ghc-8.0.1/src/.stack-work/dist/x86_64-linux/Cabal-1.24.0.0/setup/setup --builddir=.stack-work/dist/x86_64-linux/Cabal-1.24.0.0 build lib:ghcjs exe:ghcjs exe:ghcjs-boot exe:ghcjs-pkg exe:ghcjs-run exe:haddock-ghcjs exe:hsc2hs-ghcjs --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1
user8876236
  • 105
  • 5
  • The package which the linker cannot find is [this one](https://packages.debian.org/search?keywords=libtinfo-dev). Do you have it installed? – user2407038 Nov 02 '17 at 22:58
  • @user2407038 Thanks for pointing out the Debian package `libtinfo-dev`. When I do `stack setup` after installing that package it gets by that error. Now I get "fatal: program node is required but could not be found at node". I will install node and try again. – user8876236 Nov 03 '17 at 00:19
  • I have installed nodejs using the Debian package manager but when I try `stack setup` I am still getting the same error message. So I am not sure what it is referring to when it says node. – user8876236 Nov 03 '17 at 00:53
  • Different distros call the executable either node or nodejs, IIRC. You may need to create a symlink for the other name – Michael Snoyman Nov 03 '17 at 04:13
  • @MichaelSnoyman yes you are right. In Debian there is another package that symlinks nodejs to node called `nodejs-legacy`. After installing that I am able to do finish `stack setup` and also `stack build`. And the alert saying "hello from GHCJS!" pops up when I open the index.html in the executables directory. So it is working now. – user8876236 Nov 03 '17 at 11:40

1 Answers1

1

"Linking .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/ghcjs-pkg/ghcjs-pkg ... /usr/bin/ld: cannot find -ltinfo"

You are missing the libtinfo library which is part of ncurses. Depending on your environment try something like:

sudo apt-get install libtinfo-dev


As an aside, getting all the pieces that ghcjs needs to build successfully is tricky and not well documented as of September 2018. One might hope that as the project matures it will get easier to install, and that a binary package will be produced. Know that you are not the only person who has these issues. It takes some patience and trial-and-error to get ghcjs working right now.

Others have had success using nix, I did not.

John F. Miller
  • 26,961
  • 10
  • 71
  • 121