[feat/workaround] proper way to wrap an imported target around a third-party target
Hi,
I'm trying to enforce namespace usage for imported libraries.
For third-party libraries that can be imported through find_package but the don't provide a namespace, I tried to wrap them in a namespace (see attached file)
I have the following situation:
application <client> depends privately on a home-made (static)library <mylib> which depends privately on a third-party library <third>.
When declaring dependency to <third> in <mylib>, I wrap it into third::third.
on CMake 3.19.2 it works fine
on CMake 3.5.1 (that I must support as it is the default version on Ubuntu 16.04), when configuring <client>, I have the following error:
Target "client" links to target "third::third" but the target was not found...
Why the difference in behaviour and would it be possible to have a "clean" workaround?
Regards NamespaceWrapping.txt
# MyLib CMakeLists
...
find_package(third REQUIRED)
# Work around for third-party package that don't use namespace
IF(third_FOUND AND NOT TARGET third::third)
add_library(third::third UNKNOWN IMPORTED)
set_target_properties(third::third PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
IMPORTED_LOCATION "${third_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${third_INCLUDE_DIR}"
)
ENDIF()
target_link_libraries(${TARGET_NAME} PRIVATE third::third)
# Then export and install mylib::mylib
...
# client CMakeLists
find_package(mylib REQUIRED)
target_link_libraries(${TARGET_NAME} PRIVATE mylib::mylib)