交叉编译haskell代码时如何安装依赖项? [英] How do I install dependencies when cross compiling haskell code?
问题描述
我已经成功创建了一个 ghc 交叉编译器,它允许我从我的 x64 linux 机器为 armv6h(在我的例子中是树莓派)编译 haskell 代码.我已经在树莓派上成功运行了 hello world 程序.
I've successfully created a ghc cross compiler, that allows me to compile haskell code for armv6h (raspberry pi in my case) from my x64 linux machine. I've successfully run a hello world program on the raspberry.
不,我想构建我的真实应用程序,它对其他 haskell 模块有很多依赖.当我为 x64 编译时,我只是这样做
No I want to build my real app, which has a lot of dependencies on other haskell modules. When I compile for x64 I simply do
cabal install dependenciy1 depenency2 ...
我知道我可以让我自己的程序成为一个阴谋项目,并自动执行这一步.但这不是重点.
I know I could make my own programm a cabal-project an automate this step. But that's not the point here.
当我尝试使用交叉编译器时
When I try to use the cross-compiler
arm-unknown-linux-gnueabi-ghc --make myapp.hs
它告诉我它找不到的模块.当然,它们没有安装!
It tells me about modules it could not find. Of course, they are not installed!
我阅读了https://ghc.haskell.org/trac/ghc/wiki/建筑/交叉编译并据此我试过
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
random 是我要在这里安装的依赖项.我收到以下错误:
random is the depenency I'm trying to install here. I get the following error:
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
当我这样做
file /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: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.10.2, not stripped
难怪它不能执行它.它是为 arm 编译的.
No wonder it can't execute it. It's compiled for arm.
我在这里遗漏了什么吗?我的目标是引入所有依赖项,然后创建一个可以在我的树莓派上部署的静态链接应用.
Am I missing something here? My goal is to pull in all dependencies, then create a statically linked app that I can deploy on my raspberry.
推荐答案
要了解此错误,您需要了解 cabal install
内部是如何工作的.本质上,它将执行以下步骤:
To understand this error, you need to know how cabal install
works internally. In essence, it will perform the following steps:
- 下载并解压源代码
- 编译
Setup.hs
(此文件用于构建系统的自定义,例如,您可以在configure
阶段实现一些钩子来运行额外的haskell代码). - 运行
setup configure
&&设置构建&&设置安装
- Download and unpack the source code
- Compile
Setup.hs
(this file is used for customization of the build system, for example, you can implement some hooks to run additional haskell code in theconfigure
phase). - Run
setup configure <configure flags> && setup build && setup install
现在的问题是 cabal install
也将 --with-ghc
给定的 GHC 用于第 2 步,但该步骤生成的可执行文件必须在主机系统!
The problem is now that cabal install
uses the GHC given by --with-ghc
also for step 2, but the executable produced by that step must run on the host system!
一种解决方法是手动执行这些步骤,这意味着您可以完全控制.首先获取源码:
A workaround is to do the steps manually, which means you have full control. First, get the source:
$ cabal get random
Downloading random-1.0.1.3...
Unpacking to random-1.0.1.3/
$ cd random-1.0.1.3
然后,使用 host ghc 编译 Setup.hs
:
Then, compile Setup.hs
, using the host ghc:
$ ghc ./Setup.hs -o setup
最后,配置、构建和安装.正如@Yuras 在评论中所建议的,我们还添加了用于运行 hsc2hs
的 -x
选项:
And finally, configure, build and install. As suggested by @Yuras in a comment, we also add the -x
option for running 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
关于这个已经有一个阴谋集团的问题:https://github.com/haskell/cabal/issues/2085
There is already a cabal issue about this: https://github.com/haskell/cabal/issues/2085
这篇关于交叉编译haskell代码时如何安装依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!