#!/bin/bash
|
|
# this project absolute path
|
PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
|
|
# top project absolute path
|
TOP_PATH=$(realpath $PRJ_PATH/..)
|
|
# binaries build prefix install path
|
PRFX_PATH=$PRJ_PATH/install
|
|
# binaries finally install path if needed
|
#INST_PATH=/tftp
|
|
# download taballs path
|
TARBALL_PATH=$PRJ_PATH/tarballs
|
|
# config file path
|
CONF_FILE=$TOP_PATH/config.json
|
|
# shell script will exit once get command error
|
set -e
|
|
#+-------------------------+
|
#| Shell script functions |
|
#+-------------------------+
|
|
function pr_error() {
|
echo -e "\033[40;31m $1 \033[0m"
|
}
|
|
function pr_warn() {
|
echo -e "\033[40;33m $1 \033[0m"
|
}
|
|
function pr_info() {
|
echo -e "\033[40;32m $1 \033[0m"
|
}
|
|
# decompress a packet to destination path
|
function do_unpack()
|
{
|
tarball=$1
|
dstpath=`pwd`
|
|
if [[ $# == 2 ]] ; then
|
dstpath=$2
|
fi
|
|
pr_info "decompress $tarball => $dstpath"
|
|
mkdir -p $dstpath
|
case $tarball in
|
*.tar.gz)
|
tar -xzf $tarball -C $dstpath
|
;;
|
|
*.tar.bz2)
|
tar -xjf $tarball -C $dstpath
|
;;
|
|
*.tar.xz)
|
tar -xJf $tarball -C $dstpath
|
;;
|
|
*.tar.zst)
|
tar -I zstd -xf $tarball -C $dstpath
|
;;
|
|
*.tar)
|
tar -xf $tarball -C $dstpath
|
;;
|
|
*.zip)
|
unzip -qo $tarball -d $dstpath
|
;;
|
|
*)
|
pr_error "decompress Unsupport packet: $tarball"
|
return 1;
|
;;
|
esac
|
}
|
|
# parser configure file and export environment variable
|
function export_env()
|
{
|
export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
|
export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
|
export BSP_URL=`jq -r ".bsp.giturl" $CONF_FILE`
|
export CROSS_COMPILE=`jq -r ".bsp.crosstool" $CONF_FILE`
|
|
export DEF_CONFIG=${BOARD}_defconfig
|
export IMG_KER=linuxrom-${BOARD}.itb
|
|
export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
|
export ARCH=arm
|
|
export SRCS="linux-at91"
|
}
|
|
function do_fetch()
|
{
|
cd $PRJ_PATH
|
|
for src in $SRCS
|
do
|
if [ -d $src ] ; then
|
pr_info "$src source code fetched already"
|
continue
|
fi
|
|
pr_info "start fetch $src source code"
|
mkdir -p $TARBALL_PATH
|
|
# Download source code packet
|
if [ ! -s $TARBALL_PATH/$src.tar.xz ] ; then
|
wget $BSP_URL/at91/bsp/$BSP_VER/$src.tar.xz -P $TARBALL_PATH
|
fi
|
|
# decompress source code packet
|
do_unpack $TARBALL_PATH/$src.tar.xz
|
|
# do patch if patch file exist
|
patch_file=$PRJ_PATH/patches/$BOARD/$src-$BSP_VER.patch
|
if [ -s $patch_file ] ; then
|
pr_warn "do patch for $src now..."
|
cd $src
|
patch -p1 < $patch_file
|
cd -
|
fi
|
done
|
}
|
|
function build_kernel()
|
{
|
SRC=linux-at91
|
|
pr_warn "start build $SRC"
|
cd $PRJ_PATH/${SRC}
|
|
if [ ! -f .config ] ; then
|
make $DEF_CONFIG ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE}
|
fi
|
|
make -j${JOBS} ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE}
|
|
pr_info "mkimage -f linuxrom-${BOARD}.its ${IMG_KER}"
|
mkimage -f linuxrom-${BOARD}.its ${IMG_KER} > /dev/null
|
chmod a+x ${IMG_KER}
|
|
set -x
|
cp ${IMG_KER} $PRFX_PATH/
|
set +x
|
}
|
|
function do_build()
|
{
|
cd $PRJ_PATH
|
mkdir -p $PRFX_PATH
|
|
build_kernel
|
}
|
|
function do_install()
|
{
|
cd $PRJ_PATH
|
|
echo ""
|
pr_info "linux kernel installed to '$PRFX_PATH'"
|
ls $PRFX_PATH && echo ""
|
|
if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then
|
pr_info "install linux images to '$INST_PATH'"
|
cp $PRFX_PATH/* $INST_PATH
|
fi
|
}
|
|
function do_clean()
|
{
|
for d in $SRCS
|
do
|
rm -rf $PRJ_PATH/$d
|
done
|
|
rm -rf $PRJ_PATH/tarballs
|
rm -rf $PRFX_PATH
|
}
|
|
#+-------------------------+
|
#| Shell script body entry |
|
#+-------------------------+
|
|
cd $PRJ_PATH
|
|
export_env
|
|
if [[ $# == 1 && $1 == -c ]] ;then
|
pr_warn "start clean linux kernel"
|
do_clean
|
exit;
|
fi
|
|
pr_warn "start build linux kernelel for ${BOARD}"
|
|
do_fetch
|
|
do_build
|
|
do_install
|