<div dir="ltr">Normally, find modules call FIND_PACKAGE_HANDLE_STANDARD_ARGS, which in turn calls FIND_PACKAGE_MESSAGE, to print a nice message about the location and version of the dependency.<div><br></div><div>But I haven't found any way or example to do the same when using the CONFIG mode of find_package. And I really want to see some output about where the dependencies are located. So I came up with this:</div><div><br></div><div><div><div>    # Prints a message with the location and version of a package</div><div>    # found in config mode, similar to the message printed by module files.</div><div>    # When multiple configurations are installed, this tries to print</div><div>    # the location to the same library selected by the CMake generator.</div><div>    #</div><div>    # TODO: handle configuration mapping (?)</div><div>    function(find_package_config_message _package _target _version)</div><div>      # The location matching the same build configuration as downstream</div><div>      # is selected when linking to the library</div><div>      set(_build_type ${CMAKE_BUILD_TYPE})</div><div>      if (_build_type)</div><div>        string(TOUPPER "IMPORTED_LOCATION_${_build_type}" _location_prop)</div><div>        get_target_property(_location_path ${_target} ${_location_prop})</div><div>      endif()</div><div>      # If downstream has no build configuration set,</div><div>      # or the installed package has no matching configuration,</div><div>      # the first listed configuration is used to get the location to the library</div><div>      if(NOT _location_path)</div><div>        get_target_property(_target_configurations ${_target} IMPORTED_CONFIGURATIONS)</div><div>        if(_target_configurations)</div><div>          list(GET _target_configurations 0 _build_type)</div><div>          get_target_property(_location_path ${_target} "IMPORTED_LOCATION_${_build_type}")</div><div>        endif()</div><div>        if (NOT _location_path)</div><div>          message(SEND_ERROR "No imported location for target ${_target}")</div><div>        endif()</div><div>      endif()</div><div>    </div><div>      set(_message "Found ${_package}")</div><div>      set(_details "[${_build_type}]")</div><div>      if(_location_path)</div><div>        set(_message "${_message}: ${_location_path}")</div><div>        set(_details "[${_location_path}]${_details}")</div><div>      endif()</div><div>      if(_version)</div><div>        set(_message "${_message} (found version \"${_version}\")")</div><div>        set(_details "[${_version}]${_details}")</div><div>      endif()</div><div>      find_package_message(${_package} ${_message} ${_details})</div><div>    endfunction()</div></div><div><br></div><div>I think I got it right, based on the observed behavior while experimenting with different values for CMAKE_BUILD_TYPE on both upstream and downstream, and it works fine for most common cases.</div><div><br></div><div>I would use in the FooConfig.cmake like this (the function would be in FooHelpers.cmake):</div><div><br></div><div><div>    if(NOT TARGET Foo::Foo)</div><div>      include("${CMAKE_CURRENT_LIST_DIR}/FooTargets.cmake")</div><div>      include("${CMAKE_CURRENT_LIST_DIR}/FooHelpers.cmake")</div><div>      find_package_config_message(Foo Foo::Foo "@PROJECT_VERSION@")</div><div>    endif()</div></div><div><br></div><div>Any thoughts/comments?</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div class="gmail_signature"><div dir="ltr"><div style="font-size:small;font-family:arial">Sebastian Mancilla</div></div></div></div></div></div>