赞
踩
鉴于从零制作交叉编译链的命令异常繁杂,很容易因为某个细节出现错误。因此,我将自己创建交叉编译链的步骤写成了脚本,如果大家自己搭建时出现错误,可以直接使用脚本进行创建,以此来确定是自己的步骤有问题还是环境差异造成的。
总共有三个脚本,使用说明和脚本源码见后文:
sudo apt-get install libncurses5-dev
sudo apt-get install gperf
sudo apt-get install bison
sudo apt-get install flex
sudo apt-get install texinfo
sudo apt-get install help2man
sudo apt-get install gawk楚这三个参数的配置意义
注意:这只是我遇到的需要安装的工具,在编译过程中如果缺了什么工具,请自行安装。
按照下列目录结构建立 Develop 文件夹和子目录,并上传脚本文件,顶层目录名字 Develop 可以自由修改。
注意:如果是通过windows上传脚本,记得使用dos2unix命令进行格式转换。
Develop/
├── script
│ ├── toolchain_build.sh
│ ├── toolchain_config.lst
│ └── toolchain_mkpj.sh
└── source
$ ./toolchain_mkpj.sh ../../toolchain
[INFO] ../../toolchain is not exist, create it
[INFO] pkg: binutils-2.27.tar.bz2 is exist, Copy it
[INFO] pkg: gcc-5.4.0.tar.bz2 is exist, Copy it
[INFO] pkg: glibc-2.23.tar.bz2 is exist, Copy it
[INFO] pkg: linux-4.4.25.tar.xz is exist, Copy it
../../toolchain
├── build
├── script
│ ├── toolchain_build.sh
│ └── toolchain_config.lst
├── setup
│ ├── binutils-2.27.tar.bz2
│ ├── gcc-5.4.0.tar.bz2
│ ├── glibc-2.23.tar.bz2
│ └── linux-4.4.25.tar.xz
├── src
└── tool-chain
5 directories, 6 files
进入编译目录并修改配置文件(后续的命令都在该目录下操作)
$ cd ../../toolchain/script/
$ vi toolchain_config.lst
export PRJROOT=$1 # [不修改] 工程目录名,自动适配
export TARGET=arm-none-linux-gnueabi # [可修改] 目标编译器名称
export PREFIX=${PRJROOT}/tool-chain # [不修改] 当运行 make install 时的安装目录
export TARGET_PREFIX=${PREFIX}/${TARGET} # [不修改]
export PATH=${PREFIX}/bin:${PATH} # [不修改] 将新生成的GCC所在目录加入环境变量。脚本执行完毕后恢复
export ARCH=arm # [可修改] 需要编译的架构
export BINUTILS_VERSION=binutils-2.27 # [可修改] binutils版本
export GCC_VERSION=gcc-5.4.0 # [可修改] gcc版本
export GLIBC_VERSION=glibc-2.23 # [可修改] glibc版本
export KERNEL_VERSION=linux-4.4.25 # [可修改] kernel版本
$ ./toolchain_build.sh
等着吧,如果没问题1个小时左右就编译好了(不同的设备可能编译时间有很大差别),如果有问题,可以给我留言。
Good Luck :)
最终的编译链放在 toolchain/tool-chain 中。
#!/bin/bash
# Name: toolchain_mkpj.sh
# Author: Peter Peng
# Ver: 0.1
set -e
ProjectDir=""
function Usage
{
echo "usage: `basename $0` [DIR]"
echo "Create a toolchain project."
echo ""
}
function ProjectMkdir
{
if [ ! -d $ProjectDir ]; then
echo "[INFO] $ProjectDir is not exist, create it"
mkdir $ProjectDir
else
echo "[INFO] $ProjectDir is exist"
fi
[ ! -d $ProjectDir/script ] && mkdir $ProjectDir/script
[ ! -d $ProjectDir/setup ] && mkdir $ProjectDir/setup
[ ! -d $ProjectDir/src ] && mkdir $ProjectDir/src
[ ! -d $ProjectDir/build ] && mkdir $ProjectDir/build
[ ! -d $ProjectDir/tool-chain ] && mkdir $ProjectDir/tool-chain
cp toolchain_build.sh $ProjectDir/script
cp toolchain_config.lst $ProjectDir/script
}
function SourceSetup
{
local src=`pwd`/../source
local dest=$PRJROOT/setup
if [ -f $src/$BINUTILS_VERSION.tar.bz2 ]; then
echo "[INFO] pkg: $BINUTILS_VERSION.tar.bz2 is exist, Copy it"
cp $src/$BINUTILS_VERSION.tar.bz2 $dest
else
echo "[INFO] pkg: $BINUTILS_VERSION.tar.bz2 is not exist, Download it"
cd $dest
wget http://ftp.gnu.org/gnu/binutils/$BINUTILS_VERSION.tar.bz2
cd - > /dev/null
fi
if [ -f $src/$GCC_VERSION.tar.bz2 ]; then
echo "[INFO] pkg: $GCC_VERSION.tar.bz2 is exist, Copy it"
cp $src/$GCC_VERSION.tar.bz2 $dest
else
echo "[INFO] pkg: $GCC_VERSION.tar.bz2 is not exist, Download it"
cd $dest
wget http://ftp.gnu.org/gnu/gcc/$GCC_VERSION/$GCC_VERSION.tar.bz2
cd - > /dev/null
fi
if [ -f $src/$GLIBC_VERSION.tar.bz2 ]; then
echo "[INFO] pkg: $GLIBC_VERSION.tar.bz2 is exist, Copy it"
cp $src/$GLIBC_VERSION.tar.bz2 $dest
else
echo "[INFO] pkg: $GLIBC_VERSION.tar.bz2 is not exist, Download it"
cd $dest
wget ftp://ftp.gnu.org/gnu/glibc/$GLIBC_VERSION.tar.bz2
cd - > /dev/null
fi
if [ -f $src/$KERNEL_VERSION.tar.xz ]; then
echo "[INFO] pkg: $KERNEL_VERSION.tar.xz is exist, Copy it"
cp $src/$KERNEL_VERSION.tar.xz $dest
else
echo "[INFO] pkg: $KERNEL_VERSION.tar.xz is not exist, Download it"
cd $dest
wget https://www.kernel.org/pub/linux/kernel/v4.x/$KERNEL_VERSION.tar.xz
cd - > /dev/null
fi
}
#------------------------------------------------------------------------------------------
# main
#------------------------------------------------------------------------------------------
if [ $# != 1 ]; then
Usage
exit 1;
fi
ProjectDir=$1
source toolchain_config.lst $ProjectDir
ProjectMkdir
SourceSetup
echo ""
tree -L 2 $ProjectDir
#!/bin/bash
# Name: toolchain_config.lst
# Author: Peter Peng
# Ver: 0.1
export PRJROOT=$1
export TARGET=arm-none-linux-gnueabi
export PREFIX=${PRJROOT}/tool-chain
export TARGET_PREFIX=${PREFIX}/${TARGET}
export PATH=${PREFIX}/bin:${PATH}
export ARCH=arm
export BINUTILS_VERSION=binutils-2.27
export GCC_VERSION=gcc-5.4.0
export GLIBC_VERSION=glibc-2.23
export KERNEL_VERSION=linux-4.4.25
#!/bin/bash
# Name: toolchain_build.sh
# Author: Peter Peng
# Ver: 0.1
set -e
PWD_DIR=`pwd`
ROOT_DIR=`dirname $PWD_DIR`
SETUP_DIR=$ROOT_DIR/setup
BUILD_DIR=$ROOT_DIR/build
SRC_DIR=$ROOT_DIR/src
SCRIPT_DIR=$ROOT_DIR/script
function BinutilsBuild
{
echo "[INFO] $BINUTILS_VERSION build begin"
if [ ! -d $SRC_DIR/$BINUTILS_VERSION ]; then
cd $SRC_DIR
echo "[DEBUG] $BINUTILS_VERSION: uncompress tar.bz2"
tar -jxf $SETUP_DIR/$BINUTILS_VERSION.tar.bz2
cd - > /dev/null
fi
if [ ! -d $BUILD_DIR/$BINUTILS_VERSION ]; then
mkdir $BUILD_DIR/$BINUTILS_VERSION
fi
cd $BUILD_DIR/$BINUTILS_VERSION
echo "[DEBUG] $BINUTILS_VERSION: configure"
sh $SRC_DIR/$BINUTILS_VERSION/configure --target=$TARGET --prefix=$PREFIX --disable-nls --enable-shared
echo "[DEBUG] $BINUTILS_VERSION: make"
make
echo "[DEBUG] $BINUTILS_VERSION: make install"
make install
cd - > /dev/null
echo "[INFO] $BINUTILS_VERSION build succeed"
}
function GCCBuild_BOOTSTRAP
{
echo "[INFO] $GCC_VERSION build bootstrap begin"
if [ ! -d $SRC_DIR/$GCC_VERSION ]; then
cd $SRC_DIR
echo "[DEBUG] $GCC_VERSION: uncompress tar.bz2"
tar -jxf $SETUP_DIR/$GCC_VERSION.tar.bz2
cd $GCC_VERSION
./contrib/download_prerequisites
fi
if [ ! -d $BUILD_DIR/"$GCC_VERSION"_bootstrap ]; then
mkdir $BUILD_DIR/"$GCC_VERSION"_bootstrap
fi
cd $BUILD_DIR/"$GCC_VERSION"_bootstrap
echo "[DEBUG] $GCC_VERSION: configure"
sh $SRC_DIR/$GCC_VERSION/configure --target=$TARGET --prefix=$PREFIX --disable-shared --disable-threads --without-headers --disable-libmudflap --enable-languages=c,c++ --disable-nls
echo "[DEBUG] $GCC_VERSION: make all-gcc"
make all-gcc
echo "[DEBUG] $GCC_VERSION: make install-gcc"
make install-gcc
echo "[DEBUG] $GCC_VERSION: make all-target-libgcc"
make all-target-libgcc
echo "[DEBUG] $GCC_VERSION: make install-target-libgcc"
make install-target-libgcc
cd - > /dev/null
echo "[INFO] $GCC_VERSION build bootstrap succeed"
}
function GIBCBuild
{
echo "[INFO] $GLIBC_VERSION build begin"
if [ ! -d $SRC_DIR/$GLIBC_VERSION ]; then
cd $SRC_DIR
echo "[DEBUG] $GLIBC_VERSION: uncompress tar.bz2"
tar -jxf $SETUP_DIR/$GLIBC_VERSION.tar.bz2
cd - > /dev/null
fi
if [ ! -d $BUILD_DIR/$GLIBC_VERSION ]; then
mkdir $BUILD_DIR/$GLIBC_VERSION
fi
cd $BUILD_DIR/$GLIBC_VERSION
echo "[DEBUG] $GLIBC_VERSION: configure"
sh $SRC_DIR/$GLIBC_VERSION/configure --host=$TARGET --prefix=$PREFIX/$TARGET --with-tls --disable-profile --enable-add-ons --with-headers=$PREFIX/$TARGET/include libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes libc_cv_arm_tls=yes --disable-nls
echo "[DEBUG] $GLIBC_VERSION: make"
make CC=$TARGET-gcc AR=$TARGET-ar RANLIB=$TARGET-ranlib
echo "[DEBUG] $GLIBC_VERSION: make install"
make install
cd - > /dev/null
echo "[INFO] $GLIBC_VERSION build succeed"
}
function GCCBuild
{
echo "[INFO] $GCC_VERSION build begin"
if [ ! -d $BUILD_DIR/$GCC_VERSION ]; then
mkdir $BUILD_DIR/$GCC_VERSION
fi
cd $BUILD_DIR/$GCC_VERSION
echo "[DEBUG] $GCC_VERSION: configure"
sh $SRC_DIR/$GCC_VERSION/configure --target=$TARGET --prefix=$PREFIX --enable-languages=c,c++ --enable-shared --disable-nls
echo "[DEBUG] $GCC_VERSION: make"
make
echo "[DEBUG] $GCC_VERSION: make install"
make install
cd - > /dev/null
echo "[INFO] $GCC_VERSION build succeed"
}
function InstallKernelHeaders
{
echo "[INFO] $KERNEL_VERSION install headers begin"
if [ ! -d $SRC_DIR/$KERNEL_VERSION ]; then
cd $SRC_DIR
echo "[DEBUG] $KERNEL_VERSION: uncompress tar.xz"
tar -xf $SETUP_DIR/$KERNEL_VERSION.tar.xz
cd - > /dev/null
fi
cd $SRC_DIR/$KERNEL_VERSION
echo "[DEBUG] $KERNEL_VERSION: make headers_install"
make ARCH=$ARCH CROSS_COMPILE=$TARGET- INSTALL_HDR_PATH=$PREFIX/$TARGET headers_install
cd - > /dev/null
echo "[INFO] $KERNEL_VERSION install headers succeed"
}
#------------------------------------------------------------------------------------------
# main
#------------------------------------------------------------------------------------------
source toolchain_config.lst $ROOT_DIR
BinutilsBuild
InstallKernelHeaders
GCCBuild_BOOTSTRAP
GIBCBuild
GCCBuild
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。