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