diff options
author | Manuel Bentele | 2021-03-04 08:47:43 +0100 |
---|---|---|
committer | Manuel Bentele | 2021-03-04 08:47:43 +0100 |
commit | 2b5631eaa444023043a18ff3985f5a0e7a11436a (patch) | |
tree | 4f27eaf01b80eea60d5f9b65023c8db2635ec8b5 /cmake | |
parent | [BUILD] Add build support for Github actions (diff) | |
download | dnbd3-2b5631eaa444023043a18ff3985f5a0e7a11436a.tar.gz dnbd3-2b5631eaa444023043a18ff3985f5a0e7a11436a.tar.xz dnbd3-2b5631eaa444023043a18ff3985f5a0e7a11436a.zip |
[BUILD] Add support in CMake to validate (lint) the source code
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/FindClangFormat.cmake | 21 | ||||
-rw-r--r-- | cmake/Lint.cmake | 49 |
2 files changed, 70 insertions, 0 deletions
diff --git a/cmake/FindClangFormat.cmake b/cmake/FindClangFormat.cmake new file mode 100644 index 0000000..9abf943 --- /dev/null +++ b/cmake/FindClangFormat.cmake @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2021 Manuel Bentele <development@manuel-bentele.de> +# + +find_program(ClangFormat_EXECUTABLE NAMES clang-format) + +if(ClangFormat_EXECUTABLE) + execute_process(COMMAND clang-format --version + OUTPUT_VARIABLE ClangFormat_VERBOSE_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE) + string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" ClangFormat_VERSION ${ClangFormat_VERBOSE_VERSION}) +endif(ClangFormat_EXECUTABLE) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(ClangFormat + FOUND_VAR ClangFormat_FOUND + REQUIRED_VARS ClangFormat_EXECUTABLE + VERSION_VAR ClangFormat_VERSION + FAIL_MESSAGE "clang-format is not available! Please install clang-format to format the source code!") + diff --git a/cmake/Lint.cmake b/cmake/Lint.cmake new file mode 100644 index 0000000..73b4795 --- /dev/null +++ b/cmake/Lint.cmake @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# CMake macros to check style of source code files +# Copyright (C) 2021 Manuel Bentele <development@manuel-bentele.de> +# + +find_package(ClangFormat REQUIRED) + +# add target to trigger all linter targets +add_custom_target(lint) + +# macro to define lint targets +macro(add_linter LINT_NAME LINT_SOURCE_FILES) + add_custom_target(${LINT_NAME} + COMMAND clang-format --Werror --dry-run ${LINT_SOURCE_FILES} ${ARGN} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + DEPENDS ${LINT_SOURCE_FILES} ${ARGN}) + add_dependencies(lint ${LINT_NAME}) +endmacro(add_linter) + +# macro to define lint targets for kernel source code files +macro(add_kernel_linter LINT_NAME KERNEL_BUILD_DIR LINT_SOURCE_FILES LINT_HEADER_FILES) + add_custom_target(${LINT_NAME} + COMMAND ${KERNEL_BUILD_DIR}/scripts/checkpatch.pl --no-tree -f ${LINT_SOURCE_FILES} ${LINT_HEADER_FILES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + DEPENDS ${LINT_SOURCE_FILES} ${LINT_HEADER_FILES}) + add_dependencies(lint ${LINT_NAME}) +endmacro(add_kernel_linter) + +# add target to trigger all formatter targets +add_custom_target(lint-fix) + +# macro to define formatter targets +macro(add_linter_fix LINT_FIX_NAME LINT_FIX_SOURCE_FILES) + add_custom_target(${LINT_FIX_NAME} + COMMAND clang-format --Werror -i ${LINT_FIX_SOURCE_FILES} ${ARGN} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + DEPENDS ${LINT_FIX_SOURCE_FILES} ${ARGN}) + add_dependencies(lint-fix ${LINT_FIX_NAME}) +endmacro(add_linter_fix) + +# macro to define formatter targets for kernel source code files +macro(add_kernel_linter_fix LINT_FIX_NAME KERNEL_BUILD_DIR LINT_FIX_SOURCE_FILES LINT_FIX_HEADER_FILES) + add_custom_target(${LINT_FIX_NAME} + COMMAND ${KERNEL_BUILD_DIR}/scripts/checkpatch.pl --no-tree --fix-inplace -f ${LINT_FIX_SOURCE_FILES} ${LINT_FIX_HEADER_FILES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + DEPENDS ${LINT_FIX_SOURCE_FILES} ${LINT_FIX_HEADER_FILES}) + add_dependencies(lint-fix ${LINT_FIX_NAME}) +endmacro(add_kernel_linter_fix) |