cpplint.cmake 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #
  2. # CMake module to C++ static analysis against
  3. # Google C++ Style Guide (https://google.github.io/styleguide/cppguide.html)
  4. #
  5. # For more detials please follow links:
  6. #
  7. # - https://github.com/google/styleguide
  8. # - https://pypi.python.org/pypi/cpplint
  9. # - https://github.com/theandrewdavis/cpplint
  10. #
  11. # Copyright (c) 2016 Piotr L. Figlarek
  12. #
  13. # Usage
  14. # -----
  15. # Include this module via CMake include(...) command and then add each source directory
  16. # via introduced by this module cpplint_add_subdirectory(...) function. Added directory
  17. # will be recursivelly scanned and all available files will be checked.
  18. #
  19. # Example
  20. # -------
  21. # # include CMake module
  22. # include(cmake/cpplint.cmake)
  23. #
  24. # # add all source code directories
  25. # cpplint_add_subdirectory(core)
  26. # cpplint_add_subdirectory(modules/c-bind)
  27. #
  28. # License (MIT)
  29. # -------------
  30. # Permission is hereby granted, free of charge, to any person obtaining a copy
  31. # of this software and associated documentation files (the "Software"), to deal
  32. # in the Software without restriction, including without limitation the rights
  33. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  34. # copies of the Software, and to permit persons to whom the Software is
  35. # furnished to do so, subject to the following conditions:
  36. #
  37. # The above copyright notice and this permission notice shall be included in all
  38. # copies or substantial portions of the Software.
  39. #
  40. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  41. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  42. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  43. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  44. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  45. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  46. # SOFTWARE.
  47. # select files extensions to check
  48. option(CPPLINT_TEST_C_FILES "Check *.c files" ON)
  49. option(CPPLINT_TEST_H_FILES "Check *.h files" ON)
  50. option(CPPLINT_TEST_CPP_FILES "Check *.cpp files" ON)
  51. option(CPPLINT_TEST_HPP_FILES "Check *.hpp files" ON)
  52. option(CPPLINT_TEST_TPP_FILES "Check *.tpp files" ON)
  53. # target to run cpplint.py for all configured sources
  54. set(CPPLINT_TARGET lint CACHE STRING "Name of C++ style checker target")
  55. # project root directory
  56. set(CPPLINT_PROJECT_ROOT "${PROJECT_SOURCE_DIR}" CACHE STRING "Project ROOT directory")
  57. # find cpplint.py script
  58. if(CPPLINT)
  59. message(STATUS "cpplint parser: ${CPPLINT}")
  60. else()
  61. message(FATAL_ERROR "cpplint script: NOT FOUND! "
  62. "Please set the CPPLINT variable.")
  63. endif()
  64. # common target to concatenate all cpplint.py targets
  65. add_custom_target(${CPPLINT_TARGET} ALL)
  66. # use cpplint.py to check source code files inside DIR directory
  67. function(cpplint_add_subdirectory DIR)
  68. # create relative path to the directory
  69. set(ABSOLUTE_DIR ${CMAKE_CURRENT_LIST_DIR}/${DIR})
  70. # add *.c files
  71. if(CPPLINT_TEST_C_FILES)
  72. set(EXTENSIONS ${EXTENSIONS}c,)
  73. set(FILES_TO_CHECK ${FILES_TO_CHECK} ${ABSOLUTE_DIR}/*.c)
  74. endif()
  75. # add *.h files
  76. if(CPPLINT_TEST_H_FILES)
  77. set(EXTENSIONS ${EXTENSIONS}h,)
  78. set(FILES_TO_CHECK ${FILES_TO_CHECK} ${ABSOLUTE_DIR}/*.h)
  79. endif()
  80. # add *.cpp files
  81. if(CPPLINT_TEST_CPP_FILES)
  82. set(EXTENSIONS ${EXTENSIONS}cpp,)
  83. set(FILES_TO_CHECK ${FILES_TO_CHECK} ${ABSOLUTE_DIR}/*.cpp)
  84. endif()
  85. # add *.hpp files
  86. if(CPPLINT_TEST_HPP_FILES)
  87. set(EXTENSIONS ${EXTENSIONS}hpp,)
  88. set(FILES_TO_CHECK ${FILES_TO_CHECK} ${ABSOLUTE_DIR}/*.hpp)
  89. endif()
  90. # add *.tpp files
  91. if(CPPLINT_TEST_TPP_FILES)
  92. set(EXTENSIONS ${EXTENSIONS}tpp,)
  93. set(FILES_TO_CHECK ${FILES_TO_CHECK} ${ABSOLUTE_DIR}/*.tpp)
  94. endif()
  95. # find all source files inside project
  96. file(GLOB_RECURSE LIST_OF_FILES ${FILES_TO_CHECK})
  97. # create valid target name for this check
  98. string(REGEX REPLACE "/" "." TEST_NAME ${DIR})
  99. set(TARGET_NAME ${CPPLINT_TARGET}.${TEST_NAME})
  100. # perform cpplint check
  101. add_custom_target(${TARGET_NAME}
  102. COMMAND ${CPPLINT} "--extensions=${EXTENSIONS}"
  103. "--root=${CPPLINT_PROJECT_ROOT}"
  104. "--quiet"
  105. ${LIST_OF_FILES}
  106. DEPENDS ${LIST_OF_FILES}
  107. COMMENT "cpplint: Checking source code style"
  108. )
  109. # run this target when root cpplint.py test is triggered
  110. add_dependencies(${CPPLINT_TARGET} ${TARGET_NAME})
  111. # add this test to CTest
  112. add_test(${TARGET_NAME} ${CMAKE_MAKE_PROGRAM} ${TARGET_NAME})
  113. endfunction()