Skip to content

find_package: Support version ranges with package version files

In a CMake file, one should be able to specify a version range spanning multiple major and minor numbers, which will match any version in the range. Right now - that does not happen. It seems CMake requires a major-version match for both ends of the range (edit: Or offloads version range matching to the package config file, which may not bother to offer the relevant logic).

Motivating example:

I am writing an app which depends on a certain package/library, mypkg, and can accept all versions between, say, 0.3.3 and 0.5.0, not including 0.5. If I supply it with version 0.4.6 (edit: specified by by a mypkg-config-version.cmake file, but without special support from mypkg for wide version ranges) - this will be rejected by CMake. Specifically, I have:

find_package(mypkg 0.3.3...<0.5.0 REQUIRED)

but when I configure, that line yields

CMake Error at CMakeLists.txt:24 (find_package):
  Could not find a configuration file for package "mypkg" that is
  compatible with requested version range "0.3.3...<0.5.0".

  The following configuration files were considered but not accepted:

    /opt/mypkg/lib64/cmake/mypkg/mypkg-config.cmake, version: 0.4.6

... even though I said very clearly what range is compatible.

Edited by Eyal Rozenberg
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information