Enable OV clang format command for nGraph (#4792)

* Added add_clang_format commands

* Added clang_format targets as dependencies for original targets

* Update CI scripts

* Fixed code-style for nGraph unit tests

* Change job name

* Disable clang-format for Windows

* Fixed nGraph python build

* Fixed comments and enabled code style for ONNX Editor

* Disable code style check on compilation step
This commit is contained in:
Ilya Churaev 2021-03-18 06:38:10 +03:00 committed by GitHub
parent 24c31fba6a
commit 1334d7d122
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 23 additions and 440 deletions

View File

@ -2,7 +2,7 @@ name: Code Style
on: [push, pull_request]
jobs:
nGraph:
clang-format:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
@ -24,20 +24,19 @@ jobs:
cmake ..
- name: Check code style
run: make style-check
working-directory: build
run: cmake --build build --target clang_format_check_all
- name: Create code style diff
if: failure()
run: |
ngraph/maint/apply-code-format.sh
git diff >ngraph_code_style_diff.patch
cmake --build build --target clang_format_fix_all
git diff > code_style_diff.diff
- uses: actions/upload-artifact@v2
if: failure()
with:
name: ngraph_code_style_diff
path: ngraph_code_style_diff.patch
name: code_style_diff
path: code_style_diff.diff
ShellCheck:
runs-on: ubuntu-18.04

View File

@ -50,7 +50,7 @@ if(NOT DEFINED ENABLE_CPPLINT_REPORT)
ie_dependent_option (ENABLE_CPPLINT_REPORT "Build cpplint report instead of failing the build" OFF "ENABLE_CPPLINT" OFF)
endif()
ie_option (ENABLE_CLANG_FORMAT "Enable clang-format checks during the build" ON)
ie_dependent_option (ENABLE_CLANG_FORMAT "Enable clang-format checks during the build" ON "UNIX;NOT ANDROID" OFF)
ie_option (VERBOSE_BUILD "shows extra information about build" OFF)

View File

@ -254,38 +254,6 @@ if(NGRAPH_ADDRESS_SANITIZER_ENABLE OR NGRAPH_THREAD_SANITIZER_ENABLE OR NGRAPH_U
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -g -fno-omit-frame-pointer")
endif()
# Since UNIX support Bash we can use a Bash script to do the clang-format functions
# This is much faster than the cmake method
if (UNIX)
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_CURRENT_SOURCE_DIR}"
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/style_check.cmake
)
add_custom_target(style-apply
COMMAND ${CMAKE_COMMAND}
-DNGRAPH_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/style_apply.cmake
)
add_custom_target(style
COMMAND ${CMAKE_COMMAND}
-DNGRAPH_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/style_apply.cmake
)
endif()
add_custom_target(fix-mode
COMMAND ${CMAKE_COMMAND}
-DNGRAPH_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/fix_mode.cmake
)
#-----------------------------------------------------------------------------------------------
# enable or disable output from NGRAPH_DEBUG statements
#-----------------------------------------------------------------------------------------------

View File

@ -1,43 +0,0 @@
# ******************************************************************************
# Copyright 2017-2021 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.
# ******************************************************************************
function(STYLE_APPLY_FILE PATH)
execute_process(COMMAND ${CLANG_FORMAT} -style=file -i ${PATH}
OUTPUT_VARIABLE STYLE_CHECK_RESULT)
endfunction()
set(DIRECTORIES_OF_INTEREST
frontend
core
doc
test
python/pyngraph
)
set(CLANG_FORMAT_FILENAME clang-format-9)
find_program(CLANG_FORMAT ${CLANG_FORMAT_FILENAME} PATHS ENV PATH)
if (CLANG_FORMAT)
foreach(DIRECTORY ${DIRECTORIES_OF_INTEREST})
set(DIR "${NGRAPH_SOURCE_DIR}/${DIRECTORY}/*.?pp")
file(GLOB_RECURSE XPP_FILES ${DIR})
foreach(FILE ${XPP_FILES})
style_apply_file(${FILE})
endforeach(FILE)
endforeach(DIRECTORY)
else()
message(STATUS "${CLANG_FORMAT_FILENAME} not found, style not available")
endif()

View File

@ -1,53 +0,0 @@
# ******************************************************************************
# Copyright 2017-2021 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.
# ******************************************************************************
set(CLANG_FORMAT_FILENAME clang-format-9)
find_program(CLANG_FORMAT ${CLANG_FORMAT_FILENAME} PATHS ENV PATH)
macro(STYLE_CHECK_FILE PATH)
execute_process(COMMAND ${CLANG_FORMAT} -style=file -output-replacements-xml ${PATH}
OUTPUT_VARIABLE STYLE_CHECK_RESULT
)
if("${STYLE_CHECK_RESULT}" MATCHES ".*<replacement .*")
list(APPEND ERROR_LIST ${PATH})
endif()
endmacro()
set(DIRECTORIES_OF_INTEREST
frontend
core
doc
test
python/pyngraph
)
if (CLANG_FORMAT)
foreach(DIRECTORY ${DIRECTORIES_OF_INTEREST})
set(DIR "${NGRAPH_SOURCE_DIR}/${DIRECTORY}/*.?pp")
file(GLOB_RECURSE XPP_FILES ${DIR})
foreach(FILE ${XPP_FILES})
style_check_file(${FILE})
endforeach(FILE)
endforeach(DIRECTORY)
if(ERROR_LIST)
foreach(ERROR ${ERROR_LIST})
message(STATUS "style error: ${ERROR}")
endforeach()
message(FATAL_ERROR "style errors")
endif()
else()
message(STATUS "${CLANG_FORMAT_FILENAME} not found, style not available")
endif()

View File

@ -55,6 +55,8 @@ target_link_libraries(ngraph PRIVATE openvino::itt ngraph::builder ngraph::refer
ie_mark_target_as_cc(ngraph)
add_clang_format_target(ngraph_clang FOR_TARGETS ngraph)
if(NGRAPH_ADDRESS_SANITIZER)
message(STATUS "Enable Address Sanitizer")
add_compile_options(-g -fsanitize=address -fno-omit-frame-pointer)

View File

@ -41,6 +41,8 @@ endif()
target_include_directories(${TARGET_NAME} PUBLIC ${BUILDER_INCLUDE_DIR}
PRIVATE ${NGRAPH_INCLUDE_PATH})
add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})
# Add an alias so that library can be used inside the build tree, e.g. when testing
add_library(ngraph::builder ALIAS ${TARGET_NAME})

View File

@ -44,6 +44,8 @@ target_include_directories(${TARGET_NAME} PUBLIC ${REF_IMPL_INCLUDE_DIR} ${NGRAP
target_link_libraries(${TARGET_NAME} PRIVATE xbyak)
add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})
# Add an alias so that library can be used inside the build tree, e.g. when testing
add_library(ngraph::reference ALIAS ${TARGET_NAME})

View File

@ -34,3 +34,5 @@ target_include_directories(${TARGET_NAME} PRIVATE ${ONNX_COMMON_SRC_DIR} ${NGRAP
if(NGRAPH_USE_PROTOBUF_LITE)
target_compile_definitions(${TARGET_NAME} PRIVATE NGRAPH_USE_PROTOBUF_LITE)
endif()
add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})

View File

@ -37,3 +37,5 @@ target_include_directories(${TARGET_NAME} PRIVATE ${ONNX_EDITOR_SRC_DIR} ${NGRAP
if(NGRAPH_USE_PROTOBUF_LITE)
target_compile_definitions(${TARGET_NAME} PRIVATE NGRAPH_USE_PROTOBUF_LITE)
endif()
add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})

View File

@ -47,6 +47,8 @@ source_group("public include" FILES ${LIBRARY_PUBLIC_HEADERS})
add_library(onnx_importer SHARED ${LIBRARY_SRC} ${LIBRARY_HEADERS} ${LIBRARY_PUBLIC_HEADERS})
add_library(ngraph::onnx_importer ALIAS onnx_importer)
add_clang_format_target(onnx_importer_clang FOR_TARGETS onnx_importer)
if(COMMAND ie_add_vs_version_file)
ie_add_vs_version_file(NAME onnx_importer
FILEDESCRIPTION "nGraph ONNX importer library")

View File

@ -1,61 +0,0 @@
#!/bin/bash
set -e
set -u
# ******************************************************************************
# Copyright 2017-2021 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-9"
declare REQUIRED_CLANG_FORMAT_VERSION=9.0
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 core test frontend python/src/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

View File

@ -1,90 +0,0 @@
#!/bin/bash
# ******************************************************************************
# Copyright 2017-2021 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

View File

@ -1,80 +0,0 @@
#!/bin/bash
set -e
set -u
# ******************************************************************************
# Copyright 2017-2021 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-9"
declare REQUIRED_CLANG_FORMAT_VERSION=9.0
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 core test frontend python/src/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

View File

@ -1,73 +0,0 @@
#!/bin/bash
# ******************************************************************************
# Copyright 2017-2021 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
}

View File

@ -96,6 +96,8 @@ if(OpenVINO_MAIN_SOURCE_DIR)
endif()
if(OpenVINO_MAIN_SOURCE_DIR OR InferenceEngineDeveloperPackage_FOUND)
add_clang_format_target(_${PROJECT_NAME}_clang FOR_TARGETS _${PROJECT_NAME})
ie_cpack_add_component(pyngraph_${PYTHON_VERSION})
install(TARGETS _${PROJECT_NAME}

View File

@ -376,6 +376,8 @@ if (NGRAPH_ONNX_IMPORT_ENABLE AND NOT NGRAPH_USE_PROTOBUF_LITE)
onnx/onnx_tensor_names.cpp)
endif()
add_clang_format_target(unit-test_clang FOR_SOURCES ${SRC} ${MULTI_TEST_SRC})
foreach(BACKEND_NAME ${ACTIVE_BACKEND_LIST})
string(TOLOWER ${BACKEND_NAME} BACKEND_DIR)
string(REGEX REPLACE "([a-z0-9]+):(.*)" "\\1" BACKEND_DIR ${BACKEND_DIR})