Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request the greatest version with FIND_PACKAGE in CMAKE

Tags:

cmake

I have a folder containing a list of libraries:

libs\libA-1.0
    \libB-1.2
    \libB-1.4
    \libB-1.6 

I am loading such libraries in my cmake scripts using for example

FIND_PACKAGE(libB CONFIG)

CMAKE in this case simply loads the first acceptable version present in the libs folder (in this case libB-1.2 since it is the first found in the folder)

I know that I could request a specific library version using

FIND_PACKAGE(libB 1.6 CONFIG)

but I would like to recover the library with the greatest version number (or at least the latest in lexicographic order), without specifying it (so that I can update the libraries without modify all my cmake scripts).

Is this possible?

like image 491
Pierluigi Avatar asked Sep 06 '25 19:09

Pierluigi


2 Answers

Since CMAKE 3.7.0 this is now possible

To control the order in which find_package checks for compatibiliy use the two variables CMAKE_FIND_PACKAGE_SORT_ORDER and CMAKE_FIND_PACKAGE_SORT_DIRECTION. For instance in order to select the highest version one can set:

SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
like image 147
Pierluigi Avatar answered Sep 11 '25 12:09

Pierluigi


From the latest documentation:

When multiple package configuration files are available whose version files claim compatibility with the version requested it is unspecified which one is chosen. No attempt is made to choose a highest or closest version number.

In fact, if you consider there is no standard on version numbers, it would be a pain to guess what is a "highest or closest" version. Explicitly giving the exact wanted version like you wrote in your question is the best you can do.

If you really want it, you will have have to fork cmake and propose the feature. Something like:

FIND_PACKAGE(libB latest CONFIG)

This will require an alternative version file to compare version number. Something like libB-config-version-compare.cmake containing:

# given two variables to be compared:
# PACKAGE_FIRST_VERSION and PACKAGE_SECOND_VERSION

SET(PACKAGE_LATEST_VERSION ...)
like image 21
rgmt Avatar answered Sep 11 '25 14:09

rgmt