Skip to content

Instantly share code, notes, and snippets.

@diablodale
Created May 7, 2020 15:52
Show Gist options
  • Save diablodale/471341d2e139b0b42f4270c78f031378 to your computer and use it in GitHub Desktop.
Save diablodale/471341d2e139b0b42f4270c78f031378 to your computer and use it in GitHub Desktop.
cmake helper function to require a specific semver or newer of any vcpkg installed package
# cmake helper function to require a specific semver or newer
# of any vcpkg installed package
# Example: include("version-vcpkg.cmake")
# confirm_vcpkg_version(packagename 2.1.0)
#
cmake_minimum_required(VERSION 3.15)
function(confirm_vcpkg_version)
if ((NOT ARGV0) OR (NOT ARGV1))
message(
SEND_ERROR
"confirm_vcpkg_version() must have package and semver arguments"
)
return()
endif()
find_program(
VCPKG_EXE
NAMES vcpkg.exe
HINTS ENV VCPKG_ROOT
)
if (${VCPKG_EXE} STREQUAL "VCPKG_EXE-NOTFOUND")
message(
SEND_ERROR
"vcpkg.exe not found in %VCPKG_ROOT% or path"
)
return()
endif()
execute_process(
COMMAND "${VCPKG_EXE}" list ${ARGV0}
TIMEOUT 4
#RESULT_VARIABLE EXIT_CODE
OUTPUT_VARIABLE VCPKG_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE
ENCODING AUTO
)
string(
REGEX MATCH "${ARGV0}[^ \t]*[ \t]+([^ \t]+)"
VCPKG_PACKAGE_VERSION
${VCPKG_OUTPUT}
)
if (CMAKE_MATCH_1 VERSION_LESS ARGV1)
message(
SEND_ERROR
"vcpkg package ${ARGV0} ${CMAKE_MATCH_1} is older than requirement ${ARGV1}"
)
return()
endif()
endfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment