1#!/bin/bash
2# Copyright (c) 2012 The LibYuv Project Authors. All rights reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS.  All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9
10# Set up some paths and re-direct the arguments to libyuv_tests.py
11
12# This script is a copy of the chrome_tests.sh wrapper script with the following
13# changes:
14# - The locate_valgrind.sh of Chromium's Valgrind scripts dir is used to locate
15#   the Valgrind framework install.
16# - libyuv_tests.py is invoked instead of chrome_tests.py.
17# - Chromium's Valgrind scripts directory is added to the PYTHONPATH to make it
18#   possible to execute the Python scripts properly.
19
20export THISDIR=`dirname $0`
21ARGV_COPY="$@"
22
23# We need to set CHROME_VALGRIND iff using Memcheck or TSan-Valgrind:
24#   tools/valgrind-libyuv/libyuv_tests.sh --tool memcheck
25# or
26#   tools/valgrind-libyuv/libyuv_tests.sh --tool=memcheck
27# (same for "--tool=tsan")
28tool="memcheck"  # Default to memcheck.
29while (( "$#" ))
30do
31  if [[ "$1" == "--tool" ]]
32  then
33    tool="$2"
34    shift
35  elif [[ "$1" =~ --tool=(.*) ]]
36  then
37    tool="${BASH_REMATCH[1]}"
38  fi
39  shift
40done
41
42NEEDS_VALGRIND=0
43NEEDS_DRMEMORY=0
44
45case "$tool" in
46  "memcheck")
47    NEEDS_VALGRIND=1
48    ;;
49  "tsan" | "tsan_rv")
50    if [ "`uname -s`" == CYGWIN* ]
51    then
52      NEEDS_PIN=1
53    else
54      NEEDS_VALGRIND=1
55    fi
56    ;;
57  "drmemory" | "drmemory_light" | "drmemory_full" | "drmemory_pattern")
58    NEEDS_DRMEMORY=1
59    ;;
60esac
61
62# For Libyuv, we'll use the locate_valgrind.sh script in Chromium's Valgrind
63# scripts dir to locate the Valgrind framework install
64CHROME_VALGRIND_SCRIPTS=$THISDIR/../valgrind
65
66if [ "$NEEDS_VALGRIND" == "1" ]
67then
68  CHROME_VALGRIND=`sh $CHROME_VALGRIND_SCRIPTS/locate_valgrind.sh`
69  if [ "$CHROME_VALGRIND" = "" ]
70  then
71    # locate_valgrind.sh failed
72    exit 1
73  fi
74  echo "Using valgrind binaries from ${CHROME_VALGRIND}"
75
76  PATH="${CHROME_VALGRIND}/bin:$PATH"
77  # We need to set these variables to override default lib paths hard-coded into
78  # Valgrind binary.
79  export VALGRIND_LIB="$CHROME_VALGRIND/lib/valgrind"
80  export VALGRIND_LIB_INNER="$CHROME_VALGRIND/lib/valgrind"
81
82  # Clean up some /tmp directories that might be stale due to interrupted
83  # chrome_tests.py execution.
84  # FYI:
85  #   -mtime +1  <- only print files modified more than 24h ago,
86  #   -print0/-0 are needed to handle possible newlines in the filenames.
87  echo "Cleanup /tmp from Valgrind stuff"
88  find /tmp -maxdepth 1 \(\
89        -name "vgdb-pipe-*" -or -name "vg_logs_*" -or -name "valgrind.*" \
90      \) -mtime +1 -print0 | xargs -0 rm -rf
91fi
92
93if [ "$NEEDS_DRMEMORY" == "1" ]
94then
95  if [ -z "$DRMEMORY_COMMAND" ]
96  then
97    DRMEMORY_PATH="$THISDIR/../../third_party/drmemory"
98    DRMEMORY_SFX="$DRMEMORY_PATH/drmemory-windows-sfx.exe"
99    if [ ! -f "$DRMEMORY_SFX" ]
100    then
101      echo "Can't find Dr. Memory executables."
102      echo "See http://www.chromium.org/developers/how-tos/using-valgrind/dr-memory"
103      echo "for the instructions on how to get them."
104      exit 1
105    fi
106
107    chmod +x "$DRMEMORY_SFX"  # Cygwin won't run it without +x.
108    "$DRMEMORY_SFX" -o"$DRMEMORY_PATH/unpacked" -y
109    export DRMEMORY_COMMAND="$DRMEMORY_PATH/unpacked/bin/drmemory.exe"
110  fi
111fi
112
113if [ "$NEEDS_PIN" == "1" ]
114then
115  if [ -z "$PIN_COMMAND" ]
116  then
117    # Set up PIN_COMMAND to invoke TSan.
118    TSAN_PATH="$THISDIR/../../third_party/tsan"
119    TSAN_SFX="$TSAN_PATH/tsan-x86-windows-sfx.exe"
120    echo "$TSAN_SFX"
121    if [ ! -f $TSAN_SFX ]
122    then
123      echo "Can't find ThreadSanitizer executables."
124      echo "See http://www.chromium.org/developers/how-tos/using-valgrind/threadsanitizer/threadsanitizer-on-windows"
125      echo "for the instructions on how to get them."
126      exit 1
127    fi
128
129    chmod +x "$TSAN_SFX"  # Cygwin won't run it without +x.
130    "$TSAN_SFX" -o"$TSAN_PATH"/unpacked -y
131    export PIN_COMMAND="$TSAN_PATH/unpacked/tsan-x86-windows/tsan.bat"
132  fi
133fi
134
135# Add Chrome's Valgrind scripts dir to the PYTHON_PATH since it contains
136# the scripts that are needed for this script to run
137PYTHONPATH=$THISDIR/../python/google:$CHROME_VALGRIND_SCRIPTS python \
138           "$THISDIR/libyuv_tests.py" $ARGV_COPY
139