Add clang format & skip build if no source file modified (#5433)
[oweals/minetest.git] / util / travis / script.sh
1 #!/bin/bash -e
2 . util/travis/common.sh
3
4 needs_compile || exit 0
5
6 function perform_lint() {
7         CLANG_FORMAT=clang-format-3.9
8         if [ "$TRAVIS_EVENT_TYPE" = "pull_request" ]; then
9                 # Get list of every file modified in this pull request
10                 files_to_lint="$(git diff --name-only --diff-filter=ACMRTUXB $TRAVIS_COMMIT_RANGE | grep '^src/[^.]*[.]\(cpp\|h\)$' | egrep -v '^src/(gmp|lua|jsoncpp)/' || true)"
11         else
12                 # Check everything for branch pushes
13                 files_to_lint="$(find src/ -name '*.cpp' -or -name '*.h' | egrep -v '^src/(gmp|lua|jsoncpp)/')"
14         fi
15
16         local fail=0
17         for f in ${files_to_lint}; do
18                 d=$(diff -u "$f" <(${CLANG_FORMAT} "$f") || true)
19                 if ! [ -z "$d" ]; then
20                         printf "The file %s is not compliant with the coding style:\n%s\n" "$f" "$d"
21                         # Disable build failure at this moment as we need to have a complete MT source whitelist to check
22                         fail=0
23                 fi
24         done
25
26         if [ "$fail" = 1 ]; then
27                 exit 1
28         fi
29
30         exit 0
31 }
32
33 if [[ "$LINT" == "1" ]]; then
34         # Lint with exit CI
35         perform_lint
36 fi
37
38 if [[ $PLATFORM == "Unix" ]]; then
39         mkdir -p travisbuild
40         cd travisbuild || exit 1
41
42         CMAKE_FLAGS=''
43         if [[ $COMPILER == "g++-6" ]]; then
44                 export CC=gcc-6
45                 export CXX=g++-6
46         fi
47
48         # Clang builds with FreeType fail on Travis
49         if [[ $CC == "clang" ]]; then
50                 CMAKE_FLAGS+=' -DENABLE_FREETYPE=FALSE'
51         fi
52
53         if [[ $TRAVIS_OS_NAME == "osx" ]]; then
54                 CMAKE_FLAGS+=' -DCUSTOM_GETTEXT_PATH=/usr/local/opt/gettext'
55         fi
56
57         cmake -DCMAKE_BUILD_TYPE=Debug \
58                 -DRUN_IN_PLACE=TRUE \
59                 -DENABLE_GETTEXT=TRUE \
60                 -DBUILD_SERVER=TRUE \
61                 $CMAKE_FLAGS ..
62         make -j2
63
64         echo "Running unit tests."
65         CMD="../bin/minetest --run-unittests"
66         if [[ "$VALGRIND" == "1" ]]; then
67                 valgrind --leak-check=full --leak-check-heuristics=all --undef-value-errors=no --error-exitcode=9 ${CMD} && exit 0
68         else
69                 ${CMD} && exit 0
70         fi
71
72 elif [[ $PLATFORM == Win* ]]; then
73         [[ $CC == "clang" ]] && exit 1 # Not supposed to happen
74         # We need to have our build directory outside of the minetest directory because
75         #  CMake will otherwise get very very confused with symlinks and complain that
76         #  something is not a subdirectory of something even if it actually is.
77         # e.g.:
78         # /home/travis/minetest/minetest/travisbuild/minetest
79         # \/  \/  \/
80         # /home/travis/minetest/minetest/travisbuild/minetest/travisbuild/minetest
81         # \/  \/  \/
82         # /home/travis/minetest/minetest/travisbuild/minetest/travisbuild/minetest/travisbuild/minetest
83         # You get the idea.
84         OLDDIR=$(pwd)
85         cd ..
86         export EXISTING_MINETEST_DIR=$OLDDIR
87         export NO_MINETEST_GAME=1
88         if [[ $PLATFORM == "Win32" ]]; then
89                 "$OLDDIR/util/buildbot/buildwin32.sh" travisbuild && exit 0
90         elif [[ $PLATFORM == "Win64" ]]; then
91                 "$OLDDIR/util/buildbot/buildwin64.sh" travisbuild && exit 0
92         fi
93 else
94         echo "Unknown platform \"${PLATFORM}\"."
95         exit 1
96 fi
97