Commit 8e6d8a99 authored by Robert Kimball's avatar Robert Kimball Committed by Scott Cyphers

Use bash for Linux and Apple style (#3168)

* use bash method for style on Linux and Apple because it is faster

* cleanup

* address PR review comment

* make scripts executable
parent 67cf8128
......@@ -411,17 +411,31 @@ if (NOT DEFINED NGRAPH_TBB_ENABLE)
set(NGRAPH_TBB_ENABLE ${NGRAPH_CPU_ENABLE})
endif()
add_custom_target(style-check
COMMAND ${CMAKE_COMMAND}
-DNGRAPH_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
-P ${CMAKE_MODULE_PATH}style_check.cmake
)
# Since UNIX and APPLE support Bash we can use a Bash script to do the clang-format functions
# This is much faster than the cmake method
if (UNIX OR APPLE)
add_custom_target(style-check COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/maint/check-code-format.sh)
add_custom_target(style-apply COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/maint/apply-code-format.sh)
add_custom_target(style COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/maint/apply-code-format.sh)
else()
add_custom_target(style-check
COMMAND ${CMAKE_COMMAND}
-DNGRAPH_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
-P ${CMAKE_MODULE_PATH}style_check.cmake
)
add_custom_target(style-apply
COMMAND ${CMAKE_COMMAND}
-DNGRAPH_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
-P ${CMAKE_MODULE_PATH}style_apply.cmake
)
add_custom_target(style-apply
COMMAND ${CMAKE_COMMAND}
-DNGRAPH_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
-P ${CMAKE_MODULE_PATH}style_apply.cmake
)
add_custom_target(style
COMMAND ${CMAKE_COMMAND}
-DNGRAPH_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
-P ${CMAKE_MODULE_PATH}style_apply.cmake
)
endif()
add_custom_target(fix-mode
COMMAND ${CMAKE_COMMAND}
......
#!/bin/bash
set -e
set -u
# ******************************************************************************
# Copyright 2017-2019 Intel Corporation
#
# 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.
# ******************************************************************************
# NOTE: The results of `clang-format` depend _both_ of the following factors:
# - The `.clang-format` file, and
# - The particular version of the `clang-format` program being used.
#
# 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
bash_lib_die "Unable to find program ${CLANG_FORMAT_BASENAME}" >&2
fi
clang_format_lib_verify_version "${CLANG_FORMAT_PROG}" "${REQUIRED_CLANG_FORMAT_VERSION}"
echo "Verified that '${CLANG_FORMAT_PROG}' has version '${REQUIRED_CLANG_FORMAT_VERSION}'"
pushd "${THIS_SCRIPT_DIR}/.."
declare ROOT_SUBDIR
for ROOT_SUBDIR in src test python/pyngraph; do
if ! [[ -d "${ROOT_SUBDIR}" ]]; then
echo "In directory '$(pwd)', no subdirectory named '${ROOT_SUBDIR}' was found."
else
echo "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."
fi
done
popd
#!/bin/bash
# ******************************************************************************
# Copyright 2017-2019 Intel Corporation
#
# 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
#!/bin/bash
set -e
set -u
# ******************************************************************************
# Copyright 2017-2019 Intel Corporation
#
# 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.
# ******************************************************************************
# NOTE: The results of `clang-format` depend _both_ of the following factors:
# - The `.clang-format` file, and
# - The particular version of the `clang-format` program being used.
#
# 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
bash_lib_die "Unable to find program ${CLANG_FORMAT_BASENAME}" >&2
fi
clang_format_lib_verify_version "${CLANG_FORMAT_PROG}" "${REQUIRED_CLANG_FORMAT_VERSION}"
echo "Verified that '${CLANG_FORMAT_PROG}' has version '${REQUIRED_CLANG_FORMAT_VERSION}'"
declare -a FAILED_FILES=()
declare NUM_FILES_CHECKED=0
pushd "${THIS_SCRIPT_DIR}/.."
declare PYBIND_WRAPPER="python/pyngraph"
declare ROOT_SUBDIR
for ROOT_SUBDIR in src test python/pyngraph; do
if ! [[ -d "${ROOT_SUBDIR}" ]]; then
echo "In directory '$(pwd)', no subdirectory named '${ROOT_SUBDIR}' was found."
else
echo "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
# mechanism, and this confuses clang-format.
for SRC_FILE in $(find "${ROOT_SUBDIR}" -type f -and \( -name '*.cpp' -or -name '*.hpp' \) ); do
if "${CLANG_FORMAT_PROG}" -style=file -output-replacements-xml "${SRC_FILE}" | grep -c "<replacement " >/dev/null; then
FAILED_FILES+=( "${SRC_FILE}" )
fi
NUM_FILES_CHECKED=$((NUM_FILES_CHECKED+1))
done
fi
done
popd
if [[ ${#FAILED_FILES[@]} -eq 0 ]]; then
echo "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
for FAILED_SRC_FILE in ${FAILED_FILES[@]}; do
echo " ${FAILED_SRC_FILE}"
done
exit 1
fi
#!/bin/bash
# ******************************************************************************
# Copyright 2017-2019 Intel Corporation
#
# 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 (number).(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 SED_FLAGS
if [[ "$(uname)" == 'Darwin' ]]; then
SED_FLAGS='-En'
else
SED_FLAGS='-rn'
fi
local VERSION_X_Y
if ! VERSION_X_Y=$(echo "${VERSION_LINE}" | sed ${SED_FLAGS} '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