find_package_handle_standard_args terminates though not REQUIRED

Searching for library “Cerf”

# FindCerf.cmake:
...
find_path(Cerf_INCLUDE_DIR cerf.h)
find_library(Cerf_LIBRARIES NAMES cerf)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Cerf DEFAULT_MSG Cerf_LIBRARIES Cerf_INCLUDE_DIR)

terminates with error

CMake Error at C:/Program Files/CMake/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
  Could NOT find Cerf (missing: Cerf_LIBRARIES)Call Stack (most recent call first): 
    C:/Program Files/CMake/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
    cmake/FindCerf.cmake:19 (find_package_handle_standard_args)
    CMakeLists.txt:83 (find_package)

though I did not say REQUIRED. Why?

Unless I’m not looking at the right CMakeLists.txt file, there is the following written on CMakeLists.txt:83:

find_package(Cerf MODULE REQUIRED)
1 Like

Incredible - what a phantastic debugging help - you found indeed the pertinent calling CMakeLists.txt - thank you so much, Alain!

By what mechanism is the REQUIRED flag transmitted to the find_package_handle_standard_args function without explicitly appearing in the argument list?

When calling find_package(Cerf MODULE REQUIRED), CMake defines several variables (the complete list is here: https://cmake.org/cmake/help/latest/command/find_package.html#package-file-interface-variables), including Cerf_FIND_REQUIRED.

Then find_package_handle_standard_args uses whether <PackageName>_FIND_REQUIRED is true to decide whether it should use message(FATAL_ERROR), message(STATUS) or do nothing when the package is not found. See https://gitlab.kitware.com/cmake/cmake/blob/v3.16.0/Modules/FindPackageHandleStandardArgs.cmake#L145-150 for the actual code.

1 Like