forked from milkv-duo/duo-buildroot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·229 lines (185 loc) · 4.85 KB
/
build.sh
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
MILKV_BOARD_ARRAY=
MILKV_BOARD_ARRAY_LEN=
MILKV_BOARD=
MILKV_BOARD_CONFIG=
MILKV_IMAGE_CONFIG=
MILKV_DEFAULT_BOARD=milkv-duo
TOP_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
#echo "TOP_DIR: ${TOP_DIR}"
cd ${TOP_DIR}
function print_info()
{
printf "\e[1;32m%s\e[0m\n" "$1"
}
function print_err()
{
printf "\e[1;31mError: %s\e[0m\n" "$1"
}
function get_toolchain()
{
if [ ! -d host-tools ]; then
print_info "Toolchain does not exist, download it now..."
toolchain_url="https://sophon-file.sophon.cn/sophon-prod-s3/drive/23/03/07/16/host-tools.tar.gz"
echo "toolchain_url: ${toolchain_url}"
toolchain_file=${toolchain_url##*/}
echo "toolchain_file: ${toolchain_file}"
wget ${toolchain_url} -O ${toolchain_file}
if [ $? -ne 0 ]; then
print_err "Failed to download ${toolchain_url} !"
exit 1
fi
if [ ! -f ${toolchain_file} ]; then
print_err "${toolchain_file} not found!"
exit 1
fi
print_info "Extracting ${toolchain_file}..."
tar -xf ${toolchain_file}
if [ $? -ne 0 ]; then
print_err "Extract ${toolchain_file} failed!"
exit 1
fi
[ -f ${toolchain_file} ] && rm -rf ${toolchain_file}
fi
}
function get_available_board()
{
MILKV_BOARD_ARRAY=( $(find device -mindepth 1 -maxdepth 1 -type d -print ! -name "." | awk -F/ '{ print $NF }' | sort -t '-' -k2,2) )
#echo ${MILKV_BOARD_ARRAY[@]}
MILKV_BOARD_ARRAY_LEN=${#MILKV_BOARD_ARRAY[@]}
if [ $MILKV_BOARD_ARRAY_LEN -eq 0 ]; then
echo "No available config"
exit 1
fi
#echo ${MILKV_BOARD_ARRAY[@]} | xargs -n 1 | sed "=" | sed "N;s/\n/. /"
}
function choose_board()
{
echo "Select a target to build:"
echo ${MILKV_BOARD_ARRAY[@]} | xargs -n 1 | sed "=" | sed "N;s/\n/. /"
local index
read -p "Which would you like: " index
if [[ -z $index ]]; then
echo "Nothing selected."
exit 0
fi
if [[ -n $index && $index =~ ^[0-9]+$ && $index -ge 1 && $index -le $MILKV_BOARD_ARRAY_LEN ]]; then
MILKV_BOARD="${MILKV_BOARD_ARRAY[$((index - 1))]}"
#echo "index: $index, Board: $MILKV_BOARD"
else
print_err "Invalid input!"
exit 1
fi
}
function prepare_env()
{
source ${MILKV_BOARD_CONFIG}
source build/${MV_BUILD_ENV} > /dev/null 2>&1
defconfig ${MV_BOARD_LINK} > /dev/null 2>&1
echo "OUTPUT_DIR: ${OUTPUT_DIR}" # @build/milkvsetup.sh
}
function milkv_build()
{
# clean old img
old_image_count=`ls ${OUTPUT_DIR}/*.img* | wc -l`
if [ ${old_image_count} -ge 0 ]; then
pushd ${OUTPUT_DIR}
rm -rf *.img*
popd
fi
clean_all
build_all
if [ $? -eq 0 ]; then
print_info "Build board ${MILKV_BOARD} success!"
else
print_err "Build board ${MILKV_BOARD} failed!"
exit 1
fi
}
function milkv_pack_sd()
{
pack_sd_image
[ ! -d out ] && mkdir out
image_count=`ls ${OUTPUT_DIR}/*.img | wc -l`
if [ ${image_count} -ge 0 ]; then
mv ${OUTPUT_DIR}/*.img out/
# rename milkv-duo.img file with time
pushd out
for img in *.img
do
if [ "${img}" == "${MILKV_BOARD}.img" ]; then
mv $img ${MILKV_BOARD}-`date +%Y%m%d-%H%M`.img
fi
done
popd
# show latest img
latest_img=`ls -t out/*.img | head -n1`
if [ -z "${latest_img// }" ]; then
print_err "Gen image failed!"
else
print_info "Gen image successful: ${latest_img}"
fi
else
print_err "Create sd img failed!"
exit 1
fi
}
function milkv_pack()
{
if [ "${STORAGE_TYPE}" == "sd" ]; then
milkv_pack_sd
fi
}
function build_info()
{
print_info "Target Board: ${MILKV_BOARD}"
print_info "Target Board Config: ${MILKV_BOARD_CONFIG}"
print_info "Target Image Config: ${MILKV_IMAGE_CONFIG}"
}
get_available_board
function build_usage()
{
echo "Usage:"
echo "${BASH_SOURCE[0]} - Show this menu"
echo "${BASH_SOURCE[0]} lunch - Select a board to build"
echo "${BASH_SOURCE[0]} [board] - Build [board] directly, supported boards as follows:"
for board in "${MILKV_BOARD_ARRAY[@]}"; do
print_info "$board"
done
}
if [ $# -ge 1 ]; then
if [ "$1" = "lunch" ]; then
choose_board || exit 0
else
if [[ ${MILKV_BOARD_ARRAY[@]} =~ (^|[[:space:]])"${1}"($|[[:space:]]) ]]; then
MILKV_BOARD=${1}
#echo "$MILKV_BOARD"
else
print_err "${1} not supported!"
echo "Available boards: [ ${MILKV_BOARD_ARRAY[@]} ]"
exit 1
fi
fi
else
build_usage && exit 0
fi
if [ -z "${MILKV_BOARD// }" ]; then
print_err "No board specified!"
exit 1
fi
MILKV_BOARD_CONFIG=device/${MILKV_BOARD}/boardconfig.sh
MILKV_IMAGE_CONFIG=device/${MILKV_BOARD}/genimage.cfg
if [ ! -f ${MILKV_BOARD_CONFIG} ]; then
print_err "${MILKV_BOARD_CONFIG} not found!"
exit 1
fi
if [ ! -f ${MILKV_IMAGE_CONFIG} ]; then
print_err "${MILKV_IMAGE_CONFIG} not found!"
exit 1
fi
get_toolchain
build_info
export MILKV_BOARD="${MILKV_BOARD}"
prepare_env
milkv_build
milkv_pack