天涼好個秋.

搭建群晖交叉编译环境并编译Aria2

4722 阅 0 评 IT手札

一直想自己编译一个群晖上的 aria2 ,正好国庆假期,自己折腾下,这里是折腾过程的流水账,仅作记录之用。

搭建官方编译环境

找到了官方文档,搭建似乎很容易:
官方的文档 DSM_Developer_Guide

  • 编译环境的需求如下:
    • 64bit linux environment.
    • Bash (>= 4.1.5)
    • Python (>= 2.7.3)
  • 建立工作目录
mkdir -p /toolkit/
  • 下载toolkit
cd /toolkit/
git clone https://github.com/SynologyOpenSource/pkgscripts-ng.git
  • 更新环境
version="6.1"
platform="armada375"
cd /toolkit/pkgscripts-ng
./EnvDeploy -v ${version} -p ${platform}
  • 更新好之后的目录结构
/toolkit
├── build_env
│   └── ds.armada375-6.1
├── pkgscripts-ng
│   ├── CodeSign.php
│   ├── EnvDeploy
│   ├── include
│   ├── PkgCreate.py
│   ├── ProjectDepends.py
│   ├── README.md
│   ├── SynoBuild
│   └── SynoInstall
└── toolkit_tarballs
    ├── base_env-6.1.txz
    ├── ds.armada375-6.1.dev.txz
    └── ds.armada375-6.1.env.txz
  • 进入 chroot 环境
sudo chroot /toolkit/build_env/ds.armada375-6.1
  • 继续看文档发现,这是编译官方安装的环境,我似乎不需要弄这么复杂,弃坑。

搭建交叉编译环境

tar -zJf armada375-gcc493_glibc220_hard-GPL.txz /usr/local
cp -rf /usr/local/arm-unknown-linux-gnueabi/* /usr/local/

编译依赖库

在 github 上搜索了下有人写好了编译的脚本,aria2-static-builds

嗯,自己修改了下

#!/bin/bash

# In this configuration, the following dependent libraries are compiled:
#
# * zlib
# * c-ares
# * expat
# * sqlite3
# * openSSL
# * libssh2

#CHECK TOOL FOR DOWNLOAD
 aria2c --help > /dev/null
 if [ "$?" -eq 0 ] ; then
   DOWNLOADER="aria2c --check-certificate=false"
 else
   DOWNLOADER="wget -c"
 fi

## DEPENDENCES ##
ZLIB=http://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz
OPENSSL=http://www.openssl.org/source/openssl-1.0.2k.tar.gz
EXPAT=https://sourceforge.net/projects/expat/files/expat/2.2.0/expat-2.2.0.tar.bz2
SQLITE3=http://www.sqlite.org/2017/sqlite-autoconf-3180000.tar.gz
C_ARES=http://c-ares.haxx.se/download/c-ares-1.12.0.tar.gz
SSH2=https://www.libssh2.org/download/libssh2-1.7.0.tar.gz

## CONFIG ##
ARCH="unknown"
HOST="arm-unknown-linux-gnueabi"
PREFIX="/opt/aria2-arm-unknown/build_libs"
LOCAL_DIR="/opt/aria2-arm-unknown/build_libs"

TOOL_BIN_DIR="/usr/local/$HOST/bin"
PATH=${TOOL_BIN_DIR}:$PATH

CFLAGS="-mhard-float -mfpu=vfpv3"
DEST="/opt/aria2-arm-unknown/build_libs"
CC=$HOST-gcc
CXX=$HOST-g++
LDFLAGS="-L$DEST/lib"
CPPFLAGS="-I$DEST/include"
CXXFLAGS=$CFLAGS
MAKE="make -j`nproc`"
CONFIGURE="./configure --prefix=${LOCAL_DIR} --host=$HOST"
BUILD_DIRECTORY=/tmp/

## BUILD ##
cd $BUILD_DIRECTORY
#
 # zlib build
  $DOWNLOADER $ZLIB
  tar zxvf zlib-1.2.11.tar.gz
  cd zlib-1.2.11/
  PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig/ LD_LIBRARY_PATH=$PREFIX/lib/ CC=$HOST-gcc STRIP=$HOST-strip RANLIB=$HOST-ranlib CXX=$HOST-g++ AR=$HOST-ar LD=$HOST-ld ./configure --prefix=$PREFIX --static
  make
  make install
#
 # expat build
  cd ..
  $DOWNLOADER $EXPAT
  tar jxvf expat-2.2.0.tar.bz2
  cd expat-2.2.0/
  PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig/ LD_LIBRARY_PATH=$PREFIX/lib/ CC=$HOST-gcc CXX=$HOST-g++ ./configure --host=$HOST --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` --prefix=$PREFIX --enable-static=yes --enable-shared=no
  make
  make install
#
 # c-ares build
  cd ..
  $DOWNLOADER $C_ARES
  tar zxvf c-ares-1.12.0.tar.gz
  cd c-ares-1.12.0/
  PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig/ LD_LIBRARY_PATH=$PREFIX/lib/ CC=$HOST-gcc CXX=$HOST-g++ ./configure --host=$HOST --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` --prefix=$PREFIX --enable-static --disable-shared
  make
  make install
#
 # Openssl build
  cd ..
  $DOWNLOADER $OPENSSL
  tar zxvf openssl-1.0.2k.tar.gz
  cd openssl-1.0.2k/
  PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig/ LD_LIBRARY_PATH=$PREFIX/lib/ CC=$HOST-gcc CXX=$HOST-g++ ./Configure linux-armv4 $CFLAGS --prefix=$PREFIX shared zlib zlib-dynamic -D_GNU_SOURCE -D_BSD_SOURCE --with-zlib-lib=$LOCAL_DIR/lib --with-zlib-include=$LOCAL_DIR/include
  make CC=$CC
  make CC=$CC install INSTALLTOP=$DEST OPENSSLDIR=$DEST/ssl
#
 # sqlite3
  cd ..
  $DOWNLOADER $SQLITE3
  tar zxvf sqlite-autoconf-3180000.tar.gz
  cd sqlite-autoconf-3180000/
  PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig/ LD_LIBRARY_PATH=$PREFIX/lib/ CC=$HOST-gcc CXX=$HOST-g++ ./configure --host=$HOST --prefix=$PREFIX --enable-static --enable-shared --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE`
  make
  make install
#
 # libssh2
  cd ..
  $DOWNLOADER $SSH2
  tar zxvf libssh2-1.7.0.tar.gz
  cd libssh2-1.7.0/
  rm -rf $PREFIX/lib/pkgconfig/libssh2.pc
  PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig/ LD_LIBRARY_PATH=$PREFIX/lib/ CC=$HOST-gcc CXX=$HOST-g++ AR=$HOST-ar RANLIB=$HOST-ranlib ./configure --host=$HOST --without-libgcrypt --with-openssl --without-wincng --prefix=$PREFIX --enable-static --disable-shared
  make
  make install
#
 #cleaning
  cd ..
  rm -rf c-ares*
  rm -rf sqlite-autoconf*
  rm -rf zlib-*
  rm -rf expat-*
  rm -rf openssl-*
  rm -rf libssh2-*
#
 echo "finished!"

编译 Aria2

  • 下载源代码
cd /root/
wget https://github.com/aria2/aria2/releases/download/release-1.32.0/aria2-1.32.0.tar.gz
tar -zxf aria2-1.32.0.tar.gz -C ./
mv aria2-1.32.0/ aria2/
  • configure
#!/bin/bash

# In this configuration, the following dependent libraries are used:
#
# * zlib
# * c-ares
# * expat
# * sqlite3
# * openSSL
# * libssh2

## CONFIG ##
ARCH="unknown"
HOST="arm-unknown-linux-gnueabi"
PREFIX="/opt/aria2-arm-unknown"
LOCAL_DIR="/opt/aria2-arm-unknown/build_libs"

TOOL_BIN_DIR="/usr/local/$HOST/bin"
PATH=${TOOL_BIN_DIR}:$PATH

CFLAGS="-mhard-float -mfpu=vfpv3"
DEST="/opt/aria2-arm-unknown/build_libs"
CC=$HOST-gcc
CXX=$HOST-g++
LDFLAGS="-L$DEST/lib"
CPPFLAGS="-I$DEST/include"
CXXFLAGS=$CFLAGS
MAKE="make -j`nproc`"
CONFIGURE="./configure --prefix=${LOCAL_DIR} --host=$HOST"

./configure \
    --host=$HOST \
    --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` \
    --prefix=$PREFIX \
    --disable-nls \
    --without-gnutls \
    --with-openssl \
    --without-libxml2 \
    --with-libz --with-libz-prefix=${LOCAL_DIR} \
    --with-libexpat --with-libexpat-prefix=${LOCAL_DIR} \
    --with-slite3 --with-sqlite3-prefix=${LOCAL_DIR} \
    --with-libcares --with-libcares-prefix=${LOCAL_DIR} \
    --with-ca-bundle='/etc/ssl/certs/ca-certificates.crt' \
    LDFLAGS="-L$LOCAL_DIR/lib" \
    PKG_CONFIG_PATH="$LOCAL_DIR/lib/pkgconfig" \
    ARIA2_STATIC=yes
  • make
make -j4 V=s
  • 链接时遇到一些小问题,库的位置不对,复制下。
cp /usr/local/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/sysroot/lib/* /usr/local/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/lib/
  • install
make install
  • 优化编译出的二进制文件
arm-unknown-linux-gnueabi-strip -s /opt/aria2-arm-unknown/bin/armle-unknown-linux-aria2c

其他

EOF