cmakecmake-modules

How can I get the version argument of a find_package() call in my find module script?


I have custom Find.cmake. Is it possible to retrieve version, passed to the find_package() call, inside of it?


Solution

  • The requested version can be extracted via <PackageName>_FIND_VERSION variable, where <PackageName> substitution denotes the package name (the first parameter of the find_package):

    if(Foo_FIND_VERSION VERSION_LESS 2.4)
      # search for the old variant of the package Foo
    else()
      # search for the new variant of the package Foo
    endif()
    

    There are also other variables, which describes the version request in more details. See documentation for find_package.


    Note, that if your Find script searches the only instance of the package, then you don't need to explicitly check the requested version: just extract version of the package which you have found, and pass it via VERSION_VAR parameter of find_package_handle_standard_args helper. That helper will automatically checks whether requested version is compatible with the one which has been found.