I have a project that search for an optional dependency using either find_package, or pkg-config, as follows in its CMakeLists.txt:
find_package (PkgConfig REQUIRED)
if (ENABLE_LEVELDB) # leveldb is searched via pkg-config
pkg_check_modules (leveldb REQUIRED IMPORTED_TARGET leveldb)
set (HAS_LEVELDB ON)
endif ()
if (ENABLE_GDBM) # gdbm is searched via find_package
find_package (GDBM REQUIRED)
set (HAS_GDBM ON)
endif ()
The project creates a myproject-config.cmake
from a myproject-config.cmake.in
which for now contains the following:
...
find_dependency (PkgConfig)
find_dependency (gdbm)
...
My understanding is find_dependency(gdbm)
will actually search for GDBM only if find_package(gdbm REQUIRED)
has been called. What is the best way to do something similar for packages found with pkg-config?
Right now the only solution I can think of is setting a MYPROJECT_HAS_LEVELDB
to ON
or OFF
in CMakeLists.txt and have something like the following in myproject-config.cmake.in
:
if (@MYPROJECT_HAS_LEVELDB@)
pkg_check_modules (leveldb REQUIRED IMPORTED_TARGET leveldb)
endif ()
Is there a cleaner way?