android
2024-07-12 314ddfe51b0d27205b3effcbebb5a47a000ae66b
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
#!/bin/bash
 
# this project absolute path
PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
 
# top project absolute path
TOP_PATH=$(realpath $PRJ_PATH/..)
 
# SDK build workspace
SDK_DIR=sdk_build
 
# binaries build prefix install path
PRFX_PATH=$PRJ_PATH/install
 
# binaries finally install path if needed
#INST_PATH=/tftp
 
# download taballs path
TARBALL_PATH=$PRJ_PATH/tarballs
 
# config file path
CONF_FILE=$TOP_PATH/config.json
 
# 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
            ;;
 
        *.tgz)
            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 ".board" $CONF_FILE | tr 'A-Z' 'a-z'`
    export SDK_VER=`jq -r ".linux.sdk_ver" $CONF_FILE`
    export SDK_PATH=`jq -r ".linux.sdk_path" $CONF_FILE`
    export DISTRO=`jq -r ".linux.distro" $CONF_FILE | tr 'A-Z' 'a-z'`
 
    export ARCH=arm64
 
    if [[ $BOARD =~ igkboard-rk3568 ]] ; then
        LUNCH_BOARD=rockchip_rk3568_evb1_ddr4_v10_defconfig
    fi
 
    PYTHON_VERSION=$(python --version 2>&1 | awk '{print $2}' | cut -d. -f1)
    if [ $PYTHON_VERSION != "3" ] ; then
        PYTHON_VERSION=$(python --version 2>&1)
        pr_error "ERROR: This SDK build need Python3, current version is $PYTHON_VERSION"
        pr_warn "You can use 'sudo update-alternatives --config python' command to switch it"
        exit
    fi
}
 
function do_fetch()
{
    SDK_FPATH=$SDK_PATH/$SDK_VER
 
    PATCH_FLAG="$SDK_DIR/kernel/arch/arm64/configs/rockchip_linux_defconfig"
 
    if [ -e $PATCH_FLAG ] ;  then
        pr_warn "SDK source code fetched already, skip do fetch"
        return ;
    fi
 
    if [ ! -d $SDK_DIR/.repo ] ; then
 
        SDK_FILE=`ls $SDK_FPATH/*.tar.bz2 | head -n 1`
 
        if [ ! -d $SDK_FPATH ] ; then
            pr_error "ERROR: SDK package folder '$SDK_FPATH' doesn't exist!"
            exit
        fi
 
        pr_info "decompress SDK tarball $SDK_FILE..."
 
        do_unpack $SDK_FILE $SDK_DIR
    fi
    
    cd $SDK_DIR
 
    if [ ! -e $PATCH_FLAG ] ; then
        pr_info "repo sync to checkout source code..."
        ./.repo/repo/repo sync -l
    fi
 
    if ! grep -q '^CONFIG_CAN=y' $PATCH_FLAG ; then 
        pr_info "patch for linux kernel..."
        cd kernel
        patch -p1 < $PRJ_PATH/patch/kernel.patch
    fi
}
 
function do_build()
{
    pr_info "Choose your board"
 
    cd $SDK_DIR
    
    ./build.sh lunch:${LUNCH_BOARD}
 
    RK_ROOTFS_SYSTEM=$DISTRO ./build.sh
 
    pr_info "System image will be installed to $PRJ_PATH/$SDK_DIR/rockdev"
    ls ./rockdev
}
 
#+-------------------------+
#| Shell script body entry |
#+-------------------------+
 
export_env
 
pr_warn "start build linux kernel for ${BOARD}"
 
do_fetch
 
do_build