guowenxue
2024-01-20 6f4428329db5184a1ed2e10ebcc09ef385cbea6e
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
10 TOP_PATH=$(realpath $PRJ_PATH/..)
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
2ff83b 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'`
100
101     export YCT_BOARD=`echo $BOARD| cut -d- -f1`
102     export YCT_META=meta-$YCT_BOARD
103     export YCT_PATH=$PRJ_PATH/$YCT_VER-$BSP_VER
104     export BUILD_DIR=${BOARD}
105 }
106
107 function do_fetch()
108 {
109
110     mkdir -p $YCT_PATH && cd $YCT_PATH
111
112     if [ ! -d sources ] ; then
113
114         pr_info "start repo fetch Yocto $YCT_VER source code"
115
116         if ! command -v repo > /dev/null 2>&1 ; then
117             curl https://storage.googleapis.com/git-repo-downloads/repo > repo
118             chmod a+x repo
119             export PATH=$YCT_PATH:$PATH
120         fi
121
122         BSP_VER=`echo $BSP_VER | sed 's/lf/imx/'`
123         repo init -u https://github.com/nxp-imx/imx-manifest -b imx-linux-$YCT_VER -m $BSP_VER.xml
124         repo sync && rm -f repo
125
126     else
127         pr_warn "Yocto $YCT_VER source code fetched already"
128     fi
129
130     if [ ! -d $PRJ_PATH/$YCT_META ] ; then
131         pr_error "Yocto meta $YCT_META not exist"
132         exit ;
133     fi
134
135     pr_warn "start update BSP patches for $YCT_META"
1cf118 136     BSP_VER=`echo $BSP_VER | sed 's/imx/lf/'`
849fbd 137     cp $TOP_PATH/bootloader/patches/${BOARD}/uboot-imx-${BSP_VER}.patch $PRJ_PATH/$YCT_META/recipes-bsp/u-boot/files/
G 138     cp $TOP_PATH/kernel/patches/${BOARD}/linux-imx-${BSP_VER}.patch $PRJ_PATH/$YCT_META/recipes-kernel/linux/files/
139
140     if [ ! -e $YCT_PATH/sources/$YCT_META ] ; then
141         ln -s $PRJ_PATH/$YCT_META $YCT_PATH/sources/$YCT_META
142     fi
143 }
144
145 function do_build()
146 {
147     cd $YCT_PATH
148
149     if [ ! -f ${BUILD_DIR}/conf/local.conf ] ; then
150         pr_info "source $YCT_BOARD-setup.sh"
151         MACHINE=${BOARD} source sources/$YCT_META/tools/$YCT_BOARD-setup.sh -b $BUILD_DIR
152     else
153         pr_info "source poky/oe-init-build-env"
154         source sources/poky/oe-init-build-env $BUILD_DIR
155     fi
156
157     if [[ -n "$DL_PATH" ]] ; then
158         sed -i "s|^#DL_DIR.*|DL_DIR ?= \"$DL_PATH/$YCT_VER\"|g" conf/local.conf
159         sed -i "s|^DL_DIR.*|DL_DIR ?= \"$DL_PATH/$YCT_VER\"|g" conf/local.conf
160     fi
161
162     bitbake $BB_TARGET
163 }
164
165 function do_install()
166 {
167     cd $YCT_PATH
168
169     echo ""
1cf118 170     pr_info "Yocto($YCT_VER) installed to '$PRFX_PATH'"
849fbd 171
1cf118 172     mkdir -p ${PRFX_PATH}
G 173     cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/$BB_TARGET-$BOARD.wic ${PRFX_PATH}/
174     cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/$BB_TARGET-$BOARD.tar.zst ${PRFX_PATH}/
6a78bd 175
G 176     if [[ $BOARD =~ mx6ull ]] ; then
177         cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/u-boot-${BOARD}.imx ${PRFX_PATH}/u-boot-${BOARD}.imx
178     else
179         cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/imx-boot ${PRFX_PATH}/u-boot-${BOARD}.imx
180     fi
2ff83b 181
G 182     cd ${PRFX_PATH}/
183
184     if [[ `echo $COMPRESS | tr 'A-Z' 'a-z'` == "yes" ]] ; then
185         pr_info "Start bzip2 compress $BB_TARGET-$BOARD.wic"
186         rm -f $BB_TARGET-$BOARD.wic.bz2
187         bzip2 $BB_TARGET-$BOARD.wic
188     fi
189     chmod a+x u-boot-${BOARD}.imx
190
191     ls && echo ""
849fbd 192
1cf118 193     if [ -n "$INST_PATH" -a -w $INST_PATH ] ; then
G 194
195         pr_info "Start copy Yocto system images to $INST_PATH"
196         cp u-boot-${BOARD}.imx       $INST_PATH
197         cp $BB_TARGET-$BOARD.wic*    $INST_PATH
198         cp $BB_TARGET-$BOARD.tar.zst $INST_PATH
199
200         ls ${INST_PATH} && echo ""
201     fi
202
849fbd 203 }
G 204
205 function do_clean()
206 {
207     cd $PRJ_PATH
208
1cf118 209     rm -rf $PRFX_PATH
849fbd 210 }
G 211
212 #+-------------------------+
213 #| Shell script body entry |
214 #+-------------------------+
215
216 export_env
217
218 if [[ $# == 1 && $1 == -c ]] ;then
219     pr_warn "start clean Yocto source code"
220     do_clean
221     exit;
222 fi
223
224 pr_warn "start build Yocto $YCT_VER for ${BOARD}"
225
226 do_fetch
227
228 do_build
229
230 do_install