[英]How to create native ARM executables on macOS using GHC/stack
GHC now ( since 8.10.5 ) supports producing ARM code on macOS. GHC 现在( 自 8.10.5 起)支持在 macOS 上生成 ARM 代码。
What flags or options in GHC or Stack do I need to use in order for it to produce ARM executables?我需要使用 GHC 或 Stack 中的哪些标志或选项才能生成 ARM 可执行文件?
Context: I have an Apple silicon Mac.上下文:我有一台 Apple Silicon Mac。 It can run
x86_64
code via Rosetta but it can also run native ARM code.它可以通过 Rosetta 运行
x86_64
代码,但也可以运行本机 ARM 代码。
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
.对于 GHC 9.2 以下的版本,您需要在
ghc-options
中传递-fllvm
。 This is because GHC didn't include a Native Code Generator for the M1 yet.这是因为 GHC 还没有包含 M1 的本机代码生成器。 The Stack guide has a section on using ghc-options .
Stack 指南中有一节是关于使用 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.你可以全局设置,也可以设置为更窄的scope。你可以在你的项目中尝试
stack repl --resolver lts-19.1 --ghc-options -fllvm
看看它的依赖是否兼容GHC 9.0.2,这就是 LTS 19.1 使用的。
For versions GHC 9.2+, GHC has a Native Code Generator such that it can compile for the M1 without LLVM.对于 GHC 9.2+ 版本,GHC 有一个本地代码生成器,因此它可以在没有 LLVM 的情况下为 M1 编译。 This should just work, with no need to pass any special
ghc-options
.这应该可以正常工作,不需要传递任何特殊
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).确保您使用的是 Stack v2.7.5+,因为此版本添加了对在 macOS aarch64(M1 就是这样)上安装 GHC 的支持。 See the changelog .
请参阅变更日志。 You can upgrade Stack using
stack upgrade
.您可以使用
stack upgrade
升级 Stack。
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+. Stack v2.7.5 尚未在 GHC 9.0 以上的版本中进行过测试,因此如果您选择使用 GHC 9.2+,则可能需要使用 Cabal。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.