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,24 +483,28 @@ endfunction(export_targets)
#
# find_package
#
# This override is necessary because CMake's default behavior is to run
# find_package in MODULE mode, *then* in CONFIG mode. This is silly! CONFIG
# 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.
# This override implements CMAKE_FIND_PACKAGE_PREFER_CONFIG on versions of
# CMake too old to include it.
#
macro(find_package name)
if(";${ARGN};" MATCHES ";(CONFIG|MODULE|NO_MODULE);")
# Caller explicitly asking for a certain mode; so be it.
_find_package(${ARGV})
if(CMAKE_VERSION VERSION_LESS "3.15")
macro(find_package name)
if(";${ARGN};" MATCHES ";(CONFIG|MODULE|NO_MODULE);")
# Caller explicitly asking for a certain mode; so be it.
_find_package(${ARGV})
else()
# Try CONFIG
_find_package("${name}" CONFIG ${ARGN})
elseif(CMAKE_FIND_PACKAGE_PREFER_CONFIG)
# Try CONFIG
_find_package("${name}" CONFIG ${ARGN})
if(NOT ${name}_FOUND)
# CONFIG didn't work, fall back to MODULE
_find_package("${name}" MODULE ${ARGN})
endif()
else()
# Default behavior
_find_package(${ARGV})
if(NOT ${name}_FOUND)
# CONFIG didn't work, fall back to MODULE
_find_package("${name}" MODULE ${ARGN})
endif()
endif()
endmacro(find_package)
endmacro(find_package)
endif()

View File

@ -67,6 +67,7 @@ endif()
# Set certain CMake flags we expect
set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE 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(CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}/cmake")