1cc290419Sopenharmony_ci#!/bin/bash
2cc290419Sopenharmony_ci# Copyright (C) 2021 Huawei Device Co., Ltd.
3cc290419Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
4cc290419Sopenharmony_ci# you may not use this file except in compliance with the License.
5cc290419Sopenharmony_ci# You may obtain a copy of the License at
6cc290419Sopenharmony_ci#
7cc290419Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
8cc290419Sopenharmony_ci#
9cc290419Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
10cc290419Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
11cc290419Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cc290419Sopenharmony_ci# See the License for the specific language governing permissions and
13cc290419Sopenharmony_ci# limitations under the License.
14cc290419Sopenharmony_ci
15cc290419Sopenharmony_ci# gcc-9 needed
16cc290419Sopenharmony_ciset -e
17cc290419Sopenharmony_ci
18cc290419Sopenharmony_ciohos_root=$1
19cc290419Sopenharmony_ciohos_hdc_build="ohos_hdc_build"
20cc290419Sopenharmony_cicwddir=$(pwd)
21cc290419Sopenharmony_ci
22cc290419Sopenharmony_cibuild_in_source=true
23cc290419Sopenharmony_ci[ "X$ohos_root" == "X" ] && build_in_source=false
24cc290419Sopenharmony_ci
25cc290419Sopenharmony_ciif [ "$build_in_source" == "true" ] ; then
26cc290419Sopenharmony_ci	ohos_root_real=$(realpath $ohos_root)
27cc290419Sopenharmony_cifi
28cc290419Sopenharmony_ci
29cc290419Sopenharmony_ci[ "X$KEEP" == "X" ] && [ -d "$ohos_hdc_build" ] && rm -fr $ohos_hdc_build
30cc290419Sopenharmony_ci[ -d "$ohos_hdc_build" ] || mkdir $ohos_hdc_build
31cc290419Sopenharmony_ci
32cc290419Sopenharmony_ciSTATICLIB=""
33cc290419Sopenharmony_ciINCLUDES=""
34cc290419Sopenharmony_ci
35cc290419Sopenharmony_cifunction build_libusb ()
36cc290419Sopenharmony_ci{
37cc290419Sopenharmony_ci	libusb_install=$(realpath libusb)
38cc290419Sopenharmony_ci	[ "X$KEEP" == "X" ] && mkdir -pv ${libusb_install}/include && ln -svf /usr/include/libusb-1.0 ${libusb_install}/include/libusb
39cc290419Sopenharmony_ci	INCLUDES+="-I$(realpath ${libusb_install}/include) "
40cc290419Sopenharmony_ci}
41cc290419Sopenharmony_ci
42cc290419Sopenharmony_cifunction build_openssl ()
43cc290419Sopenharmony_ci{
44cc290419Sopenharmony_ci	pushd third_party_openssl
45cc290419Sopenharmony_ci	[ "X$KEEP" == "X" ] && ./Configure no-shared linux-generic64 && make
46cc290419Sopenharmony_ci	STATICLIB+="$(realpath libcrypto.a) "
47cc290419Sopenharmony_ci	INCLUDES+="-I$(realpath include) "
48cc290419Sopenharmony_ci	popd
49cc290419Sopenharmony_ci}
50cc290419Sopenharmony_ci
51cc290419Sopenharmony_cifunction build_libuv ()
52cc290419Sopenharmony_ci{
53cc290419Sopenharmony_ci	pushd third_party_libuv
54cc290419Sopenharmony_ci	[ "X$KEEP" == "X" ] && cmake . && make
55cc290419Sopenharmony_ci	STATICLIB+="$(realpath libuv_a.a) "
56cc290419Sopenharmony_ci	INCLUDES+="-I$(realpath include) "
57cc290419Sopenharmony_ci	popd
58cc290419Sopenharmony_ci}
59cc290419Sopenharmony_ci
60cc290419Sopenharmony_cifunction build_securec ()
61cc290419Sopenharmony_ci{
62cc290419Sopenharmony_ci	pushd third_party_bounds_checking_function
63cc290419Sopenharmony_ci	[ "X$KEEP" == "X" ] && gcc src/*.c -I$(pwd)/include -c && ar rcs libsecurec.a *.o
64cc290419Sopenharmony_ci	STATICLIB+="$(realpath libsecurec.a) "
65cc290419Sopenharmony_ci	INCLUDES+="-I$(realpath include) "
66cc290419Sopenharmony_ci	popd
67cc290419Sopenharmony_ci}
68cc290419Sopenharmony_ci
69cc290419Sopenharmony_cifunction build_lz4 ()
70cc290419Sopenharmony_ci{
71cc290419Sopenharmony_ci	pushd third_party_lz4
72cc290419Sopenharmony_ci	[ "X$KEEP" == "X" ] && make liblz4.a
73cc290419Sopenharmony_ci	STATICLIB+="$(realpath lib/liblz4.a) "
74cc290419Sopenharmony_ci	INCLUDES+="-I$(realpath lib) "
75cc290419Sopenharmony_ci	popd
76cc290419Sopenharmony_ci}
77cc290419Sopenharmony_ci
78cc290419Sopenharmony_cifunction build_hdc ()
79cc290419Sopenharmony_ci{
80cc290419Sopenharmony_ci	pushd developtools_hdc
81cc290419Sopenharmony_ci	echo $STATICLIB
82cc290419Sopenharmony_ci	echo $INCLUDES
83cc290419Sopenharmony_ci
84cc290419Sopenharmony_ci	DEFINES="-DHDC_HOST -DHARMONY_PROJECT"
85cc290419Sopenharmony_ci	export LDFLAGS="-Wl,--copy-dt-needed-entries"
86cc290419Sopenharmony_ci	export CXXFLAGS="-std=c++17 -ggdb -O0"
87cc290419Sopenharmony_ci
88cc290419Sopenharmony_ci	g++ ${DEFINES} ${CXXFLAGS} ${INCLUDES} $(find src/common/ src/host/ \( -name "*.cpp" -or -name "*.c" \)) -lusb-1.0 -ldl -lpthread $STATICLIB -o hdc
89cc290419Sopenharmony_ci
90cc290419Sopenharmony_ci	if [ -f hdc ]; then
91cc290419Sopenharmony_ci		echo build success
92cc290419Sopenharmony_ci		cp hdc $cwddir
93cc290419Sopenharmony_ci	else
94cc290419Sopenharmony_ci		echo build fail
95cc290419Sopenharmony_ci	fi
96cc290419Sopenharmony_ci	popd
97cc290419Sopenharmony_ci}
98cc290419Sopenharmony_ci
99cc290419Sopenharmony_cipushd $ohos_hdc_build
100cc290419Sopenharmony_ci
101cc290419Sopenharmony_ciif [ "X$KEEP" == "X" ]; then
102cc290419Sopenharmony_ci	for name in "developtools/hdc" "third_party/libuv" "third_party/openssl" "third_party/bounds_checking_function" "third_party/lz4"; do
103cc290419Sopenharmony_ci		reponame=$(echo $name | sed "s/\//_/g")
104cc290419Sopenharmony_ci		if [ "$build_in_source" == "true" ] ; then
105cc290419Sopenharmony_ci			cp -ra ${ohos_root_real}/${name} ${reponame} || exit 1
106cc290419Sopenharmony_ci		else
107cc290419Sopenharmony_ci			git clone https://gitee.com/openharmony/${reponame}
108cc290419Sopenharmony_ci		fi
109cc290419Sopenharmony_ci	done
110cc290419Sopenharmony_cifi
111cc290419Sopenharmony_ci
112cc290419Sopenharmony_cibuild_openssl
113cc290419Sopenharmony_cibuild_libuv
114cc290419Sopenharmony_cibuild_securec
115cc290419Sopenharmony_cibuild_lz4
116cc290419Sopenharmony_cibuild_libusb
117cc290419Sopenharmony_ci
118cc290419Sopenharmony_cibuild_hdc
119cc290419Sopenharmony_ci
120cc290419Sopenharmony_cipopd
121