diff --git a/cmake/find/FindPkgVersion.cmake b/cmake/find/FindPkgVersion.cmake new file mode 100644 index 0000000000000000000000000000000000000000..8df7b667df720367f229cc9e121bbf7d8ef24ac5 --- /dev/null +++ b/cmake/find/FindPkgVersion.cmake @@ -0,0 +1,48 @@ +cmake_minimum_required(VERSION 3.12 FATAL_ERROR) + +# Find a package version from a configuration file +function(findPkgVersion libname conffile paths output_var) + + string(STRIP "${paths}" _paths) + string(STRIP "${conffile}" _pkgconffile) + + # find the configuration file in the given paths + if(NOT _pkgconffile) + set(_pkgconffile "lib${libname}.pc") + endif() + + # initialize the output variable + if(NOT output_var) + set(_outvar ${libname}_VERSION) + else() + set(_outvar ${output_var}) + endif() + set(${_outvar} "${_outvar}-NOTFOUND" PARENT_SCOPE) + + set(_path_sfx pkgconfig) # search subdirectories + + # NOTE: ${libname}_PATH variable will be cached + find_path("${libname}_PATH" "${_pkgconffile}" + PATHS "${_paths}" PATH_SUFFIXES "${_path_sfx}" + DOC "Package configuration for ${_pkgconffile}") + + # NOTE: + # A CMake value is False if the constant is 0, OFF, NO, FALSE, + # N, IGNORE, "", or ends in the suffix '-NOTFOUND'. + if(${libname}_PATH) + message(STATUS "'${_pkgconffile}' found in '${${libname}_PATH}'") + else() + message(WARNING "'${_pkgconffile}' not found in paths {${_paths}}") + return() + endif() + + # extract the configuration information + file(READ "${${libname}_PATH}/${_pkgconffile}" _conf) + string(TOLOWER "${_conf}" _conf_lc) + string(REGEX MATCH "version[: \t]*([1-9.]+)" _rxmatch ${_conf_lc}) + + if(${CMAKE_MATCH_1}) + set(${_outvar} "${CMAKE_MATCH_1}" PARENT_SCOPE) + endif () + +endfunction()