From 14b1de37a847e87c78eabaf804ada0638117428e Mon Sep 17 00:00:00 2001
From: Jakub Jirutka <jakub@jirutka.cz>
Date: Wed, 24 Aug 2022 22:46:42 +0200
Subject: [PATCH] community/grpc: build ruby bindings and add ruby-grpc subpkg

ca-certificates is needed also for C libs.
---
 community/grpc/APKBUILD                       | 85 ++++++++++++++++++-
 .../grpc/makefile-use-system-abseil.patch     | 22 +++++
 community/grpc/ruby-dont-strip-library.patch  | 30 +++++++
 community/grpc/ruby-fix-protoc-path.patch     | 25 ++++++
 community/grpc/ruby-use-shared-libs.patch     | 70 +++++++++++++++
 community/grpc/ruby-use-system-certs.patch    | 15 ++++
 6 files changed, 244 insertions(+), 3 deletions(-)
 create mode 100644 community/grpc/makefile-use-system-abseil.patch
 create mode 100644 community/grpc/ruby-dont-strip-library.patch
 create mode 100644 community/grpc/ruby-fix-protoc-path.patch
 create mode 100644 community/grpc/ruby-use-shared-libs.patch
 create mode 100644 community/grpc/ruby-use-system-certs.patch

diff --git a/community/grpc/APKBUILD b/community/grpc/APKBUILD
index 635642f0cd61..3fba64004829 100644
--- a/community/grpc/APKBUILD
+++ b/community/grpc/APKBUILD
@@ -3,17 +3,21 @@
 # Maintainer: wener <wenermail@gmail.com>
 pkgname=grpc
 pkgver=1.48.0
-pkgrel=2
+pkgrel=3
 pkgdesc="The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)"
 url="https://grpc.io/"
 arch="all"
 license="Apache-2.0"
-depends="protobuf"
+depends="ca-certificates protobuf"
 _pythondepends="
 	cython
 	python3-dev
 	py3-setuptools
 	"
+_rubydepends="
+	$pkgname=$pkgver-r$pkgrel
+	ruby-google-protobuf>=3.19
+	"
 makedepends="
 	abseil-cpp-dev
 	autoconf
@@ -28,17 +32,29 @@ makedepends="
 	openssl-dev>3
 	protobuf-dev
 	re2-dev
+	ruby-dev
 	samurai
 	yaml-dev
 	zlib-dev
 	$_pythondepends
+	$_rubydepends
 	"
 checkdepends="coreutils python3 py3-six"
-subpackages="$pkgname-dev $pkgname-doc py3-grpcio:grpcio"
+subpackages="
+	$pkgname-dev
+	$pkgname-doc
+	py3-grpcio:grpcio
+	ruby-grpc:_ruby
+	"
 _googletest_rev=0e402173c97aea7a00749e825b194bfede4f2e45
 source="https://github.com/grpc/grpc/archive/v$pkgver/grpc-v$pkgver.tar.gz
 	googletest-$_googletest_rev.tar.gz::https://github.com/google/googletest/archive/$_googletest_rev.tar.gz
 	01-chttp2-maybe-uninitialized.patch
+	ruby-use-shared-libs.patch
+	ruby-dont-strip-library.patch
+	ruby-fix-protoc-path.patch
+	ruby-use-system-certs.patch
+	makefile-use-system-abseil.patch
 	"
 options="!check" # sometimes hang indefinitely on builders
 
@@ -47,6 +63,21 @@ prepare() {
 	mv "$srcdir"/googletest-$_googletest_rev third_party/googletest
 
 	default_prepare
+
+	# Remove some bundled dependencies from the gem's files list.
+	sed -i \
+		-e '/etc\/roots.pem/d' \
+		-e '/third_party\/abseil/d' \
+		-e '/third_party\/boringssl/d' \
+		-e '/third_party\/cares/d' \
+		-e '/third_party\/re2/d' \
+		-e '/third_party\/zlib/d' \
+		grpc.gemspec
+
+	# Remove unused dependency from gemspec - it's not required anyhwere,
+	# it's just Google pushing their crap everywhere...
+	sed -i '/add_dependency.*googleapis-common-protos-types/d' \
+		grpc.gemspec
 }
 
 build() {
@@ -81,6 +112,16 @@ build() {
 	# grpcio-tools
 	cd tools/distrib/python
 	python3 make_grpcio_tools.py
+
+	cd "$builddir"
+	gem build grpc.gemspec
+	TOPDIR="$PWD/_build" gem install \
+		--local \
+		--install-dir _build/ruby \
+		--ignore-dependencies \
+		--no-document \
+		--verbose \
+		grpc-$pkgver.gem
 }
 
 check() {
@@ -130,8 +171,46 @@ grpcio() {
 	python3 setup.py install --skip-build --prefix=/usr --root="$subpkgdir"
 }
 
+_ruby() {
+	pkgdesc="Send RPCs from Ruby using GRPC"
+	depends="$_rubydepends"
+
+	local gemdir="$subpkgdir/$(ruby -e 'puts Gem.default_dir')"
+
+	cd "$builddir"/_build/ruby
+
+	mkdir -p "$gemdir"
+	cp -r extensions gems specifications "$gemdir"/
+
+	# Remove unnecessary files and rubbish...
+	cd "$gemdir"/extensions/*/*/grpc-$pkgver
+	rm gem_make.out mkmf.log || true
+
+	cd "$gemdir"/gems/grpc-$pkgver
+	rm -rf .yardopts \
+		Makefile \
+		include/ \
+		src/core/ \
+		third_party/
+
+	cd src/ruby
+	rm -rf bin/ \
+		ext/ \
+		lib/grpc/*.so \
+		pb/generate_proto_ruby.sh \
+		pb/README.md \
+		pb/src/ \
+		pb/test/ \
+		spec/
+}
+
 sha512sums="
 558c659b325eb2f64f6caf78c0701eaaf3d9ae35f6d25ccd69b8995d5b82b98ca1a7ef9f497a0a1dab5914d2328c044c108373152426a15045d0c978b27f3503  grpc-v1.48.0.tar.gz
 5c5eaf6ff9f3c1bca025b7ef0234ba97232ba85b43e6354a92f49b7208f5c47581ebaf18bf58618498e5d264f2620c2b6676e81bb0f7df77112b96ba271ececf  googletest-0e402173c97aea7a00749e825b194bfede4f2e45.tar.gz
 7fa146ce86ddd4f160bb1ca9ff01cb7aca6b2b8c9aa50e4fa6b84504b9117b104be0d1e31ccb452d846549dfe1e9012ceccfcdc1f2357ed567621d71fb8b08c5  01-chttp2-maybe-uninitialized.patch
+9c7709dadb969d862df4c4300876ee0f7b60403b3fc2653b06e85b4a534c1727f37edfdcabbf3995ebf02873bc669ff4601f8c3343f2c9de44fc899f23f5d092  ruby-use-shared-libs.patch
+b32b23578fa868a389a2db693e2de1a24ee33a7aaeeeb8f410c0d87b25727cd94c5af41f5e668dfcad6c516667989900999230a3e9b16abe2f8de75c44aa67a1  ruby-dont-strip-library.patch
+4ea72d2acd8bee9c9022a4412aa0af0477faca7b0810d14decb3ad5d4da044247f51189512323bfee855b9b260a7f82b812310391451e5d8ee718297800d7a73  ruby-fix-protoc-path.patch
+631af4b9ac29c1ebabb2c88394ea2993e36cec1beda38195e1587dbd9d3c8c9eef75a17d2326d3cd2e682de551401216075ba08fdc501c098b8092d718ded381  ruby-use-system-certs.patch
+89e260934da83eb45fa6b73884cba1b1c30f99c0eb883a726e2d36ee4788246f4c6fa1b201077038af956bcb58e625f83bedba4f186c711785ec240373ce4fc5  makefile-use-system-abseil.patch
 "
diff --git a/community/grpc/makefile-use-system-abseil.patch b/community/grpc/makefile-use-system-abseil.patch
new file mode 100644
index 000000000000..ac4f9fc0510e
--- /dev/null
+++ b/community/grpc/makefile-use-system-abseil.patch
@@ -0,0 +1,22 @@
+--- a/Makefile
++++ b/Makefile
+@@ -575,8 +575,8 @@
+ 
+ # Setup abseil dependency
+ 
+-GRPC_ABSEIL_DEP = $(LIBDIR)/$(CONFIG)/libgrpc_abseil.a
+-GRPC_ABSEIL_MERGE_LIBS = $(LIBDIR)/$(CONFIG)/libgrpc_abseil.a
++GRPC_ABSEIL_DEP = -labsl_base -labsl_int128 -labsl_strings -labsl_time -labsl_bad_optional_access -labsl_throw_delegate -labsl_str_format_internal
++GRPC_ABSEIL_MERGE_LIBS = -labsl_base -labsl_int128 -labsl_strings -labsl_time -labsl_bad_optional_access -labsl_throw_delegate -labsl_str_format_internal
+ 
+ # Setup re2 dependency
+ 
+@@ -2809,7 +2809,7 @@
+     third_party/abseil-cpp/absl/types/bad_variant_access.cc \
+ 
+ 
+-LIBGRPC_ABSEIL_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(LIBGRPC_ABSEIL_SRC))))
++LIBGRPC_ABSEIL_OBJS =
+ 
+ $(LIBGRPC_ABSEIL_OBJS): CPPFLAGS += -g -Ithird_party/abseil-cpp
+ 
diff --git a/community/grpc/ruby-dont-strip-library.patch b/community/grpc/ruby-dont-strip-library.patch
new file mode 100644
index 000000000000..ef219fcd89ea
--- /dev/null
+++ b/community/grpc/ruby-dont-strip-library.patch
@@ -0,0 +1,30 @@
+From: Jakub Jirutka <jakub@jirutka.cz>
+Date: Wed, 24 Aug 2022 21:20:22 +0200
+Subject: [PATCH] Don't strip the built library
+
+abuild does it.
+
+--- a/src/ruby/ext/grpc/extconf.rb
++++ b/src/ruby/ext/grpc/extconf.rb
+@@ -141,21 +121,3 @@
+ output = File.join('grpc', 'grpc_c')
+ puts 'Generating Makefile for ' + output
+ create_makefile(output)
+-
+-strip_tool = RbConfig::CONFIG['STRIP']
+-strip_tool += ' -x' if apple_toolchain
+-
+-if grpc_config == 'opt'
+-  File.open('Makefile.new', 'w') do |o|
+-    o.puts 'hijack: all strip'
+-    o.puts
+-    File.foreach('Makefile') do |i|
+-      o.puts i
+-    end
+-    o.puts
+-    o.puts 'strip: $(DLLIB)'
+-    o.puts "\t$(ECHO) Stripping $(DLLIB)"
+-    o.puts "\t$(Q) #{strip_tool} $(DLLIB)"
+-  end
+-  File.rename('Makefile.new', 'Makefile')
+-end
diff --git a/community/grpc/ruby-fix-protoc-path.patch b/community/grpc/ruby-fix-protoc-path.patch
new file mode 100644
index 000000000000..91f0268624b2
--- /dev/null
+++ b/community/grpc/ruby-fix-protoc-path.patch
@@ -0,0 +1,25 @@
+Patch-Source: https://sources.debian.org/src/grpc/1.44.0-3/debian/patches/fix-protoc-path.patch (modified)
+
+--- a/src/ruby/end2end/package_with_underscore_test.rb
++++ b/src/ruby/end2end/package_with_underscore_test.rb
+@@ -20,8 +20,8 @@ def main
+   pb_dir = File.join(root_dir, 'src', 'ruby', 'end2end', 'protos')
+ 
+-  bins_dir = File.join(root_dir, 'cmake', 'build')
++  bins_dir = '/usr/bin'
+   plugin = File.join(bins_dir, 'grpc_ruby_plugin')
+-  protoc = File.join(bins_dir, 'third_party', 'protobuf', 'protoc')
++  protoc = File.join(bins_dir, 'protoc')
+ 
+   got = nil
+ 
+--- a/src/ruby/tools/bin/grpc_tools_ruby_protoc
++++ b/src/ruby/tools/bin/grpc_tools_ruby_protoc
+@@ -25,6 +25,5 @@ plugin_name = 'grpc_ruby_plugin' + ext
+ 
+-protoc_dir = File.join(File.dirname(__FILE__),
+-                       PLATFORM.architecture + '-' + PLATFORM.os_name)
++protoc_dir = '/usr/bin'
+ 
+ protoc_path = File.join(protoc_dir, protoc_name)
+ 
diff --git a/community/grpc/ruby-use-shared-libs.patch b/community/grpc/ruby-use-shared-libs.patch
new file mode 100644
index 000000000000..156531037382
--- /dev/null
+++ b/community/grpc/ruby-use-shared-libs.patch
@@ -0,0 +1,70 @@
+From: Jakub Jirutka <jakub@jirutka.cz>
+Date: Wed, 24 Aug 2022 21:20:22 +0200
+Subject: [PATCH] Link with shared libraries, don't embed anything
+
+- Don't statically link openssl, zlib and cares.
+- Don't build and statically link libgrpc, link shared libgrpc.
+- Don't statically link libgcc and libstdc++.
+
+--- a/src/ruby/ext/grpc/extconf.rb
++++ b/src/ruby/ext/grpc/extconf.rb
+@@ -68,11 +68,11 @@
+ end
+ 
+ # Don't embed on TruffleRuby (constant-time crypto is unsafe with Sulong, slow build times)
+-ENV['EMBED_OPENSSL'] = (RUBY_ENGINE != 'truffleruby').to_s
++ENV['EMBED_OPENSSL'] = 'false'
+ # Don't embed on TruffleRuby (the system zlib is already linked for the zlib C extension, slow build times)
+-ENV['EMBED_ZLIB'] = (RUBY_ENGINE != 'truffleruby').to_s
++ENV['EMBED_ZLIB'] = 'false'
+ 
+-ENV['EMBED_CARES'] = 'true'
++ENV['EMBED_CARES'] = 'false'
+ 
+ ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG']
+ if apple_toolchain && !cross_compiling
+@@ -92,23 +92,8 @@
+ output_dir = File.expand_path(RbConfig::CONFIG['topdir'])
+ grpc_lib_dir = File.join(output_dir, 'libs', grpc_config)
+ ENV['BUILDDIR'] = output_dir
++$LDFLAGS << ' -L' + ENV.fetch('TOPDIR', '.')
+ 
+-unless windows
+-  puts 'Building internal gRPC into ' + grpc_lib_dir
+-  nproc = 4
+-  nproc = Etc.nprocessors if Etc.respond_to? :nprocessors
+-  nproc_override = ENV['GRPC_RUBY_BUILD_PROCS']
+-  unless nproc_override.nil? or nproc_override.size == 0
+-    nproc = nproc_override
+-    puts "Overriding make parallelism to #{nproc}"
+-  end
+-  make = bsd ? 'gmake' : 'make'
+-  cmd = "#{make} -j#{nproc} -C #{grpc_root} #{grpc_lib_dir}/libgrpc.a CONFIG=#{grpc_config} Q="
+-  puts "Building grpc native library: #{cmd}"
+-  system(cmd)
+-  exit 1 unless $? == 0
+-end
+-
+ $CFLAGS << ' -I' + File.join(grpc_root, 'include')
+ 
+ ext_export_file = File.join(grpc_root, 'src', 'ruby', 'ext', 'grpc', 'ext-export')
+@@ -116,7 +101,7 @@
+ $LDFLAGS << ' -Wl,--version-script="' + ext_export_file + '.gcc"' if linux
+ $LDFLAGS << ' -Wl,-exported_symbols_list,"' + ext_export_file + '.clang"' if apple_toolchain
+ 
+-$LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') unless windows
++$LDFLAGS << ' -Wl,-wrap,memcpy -lgrpc' unless windows
+ if grpc_config == 'gcov'
+   $CFLAGS << ' -O0 -fprofile-arcs -ftest-coverage'
+   $LDFLAGS << ' -fprofile-arcs -ftest-coverage -rdynamic'
+@@ -127,10 +112,6 @@
+ end
+ 
+ $LDFLAGS << ' -Wl,-wrap,memcpy' if linux
+-# Do not statically link standard libraries on TruffleRuby as this does not work when compiling to bitcode
+-if linux && RUBY_ENGINE != 'truffleruby'
+-  $LDFLAGS << ' -static-libgcc -static-libstdc++'
+-end
+ $LDFLAGS << ' -static' if windows
+ 
+ $CFLAGS << ' -std=c11 '
diff --git a/community/grpc/ruby-use-system-certs.patch b/community/grpc/ruby-use-system-certs.patch
new file mode 100644
index 000000000000..5d3ba16bc8c7
--- /dev/null
+++ b/community/grpc/ruby-use-system-certs.patch
@@ -0,0 +1,15 @@
+From: Jakub Jirutka <jakub@jirutka.cz>
+Date: Fri, 20 May 2017 01:35:00 +0200
+Subject: [PATCH] Use system CA certificates
+
+--- a/src/ruby/lib/grpc.rb
++++ b/src/ruby/lib/grpc.rb
+@@ -12,7 +12,7 @@
+ # See the License for the specific language governing permissions and
+ # limitations under the License.
+ 
+-ssl_roots_path = File.expand_path('../../../../etc/roots.pem', __FILE__)
++ssl_roots_path = '/etc/ssl/certs/ca-certificates.crt'
+ 
+ require_relative 'grpc/errors'
+ require_relative 'grpc/structs'
-- 
GitLab