xref: /aosp_15_r20/external/libwebm/common/common.sh (revision 103e46e4cd4b6efcf6001f23fa8665fb110abf8d)
1#!/bin/sh
2##
3##  Copyright (c) 2015 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##
11set -e
12devnull='> /dev/null 2>&1'
13
14readonly ORIG_PWD="$(pwd)"
15
16elog() {
17  echo "${0##*/} failed because: $@" 1>&2
18}
19
20vlog() {
21  if [ "${VERBOSE}" = "yes" ]; then
22    echo "$@"
23  fi
24}
25
26# Terminates script when name of current directory does not match $1.
27check_dir() {
28  current_dir="$(pwd)"
29  required_dir="$1"
30  if [ "${current_dir##*/}" != "${required_dir}" ]; then
31    elog "This script must be run from the ${required_dir} directory."
32    exit 1
33  fi
34}
35
36# Terminates the script when $1 is not in $PATH. Any arguments required for
37# the tool being tested to return a successful exit code can be passed as
38# additional arguments.
39check_tool() {
40  tool="$1"
41  shift
42  tool_args="$@"
43  if ! eval "${tool}" ${tool_args} > /dev/null 2>&1; then
44    elog "${tool} must be in your path."
45    exit 1
46  fi
47}
48
49# Echoes git describe output for the directory specified by $1 to stdout.
50git_describe() {
51  git_dir="$1"
52  check_git
53  echo $(git -C "${git_dir}" describe)
54}
55
56# Echoes current git revision for the directory specifed by $1 to stdout.
57git_revision() {
58  git_dir="$1"
59  check_git
60  echo $(git -C "${git_dir}" rev-parse HEAD)
61}
62
63