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