aboutsummaryrefslogtreecommitdiff
path: root/uadk/builders.sh
blob: 28f55178b1ad49633a55c910a5263245a96ab0bc (plain)
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
#!/bin/bash -e

export UADK_VER_ENV="v2"
LOCK_FILE="/var/lock/uadk-lock"
BIN_DIR="usr/local/bin"
INC_DIR="usr/local/include"
LIB_DIR="usr/local/lib"
UACCE_PATH="/sys/class/uacce"

lock() {
	exit_code=1
	pending=0
	while [ "$exit_code" != 0 ]; do
		exit_code=0
		mkdir ${LOCK_FILE} &> /dev/null || exit_code=$?
		#mkdir ${LOCK_FILE} || exit_code=$?
		if [ "$exit_code" != 0 ]; then
			if [ "$pending" = 0 ]; then
				# Some script is accessing hardware
				echo "Wait for other building script finishing."
				pending=1
			fi
		fi
	done
}

unlock() {
	if [ -d ${LOCK_FILE} ]; then
		rmdir ${LOCK_FILE}
		echo "Release lock"
	fi
}

ctrlc_handler() {
	echo "Ctrl-C caught...performing clean up"
	unlock
	# Exit with error code
	exit 2
}

# Check whether prebuilt package exists.
# If it exists, install binaries and libraries directly.
check_prebuilt_package() {
	exit_code=0
	cd ${WORKSPACE}
	case $1 in
	"openssl")
		if [ -d ${WORKSPACE}/openssl ]; then
			cd ${WORKSPACE}/openssl
			git checkout OpenSSL_1_1_1a || exit_code=$?
			if [ "$exit_code" != 0 ]; then
				cd ${WORKSPACE}
				rm -fr ${WORKSPACE}/openssl
				git clone https://github.com/openssl/openssl.git
				cd ${WORKSPACE}/openssl
			fi
		else
			git clone https://github.com/openssl/openssl.git
			cd ${WORKSPACE}/openssl
		fi
		git checkout OpenSSL_1_1_1a
		COMMIT=`git log HEAD^..HEAD | grep commit`
		NAME=`ls ../openssl-*.tar.xz`
		NAME=${NAME#*openssl-}
		;;
	*)
		echo "Invalid package name: $1"
		return 1
		;;
	esac
	NAME=`echo ${NAME} | cut -b 1-8`
	if [ -z ${NAME} ]; then
		echo "Can not find package ${NAME}."
		return 1
	fi
	COMMIT=${COMMIT#*commit }
	COMMIT=`echo ${COMMIT} | cut -b 1-8`
	if [ ${NAME} = ${COMMIT} ]; then
		echo "Prebuilt package ${NAME} is found."
		cd ${WORKSPACE}
		case $1 in
		"openssl")
			rm -fr ${WORKSPACE}/openssl
			tar -xJf openssl-${NAME}.tar.xz
			cd ${WORKSPACE}/openssl
			./config --prefix=${LIB_ROOT}/usr/local
			make install &> /dev/null
			;;
		esac
		return 0
	fi
	echo "Package name ${NAME} could not match commit name ${COMMIT}."
	return 1
}

# Check whether repo is downloaded since pull request occurs.
# If it exists, skip to fetch repo with master branch.
check_existed_repo() {
	exit_code=0
	cd ${WORKSPACE}
	case $1 in
	"uadk")
		if [ ! -d ${WORKSPACE}/uadk ]; then
			echo "Clone UADK repo."
			git clone --depth 1 https://github.com/Linaro/uadk.git
		fi
		cd ${WORKSPACE}/uadk
		;;
	"ssluadk")
		if [ ! -d ${WORKSPACE}/uadk_engine ]; then
			echo "Clone OPENSSL-UADK repo."
			git clone --depth 1 https://github.com/Linaro/uadk_engine.git
		fi
		cd ${WORKSPACE}/uadk_engine
		;;
	*)
		echo "Invalid repo name: $1"
		return 1
		;;
	esac
	return 0
}

# Delete uadk and ssluadk repo while build is finished.
clean_repo() {
	rm -fr ${WORKSPACE}/uadk
	rm -fr ${WORKSPACE}/uadk_engine
}

build_uadk() {
	echo "Prepare to build UADK repo."
	exit_code=0
	check_existed_repo uadk || exit_code=$?
	if [ "$exit_code" != 0 ]; then
		return $exit_code
	fi

	cd ${WORKSPACE}/uadk
	autoreconf -vfi

	# static build for v2
	exit_code=0
	./configure \
	  --prefix=${WORKSPACE}/uadk-static-v2/usr/local \
	  --includedir=${WORKSPACE}/uadk-static-v2/${INC_DIR}/uadk \
	  --enable-static --disable-shared --with-static_drv
	make -j$(nproc) || exit_code=$?
	if [ "$exit_code" != 0 ]; then
		echo "Fail to build UADK statically."
		return $exit_code
	fi
	make install
	exit_code=0
	if [ -d "$UACCE_PATH" ]; then
		sudo \
		LD_LIBRARY_PATH=${WORKSPACE}/uadk-static-v2/usr/local/lib/ \
		PATH=${WORKSPACE}/uadk-static-v2/usr/local/bin:${PATH}  \
		${WORKSPACE}/uadk/test/sanity_test.sh || exit_code=$?
	fi
	if [ "$exit_code" != 0 ]; then
		echo "Fail to run UADK tests with static build."
	fi

	sh cleanup.sh
	autoreconf -vfi

	# shared build for v2
	exit_code=0
	./configure \
	  --prefix=${WORKSPACE}/uadk-shared-v2/usr/local
	make -j$(nproc) || exit_code=$?
	if [ "$exit_code" != 0 ]; then
		echo "Fail to build UADK statically."
		return $exit_code
	fi
	make install
	exit_code=0
	if [ -d "$UACCE_PATH" ]; then
		sudo \
		LD_LIBRARY_PATH=${WORKSPACE}/uadk-shared-v2/usr/local/lib/ \
		PATH=${WORKSPACE}/uadk-shared-v2/usr/local/bin:${PATH}  \
		${WORKSPACE}/uadk/test/sanity_test.sh || exit_code=$?
	fi
	if [ "$exit_code" != 0 ]; then
		echo "Fail to run UADK tests with dynamic build."
	fi

	return 0
}

build_openssl() {
	exit_code=0
	check_prebuilt_package openssl || exit_code=$?
	if [ "$exit_code" = 0 ]; then
		return 0
	fi

	./config --prefix=${LIB_ROOT}/usr/local
	exit_code=0
	make -j$(nproc) || exit_code=$?
	if [ "$exit_code" != 0 ]; then
		echo "Fail to build OpenSSL ($exit_code)"
		return $exit_code
	fi
	make install || exit_code=$?
	if [ "$exit_code" != 0 ]; then
		echo "Fail to install OpenSSL ($exit_code)"
		return $exit_code
	fi

	# get the first 8 bytes of commit number
	COMMIT=`git log HEAD^..HEAD | grep commit`
	COMMIT=${COMMIT#*commit }
	COMMIT=`echo ${COMMIT} | cut -b 1-8`
	cd ${WORKSPACE}
	# clean old packages
	rm -f openssl-*.tar.xz || exit_code=$?
	echo "rm ssl $exit_code"
	# create new package
	tar -cJf openssl-${COMMIT}.tar.xz openssl || exit_code=$?
	echo "tar ssl $exit_code"
	return 0
}

build_uadk_openssl() {
	echo "Prepare to build OPENSSL-UADK repo."
	exit_code=0
	check_existed_repo ssluadk || exit_code=$?
	if [ "$exit_code" != 0 ]; then
		return $exit_code
	fi

	cd ${WORKSPACE}/uadk_engine
	autoreconf -i -f -v
	./configure --prefix=${LIB_ROOT}/usr/local \
		--libdir=${LIB_ROOT}/${LIB_DIR}/engines-1.1

	exit_code=0
	LD_LIBRARY_PATH=${LIB_ROOT}/${LIB_DIR}:${LIB_ROOT}/${LIB_DIR}/engines-1.1 \
	  PATH=${LIB_ROOT}/${BIN_DIR}:${PATH} \
	  C_INCLUDE_PATH=${LIB_ROOT}/${INC_DIR} \
	  make || exit_code=$?
	if [ "$exit_code" != 0 ]; then
		echo "Fail to build uadk_engine ($exit_code)"
		return $exit_code
	fi
	exit_code=0
	make install || exit_code=$?
	if [ "$exit_code" != 0 ]; then
		echo "Fail to install uadk_engine ($exit_code)"
		return $exit_code
	fi
	exit_code=0
	sudo \
	  LD_LIBRARY_PATH=${LIB_ROOT}/${LIB_DIR}:${LIB_ROOT}/${LIB_DIR}/engines-1.1 \
	  PATH=${LIB_ROOT}/${BIN_DIR}/:${PATH} \
	  C_INCLUDE_PATH=${LIB_ROOT}/${INC_DIR}/ \
	  openssl engine -t uadk_engine || exit_code=$?
	if [ "$exit_code" != 0 ]; then
		echo "Fail to verify uadk_engine engine ($exit_code)"
		return $exit_code
	fi
	exit_code=0
	if [ -d "$UACCE_PATH" ]; then
		sudo \
		LD_LIBRARY_PATH=${LIB_ROOT}/${LIB_DIR}:${LIB_ROOT}/${LIB_DIR}/engines-1.1 \
		PATH=${LIB_ROOT}/${BIN_DIR}/:${PATH} \
		C_INCLUDE_PATH=${LIB_ROOT}/${INC_DIR}/ \
		test/sanity_test.sh || exit_code=$?
	fi
	if [ "$exit_code" != 0 ]; then
		echo "Fail to run sanity test ($exit_code)"
		return $exit_code
	fi

	return $exit_code
}

# Initialize trap to call ctrlc_handler function when signal 2 (SIGINT) is
# received.
trap "ctrlc_handler" 2

if [ -z "${WORKSPACE}" ]; then
  # Local build
  export WORKSPACE=${PWD}
fi
LIB_ROOT=${WORKSPACE}/uadk-shared-v2
export PKG_CONFIG_PATH=${LIB_ROOT}/${LIB_DIR}/pkgconfig/

# Prevent multiple building scripts to access hardware
lock

echo "#${BUILD_NUMBER}-${ghprbActualCommit:0:8}" > ${WORKSPACE}/version.txt

# Build dependencies already pre-installed on the node
#sudo apt update -q=2
#sudo apt install -q=2 --yes --no-install-recommends zlib1g-dev libnuma-dev

rm -fr ${WORKSPACE}/uadk-shared-v2
rm -fr ${WORKSPACE}/uadk-static-v2

exit_code=0

# Build OPENSSL
build_openssl || exit_code=$?
if [ "$exit_code" != 0 ]; then
	clean_repo
	unlock
	exit 1
fi
# Build UADK
build_uadk || exit_code=$?
if [ "$exit_code" != 0 ]; then
	clean_repo
	unlock
	exit 1
fi
# Build UADK-OPENSSL
build_uadk_openssl || exit_code=$?
if [ "$exit_code" != 0 ]; then
	clean_repo
	unlock
	exit 1
fi

clean_repo
unlock
echo "Run build script successfully!"