-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbuild.sh
executable file
·304 lines (266 loc) · 8.42 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
#!/bin/bash -e
BUILD_CONFIG=release
RUN_CMAKE="no"
ROOT_DIR=$(readlink -f $(dirname $0))
VERBOSE_ARGS=""
CMAKE_TARGET=""
CMAKE_EXTRA_ARGS=""
RUN_TEST=""
RUN_BUILD="yes"
DUMP_TEST_ERRORS_STDOUT="no"
NINJA_TOOL="ninja"
INTEGRETION_TEST="no"
ASAN_BUILD="no"
ARGV=$@
# Constants
BOLD_PINK='\e[35;1m'
RESET='\e[0m'
GREEN='\e[32;1m'
RED='\e[31;1m'
BLUE='\e[34;1m'
echo "Root directory: ${ROOT_DIR}"
function print_usage() {
cat<<EOF
Usage: build.sh [options...]
--help | -h Print this help message and exit.
--configure Run cmake stage (aka configure stage).
--verbose | -v Run verbose build.
--debug Build for debug version.
--clean Clean the current build configuration (debug or release).
--run-tests Run all tests. Optionally, pass a test name to run: "--run-tests=<test-name>".
--no-build By default, build.sh always triggers a build. This option disables this behavior.
--test-errors-stdout When a test fails, dump the captured tests output to stdout.
--run-integration-tests Run integration tests.
--use-system-modules Use system's installed gRPC, Protobuf & Abseil dependencies.
--asan Build with address sanitizer enabled.
Example usage:
# Build the release configuration, run cmake if needed
build.sh
# Force run cmake and build the debug configuration
build.sh --configure --debug
EOF
}
## Parse command line arguments
while [ $# -gt 0 ]
do
arg=$1
case $arg in
--clean)
shift || true
CMAKE_TARGET="clean"
echo "Will run 'make clean'"
;;
--debug)
shift || true
BUILD_CONFIG="debug"
echo "Building in Debug mode"
;;
--configure)
shift || true
RUN_CMAKE="yes"
echo "Running cmake: true"
;;
--no-build)
shift || true
RUN_BUILD="no"
echo "Running build: no"
;;
--run-tests)
RUN_TEST="all"
shift || true
echo "Running all tests"
;;
--run-tests=*)
RUN_TEST=${1#*=}
shift || true
echo "Running test ${RUN_TEST}"
;;
--run-integration-tests)
INTEGRETION_TEST="yes"
shift || true
echo "Running integration tests"
;;
--test-errors-stdout)
DUMP_TEST_ERRORS_STDOUT="yes"
shift || true
echo "Write test errors to stdout on failure"
;;
--use-system-modules)
CMAKE_EXTRA_ARGS="${CMAKE_EXTRA_ARGS} -DWITH_SUBMODULES_SYSTEM=ON"
shift || true
echo "Using extra cmake arguments: ${CMAKE_EXTRA_ARGS}"
;;
--asan)
CMAKE_EXTRA_ARGS="${CMAKE_EXTRA_ARGS} -DASAN_BUILD=ON"
ASAN_BUILD="yes"
shift || true
echo "Using extra cmake arguments: ${CMAKE_EXTRA_ARGS}"
;;
--verbose|-v)
shift || true
VERBOSE_ARGS="-v"
echo "Verbose build: true"
;;
--help|-h)
print_usage
exit 0
;;
*)
print_usage
exit 1
;;
esac
done
function configure() {
printf "${BOLD_PINK}Running cmake...${RESET}\n"
mkdir -p ${BUILD_DIR}
cd $_
local BUILD_TYPE=$(echo ${BUILD_CONFIG^})
rm -f CMakeCache.txt
printf "Running: cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DBUILD_TESTS=ON -Wno-dev -GNinja ${CMAKE_EXTRA_ARGS}\n"
cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DBUILD_TESTS=ON -Wno-dev -GNinja ${CMAKE_EXTRA_ARGS}
cd ${ROOT_DIR}
}
function build() {
printf "${BOLD_PINK}Building${RESET}\n"
if [ -d ${BUILD_DIR} ]; then
cd ${BUILD_DIR}
${NINJA_TOOL} ${VERBOSE_ARGS} ${CMAKE_TARGET}
cd ${ROOT_DIR}
printf "\n${GREEN}Build Successful!${RESET}\n\n"
printf "${BOLD_PINK}Module path:${RESET} ${BUILD_DIR}/libsearch.so\n\n"
if [ -z "${RUN_TEST}" ]; then
printf "You may want to run the unit tests by executing:\n"
printf " ./build.sh ${ARGV} --run-tests\n\n"
fi
printf "To load the module, execute the following command:\n"
printf " valkey-server --loadmodule ${BUILD_DIR}/libsearch.so\n\n"
fi
}
function print_test_prefix() {
printf "${BOLD_PINK}Running:${RESET} $1"
}
function print_test_ok() {
printf " ... ${GREEN}ok${RESET}\n"
}
function print_test_summary() {
printf "${BLUE}Test output can be found here:${RESET} ${TEST_OUTPUT_FILE}\n"
}
function print_test_error_and_exit() {
printf " ... ${RED}failed${RESET}\n"
if [[ "${DUMP_TEST_ERRORS_STDOUT}" == "yes" ]]; then
cat ${TEST_OUTPUT_FILE}
fi
# When running tests with ASan enabled, do not terminate the execution after the first failure continue
# running the remainder of the tests
if [[ "${ASAN_BUILD}" == "no" ]]; then
print_test_summary
exit 1
fi
}
function check_tool() {
local tool_name=$1
local message=$2
printf "Checking for ${tool_name}..."
command -v ${tool_name} > /dev/null || \
(printf "${RED}failed${RESET}.\n${RED}ERROR${RESET} - could not locate tool '${tool_name}'. ${message}\n" && exit 1)
printf "${GREEN}ok${RESET}\n"
}
function check_tools() {
local tools="cmake g++ gcc"
for tool in $tools; do
check_tool ${tool}
done
# Check for ninja. On RedHat based Linux, it is called ninja-build, while on Debian based Linux, it is simply ninja
# Ubuntu / Mint et al will report "ID_LIKE=debian"
local debian_output=$(cat /etc/*-release|grep -i debian|wc -l)
if [ ${debian_output} -gt 0 ]; then
NINJA_TOOL="ninja"
else
NINJA_TOOL="ninja-build"
fi
check_tool ${NINJA_TOOL}
}
# If any of the CMake files is newer than our "build.ninja" file, force "cmake" before building
function is_configure_required() {
local ninja_build_file=${BUILD_DIR}/build.ninja
if [[ "${RUN_CMAKE}" == "yes" ]]; then
# User asked for configure
echo "yes"
return
fi
if [ ! -f ${ninja_build_file} ] || [ ! -f ${BUILD_DIR}/CMakeCache.txt ]; then
# No ninja build file
echo "yes"
return
fi
local build_file_lastmodified=$(stat --printf "%Y" ${ninja_build_file})
local cmake_files=$(find ${ROOT_DIR} -name "CMakeLists.txt" -o -name "*.cmake"| grep -v ".build-release" | grep -v ".build-debug")
for cmake_file in ${cmake_files}; do
local cmake_file_modified=$(stat --printf "%Y" ${cmake_file})
if [ ${cmake_file_modified} -gt ${build_file_lastmodified} ]; then
echo "yes"
return
fi
done
echo "no"
}
cleanup() {
cd ${ROOT_DIR}
}
# Ensure cleanup runs on exit
trap cleanup EXIT
BUILD_DIR=${ROOT_DIR}/.build-${BUILD_CONFIG}
if [[ "${ASAN_BUILD}" == "yes" ]]; then
printf "${BOLD_PINK}ASAN build is enabled${RESET}\n"
BUILD_DIR=${BUILD_DIR}-asan
fi
TESTS_DIR=${BUILD_DIR}/tests
TEST_OUTPUT_FILE=${BUILD_DIR}/tests.out
printf "Checking if configure is required..."
FORCE_CMAKE=$(is_configure_required)
printf "${GREEN}${FORCE_CMAKE}${RESET}\n"
check_tools
START_TIME=`date +%s`
if [[ "${RUN_CMAKE}" == "yes" ]] || [[ "${FORCE_CMAKE}" == "yes" ]]; then
configure
fi
if [[ "${RUN_BUILD}" == "yes" ]]; then
build
fi
END_TIME=`date +%s`
BUILD_RUNTIME=$((END_TIME - START_TIME))
START_TIME=`date +%s`
if [[ "${ASAN_BUILD}" == "yes" ]]; then
export ASAN_OPTIONS="detect_odr_violation=0"
fi
if [[ "${RUN_TEST}" == "all" ]]; then
rm -f ${TEST_OUTPUT_FILE}
TESTS=$(ls ${TESTS_DIR}/*_test)
for test in $TESTS; do
echo "==> Running executable: ${test}" >> ${TEST_OUTPUT_FILE}
echo "" >> ${TEST_OUTPUT_FILE}
print_test_prefix "${test}"
(${test} >> ${TEST_OUTPUT_FILE} 2>&1 && print_test_ok) || print_test_error_and_exit
done
print_test_summary
elif [ ! -z "${RUN_TEST}" ]; then
rm -f ${TEST_OUTPUT_FILE}
echo "==> Running executable: ${TESTS_DIR}/${RUN_TEST}" >> ${TEST_OUTPUT_FILE}
echo "" >> ${TEST_OUTPUT_FILE}
print_test_prefix "${TESTS_DIR}/${RUN_TEST}"
(${TESTS_DIR}/${RUN_TEST} && print_test_ok) || print_test_error_and_exit
print_test_summary
elif [[ "${INTEGRETION_TEST}" == "yes" ]]; then
cd testing/integration
params=""
if [[ "${DUMP_TEST_ERRORS_STDOUT}" == "yes" ]]; then
params=" --test-errors-stdout"
fi
if [[ "${BUILD_CONFIG}" == "debug" ]]; then
params="${params} --debug"
fi
./run.sh ${params}
fi
END_TIME=`date +%s`
TEST_RUNTIME=$((END_TIME - START_TIME))