在交叉编译haskell代码时如何安装依赖项?

Dan*_*itz 15 haskell cross-compiling ghc cabal-install raspberry-pi

我已经成功创建了一个ghc交叉编译器,它允许我从我的x64 linux机器编译armv6h的haskell代码(在我的例子中是raspberry pi).我已成功在树莓上运行了一个hello world程序.

不,我想构建我真正的应用程序,它对其他haskell模块有很多依赖.当我为x64编译时,我就是这么做的

cabal install dependenciy1 depenency2 ...
Run Code Online (Sandbox Code Playgroud)

我知道我可以让我自己的程序成为一个cabal项目,自动完成这一步.但这不是重点.

当我尝试使用交叉编译器时

arm-unknown-linux-gnueabi-ghc --make myapp.hs
Run Code Online (Sandbox Code Playgroud)

它告诉我它找不到的模块.当然,他们没有安装!

我阅读https://ghc.haskell.org/trac/ghc/wiki/Building/CrossCompiling 并根据我尝试过

cabal --with-ghc=arm-unknown-linux-gnueabi-ghc --with-ghc-pkg=arm-unknown-linux-gnueabi-ghc-pkg --with-ld=arm-unknown-linux-gnueabi-ld install random
Run Code Online (Sandbox Code Playgroud)

随机是我正在尝试安装的依赖性.我收到以下错误:

Resolving dependencies...
Configuring random-1.0.1.3...
Failed to install random-1.0.1.3
Last 10 lines of the build log ( /home/daniel/.cabal/logs/random-1.0.1.3.log ):
/home/daniel/.cabal/setup-exe-cache/setup-Cabal-1.18.1.3-arm-linux-ghc-7.8.3.20140804: /home/daniel/.cabal/setup-exe-cache/setup-Cabal-1.18.1.3-arm-linux-ghc-7.8.3.20140804:      cannot execute binary file
cabal: Error: some packages failed to install:
random-1.0.1.3 failed during the configure step. The exception was:
ExitFailure 126
Run Code Online (Sandbox Code Playgroud)

当我做

file /home/daniel/.cabal/setup-exe-cache/setup-Cabal-1.18.1.3-arm-linux-ghc-7.8.3.20140804
Run Code Online (Sandbox Code Playgroud)

我明白了

/home/daniel/.cabal/setup-exe-cache/setup-Cabal-1.18.1.3-arm-linux-ghc-7.8.3.20140804: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.10.2, not stripped
Run Code Online (Sandbox Code Playgroud)

难怪它无法执行它.它是为arm编译的.

我在这里错过了什么吗?我的目标是引入所有依赖项,然后创建一个静态链接的应用程序,我可以在我的树莓上部署.

ben*_*ofs 11

要了解此错误,您需要了解cabal install内部的工作原理.实质上,它将执行以下步骤:

  1. 下载并解压缩源代码
  2. 编译Setup.hs(此文件用于构建系统的自定义,例如,您可以实现一些钩子以在configure阶段中运行其他haskell代码).
  3. setup configure <configure flags> && setup build && setup install

问题是现在cabal install使用的GHC --with-ghc也用于步骤2,但该步骤生成的可执行文件必须在主机系统上运行!

解决方法是手动执行这些步骤,这意味着您可以完全控制.首先,获取来源:

$ cabal get random
Downloading random-1.0.1.3...
Unpacking to random-1.0.1.3/
$ cd random-1.0.1.3
Run Code Online (Sandbox Code Playgroud)

然后,Setup.hs使用主机 ghc进行编译:

$ ghc ./Setup.hs -o setup
Run Code Online (Sandbox Code Playgroud)

最后,配置,构建和安装.正如@Yuras在评论中所建议的,我们还添加了-x运行选项hsc2hs:

$ ./setup configure ----with-ghc=arm-unknown-linux-gnueabi-ghc --with-ghc-pkg=arm-unknown-linux-gnueabi-ghc-pkg --with-ld=arm-unknown-linux-gnueabi-ld --hsc2hs-options=-x
$ ./setup build && ./setup install
Run Code Online (Sandbox Code Playgroud)

关于此问题已经有一个问题:https://github.com/haskell/cabal/issues/2085