Search code examples
macoshaskellarmhaskell-stack

How to create native ARM executables on macOS using GHC/stack


GHC now (since 8.10.5) supports producing ARM code on macOS.

What flags or options in GHC or Stack do I need to use in order for it to produce ARM executables?

Context: I have an Apple silicon Mac. It can run x86_64 code via Rosetta but it can also run native ARM code.


Solution

  • I will avoid discussing cross-compilation, since I don't think it is necessary.

    For versions below GHC 9.2, you need to pass -fllvm in ghc-options. This is because GHC didn't include a Native Code Generator for the M1 yet. The Stack guide has a section on using ghc-options. You can set them globally, or you can set them for a narrower scope. You can try stack repl --resolver lts-19.1 --ghc-options -fllvm in your project to see if its dependencies are compatible with GHC 9.0.2, which is what LTS 19.1 uses.

    For versions GHC 9.2+, GHC has a Native Code Generator such that it can compile for the M1 without LLVM. This should just work, with no need to pass any special ghc-options.

    Make sure you are using Stack v2.7.5+, because this version added support for installing GHC on macOS aarch64 (which is what the M1 is). See the changelog. You can upgrade Stack using stack upgrade.

    Stack v2.7.5 hasn't been tested with versions above GHC 9.0 though, so you may want to use Cabal instead, if you choose to use GHC 9.2+. See this snippet:

    $ stack repl --compiler ghc-9.2.2
    Stack has not been tested with GHC versions above 9.0, and using 9.2.2, this may fail