summaryrefslogtreecommitdiffstats
path: root/cmake
diff options
context:
space:
mode:
authorManuel Bentele2021-03-11 16:47:05 +0100
committerManuel Bentele2021-03-11 18:18:27 +0100
commitba20fa121a0e97a7862aa163a11c7337a05188f7 (patch)
treedff557d5524b711a6f90209f071ed7d3a4b38148 /cmake
parent[BUILD] Add Github workflow to validate (lint) source code (diff)
downloaddnbd3-ba20fa121a0e97a7862aa163a11c7337a05188f7.tar.gz
dnbd3-ba20fa121a0e97a7862aa163a11c7337a05188f7.tar.xz
dnbd3-ba20fa121a0e97a7862aa163a11c7337a05188f7.zip
[BUILD] Enable lint targets if lint programs are found
Diffstat (limited to 'cmake')
-rw-r--r--cmake/FindCheckPatch.cmake31
-rw-r--r--cmake/FindClangFormat.cmake3
-rw-r--r--cmake/Lint.cmake75
3 files changed, 80 insertions, 29 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
index 9abf943..a6c77d4 100644
--- a/cmake/FindClangFormat.cmake
+++ b/cmake/FindClangFormat.cmake
@@ -17,5 +17,4 @@ 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!")
-
+ 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
index 73b4795..4453fe3 100644
--- a/cmake/Lint.cmake
+++ b/cmake/Lint.cmake
@@ -4,46 +4,67 @@
# Copyright (C) 2021 Manuel Bentele <development@manuel-bentele.de>
#
-find_package(ClangFormat REQUIRED)
+find_package(ClangFormat)
+find_package(CheckPatch)
-# add target to trigger all linter targets
-add_custom_target(lint)
+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)
- 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})
+ 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 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})
+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)
-# add target to trigger all formatter targets
-add_custom_target(lint-fix)
+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)
- 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})
+ 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 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})
+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)