Commit a5353cc0 authored by Christian Convey's avatar Christian Convey Committed by Adam Procter

Adds clang-format version checking. (#168)

parent 6776d7b9
......@@ -22,30 +22,37 @@ set -u
# For this reason, this script specifies the exact version of clang-format to be used.
declare CLANG_FORMAT_BASENAME="clang-format-3.9"
declare REQUIRED_CLANG_FORMAT_VERSION=3.9
declare THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${THIS_SCRIPT_DIR}/bash_lib.sh"
source "${THIS_SCRIPT_DIR}/clang_format_lib.sh"
declare CLANG_FORMAT_PROG
if ! CLANG_FORMAT_PROG="$(which "${CLANG_FORMAT_BASENAME}")"; then
echo "Unable to find program ${CLANG_FORMAT_BASENAME}" >&2
exit 1
bash_lib_die "Unable to find program ${CLANG_FORMAT_BASENAME}" >&2
fi
clang_format_lib_verify_version "${CLANG_FORMAT_PROG}" "${REQUIRED_CLANG_FORMAT_VERSION}"
bash_lib_status "Verified that '${CLANG_FORMAT_PROG}' has version '${REQUIRED_CLANG_FORMAT_VERSION}'"
pushd "${THIS_SCRIPT_DIR}/.."
declare ROOT_SUBDIR
for ROOT_SUBDIR in src test; do
if ! [[ -d "${ROOT_SUBDIR}" ]]; then
echo "In directory '$(pwd)', no subdirectory named '${ROOT_SUBDIR}' was found." >&2
exit 1
bash_lib_die "In directory '$(pwd)', no subdirectory named '${ROOT_SUBDIR}' was found."
fi
echo "About to format C/C++ code in directory tree '$(pwd)/${ROOT_SUBDIR}' ..."
bash_lib_status "About to format C/C++ code in directory tree '$(pwd)/${ROOT_SUBDIR}' ..."
# Note that we restrict to "-type f" to exclude symlinks. Emacs sometimes
# creates dangling symlinks with .cpp/.hpp suffixes as a sort of locking
# mechanism, and this confuses clang-format.
find "${ROOT_SUBDIR}" -type f -and \( -name '*.cpp' -or -name '*.hpp' \) | xargs "${CLANG_FORMAT_PROG}" -i -style=file
echo "Done."
bash_lib_status "Done."
done
popd
......
#!/bin/bash
# Copyright 2017 Nervana Systems Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===================================================================================================
# A library of general-purpose Bash functions
#===================================================================================================
declare _intelnervana_bash_lib_SCRIPT_NAME="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}"
declare _maint_SCRIPT_DIR="$( cd $(dirname "${_intelnervana_bash_lib_SCRIPT_NAME}") && pwd )"
declare _intelnervana_bash_lib_IS_LOADED=1
bash_lib_get_my_BASH_LINENO() {
echo "${BASH_LINENO[${#BASH_LINENO[@]} -1 ]}"
}
bash_lib_get_callers_BASH_LINENO() {
echo "${BASH_LINENO[${#BASH_LINENO[@]} - 2]}"
}
bash_lib_get_my_BASH_SOURCE() {
echo "${BASH_SOURCE[${#BASH_SOURCE[@]} ]}"
}
bash_lib_get_callers_BASH_SOURCE() {
echo "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}"
}
bash_lib_status() {
local CONTEXT_STRING="$(basename $(bash_lib_get_callers_BASH_SOURCE))"
local TEXT_LINE
local IS_FIRST_LINE=1
for TEXT_LINE in "${@}"; do
if (( IS_FIRST_LINE == 1 )); then
IS_FIRST_LINE=0
printf "%s STATUS: " "${CONTEXT_STRING}" >&2
else
printf " " >&2
fi
printf "%s\n" "${TEXT_LINE}" >&2
done
}
bash_lib_print_error() {
local CONTEXT_STRING="$(basename $(bash_lib_get_callers_BASH_SOURCE)):$(bash_lib_get_callers_BASH_LINENO)"
local TEXT_LINE
local IS_FIRST_LINE=1
for TEXT_LINE in "${@}"; do
if (( IS_FIRST_LINE == 1 )); then
IS_FIRST_LINE=0
printf "%s ERROR: " "${CONTEXT_STRING}" >&2
else
printf " " >&2
fi
printf "%s\n" "${TEXT_LINE}" >&2
done
}
bash_lib_die() {
bash_lib_print_error $@
exit 1
}
bash_lib_am_sudo_or_root() {
[ "$EUID" -eq 0 ]
}
if bash_lib_am_sudo_or_root; then
bash_lib_MAYBE_SUDO=''
else
bash_lib_MAYBE_SUDO='sudo --set-home'
fi
......@@ -22,15 +22,21 @@ set -u
# For this reason, this script specifies the exact version of clang-format to be used.
declare CLANG_FORMAT_BASENAME="clang-format-3.9"
declare REQUIRED_CLANG_FORMAT_VERSION=3.9
declare THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${THIS_SCRIPT_DIR}/bash_lib.sh"
source "${THIS_SCRIPT_DIR}/clang_format_lib.sh"
declare CLANG_FORMAT_PROG
if ! CLANG_FORMAT_PROG="$(which "${CLANG_FORMAT_BASENAME}")"; then
echo "Unable to find program ${CLANG_FORMAT_BASENAME}" >&2
exit 1
bash_lib_die "Unable to find program ${CLANG_FORMAT_BASENAME}" >&2
fi
clang_format_lib_verify_version "${CLANG_FORMAT_PROG}" "${REQUIRED_CLANG_FORMAT_VERSION}"
bash_lib_status "Verified that '${CLANG_FORMAT_PROG}' has version '${REQUIRED_CLANG_FORMAT_VERSION}'"
declare -a FAILED_FILES=()
declare NUM_FILES_CHECKED=0
......@@ -39,11 +45,10 @@ pushd "${THIS_SCRIPT_DIR}/.."
declare ROOT_SUBDIR
for ROOT_SUBDIR in src test; do
if ! [[ -d "${ROOT_SUBDIR}" ]]; then
echo "In directory '$(pwd)', no subdirectory named '${ROOT_SUBDIR}' was found." >&2
exit 1
bash_lib_die "In directory '$(pwd)', no subdirectory named '${ROOT_SUBDIR}' was found."
fi
echo "About to format C/C++ code in directory tree '$(pwd)/${ROOT_SUBDIR}' ..."
bash_lib_status "About to format C/C++ code in directory tree '$(pwd)/${ROOT_SUBDIR}' ..."
declare SRC_FILE
# Note that we restrict to "-type f" to exclude symlinks. Emacs sometimes
# creates dangling symlinks with .cpp/.hpp suffixes as a sort of locking
......@@ -59,8 +64,7 @@ done
popd
if [[ ${#FAILED_FILES[@]} -eq 0 ]]; then
echo "All ${NUM_FILES_CHECKED} C/C++ files pass the code-format check."
exit 0
bash_lib_status "All ${NUM_FILES_CHECKED} C/C++ files pass the code-format check."
else
echo "${#FAILED_FILES[@]} of ${NUM_FILES_CHECKED} source files failed the code-format check:"
declare FAILED_SRC_FILE
......
#!/bin/bash
# Copyright 2017 Nervana Systems Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===================================================================================================
# Provides Bash functions for dealing with clang-format.
#===================================================================================================
declare _intelnervana_clang_format_lib_SCRIPT_NAME="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}"
declare _maint_SCRIPT_DIR="$( cd $(dirname "${_intelnervana_clang_format_lib_SCRIPT_NAME}") && pwd )"
source "${_maint_SCRIPT_DIR}/bash_lib.sh"
clang_format_lib_verify_version() {
if (( $# != 2 )); then
bash_lib_print_error "Usage: ${FUNCNAME[0]} <clang-format-prog-pathname> <required-version-number>"
return 1
fi
local PROGNAME="${1}"
local REQUIRED_VERSION_X_Y="${2}"
if ! [[ "${REQUIRED_VERSION_X_Y}" =~ ^[0-9]+.[0-9]+$ ]]; then
bash_lib_print_error "${FUNCNAME[0]}: required-version-number must have the form (numerber).(number)."
return 1
fi
if ! [[ -f "${PROGNAME}" ]]; then
bash_lib_print_error "Unable to find clang-format program named '${PROGNAME}'"
return 1
fi
local VERSION_LINE
if ! VERSION_LINE=$("${PROGNAME}" --version); then
bash_lib_print_error "Failed invocation of command '${PROGNAME} --version'"
return 1
fi
local VERSION_X_Y
if ! VERSION_X_Y=$(echo "${VERSION_LINE}" | sed -rn 's/^clang-format version ([0-9]+.[0-9]+).*$/\1/p')
then
bash_lib_print_error "Failed invocation of sed."
return 1
fi
if [[ "${REQUIRED_VERSION_X_Y}" != "${VERSION_X_Y}" ]]; then
bash_lib_print_error \
"Program '${PROGNAME}' reports version number '${VERSION_X_Y}'" \
"but we require '${REQUIRED_VERSION_X_Y}'"
return 1
fi
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment