Skip to content

Commit

Permalink
Merge pull request #45 from cginternals/cppcheck_scripts
Browse files Browse the repository at this point in the history
Add source check targets
  • Loading branch information
scheibel authored Feb 2, 2017
2 parents 6f96d29 + 50840d0 commit dea9d03
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ matrix:
compiler: gcc

before_install:
- if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get install -qq cmake qt5-default; fi
- if [ $TRAVIS_OS_NAME == osx ]; then brew update && brew install qt5; fi
- if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get install -qq cmake qt5-default cppcheck clang-tidy-3.8; fi
- if [ $TRAVIS_OS_NAME == osx ]; then brew update && brew install qt5 cppcheck; fi

before_script:
- ./configure
Expand All @@ -35,3 +35,4 @@ before_script:
script:
- cmake --build $BUILD_DIR
- cmake --build $BUILD_DIR --target test
- cmake --build $BUILD_DIR --target check-all
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ endif()

include(cmake/GetGitRevisionDescription.cmake)
include(cmake/Custom.cmake)
include(cmake/HealthCheck.cmake)

# Set policies
set_policy(CMP0028 NEW) # ENABLE CMP0028: Double colon in target name means ALIAS or IMPORTED target.
Expand Down Expand Up @@ -97,6 +98,14 @@ file(WRITE "${PROJECT_BINARY_DIR}/VERSION" "${META_NAME_VERSION}")
include(cmake/CompileOptions.cmake)


#
# Project Health Check Setup
#

enable_cppcheck(On)
enable_clang_tidy(On)


#
# Deployment/installation setup
#
Expand Down
18 changes: 18 additions & 0 deletions cmake/ClangTidy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# Function to register a target for clang-tidy
function(perform_clang_tidy check_target target)
set(includes "$<TARGET_PROPERTY:${target},INCLUDE_DIRECTORIES>")

add_custom_target(
${check_target}
COMMAND
${clang_tidy_EXECUTABLE}
"$<$<BOOL:${CMAKE_EXPORT_COMPILE_COMMANDS}>:-p\t${PROJECT_BINARY_DIR}>"
${ARGN}
-checks=*
"$<$<NOT:$<BOOL:${CMAKE_EXPORT_COMPILE_COMMANDS}>>:--\t$<$<BOOL:${includes}>:-I$<JOIN:${includes},\t-I>>>"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

add_dependencies(${check_target} ${target})
endfunction()
22 changes: 22 additions & 0 deletions cmake/Cppcheck.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Function to register a target for cppcheck
function(perform_cppcheck check_target target)
set(includes "$<TARGET_PROPERTY:${target},INCLUDE_DIRECTORIES>")

add_custom_target(
${check_target}
COMMAND
${cppcheck_EXECUTABLE}
"$<$<BOOL:${includes}>:-I$<JOIN:${includes},\t-I>>"
--check-config
--enable=warning,performance,portability,information,missingInclude
--quiet
--std=c++11
--verbose
--suppress=missingIncludeSystem
${ARGN}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

add_dependencies(${check_target} ${target})
endfunction()
27 changes: 27 additions & 0 deletions cmake/Findclang_tidy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

# Findclang_tidy results:
# clang_tidy_FOUND
# clang_tidy_EXECUTABLE

include(FindPackageHandleStandardArgs)

find_program(clang_tidy_EXECUTABLE
NAMES
clang-tidy-3.5
clang-tidy-3.6
clang-tidy-3.7
clang-tidy-3.8
clang-tidy-3.9
clang-tidy-4.0
PATHS
"${CLANG_TIDY_DIR}"
)

find_package_handle_standard_args(clang_tidy
FOUND_VAR
clang_tidy_FOUND
REQUIRED_VARS
clang_tidy_EXECUTABLE
)

mark_as_advanced(clang_tidy_EXECUTABLE)
22 changes: 22 additions & 0 deletions cmake/Findcppcheck.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Findcppcheck results:
# cppcheck_FOUND
# cppcheck_EXECUTABLE

include(FindPackageHandleStandardArgs)

find_program(cppcheck_EXECUTABLE
NAMES
cppcheck
PATHS
"${CPPCHECK_DIR}"
)

find_package_handle_standard_args(cppcheck
FOUND_VAR
cppcheck_FOUND
REQUIRED_VARS
cppcheck_EXECUTABLE
)

mark_as_advanced(cppcheck_EXECUTABLE)
75 changes: 75 additions & 0 deletions cmake/HealthCheck.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

include(${CMAKE_CURRENT_LIST_DIR}/Cppcheck.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/ClangTidy.cmake)

set(OPTION_CPPCHECK_ENABLED Off)
set(OPTION_CLANG_TIDY_ENABLED Off)

# Function to register a target for enabled health checks
function(perform_health_checks target)
if(NOT TARGET check-all)
add_custom_target(check-all)
endif()

add_custom_target(check-${target})

if (OPTION_CPPCHECK_ENABLED)
perform_cppcheck(cppcheck-${target} ${target} ${ARGN})
add_dependencies(check-${target} cppcheck-${target})
endif()

if (OPTION_CLANG_TIDY_ENABLED)
perform_clang_tidy(clang-tidy-${target} ${target} ${ARGN})
add_dependencies(check-${target} clang-tidy-${target})
endif()

add_dependencies(check-all check-${target})
endfunction()

# Enable or disable cppcheck for health checks
function(enable_cppcheck status)
if(NOT ${status})
set(OPTION_CPPCHECK_ENABLED ${status} PARENT_SCOPE)
message(STATUS "Check cppcheck skipped: Manually disabled")

return()
endif()

find_package(cppcheck)

if(NOT cppcheck_FOUND)
set(OPTION_CPPCHECK_ENABLED Off PARENT_SCOPE)
message(STATUS "Check cppcheck skipped: cppcheck not found")

return()
endif()

set(OPTION_CPPCHECK_ENABLED ${status} PARENT_SCOPE)
message(STATUS "Check cppcheck")
endfunction()

# Enable or disable clang-tidy for health checks
function(enable_clang_tidy status)
if(NOT ${status})
set(OPTION_CLANG_TIDY_ENABLED ${status} PARENT_SCOPE)
message(STATUS "Check clang-tidy skipped: Manually disabled")

return()
endif()

find_package(clang_tidy)

if(NOT clang_tidy_FOUND)
set(OPTION_CLANG_TIDY_ENABLED Off PARENT_SCOPE)
message(STATUS "Check clang-tidy skipped: clang-tidy not found")

return()
endif()

set(OPTION_CLANG_TIDY_ENABLED ${status} PARENT_SCOPE)
message(STATUS "Check clang-tidy")

if(${CMAKE_VERSION} VERSION_GREATER "3.5" AND NOT CMAKE_EXPORT_COMPILE_COMMANDS)
message(STATUS "clang-tidy makes use of the compile commands database. Make sure to configure CMake with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON")
endif()
endfunction()
11 changes: 11 additions & 0 deletions source/baselib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ target_link_libraries(${target}
)


#
# Target Health
#

perform_health_checks(
${target}
${sources}
${headers}
)


#
# Deployment
#
Expand Down
10 changes: 10 additions & 0 deletions source/examples/fibcmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ target_link_libraries(${target}
)


#
# Target Health
#

perform_health_checks(
${target}
${sources}
)


#
# Deployment
#
Expand Down
10 changes: 10 additions & 0 deletions source/examples/fibgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ target_link_libraries(${target}
)


#
# Target Health
#

perform_health_checks(
${target}
${sources}
)


#
# Deployment
#
Expand Down
11 changes: 11 additions & 0 deletions source/fiblib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ target_link_libraries(${target}
)


#
# Target Health
#

perform_health_checks(
${target}
${sources}
${headers}
)


#
# Deployment
#
Expand Down

0 comments on commit dea9d03

Please sign in to comment.