guowenxue
2024-02-28 32ad5888c3b424b8e276f1dc7a32dc50061a62b0
commit | author | age
849fbd 1 #!/bin/bash
G 2
3 # bitbake target
4 BB_TARGET=yocto-image-full
5
6 # this project absolute path
7 PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
8
9 # top project absolute path
32ad58 10 TOP_PATH=$PRJ_PATH/..
849fbd 11
1cf118 12 # prefix install path
G 13 PRFX_PATH=$PRJ_PATH/install
14
15 # final install path
16 #INST_PATH=/srv/ftp/
17
18 # compress system image or not
2ff83b 19 COMPRESS=yes
849fbd 20
G 21 # download taballs path
22 TARBALL_PATH=$PRJ_PATH/tarballs
23
24 # config file path
25 CONF_FILE=$TOP_PATH/config.json
26
27 # Download path
8e6109 28 #DL_PATH="/srv/yocto_packets/"
849fbd 29
G 30 # shell script will exit once get command error
31 set -e
32
33 #+-------------------------+
34 #| Shell script functions  |
35 #+-------------------------+
36
37 function pr_error() {
38     echo -e "\033[40;31m $1 \033[0m"
39 }
40
41 function pr_warn() {
42     echo -e "\033[40;33m $1 \033[0m"
43 }
44
45 function pr_info() {
46     echo -e "\033[40;32m $1 \033[0m"
47 }
48
49 # decompress a packet to destination path
50 function do_unpack()
51 {
52     tarball=$1
53     dstpath=`pwd`
54
55     if [[ $# == 2 ]] ; then
56         dstpath=$2
57     fi
58
59     pr_info "decompress $tarball => $dstpath"
60
61     mkdir -p $dstpath
62     case $tarball in
63         *.tar.gz)
64             tar -xzf $tarball -C $dstpath
65             ;;
66
67         *.tar.bz2)
68             tar -xjf $tarball -C $dstpath
69             ;;
70
71         *.tar.xz)
72             tar -xJf $tarball -C $dstpath
73             ;;
74
75         *.tar.zst)
76             tar -I zstd -xf $tarball -C $dstpath
77             ;;
78
79         *.tar)
80             tar -xf $tarball -C $dstpath
81             ;;
82
83         *.zip)
84             unzip -qo $tarball -d $dstpath
85             ;;
86
87         *)
88             pr_error "decompress Unsupport packet: $tarball"
89             return 1;
90             ;;
91     esac
92 }
93
94 # parser configure file and export environment variable
95 function export_env()
96 {
97     export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
98     export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
99     export YCT_VER=`jq -r ".system.version" $CONF_FILE | tr 'A-Z' 'a-z'`
8e6109 100     export BSP_URL=`jq -r ".bsp.giturl" $CONF_FILE`
849fbd 101
G 102     export YCT_BOARD=`echo $BOARD| cut -d- -f1`
103     export YCT_META=meta-$YCT_BOARD
8e6109 104     export YCT_SRC=$YCT_VER-$BSP_VER
G 105     export YCT_PATH=$PRJ_PATH/$YCT_SRC
849fbd 106     export BUILD_DIR=${BOARD}
G 107 }
108
109 function do_fetch()
110 {
8e6109 111     cd $PRJ_PATH
849fbd 112
8e6109 113     if [ ! -d $YCT_SRC/sources ] ; then
849fbd 114
8e6109 115         if [[ $BSP_URL =~ github.com ]] ; then
849fbd 116
8e6109 117             pr_info "start repo fetch Yocto $YCT_VER source code"
849fbd 118
8e6109 119             mkdir -p $YCT_PATH && cd $YCT_PATH
G 120
121             if ! command -v repo > /dev/null 2>&1 ; then
122                 curl https://storage.googleapis.com/git-repo-downloads/repo > repo
123                 chmod a+x repo
124                 export PATH=$YCT_PATH:$PATH
125             fi
126
127             BSP_VER=`echo $BSP_VER | sed 's/lf/imx/'`
128             repo init -u https://github.com/nxp-imx/imx-manifest -b imx-linux-$YCT_VER -m $BSP_VER.xml
129             repo sync && rm -f repo
130
131         else
132
133             pr_info "start download fetch Yocto $YCT_VER source code"
134
135             mkdir -p $TARBALL_PATH
136
137             # Download source code packet
138             if [ ! -s $TARBALL_PATH/$YCT_SRC.tar.xz ] ; then
86063e 139                 wget $BSP_URL/imx/bsp/$BSP_VER/$YCT_SRC.tar.xz -P $TARBALL_PATH
8e6109 140             fi
G 141
142             # decompress source code packet
143             do_unpack $TARBALL_PATH/$YCT_SRC.tar.xz
144
849fbd 145         fi
G 146
147     else
148         pr_warn "Yocto $YCT_VER source code fetched already"
149     fi
150
151     if [ ! -d $PRJ_PATH/$YCT_META ] ; then
152         pr_error "Yocto meta $YCT_META not exist"
153         exit ;
154     fi
155
156     pr_warn "start update BSP patches for $YCT_META"
1cf118 157     BSP_VER=`echo $BSP_VER | sed 's/imx/lf/'`
849fbd 158     cp $TOP_PATH/bootloader/patches/${BOARD}/uboot-imx-${BSP_VER}.patch $PRJ_PATH/$YCT_META/recipes-bsp/u-boot/files/
G 159     cp $TOP_PATH/kernel/patches/${BOARD}/linux-imx-${BSP_VER}.patch $PRJ_PATH/$YCT_META/recipes-kernel/linux/files/
32ad58 160     cp $TOP_PATH/images/patches/config-*.txt $PRJ_PATH/$YCT_META/recipes-bsp/u-boot/files/
849fbd 161
G 162     if [ ! -e $YCT_PATH/sources/$YCT_META ] ; then
163         ln -s $PRJ_PATH/$YCT_META $YCT_PATH/sources/$YCT_META
164     fi
165 }
166
167 function do_build()
168 {
169     cd $YCT_PATH
170
171     if [ ! -f ${BUILD_DIR}/conf/local.conf ] ; then
172         pr_info "source $YCT_BOARD-setup.sh"
173         MACHINE=${BOARD} source sources/$YCT_META/tools/$YCT_BOARD-setup.sh -b $BUILD_DIR
174     else
175         pr_info "source poky/oe-init-build-env"
176         source sources/poky/oe-init-build-env $BUILD_DIR
177     fi
178
179     if [[ -n "$DL_PATH" ]] ; then
180         sed -i "s|^#DL_DIR.*|DL_DIR ?= \"$DL_PATH/$YCT_VER\"|g" conf/local.conf
181         sed -i "s|^DL_DIR.*|DL_DIR ?= \"$DL_PATH/$YCT_VER\"|g" conf/local.conf
182     fi
183
184     bitbake $BB_TARGET
185 }
186
187 function do_install()
188 {
2334ec 189     IMAGE_NAME=yocto-${YCT_VER}-${BSP_VER}.wic
G 190     ROOTFS_TAR=rootfs-yocto-${YCT_VER}.tar.zst
191
849fbd 192     cd $YCT_PATH
G 193
194     echo ""
1cf118 195     pr_info "Yocto($YCT_VER) installed to '$PRFX_PATH'"
849fbd 196
1cf118 197     mkdir -p ${PRFX_PATH}
2334ec 198     cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/$BB_TARGET-$BOARD.wic ${PRFX_PATH}/$IMAGE_NAME
G 199     cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/$BB_TARGET-$BOARD.tar.zst ${PRFX_PATH}/$ROOTFS_TAR
6a78bd 200
G 201     if [[ $BOARD =~ mx6ull ]] ; then
202         cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/u-boot-${BOARD}.imx ${PRFX_PATH}/u-boot-${BOARD}.imx
203     else
204         cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/imx-boot ${PRFX_PATH}/u-boot-${BOARD}.imx
205     fi
2ff83b 206
G 207     cd ${PRFX_PATH}/
208
209     if [[ `echo $COMPRESS | tr 'A-Z' 'a-z'` == "yes" ]] ; then
2334ec 210         pr_info "Start bzip2 compress $IMAGE_NAME"
G 211         rm -f $IMAGE_NAME.bz2
212         bzip2 $IMAGE_NAME
2ff83b 213     fi
G 214     chmod a+x u-boot-${BOARD}.imx
215
216     ls && echo ""
849fbd 217
1cf118 218     if [ -n "$INST_PATH" -a -w $INST_PATH ] ; then
G 219
220         pr_info "Start copy Yocto system images to $INST_PATH"
2334ec 221         cp u-boot-${BOARD}.imx $INST_PATH
G 222         cp $IMAGE_NAME*        $INST_PATH
223         cp $ROOTFS_TAR         $INST_PATH
1cf118 224
G 225         ls ${INST_PATH} && echo ""
226     fi
227
849fbd 228 }
G 229
230 function do_clean()
231 {
232     cd $PRJ_PATH
233
1cf118 234     rm -rf $PRFX_PATH
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 Yocto source code"
245     do_clean
246     exit;
247 fi
248
249 pr_warn "start build Yocto $YCT_VER for ${BOARD}"
250
251 do_fetch
252
253 do_build
254
255 do_install