Subject: [bug#62196] [PATCH 037/223] gnu: Add ruby-hiredis.

From: Maxim Cournoyer <[email-masked]>
Date: Mon, 20 Mar 2023 13:13:29 -0400

* gnu/packages/databases.scm (ruby-hiredis): New variable.
* gnu/packages/patches/ruby-hiredis-use-system-hiredis.patch: New file.
* gnu/local.mk (dist_patch_DATA): Register it.
---
 gnu/local.mk                                  |  1 +
 gnu/packages/databases.scm                    | 62 ++++++++++++++++++-
 .../ruby-hiredis-use-system-hiredis.patch     | 52 ++++++++++++++++
 3 files changed, 114 insertions(+), 1 deletion(-)
 create mode 100644 gnu/packages/patches/ruby-hiredis-use-system-hiredis.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 2abece0d2f..8109312e77 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1819,6 +1819,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/rocm-opencl-runtime-4.3-noclinfo.patch 	\
   %D%/packages/patches/rocm-opencl-runtime-4.3-noopencl.patch 	\
   %D%/packages/patches/rottlog-direntry.patch 			\
+  %D%/packages/patches/ruby-hiredis-use-system-hiredis.patch	\
   %D%/packages/patches/ruby-hydra-minimal-no-byebug.patch	\
   %D%/packages/patches/ruby-anystyle-data-immutable-install.patch	\
   %D%/packages/patches/ruby-anystyle-fix-dictionary-populate.patch	\
diff --git a/gnu/packages/databases.scm b/gnu/packages/databases.scm
index e51ddd5bf2..a4560d40e3 100644
--- a/gnu/packages/databases.scm
+++ b/gnu/packages/databases.scm
@@ -32,7 +32,7 @@
 ;;; Copyright © 2017 Kristofer Buffington <[email-masked]>
 ;;; Copyright © 2018 Amirouche Boubekki <[email-masked]>
 ;;; Copyright © 2018 Joshua Sierles, Nextjournal <[email-masked]>
-;;; Copyright © 2018, 2021, 2022 Maxim Cournoyer <[email-masked]>
+;;; Copyright © 2018, 2021, 2022, 2023 Maxim Cournoyer <[email-masked]>
 ;;; Copyright © 2019 Jack Hill <[email-masked]>
 ;;; Copyright © 2019 Alex Griffin <[email-masked]>
 ;;; Copyright © 2019 Gábor Boskovits <[email-masked]>
@@ -2520,6 +2520,66 @@ (define-public hiredis
     (home-page "https://github.com/redis/hiredis")
     (license license:bsd-3)))
 
+(define-public ruby-hiredis
+  (package
+    (name "ruby-hiredis")
+    (version "0.6.3")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/redis/hiredis-rb")
+                    (commit (string-append "v" version))))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "05y4g7frhym59m9x208zpvg2qvqvfjlgqmygxj8sqgl07n0ww1ks"))
+              (patches (search-patches
+                        "ruby-hiredis-use-system-hiredis.patch"))))
+    (build-system ruby-build-system)
+    (arguments
+     (list
+      #:tests? #f                       ;require native extension
+      #:phases
+      #~(modify-phases %standard-phases
+          (add-after 'unpack 'patch-hiredis-include-directory
+            (lambda* (#:key inputs #:allow-other-keys)
+              (substitute* "ext/hiredis_ext/extconf.rb"
+                ;; Adjust the hiredis include directory.
+                (("\\$CFLAGS << \" -I/usr/include/hiredis\"")
+                 (format #f "$CFLAGS << \" -I~a\""
+                         (search-input-directory inputs "include/hiredis"))))))
+          (add-after 'unpack 'disable-building-c-extension
+            (lambda _
+              ;; FIXME: The produced native extension appears to segfault when
+              ;; run; disable building it until a solution is found (see:
+              ;; https://github.com/redis/hiredis-rb/issues/93).
+              (substitute* "ext/hiredis_ext/extconf.rb"
+                (("build_hiredis = true")
+                 "build_hiredis = false"))))
+          ;; FIXME: Un-comment phase after the extension can be made to run
+          ;; without crashing (see above).
+          ;; (add-after 'build 'build-ext
+          ;;   (lambda _
+          ;;     (setenv "CC" #$(cc-for-target))
+          ;;     (invoke "rake" "compile")))
+          (add-before 'check 'start-redis
+            (lambda _
+              (invoke "redis-server" "--daemonize" "yes")))
+          (add-after 'install 'delete-mkmf.log
+            (lambda _
+              ;; This build log captures non-deterministic file names (see:
+              ;; https://github.com/rubygems/rubygems/issues/6259).
+              (for-each delete-file (find-files #$output "^mkmf\\.log$")))))))
+    (native-inputs (list redis ruby-rake-compiler))
+    (inputs (list hiredis))
+    (synopsis "Ruby wrapper for hiredis")
+    (description "@code{hiredis-rb} is a Ruby extension that wraps
+@code{hiredis}, a minimalist C client for Redis.  Both the synchronous
+connection API and a separate protocol reader are supported.  It is primarily
+intended to speed up parsing multi bulk replies.")
+    (home-page "https://github.com/redis/hiredis-rb")
+    (license license:bsd-3)))
+
 (define-public ruby-redis
   (package
     (name "ruby-redis")
diff --git a/gnu/packages/patches/ruby-hiredis-use-system-hiredis.patch b/gnu/packages/patches/ruby-hiredis-use-system-hiredis.patch
new file mode 100644
index 0000000000..1b238f5099
--- /dev/null
+++ b/gnu/packages/patches/ruby-hiredis-use-system-hiredis.patch
@@ -0,0 +1,52 @@
+Retrieved 2023-03-05 from
+https://sources.debian.org/data/main/r/ruby-hiredis/0.6.3-2/debian/patches/use_system_libhiredis.patch.
+
+From: Apollon Oikonomopoulos <[email-masked]>
+Date: Mon, 20 Jan 2020 09:28:45 -0300
+Subject: Use system libhiredis
+
+Last-Update: 2013-04-10
+Forwarded: no
+
+Use Debian's libhiredis version instead of downloading one and statically
+linking against it.
+---
+ ext/hiredis_ext/extconf.rb | 18 +++---------------
+ 1 file changed, 3 insertions(+), 15 deletions(-)
+
+diff --git a/ext/hiredis_ext/extconf.rb b/ext/hiredis_ext/extconf.rb
+index da39eb5..ffa3abf 100644
+--- a/ext/hiredis_ext/extconf.rb
++++ b/ext/hiredis_ext/extconf.rb
+@@ -9,12 +9,6 @@ end
+ 
+ RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
+ 
+-hiredis_dir = File.join(File.dirname(__FILE__), %w{.. .. vendor hiredis})
+-unless File.directory?(hiredis_dir)
+-  STDERR.puts "vendor/hiredis missing, please checkout its submodule..."
+-  exit 1
+-end
+-
+ RbConfig::CONFIG['configure_args'] =~ /with-make-prog\=(\w+)/
+ make_program = $1 || ENV['make']
+ make_program ||= case RUBY_PLATFORM
+@@ -27,15 +21,9 @@ else
+ end
+ 
+ if build_hiredis
+-  # Make sure hiredis is built...
+-  Dir.chdir(hiredis_dir) do
+-    success = system("#{make_program} static")
+-    raise "Building hiredis failed" if !success
+-  end
+-
+-  # Statically link to hiredis (mkmf can't do this for us)
+-  $CFLAGS << " -I#{hiredis_dir}"
+-  $LDFLAGS << " #{hiredis_dir}/libhiredis.a"
++  # Debian: use system hiredis
++  $CFLAGS << " -I/usr/include/hiredis"
++  $LDFLAGS << " -lhiredis"
+ 
+   have_func("rb_thread_fd_select")
+   create_makefile('hiredis/ext/hiredis_ext')
-- 
2.39.1