Possible bug in `find_dependency` or the correct usage thereof
Hi,
I am trying to use find_dependency
macro in my *-config.cmake
file to list the dependencies. Because the minimum CMake version that supports forwarding arguments to the find_package
function is v3.9.0
, I have bumped my version accordingly.
In my project, I am using the following lines in my CMakeLists.txt
:
cmake_minimum_required(VERSION 3.9.0)
project(polo VERSION 1.0.0 LANGUAGES C CXX)
find_package(Threads REQUIRED)
find_package(LAPACK REQUIRED NAMES OpenBLAS lapack)
find_package(ZeroMQ REQUIRED)
find_package(cereal REQUIRED)
find_package(CURL QUIET)
add_library(polo INTERFACE)
add_library(polo::polo ALIAS polo)
target_compile_features(polo INTERFACE cxx_std_11)
if (CURL_FOUND)
target_compile_definitions(polo INTERFACE POLO_WITH_CURL)
endif()
# ...
Similarly, I use the following lines in my polo-config.cmake
file:
include(CMakeFindDependencyMacro)
find_dependency(Threads REQUIRED)
find_dependency(LAPACK REQUIRED NAMES OpenBLAS lapack)
find_dependency(ZeroMQ REQUIRED)
find_dependency(cereal REQUIRED)
find_dependency(CURL QUIET)
include("${CMAKE_CURRENT_LIST_DIR}/polo-targets.cmake")
You can check the GitHub repository for the full CMakeLists.txt
under src
directory.
When I try to build and install the library on a system where cURL is not installed, everything goes smooth. When I check the polo-targets.cmake
file, I see the following lines:
set_target_properties(polo::polo PROPERTIES
INTERFACE_COMPILE_FEATURES "cxx_std_11"
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "Threads::Threads;libzmq;cereal;OpenBLAS::OpenBLAS"
)
which is the expected result. When I read the help:
~> cmake --help-module CMakeFindDependencyMacro
CMakeFindDependencyMacro
-------------------------
::
find_dependency(<dep> [...])
``find_dependency()`` wraps a ``find_package()`` call for a package
dependency. It is designed to be used in a <package>Config.cmake file, and it
forwards the correct parameters for QUIET and REQUIRED which were passed to
the original ``find_package()`` call. It also sets an informative
diagnostic message if the dependency could not be found.
Any additional arguments specified are forwarded to ``find_package()``.
I just assume that QUIET
should have been passed to find_package
properly. However, when I use a dummy CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.9.0)
project(polo-experiments)
find_package(Boost REQUIRED COMPONENTS program_options)
find_package(polo CONFIG REQUIRED)
enable_testing()
I get the following error:
-- Boost version: 1.57.0
-- Found the following Boost libraries:
-- program_options
CMake Error at /home/112018-00062/local/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR)
Call Stack (most recent call first):
/home/112018-00062/local/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:377 (_FPHSA_FAILURE_MESSAGE)
/home/112018-00062/local/share/cmake/Modules/FindCURL.cmake:48 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
/home/112018-00062/local/share/cmake/Modules/CMakeFindDependencyMacro.cmake:37 (find_package)
/home/112018-00062/local/lib64/cmake/polo/polo-config.cmake:6 (find_dependency)
CMakeLists.txt:6 (find_package)
Is this a possible bug in find_dependency
, or am I using either polo-config.cmake
file or the dummy CMakeLists.txt
file in a wrong way? To me, the dummy project which requires polo
has to write the find_package(polo CONFIG REQUIRED)
. But the REQUIRED
option seems to override QUIET
option of polo
's find_dependency
.
I have tried changing the polo-config.cmake
file to read as follows:
find_package(Threads REQUIRED)
find_package(LAPACK REQUIRED NAMES OpenBLAS lapack)
find_package(ZeroMQ REQUIRED)
find_package(cereal REQUIRED)
find_package(CURL QUIET)
include("${CMAKE_CURRENT_LIST_DIR}/polo-targets.cmake")
which seems to have solved the problem. Then, I have one last question:
- Should I simply use the modified
polo-config.cmake
as above? If yes, why should I even think of usingfind_dependency
? I was a bit daunted when I read the help onfind_dependency
, i.e., when I saw that "It is designed to be used in a Config.cmake file, ..." I thought I should not usefind_package
in the config files.