CMake:如何在配置文件中实现版本检查?

CMake: How to implement version check in a config file?

我为 opencl-icd 自定义 CMake 包配置文件 OpenCLConfig.cmake

参考

现在,我遇到一个问题:如何在配置文件中实现版本检查。

我在OpenCLConfig.cmake旁边创建了OpenCLConfigVersion.cmake,内容是:

#OpenCLConfigVersion.cmake

set(OpenCL_VERSION 2.2)
set(OpenCL_VERSION_MAJOR 2)
set(OpenCL_VERSION_MINOR 2)
SET(OpenCV_VERSION_PATCH 0)
SET(OpenCV_VERSION_TWEAK 0)

虽然我在其他人中调用它 opencl-icd CMakeLists.txt,

find_package(OpenCL config)

我收到这个错误:

CMake Error at CMakeLists.txt:10 (find_package): Could not find a 
configuration file for package "OpenCL" that is compatible with requested 
version "2". The following configuration files were considered but not 
accepted: C:/SDKs/ocl-icd/OpenCLConfig.cmake, version: unknown 

我在 CMakeLists.txt 中将 OpenCL_DIR 设置为 "C:/SDKs/ocl-icd"

我的自定义OpenCLConfig.cmake内容是

set(OpenCL_FOUND TRUE)
set(OpenCL_ROOT_DIR "C:/SDKs/ocl-icd")

set(OpenCL_VERSION 2.2)
set(OpenCL_VERSION_MAJOR 2)
set(OpenCL_VERSION_MINOR 2)
SET(OpenCV_VERSION_PATCH 0)
SET(OpenCV_VERSION_TWEAK 0)

find_path(OpenCL_INCLUDE_DIR NAMES CL/cl.h PATHS "${OpenCL_ROOT_DIR}/include")
set(OpenCL_INCLUDE_DIRS "${OpenCL_INCLUDE_DIR}")
find_library(OpenCL_LIBRARY NAMES OpenCL.lib PATHS "${OpenCL_ROOT_DIR}/lib") 
set(OpenCL_LIBRARIES ${OpenCL_LIBRARY})

版本问题如何处理

我已经解决了

我参考OpenCVConfig-version.cmake写我的OpenCLConfigVersion.cmake

#OpenCLConfigVersion.cmake

set(OpenCL_VERSION 2.2)
set(PACKAGE_VERSION ${OpenCL_VERSION})

set(PACKAGE_VERSION_EXACT False)
set(PACKAGE_VERSION_COMPATIBLE False)

if(PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION)
  set(PACKAGE_VERSION_EXACT True)
  set(PACKAGE_VERSION_COMPATIBLE True)
endif()

if(PACKAGE_FIND_VERSION_MAJOR EQUAL 2
   AND PACKAGE_FIND_VERSION VERSION_LESS PACKAGE_VERSION)
  set(PACKAGE_VERSION_COMPATIBLE True)
endif()

修改后正常使用