diff options
author | Manuel Bentele | 2021-03-15 08:33:04 +0100 |
---|---|---|
committer | Manuel Bentele | 2021-03-15 08:33:04 +0100 |
commit | dcf4cd421bc0f93d602da9c45357be036b7b8354 (patch) | |
tree | 92e2b3d93570f2cca07404a46b946dbbaf0a6740 /cmake | |
parent | Add build support for Github actions (diff) | |
download | xloop-dcf4cd421bc0f93d602da9c45357be036b7b8354.tar.gz xloop-dcf4cd421bc0f93d602da9c45357be036b7b8354.tar.xz xloop-dcf4cd421bc0f93d602da9c45357be036b7b8354.zip |
Add support in CMake to validate (lint) the source code
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/FindCheckPatch.cmake | 31 | ||||
-rw-r--r-- | cmake/FindClangFormat.cmake | 20 | ||||
-rw-r--r-- | cmake/Lint.cmake | 70 |
3 files changed, 121 insertions, 0 deletions
diff --git a/cmake/FindCheckPatch.cmake b/cmake/FindCheckPatch.cmake new file mode 100644 index 0000000..2772676 --- /dev/null +++ b/cmake/FindCheckPatch.cmake @@ -0,0 +1,31 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2021 Manuel Bentele <development@manuel-bentele.de> +# + +# check if custom Linux kernel script directory was specified +if(NOT KERNEL_SCRIPTS_DIR) + set(KERNEL_SCRIPTS_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build/scripts" + CACHE PATH "Path to Linux kernel scripts directory") +endif(NOT KERNEL_SCRIPTS_DIR) + +# find the checkpatch.pl script in the given KERNEL_SCRIPTS_DIR +find_program(CheckPatch_EXECUTABLE + NAMES checkpatch.pl + PATHS ${KERNEL_SCRIPTS_DIR}) + + +# get the checkpatch.pl version +if(CheckPatch_EXECUTABLE) + execute_process(COMMAND ${CheckPatch_EXECUTABLE} --version + OUTPUT_VARIABLE CheckPatch_VERBOSE_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE) + string(REGEX REPLACE ".*Version:.([0-9]+\\.[0-9]+).*" "\\1" CheckPatch_VERSION ${CheckPatch_VERBOSE_VERSION}) +endif(CheckPatch_EXECUTABLE) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(CheckPatch + FOUND_VAR CheckPatch_FOUND + REQUIRED_VARS CheckPatch_EXECUTABLE + VERSION_VAR CheckPatch_VERSION + FAIL_MESSAGE "checkpatch.pl is not available! Please install checkpatch.pl to lint and format the source code!") diff --git a/cmake/FindClangFormat.cmake b/cmake/FindClangFormat.cmake new file mode 100644 index 0000000..a6c77d4 --- /dev/null +++ b/cmake/FindClangFormat.cmake @@ -0,0 +1,20 @@ +# 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 lint and format the source code!") diff --git a/cmake/Lint.cmake b/cmake/Lint.cmake new file mode 100644 index 0000000..4453fe3 --- /dev/null +++ b/cmake/Lint.cmake @@ -0,0 +1,70 @@ +# 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) +find_package(CheckPatch) + +if(ClangFormat_FOUND OR CheckPatch_FOUND) + # add target to trigger all linter targets + add_custom_target(lint) +endif(ClangFormat_FOUND OR CheckPatch_FOUND) + +# macro to define lint targets +macro(add_linter LINT_NAME LINT_SOURCE_FILES) + if(ClangFormat_FOUND) + add_custom_target(${LINT_NAME} + COMMAND ${ClangFormat_EXECUTABLE} --Werror --dry-run ${LINT_SOURCE_FILES} ${ARGN} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + DEPENDS ${LINT_SOURCE_FILES} ${ARGN}) + add_dependencies(lint ${LINT_NAME}) + endif(ClangFormat_FOUND) +endmacro(add_linter) + +# macro to define lint targets for kernel source code files +macro(add_kernel_linter LINT_NAME LINT_IGNORE_OPTIONS LINT_SOURCE_FILES LINT_HEADER_FILES) + if(CheckPatch_FOUND) + set(LINT_IGNORE_ARGS "") + foreach(IGNORE_OPTION ${LINT_IGNORE_OPTIONS}) + list(APPEND LINT_IGNORE_ARGS "--ignore" "${IGNORE_OPTION}") + endforeach(IGNORE_OPTION ${LINT_IGNORE_OPTIONS}) + add_custom_target(${LINT_NAME} + COMMAND ${CheckPatch_EXECUTABLE} --no-tree --max-line-length=120 ${LINT_IGNORE_ARGS} -f ${LINT_SOURCE_FILES} ${LINT_HEADER_FILES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + DEPENDS ${LINT_SOURCE_FILES} ${LINT_HEADER_FILES}) + add_dependencies(lint ${LINT_NAME}) + endif(CheckPatch_FOUND) +endmacro(add_kernel_linter) + +if(ClangFormat_FOUND OR CheckPatch_FOUND) + # add target to trigger all formatter targets + add_custom_target(lint-fix) +endif(ClangFormat_FOUND OR CheckPatch_FOUND) + +# macro to define formatter targets +macro(add_linter_fix LINT_FIX_NAME LINT_FIX_SOURCE_FILES) + if(ClangFormat_FOUND) + add_custom_target(${LINT_FIX_NAME} + COMMAND ${ClangFormat_EXECUTABLE} --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}) + endif(ClangFormat_FOUND) +endmacro(add_linter_fix) + +# macro to define formatter targets for kernel source code files +macro(add_kernel_linter_fix LINT_FIX_NAME LINT_FIX_IGNORE_OPTIONS LINT_FIX_SOURCE_FILES LINT_FIX_HEADER_FILES) + if(CheckPatch_FOUND) + set(LINT_FIX_IGNORE_ARGS "") + foreach(IGNORE_OPTION ${LINT_FIX_IGNORE_OPTIONS}) + list(APPEND LINT_FIX_IGNORE_ARGS "--ignore" "${IGNORE_OPTION}") + endforeach(IGNORE_OPTION ${LINT_FIX_IGNORE_OPTIONS}) + add_custom_target(${LINT_FIX_NAME} + COMMAND ${CheckPatch_EXECUTABLE} --no-tree --max-line-length=120 ${LINT_FIX_IGNORE_ARGS} --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}) + endif(CheckPatch_FOUND) +endmacro(add_kernel_linter_fix) |