CMake: Use CMAKE_FIND_PACKAGE_PREFER_CONFIG to control find_package override

This commit is contained in:
Sam Edwards 2019-12-24 19:17:23 -07:00
parent 74e9c610d3
commit b00d7b23bc
2 changed files with 22 additions and 17 deletions

View File

@ -483,18 +483,16 @@ endfunction(export_targets)
# #
# find_package # find_package
# #
# This override is necessary because CMake's default behavior is to run # This override implements CMAKE_FIND_PACKAGE_PREFER_CONFIG on versions of
# find_package in MODULE mode, *then* in CONFIG mode. This is silly! CONFIG # CMake too old to include it.
# mode makes more sense to be done first, since any system config file will
# know vastly more about the package's configuration than a module can hope to
# guess.
# #
if(CMAKE_VERSION VERSION_LESS "3.15")
macro(find_package name) macro(find_package name)
if(";${ARGN};" MATCHES ";(CONFIG|MODULE|NO_MODULE);") if(";${ARGN};" MATCHES ";(CONFIG|MODULE|NO_MODULE);")
# Caller explicitly asking for a certain mode; so be it. # Caller explicitly asking for a certain mode; so be it.
_find_package(${ARGV}) _find_package(${ARGV})
else() elseif(CMAKE_FIND_PACKAGE_PREFER_CONFIG)
# Try CONFIG # Try CONFIG
_find_package("${name}" CONFIG ${ARGN}) _find_package("${name}" CONFIG ${ARGN})
@ -502,5 +500,11 @@ macro(find_package name)
# CONFIG didn't work, fall back to MODULE # CONFIG didn't work, fall back to MODULE
_find_package("${name}" MODULE ${ARGN}) _find_package("${name}" MODULE ${ARGN})
endif() endif()
else()
# Default behavior
_find_package(${ARGV})
endif() endif()
endmacro(find_package) endmacro(find_package)
endif()

View File

@ -67,6 +67,7 @@ endif()
# Set certain CMake flags we expect # Set certain CMake flags we expect
set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON) set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
# Set up the output directory structure, mimicking that of makepanda # Set up the output directory structure, mimicking that of makepanda
set(CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}/cmake") set(CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}/cmake")