mac 交叉编译windows版下载利器 aria2c (定制版)

4 篇文章 0 订阅
2 篇文章 0 订阅

mac 交叉编译Windows版下载利器 aria2c (定制版)


前言

为什么要自己编译 aria2c 定制版?自行搜索下吧,当然主要目的还是要下载一些大文件,没办法,很多开发工具来自国外,下载速度太慢,。不过要慎重使用,有不少网站有限制而且还会BAN IP地址的,使用前最好简单测试下,一般不宜超过32线程。不过,国内下载baidupan之类的到完全不受限制(起码,目前没看到限制),这个可能是aria2c的最大用途之一。交叉编译Windows版,是因为在Windows下用Docker不太熟练,直接编译又需要安装太多工具。Mac 类似Linux,原作者也是用Ubuntu docker交叉编译的Windows版本,所以还是选择在mac下用Docker编译Windows aria2c


一、准备工作

1、下载mac Docker , 官网地址:https://www.docker.com/products/docker-desktop
有点大,接近600M,可以用curl 多线程下 或在Windows下用IDM之类的下载。
2、下载 aria2c
https://github.com/aria2/aria2
最好是压缩包,便于传输。
3、编译 aria2c 需要以下几个软件包:
https://gmplib.org/download/gmp/gmp-6.1.2.tar.lz
https://github.com/libexpat/libexpat/releases/download/R_2_2_7/expat-2.2.7.tar.bz2
https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz
http://zlib.net/zlib-1.2.11.tar.gz
https://c-ares.haxx.se/download/c-ares-1.15.0.tar.gz
https://www.libssh2.org/download/libssh2-1.9.0.tar.gz
这些源码吧,最好提前下载好,复制到docker容器里去;或者有合适科学上网方法 也可以在容器里直接下载。不然,下载时间会很长。

二、编译过程

1.安装Docker

这个很简单,双击下载的 Docker.img ,然后拖动那个鲸鱼符号在这里插入图片描述

到应用程序里就行了。安装完后,运行一下docker 应用,能正常运行就行了,不用点击任何选项。

2.下载Ubuntu

打开一个终端窗口,然后发命令如下:

docker version

会显示当前安装的docker 版本等信息:

$ docker version
Client: Docker Engine - Community
 Cloud integration: 1.0.9
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:13:00 2021
 OS/Arch:           darwin/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:15:47 2021
  OS/Arch:          linux/amd64
  Experimental:     false

看到上述类似信息后,表明安装正常,开始安装编译平台ubuntu容器:

docker pull ubuntu

该命令从docker服务器下载最新版Ubuntu容器,大约70多M字节,下载完后会显示:

Using default tag: latest
latest: Pulling from library/ubuntu
5d3b2c2d21bb: Pull complete 
3fc2062ea667: Pull complete 
75adf526d75b: Pull complete 
Digest: sha256:b4f9e18267eb98998f6130342baacaeb9553f136142d40959a1b46d6401f0f2b
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

用一下命令可查看已下载容器映像:

$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
ubuntu       latest    4dd97cefde62   1 days ago   72.9MB

到此为止,Ubuntu容器映像已下载完毕。

3.编译过程

首先,启动ubuntu容器,并进入交互模式:

$ docker run -it ubuntu bash
root@b1624dc053cd:/# ls -l
total 48
lrwxrwxrwx   1 root root    7 Feb 17 01:04 bin -> usr/bin
drwxr-xr-x   2 root root 4096 Apr 15  2020 boot
drwxr-xr-x   5 root root  360 Mar  8 08:02 dev
drwxr-xr-x   1 root root 4096 Mar  8 08:02 etc
drwxr-xr-x   2 root root 4096 Apr 15  2020 home
lrwxrwxrwx   1 root root    7 Feb 17 01:04 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Feb 17 01:04 lib32 -> usr/lib32
lrwxrwxrwx   1 root root    9 Feb 17 01:04 lib64 -> usr/lib64
lrwxrwxrwx   1 root root   10 Feb 17 01:04 libx32 -> usr/libx32
drwxr-xr-x   2 root root 4096 Feb 17 01:04 media
drwxr-xr-x   2 root root 4096 Feb 17 01:04 mnt
drwxr-xr-x   2 root root 4096 Feb 17 01:04 opt
dr-xr-xr-x 113 root root    0 Mar  8 08:02 proc
drwx------   2 root root 4096 Feb 17 01:19 root
drwxr-xr-x   1 root root 4096 Mar  4 02:24 run
lrwxrwxrwx   1 root root    8 Feb 17 01:04 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Feb 17 01:04 srv
dr-xr-xr-x  13 root root    0 Mar  8 08:02 sys
drwxrwxrwt   2 root root 4096 Feb 17 01:19 tmp
drwxr-xr-x   1 root root 4096 Feb 17 01:04 usr
drwxr-xr-x   1 root root 4096 Feb 17 01:19 var
root@b1624dc053cd:/# uname -a
Linux b1624dc053cd 4.19.121-linuxkit #1 SMP Thu Jan 21 15:36:34 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
root@b1624dc053cd:/# cat /etc/issue
Ubuntu 20.04.2 LTS \n \l

嗯,确定装的是Ubuntu LTS版本,这很重要,因为国内源,基本没有镜像19.04之类的短期支持版本(aria2c 原作者使用的版本),从archive下比较慢。
其次,修改Ubuntu下载源,因为要装很多软件,docker容器自带软件很少。源可以选阿里云、中科大,这里用清华的源:

root@b1624dc053cd:/# cp -p /etc/apt/sources.list /etc/apt/sources.list.orig
root@b1624dc053cd:/# sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
root@b1624dc053cd:/# export HOST=i686-w64-mingw32

这些命令除第一条备份外,来源于 aria2c源码中的Dockerfile.mingw 文件。本来可以直接用这个文件创建容器,并实现自动编译 (可参考:https://blog.csdn.net/rankun1/article/details/90580593,这篇文章在Ubuntu虚拟机中,使用Docker容器自动编译)。因为需要自行修改aria2c源码,需要找个网站放修改源码;还有从github直接下载的源码,在Ubuntu下编译会报错,所以没有采用自动创建Docker容器并编译的做法。
第三,安装必须的ubuntu系统软件包:

root@b1624dc053cd:/# apt update        #更新源
root@b1624dc053cd:/# apt install -y make binutils autoconf automake autotools-dev libtool \
> pkg-config git curl dpkg-dev gcc-mingw-w64 g++-mingw-w64 \
> autopoint libcppunit-dev libxml2-dev libgcrypt11-dev lzip gettext

注意在mac docker 容器中 libgcrypt11-dev ,测试了不能安装,原因未知,需要删除,当然如果你能安装就不用删除。如下所示:

root@b1624dc053cd:/# apt-get install libgcrypt11-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package libgcrypt11-dev

估计可能与缓冲有关,暂时不管。
下面单独安装:

root@b1624dc053cd:/# curl -R -O https://mirrors.tuna.tsinghua.edu.cn/ubuntu/pool/main/libg/libgcrypt20/libgcrypt11-dev_1.5.4-3+really1.8.1-4ubuntu1.2_amd64.deb
root@b1624dc053cd:/# apt install ./libgcrypt11-dev_1.5.4-3+really1.8.1-4ubuntu1.2_amd64.deb 

一切正常。

下面开始编译前面下载的gmp等几个必须软件,如果没有下载,在容器里面用下列命令下载:

    cd / && \
    curl -x http://proxyip:port -L -O https://github.com/libexpat/libexpat/releases/download/R_2_2_7/expat-2.2.7.tar.bz2 && \
    curl -x http://proxyip:port -L -O https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz && \
    curl -x http://proxyip:port -L -O http://zlib.net/zlib-1.2.11.tar.gz && \
    curl -x http://proxyip:port -L -O https://c-ares.haxx.se/download/c-ares-1.15.0.tar.gz && \
    curl -x http://proxyip:port -L -O https://www.libssh2.org/download/libssh2-1.9.0.tar.gz

如果想单个逐行执行,请删掉行尾的 && \ 三个字符。
proxyip:port 代理服务器地址和端口。
确定所有下载完成后,在根目录下,使用下列命令自动编译:

cd / && tar xf gmp-6.1.2.tar.lz && \
    cd gmp-6.1.2 && \
    ./configure \
        --disable-shared \
        --enable-static \
        --prefix=/usr/local/$HOST \
        --host=$HOST \
        --disable-cxx \
        --enable-fat \
        CFLAGS="-mtune=generic -O2 -g0" && \
    make install

cd / && tar xf expat-2.2.7.tar.bz2 && \
    cd expat-2.2.7 && \
    ./configure \
        --disable-shared \
        --enable-static \
        --prefix=/usr/local/$HOST \
        --host=$HOST \
        --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` && \
    make install

cd / && tar xf sqlite-autoconf-3290000.tar.gz && \
    cd sqlite-autoconf-3290000 && \
    ./configure \
        --disable-shared \
        --enable-static \
        --prefix=/usr/local/$HOST \
        --host=$HOST \
        --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` && \
    make install

cd / && tar xf zlib-1.2.11.tar.gz && \
    cd zlib-1.2.11 && \
    CC=$HOST-gcc \
    AR=$HOST-ar \
    LD=$HOST-ld \
    RANLIB=$HOST-ranlib \
    STRIP=$HOST-strip \
    ./configure \
        --prefix=/usr/local/$HOST \
        --libdir=/usr/local/$HOST/lib \
        --includedir=/usr/local/$HOST/include \
        --static && \
    make install

cd / && tar xf c-ares-1.15.0.tar.gz && \
    cd c-ares-1.15.0 && \
    ./configure \
        --disable-shared \
        --enable-static \
        --without-random \
        --prefix=/usr/local/$HOST \
        --host=$HOST \
        --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` \
        LIBS="-lws2_32" && \
    make install

cd / && tar xf libssh2-1.9.0.tar.gz && \
    cd libssh2-1.9.0 && \
    ./configure \
        --disable-shared \
        --enable-static \
        --prefix=/usr/local/$HOST \
        --host=$HOST \
        --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` \
        --without-openssl \
        --with-wincng \
        LIBS="-lws2_32" && \
    make install

正常情况下,应该不会报任何错误,有问题请自行搜索,多半是软件包安装不全或下载软件有问题。
最后一步,编译aria2c 请使用附件的aria2c.tar.gz文件解压到根目录下/aria2c-master 目录。
核心改变有两个文件,第一个文件是
/aria2c-master/configure.ac

732 AM_GNU_GETTEXT_VERSION([0.18])

这是源码,需要改变一个字符变成:

732 AM_GNU_GETTEXT_VERSION([0.19])

也是当前Ubuntu安装的gettext版本。虽然很多人建议(包括GNU官方)都建议改成:

732 AM_GNU_GETTEXT_REQUIRE_VERSION([0.18])

但这是行不通的,会漏拷文件,除非你自己拷贝相关文件(config.rpath,ABOUT_NLS,po/Makfile.in.in),估计gettext 0.21版本应该没这问题了。
还有个文件是aria2c-master/src/OptionHandlerFactory.cc 改动两个字符,具体请自行搜索。
一切正常后,最后一个命令:

cd /aria2-master && autoreconf -i && ./mingw-config && make && \
    $HOST-strip src/aria2c.exe

机器比较老,时间大约15分钟,可以休息下。

4.成果

编译后的文件如下所示:

root@b1624dc053cd:/aria2-master# ls -l src/aria2c.exe 
-rwxr-xr-x 1 root root 5256192 Mar  8 10:32 src/aria2c.exe

PS:如何把下载的aria2-master.tar.gz 从主机复制到容器?很简单:

$ docker cp aria2-master.tar.gz b1624dc053cd:/

注意b1624dc053cd是容器id,跟前面的登录后显示的是一致的。如何查看容器ID?

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED      STATUS      PORTS     NAMES
b1624dc053cd   ubuntu    "bash"

这是查看正在运行中的容器ID

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值