]> git.ipfire.org Git - thirdparty/ccache.git/blob - cmake/Findhiredis.cmake
chore: Remove redundant copy of DEF_WRAP_1_R
[thirdparty/ccache.git] / cmake / Findhiredis.cmake
1 if(hiredis_FOUND)
2 return()
3 endif()
4
5 set(hiredis_FOUND FALSE)
6
7 if(HIREDIS_FROM_INTERNET AND NOT HIREDIS_FROM_INTERNET STREQUAL "AUTO")
8 message(STATUS "Using hiredis from the Internet")
9 set(do_download TRUE)
10 else()
11 find_package(PkgConfig)
12 if(PKG_CONFIG_FOUND)
13 pkg_check_modules(HIREDIS hiredis>=${hiredis_FIND_VERSION})
14 find_library(HIREDIS_LIBRARY ${HIREDIS_LIBRARIES} HINTS ${HIREDIS_LIBDIR})
15 find_path(HIREDIS_INCLUDE_DIR hiredis/hiredis.h HINTS ${HIREDIS_PREFIX}/include)
16 if(HIREDIS_LIBRARY AND HIREDIS_INCLUDE_DIR)
17 message(STATUS "Using hiredis from ${HIREDIS_LIBRARY} via pkg-config")
18 set(hiredis_FOUND TRUE)
19 endif()
20 endif()
21
22 if(NOT hiredis_FOUND)
23 find_library(HIREDIS_LIBRARY hiredis)
24 find_path(HIREDIS_INCLUDE_DIR hiredis/hiredis.h)
25 if(HIREDIS_LIBRARY AND HIREDIS_INCLUDE_DIR)
26 message(STATUS "Using hiredis from ${HIREDIS_LIBRARY}")
27 set(hiredis_FOUND TRUE)
28 endif()
29 endif()
30
31 if(hiredis_FOUND)
32 mark_as_advanced(HIREDIS_INCLUDE_DIR HIREDIS_LIBRARY)
33 add_library(HIREDIS::HIREDIS UNKNOWN IMPORTED)
34 set_target_properties(
35 HIREDIS::HIREDIS
36 PROPERTIES
37 IMPORTED_LOCATION "${HIREDIS_LIBRARY}"
38 INTERFACE_COMPILE_OPTIONS "${HIREDIS_CFLAGS_OTHER}"
39 INTERFACE_INCLUDE_DIRECTORIES "${HIREDIS_INCLUDE_DIR}"
40 )
41 if(WIN32 AND STATIC_LINK)
42 target_link_libraries(HIREDIS::HIREDIS INTERFACE ws2_32)
43 endif()
44 set(hiredis_FOUND TRUE)
45 set(target HIREDIS::HIREDIS)
46 elseif(HIREDIS_FROM_INTERNET STREQUAL "AUTO")
47 message(STATUS "*** WARNING ***: Using hiredis from the Internet because it was not found and HIREDIS_FROM_INTERNET is AUTO")
48 set(do_download TRUE)
49 else()
50 include(FindPackageHandleStandardArgs)
51 find_package_handle_standard_args(
52 hiredis
53 REQUIRED_VARS HIREDIS_LIBRARY HIREDIS_INCLUDE_DIR
54 )
55 endif()
56 endif()
57
58 if(do_download)
59 set(hiredis_version "1.2.0")
60 set(hiredis_dir ${CMAKE_BINARY_DIR}/hiredis-${hiredis_version})
61 set(hiredis_build ${CMAKE_BINARY_DIR}/hiredis-build)
62
63 include(FetchContent)
64 FetchContent_Declare(
65 hiredis
66 URL https://github.com/redis/hiredis/archive/refs/tags/v${hiredis_version}.tar.gz
67 SOURCE_DIR ${hiredis_dir}
68 BINARY_DIR ${hiredis_build}
69 )
70
71 FetchContent_GetProperties(hiredis)
72 if(NOT hiredis_POPULATED)
73 FetchContent_Populate(hiredis)
74 endif()
75
76 set(
77 hiredis_sources
78 "${hiredis_dir}/alloc.c"
79 "${hiredis_dir}/async.c"
80 "${hiredis_dir}/dict.c"
81 "${hiredis_dir}/hiredis.c"
82 "${hiredis_dir}/net.c"
83 "${hiredis_dir}/read.c"
84 "${hiredis_dir}/sds.c"
85 "${hiredis_dir}/sockcompat.c"
86 )
87 add_library(libhiredis_static STATIC EXCLUDE_FROM_ALL ${hiredis_sources})
88 add_library(HIREDIS::HIREDIS ALIAS libhiredis_static)
89
90 if(WIN32)
91 target_compile_definitions(libhiredis_static PRIVATE _CRT_SECURE_NO_WARNINGS)
92 endif()
93
94 make_directory("${hiredis_dir}/include")
95 make_directory("${hiredis_dir}/include/hiredis")
96 file(GLOB hiredis_headers "${hiredis_dir}/*.h")
97 file(COPY ${hiredis_headers} DESTINATION "${hiredis_dir}/include/hiredis")
98 set_target_properties(
99 libhiredis_static
100 PROPERTIES
101 INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${hiredis_dir}/include>")
102
103 set(hiredis_FOUND TRUE)
104 set(target libhiredis_static)
105 endif()
106
107 if(WIN32 AND hiredis_FOUND)
108 target_link_libraries(${target} INTERFACE ws2_32)
109 endif()
110
111 include(FeatureSummary)
112 set_package_properties(
113 hiredis
114 PROPERTIES
115 URL "https://github.com/redis/hiredis"
116 DESCRIPTION "Hiredis is a minimalistic C client library for the Redis database"
117 )