Skip to content

Commit

Permalink
Added CMake format targets generation using clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
pinam45 committed Oct 24, 2018
1 parent 3d66842 commit 158684f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
98 changes: 98 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
Language: Cpp
AccessModifierOffset: '-4'
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: 'false'
AlignConsecutiveDeclarations: 'false'
AlignEscapedNewlines: Left
AlignOperands: 'true'
AlignTrailingComments: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'false'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: 'false'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'Yes'
BinPackArguments: 'false'
BinPackParameters: 'false'
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterJavaFieldAnnotations: 'true'
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: 'false'
ColumnLimit: '100'
CompactNamespaces: 'false'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
ConstructorInitializerIndentWidth: '2'
ContinuationIndentWidth: '2'
Cpp11BracedListStyle: 'true'
DerivePointerAlignment: 'false'
DisableFormat: 'false'
ExperimentalAutoDetectBinPacking: 'false'
FixNamespaceComments: 'true'
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IndentCaseLabels: 'true'
IndentPPDirectives: AfterHash
IndentWidth: '4'
IndentWrappedFunctionNames: 'false'
KeepEmptyLinesAtTheStartOfBlocks: 'true'
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: All
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 1000000
PointerAlignment: Left
ReflowComments: 'false'
SortIncludes: 'false'
SortUsingDeclarations: 'false'
SpaceAfterCStyleCast: 'false'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCpp11BracedList: 'false'
SpaceBeforeCtorInitializerColon: 'false'
SpaceBeforeInheritanceColon: 'true'
SpaceBeforeParens: Never
SpaceBeforeRangeBasedForLoopColon: 'false'
SpaceInEmptyParentheses: 'false'
SpacesBeforeTrailingComments: '1'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
Standard: Cpp11
TabWidth: '4'
UseTab: ForIndentation
...
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ include(cmake/common.cmake)
# Config
include(cmake/config.cmake)

# clang-format
include(cmake/clang-format.cmake)

# Libraries
include(cmake/opengl.cmake)
include(cmake/SFML.cmake)
Expand Down Expand Up @@ -120,5 +123,8 @@ target_add_compile_definition(MagicPlayer ENABLE_CONSOLE_LOG)
target_add_compile_definition(MagicPlayer SPDLOG_DEBUG_ON RELWITHDEBINFO DEBUG)
#target_add_compile_definition(MagicPlayer SPDLOG_TRACE_ON DEBUG)

# Generate format target
target_generate_clang_format(MagicPlayer)

# Verbose makefile
#set(CMAKE_VERBOSE_MAKEFILE ON)
29 changes: 29 additions & 0 deletions cmake/clang-format.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

## target_generate_clang_format(target)
# Generate a format target for the target (format-${target}).
# The generated target lanch clang-format on all the target sources with -style=file
# {value} [in] target: Target from wich generate format target
function(target_generate_clang_format target)
if(NOT TARGET ${target})
message(FATAL_ERROR "${target} is not a target)")
endif()

set(format-target "format-${target}")
find_program(CLANG_FORMAT clang-format
NAMES clang-format-9 clang-format-8 clang-format-7 clang-format-6)
if(${CLANG_FORMAT} STREQUAL CLANG_FORMAT-NOTFOUND)
message(WARNING "clang-format not found, ${format-target} not generated")
return()
else()
message(STATUS "clang-format found: ${CLANG_FORMAT}")
endif()

get_property(target_sources TARGET ${target} PROPERTY SOURCES)
add_custom_target(
${format-target}
COMMAND "${CLANG_FORMAT}" -style=file -i ${target_sources}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
VERBATIM
)
message(STATUS "Format target ${format-target} generated")
endfunction()

0 comments on commit 158684f

Please sign in to comment.