Skip to content

Commit

Permalink
Cmake static or shared (#1160)
Browse files Browse the repository at this point in the history
* cmake: build either static or shared libs

* cmake: allow to build non-PIC static libs

* fix typo

* cmake: add ALIAS targets

* cmake: link to OpenSSL imported targets

CMake imported targets are more robust

* turn ENABLE_EXAMPLES to a CMake option

* fix typo

* install pdb files if shared

* fix hiredis_ssl-config file

* Fix more targets

* CMake knows when to enable CMAKE_POSITION_INDEPENDENT_CODE

* Restore setting of /Z7

* [ci] fix building of shared and static libs

* Apply suggestions from code review

Co-authored-by: Bjorn Svensson <bjorn.a.svensson@est.tech>

* Make it possible to change name of exported target

---------

Co-authored-by: SpaceIm <30052553+SpaceIm@users.noreply.github.com>
Co-authored-by: Bjorn Svensson <bjorn.a.svensson@est.tech>
  • Loading branch information
3 people authored Mar 8, 2023
1 parent 1cbd5bc commit e9243d4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 95 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ jobs:
steps:
- uses: microsoft/setup-msbuild@v1.0.2
- uses: actions/checkout@v3
- name: Run CMake
run: cmake -Wno-dev CmakeLists.txt
- name: Build hiredis
- name: Run CMake (shared lib)
run: cmake -Wno-dev CMakeLists.txt
- name: Build hiredis (shared lib)
run: MSBuild hiredis.vcxproj /p:Configuration=Debug
- name: Run CMake (static lib)
run: cmake -Wno-dev CMakeLists.txt -DBUILD_SHARED_LIBS=OFF
- name: Build hiredis (static lib)
run: MSBuild hiredis.vcxproj /p:Configuration=Debug
- name: Build hiredis_static
run: MSBuild hiredis_static.vcxproj /p:Configuration=Debug
- name: Build hiredis-test
run: MSBuild hiredis-test.vcxproj /p:Configuration=Debug
# use memurai, redis compatible server, since it is easy to install. Can't
Expand Down
107 changes: 29 additions & 78 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON)
OPTION(ENABLE_SSL "Build hiredis_ssl for SSL support" OFF)
OPTION(DISABLE_TESTS "If tests should be compiled or not" OFF)
OPTION(ENABLE_SSL_TESTS "Should we test SSL connections" OFF)
OPTION(ENABLE_EXAMPLES "Enable building hiredis examples" OFF)
OPTION(ENABLE_ASYNC_TESTS "Should we run all asynchronous API tests" OFF)

MACRO(getVersionBit name)
Expand All @@ -25,11 +26,8 @@ INCLUDE(GNUInstallDirs)

# Hiredis requires C99
SET(CMAKE_C_STANDARD 99)
SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
SET(CMAKE_DEBUG_POSTFIX d)

SET(ENABLE_EXAMPLES OFF CACHE BOOL "Enable building hiredis examples")

SET(hiredis_sources
alloc.c
async.c
Expand All @@ -45,50 +43,27 @@ IF(WIN32)
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -DWIN32_LEAN_AND_MEAN)
ENDIF()

ADD_LIBRARY(hiredis_static STATIC ${hiredis_sources})
ADD_LIBRARY(hiredis::hiredis_static ALIAS hiredis_static)
SET(HIREDIS_DEFAULT_LIBRARY hiredis_static)
SET(HIREDIS_TARGETS hiredis_static)

IF(NOT MSVC)
SET_TARGET_PROPERTIES(hiredis_static
PROPERTIES OUTPUT_NAME hiredis)
ENDIF()
ADD_LIBRARY(hiredis ${hiredis_sources})
ADD_LIBRARY(hiredis::hiredis ALIAS hiredis)
set(hiredis_export_name hiredis CACHE STRING "Name of the exported target")
set_target_properties(hiredis PROPERTIES EXPORT_NAME ${hiredis_export_name})

IF(BUILD_SHARED_LIBS)
ADD_LIBRARY(hiredis SHARED ${hiredis_sources})
ADD_LIBRARY(hiredis::hiredis ALIAS hiredis)
SET(HIREDIS_DEFAULT_LIBRARY hiredis)
SET(HIREDIS_TARGETS ${HIREDIS_TARGETS} hiredis)
SET_TARGET_PROPERTIES(hiredis
PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE
VERSION "${HIREDIS_SONAME}")
ENDIF()
SET_TARGET_PROPERTIES(hiredis
PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE
VERSION "${HIREDIS_SONAME}")
IF(MSVC)
SET_TARGET_PROPERTIES(hiredis_static
SET_TARGET_PROPERTIES(hiredis
PROPERTIES COMPILE_FLAGS /Z7)
ENDIF()
IF(WIN32 OR MINGW)
IF(BUILD_SHARED_LIBS)
TARGET_LINK_LIBRARIES(hiredis PUBLIC ws2_32 crypt32)
ENDIF()
TARGET_LINK_LIBRARIES(hiredis_static PUBLIC ws2_32 crypt32)
IF(WIN32)
TARGET_LINK_LIBRARIES(hiredis PUBLIC ws2_32 crypt32)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
IF(BUILD_SHARED_LIBS)
TARGET_LINK_LIBRARIES(hiredis PUBLIC m)
ENDIF()
TARGET_LINK_LIBRARIES(hiredis_static PUBLIC m)
TARGET_LINK_LIBRARIES(hiredis PUBLIC m)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "SunOS")
IF(BUILD_SHARED_LIBS)
TARGET_LINK_LIBRARIES(hiredis PUBLIC socket)
ENDIF()
TARGET_LINK_LIBRARIES(hiredis_static PUBLIC socket)
TARGET_LINK_LIBRARIES(hiredis PUBLIC socket)
ENDIF()

IF(BUILD_SHARED_LIBS)
TARGET_INCLUDE_DIRECTORIES(hiredis PUBLIC $<INSTALL_INTERFACE:include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
ENDIF()
TARGET_INCLUDE_DIRECTORIES(hiredis_static PUBLIC $<INSTALL_INTERFACE:include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
TARGET_INCLUDE_DIRECTORIES(hiredis PUBLIC $<INSTALL_INTERFACE:include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

CONFIGURE_FILE(hiredis.pc.in hiredis.pc @ONLY)

Expand Down Expand Up @@ -118,7 +93,7 @@ set(CPACK_RPM_PACKAGE_AUTOREQPROV ON)

include(CPack)

INSTALL(TARGETS ${HIREDIS_TARGETS}
INSTALL(TARGETS hiredis
EXPORT hiredis-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down Expand Up @@ -179,52 +154,28 @@ IF(ENABLE_SSL)
FIND_PACKAGE(OpenSSL REQUIRED)
SET(hiredis_ssl_sources
ssl.c)

ADD_LIBRARY(hiredis_ssl_static STATIC
${hiredis_ssl_sources})
SET(HIREDIS_SSL_DEFAULT_LIBRARY hiredis_ssl_static)
SET(HIREDIS_SSL_TARGETS hiredis_ssl_static)
IF(BUILD_SHARED_LIBS)
ADD_LIBRARY(hiredis_ssl SHARED
${hiredis_ssl_sources})
SET(HIREDIS_SSL_DEFAULT_LIBRARY hiredis_ssl)
SET(HIREDIS_SSL_TARGETS ${HIREDIS_SSL_TARGETS} hiredis_ssl)
ENDIF()
IF(NOT MSVC)
SET_TARGET_PROPERTIES(hiredis_ssl_static
PROPERTIES OUTPUT_NAME hiredis_ssl)
ENDIF()
ADD_LIBRARY(hiredis_ssl ${hiredis_ssl_sources})
ADD_LIBRARY(hiredis::hiredis_ssl ALIAS hiredis_ssl)

IF (APPLE AND BUILD_SHARED_LIBS)
SET_PROPERTY(TARGET hiredis_ssl PROPERTY LINK_FLAGS "-Wl,-undefined -Wl,dynamic_lookup")
ENDIF()

IF(BUILD_SHARED_LIBS)
SET_TARGET_PROPERTIES(hiredis_ssl
PROPERTIES
WINDOWS_EXPORT_ALL_SYMBOLS TRUE
VERSION "${HIREDIS_SONAME}")
ENDIF()
SET_TARGET_PROPERTIES(hiredis_ssl
PROPERTIES
WINDOWS_EXPORT_ALL_SYMBOLS TRUE
VERSION "${HIREDIS_SONAME}")
IF(MSVC)
SET_TARGET_PROPERTIES(hiredis_ssl_static
SET_TARGET_PROPERTIES(hiredis_ssl
PROPERTIES COMPILE_FLAGS /Z7)
ENDIF()

TARGET_INCLUDE_DIRECTORIES(hiredis_ssl_static PRIVATE "${OPENSSL_INCLUDE_DIR}")
IF(BUILD_SHARED_LIBS)
TARGET_INCLUDE_DIRECTORIES(hiredis_ssl PRIVATE "${OPENSSL_INCLUDE_DIR}")
TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE ${OPENSSL_LIBRARIES})
ENDIF()

IF (WIN32 OR MINGW)
IF (BUILD_SHARED_LIBS)
TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE hiredis)
ENDIF()
TARGET_LINK_LIBRARIES(hiredis_ssl_static PUBLIC hiredis_static)
TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE OpenSSL::SSL)
IF(WIN32)
TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE hiredis)
ENDIF()
CONFIGURE_FILE(hiredis_ssl.pc.in hiredis_ssl.pc @ONLY)

INSTALL(TARGETS ${HIREDIS_SSL_TARGETS}
INSTALL(TARGETS hiredis_ssl
EXPORT hiredis_ssl-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down Expand Up @@ -267,10 +218,10 @@ ENDIF()
IF(NOT DISABLE_TESTS)
ENABLE_TESTING()
ADD_EXECUTABLE(hiredis-test test.c)
TARGET_LINK_LIBRARIES(hiredis-test ${HIREDIS_DEFAULT_LIBRARY})
TARGET_LINK_LIBRARIES(hiredis-test hiredis)
IF(ENABLE_SSL_TESTS)
ADD_DEFINITIONS(-DHIREDIS_TEST_SSL=1)
TARGET_LINK_LIBRARIES(hiredis-test ${HIREDIS_SSL_DEFAULT_LIBRARY})
TARGET_LINK_LIBRARIES(hiredis-test hiredis_ssl)
ENDIF()
IF(ENABLE_ASYNC_TESTS)
ADD_DEFINITIONS(-DHIREDIS_TEST_ASYNC=1)
Expand All @@ -282,5 +233,5 @@ ENDIF()

# Add examples
IF(ENABLE_EXAMPLES)
ADD_SUBDIRECTORY(examples)
ADD_SUBDIRECTORY(examples)
ENDIF(ENABLE_EXAMPLES)
20 changes: 10 additions & 10 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (GLIB2_FOUND)
INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS})
LINK_DIRECTORIES(${GLIB2_LIBRARY_DIRS})
ADD_EXECUTABLE(example-glib example-glib.c)
TARGET_LINK_LIBRARIES(example-glib ${HIREDIS_DEFAULT_LIBRARY} ${GLIB2_LIBRARIES})
TARGET_LINK_LIBRARIES(example-glib hiredis ${GLIB2_LIBRARIES})
ENDIF(GLIB2_FOUND)

FIND_PATH(LIBEV ev.h
Expand All @@ -16,46 +16,46 @@ FIND_PATH(LIBEV ev.h
if (LIBEV)
# Just compile and link with libev
ADD_EXECUTABLE(example-libev example-libev.c)
TARGET_LINK_LIBRARIES(example-libev ${HIREDIS_DEFAULT_LIBRARY} ev)
TARGET_LINK_LIBRARIES(example-libev hiredis ev)
ENDIF()

FIND_PATH(LIBEVENT event.h)
if (LIBEVENT)
ADD_EXECUTABLE(example-libevent example-libevent.c)
TARGET_LINK_LIBRARIES(example-libevent ${HIREDIS_DEFAULT_LIBRARY} event)
TARGET_LINK_LIBRARIES(example-libevent hiredis event)
ENDIF()

FIND_PATH(LIBHV hv/hv.h)
IF (LIBHV)
ADD_EXECUTABLE(example-libhv example-libhv.c)
TARGET_LINK_LIBRARIES(example-libhv ${HIREDIS_DEFAULT_LIBRARY} hv)
TARGET_LINK_LIBRARIES(example-libhv hiredis hv)
ENDIF()

FIND_PATH(LIBUV uv.h)
IF (LIBUV)
ADD_EXECUTABLE(example-libuv example-libuv.c)
TARGET_LINK_LIBRARIES(example-libuv ${HIREDIS_DEFAULT_LIBRARY} uv)
TARGET_LINK_LIBRARIES(example-libuv hiredis uv)
ENDIF()

FIND_PATH(LIBSDEVENT systemd/sd-event.h)
IF (LIBSDEVENT)
ADD_EXECUTABLE(example-libsdevent example-libsdevent.c)
TARGET_LINK_LIBRARIES(example-libsdevent ${HIREDIS_DEFAULT_LIBRARY} systemd)
TARGET_LINK_LIBRARIES(example-libsdevent hiredis systemd)
ENDIF()

IF (APPLE)
FIND_LIBRARY(CF CoreFoundation)
ADD_EXECUTABLE(example-macosx example-macosx.c)
TARGET_LINK_LIBRARIES(example-macosx ${HIREDIS_DEFAULT_LIBRARY} ${CF})
TARGET_LINK_LIBRARIES(example-macosx hiredis ${CF})
ENDIF()

IF (ENABLE_SSL)
ADD_EXECUTABLE(example-ssl example-ssl.c)
TARGET_LINK_LIBRARIES(example-ssl ${HIREDIS_DEFAULT_LIBRARY} ${HIREDIS_SSL_DEFAULT_LIBRARY})
TARGET_LINK_LIBRARIES(example-ssl hiredis hiredis_ssl)
ENDIF()

ADD_EXECUTABLE(example example.c)
TARGET_LINK_LIBRARIES(example ${HIREDIS_DEFAULT_LIBRARY})
TARGET_LINK_LIBRARIES(example hiredis)

ADD_EXECUTABLE(example-push example-push.c)
TARGET_LINK_LIBRARIES(example-push ${HIREDIS_DEFAULT_LIBRARY})
TARGET_LINK_LIBRARIES(example-push hiredis)
4 changes: 2 additions & 2 deletions hiredis-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

set_and_check(hiredis_INCLUDEDIR "@PACKAGE_INCLUDE_INSTALL_DIR@")

IF (NOT TARGET hiredis::hiredis)
IF (NOT TARGET hiredis::@hiredis_export_name@)
INCLUDE(${CMAKE_CURRENT_LIST_DIR}/hiredis-targets.cmake)
ENDIF()

SET(hiredis_LIBRARIES hiredis::hiredis)
SET(hiredis_LIBRARIES hiredis::@hiredis_export_name@)
SET(hiredis_INCLUDE_DIRS ${hiredis_INCLUDEDIR})

check_required_components(hiredis)
Expand Down
3 changes: 3 additions & 0 deletions hiredis_ssl-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

set_and_check(hiredis_ssl_INCLUDEDIR "@PACKAGE_INCLUDE_INSTALL_DIR@")

include(CMakeFindDependencyMacro)
find_dependency(OpenSSL)

IF (NOT TARGET hiredis::hiredis_ssl)
INCLUDE(${CMAKE_CURRENT_LIST_DIR}/hiredis_ssl-targets.cmake)
ENDIF()
Expand Down

0 comments on commit e9243d4

Please sign in to comment.