From a9508f44b559d2ccad686079f4fd2a243ca772f1 Mon Sep 17 00:00:00 2001 From: Maurizio Drocco Date: Thu, 12 Jul 2018 22:45:48 -0700 Subject: [PATCH] [pnnl/gmt#2] cmake scripting for clang-format/tidy --- .clang-format | 96 +++++++++++++++++++++++++++++++++++++ .clang-tidy | 48 +++++++++++++++++++ CMakeLists.txt | 26 ++++++++++ cmake/CheckClangTools.cmake | 29 +++++++++++ cmake/config.cmake | 1 + src/CMakeLists.txt | 3 ++ test/CMakeLists.txt | 3 ++ 7 files changed, 206 insertions(+) create mode 100644 .clang-format create mode 100644 .clang-tidy create mode 100644 cmake/CheckClangTools.cmake create mode 100644 cmake/config.cmake diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..32bdeb7 --- /dev/null +++ b/.clang-format @@ -0,0 +1,96 @@ +--- +Language: Cpp +# BasedOnStyle: Google +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: true +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: true +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +IncludeCategories: + - Regex: '^<.*\.h>' + Priority: 1 + - Regex: '^<.*' + Priority: 2 + - Regex: '.*' + Priority: 3 +IncludeIsMainRegex: '([-_](test|unittest))?$' +IndentCaseLabels: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: false +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +ReflowComments: true +SortIncludes: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Auto +TabWidth: 8 +UseTab: Never +... + diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..b244198 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,48 @@ +--- +Checks: '-*,clang-diagnostic-*,google*,-google-runtime-references,-google-readability-braces-around-statements,misc*,-misc-unused-parameters' +WarningsAsErrors: '' +HeaderFilterRegex: 'shad/.*' +AnalyzeTemporaryDtors: false +FormatStyle: none +User: minu455 +CheckOptions: + - key: google-build-namespaces.HeaderFileExtensions + value: h,hh,hpp,hxx + - key: google-global-names-in-headers.HeaderFileExtensions + value: h + - key: google-readability-braces-around-statements.ShortStatementLines + value: '1' + - key: google-readability-function-size.BranchThreshold + value: '4294967295' + - key: google-readability-function-size.LineThreshold + value: '4294967295' + - key: google-readability-function-size.NestingThreshold + value: '4294967295' + - key: google-readability-function-size.ParameterThreshold + value: '4294967295' + - key: google-readability-function-size.StatementThreshold + value: '800' + - key: google-readability-namespace-comments.ShortNamespaceLines + value: '10' + - key: google-readability-namespace-comments.SpacesBeforeComments + value: '2' + - key: google-runtime-int.SignedTypePrefix + value: int + - key: google-runtime-int.TypeSuffix + value: '' + - key: google-runtime-int.UnsignedTypePrefix + value: uint + - key: modernize-loop-convert.MaxCopySize + value: '16' + - key: modernize-loop-convert.MinConfidence + value: reasonable + - key: modernize-loop-convert.NamingStyle + value: CamelCase + - key: modernize-pass-by-value.IncludeStyle + value: llvm + - key: modernize-replace-auto-ptr.IncludeStyle + value: llvm + - key: modernize-use-nullptr.NullMacros + value: 'NULL' +... + diff --git a/CMakeLists.txt b/CMakeLists.txt index 4341845..fc08acc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,12 @@ if (POLICY CMP0048) set(cmake_3_0_LANGUAGES LANGUAGES) endif() +# Add path for custom CMake modules +set(CMAKE_MODULE_PATH + ${CMAKE_MODULE_PATH} + "${CMAKE_CURRENT_SOURCE_DIR}/cmake" + ) + if (NOT PACKAGE_VERSION) set(PACKAGE_VERSION "${GMT_VERSION_MAJOR}.${GMT_VERSION_MINOR}.${GMT_VERSION_PATCH}") @@ -51,6 +57,9 @@ if (CMAKE_BUILD_TYPE AND endif() +include(config) + +set(GMT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) set(GMT_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(GMT_MAIN_INCLUDE_DIR ${GMT_MAIN_SRC_DIR}/include) set(GMT_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include) @@ -84,3 +93,20 @@ install(DIRECTORY ${GMT_MAIN_INCLUDE_DIR}/gmt PATTERN "*~" EXCLUDE PATTERN "CMakeLists.txt" ) + +# Adding clang-format target if executable is found +if(CLANG_FORMAT_EXE) + # Additional targets to perform clang-format/clang-tidy + # Get all project files + file(GLOB_RECURSE + ALL_CXX_SOURCE_FILES + *.[chi]pp *.[chi]xx *.cc *.hh *.ii *.[CHI] *.[ch] + ) + + add_custom_target( + clang-format + COMMAND ${CLANG_FORMAT_EXE} + -i + ${ALL_CXX_SOURCE_FILES} + ) +endif() diff --git a/cmake/CheckClangTools.cmake b/cmake/CheckClangTools.cmake new file mode 100644 index 0000000..8ef15e0 --- /dev/null +++ b/cmake/CheckClangTools.cmake @@ -0,0 +1,29 @@ +find_program( + CLANG_TIDY_EXE + NAMES "clang-tidy" + DOC "Path to clang-tidy executable") + +if(CLANG_TIDY_EXE) + set( + DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-p=${GMT_BINARY_DIR} -format-style=file") + message(STATUS "ClangTidy found: ${CLANG_TIDY_EXE}") + if (NOT CMAKE_EXPORT_COMPILE_COMMANDS) + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + endif() +else() + message(STATUS "ClangTidy not found.") +endif() + +find_program( + CLANG_FORMAT_EXE + NAMES "clang-format" + DOC "Path to clang-format executable") + +if (CLANG_FORMAT_EXE) + message(STATUS "ClangFormat found: ${CLANG_FORMAT_EXE}") + if (NOT CMAKE_EXPORT_COMPILE_COMMANDS) + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + endif() +else() + message(STATUS "ClangFormat not found.") +endif() diff --git a/cmake/config.cmake b/cmake/config.cmake new file mode 100644 index 0000000..bf984b2 --- /dev/null +++ b/cmake/config.cmake @@ -0,0 +1 @@ +include(CheckClangTools) \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c57c9ff..50dcec3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,5 +10,8 @@ include_directories(${THREADS_PTHREADS_INCLUDE_DIR}) add_library(gmt ${sources}) set_target_properties(gmt PROPERTIES LINKER_LANGUAGE CXX) +if (CLANG_TIDY_EXE) + set_target_properties(gmt PROPERTIES CXX_CLANG_TIDY "${DO_CLANG_TIDY}") +endif() target_link_libraries(gmt ${MPI_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) install(TARGETS gmt DESTINATION lib) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b690e68..d21d431 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -52,6 +52,9 @@ set_source_files_properties(main.c PROPERTIES LANGUAGE CXX ) add_executable(gmttest main.c) set_target_properties(gmttest PROPERTIES LINKER_LANGUAGE CXX) +if (CLANG_TIDY_EXE) + set_target_properties(gmttest PROPERTIES CXX_CLANG_TIDY "${DO_CLANG_TIDY}") +endif() target_link_libraries(gmttest gmttestlib gmt ${MPI_LIBRARIES}) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/run_test.sh ${CMAKE_CURRENT_BINARY_DIR}/run_test.sh COPYONLY)