Skip to content
Snippets Groups Projects
Commit bb7abf82 authored by Ammar Nejati's avatar Ammar Nejati
Browse files

CMake: Add a CMake module 'FindPkgVersion' to find the version of a package from a config file

This module is a simple replacement for `PkgConfig`.
parent 698890d7
No related branches found
No related tags found
1 merge request!411Remove PkgConfig dependence on Windows
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment