cmakevcpkgopenvr

Linking OpenVR through VCPKG via CMAKE


I have Installed OpenVR through vcpkg and am trying to link it via CMAKE, the issue is that I don't know how to do it, nor can I find any info on how to.

Unlike other packages where once installed it tells you how to link via CMAKE by using find_package and target_link_libraries, OpenVR doesn't. I initially assumed that

find_package(openvr CONFIG REQUIRED) 
target_link_libraries(${Proj_Name} PRIVATE openvr::openvr)

would work but instead I get the error

CMake Error at C:/dev/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake:793 (_find_package):
Could not find a package configuration file provided by "openvr" with any
of the following names:

openvrConfig.cmake
openvr-config.cmake

Add the installation prefix of "openvr" to CMAKE_PREFIX_PATH or set
"openvr_DIR" to a directory containing one of the above files.  If "openvr"
provides a separate development package or SDK, be sure it has been
installed.

Usually vcpkg.cmake provides CMAKE with the config.cmake files but not for OpenVR, so how do i link it properly?


Solution

  • Here is a find module that seems to work. OpenVR does some very non-standard things with its debug library naming. I tested this on Linux, and it works, but I do not have easy access to a Windows machine right now. Let me see the error message if it fails and I can try to fix it.

    You'll use it like this, from the top-level 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)
    

    And then just put the following find module in 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()