Feature Request: Add find_package for targets in INTERFACE_LINK_LIBRARIES in config files
I am on the latest CMake, Ubuntu 16.04. Suppose I am building a upstream library that depends on an external library that uses targets in their CMake (e.g Boost), so the CMakeLists would look something like.
cmake_minimum_required(VERSION 3.9)
project(mysdk)
add_library(${PROJECT_NAME} mylib.h mylib.cpp)
find_package(Boost COMPONENTS filesystem REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::filesystem)
export(TARGETS FILE ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}Targets.cmake)
file(WRITE ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}Config.cmake
"include(\${CMAKE_CURRENT_LIST_DIR}/${CMAKE_PROJECT_NAME}Targets.cmake)\n")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include
DESTINATION ${CMAKE_INSTALL_PREFIX}
FILES_MATCHING PATTERN "*.h")
install(FILES ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}Config.cmake
DESTINATION lib/cmake/${CMAKE_PROJECT_NAME})
install(EXPORT ${CMAKE_PROJECT_NAME}Targets NAMESPACE mysdk::
DESTINATION lib/cmake/${CMAKE_PROJECT_NAME})
install(TARGETS ${PROJECT_NAME}
EXPORT ${CMAKE_PROJECT_NAME}Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
After mysdk
is built and installed, inside mysdkTargets.cmake
there will be something like
# Create imported target mysdk::mysdk
add_library(mysdk::mysdk IMPORTED)
set_target_properties(mysdk::mysdk PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "/usr/local/include;/usr/local/include"
INTERFACE_LINK_LIBRARIES "Boost::filesystem"
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "/usr/local/include"
)
When I try to use the upstream library mysdk
in my downstream project, I ran into the problem where I had to add find_package(Boost COMPONENTS filesystem REQUIRED)
in my CMakeLists otherwise there will be a target Boost::filesystem
unrecognized error. See below example CMakeLists
cmake_minimum_required(VERSION 3.9)
project(myproject)
add_executable(${PROJECT_NAME} main.cpp)
find_package(Boost COMPONENTS filesystem REQUIRED) # this line is needed otherwise there will be error
find_package(mysdk REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC mysdk::mysdk)
IMO for a smoother experience for the user of the downstream projects, it would make sense to completely abstract away the boost dependency into mysdkTargets.cmake
. An obvious way to achieve this is somehow embed the find_package(Boost COMPONENTS filesystem REQUIRED)
into mysdkTargets.cmake
. Could we add this feature in future CMake releases?