给 PI zero W 交叉编译 haskell 的应用

使用 PI zero W 直接编译还是 chroot 来编译都是很慢的,于是我就尝试了交叉编译,这次我在 MacOS 上做这件事情,在 Linux 也是类似的。

The SDK

安装 llvm

llvm 现在可以直接生产不同平台的二进制文件,所以我们用他来做交叉编译的基础编译器。

mkdir -p ${HOME}/raspbian-sdk/{prebuilt,sysroot}
wget http://releases.llvm.org/6.0.0/clang+llvm-6.0.0-x86_64-apple-darwin.tar.xz
tar xvf clang+llvm-6.0.0-x86_64-apple-darwin.tar.xz -C ${HOME}/raspbian-sdk/prebuilt --strip-components=1

安装 binutils

wget https://ftp.gnu.org/gnu/binutils/binutils-2.29.1.tar.bz2

tar xvf binutils-2.29.1.tar.bz2
cd binutils-2.29.1
./configure --prefix="${HOME}/raspbian-sdk/prebuilt" \
            --target=arm-linux-gnueabihf \
            --enable-gold=yes \
            --enable-ld=yes \
            --enable-targets=arm-linux-gnueabihf \
            --enable-multilib \
            --enable-interwork \
            --disable-werror \
            --quiet
make
make install

同步 PI zero W 系统

rsync -rzLR --safe-links pi@raspberrypi:/usr/lib/arm-linux-gnueabihf ${HOME}/raspbian-sdk/sysroot
rsync -rzLR --safe-links pi@raspberrypi:/usr/lib/gcc/arm-linux-gnueabihf ${HOME}/raspbian-sdk/sysroot
rsync -rzLR --safe-links pi@raspberrypi:/usr/include ${HOME}/raspbian-sdk/sysroot
rsync -rzLR --safe-links pi@raspberrypi:/lib/arm-linux-gnueabihf ${HOME}/raspbian-sdk/sysroot

扩展编译工具

git clone https://github.com/zw3rk/toolchain-wrapper.git ${HOME}/raspbian-sdk/toolchain-wrapper
cd ${HOME}/raspbian-sdk/toolchain-wrapper
./bootstrap

修改配置文件

# ${HOME}/raspbian-sdk/toolchain-wrapper/raspberrypi-toolchain.config
# Raspberry Pi
RPI_TARGET=arm-linux-gnueabihf
RPI_NDK=$HOME/raspbian-sdk
RPI_PATH=${RPI_NDK}/prebuilt/${RPI_TARGET}/bin/
RPI_SYSROOT=${RPI_NDK}/sysroot
RPI_TOOLCHAIN_LIB=${RPI_NDK}/sysroot/usr/lib/gcc/${RPI_TARGET}/6.3.0/

配置环境变量

# .envrc
export PATH=$HOME/raspbian-sdk/prebuilt/bin:$HOME/raspbian-sdk/toolchain-wrapper:$PATH

安装交叉编译器 ghc

确保机器已经装好了 ghc 8.6.5

wget https://downloads.haskell.org/~ghc/8.6.5/ghc-8.6.5-src.tar.xz

tar cjvf ghc-8.6.5-src.tar.xz

cd ghc-8.6.5

./configure --prefix=$HOME/raspbian-sdk/prebuilt \
            --target=arm-linux-gnueabihf \
            CC=arm-linux-gnueabihf-clang \
            LD=arm-linux-gnueabihf-ld.gold \
            NM=arm-linux-gnueabihf-nm \
            RANLIB=arm-linux-gnueabihf-ranlib \
            AS=arm-linux-gnueabihf-as \
            STRIP=arm-linux-gnueabihf-strip

修改 mk/build.mk

# mk/build.mk
BuildFlavour = quick-cross
stage=1

编译和安装

make
make install

安装依赖软件包

一些软件包没办法进行交叉编译,或者交叉编译失败,我们需要修改一下 cabal 文件,然后安装。

entropy

git clone https://github.com/TomMD/entropy.git
cd entropy
git apply the-blow-patch
arm-linux-gnueabihf-cabal install --allow-newer

patch file

diff --git a/entropy.cabal b/entropy.cabal
index 6dd6af5..ff40c92 100644
--- a/entropy.cabal
+++ b/entropy.cabal
@@ -15,9 +15,9 @@ homepage:       https://github.com/TomMD/entropy
 bug-reports:    https://github.com/TomMD/entropy/issues
 stability:      stable

--- build-type:  Simple
+build-type:  Simple
 -- ^^ Used for HaLVM
-build-type:     Custom
+-- build-type:     Custom

 -- ^^ Test for RDRAND support using 'ghc'
 cabal-version:  >=1.10

cryptonite

git clone https://github.com/haskell-crypto/cryptonite.git
cd cryptonite
git apply the-blow-patch
arm-linux-gnueabihf-cabal install --allow-newer

patch file

diff --git a/cryptonite.cabal b/cryptonite.cabal
index 22f16c4..fca002a 100644
--- a/cryptonite.cabal
+++ b/cryptonite.cabal
@@ -84,7 +84,7 @@ Flag support_sse

 Flag integer-gmp
   Description:       Whether or not to use GMP for some functions
-  Default:           True
+  Default:           False
   Manual:            True

 Flag support_deepseq

wiringPi

git clone https://github.com/Lupino/hs-wiringPi.git
cd hs-wiringPi
arm-linux-gnueabihf-cabal install --allow-newer

编译目标应用程序

arm-linux-gnueabihf-cabal install --allow-newer --bindir bin/arm-linux-gnueabihf

参考

https://medium.com/@zw3rk/making-a-raspbian-cross-compilation-sdk-830fe56d75ba

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,504评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,898评论 1 300
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,218评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,322评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,693评论 3 290
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,812评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,010评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,747评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,476评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,700评论 2 251
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,190评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,541评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,206评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,129评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,903评论 0 199
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,894评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,748评论 2 274

推荐阅读更多精彩内容