INVALID_EXIT=0
-pushd () {
+function pushd () {
command pushd "$@" > /dev/null
}
-popd () {
+function popd () {
command popd "$@" > /dev/null
}
-command_exists() {
+function command_exists() {
command -v $1 > /dev/null 2>&1
}
-check_cpp_tool() {
- if ! command_exists clang-format-3.9; then
- echo "Error: clang-format-3.9 is not available."
- echo " Please install clang-format-3.9."
- exit 1
- fi
-}
-
-check_python_tool() {
- if ! command_exists yapf; then
- echo "Error: yapf is not available."
- echo " Please install yapf."
- exit 1
- fi
-}
-
-check_newline() {
+function check_newline() {
# Check all files (CMakeLists.txt, *.cl, ... not only for C++, Python)
FILES_TO_CHECK=$(git ls-files)
if [[ ${#FILES_TO_CHECK} -ne 0 ]]; then
- CRCHECK=$(file $FILES_TO_CHECK | grep 'with CR')
+ CRCHECK=$(file ${FILES_TO_CHECK} | grep 'with CR')
fi
FILES_TO_FIX=($(echo "$CRCHECK" | grep "with CRLF line" | cut -d':' -f1))
for f in ${FILES_TO_FIX[@]}; do
tr -d '\r' < $f > $f.fixed && cat $f.fixed > $f && rm $f.fixed
done
- FILES_TO_FIX=($(echo "$CRCHECK" | grep "with CR line" | cut -d':' -f1))
+ FILES_TO_FIX=($(echo "${CRCHECK}" | grep "with CR line" | cut -d':' -f1))
for f in ${FILES_TO_FIX[@]}; do
tr '\r' '\n' < $f > $f.fixed && cat $f.fixed > $f && rm $f.fixed
done
}
-check_permission() {
+function check_permission() {
# Check all files except script
FILES_TO_CHECK=()
- for NON_SCRIPT_FILE in $(git ls-files -- . ':!:nnas' ':!:nnfw' ':!:nncc' ':!:*.sh' ':!:*.py' ':!:compiler/' ':!:infra/nncc/' ':!:scripts/standalone'); do
+ for NON_SCRIPT_FILE in $(git ls-files -- . ':!:nnas' ':!:nnfw' ':!:nncc' ':!:*.sh' ':!:*.py' ':!:scripts/standalone'); do
FILES_TO_CHECK+=("${NON_SCRIPT_FILE}")
done
done
}
-check_cpp_files() {
+function check_cpp_files() {
+ if [[ ${__Check_CPP} -ne 0 ]]; then
+ return
+ fi
+
+ CLANG_FORMAT_CANDIDATES=()
+ CLANG_FORMAT_CANDIDATES+=("clang-format")
+ CLANG_FORMAT_CANDIDATES+=("clang-format-3.9")
+
+ for CLANG_FORMAT_CANDIDATE in ${CLANG_FORMAT_CANDIDATES[@]}; do
+ if command_exists ${CLANG_FORMAT_CANDIDATE} ; then
+ CLANG_FORMAT="${CLANG_FORMAT_CANDIDATE}"
+ fi
+ done
+
+ if [[ -z ${CLANG_FORMAT} ]]; then
+ echo "[ERROR] clang-format is unavailable"
+ echo
+ echo "Please install clang-format before running format check"
+ exit 1
+ fi
+
DIRECTORIES_NOT_TO_BE_TESTED=$1
# Check c++ files
- CPP_FILES_TO_CHECK=$(git ls-files '*.h' '*.cpp' '*.cc' '*.cl' ':!:runtimes/include/NeuralNetworks.h' ':!:compiler/*' ':!:res/*')
- ARR=($CPP_FILES_TO_CHECK)
+ CPP_FILES_TO_CHECK=$(git ls-files '*.h' '*.cpp' '*.cc' '*.c' '*.cl' ':!:runtimes/include/NeuralNetworks.h' ':!:compiler/*')
+ ARR=(${CPP_FILES_TO_CHECK})
for s in ${DIRECTORIES_NOT_TO_BE_TESTED[@]}; do
skip=${s#'.'/}/
ARR=(${ARR[*]//$skip*/})
done
CPP_FILES_TO_CHECK=${ARR[*]}
if [[ ${#CPP_FILES_TO_CHECK} -ne 0 ]]; then
- clang-format-3.9 -i $CPP_FILES_TO_CHECK
+ clang-format-3.9 -i ${CPP_FILES_TO_CHECK}
EXIT_CODE=$?
- if [[ $EXIT_CODE -ne 0 ]]; then
- INVALID_EXIT=$EXIT_CODE
+ if [[ ${EXIT_CODE} -ne 0 ]]; then
+ INVALID_EXIT=${EXIT_CODE}
fi
fi
}
-check_python_files() {
+function check_python_files() {
+ if [[ ${__Check_PYTHON} -ne 0 ]]; then
+ return
+ fi
+
+ if ! command_exists yapf; then
+ echo "[ERROR] yapf is unavailable"
+ echo " Please install yapf."
+ exit 1
+ fi
+
DIRECTORIES_NOT_TO_BE_TESTED=$1
# Check python files
PYTHON_FILES_TO_CHECK=$(git ls-files '*.py' ':!:compiler/*')
- ARR=($PYTHON_FILES_TO_CHECK)
+ ARR=(${PYTHON_FILES_TO_CHECK})
for s in ${DIRECTORIES_NOT_TO_BE_TESTED[@]}; do
skip=${s#'.'/}/
ARR=(${ARR[*]//$skip*/})
done
PYTHON_FILES_TO_CHECK=${ARR[*]}
if [[ ${#PYTHON_FILES_TO_CHECK} -ne 0 ]]; then
- yapf -i --style='{based_on_style: pep8, column_limit: 90}' $PYTHON_FILES_TO_CHECK
+ yapf -i --style='{based_on_style: pep8, column_limit: 90}' ${PYTHON_FILES_TO_CHECK}
EXIT_CODE=$?
- if [[ $EXIT_CODE -ne 0 ]]; then
- INVALID_EXIT=$EXIT_CODE
+ if [[ ${EXIT_CODE} -ne 0 ]]; then
+ INVALID_EXIT=${EXIT_CODE}
fi
fi
}
-echo "Make sure commit all changes before running this checker."
+pushd ${NNAS_PROJECT_PATH}
+
+if [ -n "$(git diff)" ]; then
+ echo "[ERROR] Commit all the changes before running format check"
+ exit 1
+fi
__Check_CPP=${CHECK_CPP:-"1"}
__Check_PYTHON=${CHECK_PYTHON:-"1"}
DIRECTORIES_NOT_TO_BE_TESTED=()
for DIR_NOT_TO_BE_TESTED in $(find -name '.FORMATDENY' -exec dirname {} \;); do
- DIRECTORIES_NOT_TO_BE_TESTED+=("$DIR_NOT_TO_BE_TESTED")
+ DIRECTORIES_NOT_TO_BE_TESTED+=("${DIR_NOT_TO_BE_TESTED}")
done
check_newline
check_permission
+check_cpp_files ${DIRECTORIES_NOT_TO_BE_TESTED}
+check_python_files ${DIRECTORIES_NOT_TO_BE_TESTED}
-if [[ $__Check_CPP -ne 0 ]]; then
- check_cpp_tool
- check_cpp_files $DIRECTORIES_NOT_TO_BE_TESTED
-fi
-
-if [[ $__Check_PYTHON -ne 0 ]]; then
- check_python_tool
- check_python_files $DIRECTORIES_NOT_TO_BE_TESTED
-fi
+DIFF=$(git diff | tee format.patch)
-git diff --ignore-submodules > format.patch
-PATCHFILE_SIZE=$(stat -c%s format.patch)
+popd
-if [[ -z "${CRCHECK}" ]] && [[ $PATCHFILE_SIZE -eq 0 ]] && [[ $INVALID_EXIT -eq 0 ]]; then
+if [[ -z "${CRCHECK}" ]] && [[ ! -n "${DIFF}" ]] && [[ ${INVALID_EXIT} -eq 0 ]]; then
echo "[PASSED] Format checker succeed."
return
fi
if [[ ! -z "${CRCHECK}" ]]; then
echo "[FAILED] Please use LF for newline for following files."
- echo "$CRCHECK"
+ echo "${CRCHECK}"
fi
-if [[ $PATCHFILE_SIZE -ne 0 ]]; then
+if [[ ${PATCHFILE_SIZE} -ne 0 ]]; then
echo "[FAILED] Format checker failed and update code to follow convention."
echo " You can find changes in format.patch"
fi
-if [[ $INVALID_EXIT -ne 0 ]]; then
+if [[ ${INVALID_EXIT} -ne 0 ]]; then
echo "[[FAILED] Invalid format checker exit."
fi
+
exit 1