-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.sh
executable file
·289 lines (228 loc) · 7.6 KB
/
compile.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
#!/bin/bash
exec </dev/null
set +x
set -e
set -u
set -o pipefail
trap 'code=$?; set +x; echo "ERROR code $code at line $LINENO" 1>&2; wait || true; exit $code' ERR
set +u
if test -z "$CXX"; then
export CXX="$(which g++)"
export CC="$(which gcc)"
fi
if test -z "$BUILD_TYPE"; then
export BUILD_TYPE="debug"
fi
set -u
case "$BUILD_TYPE" in
debug)
CL3_GEN_NAME="dbg"
;;
release)
CL3_GEN_NAME="rel"
;;
esac
export CL3_ROOT="$(dirname "$(readlink -f "$0")")"
export CL3_GENDIR="$PWD/gen/$CL3_GEN_NAME"
export CL3_WORKDIR="$PWD/tmp/$CL3_GEN_NAME"
export CL3_PJOBS=$(grep "^processor[[:space:]]]*:[[:space:]]*[0-9]\+$" /proc/cpuinfo | wc -l)
export CFLAGS=" -fPIC -rdynamic -g -O0 -march=native"
export CXXFLAGS="-fPIC -rdynamic -g -O0 -march=native"
rm -rf gen tmp
mkdir -p gen/{dbg,rel}/{lib,include,bin} tmp/gtest tmp/musl
export -p > "tmp/env.log"
echo "Building gtest ... "
(
set -x
mkdir -p "tmp/gtest/build"
cd "tmp/gtest/build"
cmake "$CL3_ROOT/util/gtest"
make -j $CL3_PJOBS
) > "tmp/gtest/build.log" 2>&1
# echo "Building musl ... "
# (
# mkdir -p "tmp/musl/build"
# cd "tmp/musl/build"
# CFLAGS="-O3 -fPIC -flto -march=native" "$CL3_ROOT/src/musl/configure" --disable-shared --enable-visibility --enable-debug
# make -j $CL3_PJOBS
# ) > "tmp/musl/build.log" 2>&1
for m in dbg rel; do
mkdir -p gen/$m/include/cl3
for f in core llvm; do
# ln -nfs "$CL3_ROOT/src/$f" "gen/$m/include/cl3/$f"
mkdir -p "tmp/$m/$f/lib" "tmp/$m/$f/tests"
done
ln -nfs "$CL3_ROOT/util/gtest/googletest/include/gtest" "gen/$m/include/gtest"
for f in tmp/gtest/build/lib/*.a; do
ln -nfs "../../../$f" "gen/$m/lib/"
done
done
if ! test -d "data"; then
ln -snf "$CL3_ROOT/data"
fi
function RunValgrind()
{
VALGRIND_OPTS="--quiet --leak-check=full --show-reachable=no --track-origins=yes --num-callers=30 --trace-children=no --track-fds=yes --error-exitcode=1 --suppressions=$CL3_ROOT/util/valgrind.supp --gen-suppressions=all --log-file=valgrind.log"
if ! time valgrind $VALGRIND_OPTS "$@"; then
cat valgrind.log
return 1
fi
}
function DetectInfrabox()
{
if export -p | grep -qF "INFRABOX_CLI=" || export -p | grep -qF "INFRABOX_JOB_ID="; then
echo 1
else
echo 0
fi
}
INFRABOX=$(DetectInfrabox)
if (($INFRABOX)); then
echo "Running inside of Infrabox"
export -p | sort | grep -i "infrabox"
else
echo "Running outside of Infrabox"
fi
LLVM_LDFLAGS="$(llvm-config --ldflags)"
LLVM_CXXFLAGS="$(llvm-config --cppflags) -Wno-unused-parameter"
LLVM_LIBS="$(llvm-config --libs all)"
LLVM_VER="$(llvm-config --version)"
LLVM_SYSLIBS="$(llvm-config --system-libs)"
echo "Using LLVM version: $LLVM_VER"
ln -vsnf "$CL3_GENDIR" "$CL3_ROOT/gen/last-build" || true
OPTS_ARCH="-march=native"
OPTS_BASE="$OPTS_ARCH -fvisibility-inlines-hidden -fvisibility=hidden -std=c++11 -fdata-sections -ffunction-sections -flto -lpthread -Wno-multichar -I $CL3_GENDIR/include -L $CL3_GENDIR/lib"
OPTS_QA="-Wno-unused-parameter -Wno-deprecated-declarations -Wno-unused-function -Wall -Wextra -Werror"
OPTS_DEBUG="-g -O0 -DCL3_DEBUG --coverage -rdynamic"
OPTS_RELEASE=" -O3 -DCL3_RELEASE"
OPTS_LIB="-fPIC -shared"
OPTS_EXE="-fPIE"
case "$BUILD_TYPE" in
debug)
OPTS_LIB="$OPTS_BASE $OPTS_LIB $OPTS_DEBUG"
OPTS_EXE="$OPTS_BASE $OPTS_EXE $OPTS_DEBUG"
;;
release)
OPTS_LIB="$OPTS_BASE $OPTS_LIB $OPTS_RELEASE"
OPTS_EXE="$OPTS_BASE $OPTS_EXE $OPTS_RELEASE"
;;
esac
OPTS_CL3CORELIB="$OPTS_LIB $OPTS_QA -ldl -lm -DINSIDE_CL3CORE"
OPTS_CL3LLVMLIB="$OPTS_LIB $OPTS_QA $LLVM_CXXFLAGS $LLVM_LDFLAGS $LLVM_LIBS $LLVM_SYSLIBS -lcl3-core -DINSIDE_CL3LLVM"
OPTS_CL3CORETESTS="$OPTS_EXE $OPTS_QA -lcl3-core"
OPTS_CL3LLVMTESTS="$OPTS_EXE $OPTS_QA $LLVM_CXXFLAGS $LLVM_LDFLAGS $LLVM_LIBS -lcl3-core -lcl3-llvm"
function GenerateHeaderBundle()
{
dst_dir="$CL3_GENDIR/include/cl3/$1"
mkdir -p "$dst_dir"
cp "$CL3_ROOT/src/$1/"*.hpp "$dst_dir/"
(
cd "$dst_dir"
for file in *.hpp; do
echo "#include \"$1/$file\""
done
) > "$CL3_GENDIR/include/cl3/$1.hpp"
}
function GenerateSourceBundle()
{
dst_dir="$CL3_GENDIR/src/cl3/$1"
mkdir -p "$dst_dir"
cp "$CL3_ROOT/src/$1/"*.cpp "$dst_dir"
for f in "$CL3_ROOT/src/$1/"*.hpp; do
ln -snf "$f" "$dst_dir/"
done
(
cd "$dst_dir"
echo "#ifndef _include_cl3_core_bundle_"
echo "#define _include_cl3_core_bundle_"
echo "#define CL3_SOURCE_BUNDLE"
for file in *.cpp; do
echo "#include \"../../src/cl3/$1/$file\""
done
echo "#endif"
) > "$CL3_GENDIR/include/cl3/$1.cpp"
}
function TestSourceBundle()
{
cd "$CL3_WORKDIR"
time "$CXX" -o "test-source-bundle-$1" "$CL3_ROOT/util/test-source-bundle-$1.cpp" $OPTS_EXE $2
"./test-source-bundle-$1"
}
function GenerateLibAmalgam()
{
for f in "$CL3_GENDIR/src/cl3/$1/"*.cpp; do
# f="$(readlink -f "$f")"
echo "#include \"$f\""
done > "$CL3_WORKDIR/$1-lib.cpp"
}
function GenerateTestAmalgam()
{
for f in "$CL3_ROOT/src/$1/test/"*.cpp; do
# f="$(readlink -f "$f")"
echo "#include \"$f\""
done > "$CL3_WORKDIR/$1-test.cpp"
}
GenerateSourceBundle core
GenerateSourceBundle llvm
GenerateHeaderBundle core
GenerateHeaderBundle llvm
TestSourceBundle core "-ldl -lm" &
# TestSourceBundle llvm "-ldl -lm $LLVM_CXXFLAGS $LLVM_LDFLAGS $LLVM_LIBS" &
GenerateLibAmalgam core
GenerateLibAmalgam llvm
GenerateTestAmalgam core
GenerateTestAmalgam llvm
make -C "$CL3_ROOT/examples/how-to-use-source-bundle" clean
time make -C "$CL3_ROOT/examples/how-to-use-source-bundle" example & p3=$!
cd "$CL3_WORKDIR/core/lib"
time "$CXX" -o "$CL3_GENDIR/lib/libcl3-core.so" "$CL3_WORKDIR/core-lib.cpp" $OPTS_CL3CORELIB & p1=$! # "$CL3_ROOT/tmp/musl/build/lib/libc.a"
time "$CXX" -o "$CL3_GENDIR/lib/libcl3-core_noamalgam.so" "$CL3_GENDIR/src/cl3/core/"*.cpp $OPTS_CL3CORELIB &
wait $p1
cd "$CL3_WORKDIR/core/tests"
time "$CXX" -o "$CL3_GENDIR/bin/cl3-core-tests" "$CL3_WORKDIR/core-test.cpp" "$CL3_GENDIR/lib/libgtest"*.a $OPTS_CL3CORETESTS & p2=$!
cd "$CL3_WORKDIR/llvm/lib"
time "$CXX" -o "$CL3_GENDIR/lib/libcl3-llvm.so" "$CL3_WORKDIR/llvm-lib.cpp" $OPTS_CL3LLVMLIB & p1=$!
time "$CXX" -o "$CL3_GENDIR/lib/libcl3-llvm_noamalgam.so" "$CL3_GENDIR/src/cl3/llvm/"*.cpp $OPTS_CL3LLVMLIB &
wait $p1
cd "$CL3_WORKDIR/llvm/tests"
time "$CXX" -o "$CL3_GENDIR/bin/cl3-llvm-tests" "$CL3_WORKDIR/llvm-test.cpp" "$CL3_GENDIR/lib/libgtest"*.a $OPTS_CL3LLVMTESTS & p1=$!
cd "$CL3_WORKDIR"
export LD_LIBRARY_PATH="$CL3_GENDIR/lib"
wait $p2
RunValgrind "$CL3_GENDIR/bin/cl3-core-tests" '--gtest_filter=-system_memory*' --gtest_output=xml:gtest.xml dummy1 dummy2 dummy3
time "$CL3_GENDIR/bin/cl3-core-tests" '--gtest_filter=system_memory*' --gtest_output=xml:gtest.xml dummy1 dummy2 dummy3
wait $p1
wait $p3
RunValgrind "$CL3_GENDIR/bin/cl3-llvm-tests" --gtest_output=xml:gtest.xml
time make -C "$CL3_ROOT/examples/how-to-use-source-bundle" run
if test "$BUILD_TYPE" == "debug" && ((INFRABOX==0)); then
(
set -x
mkdir -p "$CL3_GENDIR/coverage"
lcov --capture --rc lcov_branch_coverage=1 --rc geninfo_auto_base=1 \
--directory "$CL3_WORKDIR/core/lib" --directory "$CL3_WORKDIR/core/tests" \
--directory "$CL3_WORKDIR/llvm/lib" --directory "$CL3_WORKDIR/llvm/tests" \
--output-file coverage.info
lcov --remove coverage.info '*gtest*' '/usr/include/*' '*/extern_*' '*/test/*' -o coverage.info
genhtml coverage.info --output-directory "$CL3_GENDIR/coverage"
) > "$CL3_WORKDIR/lcov.log" 2>&1
echo "coverage URL: $(readlink -f "$CL3_GENDIR/coverage/src/cl3/core/index-sort-l.html")"
fi
if ((${BASH_VERSINFO[0]} > 4 || (${BASH_VERSINFO[0]} == 4 && ${BASH_VERSINFO[1]} >= 3))); then
trap - ERR
set +e
while true; do
wait -n
code=$?
if(($code == 127)); then
break;
elif(($code != 0)); then
echo "ERROR: background task failed with code $code" 1>&2
exit $code
fi
done
set -e
else
wait
fi
echo "Build successful"