Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmake scripts for cpplint and clang-format #3440

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ exclude=
# generated files
demos/speech_recognition_deepspeech_demo/python/ctcdecode-numpy/ctcdecode_numpy/impl.py,
models/public/mozilla-deepspeech-0.8.2/mds_convert_utils/memmapped_file_system_pb2.py,
# 3rd party files
cmake/cpplint/cpplint.py

select=
# flake8-comprehensions issues
Expand Down
3 changes: 2 additions & 1 deletion ci/check-basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ def complain(message):
all_passed = False

print('running bandit...', flush=True)
if subprocess.run([sys.executable, '-m', 'bandit', '.', '-r', '-c', '.bandit'], cwd=OMZ_ROOT).returncode != 0:
if subprocess.run([sys.executable, '-m', 'bandit', '.', '-r', '-c', '.bandit',
'--exclude', './cmake/cpplint/cpplint.py'], cwd=OMZ_ROOT).returncode != 0:
all_passed = False

print('running documentation checks...', flush=True)
Expand Down
16 changes: 16 additions & 0 deletions cmake/CMakeScriptsConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

cmake_minimum_required(VERSION 3.10)

if(NOT DEFINED CMakeScripts_DIR)
message(FATAL_ERROR "CMakeScripts_DIR is not defined")
endif()

set(OLD_CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH})
set(CMAKE_MODULE_PATH "${CMakeScripts_DIR}")

# Code style utils
include(cpplint/cpplint)
include(clang_format/clang_format)
130 changes: 130 additions & 0 deletions cmake/clang_format/clang_format.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

if(ENABLE_CLANG_FORMAT)
set(CLANG_FORMAT_FILENAME "clang-format" "clang-format-10")
message(STATUS "Detecting ClangFormat")
find_program(CLANG_FORMAT NAMES ${CLANG_FORMAT_FILENAME} PATHS ENV PATH)
if(CLANG_FORMAT)
execute_process(COMMAND ${CLANG_FORMAT} ${CMAKE_CURRENT_SOURCE_DIR} ARGS --version OUTPUT_VARIABLE CLANG_VERSION)
if(NOT CLANG_VERSION OR CLANG_VERSION STREQUAL "")
message(WARNING "Supported clang-format version is 10!")
set(ENABLE_CLANG_FORMAT OFF)
else()
string(REGEX REPLACE "[^0-9]+([0-9]+)\\..*" "\\1" CLANG_FORMAT_MAJOR_VERSION ${CLANG_VERSION})
if(NOT ${CLANG_FORMAT_MAJOR_VERSION} EQUAL "10")
message(WARNING "Supported clang-format version is 10!")
set(ENABLE_CLANG_FORMAT OFF)
endif()
message(STATUS "Detecting ClangFormat- done")
endif()
else()
message(WARNING "Supported clang-format version not found!")
set(ENABLE_CLANG_FORMAT OFF)
endif()
endif()

if(ENABLE_CLANG_FORMAT AND NOT TARGET clang_format_check_all)
add_custom_target(clang_format_check_all)
add_custom_target(clang_format_fix_all)
set_target_properties(clang_format_check_all clang_format_fix_all
PROPERTIES FOLDER clang_format)
set(CLANG_FORMAT_ALL_OUTPUT_FILES "" CACHE INTERNAL "All clang-format output files")
endif()

function(add_clang_format_target TARGET_NAME)
if(NOT ENABLE_CLANG_FORMAT)
return()
endif()
set(options ALL)
set(oneValueArgs "")
set(multiValueArgs "FOR_TARGETS" "FOR_SOURCES" "EXCLUDE_PATTERNS")
cmake_parse_arguments(CLANG_FORMAT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

foreach(target IN LISTS CLANG_FORMAT_FOR_TARGETS)
get_target_property(target_sources "${target}" SOURCES)
list(APPEND CLANG_FORMAT_FOR_SOURCES ${target_sources})
endforeach()

list(REMOVE_DUPLICATES CLANG_FORMAT_FOR_SOURCES)

set(all_output_files "")
foreach(source_file IN LISTS CLANG_FORMAT_FOR_SOURCES)
set(exclude FALSE)
foreach(pattern IN LISTS CLANG_FORMAT_EXCLUDE_PATTERNS)
if(source_file MATCHES "${pattern}")
set(exclude ON)
break()
endif()
endforeach()

if(exclude)
continue()
endif()

# ignore object libraries
if(NOT EXISTS "${source_file}")
continue()
endif()

file(RELATIVE_PATH source_file_relative "${CMAKE_CURRENT_SOURCE_DIR}" "${source_file}")
set(output_file "${CMAKE_CURRENT_BINARY_DIR}/clang_format/${source_file_relative}.clang")
string(REPLACE ".." "__" output_file "${output_file}")
get_filename_component(output_dir "${output_file}" DIRECTORY)
file(MAKE_DIRECTORY "${output_dir}")

add_custom_command(
OUTPUT
"${output_file}"
COMMAND
"${CMAKE_COMMAND}"
-D "CLANG_FORMAT=${CLANG_FORMAT}"
-D "INPUT_FILE=${source_file}"
-D "OUTPUT_FILE=${output_file}"
-P "${CMakeScripts_DIR}/clang_format/clang_format_check.cmake"
DEPENDS
"${source_file}"
"${CMakeScripts_DIR}/clang_format/clang_format_check.cmake"
COMMENT
"[clang-format] ${source_file}"
VERBATIM)

list(APPEND all_output_files "${output_file}")
endforeach()

set(CLANG_FORMAT_ALL_OUTPUT_FILES
${CLANG_FORMAT_ALL_OUTPUT_FILES} ${all_output_files}
CACHE INTERNAL
"All clang-format output files")

add_custom_target(${TARGET_NAME}
DEPENDS ${all_output_files}
COMMENT "[clang-format] ${TARGET_NAME}")

add_custom_target(${TARGET_NAME}_fix
COMMAND
"${CMAKE_COMMAND}"
-D "CLANG_FORMAT=${CLANG_FORMAT}"
-D "INPUT_FILES=${CLANG_FORMAT_FOR_SOURCES}"
-D "EXCLUDE_PATTERNS=${CLANG_FORMAT_EXCLUDE_PATTERNS}"
-P "${CMakeScripts_DIR}/clang_format/clang_format_fix.cmake"
DEPENDS
"${CLANG_FORMAT_FOR_SOURCES}"
"${CMakeScripts_DIR}/clang_format/clang_format_fix.cmake"
COMMENT
"[clang-format] ${TARGET_NAME}_fix"
VERBATIM)

set_target_properties(${TARGET_NAME} ${TARGET_NAME}_fix
PROPERTIES FOLDER clang_format)

if(CLANG_FORMAT_FOR_TARGETS)
foreach(target IN LISTS CLANG_FORMAT_FOR_TARGETS)
add_dependencies(${target} ${TARGET_NAME}_fix)
endforeach()
endif()

add_dependencies(clang_format_check_all ${TARGET_NAME})
add_dependencies(clang_format_fix_all ${TARGET_NAME}_fix)
endfunction()
17 changes: 17 additions & 0 deletions cmake/clang_format/clang_format_check.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

file(REMOVE "${OUTPUT_FILE}")

execute_process(COMMAND ${CLANG_FORMAT} -style=file -output-replacements-xml ${INPUT_FILE}
OUTPUT_VARIABLE STYLE_CHECK_RESULT
)

file(WRITE "${OUTPUT_FILE}" "${STYLE_CHECK_RESULT}")

if(NOT SKIP_RETURN_CODE)
if("${STYLE_CHECK_RESULT}" MATCHES ".*<replacement .*")
message(FATAL_ERROR "[clang-format] Code style check failed for: ${INPUT_FILE}")
endif()
endif()
24 changes: 24 additions & 0 deletions cmake/clang_format/clang_format_fix.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

function(style_apply_file INPUT_FILE)
execute_process(COMMAND ${CLANG_FORMAT} -style=file -i ${INPUT_FILE}
OUTPUT_VARIABLE STYLE_CHECK_RESULT)
endfunction()

foreach(source_file IN LISTS INPUT_FILES)
set(exclude FALSE)
foreach(pattern IN LISTS EXCLUDE_PATTERNS)
if(source_file MATCHES "${pattern}")
set(exclude ON)
break()
endif()
endforeach()

if(exclude)
continue()
endif()

style_apply_file(${source_file})
endforeach()
107 changes: 107 additions & 0 deletions cmake/cpplint/cpplint.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

if(ENABLE_CPPLINT)
find_package(PythonInterp 3 QUIET)

if(NOT PYTHONINTERP_FOUND)
message(WARNING "Python3 interpreter was not found (required for cpplint check)")
set(ENABLE_CPPLINT OFF)
endif()
endif()

if(ENABLE_CPPLINT AND NOT TARGET cpplint_all)
add_custom_target(cpplint_all ALL)
set_target_properties(cpplint_all PROPERTIES FOLDER cpplint)
set(CPPLINT_ALL_OUTPUT_FILES "" CACHE INTERNAL "All cpplint output files")
endif()

function(add_cpplint_target TARGET_NAME)
if(NOT ENABLE_CPPLINT)
return()
endif()

set(options "")
set(oneValueArgs "")
set(multiValueArgs FOR_TARGETS FOR_SOURCES EXCLUDE_PATTERNS CUSTOM_FILTERS)
cmake_parse_arguments(CPPLINT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

foreach(target IN LISTS CPPLINT_FOR_TARGETS)
get_target_property(target_sources "${target}" SOURCES)
list(APPEND CPPLINT_FOR_SOURCES ${target_sources})
endforeach()
list(REMOVE_DUPLICATES CPPLINT_FOR_SOURCES)

set(custom_filter "")
foreach(filter IN LISTS CPPLINT_CUSTOM_FILTERS)
string(CONCAT custom_filter "${custom_filter}" "," "${filter}")
endforeach()

set(all_output_files "")
foreach(source_file IN LISTS CPPLINT_FOR_SOURCES)
set(exclude FALSE)
foreach(pattern IN LISTS CPPLINT_EXCLUDE_PATTERNS)
if(source_file MATCHES "${pattern}")
set(exclude ON)
break()
endif()
endforeach()

if(exclude)
continue()
endif()

# ignore object libraries
if(NOT EXISTS "${source_file}")
continue()
endif()

file(RELATIVE_PATH source_file_relative "${CMAKE_CURRENT_SOURCE_DIR}" "${source_file}")
set(output_file "${CMAKE_CURRENT_BINARY_DIR}/cpplint/${source_file_relative}.cpplint")
string(REPLACE ".." "__" output_file "${output_file}")
get_filename_component(output_dir "${output_file}" DIRECTORY)
file(MAKE_DIRECTORY "${output_dir}")

add_custom_command(
OUTPUT
"${output_file}"
COMMAND
"${CMAKE_COMMAND}"
-D "PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}"
-D "CPPLINT_SCRIPT=${CMakeScripts_DIR}/cpplint/cpplint.py"
-D "INPUT_FILE=${source_file}"
-D "OUTPUT_FILE=${output_file}"
-D "WORKING_DIRECTORY=${CMAKE_CURRENT_SOURCE_DIR}"
-D "SKIP_RETURN_CODE=${ENABLE_CPPLINT_REPORT}"
-D "CUSTOM_FILTER=${custom_filter}"
-P "${CMakeScripts_DIR}/cpplint/cpplint_run.cmake"
DEPENDS
"${source_file}"
"${CMakeScripts_DIR}/cpplint/cpplint.py"
"${CMakeScripts_DIR}/cpplint/cpplint_run.cmake"
COMMENT
"[cpplint] ${source_file}"
VERBATIM)

list(APPEND all_output_files "${output_file}")
endforeach()

set(CPPLINT_ALL_OUTPUT_FILES
${CPPLINT_ALL_OUTPUT_FILES} ${all_output_files}
CACHE INTERNAL
"All cpplint output files")

add_custom_target(${TARGET_NAME} ALL
DEPENDS ${all_output_files}
COMMENT "[cpplint] ${TARGET_NAME}")
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER cpplint)

if(CPPLINT_FOR_TARGETS)
foreach(target IN LISTS CPPLINT_FOR_TARGETS)
add_dependencies(${target} ${TARGET_NAME})
endforeach()
endif()

add_dependencies(cpplint_all ${TARGET_NAME})
endfunction()
Loading