forked from friendlyarm/friendlywrt_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·361 lines (312 loc) · 9.77 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/bash
set -eu
SCRIPTS_DIR=$(cd `dirname $0`; pwd)
if [ -h $0 ]
then
CMD=$(readlink $0)
SCRIPTS_DIR=$(dirname $CMD)
fi
cd $SCRIPTS_DIR
cd ../
TOP_DIR=$(pwd)
SDFUSE_DIR=$TOP_DIR/scripts/sd-fuse
firsttime_usage()
{
echo ""
echo "# select board: "
ALL_MK=`find ./device/friendlyelec -type f -name "*.mk" -printf "%f\n"`
for mk in ${ALL_MK}; do
echo " ./build.sh $mk"
done
ALL_MK_LINK=`find ./device/friendlyelec -type l -name "*.mk" -printf "%f\n"`
for mk in ${ALL_MK_LINK}; do
echo " ./build.sh $mk"
done
echo ""
}
usage()
{
echo "USAGE: ./build.sh <parameter>"
firsttime_usage
echo "# build module: "
echo " ./build.sh all -build all"
echo " ./build.sh uboot -build uboot only"
echo " ./build.sh kernel -build kernel only"
echo " ./build.sh friendlywrt -build friendlywrt rootfs only"
echo " ./build.sh sd-img -pack sd-card image, used to create bootable SD card"
echo " ./build.sh emmc-img -pack sd-card image, used to write friendlywrt to emmc"
echo "# clean"
echo " ./build.sh cleanall"
echo ""
}
if [ $# -ne 1 ]; then
usage
exit 1
fi
function log_error()
{
local now=`date +%s`
printf "\033[1;31m[ERROR]: $*\033[0m \n"
}
function log_warn()
{
local now=`date +%s`
printf "\033[1;31m[WARN]: $*\033[0m \n"
}
function log_info()
{
local now=`date +%s`
printf "\033[1;32m[INFO]: $* \033[0m \n"
}
function build_uboot(){
# build uboot
echo "============Start building uboot============"
echo "SRC = ${TOP_DIR}/u-boot"
echo "TARGET_ARCH = $TARGET_ARCH"
echo "TARGET_PLAT = $TARGET_PLAT"
echo "TARGET_UBOOT_CONFIG=$TARGET_UBOOT_CONFIG"
echo "========================================="
(cd ${SDFUSE_DIR} && {
DISABLE_MKIMG=1 UBOOT_SRC=${TOP_DIR}/u-boot ./build-uboot.sh friendlywrt_4.14_armhf
})
if [ $? -eq 0 ]; then
echo "====Building uboot ok!===="
else
echo "====Building uboot failed!===="
exit 1
fi
}
function build_kernel(){
# build kernel
echo "============Start building kernel============"
echo "SRC = ${TOP_DIR}/kernel"
echo "TARGET_ARCH = $TARGET_ARCH"
echo "TARGET_PLAT = $TARGET_PLAT"
echo "TARGET_KERNEL_CONFIG = $TARGET_KERNEL_CONFIG"
echo "=========================================="
(cd ${SDFUSE_DIR} && {
DISABLE_MKIMG=1 KERNEL_SRC=${TOP_DIR}/kernel ./build-kernel.sh friendlywrt_4.14_armhf
})
if [ $? -eq 0 ]; then
echo "====Building kernel ok!===="
else
echo "====Building kernel failed!===="
exit 1
fi
}
function build_friendlywrt(){
# build friendlywrt
echo "==========Start build friendlywrt=========="
echo "TARGET_FRIENDLYWRT_CONFIG=$TARGET_FRIENDLYWRT_CONFIG"
echo "FRIENDLYWRT_SRC=$FRIENDLYWRT_SRC"
echo "========================================="
/usr/bin/time -f "you take %E to build friendlywrt" $SCRIPTS_DIR/mk-friendlywrt.sh $TARGET_FRIENDLYWRT_CONFIG $FRIENDLYWRT_SRC
if [ $? -eq 0 ]; then
echo "====Building friendlywrt ok!===="
else
echo "====Building friendlywrt failed!===="
exit 1
fi
}
function build_all() {
build_uboot
build_kernel
build_friendlywrt
build_sdimg
}
function clean_all(){
echo "clean uboot, kernel, friendlywrt, img files"
cd $TOP_DIR/u-boot/ && make distclean && cd -
cd $TOP_DIR/kernel && make distclean && cd -
cd $TOP_DIR/friendlywrt &&
cd ${SDFUSE_DIR} && ./clean.sh && cd -
}
function copy_and_verify(){
if [ ! -f $1 ]; then
echo "not found: $1"
echo "$3"
exit 1
fi
cp $1 $2
}
function prepare_image_for_friendlyelec_eflasher(){
local OS_DIR=$1
local ROOTFS=$2
if [ ! -d ${SDFUSE_DIR}/${OS_DIR} ]; then
mkdir ${SDFUSE_DIR}/${OS_DIR}
fi
rm -rf ${SDFUSE_DIR}/${OS_DIR}/*
# clean
rm -rf ${SDFUSE_DIR}/out/rootfs.*
rm -rf ${SDFUSE_DIR}/out/boot.*
local ROOTFS_DIR=$(mktemp -d ${SDFUSE_DIR}/out/rootfs.XXXXXXXXX)
log_info "Copying ${TOP_DIR}/${FRIENDLYWRT_ROOTFS} to ${ROOTFS_DIR}/"
cp -af ${TOP_DIR}/${FRIENDLYWRT_SRC}/${FRIENDLYWRT_ROOTFS}/* ${ROOTFS_DIR}/
for (( i=0; i<${#FRIENDLYWRT_FILES[@]}; i++ ));
do
# apply patch to rootfs
if [ ! -z ${FRIENDLYWRT_FILES[$i]} ]; then
log_info "Applying ${FRIENDLYWRT_FILES[$i]} to ${ROOTFS_DIR}"
if [ -f ${TOP_DIR}/${FRIENDLYWRT_FILES[$i]}/install.sh ]; then
(cd ${TOP_DIR}/${FRIENDLYWRT_FILES[$i]} && {
./install.sh ${ROOTFS_DIR}
})
else
rsync -a --no-o --no-g --exclude='.git' ${TOP_DIR}/${FRIENDLYWRT_FILES[$i]}/* ${ROOTFS_DIR}/
fi
fi
done
local BOOT_DIR=$(mktemp -d ${SDFUSE_DIR}/out/boot.XXXXXXXXX)
# prepare uboot bin, boot.img and rootfs.img
local UBOOT_DIR=${TOP_DIR}/u-boot
local KERNEL_DIR=${TOP_DIR}/kernel
(cd ${SDFUSE_DIR} && {
./tools/update_uboot_bin.sh ${UBOOT_DIR} ./${OS_DIR}
if [ $? -ne 0 ]; then
log_error "error: fail to copy uboot bin file."
exit 1
fi
./tools/setup_boot_and_rootfs.sh ${UBOOT_DIR} ${KERNEL_DIR} ${BOOT_DIR} ${ROOTFS_DIR} ./prebuilt
if [ $? -ne 0 ]; then
log_error "error: fail to copy kernel to rootfs.img."
exit 1
fi
log_info "prepare boot.img ..."
./build-boot-img.sh ${BOOT_DIR} ./${OS_DIR}/boot.img
if [ $? -ne 0 ]; then
log_error "error: fail to gen boot.img."
exit 1
fi
log_info "prepare rootfs.img ..."
./build-rootfs-img.sh ${ROOTFS_DIR} ./${OS_DIR}/rootfs.img 0
if [ $? -ne 0 ]; then
log_error "error: fail to gen rootfs.img."
exit 1
fi
cat > ./${OS_DIR}/info.conf << EOL
title=${OS_DIR}
require-board=${TARGET_PLAT}
version=$(date +%Y-%m-%d)
EOL
./tools/update_prebuilt.sh ./${OS_DIR} ./prebuilt
})
# clean
rm -rf ${ROOTFS_DIR}
rm -rf ${BOOT_DIR}
}
function clean_device_files()
{
# create tmp dir
if [ ! -d ${1}/tmp ]; then
mkdir ${1}/tmp
fi
chmod 1777 ${1}/tmp
chown root:root ${1}/tmp
(cd ${1}/dev && find . ! -type d -exec rm {} \;)
}
function build_sdimg(){
source ${SDFUSE_DIR}/tools/util.sh
local HAS_BUILT_UBOOT=`has_built_uboot ${TOP_DIR}/u-boot ${SDFUSE_DIR}/out`
local HAS_BUILD_KERN=`has_built_kernel ${TOP_DIR}/kernel ${SDFUSE_DIR}/out`
local HAS_BUILD_KERN_MODULES=`has_built_kernel_modules ${TOP_DIR}/kernel ${SDFUSE_DIR}/out`
# log_info "HAS_BUILT_UBOOT = ${HAS_BUILT_UBOOT}"
# log_info "HAS_BUILD_KERN = ${HAS_BUILD_KERN}"
# log_info "HAS_BUILD_KERN_MODULES = ${HAS_BUILD_KERN_MODULES}"
if [ ${HAS_BUILT_UBOOT} -ne 1 ]; then
log_error "error: please build u-boot first."
exit 1
fi
if [ ${HAS_BUILD_KERN} -ne 1 ]; then
log_error "error: please build kernel first."
exit 1
fi
if [ ${HAS_BUILD_KERN_MODULES} -ne 1 ]; then
log_error "error: please build kernel first (miss kernel modules)."
exit 1
fi
local ROOTFS=${TOP_DIR}/${FRIENDLYWRT_SRC}/${FRIENDLYWRT_ROOTFS}
prepare_image_for_friendlyelec_eflasher ${TARGET_IMAGE_DIRNAME} ${ROOTFS} && (cd ${SDFUSE_DIR} && {
./mk-sd-image.sh ${TARGET_IMAGE_DIRNAME} ${TARGET_SD_RAW_FILENAME}
echo "-----------------------------------------"
echo "Run the following command for sdcard install:"
echo " sudo dd if=out/${TARGET_SD_RAW_FILENAME} bs=1M of=/dev/sdX"
echo "-----------------------------------------"
})
}
function install_toolchain() {
if [ ! -d /opt/FriendlyARM/toolchain/4.9.3 ]; then
log_info "installing toolchain: arm-linux-gcc 4.9.3"
sudo su -c "mkdir -p /opt/FriendlyARM/toolchain && cat $TOP_DIR/toolchain/gcc-x64/toolchain-4.9.3-armhf.tar.gz* | sudo tar xz -C /"
fi
if [ ! -d /opt/FriendlyARM/toolchain/6.4-aarch64 ]; then
log_info "installing toolchain: aarch-linux-gcc 6.4"
sudo su -c "mkdir -p /opt/FriendlyARM/toolchain && cat $TOP_DIR/toolchain/gcc-x64/toolchain-6.4-aarch64.tar.gz* | sudo tar xz -C /"
fi
}
function build_emmcimg() {
local ROOTFS=${TOP_DIR}/${FRIENDLYWRT_SRC}/${FRIENDLYWRT_ROOTFS}
prepare_image_for_friendlyelec_eflasher ${TARGET_IMAGE_DIRNAME} ${ROOTFS} && (cd ${SDFUSE_DIR} && {
./mk-emmc-image.sh ${TARGET_IMAGE_DIRNAME} ${TARGET_EFLASHER_RAW_FILENAME}
echo "-----------------------------------------"
echo "Run the following command for sdcard install:"
echo " sudo dd if=out/${TARGET_EFLASHER_RAW_FILENAME} bs=1M of=/dev/sdX"
echo "-----------------------------------------"
})
}
##############################################
# These arrays will be populated in the.mk file
declare -a FRIENDLYWRT_PACKAGES=("")
declare -a FRIENDLYWRT_FILES=("")
MK_LINK=".current_config.mk"
FOUND_MK_FILE=`find device/friendlyelec -name ${1} | wc -l`
if [ $FOUND_MK_FILE -gt 0 ]; then
MK_FILE=`ls device/friendlyelec/*/${1}`
echo "using config ${MK_FILE}"
rm -f ${MK_LINK}
ln -s ${MK_FILE} ${MK_LINK}
source ${MK_LINK}
install_toolchain
build_all
else
BUILD_TARGET=${1}
if [ -e "${MK_LINK}" ]; then
source ${MK_LINK}
# display var
# ( set -o posix ; set ) | less
else
echo "no .current_config.mk, please select a board first."
firsttime_usage
exit 1
fi
install_toolchain
#=========================
# build target
#=========================
if [ $BUILD_TARGET == uboot ];then
build_uboot
exit 0
elif [ $BUILD_TARGET == kernel ];then
build_kernel
exit 0
elif [ $BUILD_TARGET == friendlywrt ];then
build_friendlywrt
exit 0
elif [ $BUILD_TARGET == sd-img ]; then
build_sdimg
exit 0
elif [ $BUILD_TARGET == emmc-img ]; then
build_emmcimg
exit 0
elif [ $BUILD_TARGET == all ];then
build_all
exit 0
elif [ $BUILD_TARGET == cleanall ];then
clean_all
exit 0
else
echo "Can't find a build config file, please check again"
usage
exit 1
fi
fi
exit 0