[CMake] Usage requirements for external project dependencies

Alexander Lamaison awl03 at doc.ic.ac.uk
Sun Sep 7 05:32:31 EDT 2014


What is the best way to manage usage requirements for projects and
packages that are not part of your source tree?

Using find_package() to locate a dependency typically results in code
like:

add_executable(my_exe ${SOURCES})
find_package(Foo)
target_compile_definitions(my_exe SOME_DEF_NEEDED_TO_USE_FOO)
target_include_directories(my_exe ${FOO_INCLUDE_DIR})
target_link_libraries(my_exe ${FOO_LIBRARIES})

where the include directories, compiler definitions and libraries (Foo's
usage requirements) have to be found by reading the FindFoo.cmake
documentation.

Since 2.8.11, CMake has a much better approach for propagating usage
requirements between targets in the source tree [1].  The source target
specifies target_compile_definitions/include_directories/link_libraries
itself using the PUBLIC or INTERFACE keyword.  Any target that links
with the source target automatically inherits all the usage
requirements.

What I would like is to be able to do is:

add_executable(my_exe ${SOURCES})
find_package(Foo)
target_link_libraries(my_exe Foo)

and have it Just Work.  Is that possible?

I've tried doing it with ExternalProject instead of find_package:

add_executable(my_exe ${SOURCES})
ExternalProject_Add(Foo ...) # FAILS HERE
target_include_directories(Foo INTERFACE ...)
target_link_libraries(my_exe Foo)

But that fails with "target_include_directories called with
non-compilable target type".

Is there a better way?  Is there something I'm missing?

I'd love to see a solution where adding a dependency with the Hunter
package manager [2] becomes a one-liner once you know the package name.
Right now, you still have to know the package-specific usage
requirements every time.

[1]
http://www.cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html#build-specification-and-usage-requirements
[2] https://github.com/ruslo/hunter

Thanks,

Alex

-- 
Swish - Easy SFTP for Windows Explorer (http://www.swish-sftp.org)



More information about the CMake mailing list