Installlation-cum-configuration of a project should be better automated
(I'm filing this as an official issue, although it's possible the solution is more in terms of use patterns / documentation / third-party modules. )
According to the oft-linked-to talk by Daniel Pfeiffer (slides here), this is an example of how to allow for project installation and for its properly-configured use in other projects:
In CMakeLists.txt
:
find_package(Bar 2.0 REQUIRED)
add_library(Foo ...)
target_link_libraries(Foo PRIVATE Bar::Bar)
install(TARGETS Foo EXPORT FooTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
install(EXPORT FooTargets
FILE FooTargets.cmake
NAMESPACE Foo::
DESTINATION lib/cmake/Foo
)
in a second file (which?):
include(CMakePackageConfigHelpers)
write_basic_package_version_file("FooConfigVersion.cmake"
VERSION ${Foo_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES "FooConfig.cmake" "FooConfigVersion.cmake"
DESTINATION lib/cmake/Foo
)
in a third file (which?):
include(CMakeFindDependencyMacro)
find_dependency(Bar 2.0)
include("${CMAKE_CURRENT_LIST_DIR}/FooTargets.cmake")
So, we have:
- At least four different CMake files (
CMakeLists.txt
,FooTargets.cmake
,FooConfig.cmake
,FooConfigVersion.cmake
) - ... more than one of which is generated, but more than one is written by hand
- ... and which also include each other
- ... and whose names do not make it clearer which does what
- ... with some, but not all of them, getting installed.
Shouldn't more of this boilerplate code be subsumed by the INSTALL command or elsewhere? Especially considering what is effectively some duplication of code?