Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make find_package search with config mode and fallback on module mode?

Tags:

c++

cmake

When a library defines a build with CMake and goes through the trouble of building an install package for itself, there will be a XXXConfig.cmake.

If a library doesn't have a way to export it's targets to CMake, CMake tries to bridge the gap by providing FindXXX.cmake scripts that attempt to locate such libraries.

In the docs, FindXXX.cmake (module mode), is attempted first, and only if that fails does it attempt to use XXXConfig.cmake (config mode). But this seems like a really backwards to me.

The problem is, for example, I have built CURL from source, and the ConfigXXX produces a different target name than FindXXX, so, when trying to use it, it fails because FindXXX took responsibility for the find_package request and loaded a different target name than what I was expecting.

Can I at least tell CMake somehow to do things the other way around? Config mode first.

I know I can disable module mode entirely, but I'd rather have it as a fallback option.

like image 919
johnb003 Avatar asked Jun 21 '18 23:06

johnb003


2 Answers

New in version 3.15:

Set CMAKE_FIND_PACKAGE_PREFER_CONFIG to TRUE to tell find_package() to first search using Config mode before falling back to Module mode.

References: 1, 2.

like image 137
user12129795 Avatar answered Sep 18 '22 23:09

user12129795


Just use find_package with CONFIG mode, check its result, and, if result is false, repeat the call with MODULE mode:

# First time do not use common *REQUIRED* but use QUIET for do not output error messages on fail.
find_package(XXX CONFIG QUIET)
if(NOT XXX_FOUND)
    # Previous call has been failed. Fallback with MODULE mode.
    find_package(XXX MODULE REQUIRED) # Now it is OK to use REQUIRED if needed.
    # ... There could be additional actions for wrap result "as if" CONFIG mode.
endif()
# ... use XXX
like image 35
Tsyvarev Avatar answered Sep 19 '22 23:09

Tsyvarev