Skip to content

Commit b00d7b2

Browse files
committed
CMake: Use CMAKE_FIND_PACKAGE_PREFER_CONFIG to control find_package override
1 parent 74e9c61 commit b00d7b2

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

cmake/macros/PackageConfig.cmake

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -483,24 +483,28 @@ endfunction(export_targets)
483483
#
484484
# find_package
485485
#
486-
# This override is necessary because CMake's default behavior is to run
487-
# find_package in MODULE mode, *then* in CONFIG mode. This is silly! CONFIG
488-
# mode makes more sense to be done first, since any system config file will
489-
# know vastly more about the package's configuration than a module can hope to
490-
# guess.
491-
#
492-
macro(find_package name)
493-
if(";${ARGN};" MATCHES ";(CONFIG|MODULE|NO_MODULE);")
494-
# Caller explicitly asking for a certain mode; so be it.
495-
_find_package(${ARGV})
486+
# This override implements CMAKE_FIND_PACKAGE_PREFER_CONFIG on versions of
487+
# CMake too old to include it.
488+
#
489+
if(CMAKE_VERSION VERSION_LESS "3.15")
490+
macro(find_package name)
491+
if(";${ARGN};" MATCHES ";(CONFIG|MODULE|NO_MODULE);")
492+
# Caller explicitly asking for a certain mode; so be it.
493+
_find_package(${ARGV})
494+
495+
elseif(CMAKE_FIND_PACKAGE_PREFER_CONFIG)
496+
# Try CONFIG
497+
_find_package("${name}" CONFIG ${ARGN})
498+
499+
if(NOT ${name}_FOUND)
500+
# CONFIG didn't work, fall back to MODULE
501+
_find_package("${name}" MODULE ${ARGN})
502+
endif()
496503
497-
else()
498-
# Try CONFIG
499-
_find_package("${name}" CONFIG ${ARGN})
504+
else()
505+
# Default behavior
506+
_find_package(${ARGV})
500507
501-
if(NOT ${name}_FOUND)
502-
# CONFIG didn't work, fall back to MODULE
503-
_find_package("${name}" MODULE ${ARGN})
504508
endif()
505-
endif()
506-
endmacro(find_package)
509+
endmacro(find_package)
510+
endif()

dtool/CompilerFlags.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ endif()
6767
# Set certain CMake flags we expect
6868
set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
6969
set(CMAKE_INCLUDE_CURRENT_DIR ON)
70+
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
7071

7172
# Set up the output directory structure, mimicking that of makepanda
7273
set(CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}/cmake")

0 commit comments

Comments
 (0)