]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add support for downloading hiredis from Internet
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 24 Jul 2021 20:00:01 +0000 (22:00 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 25 Jul 2021 05:25:30 +0000 (07:25 +0200)
.github/workflows/build.yaml
CMakeLists.txt
cmake/Findhiredis.cmake
doc/INSTALL.md
src/storage/secondary/RedisStorage.cpp

index db8843ac101976ec9f2b34aaad7165764d57c38c..056f5212c798dd70000bda5dc0159315db5b8a74 100644 (file)
@@ -165,7 +165,7 @@ jobs:
             CFLAGS: -m32 -g -O2
             CXXFLAGS: -m32 -g -O2
             LDFLAGS: -m32
-            CMAKE_PARAMS: -DCMAKE_BUILD_TYPE=CI -DZSTD_FROM_INTERNET=ON -DREDIS_STORAGE_BACKEND=OFF
+            CMAKE_PARAMS: -DCMAKE_BUILD_TYPE=CI -DZSTD_FROM_INTERNET=ON -DHIREDIS_FROM_INTERNET=ON
             ENABLE_CACHE_CLEANUP_TESTS: 1
             apt_get: elfutils gcc-multilib g++-multilib lib32stdc++-5-dev
 
@@ -173,7 +173,7 @@ jobs:
             os: ubuntu-18.04
             CC: gcc
             CXX: g++
-            CMAKE_PARAMS: -DCMAKE_BUILD_TYPE=CI -DZSTD_FROM_INTERNET=ON -DREDIS_STORAGE_BACKEND=OFF
+            CMAKE_PARAMS: -DCMAKE_BUILD_TYPE=CI -DZSTD_FROM_INTERNET=ON -DHIREDIS_FROM_INTERNET=ON
             ENABLE_CACHE_CLEANUP_TESTS: 1
             CUDA: 10.1.243-1
             apt_get: elfutils libzstd-dev
index f06f0468b8310b3a7e260a5bc854f02e3103b633..0673a4a3ec17c800096830150f3611af659b903f 100644 (file)
@@ -87,10 +87,12 @@ include(GenerateVersionFile)
 #
 # Third party
 #
+set(HIREDIS_FROM_INTERNET_DEFAULT OFF)
 set(ZSTD_FROM_INTERNET_DEFAULT OFF)
 
 # Default to downloading deps for Visual Studio, unless using a package manager.
 if(MSVC AND NOT CMAKE_TOOLCHAIN_FILE MATCHES "vcpkg|conan")
+  set(HIREDIS_FROM_INTERNET_DEFAULT ON)
   set(ZSTD_FROM_INTERNET_DEFAULT ON)
 endif()
 
@@ -99,6 +101,7 @@ find_package(zstd 1.1.2 REQUIRED)
 
 option(REDIS_STORAGE_BACKEND "Enable Redis secondary storage" ON)
 if(REDIS_STORAGE_BACKEND)
+  option(HIREDIS_FROM_INTERNET "Download and use libhiredis from the Internet" ${HIREDIS_FROM_INTERNET_DEFAULT})
   find_package(hiredis 0.13.3 REQUIRED)
 endif()
 
index ec5ff2351ff2993054eb33dd7c0d964e54f03f17..21bec80e017ccd0216086d2fec55e53487e7e382 100644 (file)
@@ -1,27 +1,75 @@
-find_package(PkgConfig)
-if(PKG_CONFIG_FOUND)
-  pkg_check_modules(HIREDIS REQUIRED hiredis>=${hiredis_FIND_VERSION})
-  find_library(HIREDIS_LIBRARY ${HIREDIS_LIBRARIES} HINTS ${HIREDIS_LIBDIR})
-  find_path(HIREDIS_INCLUDE_DIR hiredis/hiredis.h HINTS ${HIREDIS_PREFIX}/include)
+if(HIREDIS_FROM_INTERNET)
+  set(hiredis_version "1.0.0")
+  set(hiredis_url https://github.com/redis/hiredis/archive/v${hiredis_version}.tar.gz)
+
+  set(hiredis_dir ${CMAKE_BINARY_DIR}/hiredis-${hiredis_version})
+  set(hiredis_build ${CMAKE_BINARY_DIR}/hiredis-build)
+
+  if(NOT EXISTS "${hiredis_dir}.tar.gz")
+    file(DOWNLOAD "${hiredis_url}" "${hiredis_dir}.tar.gz" STATUS download_status)
+    list(GET download_status 0 error_code)
+    if(error_code)
+      file(REMOVE "${hiredis_dir}.tar.gz")
+      list(GET download_status 1 error_message)
+      message(FATAL "Failed to download hiredis: ${error_message}")
+    endif()
+  endif()
+
+  execute_process(
+    COMMAND tar xf "${hiredis_dir}.tar.gz"
+    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
+    RESULT_VARIABLE tar_error)
+  if(NOT tar_error EQUAL 0)
+    message(FATAL "extracting ${hiredis_dir}.tar.gz failed")
+  endif()
+
+  set(
+    hiredis_sources
+    "${hiredis_dir}/alloc.c"
+    "${hiredis_dir}/async.c"
+    "${hiredis_dir}/dict.c"
+    "${hiredis_dir}/hiredis.c"
+    "${hiredis_dir}/net.c"
+    "${hiredis_dir}/read.c"
+    "${hiredis_dir}/sds.c"
+    "${hiredis_dir}/sockcompat.c"
+  )
+  add_library(libhiredis_static STATIC EXCLUDE_FROM_ALL ${hiredis_sources})
+  add_library(HIREDIS::HIREDIS ALIAS libhiredis_static)
+  make_directory("${hiredis_dir}/include")
+  make_directory("${hiredis_dir}/include/hiredis")
+  file(GLOB hiredis_headers "${hiredis_dir}/*.h")
+  file(COPY ${hiredis_headers} DESTINATION "${hiredis_dir}/include/hiredis")
+  set_target_properties(
+    libhiredis_static
+    PROPERTIES
+    INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${hiredis_dir}/include>")
 else()
-  find_library(HIREDIS_LIBRARY hiredis)
-  find_path(HIREDIS_INCLUDE_DIR hiredis/hiredis.h)
-endif()
+  find_package(PkgConfig)
+  if(PKG_CONFIG_FOUND)
+    pkg_check_modules(HIREDIS REQUIRED hiredis>=${hiredis_FIND_VERSION})
+    find_library(HIREDIS_LIBRARY ${HIREDIS_LIBRARIES} HINTS ${HIREDIS_LIBDIR})
+    find_path(HIREDIS_INCLUDE_DIR hiredis/hiredis.h HINTS ${HIREDIS_PREFIX}/include)
+  else()
+    find_library(HIREDIS_LIBRARY hiredis)
+    find_path(HIREDIS_INCLUDE_DIR hiredis/hiredis.h)
+  endif()
 
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(
-  hiredis
-  "please install libhiredis or disable with -DREDIS_STORAGE_BACKEND=OFF"
-  HIREDIS_INCLUDE_DIR HIREDIS_LIBRARY)
-mark_as_advanced(HIREDIS_INCLUDE_DIR HIREDIS_LIBRARY)
+  include(FindPackageHandleStandardArgs)
+  find_package_handle_standard_args(
+    hiredis
+    "please install libhiredis or use -DHIREDIS_FROM_INTERNET=ON or disable with -DREDIS_STORAGE_BACKEND=OFF"
+    HIREDIS_INCLUDE_DIR HIREDIS_LIBRARY)
+  mark_as_advanced(HIREDIS_INCLUDE_DIR HIREDIS_LIBRARY)
 
-add_library(HIREDIS::HIREDIS UNKNOWN IMPORTED)
-set_target_properties(
-  HIREDIS::HIREDIS
-  PROPERTIES
-  IMPORTED_LOCATION "${HIREDIS_LIBRARY}"
-  INTERFACE_COMPILE_OPTIONS "${HIREDIS_CFLAGS_OTHER}"
-  INTERFACE_INCLUDE_DIRECTORIES "${HIREDIS_INCLUDE_DIR}")
+  add_library(HIREDIS::HIREDIS UNKNOWN IMPORTED)
+  set_target_properties(
+    HIREDIS::HIREDIS
+    PROPERTIES
+    IMPORTED_LOCATION "${HIREDIS_LIBRARY}"
+    INTERFACE_COMPILE_OPTIONS "${HIREDIS_CFLAGS_OTHER}"
+    INTERFACE_INCLUDE_DIRECTORIES "${HIREDIS_INCLUDE_DIR}")
+endif()
 
 include(FeatureSummary)
 set_package_properties(
index 79f422a054feaf4ead28bfe65d7e96696244b4e7..4391ffaf9ab3124cb9514953b88e789b236bc243 100644 (file)
@@ -14,6 +14,7 @@ To build ccache you need:
 - [libzstd](http://www.zstd.net). If you don't have libzstd installed and
   can't or don't want to install it in a standard system location, there are
   two options:
+
     1. Install zstd in a custom path and set `CMAKE_PREFIX_PATH` to it, e.g.
        by passing `-DCMAKE_PREFIX_PATH=/some/custom/path` to `cmake`, or
     2. Pass `-DZSTD_FROM_INTERNET=ON` to `cmake` to make it download libzstd
@@ -24,6 +25,18 @@ To build ccache you need:
 
 Optional:
 
+- [hiredis](https://github.com/redis/hiredis) for the Redis storage backend. If
+  you don't have libhiredis installed and can't or don't want to install it in a
+  standard system location, there are two options:
+
+    1. Install hiredis in a custom path and set `CMAKE_PREFIX_PATH` to it, e.g.
+       by passing `-DCMAKE_PREFIX_PATH=/some/custom/path` to `cmake`, or
+    2. Pass `-DHIREDIS_FROM_INTERNET=ON` to `cmake` to make it download hiredis
+       from the Internet and unpack it in the local binary tree. Ccache will
+       then be linked statically to the locally built libhiredis.
+
+  To link libhiredis statically you can use
+  `-DHIREDIS_LIBRARY=/path/to/libhiredis.a`.
 - GNU Bourne Again SHell (bash) for tests.
 - [AsciiDoc](https://www.methods.co.nz/asciidoc/) to build the HTML
   documentation.
index 09943c8e7e82ec3aed5401b51546e81712303990..8740edcb28ab04c7956835730ff5ed2e5aa3cae9 100644 (file)
 #include <util/expected.hpp>
 #include <util/string.hpp>
 
+#ifdef __GNUC__
+#  pragma GCC diagnostic push
+// Ignore "ISO C++ forbids flexible array member ‘buf’" warning from -Wpedantic.
+#  pragma GCC diagnostic ignored "-Wpedantic"
+#endif
 #include <hiredis/hiredis.h>
+#ifdef __GNUC__
+#  pragma GCC diagnostic pop
+#endif
 
 #include <cstdarg>
 #include <memory>