这是一个似乎可以工作的查找模块。OpenVR 用它的调试库命名做了一些非常不标准的事情。我在 Linux 上对此进行了测试,它可以工作,但我现在无法轻松访问 Windows 机器。如果失败,让我看看错误消息,我可以尝试修复它。
您将从顶级 CMakeLists.txt 中像这样使用它
# ./CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(test)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(OpenVR REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE OpenVR::OpenVR)
然后只需将以下查找模块放入cmake/FindOpenVR.cmake
:
# cmake/FindOpenVR.cmake
cmake_minimum_required(VERSION 3.22)
function(_OpenVR_find)
include(FindPackageHandleStandardArgs)
include(SelectLibraryConfigurations)
## Find the include path
find_path(OpenVR_INCLUDE_DIR NAMES openvr.h)
## Determine version by scanning header
if (OpenVR_INCLUDE_DIR)
set(openvr_header "${OpenVR_INCLUDE_DIR}/openvr.h")
set(re_major "^\tstatic const uint32_t k_nSteamVRVersionMajor = ([0-9]+).*;$")
set(re_minor "^\tstatic const uint32_t k_nSteamVRVersionMinor = ([0-9]+).*;$")
set(re_patch "^\tstatic const uint32_t k_nSteamVRVersionBuild = ([0-9]+).*;$")
file(STRINGS "${openvr_header}" OpenVR_VERSION_MAJOR REGEX "${re_major}")
file(STRINGS "${openvr_header}" OpenVR_VERSION_MINOR REGEX "${re_minor}")
file(STRINGS "${openvr_header}" OpenVR_VERSION_PATCH REGEX "${re_patch}")
string(REGEX REPLACE "${re_major}" "\\1"
OpenVR_VERSION_MAJOR "${OpenVR_VERSION_MAJOR}")
string(REGEX REPLACE "${re_minor}" "\\1"
OpenVR_VERSION_MINOR "${OpenVR_VERSION_MINOR}")
string(REGEX REPLACE "${re_patch}" "\\1"
OpenVR_VERSION_PATCH "${OpenVR_VERSION_PATCH}")
if (OpenVR_VERSION_MAJOR AND OpenVR_VERSION_MINOR AND OpenVR_VERSION_PATCH)
set(OpenVR_VERSION
"${OpenVR_VERSION_MAJOR}.${OpenVR_VERSION_MINOR}.${OpenVR_VERSION_PATCH}")
endif ()
endif ()
## Find the library
find_library(OpenVR_LIBRARY_RELEASE NAMES openvr_api)
# OpenVR uses a highly non-standard additional suffix to mark debug libraries
list(TRANSFORM CMAKE_FIND_LIBRARY_SUFFIXES APPEND ".dbg")
find_library(OpenVR_LIBRARY_DEBUG NAMES openvr_api)
select_library_configurations(OpenVR)
## Perform all the standard required, version, etc. argument checks.
find_package_handle_standard_args(
OpenVR
REQUIRED_VARS OpenVR_LIBRARY OpenVR_INCLUDE_DIR
VERSION_VAR OpenVR_VERSION
HANDLE_VERSION_RANGE
HANDLE_COMPONENTS
)
## Create OpenVR::OpenVR imported target.
if (OpenVR_FOUND AND NOT OpenVR::OpenVR)
add_library(OpenVR::OpenVR UNKNOWN IMPORTED)
target_include_directories(OpenVR::OpenVR INTERFACE "${OpenVR_INCLUDE_DIR}")
set_target_properties(
OpenVR::OpenVR PROPERTIES IMPORTED_LOCATION "${OpenVR_LIBRARY}"
)
foreach (cfg IN ITEMS RELEASE DEBUG)
if (OpenVR_LIBRARY_${cfg})
set_property(
TARGET OpenVR::OpenVR APPEND PROPERTY IMPORTED_CONFIGURATIONS ${cfg}
)
set_target_properties(
OpenVR::OpenVR PROPERTIES
IMPORTED_LOCATION_${cfg} "${OpenVR_LIBRARY_${cfg}}"
)
endif ()
endforeach ()
endif ()
## Export whitelisted variables
set(OpenVR_FOUND "${OpenVR_FOUND}" PARENT_SCOPE)
set(OpenVR_VERSION "${OpenVR_VERSION}" PARENT_SCOPE)
endfunction()
_OpenVR_find()
function(_OpenVR_find)
endfunction()
function(_OpenVR_find)
endfunction()