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

377 阅读1分钟

使用 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

参考

medium.com/@zw3rk/maki…