|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================== |
| 16 | +# |
| 17 | +# Common Bash functions used by build scripts |
| 18 | + |
| 19 | +COLOR_NC='\033[0m' |
| 20 | +COLOR_BOLD='\033[1m' |
| 21 | +COLOR_LIGHT_GRAY='\033[0;37m' |
| 22 | +COLOR_GREEN='\033[0;32m' |
| 23 | +COLOR_RED='\033[0;31m' |
| 24 | + |
| 25 | +die() { |
| 26 | + # Print a message and exit with code 1. |
| 27 | + # |
| 28 | + # Usage: die <error_message> |
| 29 | + # e.g., die "Something bad happened." |
| 30 | + |
| 31 | + echo $@ |
| 32 | + exit 1 |
| 33 | +} |
| 34 | + |
| 35 | +num_cpus() { |
| 36 | + # Get the number of CPUs |
| 37 | + N_CPUS=$(grep -c ^processor /proc/cpuinfo) |
| 38 | + if [[ -z ${N_CPUS} ]]; then |
| 39 | + die "ERROR: Unable to determine the number of CPUs" |
| 40 | + fi |
| 41 | + |
| 42 | + echo ${N_CPUS} |
| 43 | +} |
| 44 | + |
| 45 | +# List files changed (i.e., added, or revised). |
| 46 | +# Usage: get_changed_files_from_master_branch |
| 47 | +get_changed_files_from_master_branch() { |
| 48 | + git diff origin/master --diff-filter=d --name-only "$@" |
| 49 | +} |
| 50 | + |
| 51 | +# List bazel files changed that still exist, |
| 52 | +# i.e., not removed. |
| 53 | +# Usage: get_bazel_files_to_check [--incremental] |
| 54 | +get_bazel_files_to_check() { |
| 55 | + if [[ "$1" == "--incremental" ]]; then |
| 56 | + get_changed_files_from_master_branch -- 'BUILD*' |
| 57 | + elif [[ -z "$1" ]]; then |
| 58 | + find . -name 'BUILD*' |
| 59 | + else |
| 60 | + die "Found unsupported args: $@ for get_bazel_files_to_check." |
| 61 | + fi |
| 62 | +} |
| 63 | + |
| 64 | +# List python files changed that still exist, |
| 65 | +# i.e., not removed. |
| 66 | +# Usage: get_py_files_to_check [--incremental] |
| 67 | +get_py_files_to_check() { |
| 68 | + if [[ "$1" == "--incremental" ]]; then |
| 69 | + get_changed_files_from_master_branch -- '*.py' |
| 70 | + elif [[ -z "$1" ]]; then |
| 71 | + find . -name '*.py' |
| 72 | + else |
| 73 | + die "Found unsupported args: $@ for get_py_files_to_check." |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +# List .h|.cc files changed that still exist, |
| 78 | +# i.e., not removed. |
| 79 | +# Usage: get_clang_files_to_check [--incremental] |
| 80 | +get_clang_files_to_check() { |
| 81 | + if [[ "$1" == "--incremental" ]]; then |
| 82 | + get_changed_files_from_master_branch -- '*.h' '*.cc' |
| 83 | + elif [[ -z "$1" ]]; then |
| 84 | + find . -name '*.h' -o -name '*.cc' |
| 85 | + else |
| 86 | + die "Found unsupported args: $@ for get_clang_files_to_check." |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +realpath() { |
| 91 | + # Get the real path of a file |
| 92 | + # Usage: realpath <file_path> |
| 93 | + |
| 94 | + if [[ $# != "1" ]]; then |
| 95 | + die "realpath: incorrect usage" |
| 96 | + fi |
| 97 | + |
| 98 | + [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}" |
| 99 | +} |
| 100 | + |
| 101 | +to_lower () { |
| 102 | + # Convert string to lower case. |
| 103 | + # Usage: to_lower <string> |
| 104 | + |
| 105 | + echo "$1" | tr '[:upper:]' '[:lower:]' |
| 106 | +} |
| 107 | + |
| 108 | +calc_elapsed_time() { |
| 109 | + # Calculate elapsed time. Takes nanosecond format input of the kind output |
| 110 | + # by date +'%s%N' |
| 111 | + # |
| 112 | + # Usage: calc_elapsed_time <START_TIME> <END_TIME> |
| 113 | + |
| 114 | + if [[ $# != "2" ]]; then |
| 115 | + die "calc_elapsed_time: incorrect usage" |
| 116 | + fi |
| 117 | + |
| 118 | + START_TIME=$1 |
| 119 | + END_TIME=$2 |
| 120 | + |
| 121 | + if [[ ${START_TIME} == *"N" ]]; then |
| 122 | + # Nanosecond precision not available |
| 123 | + START_TIME=$(echo ${START_TIME} | sed -e 's/N//g') |
| 124 | + END_TIME=$(echo ${END_TIME} | sed -e 's/N//g') |
| 125 | + ELAPSED="$(expr ${END_TIME} - ${START_TIME}) s" |
| 126 | + else |
| 127 | + ELAPSED="$(expr $(expr ${END_TIME} - ${START_TIME}) / 1000000) ms" |
| 128 | + fi |
| 129 | + |
| 130 | + echo ${ELAPSED} |
| 131 | +} |
0 commit comments