15

プロジェクトをリントするために、clang-tidy 用のカスタム cmake ターゲットを作成しようとしています。ソース フォルダーは次のようになります。

src/scripts/run-clang-tidy.py
src/.clang-tidy
src/...

これまでの私の計画では、カスタム コマンドを使用して、これらの両方のファイルをビルド ディレクトリにコピーすることでした。

add_custom_command(
    OUTPUT run-clang-tidy.py .clang-tidy
    COMMAND cp ${CMAKE_SOURCE_DIR}/scripts/run-clang-tidy.py ${CMAKE_SOURCE_DIR}/.clang-tidy ${CMAKE_CURRENT_BINARY_DIR})

run-clang-tidy.pyカスタムターゲットを使用して、ビルドディレクトリ(作業ディレクトリである必要があります)で呼び出したいので、次のように呼び出すことができます:

make lint

で指定されたチェックを実行する必要があります.clang-tidy

このスクリプトが機能するには、CMAKE_EXPORT_COMPILE_COMMANDSオプションも必要です。次のコマンドで設定しようとしましたが、認識されません。

add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)

への呼び出しはadd_custom_targetどのようになりますか?

4

4 に答える 4

29

CMake 3.6 以降、 のネイティブ統合clang-tidyが実装されています[ 1、2 ]include-what-you-useメカニズムは、CMake 3.3 [ 3 ]以降にあった統合に似ています。

于 2016-08-11T00:57:07.173 に答える
12

追加の Python スクリプトを必要としない別の方法を提案できます。

まず、カスタム CMake ルールにclang-tidyandを統合したかったので、最初にプロジェクトのルート ディレクトリにあるandファイルを生成しました。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ます。

于 2016-04-22T15:59:02.903 に答える
8

Alexander Shukaevが言及したドキュメントは詳細が少し不足しているため、例を追加します。警告文字列のフォーマットにより、IDE は clang-tidy の結果がコンパイラの警告であると認識し、ソース コードにマークを付けます。また、オブジェクトファイルが作成された後、各ファイルを並行して実行します。

if ( CMAKE_VERSION GREATER "3.5" )
  set(ENABLE_CLANG_TIDY OFF CACHE BOOL "Add clang-tidy automatically to builds")
  if (ENABLE_CLANG_TIDY)
    find_program (CLANG_TIDY_EXE NAMES "clang-tidy" PATHS /usr/local/opt/llvm/bin )
    if (CLANG_TIDY_EXE)
      message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
      set(CLANG_TIDY_CHECKS "-*,modernize-*")
      set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE};-checks=${CLANG_TIDY_CHECKS};-header-filter='${CMAKE_SOURCE_DIR}/*'"
        CACHE STRING "" FORCE)
    else()
      message(AUTHOR_WARNING "clang-tidy not found!")
      set(CMAKE_CXX_CLANG_TIDY "" CACHE STRING "" FORCE) # delete it
    endif()
  endif()
endif()

これに関して私が経験した唯一の問題は、自動生成されmoc_*.cxxたファイルと、ExternalProject.

于 2017-10-06T17:18:10.767 に答える
3

add_definitionsCMake変数を設定します。構成段階でのみ使用できます。ビルド段階で実行されるコマンドの環境変数を設定する場合は、適切なシェル メカニズムをCOMMANDキーワードで使用します。

add_custom_target(lint
    COMMAND CMAKE_EXPORT_COMPILE_COMMANDS=ON python run-clang-tidy.py
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/run-clang-tidy.py
            ${CMAKE_CURRENT_BINARY_DIR}/.clang-tidy

keyword に指定されたものはすべてCOMMAND、シェルによって「そのまま」解釈されます (CMake の解釈後、ここでは何もしません)。

于 2015-08-31T12:53:55.990 に答える