tools_common.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #!/bin/sh
  2. ##
  3. ## Copyright (c) 2014 The WebM project authors. All Rights Reserved.
  4. ##
  5. ## Use of this source code is governed by a BSD-style license
  6. ## that can be found in the LICENSE file in the root of the source
  7. ## tree. An additional intellectual property rights grant can be found
  8. ## in the file PATENTS. All contributing project authors may
  9. ## be found in the AUTHORS file in the root of the source tree.
  10. ##
  11. ## This file contains shell code shared by test scripts for libvpx tools.
  12. # Use $VPX_TEST_TOOLS_COMMON_SH as a pseudo include guard.
  13. if [ -z "${VPX_TEST_TOOLS_COMMON_SH}" ]; then
  14. VPX_TEST_TOOLS_COMMON_SH=included
  15. set -e
  16. devnull='> /dev/null 2>&1'
  17. VPX_TEST_PREFIX=""
  18. elog() {
  19. echo "$@" 1>&2
  20. }
  21. vlog() {
  22. if [ "${VPX_TEST_VERBOSE_OUTPUT}" = "yes" ]; then
  23. echo "$@"
  24. fi
  25. }
  26. # Sets $VPX_TOOL_TEST to the name specified by positional parameter one.
  27. test_begin() {
  28. VPX_TOOL_TEST="${1}"
  29. }
  30. # Clears the VPX_TOOL_TEST variable after confirming that $VPX_TOOL_TEST matches
  31. # positional parameter one.
  32. test_end() {
  33. if [ "$1" != "${VPX_TOOL_TEST}" ]; then
  34. echo "FAIL completed test mismatch!."
  35. echo " completed test: ${1}"
  36. echo " active test: ${VPX_TOOL_TEST}."
  37. return 1
  38. fi
  39. VPX_TOOL_TEST='<unset>'
  40. }
  41. # Echoes the target configuration being tested.
  42. test_configuration_target() {
  43. vpx_config_mk="${LIBVPX_CONFIG_PATH}/config.mk"
  44. # Find the TOOLCHAIN line, split it using ':=' as the field separator, and
  45. # print the last field to get the value. Then pipe the value to tr to consume
  46. # any leading/trailing spaces while allowing tr to echo the output to stdout.
  47. awk -F ':=' '/TOOLCHAIN/ { print $NF }' "${vpx_config_mk}" | tr -d ' '
  48. }
  49. # Trap function used for failure reports and tool output directory removal.
  50. # When the contents of $VPX_TOOL_TEST do not match the string '<unset>', reports
  51. # failure of test stored in $VPX_TOOL_TEST.
  52. cleanup() {
  53. if [ -n "${VPX_TOOL_TEST}" ] && [ "${VPX_TOOL_TEST}" != '<unset>' ]; then
  54. echo "FAIL: $VPX_TOOL_TEST"
  55. fi
  56. if [ -n "${VPX_TEST_OUTPUT_DIR}" ] && [ -d "${VPX_TEST_OUTPUT_DIR}" ]; then
  57. rm -rf "${VPX_TEST_OUTPUT_DIR}"
  58. fi
  59. }
  60. # Echoes the git hash portion of the VERSION_STRING variable defined in
  61. # $LIBVPX_CONFIG_PATH/config.mk to stdout, or the version number string when
  62. # no git hash is contained in VERSION_STRING.
  63. config_hash() {
  64. vpx_config_mk="${LIBVPX_CONFIG_PATH}/config.mk"
  65. # Find VERSION_STRING line, split it with "-g" and print the last field to
  66. # output the git hash to stdout.
  67. vpx_version=$(awk -F -g '/VERSION_STRING/ {print $NF}' "${vpx_config_mk}")
  68. # Handle two situations here:
  69. # 1. The default case: $vpx_version is a git hash, so echo it unchanged.
  70. # 2. When being run a non-dev tree, the -g portion is not present in the
  71. # version string: It's only the version number.
  72. # In this case $vpx_version is something like 'VERSION_STRING=v1.3.0', so
  73. # we echo only what is after the '='.
  74. echo "${vpx_version##*=}"
  75. }
  76. # Echoes the short form of the current git hash.
  77. current_hash() {
  78. if git --version > /dev/null 2>&1; then
  79. (cd "$(dirname "${0}")"
  80. git rev-parse --short HEAD)
  81. else
  82. # Return the config hash if git is unavailable: Fail silently, git hashes
  83. # are used only for warnings.
  84. config_hash
  85. fi
  86. }
  87. # Echoes warnings to stdout when git hash in vpx_config.h does not match the
  88. # current git hash.
  89. check_git_hashes() {
  90. hash_at_configure_time=$(config_hash)
  91. hash_now=$(current_hash)
  92. if [ "${hash_at_configure_time}" != "${hash_now}" ]; then
  93. echo "Warning: git hash has changed since last configure."
  94. fi
  95. }
  96. # $1 is the name of an environment variable containing a directory name to
  97. # test.
  98. test_env_var_dir() {
  99. local dir=$(eval echo "\${$1}")
  100. if [ ! -d "${dir}" ]; then
  101. elog "'${dir}': No such directory"
  102. elog "The $1 environment variable must be set to a valid directory."
  103. return 1
  104. fi
  105. }
  106. # This script requires that the LIBVPX_BIN_PATH, LIBVPX_CONFIG_PATH, and
  107. # LIBVPX_TEST_DATA_PATH variables are in the environment: Confirm that
  108. # the variables are set and that they all evaluate to directory paths.
  109. verify_vpx_test_environment() {
  110. test_env_var_dir "LIBVPX_BIN_PATH" \
  111. && test_env_var_dir "LIBVPX_CONFIG_PATH" \
  112. && test_env_var_dir "LIBVPX_TEST_DATA_PATH"
  113. }
  114. # Greps vpx_config.h in LIBVPX_CONFIG_PATH for positional parameter one, which
  115. # should be a LIBVPX preprocessor flag. Echoes yes to stdout when the feature
  116. # is available.
  117. vpx_config_option_enabled() {
  118. vpx_config_option="${1}"
  119. vpx_config_file="${LIBVPX_CONFIG_PATH}/vpx_config.h"
  120. config_line=$(grep "${vpx_config_option}" "${vpx_config_file}")
  121. if echo "${config_line}" | egrep -q '1$'; then
  122. echo yes
  123. fi
  124. }
  125. # Echoes yes when output of test_configuration_target() contains win32 or win64.
  126. is_windows_target() {
  127. if test_configuration_target \
  128. | grep -q -e win32 -e win64 > /dev/null 2>&1; then
  129. echo yes
  130. fi
  131. }
  132. # Echoes path to $1 when it's executable and exists in ${LIBVPX_BIN_PATH}, or an
  133. # empty string. Caller is responsible for testing the string once the function
  134. # returns.
  135. vpx_tool_path() {
  136. local tool_name="$1"
  137. local tool_path="${LIBVPX_BIN_PATH}/${tool_name}${VPX_TEST_EXE_SUFFIX}"
  138. if [ ! -x "${tool_path}" ]; then
  139. # Try one directory up: when running via examples.sh the tool could be in
  140. # the parent directory of $LIBVPX_BIN_PATH.
  141. tool_path="${LIBVPX_BIN_PATH}/../${tool_name}${VPX_TEST_EXE_SUFFIX}"
  142. fi
  143. if [ ! -x "${tool_path}" ]; then
  144. tool_path=""
  145. fi
  146. echo "${tool_path}"
  147. }
  148. # Echoes yes to stdout when the file named by positional parameter one exists
  149. # in LIBVPX_BIN_PATH, and is executable.
  150. vpx_tool_available() {
  151. local tool_name="$1"
  152. local tool="${LIBVPX_BIN_PATH}/${tool_name}${VPX_TEST_EXE_SUFFIX}"
  153. [ -x "${tool}" ] && echo yes
  154. }
  155. # Echoes yes to stdout when vpx_config_option_enabled() reports yes for
  156. # CONFIG_VP8_DECODER.
  157. vp8_decode_available() {
  158. [ "$(vpx_config_option_enabled CONFIG_VP8_DECODER)" = "yes" ] && echo yes
  159. }
  160. # Echoes yes to stdout when vpx_config_option_enabled() reports yes for
  161. # CONFIG_VP8_ENCODER.
  162. vp8_encode_available() {
  163. [ "$(vpx_config_option_enabled CONFIG_VP8_ENCODER)" = "yes" ] && echo yes
  164. }
  165. # Echoes yes to stdout when vpx_config_option_enabled() reports yes for
  166. # CONFIG_VP9_DECODER.
  167. vp9_decode_available() {
  168. [ "$(vpx_config_option_enabled CONFIG_VP9_DECODER)" = "yes" ] && echo yes
  169. }
  170. # Echoes yes to stdout when vpx_config_option_enabled() reports yes for
  171. # CONFIG_VP9_ENCODER.
  172. vp9_encode_available() {
  173. [ "$(vpx_config_option_enabled CONFIG_VP9_ENCODER)" = "yes" ] && echo yes
  174. }
  175. # Echoes yes to stdout when vpx_config_option_enabled() reports yes for
  176. # CONFIG_WEBM_IO.
  177. webm_io_available() {
  178. [ "$(vpx_config_option_enabled CONFIG_WEBM_IO)" = "yes" ] && echo yes
  179. }
  180. # Filters strings from $1 using the filter specified by $2. Filter behavior
  181. # depends on the presence of $3. When $3 is present, strings that match the
  182. # filter are excluded. When $3 is omitted, strings matching the filter are
  183. # included.
  184. # The filtered result is echoed to stdout.
  185. filter_strings() {
  186. strings=${1}
  187. filter=${2}
  188. exclude=${3}
  189. if [ -n "${exclude}" ]; then
  190. # When positional parameter three exists the caller wants to remove strings.
  191. # Tell grep to invert matches using the -v argument.
  192. exclude='-v'
  193. else
  194. unset exclude
  195. fi
  196. if [ -n "${filter}" ]; then
  197. for s in ${strings}; do
  198. if echo "${s}" | egrep -q ${exclude} "${filter}" > /dev/null 2>&1; then
  199. filtered_strings="${filtered_strings} ${s}"
  200. fi
  201. done
  202. else
  203. filtered_strings="${strings}"
  204. fi
  205. echo "${filtered_strings}"
  206. }
  207. # Runs user test functions passed via positional parameters one and two.
  208. # Functions in positional parameter one are treated as environment verification
  209. # functions and are run unconditionally. Functions in positional parameter two
  210. # are run according to the rules specified in vpx_test_usage().
  211. run_tests() {
  212. local env_tests="verify_vpx_test_environment $1"
  213. local tests_to_filter="$2"
  214. local test_name="${VPX_TEST_NAME}"
  215. if [ -z "${test_name}" ]; then
  216. test_name="$(basename "${0%.*}")"
  217. fi
  218. if [ "${VPX_TEST_RUN_DISABLED_TESTS}" != "yes" ]; then
  219. # Filter out DISABLED tests.
  220. tests_to_filter=$(filter_strings "${tests_to_filter}" ^DISABLED exclude)
  221. fi
  222. if [ -n "${VPX_TEST_FILTER}" ]; then
  223. # Remove tests not matching the user's filter.
  224. tests_to_filter=$(filter_strings "${tests_to_filter}" ${VPX_TEST_FILTER})
  225. fi
  226. # User requested test listing: Dump test names and return.
  227. if [ "${VPX_TEST_LIST_TESTS}" = "yes" ]; then
  228. for test_name in $tests_to_filter; do
  229. echo ${test_name}
  230. done
  231. return
  232. fi
  233. # Don't bother with the environment tests if everything else was disabled.
  234. [ -z "${tests_to_filter}" ] && return
  235. # Combine environment and actual tests.
  236. local tests_to_run="${env_tests} ${tests_to_filter}"
  237. check_git_hashes
  238. # Run tests.
  239. for test in ${tests_to_run}; do
  240. test_begin "${test}"
  241. vlog " RUN ${test}"
  242. "${test}"
  243. vlog " PASS ${test}"
  244. test_end "${test}"
  245. done
  246. local tested_config="$(test_configuration_target) @ $(current_hash)"
  247. echo "${test_name}: Done, all tests pass for ${tested_config}."
  248. }
  249. vpx_test_usage() {
  250. cat << EOF
  251. Usage: ${0##*/} [arguments]
  252. --bin-path <path to libvpx binaries directory>
  253. --config-path <path to libvpx config directory>
  254. --filter <filter>: User test filter. Only tests matching filter are run.
  255. --run-disabled-tests: Run disabled tests.
  256. --help: Display this message and exit.
  257. --test-data-path <path to libvpx test data directory>
  258. --show-program-output: Shows output from all programs being tested.
  259. --prefix: Allows for a user specified prefix to be inserted before all test
  260. programs. Grants the ability, for example, to run test programs
  261. within valgrind.
  262. --list-tests: List all test names and exit without actually running tests.
  263. --verbose: Verbose output.
  264. When the --bin-path option is not specified the script attempts to use
  265. \$LIBVPX_BIN_PATH and then the current directory.
  266. When the --config-path option is not specified the script attempts to use
  267. \$LIBVPX_CONFIG_PATH and then the current directory.
  268. When the -test-data-path option is not specified the script attempts to use
  269. \$LIBVPX_TEST_DATA_PATH and then the current directory.
  270. EOF
  271. }
  272. # Returns non-zero (failure) when required environment variables are empty
  273. # strings.
  274. vpx_test_check_environment() {
  275. if [ -z "${LIBVPX_BIN_PATH}" ] || \
  276. [ -z "${LIBVPX_CONFIG_PATH}" ] || \
  277. [ -z "${LIBVPX_TEST_DATA_PATH}" ]; then
  278. return 1
  279. fi
  280. }
  281. # Parse the command line.
  282. while [ -n "$1" ]; do
  283. case "$1" in
  284. --bin-path)
  285. LIBVPX_BIN_PATH="$2"
  286. shift
  287. ;;
  288. --config-path)
  289. LIBVPX_CONFIG_PATH="$2"
  290. shift
  291. ;;
  292. --filter)
  293. VPX_TEST_FILTER="$2"
  294. shift
  295. ;;
  296. --run-disabled-tests)
  297. VPX_TEST_RUN_DISABLED_TESTS=yes
  298. ;;
  299. --help)
  300. vpx_test_usage
  301. exit
  302. ;;
  303. --test-data-path)
  304. LIBVPX_TEST_DATA_PATH="$2"
  305. shift
  306. ;;
  307. --prefix)
  308. VPX_TEST_PREFIX="$2"
  309. shift
  310. ;;
  311. --verbose)
  312. VPX_TEST_VERBOSE_OUTPUT=yes
  313. ;;
  314. --show-program-output)
  315. devnull=
  316. ;;
  317. --list-tests)
  318. VPX_TEST_LIST_TESTS=yes
  319. ;;
  320. *)
  321. vpx_test_usage
  322. exit 1
  323. ;;
  324. esac
  325. shift
  326. done
  327. # Handle running the tests from a build directory without arguments when running
  328. # the tests on *nix/macosx.
  329. LIBVPX_BIN_PATH="${LIBVPX_BIN_PATH:-.}"
  330. LIBVPX_CONFIG_PATH="${LIBVPX_CONFIG_PATH:-.}"
  331. LIBVPX_TEST_DATA_PATH="${LIBVPX_TEST_DATA_PATH:-.}"
  332. # Create a temporary directory for output files, and a trap to clean it up.
  333. if [ -n "${TMPDIR}" ]; then
  334. VPX_TEST_TEMP_ROOT="${TMPDIR}"
  335. elif [ -n "${TEMPDIR}" ]; then
  336. VPX_TEST_TEMP_ROOT="${TEMPDIR}"
  337. else
  338. VPX_TEST_TEMP_ROOT=/tmp
  339. fi
  340. VPX_TEST_OUTPUT_DIR="${VPX_TEST_TEMP_ROOT}/vpx_test_$$"
  341. if ! mkdir -p "${VPX_TEST_OUTPUT_DIR}" || \
  342. [ ! -d "${VPX_TEST_OUTPUT_DIR}" ]; then
  343. echo "${0##*/}: Cannot create output directory, giving up."
  344. echo "${0##*/}: VPX_TEST_OUTPUT_DIR=${VPX_TEST_OUTPUT_DIR}"
  345. exit 1
  346. fi
  347. if [ "$(is_windows_target)" = "yes" ]; then
  348. VPX_TEST_EXE_SUFFIX=".exe"
  349. fi
  350. # Variables shared by tests.
  351. VP8_IVF_FILE="${LIBVPX_TEST_DATA_PATH}/vp80-00-comprehensive-001.ivf"
  352. VP9_IVF_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-09-subpixel-00.ivf"
  353. VP9_WEBM_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-00-quantizer-00.webm"
  354. VP9_FPM_WEBM_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-07-frame_parallel-1.webm"
  355. VP9_LT_50_FRAMES_WEBM_FILE="${LIBVPX_TEST_DATA_PATH}/vp90-2-02-size-32x08.webm"
  356. VP9_RAW_FILE="${LIBVPX_TEST_DATA_PATH}/crbug-1539.rawfile"
  357. YUV_RAW_INPUT="${LIBVPX_TEST_DATA_PATH}/hantro_collage_w352h288.yuv"
  358. YUV_RAW_INPUT_WIDTH=352
  359. YUV_RAW_INPUT_HEIGHT=288
  360. Y4M_NOSQ_PAR_INPUT="${LIBVPX_TEST_DATA_PATH}/park_joy_90p_8_420_a10-1.y4m"
  361. Y4M_720P_INPUT="${LIBVPX_TEST_DATA_PATH}/niklas_1280_720_30.y4m"
  362. Y4M_720P_INPUT_WIDTH=1280
  363. Y4M_720P_INPUT_HEIGHT=720
  364. # Setup a trap function to clean up after tests complete.
  365. trap cleanup EXIT
  366. vlog "$(basename "${0%.*}") test configuration:
  367. LIBVPX_BIN_PATH=${LIBVPX_BIN_PATH}
  368. LIBVPX_CONFIG_PATH=${LIBVPX_CONFIG_PATH}
  369. LIBVPX_TEST_DATA_PATH=${LIBVPX_TEST_DATA_PATH}
  370. VP8_IVF_FILE=${VP8_IVF_FILE}
  371. VP9_IVF_FILE=${VP9_IVF_FILE}
  372. VP9_WEBM_FILE=${VP9_WEBM_FILE}
  373. VPX_TEST_EXE_SUFFIX=${VPX_TEST_EXE_SUFFIX}
  374. VPX_TEST_FILTER=${VPX_TEST_FILTER}
  375. VPX_TEST_LIST_TESTS=${VPX_TEST_LIST_TESTS}
  376. VPX_TEST_OUTPUT_DIR=${VPX_TEST_OUTPUT_DIR}
  377. VPX_TEST_PREFIX=${VPX_TEST_PREFIX}
  378. VPX_TEST_RUN_DISABLED_TESTS=${VPX_TEST_RUN_DISABLED_TESTS}
  379. VPX_TEST_SHOW_PROGRAM_OUTPUT=${VPX_TEST_SHOW_PROGRAM_OUTPUT}
  380. VPX_TEST_TEMP_ROOT=${VPX_TEST_TEMP_ROOT}
  381. VPX_TEST_VERBOSE_OUTPUT=${VPX_TEST_VERBOSE_OUTPUT}
  382. YUV_RAW_INPUT=${YUV_RAW_INPUT}
  383. YUV_RAW_INPUT_WIDTH=${YUV_RAW_INPUT_WIDTH}
  384. YUV_RAW_INPUT_HEIGHT=${YUV_RAW_INPUT_HEIGHT}
  385. Y4M_NOSQ_PAR_INPUT=${Y4M_NOSQ_PAR_INPUT}"
  386. fi # End $VPX_TEST_TOOLS_COMMON_SH pseudo include guard.