summaryrefslogtreecommitdiff
path: root/build_and_test.sh
blob: ca4e143e341ff340e67daf771381485dd1ed74bc (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
329
330
331
332
333
334
#!/bin/sh

BUILD_TYPE=
USE_COMPILER=
DESTINATION=
EXTRA_OPTIONS=
EXTRA_LINK_OPTIONS=

while [ $# -gt 0 ]; do
  case $1 in
    -H|--help)
      echo "Build script for C projects"
      echo ""
      echo "Usage:  ./build_and_test.sh [OPTIONS]"
      echo ""
      echo "  -H  --help          - Print this info"
      echo "  -t  --type          - Set build type:       debug, release"
      echo "  -c  --compiler      - Set compiler to use:  gcc, clang, msvc, emcc"
      echo "  -d  --destination   - Set destination path"
      echo "  -o  --options       - Set additional options for the compiler"
      echo "  -l  --link          - Set additional options for the linker"
      echo ""
      exit 0
      ;;
    -t|--type)
      BUILD_TYPE=$2
      shift
      shift
      ;;
    -c|--compiler)
      USE_COMPILER="$2"
      shift
      shift
      ;;
    -d|--destination)
      DESTINATION="$2"
      shift
      shift
      ;;
    -o|--options)
      EXTRA_OPTIONS="$2"
      shift
      shift
      ;;
    -l|--link)
      EXTRA_LINK_OPTIONS="$2"
      shift
      shift
      ;;
    *)
      echo "Unknown option $1"
      shift
      ;;
  esac
done

FOLDER=out_gcc
OS=Linux
COMPILE=gcc
COMPILEPP=g++
OBJ_POSTFIX=.o
EXE_POSTFIX=
FLAGS=
FLAG_OBJ="-c -o "
FLAG_EXE="-o "
LINK_FLAGS=

if    [ "$USE_COMPILER" = "gcc" ]; then
  FOLDER=out_gcc
  COMPILE=gcc
  COMPILEPP=g++
elif  [ "$USE_COMPILER" = "clang" ]; then
  FOLDER=out_clang
  COMPILE=clang
  COMPILEPP=clang++
elif  [ "$USE_COMPILER" = "msvc" ]; then
  FOLDER=out_msvc
  COMPILE=cl.exe
  COMPILEPP=cl.exe
  OBJ_POSTFIX=.obj
  FLAG_OBJ="-c -Fo"
  FLAG_EXE="-Fe"
elif  [ "$USE_COMPILER" = "emcc" ]; then
  FOLDER=out_emcc
  COMPILE=emcc
  COMPILEPP=em++
  EXE_POSTFIX=.js
  LINK_FLAGS="-sFULL_ES3=1"
elif  [ "$USE_COMPILER" != "" ]; then
  echo "Unknown C compiler"
  exit 1
fi

if [ "$DESTINATION" != "" ]; then
  FOLDER="$DESTINATION"
fi

if [ "$USE_COMPILER" != "" ]; then
  if command -v $COMPILE >/dev/null 2>&1; then
    echo "C compiler found"
  else
    echo "C compiler not found"
    exit 1
  fi
fi

if [ "$COMPILE" != "emcc" ]; then
  case $(uname | tr '[:upper:]' '[:lower:]') in
    *darwin*)
      OS=macOS
      if [ "$USE_COMPILER" = "" ]; then
        if    command -v clang >/dev/null 2>&1; then
          echo "C compiler found - Clang"
          COMPILE=clang
          COMPILEPP=clang++
        elif  command -v gcc >/dev/null 2>&1; then
          echo "C compiler found - GCC"
        else
          echo "C compiler not found"
          exit 1
        fi
      fi
      ;;
    *msys*|*cygwin*|*mingw*|*nt*|*win*)
      OS=Windows
      EXE_POSTFIX=.exe
      if [ "$USE_COMPILER" = "" ]; then
        if    command -v cl.exe >/dev/null 2>&1; then
          echo "C compiler found - MSVC"
          COMPILE=cl.exe
          COMPILEPP=cl.exe
          OBJ_POSTFIX=.obj
          FLAG_OBJ="-c -Fo"
          FLAG_EXE="-Fe"
        elif  command -v gcc >/dev/null 2>&1; then
          echo "C compiler found - GCC"
        elif  command -v clang >/dev/null 2>&1; then
          echo "C compiler found - Clang"
          COMPILE=clang
          COMPILEPP=clang++
        else
          echo "C compiler not found"
          exit 1
        fi
      fi
      if [ "$COMPILE" = "cl.exe" ]; then
        LINK_FLAGS="Shlwapi.lib Advapi32.lib"
      else
        LINK_FLAGS="-lShlwapi -lAdvapi32"
      fi
      ;;
    *)
      if [ "$USE_COMPILER" = "" ]; then
        if    command -v gcc >/dev/null 2>&1; then
          echo "C compiler found - GCC"
        elif  command -v clang >/dev/null 2>&1; then
          echo "C compiler found - Clang"
          COMPILE=clang
          COMPILEPP=clang++
        else
          echo "C compiler not found"
          exit 1
        fi
      fi
      ;;
  esac
fi

if [ "$COMPILE" = "gcc" ] || [ "$COMPILE" = "emcc" ]; then
  if    [ "$BUILD_TYPE" = "release" ]; then
    FLAGS="-O3 -DNDEBUG"
  elif  [ "$COMPILE" = "gcc" ] && [ "$OS" != "Windows" ]; then
    FLAGS="-O0 -fsanitize=undefined,address,leak"
  elif  [ "$OS" != "Windows" ] && [ "$COMPILE" != "emcc" ]; then
    FLAGS="-O0 -fsanitize=undefined,address"
  else
    FLAGS="-O0"
  fi
elif [ "$COMPILE" = "clang" ]; then
  if    [ "$BUILD_TYPE" = "release" ]; then
    FLAGS="-O3 -DNDEBUG"
  else
    FLAGS="-O0"
  fi
else
  if [ "$BUILD_TYPE" = "release" ]; then
    FLAGS="-O2 -DNDEBUG"
  else
    FLAGS="-Od"
  fi
fi

if [ "$COMPILE" = "clang" ] || [ "$OS" = "macOS" ]; then
  FLAGS="-fblocks $FLAGS"
  LINK_FLAGS="-lBlocksRuntime $LINK_FLAGS"
fi

if [ "$EXTRA_OPTIONS" != "" ]; then
  FLAGS="$EXTRA_OPTIONS $FLAGS"
fi

if [ "$EXTRA_LINK_OPTIONS" != "" ]; then
  LINK_FLAGS="$EXTRA_LINK_OPTIONS $LINK_FLAGS"
fi

if [ ! -d "$FOLDER" ]; then
  mkdir "$FOLDER"
fi

echo ""
echo "Compiler options: $FLAGS"
echo "Link options:     $LINK_FLAGS"
echo ""

echo "Build kit"
$COMPILE $FLAGS \
  ${FLAG_OBJ}"$FOLDER/kit$OBJ_POSTFIX" \
  "source/kit/_lib.c"
if [ $? -ne 0 ]; then
  exit 1
fi

if [ "$COMPILE" = "emcc" ]; then
  exit 0
fi

echo "Build test suite"
$COMPILE $FLAGS \
  $FLAG_EXE"$FOLDER/test_suite$EXE_POSTFIX" \
  "$FOLDER/kit$OBJ_POSTFIX" \
  "source/tests/_exe.c" \
  $LINK_FLAGS
if [ $? -ne 0 ]; then
  exit 1
fi

$COMPILE $FLAGS \
  $FLAG_EXE"$FOLDER/test_too_many_assertions$EXE_POSTFIX" \
  "$FOLDER/kit$OBJ_POSTFIX" \
  "source/tests/test_too_many_assertions.c" \
  $LINK_FLAGS
if [ $? -ne 0 ]; then
  exit 1
fi

$COMPILE $FLAGS \
  $FLAG_EXE"$FOLDER/test_too_many_tests$EXE_POSTFIX" \
  "$FOLDER/kit$OBJ_POSTFIX" \
  "source/tests/test_too_many_tests.c" \
  $LINK_FLAGS
if [ $? -ne 0 ]; then
  exit 1
fi

$COMPILEPP $FLAGS \
  $FLAG_EXE"$FOLDER/test_cpp$EXE_POSTFIX" \
  "$FOLDER/kit$OBJ_POSTFIX" \
  "source/tests/test_cpp.cpp" \
  $LINK_FLAGS
if [ $? -ne 0 ]; then
  exit 1
fi

$COMPILEPP $FLAGS \
  $FLAG_EXE"$FOLDER/test_signals$EXE_POSTFIX" \
  "$FOLDER/kit$OBJ_POSTFIX" \
  "source/tests/test_signals.cpp" \
  $LINK_FLAGS
if [ $? -ne 0 ]; then
  exit 1
fi

$COMPILE $FLAGS \
  $FLAG_EXE"$FOLDER/test_interprocess$EXE_POSTFIX" \
  "$FOLDER/kit$OBJ_POSTFIX" \
  "source/tests/test_interprocess.c" \
  $LINK_FLAGS
if [ $? -ne 0 ]; then
  exit 1
fi

echo "Run tests"
echo ""

STATUS=0

$FOLDER/test_suite
if [ $? -ne 0 ]; then
  STATUS=1
fi

$FOLDER/test_too_many_assertions --quiet
if [ $? -eq 0 ]; then
  echo "too many assertions - OK"
else
  echo "too many assertions - FAILED (code $?)"
  STATUS=1
fi

$FOLDER/test_too_many_tests --quiet
if [ $? -eq 0 ]; then
  echo "too many tests      - OK"
else
  echo "too many tests      - FAILED (code $?)"
  STATUS=1
fi

$FOLDER/test_cpp --quiet
if [ $? -eq 0 ]; then
  echo "cpp                 - OK"
else
  echo "cpp                 - FAILED (code $?)"
  STATUS=1
fi

$FOLDER/test_signals --quiet
if [ $? -eq 0 ]; then
  echo "signals             - OK"
else
  echo "signals             - FAILED (code $?)"
  STATUS=1
fi

$FOLDER/test_interprocess clean
$FOLDER/test_interprocess reader &
$FOLDER/test_interprocess writer
if [ $? -eq 0 ]; then
  echo "interprocess        - OK"
else
  echo "interprocess        - FAILED (code $?)"
  STATUS=1
fi

exit $STATUS