guowenxue
2024-12-23 b8e5f60912c77d52214c21e67fa91ec5f522c54c
commit | author | age
849fbd 1 #!/bin/bash
G 2
3 # this project absolute path
4 PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
5
6 # top project absolute path
7 TOP_PATH=$(realpath $PRJ_PATH/..)
8
9 # binaries build prefix install path
10 PRFX_PATH=$PRJ_PATH/install
11
12 # binaries finally install path if needed
13 #INST_PATH=/tftp
14
8e6109 15 # download taballs path
G 16 TARBALL_PATH=$PRJ_PATH/tarballs
17
849fbd 18 # config file path
G 19 CONF_FILE=$TOP_PATH/config.json
20
21 # shell script will exit once get command error
22 set -e
23
24 #+-------------------------+
25 #| Shell script functions  |
26 #+-------------------------+
27
28 function pr_error() {
29     echo -e "\033[40;31m $1 \033[0m"
30 }
31
32 function pr_warn() {
33     echo -e "\033[40;33m $1 \033[0m"
34 }
35
36 function pr_info() {
37     echo -e "\033[40;32m $1 \033[0m"
38 }
39
8e6109 40 # decompress a packet to destination path
G 41 function do_unpack()
42 {
43     tarball=$1
44     dstpath=`pwd`
45
46     if [[ $# == 2 ]] ; then
47         dstpath=$2
48     fi
49
50     pr_info "decompress $tarball => $dstpath"
51
52     mkdir -p $dstpath
53     case $tarball in
54         *.tar.gz)
55             tar -xzf $tarball -C $dstpath
56             ;;
57
58         *.tar.bz2)
59             tar -xjf $tarball -C $dstpath
60             ;;
61
62         *.tar.xz)
63             tar -xJf $tarball -C $dstpath
64             ;;
65
66         *.tar.zst)
67             tar -I zstd -xf $tarball -C $dstpath
68             ;;
69
70         *.tar)
71             tar -xf $tarball -C $dstpath
72             ;;
73
74         *.zip)
75             unzip -qo $tarball -d $dstpath
76             ;;
77
78         *)
79             pr_error "decompress Unsupport packet: $tarball"
80             return 1;
81             ;;
82     esac
83 }
84
849fbd 85 # parser configure file and export environment variable
G 86 function export_env()
87 {
88     export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
89     export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
8e6109 90     export BSP_URL=`jq -r ".bsp.giturl" $CONF_FILE`
G 91     export CROSS_COMPILE=`jq -r ".bsp.cortexAtool" $CONF_FILE`
849fbd 92
G 93     export BRANCH=$BSP_VER
8e6109 94     export KER_SRC=linux-imx
G 95     export KER_PATH=$PRJ_PATH/$KER_SRC
256f95 96     export DRV_PATH=$TOP_PATH/drivers/${BOARD}
849fbd 97
G 98     export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
d80f34 99
G 100     if [[ $BOARD =~ mx6ull ]] ; then
101         export ARCH=arm
102     else
103         export ARCH=arm64
104     fi
849fbd 105 }
G 106
107 function build_kernel()
108 {
b05652 109     defconfig=${BOARD}_defconfig
8e6109 110     patch_file=$PRJ_PATH/patches/$BOARD/${KER_SRC}-$BSP_VER.patch
849fbd 111
G 112     cd $PRJ_PATH
113
114     if [ -d $KER_PATH ] ; then
115         pr_info "linux kernel source code fetched already"
116     else
117         pr_info "start fetch linux kernel source code"
8e6109 118
G 119         if [[ $BSP_URL =~ github.com ]] ; then
120             git clone $BSP_URL/$KER_SRC.git -b $BRANCH --depth=1
121         else
122             mkdir -p $TARBALL_PATH
123
124             # Download source code packet
125             if [ ! -s $TARBALL_PATH/$KER_SRC.tar.xz ] ; then
86063e 126                 wget $BSP_URL/imx/bsp/$BSP_VER/$KER_SRC.tar.xz -P $TARBALL_PATH
8e6109 127             fi
G 128
129             # decompress source code packet
130             do_unpack $TARBALL_PATH/$KER_SRC.tar.xz
131         fi
849fbd 132     fi
G 133
134     cd $KER_PATH
135
d80f34 136     if [ ! -s arch/$ARCH/configs/$defconfig -a -s $patch_file ] ; then
86063e 137         pr_warn "do patch for $KER_SRC now..."
b05652 138         patch -p1 < $patch_file
G 139     fi
140
86063e 141     pr_warn "start build $KER_SRC"
G 142
849fbd 143     if [ ! -s .config ] ; then
G 144         make ${defconfig}
145     fi
146
147     make -j ${JOBS}
148 }
149
150 function do_install()
151 {
152     pr_info "start install linux kernel images"
153
154     cd $KER_PATH
155
156     if [ -d $PRFX_PATH ] ; then
157         rm -rf $PRFX_PATH/*
158     fi
159
d80f34 160     mkdir -p $PRFX_PATH/overlays
G 161
162     # Install system image and device tree binaries
163
164     if [[ $BOARD =~ 6ull ]] ; then
165         set -x
166         cp -f arch/${ARCH}/boot/zImage $PRFX_PATH
167         cp -f arch/${ARCH}/boot/dts/${BOARD}.dtb $PRFX_PATH
0687bf 168         if [ -d arch/${ARCH}/boot/dts/${BOARD} ] ; then
G 169             cp -f arch/${ARCH}/boot/dts/${BOARD}/*.dtbo $PRFX_PATH/overlays
170         fi
d80f34 171         set +x
G 172     else
173         set -x
174         cp -f arch/${ARCH}/boot/Image $PRFX_PATH
175         cp -f arch/${ARCH}/boot/dts/freescale/${BOARD}.dtb $PRFX_PATH
0687bf 176         if [ -d arch/${ARCH}/boot/dts/${BOARD} ] ; then
G 177             cp -f arch/${ARCH}/boot/dts/${BOARD}/*.dtbo $PRFX_PATH/overlays
178         fi
d80f34 179         set +x
G 180     fi
849fbd 181
G 182     # Install kernel modules
183     make modules_install INSTALL_MOD_PATH=$PRFX_PATH INSTALL_MOD_STRIP=1
184
185     echo ""
186     pr_info "linux kernel installed to '$PRFX_PATH'"
187     ls $PRFX_PATH && echo ""
188
189     if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then
190         pr_info "install linux kernel to '$INST_PATH'"
d80f34 191         cp -f $PRFX_PATH/*Image $INST_PATH
G 192         cp -f $PRFX_PATH/${BOARD}.dtb $INST_PATH
849fbd 193     fi
G 194 }
195
256f95 196 function build_driver()
G 197 {
198     if [ ! -d $DRV_PATH ] ; then
199         return 0;
200     fi
201
202     pr_info "start ${BOARD} linux drivers"
203
0687bf 204     if [ -d $DRV_PATH ] ; then
G 205         cd $DRV_PATH && ./build.sh
206     fi
256f95 207
G 208     cd $PRJ_PATH
209 }
210
849fbd 211 function do_build()
G 212 {
213     cd $PRJ_PATH
214
215     build_kernel
216
217     do_install
256f95 218
G 219     build_driver
849fbd 220 }
G 221
222 function do_clean()
223 {
224     cd $PRJ_PATH
225
8e6109 226     rm -rf $PRJ_PATH/$KER_SRC
G 227     rm -rf $PRJ_PATH/tarballs
849fbd 228     rm -rf $PRFX_PATH
256f95 229
0687bf 230     if [ -d $DRV_PATH ] ; then
G 231         cd $DRV_PATH && ./build.sh -c
232     fi
256f95 233
G 234     cd
849fbd 235 }
G 236
237 #+-------------------------+
238 #| Shell script body entry |
239 #+-------------------------+
240
241 export_env
242
243 if [[ $# == 1 && $1 == -c ]] ;then
244     pr_warn "start clean linux kernel"
245     do_clean
246     exit;
247 fi
248
249 pr_warn "start build linux kernel for ${BOARD}"
250
251 do_build
252