guowenxue
2023-12-12 849fbd3ab201e2e4694d302787fed48fb2172898
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
12 # binaries install path
13 INST_PATH=$PRJ_PATH/install
14
15 # download taballs path
16 TARBALL_PATH=$PRJ_PATH/tarballs
17
18 # config file path
19 CONF_FILE=$TOP_PATH/config.json
20
21 # Download path
22 #DL_PATH="/srv/yocto_packets/"
23
24 # shell script will exit once get command error
25 set -e
26
27 #+-------------------------+
28 #| Shell script functions  |
29 #+-------------------------+
30
31 function pr_error() {
32     echo -e "\033[40;31m $1 \033[0m"
33 }
34
35 function pr_warn() {
36     echo -e "\033[40;33m $1 \033[0m"
37 }
38
39 function pr_info() {
40     echo -e "\033[40;32m $1 \033[0m"
41 }
42
43 # decompress a packet to destination path
44 function do_unpack()
45 {
46     tarball=$1
47     dstpath=`pwd`
48
49     if [[ $# == 2 ]] ; then
50         dstpath=$2
51     fi
52
53     pr_info "decompress $tarball => $dstpath"
54
55     mkdir -p $dstpath
56     case $tarball in
57         *.tar.gz)
58             tar -xzf $tarball -C $dstpath
59             ;;
60
61         *.tar.bz2)
62             tar -xjf $tarball -C $dstpath
63             ;;
64
65         *.tar.xz)
66             tar -xJf $tarball -C $dstpath
67             ;;
68
69         *.tar.zst)
70             tar -I zstd -xf $tarball -C $dstpath
71             ;;
72
73         *.tar)
74             tar -xf $tarball -C $dstpath
75             ;;
76
77         *.zip)
78             unzip -qo $tarball -d $dstpath
79             ;;
80
81         *)
82             pr_error "decompress Unsupport packet: $tarball"
83             return 1;
84             ;;
85     esac
86 }
87
88 # parser configure file and export environment variable
89 function export_env()
90 {
91     export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
92     export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
93     export YCT_VER=`jq -r ".system.version" $CONF_FILE | tr 'A-Z' 'a-z'`
94
95     export YCT_BOARD=`echo $BOARD| cut -d- -f1`
96     export YCT_META=meta-$YCT_BOARD
97     export YCT_PATH=$PRJ_PATH/$YCT_VER-$BSP_VER
98     export BUILD_DIR=${BOARD}
99 }
100
101 function do_fetch()
102 {
103
104     mkdir -p $YCT_PATH && cd $YCT_PATH
105
106     if [ ! -d sources ] ; then
107
108         pr_info "start repo fetch Yocto $YCT_VER source code"
109
110         if ! command -v repo > /dev/null 2>&1 ; then
111             curl https://storage.googleapis.com/git-repo-downloads/repo > repo
112             chmod a+x repo
113             export PATH=$YCT_PATH:$PATH
114         fi
115
116         BSP_VER=`echo $BSP_VER | sed 's/lf/imx/'`
117         repo init -u https://github.com/nxp-imx/imx-manifest -b imx-linux-$YCT_VER -m $BSP_VER.xml
118         repo sync && rm -f repo
119
120     else
121         pr_warn "Yocto $YCT_VER source code fetched already"
122     fi
123
124     if [ ! -d $PRJ_PATH/$YCT_META ] ; then
125         pr_error "Yocto meta $YCT_META not exist"
126         exit ;
127     fi
128
129     pr_warn "start update BSP patches for $YCT_META"
130     cp $TOP_PATH/bootloader/patches/${BOARD}/uboot-imx-${BSP_VER}.patch $PRJ_PATH/$YCT_META/recipes-bsp/u-boot/files/
131     cp $TOP_PATH/kernel/patches/${BOARD}/linux-imx-${BSP_VER}.patch $PRJ_PATH/$YCT_META/recipes-kernel/linux/files/
132
133     if [ ! -e $YCT_PATH/sources/$YCT_META ] ; then
134         ln -s $PRJ_PATH/$YCT_META $YCT_PATH/sources/$YCT_META
135     fi
136 }
137
138 function do_build()
139 {
140     cd $YCT_PATH
141
142     if [ ! -f ${BUILD_DIR}/conf/local.conf ] ; then
143         pr_info "source $YCT_BOARD-setup.sh"
144         MACHINE=${BOARD} source sources/$YCT_META/tools/$YCT_BOARD-setup.sh -b $BUILD_DIR
145     else
146         pr_info "source poky/oe-init-build-env"
147         source sources/poky/oe-init-build-env $BUILD_DIR
148     fi
149
150     if [[ -n "$DL_PATH" ]] ; then
151         sed -i "s|^#DL_DIR.*|DL_DIR ?= \"$DL_PATH/$YCT_VER\"|g" conf/local.conf
152         sed -i "s|^DL_DIR.*|DL_DIR ?= \"$DL_PATH/$YCT_VER\"|g" conf/local.conf
153     fi
154
155     bitbake $BB_TARGET
156 }
157
158 function do_install()
159 {
160     cd $YCT_PATH
161
162     echo ""
163     pr_info "Yocto($YCT_VER) installed to '$INST_PATH'"
164
165     mkdir -p ${INST_PATH}
166     cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/$BB_TARGET-$BOARD.wic.zst ${INST_PATH}/yocto-image-${YCT_VER}.wic.zst
167     cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/$BB_TARGET-$BOARD.tar.zst ${INST_PATH}/rootfs-yocto-${YCT_VER}.tar.zst
168     cp $YCT_PATH/$BUILD_DIR/tmp/deploy/images/$BOARD/imx-boot ${INST_PATH}/u-boot-${BOARD}.imx
169     chmod a+x ${INST_PATH}/u-boot-${BOARD}.imx
170
171     ls ${INST_PATH} && echo ""
172 }
173
174 function do_clean()
175 {
176     cd $PRJ_PATH
177
178     rm -rf $INST_PATH
179 }
180
181 #+-------------------------+
182 #| Shell script body entry |
183 #+-------------------------+
184
185 export_env
186
187 if [[ $# == 1 && $1 == -c ]] ;then
188     pr_warn "start clean Yocto source code"
189     do_clean
190     exit;
191 fi
192
193 pr_warn "start build Yocto $YCT_VER for ${BOARD}"
194
195 do_fetch
196
197 do_build
198
199 do_install