CMake - 检查是否安装了Pylint

时间:2017-06-24 19:54:25

标签: cmake pylint

我使用Cmake浏览目录中的所有.py文件,并使用Pylint检测错误并检查编码标准。

我有办法检查是否使用cmake安装了Pylint?这段代码是否独立于操作系统(例如Ubuntu和Windows)?

2 个答案:

答案 0 :(得分:2)

您应该创建一个FindPylint.cmake文件并include()其目录。然后运行find_package(Pylint REQUIRED)

FindPylint.cmake:

execute_process(
    COMMAND pylint --version
    OUTPUT_VARIABLE pylint_out
    RESULT_VARIABLE pylint_error
    ERROR_VARIABLE pylint_suppress)

if (NOT pylint_error)
    string(REGEX MATCH "pylint .\..\.." pylint_version_string "${pylint_out}")
    string(SUBSTRING "${pylint_version_string}" 7 5 pylint_version)
endif ()

if (pylint_version)
    set(PYLINT_FOUND 1
        CACHE INTERNAL "Pylint version ${pylint_version} found")
endif ()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Pylint REQUIRED_VARS pylint_version
                                    VERSION_VAR pylint_version)

一些解释:

  • 错误输出实际上不是错误,它显示为No config file found, using default configuration,因此我们通过忽略pylint_suppress变量来抑制它。
  • pylint输出不仅仅是版本,所以我们需要做一些正则表达式/字符串处理。
  • CACHE INTERNAL变量并非绝对必要,但稍后可以派上用场检查是否找到了Pylint。

答案 1 :(得分:0)

<强>的CMakeLists.txt

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})

find_package ( Pylint )

if ( PYLINT_FOUND )
  message ( STATUS "pylint version: ${PYLINT_VERSION}" )
endif ()

在同一目录中添加 FindPylint.cmake

# - Find Pylint
# Find the Pylint executable and extract the version number
#
# OUTPUT Variables
#
#   PYLINT_FOUND
#       True if the pylint package was found
#   PYLINT_EXECUTABLE
#       The pylint executable location
#   PYLINT_VERSION
#       A string denoting the version of pylint that has been found

find_program ( PYLINT_EXECUTABLE pylint PATHS /usr/bin )

if ( PYLINT_EXECUTABLE )
  execute_process ( COMMAND ${PYLINT_EXECUTABLE} --version OUTPUT_VARIABLE PYLINT_VERSION_RAW ERROR_QUIET )
  if (PYLINT_VERSION_RAW)
    string ( REGEX REPLACE "^pylint ([0-9]+.[0-9]+.[0-9]+),.*" "\\1" PYLINT_VERSION ${PYLINT_VERSION_RAW})
  else ()
    set ( PYLINT_VERSION "unknown" )
  endif()
endif ()

include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS ( Pylint DEFAULT_MSG PYLINT_EXECUTABLE )

mark_as_advanced ( PYLINT_EXECUTABLE PYLINT_VERSION )

基于此code