forked from BoatPeopleBRTIK/build-artik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_ubuntu_sysroot.sh
executable file
·428 lines (359 loc) · 10.4 KB
/
build_ubuntu_sysroot.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/bin/bash
set -ex
BUILDCONFIG=
ARCH=armhf
SBUILD_CONF=~/.sbuildrc
TARGET_DIR=
PORT=
SKIP_BUILD=false
PREBUILT_REPO_DIR=
TARGET_BOARD=
print_usage()
{
echo "-h/--help Show help options"
echo "-p|--package Target package file"
echo "-c/--config Config file path to build ex) -c config/artik5.cfg"
echo "--chroot Chroot name"
echo "-C|--sbuild-conf Sbuild configuration path"
echo "-D|--dest-dir Build output directory"
echo "-s|--server-port Server port"
echo "--skip-build Skip package build"
echo "--use-prebuilt-repo Use prebuilt repository"
echo "-n|--ubuntu-name Ubuntu image name"
echo "--ubuntu-version Ubuntu version ex) --ubuntu-version bionic|xenial"
echo "-b [TARGET_BOARD] Target board ex) -b artik710|artik530|artik5|artik10"
exit 0
}
parse_options()
{
for opt in "$@"
do
case "$opt" in
-h|--help)
print_usage
shift ;;
-A|--arch)
ARCH="$2"
shift ;;
-c|--config)
CONFIG_FILE="$2"
shift ;;
--chroot)
CHROOT="$2"
shift ;;
-C|--sbuild-conf)
SBUILD_CONF=`readlink -e "$2"`
shift ;;
-D|--dest-dir)
DEST_DIR=`readlink -e "$2"`
shift ;;
-s|--server-port)
PORT="$2"
shift ;;
--skip-build)
SKIP_BUILD=true
shift ;;
--use-prebuilt-repo)
PREBUILT_REPO_DIR=`readlink -e "$2"`
shift ;;
--ubuntu-version)
UBUNTU_VERSION="$2"
shift ;;
-b)
TARGET_BOARD="$2"
shift ;;
*)
shift ;;
esac
done
}
package_check()
{
command -v $1 >/dev/null 2>&1 || { echo >&2 "${1} not installed. Aborting."; exit 1; }
}
gen_ubuntu_meta()
{
where=$(readlink -e $1)
origin=$2
label=$3
pushd $where
apt-ftparchive sources . \
| tee "$where"/Sources \
| gzip -9 > "$where"/Sources.gz
apt-ftparchive packages "$where" \
| sed "s@$where@@" \
| tee "$where"/Packages \
| gzip -9 > "$where"/Packages.gz
# sponge comes from moreutils
apt-ftparchive \
-o"APT::FTPArchive::Release::Origin=$origin" \
-o"APT::FTPArchive::Release::Label=$label" \
-o"APT::FTPArchive::Release::Codename=$where" release "$where" \
| sponge "$where"/Release
popd
}
move_build_output()
{
where=$(readlink -e $1)
set +e
mv *.build *.changes *.dsc *.deb *.ddeb *.tar.* *.udeb $where
}
start_local_server()
{
where=$(readlink -e $1)
port=$2
pushd $where
gen_ubuntu_meta $where artik-local repo
python3 -m http.server $port --bind 127.0.0.1&
SERVER_PID=$!
popd
}
stop_local_server()
{
kill -9 $SERVER_PID
}
build_package()
{
local pkg=$1
local dest_dir=$2
if [ "$JOBS" == "" ]; then
JOBS=`getconf _NPROCESSORS_ONLN`
fi
if [ -d $pkg ]; then
debian_dir=`find ./$pkg -name "debian" -type d`
if [ -d $debian_dir ]; then
pushd $debian_dir/../
echo "Build $pkg.."
SBUILD_CONFIG=$SBUILD_CONF sbuild --chroot $CHROOT \
--host $BUILD_ARCH \
--extra-repository="deb [trusted=yes] http://localhost:$PORT ./" \
--anything-failed-commands="touch $dest_dir/.build_failed" \
--dpkg-source-opts="-I.git*" \
-j$JOBS --verbose
popd
move_build_output $dest_dir
gen_ubuntu_meta $dest_dir artik-local repo
if [ -e $dest_dir/.build_failed ]; then
abnormal_exit
exit -1
fi
fi
fi
}
abnormal_exit()
{
if [ "$SERVER_PID" != "" ]; then
kill -9 $SERVER_PID
fi
if [ -e "${TARGET_DIR}/debs/.build_failed" ]; then
rm -f $TARGET_DIR/debs/.build_failed
fi
}
error()
{
JOB="$0" # job name
LASTLINE="$1" # line of error occurrence
LASTERR="$2" # error code
echo "ERROR in ${JOB} : line ${LASTLINE} with exit code ${LASTERR}"
exit 1
}
find_unused_port()
{
read LOWERPORT UPPERPORT < /proc/sys/net/ipv4/ip_local_port_range
while :
do
PORT="`shuf -i $LOWERPORT-$UPPERPORT -n 1`"
ss -lpn | grep -q ":$PORT " || break
done
}
gen_artik_release()
{
upper_model=$(echo -n ${TARGET_BOARD} | awk '{print toupper($0)}')
cat > $TARGET_DIR/artik_sysroot_release << __EOF__
OFFICIAL_VERSION=${OFFICIAL_VERSION}
BUILD_VERSION=${BUILD_VERSION}
BUILD_DATE=${BUILD_DATE}
MODEL=${upper_model}
__EOF__
}
change_symlink_path()
{
pushd $TARGET_DIR
symlink_sysroot=$(sudo find rootfs/ -type l -lname '/*')
for file in $symlink_sysroot
do
link=$(ls -l $file | awk '{print $11}')
tmp=(`echo $file | tr "/" "\n"`)
i=$((${#tmp[@]}-2))
if [ $i -eq 0 ]; then
link='.'$link
elif [ $i -gt 0 ]; then
link='..'$link
fi
link=$(seq -s../ $i | tr -d '[:digit:]')$link
sudo rm -rf $file
sudo ln -s $link $file
done
popd
}
trap abnormal_exit INT ERR
trap 'error ${LINENO} ${?}' ERR
package_check sbuild sponge python3
parse_options "$@"
if [ "$CONFIG_FILE" != "" ]
then
. $CONFIG_FILE
fi
if [ "$BUILD_DATE" == "" ]; then
BUILD_DATE=`date +"%Y%m%d.%H%M%S"`
fi
if [ "$BUILD_VERSION" == "" ]; then
BUILD_VERSION="UNRELEASED"
fi
if [ "$TARGET_DIR" == "" ]; then
ARTIK_BUILD_DIR=`pwd`
TARGET_DIR="$ARTIK_BUILD_DIR/output/images/${TARGET_BOARD}"
fi
export BUILD_DATE=$BUILD_DATE
export BUILD_VERSION=$BUILD_VERSION
export TARGET_DIR=$TARGET_DIR/$BUILD_VERSION/$BUILD_DATE
BUILD_ARCH=$ARCH
if [ "$BUILD_ARCH" == "arm" ]; then
BUILD_ARCH="armhf"
fi
if [ "$PORT" == "" ]; then
find_unused_port
fi
mkdir -p $TARGET_DIR
gen_artik_release
[ -d $TARGET_DIR/debs ] || mkdir -p $TARGET_DIR/debs
if [ "$PREBUILT_REPO_DIR" != "" ]; then
cp -rf $PREBUILT_REPO_DIR/* $TARGET_DIR/debs
fi
cp -f ${E2E_PLUGIN_DIR}/ubuntu/${UBUNTU_VERSION}/${BUILD_ARCH}/*.deb $TARGET_DIR/debs
start_local_server $TARGET_DIR/debs $PORT
pushd ../
if ! $SKIP_BUILD; then
UBUNTU_PACKAGES=`cat ${UBUNTU_PACKAGE_FILE}`
for pkg in $UBUNTU_PACKAGES
do
build_package $pkg $TARGET_DIR/debs
done
fi
popd
if [ "$TARGET_BOARD" == "artik310s" ]; then
IMG_DIR=../ubuntu-build-service/bionic-artik
else
IMG_DIR=../ubuntu-build-service/xenial-${BUILD_ARCH}-${TARGET_BOARD}
fi
UBUNTU_NAME=SYSROOT-ubuntu-arm-$TARGET_BOARD-rootfs-$BUILD_VERSION-$BUILD_DATE
if [ "$IMG_DIR" != "" ]; then
echo "An ubuntu image generation starting..."
pushd $IMG_DIR
make clean
BUILD_SYSROOT=true PORT=$PORT ./configure
make IMAGEPREFIX=$UBUNTU_NAME
mv $UBUNTU_NAME* $TARGET_DIR
fi
# modify artik_sysroot_release
mkdir -p $TARGET_DIR/rootfs
sudo tar zxf $TARGET_DIR/${UBUNTU_NAME}.tar.gz -C $TARGET_DIR/rootfs
dpkg_list=`sudo chroot ${TARGET_DIR}/rootfs /bin/bash -c "dpkg -l | grep libartik-sdk"`
dpkg_array=(${dpkg_list// / })
SDK_VERSION=${dpkg_array[2]}
echo "SDK_VERSION=${SDK_VERSION}" | sudo tee --append $TARGET_DIR/artik_sysroot_release
sudo mv $TARGET_DIR/artik_sysroot_release $TARGET_DIR/rootfs/
sync
change_symlink_path
sudo tar zcf $TARGET_DIR/${UBUNTU_NAME}.tar.gz -C $TARGET_DIR/rootfs .
sudo tar --exclude=usr/lib/python2.7 --exclude=usr/lib/python3.5 -zcf $TARGET_DIR/${UBUNTU_NAME}-IDE.tar.gz -C $TARGET_DIR/rootfs usr/include usr/lib lib artik_sysroot_release
sudo rm -rf $TARGET_DIR/rootfs
stop_local_server
cat > $TARGET_DIR/install_sysroot.sh << __EOF__
#!/bin/sh
uudecode \$0
read -r -p "Install Path: " INSTALL_PATH
export INSTALL_PATH=\$(readlink -f "\$INSTALL_PATH")
mkdir -p \$INSTALL_PATH/BUILDROOT
sudo tar zxf $UBUNTU_NAME.tar.gz -C \$INSTALL_PATH/BUILDROOT
sudo rm -f $UBUNTU_NAME.tar.gz
__EOF__
if [ "$ARCH" == "arm" -o "$ARCH" == "armhf" ]; then
cat >> $TARGET_DIR/install_sysroot.sh << __EOF__
cat > \$INSTALL_PATH/sysroot_env << __EOF__
export PATH=:$PATH
export PKG_CONFIG_SYSROOT_DIR=\$INSTALL_PATH/BUILDROOT
export PKG_CONFIG_PATH=\$INSTALL_PATH/BUILDROOT/usr/lib/pkgconfig:\$INSTALL_PATH/BUILDROOT/usr/share/pkgconfig:\$INSTALL_PATH/BUILDROOT/usr/lib/arm-linux-gnueabihf/pkgconfig
export CC="arm-linux-gnueabihf-gcc --sysroot=\$INSTALL_PATH/BUILDROOT"
export LD="arm-linux-gnueabihf-ld --sysroot=\$INSTALL_PATH/BUILDROOT"
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabihf-
export TOOLCHAIN_FILE=\$INSTALL_PATH/toolchain.arm.cmake
IN__EOF__
cat > \$INSTALL_PATH/toolchain.arm.cmake << __EOF__
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_C_COMPILER /usr/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabihf-g++)
SET(CMAKE_LINKER /usr/bin/arm-linux-gnueabihf-ld)
SET(CMAKE_NM /usr/bin/arm-linux-gnueabihf-nm)
SET(CMAKE_OBJCOPY /usr/bin/arm-linux-gnueabihf-objcopy)
SET(CMAKE_OBJDUMP /usr/bin/arm-linux-gnueabihf-objdump)
SET(CMAKE_RANLIB /usr/bin/arm-linux-gnueabihf-ranlib)
SET(CMAKE_FIND_ROOT_PATH
$ENV{PKG_CONFIG_SYSROOT_DIR}
)
SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_SYSROOT
$ENV{PKG_CONFIG_SYSROOT_DIR}
)
IN__EOF__
echo "Sysroot in extracted on \$INSTALL_PATH/sysroot_env\""
echo "Please run \"source \$INSTALL_PATH/sysroot_env\" before compile."
exit
__EOF__
elif [ "$ARCH" == "aarch64" -o "$ARCH" == "arm64" ]; then
cat >> $TARGET_DIR/install_sysroot.sh << __EOF__
cat > \$INSTALL_PATH/sysroot_env << __EOF__
export PATH=:$PATH
export PKG_CONFIG_SYSROOT_DIR=\$INSTALL_PATH/BUILDROOT
export PKG_CONFIG_PATH=\$INSTALL_PATH/BUILDROOT/usr/lib/pkgconfig:\$INSTALL_PATH/BUILDROOT/usr/share/pkgconfig:\$INSTALL_PATH/BUILDROOT/usr/lib/aarch64-linux-gnu/pkgconfig
export CC="aarch64-linux-gnu-gcc --sysroot=\$INSTALL_PATH/BUILDROOT"
export LD="aarch64-linux-gnu-ld --sysroot=\$INSTALL_PATH/BUILDROOT"
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
export TOOLCHAIN_FILE=\$INSTALL_PATH/toolchain.aarch64.cmake
IN__EOF__
cat > \$INSTALL_PATH/toolchain.aarch64.cmake << __EOF__
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR aarch64)
SET(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++)
SET(CMAKE_LINKER /usr/bin/aarch64-linux-gnu-ld)
SET(CMAKE_NM /usr/bin/aarch64-linux-gnu-nm)
SET(CMAKE_OBJCOPY /usr/bin/aarch64-linux-gnu-objcopy)
SET(CMAKE_OBJDUMP /usr/bin/aarch64-linux-gnu-objdump)
SET(CMAKE_RANLIB /usr/bin/aarch64-linux-gnu-ranlib)
SET(CMAKE_FIND_ROOT_PATH
$ENV{PKG_CONFIG_SYSROOT_DIR}
)
SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_SYSROOT
$ENV{PKG_CONFIG_SYSROOT_DIR}
)
IN__EOF__
echo "Sysroot in extracted on \$INSTALL_PATH/sysroot_env\""
echo "Please run \"source \$INSTALL_PATH/sysroot_env\" before compile."
exit
__EOF__
fi
sed -i -e "s/IN__EOF__/__EOF__/g" $TARGET_DIR/install_sysroot.sh
uuencode $TARGET_DIR/$UBUNTU_NAME.tar.gz $UBUNTU_NAME.tar.gz >> $TARGET_DIR/install_sysroot.sh
chmod 755 $TARGET_DIR/install_sysroot.sh
echo "A new Ubuntu sysroot has been created"