我可以建议另一种方法,不需要额外的 Python 脚本。
首先,我想在自定义CMake规则中集成clang-tidy
和,所以我首先生成了位于项目根目录下的文件。clang-format
.clang-tidy
.clang-format
生成配置文件
要生成.clang-tidy
,首先为您的项目找到合适的选项,然后,只需执行以下操作:
$> clang-tidy <source-files> -dump-config <tidy-options> -- <compile-options> > .clang-tidy
同样,clang-format
您可以使用该-style=xxx
选项从默认样式开始,然后转储它。例如,从 LLVM 样式开始:
$> clang-format -style=LLVM -dump-config > .clang-format
然后,根据需要对其进行编辑并正确配置。它应该是这样的:
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: true
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: false
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: true
BinPackArguments: true
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: false
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
MaxEmptyLinesToKeep: 1
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
SpacesBeforeTrailingComments: 1
Cpp11BracedListStyle: true
Standard: Cpp11
IndentWidth: 2
TabWidth: 8
UseTab: Never
BreakBeforeBraces: Attach
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterCStyleCast: false
SpacesInContainerLiterals: true
SpaceBeforeAssignmentOperators: true
ContinuationIndentWidth: 4
CommentPragmas: '^ IWYU pragma:'
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
SpaceBeforeParens: ControlStatements
DisableFormat: false
...
创建自定义 CMake 规则
CMake 允许以非常简单的方式定义自定义规则,您只需在文件中编写一组 CMake 命令并调用该add_custom_target()
过程,然后将其包含在您的CMakeList.txt
文件中。这就是我们要做的,我们首先cmake/clang-dev-tools.cmake
在项目的根目录下创建一个文件:
# Additional target to perform clang-format/clang-tidy run
# Requires clang-format and clang-tidy
# Get all project files
file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.hpp)
add_custom_target(
clang-format
COMMAND /usr/bin/clang-format
-style=file
-i
${ALL_SOURCE_FILES}
)
add_custom_target(
clang-tidy
COMMAND /usr/bin/clang-tidy
${ALL_SOURCE_FILES}
-config=''
--
-std=c++11
${INCLUDE_DIRECTORIES}
)
然后,编辑您CMakeLists.txt
并添加:
# Including extra cmake rules
include(cmake/clang-dev-tools.cmake)
然后,一旦构建系统重新生成,您应该能够运行make clang-tidy
和make clang-format
.