+ 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.
This commit is contained in:
Moody 2021-06-30 18:48:25 +08:00
parent 3d60ab1a6d
commit ecc41d64cf
2 changed files with 25 additions and 21 deletions

View File

@ -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
#

View File

@ -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 $<$<TARGET_EXISTS:uv::uv-shared>: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 $<$<TARGET_EXISTS:uv::uv-static>:uv::uv-static> $<$<NOT:$<TARGET_EXISTS: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)