OpenCVPylint.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. if(COMMAND ocv_pylint_add_target)
  2. return()
  3. endif()
  4. find_package(Pylint QUIET)
  5. if(NOT PYLINT_FOUND OR NOT PYLINT_EXECUTABLE)
  6. include("${CMAKE_CURRENT_LIST_DIR}/FindPylint.cmake")
  7. endif()
  8. if(NOT PYLINT_FOUND)
  9. macro(ocv_pylint_add_target) # dummy
  10. endmacro()
  11. return()
  12. endif()
  13. macro(ocv_pylint_cleanup)
  14. foreach(__id ${PYLINT_TARGET_ID})
  15. ocv_clear_vars(
  16. PYLINT_TARGET_${__id}_CWD
  17. PYLINT_TARGET_${__id}_TARGET
  18. PYLINT_TARGET_${__id}_RCFILE
  19. PYLINT_TARGET_${__id}_OPTIONS
  20. )
  21. endforeach()
  22. ocv_clear_vars(PYLINT_TARGET_ID)
  23. endmacro()
  24. ocv_pylint_cleanup()
  25. macro(ocv_pylint_add_target)
  26. cmake_parse_arguments(__pylint "" "CWD;TARGET;RCFILE;" "OPTIONS" ${ARGN})
  27. if(__pylint_UNPARSED_ARGUMENTS)
  28. message(WARNING "Unsupported arguments: ${__pylint_UNPARSED_ARGUMENTS}
  29. (keep versions of opencv/opencv_contrib synchronized)
  30. ")
  31. endif()
  32. ocv_assert(__pylint_TARGET)
  33. set(__cwd ${__pylint_CWD})
  34. if(__cwd STREQUAL "default")
  35. get_filename_component(__cwd "${__pylint_TARGET}" DIRECTORY)
  36. endif()
  37. set(__rcfile ${__pylint_RCFILE})
  38. if(NOT __rcfile AND NOT __pylint_OPTIONS)
  39. if(__cwd)
  40. set(__path "${__cwd}")
  41. else()
  42. get_filename_component(__path "${__pylint_TARGET}" DIRECTORY)
  43. endif()
  44. while(__path MATCHES "^${CMAKE_SOURCE_DIR}")
  45. if(EXISTS "${__path}/pylintrc")
  46. set(__rcfile "${__path}/pylintrc")
  47. break()
  48. endif()
  49. if(EXISTS "${__path}/.pylintrc")
  50. set(__rcfile "${__path}/.pylintrc")
  51. break()
  52. endif()
  53. get_filename_component(__path "${__path}" DIRECTORY)
  54. endwhile()
  55. if(NOT __rcfile)
  56. set(__rcfile "${CMAKE_BINARY_DIR}/pylintrc")
  57. endif()
  58. endif()
  59. list(LENGTH PYLINT_TARGET_ID __id)
  60. list(APPEND PYLINT_TARGET_ID ${__id})
  61. set(PYLINT_TARGET_ID "${PYLINT_TARGET_ID}" CACHE INTERNAL "")
  62. set(PYLINT_TARGET_${__id}_CWD "${__cwd}" CACHE INTERNAL "")
  63. set(PYLINT_TARGET_${__id}_TARGET "${__pylint_TARGET}" CACHE INTERNAL "")
  64. set(PYLINT_TARGET_${__id}_RCFILE "${__rcfile}" CACHE INTERNAL "")
  65. set(PYLINT_TARGET_${__id}_OPTIONS "${__pylint_options}" CACHE INTERNAL "")
  66. endmacro()
  67. macro(ocv_pylint_add_directory_recurse __path)
  68. file(GLOB_RECURSE __python_scripts ${__path}/*.py)
  69. list(LENGTH __python_scripts __total)
  70. if(__total EQUAL 0)
  71. message(WARNING "Pylint: Python files are not found: ${__path}")
  72. endif()
  73. foreach(__script ${__python_scripts})
  74. ocv_pylint_add_target(TARGET ${__script} ${ARGN})
  75. endforeach()
  76. endmacro()
  77. macro(ocv_pylint_add_directory __path)
  78. file(GLOB __python_scripts ${__path}/*.py)
  79. list(LENGTH __python_scripts __total)
  80. if(__total EQUAL 0)
  81. message(WARNING "Pylint: Python files are not found: ${__path}")
  82. endif()
  83. foreach(__script ${__python_scripts})
  84. ocv_pylint_add_target(TARGET ${__script} ${ARGN})
  85. endforeach()
  86. endmacro()
  87. function(ocv_pylint_finalize)
  88. if(NOT PYLINT_FOUND)
  89. return()
  90. endif()
  91. add_custom_command(
  92. OUTPUT "${CMAKE_BINARY_DIR}/pylintrc"
  93. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/platforms/scripts/pylintrc" "${CMAKE_BINARY_DIR}/pylintrc"
  94. DEPENDS "${CMAKE_SOURCE_DIR}/platforms/scripts/pylintrc"
  95. )
  96. set(PYLINT_CONFIG_SCRIPT "")
  97. ocv_cmake_script_append_var(PYLINT_CONFIG_SCRIPT
  98. PYLINT_EXECUTABLE
  99. PYLINT_TARGET_ID
  100. )
  101. set(__sources "")
  102. foreach(__id ${PYLINT_TARGET_ID})
  103. ocv_cmake_script_append_var(PYLINT_CONFIG_SCRIPT
  104. PYLINT_TARGET_${__id}_CWD
  105. PYLINT_TARGET_${__id}_TARGET
  106. PYLINT_TARGET_${__id}_RCFILE
  107. PYLINT_TARGET_${__id}_OPTIONS
  108. )
  109. list(APPEND __sources ${PYLINT_TARGET_${__id}_TARGET} ${PYLINT_TARGET_${__id}_RCFILE})
  110. endforeach()
  111. list(REMOVE_DUPLICATES __sources)
  112. list(LENGTH PYLINT_TARGET_ID __total)
  113. set(PYLINT_TOTAL_TARGETS "${__total}" CACHE INTERNAL "")
  114. configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/pylint.cmake.in" "${CMAKE_BINARY_DIR}/pylint.cmake" @ONLY)
  115. add_custom_target(check_pylint
  116. COMMAND ${CMAKE_COMMAND} -P "${CMAKE_BINARY_DIR}/pylint.cmake"
  117. COMMENT "Running pylint"
  118. DEPENDS ${__sources}
  119. SOURCES ${__sources}
  120. )
  121. endfunction()