工作中需要在windows环境下编译OpenWRT平台运行的的App,所以需要windows平台的交叉编译工具链。在网上找了一圈没有找到现成的,只能自己照着OpenWRT的makefile编译了一份交叉编译toolchain。
编译出来还未验证是否可用,待春节复工之后验证。
【Updata】可以在WSL里运行起来,无法在cygwin里使用。Orz~
此交叉编译toolchain信息为:
host: windows 10 64bit
target: OpenWRT lede-17.01 branch.(linux-4.4.184 / gcc-5.4.0 / musl-1.1.16) 注意C库使用的是musl而非glibc
链接: https://pan.baidu.com/s/1w5yf1CJ3rYZQuSenrJVvqA 提取码: qt8n
可以在windows10的WSL中使用,用cygwin应该也可以吧。
使用windows的 linux子系统 ubuntu 18.04LTS。
代码版本 | 下载链接 |
---|---|
binutils-2.25.1.tar.bz2 | http://ftp.gnu.org/gnu/binutils/ |
gcc-5.4.0.tar.bz2 | http://ftp.gnu.org/gnu/gcc/ |
gmp-6.1.2.tar.xz | http://ftp.gnu.org/gnu/gmp/ |
linux-4.4.184.tar.xz | https://cdn.kernel.org/pub/linux/kernel/v4.x/ |
musl-1.1.16.tar.xz | https://musl.libc.org/releases/ |
WIN+R键输入regedit,修改注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\obcaseinsensitive为0表示打开大小写敏感,修改之后重启。
编译过程发现非常慢,任务管理器看"Antimalware Service Executable"进程占CPU非常高。
关闭“实时保护”可以提高编译速度,方法:
设置–>更新和安全–>Windows安全中心–>病毒和威胁防护–>"病毒和威胁防护"设置–>关闭实时保护
在ubuntu里安装依赖软件,使用 阿里源 可以加快下载速度
sudo apt-get install binutils gcc make patch bison flex autoconf libmpc-dev g++
我的toolchain在d:/toolchain目录下编译,在ubuntu里映射为/mnt/d/toolchain, toolchain目录下再建三个子目录
build_dir:在此目录下编译
staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi:安装到此目录
patches:patch存放目录,patch从openwrt源码里copy过来(openwrt/toolchain/xxx/patches,linux的patch在target/linux/generic/pathces-4.4)
编译GCC依赖gmp库,gmp没有找到使用apt-get安装的方法,需要自己编译安装:
cd /mnt/d/toolchain/build_dir
tar -xJf gmp-6.1.2.tar.xz
cd gmp-6.1.2/
./configure --prefix=/usr
make
sudo make install
安装成功会在/usr/include下有gmp.h
编译toolchain主要分5个步骤:
cd /mnt/d/toolchain/build_dir
tar -jxf binutils-2.25.1.tar.bz2
./patch-kernel.sh ./binutils-2.25.1 ../patches/binutils/patches/2.25.1/
ln -snf binutils-2.25.1 ./binutils
cp config.{sub,guess} binutils-2.25.1
sed -i -e 's, " Linaro.*,,' ./binutils-2.25.1/bfd/version.h
其中config.sub, config.guess, patch-kernel.sh, patch-specs.sh是从openwrt源码scripts目录下copy到build_dir下。
cd /mnt/d/toolchain/build_dir/binutils-2.25.1
./configure --prefix=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi --target=arm-openwrt-linux-muslgnueabi --with-sysroot=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi --enable-deterministic-archives --enable-plugins --disable-multilib --disable-werror --disable-nls --disable-sim --disable-gdb --with-float=soft --disable-libssp
make all
mkdir -p /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/initial
make prefix=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/initial install
make prefix=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi install
ln -sf lib /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/initial/lib64
cd /mnt/d/toolchain/build_dir/
mkdir gcc-5.4.0-initial
sed -i -e 's,TARGET_CROSS=.*,TARGET_CROSS=arm-openwrt-linux-muslgnueabi-,' /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/info.mk
sed -i -e 's,GCC_VERSION=.*,GCC_VERSION=5.4.0,' /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/info.mk
tar -jxf gcc-5.4.0.tar.bz2 --exclude='gcc/testsuite/*' --exclude=gcc/ada/*.ad* --exclude=libjava
./patch-kernel.sh ./gcc-5.4.0 ../patches/gcc/patches/5.4.0/
ln -snf gcc-5.4.0 ./gcc
cp config.{sub,guess} gcc-5.4.0
sed -i -e 's,^MULTILIB_OSDIRNAMES,# MULTILIB_OSDIRNAMES,' ./gcc-5.4.0/gcc/config/*/t-*
sed -i -e 'd' ./gcc-5.4.0/gcc/DEV-PHASE
sed -i -e 's, DATESTAMP,,' ./gcc-5.4.0/gcc/version.c
sed -i -e 's,gcc_no_link=yes,gcc_no_link=no,' ./gcc-5.4.0/libstdc++-v3/configure
其中info.mk是从openwrt源码toolchian/info.mk copy到staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi目录下。
cd ./gcc-5.4.0-initial && rm -f config.cache
../gcc-5.4.0/configure --with-bugurl=http://www.lede-project.org/bugs/ --with-pkgversion="LEDE GCC 5.4.0 r0+4040-0b8ca9c275" --prefix=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi --target=arm-openwrt-linux-muslgnueabi --with-gnu-ld --enable-target-optspace --disable-libgomp --disable-libmudflap --disable-multilib --disable-libmpx --disable-nls --without-isl --without-cloog --with-host-libstdcxx=-lstdc++ --with-float=soft --with-gmp=/usr --with-mpfr=/usr --with-mpc=/usr --disable-decimal-float --with-diagnostics-color=auto-if-env --disable-libssp --enable-__cxa_atexit --with-newlib --with-sysroot=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi --enable-languages=c --disable-shared --disable-threads
make CFLAGS="-O2 -I/usr/include " CFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" CXXFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" GOCFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" all-build-libiberty all-gcc all-target-libgcc
make CFLAGS="-O2 -I/usr/include " CFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" CXXFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" GOCFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" prefix="/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/initial" install-gcc install-target-libgcc
cd /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/initial/lib/gcc/arm-openwrt-linux-muslgnueabi/5.4.0
ln -s libgcc.a libgcc_eh.a
cp libgcc.a libgcc_initial.a
ln -sf lib /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/initial/lib64
cp -rf /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/initial/* /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/
cd /mnt/d/toolchain/build_dir/
tar -xJf linux-4.4.184.tar.xz
cp -fpR ../patches/linux/generic/files/* linux-4.4.184/
find ./linux-4.4.184/ -name \*.rej -or -name \*.orig | xargs -r rm -f
./patch-kernel.sh ./linux-4.4.184 ../patches/linux/generic/patches-4.4
rm -f ./linux
ln -s linux-4.4.184 ./linux
sed -i -e 's/@expr length/@-expr length/' ./linux-4.4.184/Makefile
cd linux-4.4.184
yes '' | make HOSTCFLAGS="-O2 -I/usr/include -Wall -Wmissing-prototypes -Wstrict-prototypes" ARCH=arm CC="arm-openwrt-linux-muslgnueabi-gcc" CFLAGS="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" CROSS_COMPILE=arm-openwrt-linux-muslgnueabi- KBUILD_HAVE_NLS=no CONFIG_SHELL=bash oldconfig
mkdir ../linux-dev
make HOSTCFLAGS="-O2 -I/usr/include -Wall -Wmissing-prototypes -Wstrict-prototypes" ARCH=arm CC="arm-openwrt-linux-muslgnueabi-gcc" CFLAGS="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" CROSS_COMPILE=arm-openwrt-linux-muslgnueabi- KBUILD_HAVE_NLS=no CONFIG_SHELL=bash INSTALL_HDR_PATH="/mnt/d/toolchain/build_dir/linux-dev/" headers_install
do nothing
cp -fpR /mnt/d/toolchain/build_dir/linux-dev/* /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/
cd /mnt/d/toolchain/build_dir/
tar -xJf musl-1.1.16.tar.xz
./patch-kernel.sh ./musl-1.1.16 ../patches/musl/patches/
ln -snf musl-1.1.16 ./musl
cd ./musl-1.1.16
PATH=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/bin:$PATH
rm -f config.cache
AR="arm-openwrt-linux-muslgnueabi-gcc-ar" AS="arm-openwrt-linux-muslgnueabi-gcc -c -Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft -Wformat -Werror=format-security -fstack-protector -D_FORTIFY_SOURCE=1 -Wl,-z,now -Wl,-z,relro" LD=arm-openwrt-linux-muslgnueabi-ld NM="arm-openwrt-linux-muslgnueabi-gcc-nm" CC="arm-openwrt-linux-muslgnueabi-gcc" GCC="arm-openwrt-linux-muslgnueabi-gcc" CXX="arm-openwrt-linux-muslgnueabi-g++" RANLIB="arm-openwrt-linux-muslgnueabi-gcc-ranlib" STRIP=arm-openwrt-linux-muslgnueabi-strip OBJCOPY=arm-openwrt-linux-muslgnueabi-objcopy OBJDUMP=arm-openwrt-linux-muslgnueabi-objdump SIZE=arm-openwrt-linux-muslgnueabi-size CFLAGS="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft -Wformat -Werror=format-security -fstack-protector -D_FORTIFY_SOURCE=1 -Wl,-z,now -Wl,-z,relro" CROSS_COMPILE="arm-openwrt-linux-muslgnueabi-" ./configure --prefix=/ --target=arm-openwrt-linux-muslgnueabi --disable-gcc-wrapper --enable-debug
make DESTDIR="/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/" LIBCC="/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/lib/gcc/arm-openwrt-linux-muslgnueabi/5.4.0/libgcc_initial.a" all
sed -i -e 's,^\(LIBC_TYPE\)=.*,\1=musl,' /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/info.mk
sed -i -e 's,^\(LIBC_URL\)=.*,\1=http://www.musl-libc.org/,' /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/info.mk
sed -i -e 's,^\(LIBC_VERSION\)=.*,\1=1.1.16,' /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/info.mk
sed -i -e 's,^\(LIBC_SO_VERSION\)=.*,\1=1.1.16,' /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/info.mk
make DESTDIR="/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/" LIBCC="/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/lib/gcc/arm-openwrt-linux-muslgnueabi/5.4.0/libgcc_initial.a" install
cp -fpR ./include /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/
cd /mnt/d/toolchain/build_dir/
mkdir gcc-5.4.0-final
install -d -m0755 /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi
rm -rf /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi/sys-include
ln -sf ../include /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi/sys-include
rm -rf /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi/lib
ln -sf ../lib /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi/lib
cd gcc-5.4.0-final
../gcc-5.4.0/configure --with-bugurl=http://www.lede-project.org/bugs/ --with-pkgversion="LEDE GCC 5.4.0 r0+4040-0b8ca9c275" --prefix=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi --target=arm-openwrt-linux-muslgnueabi --with-gnu-ld --enable-target-optspace --disable-libgomp --disable-libmudflap --disable-multilib --disable-libmpx --disable-nls --without-isl --without-cloog --with-host-libstdcxx=-lstdc++ --with-float=soft --with-gmp=/usr --with-mpfr=/usr --with-mpc=/usr --disable-decimal-float --with-diagnostics-color=auto-if-env --disable-libssp --enable-__cxa_atexit --with-headers=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/include --disable-libsanitizer --enable-languages="c,c++" --enable-shared --enable-threads --with-slibdir=/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/lib --enable-lto --with-libelf=/usr
make CFLAGS="-O2 -I/usr/include " CFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" CXXFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" GOCFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" gcc_cv_libc_provides_ssp=yes all
install -d -m0755 /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi
rm -rf /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi/sys-include
ln -sf ../include /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi/sys-include
rm -rf /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi/lib
ln -sf ../lib /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/arm-openwrt-linux-muslgnueabi/lib
make CFLAGS="-O2 -I/usr/include " CFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" CXXFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" GOCFLAGS_FOR_TARGET="-Os -pipe -mcpu=cortex-a7 -mfpu=neon-vfpv4 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -mfloat-abi=soft" gcc_cv_libc_provides_ssp=yes install
cd /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi
ln -sf arm-openwrt-linux-muslgnueabi arm-openwrt-linux
cd bin
for app in arm-openwrt-linux-muslgnueabi-* ; do ln -sf ${app} arm-openwrt-linux${app##arm-openwrt-linux-muslgnueabi}; done;
rm -f /mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi/lib/libiberty.a
./patch-specs.sh "/mnt/d/toolchain/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.4.0_musl-1.1.16_eabi"