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