appveyor.yml: add a /arch:AVX2 config on Windows
[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 # Hack for appveyor to get GNU find in path before windows one.
12 export PATH=$(dirname ${BASH}):$PATH
13
14 # Set-up some bash options
15 set -o nounset   ## set -u : exit the script if you try to use an uninitialised variable
16 set -o errexit   ## set -e : exit the script if any statement returns a non-true return value
17 set -o pipefail  ## Fail on error in pipe
18
19 function opjpath ()
20 {
21         if [ "${OPJ_CI_IS_CYGWIN:-}" == "1" ]; then
22                 cygpath $1 "$2"
23         else
24                 echo "$2"
25         fi
26 }
27
28 # ABI check is done by abi-check.sh
29 if [ "${OPJ_CI_ABI_CHECK:-}" == "1" ]; then
30         exit 0
31 fi
32
33 if [ "${OPJ_CI_CC:-}" != "" ]; then
34     export CC=${OPJ_CI_CC}
35     echo "Using ${CC}"
36 fi
37
38 if [ "${OPJ_CI_CXX:-}" != "" ]; then
39     export CXX=${OPJ_CI_CXX}
40     echo "Using ${CXX}"
41 fi
42
43 # Set-up some variables
44 if [ "${OPJ_CI_BUILD_CONFIGURATION:-}" == "" ]; then
45         export OPJ_CI_BUILD_CONFIGURATION=Release #default
46 fi
47 OPJ_SOURCE_DIR=$(cd $(dirname $0)/../.. && pwd)
48
49 if [ "${OPJ_DO_SUBMIT:-}" == "" ]; then
50         OPJ_DO_SUBMIT=0 # Do not flood cdash by default
51 fi
52 if [ "${TRAVIS_REPO_SLUG:-}" != "" ]; then
53         OPJ_OWNER=$(echo "${TRAVIS_REPO_SLUG}" | sed 's/\(^.*\)\/.*/\1/')
54         OPJ_SITE="${OPJ_OWNER}.travis-ci.org"
55         if [ "${OPJ_OWNER}" == "uclouvain" ]; then
56                 OPJ_DO_SUBMIT=1
57         fi
58 elif [ "${APPVEYOR_REPO_NAME:-}" != "" ]; then
59         OPJ_OWNER=$(echo "${APPVEYOR_REPO_NAME}" | sed 's/\(^.*\)\/.*/\1/')
60         OPJ_SITE="${OPJ_OWNER}.appveyor.com"
61         if [ "${OPJ_OWNER}" == "uclouvain" ]; then
62                 OPJ_DO_SUBMIT=1
63         fi
64 else
65         OPJ_SITE="$(hostname)"
66 fi
67
68 if [ "${TRAVIS_OS_NAME:-}" == "" ]; then
69   # Let's guess OS for testing purposes
70         echo "Guessing OS"
71         if uname -s | grep -i Darwin &> /dev/null; then
72                 TRAVIS_OS_NAME=osx
73         elif uname -s | grep -i Linux &> /dev/null; then
74                 TRAVIS_OS_NAME=linux
75                 if [ "${CC:-}" == "" ]; then
76                         # default to gcc
77                         export CC=gcc
78                 fi
79         elif uname -s | grep -i CYGWIN &> /dev/null; then
80                 TRAVIS_OS_NAME=windows
81         elif uname -s | grep -i MINGW &> /dev/null; then
82                 TRAVIS_OS_NAME=windows
83         elif [ "${APPVEYOR:-}" == "True" ]; then
84                 TRAVIS_OS_NAME=windows
85         else
86                 echo "Failed to guess OS"; exit 1
87         fi
88         echo "${TRAVIS_OS_NAME}"
89 fi
90
91 if [ "${TRAVIS_OS_NAME}" == "osx" ]; then
92         OPJ_OS_NAME=$(sw_vers -productName | tr -d ' ')$(sw_vers -productVersion | sed 's/\([^0-9]*\.[0-9]*\).*/\1/')
93         OPJ_CC_VERSION=$(xcodebuild -version | grep -i xcode)
94         OPJ_CC_VERSION=xcode${OPJ_CC_VERSION:6}
95 elif [ "${TRAVIS_OS_NAME}" == "linux" ]; then
96         OPJ_OS_NAME=linux
97         if which lsb_release > /dev/null; then
98                 OPJ_OS_NAME=$(lsb_release -si)$(lsb_release -sr | sed 's/\([^0-9]*\.[0-9]*\).*/\1/')
99         fi
100         if [ -z "${CC##*gcc*}" ]; then
101                 OPJ_CC_VERSION=$(${CC} --version | head -1 | sed 's/.*\ \([0-9.]*[0-9]\)/\1/')
102                 if [ -z "${CC##*mingw*}" ]; then
103                         OPJ_CC_VERSION=mingw${OPJ_CC_VERSION}
104                         # disable testing for now
105                         export OPJ_CI_SKIP_TESTS=1
106                 else
107                         OPJ_CC_VERSION=gcc${OPJ_CC_VERSION}
108                 fi
109         elif [ -z "${CC##*clang*}" ]; then
110                 OPJ_CC_VERSION=clang$(${CC} --version | grep version | sed 's/.*version \([^0-9.]*[0-9.]*\).*/\1/')
111         else
112                 echo "Compiler not supported: ${CC}"; exit 1
113         fi
114         if [ "${OPJ_CI_INSTRUCTION_SETS-:}" == "-mavx2" ]; then
115                 AVX2_AVAIL=1
116                 cat /proc/cpuinfo | grep avx2 >/dev/null || AVX2_AVAIL=0
117                 if [[ "${AVX2_AVAIL}" == "1" ]]; then
118                         echo "AVX2 available on CPU"
119                 else
120                         echo "AVX2 not available on CPU. Disabling tests"
121                         cat /proc/cpuinfo  | grep flags | head -n 1
122                         export OPJ_CI_SKIP_TESTS=1
123                 fi
124         fi
125 elif [ "${TRAVIS_OS_NAME}" == "windows" ]; then
126         OPJ_OS_NAME=windows
127         if which cl > /dev/null; then
128                 OPJ_CL_VERSION=$(cl 2>&1 | grep Version | sed 's/.*Version \([0-9]*\).*/\1/')
129                 if [ ${OPJ_CL_VERSION} -eq 19 ]; then
130                         OPJ_CC_VERSION=vs2015
131                 elif [ ${OPJ_CL_VERSION} -eq 18 ]; then
132                         OPJ_CC_VERSION=vs2013
133                 elif [ ${OPJ_CL_VERSION} -eq 17 ]; then
134                         OPJ_CC_VERSION=vs2012
135                 elif [ ${OPJ_CL_VERSION} -eq 16 ]; then
136                         OPJ_CC_VERSION=vs2010
137                 elif [ ${OPJ_CL_VERSION} -eq 15 ]; then
138                         OPJ_CC_VERSION=vs2008
139                 elif [ ${OPJ_CL_VERSION} -eq 14 ]; then
140                         OPJ_CC_VERSION=vs2005
141                 else
142                         OPJ_CC_VERSION=vs????
143                 fi
144         fi
145         if [ "${OPJ_CI_INSTRUCTION_SETS-:}" == "/arch:AVX2" ]; then
146                 cl $PWD/tools/travis-ci/detect-avx2.c
147                 if ./detect-avx2.exe; then
148                         echo "AVX2 available on CPU"
149                 else
150                         echo "AVX2 not available on CPU. Disabling tests"
151                         export OPJ_CI_SKIP_TESTS=1
152                 fi
153         fi
154 else
155         echo "OS not supported: ${TRAVIS_OS_NAME}"; exit 1
156 fi
157
158 if [ "${OPJ_CI_ARCH:-}" == "" ]; then
159         echo "Guessing build architecture"
160         MACHINE_ARCH=$(uname -m)
161         if [ "${MACHINE_ARCH}" == "x86_64" ]; then
162                 export OPJ_CI_ARCH=x86_64
163         fi
164         echo "${OPJ_CI_ARCH}"
165 fi
166
167 if [ "${TRAVIS_BRANCH:-}" == "" ]; then
168         if [ "${APPVEYOR_REPO_BRANCH:-}" != "" ]; then
169                 TRAVIS_BRANCH=${APPVEYOR_REPO_BRANCH}
170         else
171                 echo "Guessing branch"
172                 TRAVIS_BRANCH=$(git -C ${OPJ_SOURCE_DIR} branch | grep '*' | tr -d '*[[:blank:]]')
173         fi
174 fi
175
176 OPJ_BUILDNAME=${OPJ_OS_NAME}-${OPJ_CC_VERSION}-${OPJ_CI_ARCH}-${TRAVIS_BRANCH}
177 OPJ_BUILDNAME_TEST=${OPJ_OS_NAME}-${OPJ_CC_VERSION}-${OPJ_CI_ARCH}
178 if [ "${TRAVIS_PULL_REQUEST:-}" != "false" ] && [ "${TRAVIS_PULL_REQUEST:-}" != "" ]; then
179         OPJ_BUILDNAME=${OPJ_BUILDNAME}-pr${TRAVIS_PULL_REQUEST}
180 elif [ "${APPVEYOR_PULL_REQUEST_NUMBER:-}" != "" ]; then
181         OPJ_BUILDNAME=${OPJ_BUILDNAME}-pr${APPVEYOR_PULL_REQUEST_NUMBER}
182 fi
183 OPJ_BUILDNAME=${OPJ_BUILDNAME}-${OPJ_CI_BUILD_CONFIGURATION}-3rdP
184 OPJ_BUILDNAME_TEST=${OPJ_BUILDNAME_TEST}-${OPJ_CI_BUILD_CONFIGURATION}-3rdP
185 if [ "${OPJ_CI_ASAN:-}" == "1" ]; then
186         OPJ_BUILDNAME=${OPJ_BUILDNAME}-ASan
187         OPJ_BUILDNAME_TEST=${OPJ_BUILDNAME_TEST}-ASan
188 fi
189
190 if [ "${OPJ_NONCOMMERCIAL:-}" == "1" ] && [ "${OPJ_CI_SKIP_TESTS:-}" != "1" ] && [ -d kdu ]; then
191         echo "
192 Testing will use Kakadu trial binaries. Here's the copyright notice from kakadu:
193 Copyright is owned by NewSouth Innovations Pty Limited, commercial arm of the UNSW Australia in Sydney.
194 You are free to trial these executables and even to re-distribute them,
195 so long as such use or re-distribution is accompanied with this copyright notice and is not for commercial gain.
196 Note: Binaries can only be used for non-commercial purposes.
197 "
198 fi
199
200 if [ -d cmake-install ]; then
201         export PATH=${PWD}/cmake-install/bin:${PATH}
202 fi
203
204 set -x
205 # This will print configuration
206 # travis-ci doesn't dump cmake version in system info, let's print it 
207 cmake --version
208
209 export TRAVIS_OS_NAME=${TRAVIS_OS_NAME}
210 export OPJ_SITE=${OPJ_SITE}
211 export OPJ_BUILDNAME=${OPJ_BUILDNAME}
212 export OPJ_SOURCE_DIR=$(opjpath -m ${OPJ_SOURCE_DIR})
213 export OPJ_BINARY_DIR=$(opjpath -m ${PWD}/build)
214 export OPJ_BUILD_CONFIGURATION=${OPJ_CI_BUILD_CONFIGURATION}
215 export OPJ_DO_SUBMIT=${OPJ_DO_SUBMIT}
216
217 if [ "${OPJ_SKIP_REBUILD:-}" != "1" ]; then
218     ctest -S ${OPJ_SOURCE_DIR}/tools/ctest_scripts/travis-ci.cmake -V || true
219 fi
220 # ctest will exit with various error codes depending on version.
221 # ignore ctest exit code & parse this ourselves
222 set +x
223
224
225
226 if [ "${OPJ_CI_CHECK_STYLE:-}" == "1" ]; then
227     export OPJSTYLE=${PWD}/scripts/opjstyle
228     export PATH=${HOME}/.local/bin:${PATH}
229     scripts/verify-indentation.sh
230 fi
231
232
233 # Deployment if needed
234 #---------------------
235 if [ "${TRAVIS_TAG:-}" != "" ]; then
236                 OPJ_TAG_NAME=${TRAVIS_TAG}
237         elif [ "${APPVEYOR_REPO_TAG:-}" == "true" ]; then
238                 OPJ_TAG_NAME=${APPVEYOR_REPO_TAG_NAME}
239         else
240                 OPJ_TAG_NAME=""
241         fi
242 if [ "${OPJ_CI_INCLUDE_IF_DEPLOY:-}" == "1" ] && [ "${OPJ_TAG_NAME:-}" != "" ]; then
243 #if [ "${OPJ_CI_INCLUDE_IF_DEPLOY:-}" == "1" ]; then
244         OPJ_CI_DEPLOY=1         # unused for now
245         OPJ_CUR_DIR=${PWD}
246         if [ "${TRAVIS_OS_NAME:-}" == "linux" ]; then
247                 OPJ_PACK_GENERATOR="TGZ" # ZIP generator currently segfaults on linux
248         else
249                 OPJ_PACK_GENERATOR="ZIP"
250         fi
251         OPJ_PACK_NAME="openjpeg-${OPJ_TAG_NAME}-${TRAVIS_OS_NAME}-${OPJ_CI_ARCH}"
252         cd ${OPJ_BINARY_DIR}
253         cmake -D CPACK_GENERATOR:STRING=${OPJ_PACK_GENERATOR} -D CPACK_PACKAGE_FILE_NAME:STRING=${OPJ_PACK_NAME} ${OPJ_SOURCE_DIR}
254         cd ${OPJ_CUR_DIR}
255         cmake --build ${OPJ_BINARY_DIR} --target package
256         echo "ready to deploy $(ls ${OPJ_BINARY_DIR}/${OPJ_PACK_NAME}*) to GitHub releases"
257         if [ "${APPVEYOR_REPO_TAG:-}" == "true" ]; then
258                 appveyor PushArtifact "${OPJ_BINARY_DIR}/${OPJ_PACK_NAME}.zip"
259         fi
260 else
261         OPJ_CI_DEPLOY=0
262 fi
263
264 # let's parse configure/build/tests for failure
265
266 echo "
267 Parsing logs for failures
268 "
269 OPJ_CI_RESULT=0
270
271 # 1st configure step
272 OPJ_CONFIGURE_XML=$(find build -path 'build/Testing/*' -name 'Configure.xml')
273 if [ ! -f "${OPJ_CONFIGURE_XML}" ]; then
274         echo "No configure log found"
275         OPJ_CI_RESULT=1
276 else
277         if ! grep '<ConfigureStatus>0</ConfigureStatus>' ${OPJ_CONFIGURE_XML} &> /dev/null; then
278                 echo "Errors were found in configure log"
279                 OPJ_CI_RESULT=1
280         fi
281 fi
282
283 # 2nd build step
284 # We must have one Build.xml file
285 OPJ_BUILD_XML=$(find build -path 'build/Testing/*' -name 'Build.xml')
286 if [ ! -f "${OPJ_BUILD_XML}" ]; then
287         echo "No build log found"
288         OPJ_CI_RESULT=1
289 else
290         if grep '<Error>' ${OPJ_BUILD_XML} &> /dev/null; then
291                 echo "Errors were found in build log"
292                 OPJ_CI_RESULT=1
293         fi
294 fi
295
296 if [ ${OPJ_CI_RESULT} -ne 0 ]; then
297         # Don't trash output with failing tests when there are configure/build errors
298         exit ${OPJ_CI_RESULT}
299 fi
300
301 if [ "${OPJ_CI_SKIP_TESTS:-}" != "1" ]; then
302         OPJ_TEST_XML=$(find build -path 'build/Testing/*' -name 'Test.xml')
303         if [ ! -f "${OPJ_TEST_XML}" ]; then
304                 echo "No test log found"
305                 OPJ_CI_RESULT=1
306         else
307                 echo "Parsing tests for new/unknown failures"
308                 # 3rd test step
309                 OPJ_FAILEDTEST_LOG=$(find build -path 'build/Testing/Temporary/*' -name 'LastTestsFailed_*.log')
310                 if [ -f "${OPJ_FAILEDTEST_LOG}" ]; then
311                         awk -F: '{ print $2 }' ${OPJ_FAILEDTEST_LOG} > failures.txt
312                         while read FAILEDTEST; do
313                                 # Start with common errors
314                                 if grep -x "${FAILEDTEST}" $(opjpath -u ${OPJ_SOURCE_DIR})/tools/travis-ci/knownfailures-all.txt > /dev/null; then
315                                         continue
316                                 fi
317                                 if [ -f $(opjpath -u ${OPJ_SOURCE_DIR})/tools/travis-ci/knownfailures-${OPJ_BUILDNAME_TEST}.txt ]; then
318                                         if grep -x "${FAILEDTEST}" $(opjpath -u ${OPJ_SOURCE_DIR})/tools/travis-ci/knownfailures-${OPJ_BUILDNAME_TEST}.txt > /dev/null; then
319                                                 continue
320                                         fi
321                                 fi
322                                 echo "${FAILEDTEST}"
323                                 OPJ_CI_RESULT=1
324                         done < failures.txt
325                 fi
326         fi
327         
328         if [ ${OPJ_CI_RESULT} -eq 0 ]; then
329                 echo "No new/unknown test failure found
330                 "
331         else
332                 echo "
333 New/unknown test failure found!!!
334         "
335         fi
336         
337         # 4th memcheck step
338         OPJ_MEMCHECK_XML=$(find build -path 'build/Testing/*' -name 'DynamicAnalysis.xml')
339         if [ -f "${OPJ_MEMCHECK_XML}" ]; then
340                 if grep '<Defect Type' ${OPJ_MEMCHECK_XML} 2> /dev/null; then
341                         echo "Errors were found in dynamic analysis log"
342                         OPJ_CI_RESULT=1
343                 fi
344         fi
345 fi
346
347 if [ "${OPJ_CI_PERF_TESTS:-}" == "1" ]; then
348     cd tests/performance
349     echo "Running performance tests on current version (dry-run)"
350     PATH=../../build/bin:$PATH python ./perf_test.py
351     echo "Running performance tests on current version"
352     PATH=../../build/bin:$PATH python ./perf_test.py -o /tmp/new.csv
353     if [ "${OPJ_NONCOMMERCIAL:-}" == "1" ] && [ -d ../../kdu ]; then
354         echo "Running performances tests with Kakadu"
355         LD_LIBRARY_PATH=../../kdu PATH=../../kdu::$PATH python ./perf_test.py -kakadu -o /tmp/kakadu.csv
356         echo "Comparing current version with Kakadu"
357         python compare_perfs.py /tmp/kakadu.csv /tmp/new.csv || true
358     fi
359     cd ../..
360
361     REF_VERSION=master
362     if [ "${TRAVIS_PULL_REQUEST:-false}" == "false" ]; then
363         REF_VERSION=v2.1.2
364     fi
365     if [ ! -d ref_opj ]; then
366         git clone https://github.com/uclouvain/openjpeg ref_opj
367     fi
368     echo "Building reference version (${REF_VERSION})"
369     cd ref_opj
370     git checkout ${REF_VERSION}
371     mkdir -p build
372     cd build
373     cmake .. -DCMAKE_BUILD_TYPE=${OPJ_BUILD_CONFIGURATION}
374     make -j3
375     cd ../..
376     cd tests/performance
377     echo "Running performance tests on ${REF_VERSION} version (dry-run)"
378     PATH=../../ref_opj/build/bin:$PATH python ./perf_test.py
379     echo "Running performance tests on ${REF_VERSION} version"
380     PATH=../../ref_opj/build/bin:$PATH python ./perf_test.py -o /tmp/ref.csv
381     echo "Comparing current version with ${REF_VERSION} version"
382     # we should normally set OPJ_CI_RESULT=1 in case of failure, but
383     # this is too unreliable
384     python compare_perfs.py /tmp/ref.csv /tmp/new.csv || true
385     cd ../..
386 fi
387
388 if [ "${OPJ_CI_PROFILE:-}" == "1" ]; then
389     rm -rf build_gprof
390     mkdir build_gprof
391     cd build_gprof
392     # We need static linking for gprof
393     cmake "-DCMAKE_C_FLAGS=-pg -O3" -DCMAKE_EXE_LINKER_FLAGS=-pg -DCMAKE_SHARED_LINKER_FLAGS=-pg -DBUILD_SHARED_LIBS=OFF ..
394     make -j3
395     cd ..
396     build_gprof/bin/opj_decompress -i data/input/nonregression/kodak_2layers_lrcp.j2c -o out.tif > /dev/null
397     echo "Most CPU consuming functions:"
398     gprof build_gprof/bin/opj_decompress gmon.out | head || true
399
400     rm -f massif.out.*
401     valgrind --tool=massif build/bin/opj_decompress -i data/input/nonregression/kodak_2layers_lrcp.j2c -o out.tif >/dev/null 2>/dev/null
402     echo ""
403     echo "Memory consumption profile:"
404     python tests/profiling/filter_massif_output.py massif.out.*
405 fi
406
407 exit ${OPJ_CI_RESULT}