diff --git a/kde-modules/KDEClangFormat.cmake b/kde-modules/KDEClangFormat.cmake new file mode 100644 --- /dev/null +++ b/kde-modules/KDEClangFormat.cmake @@ -0,0 +1,82 @@ +#.rst: +# KDEClangFormat +# -------------------- +# +# This module provides a functionality to format the source +# code of your repository according to a predefined KDE +# clang-format file. +# +# This module provides the following function: +# +# :: +# +# kde_clang_format() +# +# Using this function will create a clang-format target that will format all +# passed to the function with the predefined KDE clang-format style. +# +# Example usage: +# +# include(KDEClangFormat) +# file(GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES src/*.cpp src/*.h) +# kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES}) +# +# Since 5.64 + +#============================================================================= +# Copyright 2019 Christoph Cullmann +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. The name of the author may not be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# try to find clang-format in path +find_program(KDE_CLANG_FORMAT_EXECUTABLE clang-format) + +# instantiate our clang-format file, must be in source directory for tooling if we have the tool +if(KDE_CLANG_FORMAT_EXECUTABLE) + configure_file(${CMAKE_CURRENT_LIST_DIR}/clang-format.cmake ${CMAKE_CURRENT_SOURCE_DIR}/.clang-format @ONLY) +endif() + +# formatting target +function(KDE_CLANG_FORMAT) + # add target only if clang-format available + if(KDE_CLANG_FORMAT_EXECUTABLE) + add_custom_target(clang-format + COMMAND + ${KDE_CLANG_FORMAT_EXECUTABLE} + -style=file + -i + ${ARGV} + WORKING_DIRECTORY + ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT + "Formatting sources in ${CMAKE_CURRENT_SOURCE_DIR} with ${KDE_CLANG_FORMAT_EXECUTABLE}..." + ) + else() + add_custom_target(clang-format + COMMAND + ${CMAKE_COMMAND} -E echo + "Could not set up the clang-format target as the clang-format executable is missing." + ) + endif() +endfunction() diff --git a/kde-modules/clang-format.cmake b/kde-modules/clang-format.cmake new file mode 100644 --- /dev/null +++ b/kde-modules/clang-format.cmake @@ -0,0 +1,94 @@ +--- +# SPDX-License-Identifier: MIT +# +# Copyright (C) 2019 Christoph Cullmann +# Copyright (C) 2019 Gernot Gebhard +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +# Style for C++ +Language: Cpp + +# base is WebKit coding style: http://www.webkit.org/coding/coding-style.html +# below are only things set that diverge from this style! +BasedOnStyle: WebKit + +# enforce C++11 (e.g. for std::vector> +Standard: Cpp11 + +# 4 spaces indent +TabWidth: 4 + +# 3 * 80 wide lines +ColumnLimit: 240 + +# sort includes inside line separated groups +SortIncludes: true + +# break before braces on function, namespace and class definitions. +BreakBeforeBraces: Linux + +# CrlInstruction *a; +PointerAlignment: Right + +# horizontally aligns arguments after an open bracket. +AlignAfterOpenBracket: Align + +# align trailing comments +AlignTrailingComments: true + +# don't move all parameters to new line +AllowAllParametersOfDeclarationOnNextLine: false + +# no single line functions +AllowShortFunctionsOnASingleLine: None + +# always break before you encounter multi line strings +AlwaysBreakBeforeMultilineStrings: true + +# don't move arguments to own lines if they are not all on the same +BinPackArguments: false + +# don't move parameters to own lines if they are not all on the same +BinPackParameters: false + +# don't break binary ops +BreakBeforeBinaryOperators: None + +# don't break ternary ops +BreakBeforeTernaryOperators: false + +# format C++11 braced lists like function calls +Cpp11BracedListStyle: true + +# remove empty lines +KeepEmptyLinesAtTheStartOfBlocks: false + +# no namespace indentation to keep indent level low +NamespaceIndentation: None + +# we use template< without space. +SpaceAfterTemplateKeyword: false + +# macros for which the opening brace stays attached. +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH, forever, Q_FOREVER, QBENCHMARK, QBENCHMARK_ONCE ] + +# Break constructor initializers before the colon and after the commas. +BreakConstructorInitializers: BeforeColon