summaryrefslogtreecommitdiffstats
path: root/cmake
diff options
context:
space:
mode:
Diffstat (limited to 'cmake')
-rw-r--r--cmake/FindClangFormat.cmake21
-rw-r--r--cmake/Lint.cmake49
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)