From ecc41d64cf87ebc5affe7edcb31fa2e25fea0249 Mon Sep 17 00:00:00 2001 From: Moody <76251897+moodyhunter@users.noreply.github.com> Date: Wed, 30 Jun 2021 18:48:25 +0800 Subject: [PATCH] + added CMake package configuration files Previously, uvw provides only a pkg-config .pc file, although a uvw.cmake presents, it's not properly installed and placed in to a correct cmake `find_package` search path. This leads to many problems, where in this commit, a `uvw::uvw` ALIAS target is created for uvw static library, - if `BUILD_UVW_LIBS` is set, FETCH_LIBUV must be set as well (otherwise it'll not build, it's the current behavior). - if `BUILD_UVW_LIBS` is NOT set: - when `FETCH_LIBUV` is set, it's guaranteed that uv::uv-static target exists, so a genex is removed. - when `FETCH_LIBUV` is NOT set, it's the caller's responsibility to make sure libuv can be properly linked This commit also fixes CMake package config files for uvw, they are now installed as ${LIBDIR}/cmake/uvw/uvwConfig.cmake with target files for appropriate configuration (e.g. debug, release, etc...). Callers can now use `find_package(uvw)` and link with the `uvw::uvw` target to use uvw in their projects directly using CMake. --- CMakeLists.txt | 12 ++++++++++++ src/CMakeLists.txt | 34 +++++++++++++--------------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b82fb73..b67fef43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -155,6 +155,18 @@ install( PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ ) +# +# Install targets +# + +install(EXPORT uvwConfig NAMESPACE uvw:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/uvw) +install(TARGETS uvw EXPORT uvwConfig ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) +if(FETCH_LIBUV AND BUILD_UVW_LIBS) + # libuv is only fetched when both above conditions are true + install(TARGETS uv_a EXPORT uvwConfig ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS uv EXPORT uvwConfig LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif(FETCH_LIBUV AND BUILD_UVW_LIBS) + # # Pkg-Config # diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 414f6c3c..3e949e47 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,34 +57,26 @@ function(add_uvw_library LIB_NAME) endif() endfunction() -# +# # Build and install libraries # if (BUILD_UVW_SHARED_LIB) - add_library(uvw-shared SHARED) - add_library(uvw::uvw-shared ALIAS uvw-shared) - target_link_libraries(uvw-shared PUBLIC $<$:uv::uv-shared>) - set_target_properties(uvw-shared PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1) - - add_uvw_library(uvw-shared) - - install(TARGETS uvw-shared EXPORT uvw ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}) - + add_library(uvw SHARED) + add_library(uvw::uvw-shared ALIAS uvw) + # If libuv is not fetched by ourselves, it's the caller's responsibility to make sure of the linkage. if(FETCH_LIBUV) - install(TARGETS uv_a EXPORT uvw ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + target_link_libraries(uvw PUBLIC uv::uv-shared) endif() else() - add_library(uvw-static STATIC) - add_library(uvw::uvw-static ALIAS uvw-static) - target_link_libraries(uvw-static PUBLIC $<$:uv::uv-static> $<$>:uv_a dl>) - set_target_properties(uvw-static PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1) - - add_uvw_library(uvw-static) - - install(TARGETS uvw-static EXPORT uvw ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) - + add_library(uvw STATIC) + add_library(uvw::uvw-static ALIAS uvw) + # If libuv is not fetched by ourselves, it's the caller's responsibility to make sure of the linkage. if(FETCH_LIBUV) - install(TARGETS uv EXPORT uvw LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) + target_link_libraries(uvw PUBLIC uv::uv-static) endif() endif() + +add_library(uvw::uvw ALIAS uvw) +set_target_properties(uvw PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1) +add_uvw_library(uvw)