export TRAVIS_OS_NAME
[openjpeg.git] / tools / travis-ci / run.sh
1 #!/bin/bash
2
3 # This script executes the script step when running under travis-ci
4
5 #if cygwin, check path
6 case ${MACHTYPE} in
7         *cygwin*) OPJ_CI_IS_CYGWIN=1;;
8         *) ;;
9 esac
10
11 if [ "${OPJ_CI_IS_CYGWIN:-}" == "1" ]; then
12         # Hack for appveyor
13         if ! which wget; then
14                 # PATH is not yet set up
15                 export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
16         fi
17 fi
18
19 # Set-up some bash options
20 set -o nounset   ## set -u : exit the script if you try to use an uninitialised variable
21 set -o errexit   ## set -e : exit the script if any statement returns a non-true return value
22 set -o pipefail  ## Fail on error in pipe
23
24 function opjpath ()
25 {
26         if [ "${OPJ_CI_IS_CYGWIN:-}" == "1" ]; then
27                 cygpath $1 "$2"
28         else
29                 echo "$2"
30         fi
31 }
32
33 # ABI check is done by abi-check.sh
34 if [ "${OPJ_CI_ABI_CHECK:-}" == "1" ]; then
35         exit 0
36 fi
37
38 # Set-up some variables
39 if [ "${OPJ_CI_BUILD_CONFIGURATION:-}" == "" ]; then
40         export OPJ_CI_BUILD_CONFIGURATION=Release #default
41 fi
42 OPJ_SOURCE_DIR=$(cd $(dirname $0)/../.. && pwd)
43
44 if [ "${OPJ_DO_SUBMIT:-}" == "" ]; then
45         OPJ_DO_SUBMIT=0 # Do not flood cdash by default
46 fi
47 if [ "${TRAVIS_REPO_SLUG:-}" != "" ]; then
48         OPJ_OWNER=$(echo "${TRAVIS_REPO_SLUG}" | sed 's/\(^.*\)\/.*/\1/')
49         OPJ_SITE="${OPJ_OWNER}.travis-ci.org"
50         if [ "${OPJ_OWNER}" == "uclouvain" ]; then
51                 OPJ_DO_SUBMIT=1
52         fi
53 else
54         OPJ_SITE="$(hostname)"
55 fi
56
57 if [ "${TRAVIS_OS_NAME:-}" == "" ]; then
58   # Let's guess OS for testing purposes
59         echo "Guessing OS"
60         if uname -s | grep -i Darwin &> /dev/null; then
61                 TRAVIS_OS_NAME=osx
62         elif uname -s | grep -i Linux &> /dev/null; then
63                 TRAVIS_OS_NAME=linux
64                 if [ "${CC:-}" == "" ]; then
65                         # default to gcc
66                         export CC=gcc
67                 fi
68         elif uname -s | grep -i CYGWIN &> /dev/null; then
69                 TRAVIS_OS_NAME=windows
70         elif uname -s | grep -i MINGW32 &> /dev/null; then
71                 TRAVIS_OS_NAME=windows
72         else
73                 echo "Failed to guess OS"; exit 1
74         fi
75         echo "${TRAVIS_OS_NAME}"
76 fi
77
78 if [ "${TRAVIS_OS_NAME}" == "osx" ]; then
79         OPJ_OS_NAME=$(sw_vers -productName | tr -d ' ')$(sw_vers -productVersion | sed 's/\([^0-9]*\.[0-9]*\).*/\1/')
80         OPJ_CC_VERSION=$(xcodebuild -version | grep -i xcode)
81         OPJ_CC_VERSION=xcode${OPJ_CC_VERSION:6}
82 elif [ "${TRAVIS_OS_NAME}" == "linux" ]; then
83         OPJ_OS_NAME=linux
84         if which lsb_release > /dev/null; then
85                 OPJ_OS_NAME=$(lsb_release -si)$(lsb_release -sr | sed 's/\([^0-9]*\.[0-9]*\).*/\1/')
86         fi
87         if [ -z "${CC##*gcc*}" ]; then
88                 OPJ_CC_VERSION=$(${CC} --version | head -1 | sed 's/.*\ \([0-9.]*[0-9]\)/\1/')
89                 if [ -z "${CC##*mingw*}" ]; then
90                         OPJ_CC_VERSION=mingw${OPJ_CC_VERSION}
91                         # disable testing for now
92                         export OPJ_CI_SKIP_TESTS=1
93                 else
94                         OPJ_CC_VERSION=gcc${OPJ_CC_VERSION}
95                 fi
96         elif [ -z "${CC##*clang*}" ]; then
97                 OPJ_CC_VERSION=clang$(${CC} --version | grep version | sed 's/.*version \([^0-9.]*[0-9.]*\).*/\1/')
98         else
99                 echo "Compiler not supported: ${CC}"; exit 1
100         fi
101 elif [ "${TRAVIS_OS_NAME}" == "windows" ]; then
102         OPJ_OS_NAME=windows
103         OPJ_CC_VERSION=vs2015
104 else
105         echo "OS not supported: ${TRAVIS_OS_NAME}"; exit 1
106 fi
107
108 if [ "${OPJ_CI_ARCH:-}" == "" ]; then
109         echo "Guessing build architecture"
110         MACHINE_ARCH=$(uname -m)
111         if [ "${MACHINE_ARCH}" == "x86_64" ]; then
112                 export OPJ_CI_ARCH=x86_64
113         fi
114         echo "${OPJ_CI_ARCH}"
115 fi
116
117 if [ "${TRAVIS_BRANCH:-}" == "" ]; then
118         if [ "${APPVEYOR_REPO_BRANCH:-}" != "" ]; then
119                 TRAVIS_BRANCH=${APPVEYOR_REPO_BRANCH}
120         else
121                 echo "Guessing branch"
122                 TRAVIS_BRANCH=$(git -C ${OPJ_SOURCE_DIR} branch | grep '*' | tr -d '*[[:blank:]]')
123         fi
124 fi
125
126 OPJ_BUILDNAME=${OPJ_OS_NAME}-${OPJ_CC_VERSION}-${OPJ_CI_ARCH}-${TRAVIS_BRANCH}
127 OPJ_BUILDNAME_TEST=${OPJ_OS_NAME}-${OPJ_CC_VERSION}-${OPJ_CI_ARCH}
128 if [ "${TRAVIS_PULL_REQUEST:-}" != "false" ] && [ "${TRAVIS_PULL_REQUEST:-}" != "" ]; then
129         OPJ_BUILDNAME=${OPJ_BUILDNAME}-pr${TRAVIS_PULL_REQUEST}
130 elif [ "${APPVEYOR_PULL_REQUEST_NUMBER:-}" != "" ]; then
131         OPJ_BUILDNAME=${OPJ_BUILDNAME}-pr${APPVEYOR_PULL_REQUEST_NUMBER}
132 fi
133 OPJ_BUILDNAME=${OPJ_BUILDNAME}-${OPJ_CI_BUILD_CONFIGURATION}-3rdP
134 OPJ_BUILDNAME_TEST=${OPJ_BUILDNAME_TEST}-${OPJ_CI_BUILD_CONFIGURATION}-3rdP
135 if [ "${OPJ_CI_ASAN:-}" == "1" ]; then
136         OPJ_BUILDNAME=${OPJ_BUILDNAME}-ASan
137         OPJ_BUILDNAME_TEST=${OPJ_BUILDNAME_TEST}-ASan
138 fi
139
140 if [ "${OPJ_NONCOMMERCIAL:-}" == "1" ] && [ "${OPJ_CI_SKIP_TESTS:-}" != "1" ] && [ -d kdu ]; then
141         echo "
142 Testing will use Kakadu trial binaries. Here's the copyright notice from kakadu:
143 Copyright is owned by NewSouth Innovations Pty Limited, commercial arm of the UNSW Australia in Sydney.
144 You are free to trial these executables and even to re-distribute them,
145 so long as such use or re-distribution is accompanied with this copyright notice and is not for commercial gain.
146 Note: Binaries can only be used for non-commercial purposes.
147 "
148 fi
149
150 if [ -d cmake-install ]; then
151         export PATH=${PWD}/cmake-install/bin:${PATH}
152 fi
153
154 set -x
155 # This will print configuration
156 # travis-ci doesn't dump cmake version in system info, let's print it 
157 cmake --version
158
159 export TRAVIS_OS_NAME=${TRAVIS_OS_NAME}
160 export OPJ_SITE=${OPJ_SITE}
161 export OPJ_BUILDNAME=${OPJ_BUILDNAME}
162 export OPJ_SOURCE_DIR=$(opjpath -m ${OPJ_SOURCE_DIR})
163 export OPJ_BINARY_DIR=$(opjpath -m ${PWD}/build)
164 export OPJ_BUILD_CONFIGURATION=${OPJ_CI_BUILD_CONFIGURATION}
165 export OPJ_DO_SUBMIT=${OPJ_DO_SUBMIT}
166
167 ctest -S ${OPJ_SOURCE_DIR}/tools/ctest_scripts/travis-ci.cmake -VV || true
168 # ctest will exit with various error codes depending on version.
169 # ignore ctest exit code & parse this ourselves
170 #set +x
171
172 # let's parse configure/build/tests for failure
173
174 echo "
175 Parsing logs for failures
176 "
177 OPJ_CI_RESULT=0
178 ls -l .
179 ls -l build
180 ls -l build/Testing
181
182 # 1st configure step
183 OPJ_CONFIGURE_XML=$(find build -path 'build/Testing/*' -name 'Configure.xml')
184 if [ ! -f "${OPJ_CONFIGURE_XML}" ]; then
185         echo "No configure log found"
186         OPJ_CI_RESULT=1
187 else
188         if ! grep '<ConfigureStatus>0</ConfigureStatus>' ${OPJ_CONFIGURE_XML} &> /dev/null; then
189                 echo "Errors were found in configure log"
190                 OPJ_CI_RESULT=1
191         fi
192 fi
193
194 # 2nd build step
195 # We must have one Build.xml file
196 OPJ_BUILD_XML=$(find build -path 'build/Testing/*' -name 'Build.xml')
197 if [ ! -f "${OPJ_BUILD_XML}" ]; then
198         echo "No build log found"
199         OPJ_CI_RESULT=1
200 else
201         if grep '<Error>' ${OPJ_BUILD_XML} &> /dev/null; then
202                 echo "Errors were found in build log"
203                 OPJ_CI_RESULT=1
204         fi
205 fi
206
207 if [ ${OPJ_CI_RESULT} -ne 0 ]; then
208         # Don't trash output with failing tests when there are configure/build errors
209         exit ${OPJ_CI_RESULT}
210 fi
211
212 if [ "${OPJ_CI_SKIP_TESTS:-}" != "1" ]; then
213         OPJ_TEST_XML=$(find build -path 'build/Testing/*' -name 'Test.xml')
214         if [ ! -f "${OPJ_TEST_XML}" ]; then
215                 echo "No test log found"
216                 OPJ_CI_RESULT=1
217         else
218                 echo "Parsing tests for new/unknown failures"
219                 # 3rd test step
220                 OPJ_FAILEDTEST_LOG=$(find build -path 'build/Testing/Temporary/*' -name 'LastTestsFailed_*.log')
221                 if [ -f "${OPJ_FAILEDTEST_LOG}" ]; then
222                         awk -F: '{ print $2 }' ${OPJ_FAILEDTEST_LOG} > failures.txt
223                         while read FAILEDTEST; do
224                                 # Start with common errors
225                                 if grep -x "${FAILEDTEST}" $(opjpath -u ${OPJ_SOURCE_DIR})/tools/travis-ci/knownfailures-all.txt > /dev/null; then
226                                         continue
227                                 fi
228                                 if [ -f $(opjpath -u ${OPJ_SOURCE_DIR})/tools/travis-ci/knownfailures-${OPJ_BUILDNAME_TEST}.txt ]; then
229                                         if grep -x "${FAILEDTEST}" $(opjpath -u ${OPJ_SOURCE_DIR})/tools/travis-ci/knownfailures-${OPJ_BUILDNAME_TEST}.txt > /dev/null; then
230                                                 continue
231                                         fi
232                                 fi
233                                 echo "${FAILEDTEST}"
234                                 OPJ_CI_RESULT=1
235                         done < failures.txt
236                 fi
237         fi
238         
239         if [ ${OPJ_CI_RESULT} -eq 0 ]; then
240                 echo "No new/unknown test failure found
241                 "
242         else
243                 echo "
244 New/unknown test failure found!!!
245         "
246         fi
247         
248         # 4th memcheck step
249         OPJ_MEMCHECK_XML=$(find build -path 'build/Testing/*' -name 'DynamicAnalysis.xml')
250         if [ -f "${OPJ_MEMCHECK_XML}" ]; then
251                 if grep '<Defect Type' ${OPJ_MEMCHECK_XML} 2> /dev/null; then
252                         echo "Errors were found in dynamic analysis log"
253                         OPJ_CI_RESULT=1
254                 fi
255         fi
256 fi
257
258 exit ${OPJ_CI_RESULT}