Merge pull request #16122 from alalek:cmake_update_cpu_compiler_detection
[platform/upstream/opencv.git] / cmake / OpenCVPylint.cmake
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   add_custom_command(
101       OUTPUT "${CMAKE_BINARY_DIR}/pylintrc"
102       COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/platforms/scripts/pylintrc" "${CMAKE_BINARY_DIR}/pylintrc"
103       DEPENDS "${CMAKE_SOURCE_DIR}/platforms/scripts/pylintrc"
104   )
105
106   set(PYLINT_CONFIG_SCRIPT "")
107   ocv_cmake_script_append_var(PYLINT_CONFIG_SCRIPT
108       PYLINT_EXECUTABLE
109       PYLINT_TARGET_ID
110   )
111   set(__sources "")
112   foreach(__id ${PYLINT_TARGET_ID})
113     ocv_cmake_script_append_var(PYLINT_CONFIG_SCRIPT
114         PYLINT_TARGET_${__id}_CWD
115         PYLINT_TARGET_${__id}_TARGET
116         PYLINT_TARGET_${__id}_RCFILE
117         PYLINT_TARGET_${__id}_OPTIONS
118     )
119     list(APPEND __sources ${PYLINT_TARGET_${__id}_TARGET} ${PYLINT_TARGET_${__id}_RCFILE})
120   endforeach()
121   list(REMOVE_DUPLICATES __sources)
122
123   list(LENGTH PYLINT_TARGET_ID __total)
124   set(PYLINT_TOTAL_TARGETS "${__total}" CACHE INTERNAL "")
125   message(STATUS "Pylint: registered ${__total} targets. Build 'check_pylint' target to run checks (\"cmake --build . --target check_pylint\" or \"make check_pylint\")")
126   configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/pylint.cmake.in" "${CMAKE_BINARY_DIR}/pylint.cmake" @ONLY)
127
128   add_custom_target(check_pylint
129       COMMAND ${CMAKE_COMMAND} -P "${CMAKE_BINARY_DIR}/pylint.cmake"
130       COMMENT "Running pylint"
131       DEPENDS ${__sources}
132       SOURCES ${__sources}
133   )
134 endfunction()