How can I make find_package search with config mode and fallback on module mode?
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
New in version 3.15:
Set
CMAKE_FIND_PACKAGE_PREFER_CONFIG
toTRUE
to tellfind_package()
to first search using Config mode before falling back to Module mode.
References: 1, 2.