交叉編譯Windows Aria2

編譯環境 Ubuntu 16.0.4
首先安裝相關依賴

apt-get install -y gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64 libgnutls28-dev nettle-dev libgmp-dev libssh2-1-dev libc-ares-dev libxml2-dev zlib1g-dev libsqlite3-dev pkg-config libcppunit-dev autoconf automake autotools-dev autopoint libtool git gcc g++ libxml2-dev make quilt libcurl4-openssl-dev libevent-dev ca-certificates libssl-dev  build-essential intltool libgcrypt-dev libssl-dev python-pip
pip install --upgrade pip
pip install sphinx

配置函式庫

安裝 zlib c-ares expat sqlite3 openSSL libssh2 並進行相關配置
這裡使用腳本進行安裝

vim aria2-x86_64-w64-mingw-build-libs

貼上以下內容

#!/bin/bash

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

#IMPORTANT: Require install gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64

#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.2o.tar.gz
EXPAT=https://sourceforge.net/projects/expat/files/expat/2.2.0/expat-2.2.0.tar.bz2
SQLITE3=http://www.sqlite.org/2018/sqlite-autoconf-3230100.tar.gz
C_ARES=http://c-ares.haxx.se/download/c-ares-1.14.0.tar.gz
SSH2=https://www.libssh2.org/download/libssh2-1.7.0.tar.gz

## CONFIG ##
BUILD_DIRECTORY=/tmp/
HOST=x86_64-w64-mingw32
PREFIX=/usr/x86_64-w64-mingw32

## BUILD ##
cd $BUILD_DIRECTORY
#
 # zlib build
  $DOWNLOADER $ZLIB
  tar zxvf zlib-1.2.11.tar.gz
  cd zlib-1.2.11/
  CC=$HOST-gcc CXX=$HOST-g++ AR=$HOST-ar RANLIB=$HOST-ranlib ./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/
  CC=$HOST-gcc CXX=$HOST-g++ AR=$HOST-ar RANLIB=$HOST-ranlib ./configure --prefix=$PREFIX --host=$HOST --enable-static --enable-shared
  make
  make install
#
 # c-ares build
  cd ..
  $DOWNLOADER $C_ARES
  tar zxvf c-ares-1.14.0.tar.gz
  cd c-ares-1.14.0/
  CC=$HOST-gcc CXX=$HOST-g++ AR=$HOST-ar RANLIB=$HOST-ranlib ./configure --prefix=$PREFIX --host=$HOST --enable-static --disable-shared
  make
  make install
#
 # Openssl build
  cd ..
  $DOWNLOADER $OPENSSL
  tar zxvf openssl-1.0.2o.tar.gz
  cd openssl-1.0.2o/
  ./Configure mingw64 --cross-compile-prefix=$HOST- --prefix=$PREFIX shared
  make
  make install
#
 # sqlite3
  cd ..
  $DOWNLOADER $SQLITE3
  tar zxvf sqlite-autoconf-3230100.tar.gz
  cd sqlite-autoconf-3230100/
  CC=$HOST-gcc CXX=$HOST-g++ AR=$HOST-ar RANLIB=$HOST-ranlib ./configure --prefix=$PREFIX --host=$HOST --enable-static --enable-shared
  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
  CC=$HOST-gcc CXX=$HOST-g++ AR=$HOST-ar RANLIB=$HOST-ranlib ./configure --prefix=$PREFIX --host=$HOST --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!"
 

貼上後儲存並離開
賦予執行權限

chmod +x aria2-x86_64-w64-mingw-build-libs

執行腳本

./aria2-x86_64-w64-mingw-build-libs

這裡要等帶一段時間

完成後,下載aria2原碼

wget https://github.com/aria2/aria2/releases/download/release-1.34.0/aria2-1.34.0.tar.gz

解壓縮

tar -zxvf aria2-1.34.0.tar.gz

切換至該目錄

cd aria2-1.34.0

修改Aria2為128線程

這裡使用腳本來進行修改

vim replace.sh

貼上以下內容

#!/bin/bash
sed -i 's/"1", 1, 16/"128", 1, -1/g' ./src/OptionHandlerFactory.cc

sed -i 's/"20M", 1_m, 1_g/"4K", 1_k, 1_g/g' ./src/OptionHandlerFactory.cc

sed -i 's/PREF_CONNECT_TIMEOUT, TEXT_CONNECT_TIMEOUT, "60", 1, 600/PREF_CONNECT_TIMEOUT, TEXT_CONNECT_TIMEOUT, "30", 1, 600/g' ./src/OptionHandlerFactory.cc

sed -i 's/PREF_PIECE_LENGTH, TEXT_PIECE_LENGTH, "1M", 1_m, 1_g/PREF_PIECE_LENGTH, TEXT_PIECE_LENGTH, "4k", 1_k, 1_g/g' ./src/OptionHandlerFactory.cc

sed -i 's/new NumberOptionHandler(PREF_RETRY_WAIT, TEXT_RETRY_WAIT, "0", 0, 600/new NumberOptionHandler(PREF_RETRY_WAIT, TEXT_RETRY_WAIT, "2", 0, 600/g' ./src/OptionHandlerFactory.cc

sed -i 's/new NumberOptionHandler(PREF_SPLIT, TEXT_SPLIT, "5", 1, -1,/new NumberOptionHandler(PREF_SPLIT, TEXT_SPLIT, "8", 1, -1,/g' ./src/OptionHandlerFactory.cc

賦予執行權限

chmod +x replace.sh

執行腳本

sh replace.sh

配置Aria2的編譯環境

完成後,配置Aria2的編譯環境
這裡使用腳本來進行配置

vim aria2-x86_64-w64-mingw-config

貼上以下內容

#!/bin/bash

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

HOST=x86_64-w64-mingw32
PREFIX=/usr/x86_64-w64-mingw32

./configure \
    --host=$HOST \
    --prefix=$PREFIX \
    --without-included-gettext \
    --disable-nls \
    --with-libcares \
    --without-gnutls \
    --without-wintls \
    --with-openssl \
    --with-sqlite3 \
    --without-libxml2 \
    --with-libexpat \
    --with-libz \
    --without-libgmp \
    --with-libssh2 \
    --without-libgcrypt \
    --without-libnettle \
    --with-cppunit-prefix=$PREFIX \
    ARIA2_STATIC=yes \
    CPPFLAGS="-I$PREFIX/include" \
    LDFLAGS="-L$PREFIX/lib" \
    PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"

儲存並離開
賦予執行權限

chmod +x aria2-x86_64-w64-mingw-config

執行配置編譯環境腳本 [ 注意!!!這裡是在aria2-1.34.0該目錄底下進行動作的 ]

./aria2-x86_64-w64-mingw-config

編譯

過程沒出錯的話就可以開始編譯了

make
如果想要加快編譯速度的話
可以使用
make -j 4 #使用4線程進行編譯的意思,如果不帶數字的話,代表不限制線程數,ps:小心翻車

編譯完成後產生的aria2c.exe會在aria2-1.34.0/src目錄底下
複製出來

cp aria2-1.34.0/src/aria2c.exe /root

去除aria2c.exe的連接符

切換至/root
去除aria2c.exe的連接符,能幫它減肥

strip -s aria2c.exe

到這裡就完成啦

Last modification:June 1, 2018
If you think my article is useful to you, please feel free to appreciate