SUMMARY="A RPC library and framework"
DESCRIPTION="gRPC is a modern, open source, high-performance remote procedure \
call (RPC) framework that can run anywhere. gRPC enables client and server \
applications to communicate transparently, and simplifies the building of \
connected systems."
HOMEPAGE="https://grpc.io/"
COPYRIGHT="2015-2022 The gRPC Authors"
LICENSE="Apache v2"
REVISION="1"
SOURCE_URI="https://github.com/grpc/grpc/archive/refs/tags/v$portVersion.tar.gz"
CHECKSUM_SHA256="b74ce7d26fe187970d1d8e2c06a5d3391122f7bc1fdce569aff5e435fb8fe780"
SOURCE_FILENAME="grpc-$portVersion.tar.gz"
gtestCommit="c9ccac7cb7345901884aabf5d1a786cfa6e2f397"
SOURCE_URI_2="https://github.com/google/googletest/archive/$gtestCommit/googletest-$gtestCommit.tar.gz"
CHECKSUM_SHA256_2="443d383db648ebb8e391382c0ab63263b7091d03197f304390baac10f178a468"
SOURCE_DIR_2="googletest-$gtestCommit"
PATCHES="grpc-$portVersion.patchset"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

libVersion="21.0.0"
libVersionCompat="$libVersion compat >= ${libVersion%%.*}"
libCppVersion="1.43.2"
libCppVersionCompat="$libCppVersion compat >= ${libCppVersion%%.*}"

PROVIDES="
	grpc$secondaryArchSuffix = $portVersion
	cmd:grpc_cpp_plugin
	lib:libaddress_sorting$secondaryArchSuffix = $libVersionCompat
	lib:libgpr$secondaryArchSuffix = $libVersionCompat
	lib:libgrpc$secondaryArchSuffix = $libVersionCompat
	lib:libgrpc_plugin_support$secondaryArchSuffix = $libCppVersionCompat
	lib:libgrpc_unsecure$secondaryArchSuffix = $libVersionCompat
	lib:libgrpc++$secondaryArchSuffix = $libCppVersionCompat
	lib:libgrpc++_alts$secondaryArchSuffix = $libCppVersionCompat
	lib:libgrpc++_error_details$secondaryArchSuffix = $libCppVersionCompat
	lib:libgrpc++_reflection$secondaryArchSuffix = $libCppVersionCompat
	lib:libgrpc++_unsecure$secondaryArchSuffix = $libCppVersionCompat
	lib:libgrpcpp_channelz$secondaryArchSuffix = $libCppVersionCompat
	lib:libupb$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libabsl_cord$secondaryArchSuffix
	lib:libabsl_status$secondaryArchSuffix
	lib:libabsl_strings$secondaryArchSuffix
	lib:libabsl_synchronization$secondaryArchSuffix
	lib:libabsl_time$secondaryArchSuffix
	lib:libcares$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libprotobuf$secondaryArchSuffix
	lib:libprotoc$secondaryArchSuffix
	lib:libre2$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	grpc${secondaryArchSuffix}_devel = $portVersion
	devel:libaddress_sorting$secondaryArchSuffix = $libVersionCompat
	devel:libgpr$secondaryArchSuffix = $libVersionCompat
	devel:libgrpc$secondaryArchSuffix = $libVersionCompat
	devel:libgrpc_unsecure$secondaryArchSuffix = $libVersionCompat
	devel:libgrpc++$secondaryArchSuffix = $libCppVersionCompat
	devel:libgrpc++_alts$secondaryArchSuffix = $libCppVersionCompat
	devel:libgrpc++_error_details$secondaryArchSuffix = $libCppVersionCompat
	devel:libgrpc++_reflection$secondaryArchSuffix = $libCppVersionCompat
	devel:libgrpc++_unsecure$secondaryArchSuffix = $libCppVersionCompat
	devel:libgrpcpp_channelz$secondaryArchSuffix = $libCppVersionCompat
	devel:libupb$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	grpc$secondaryArchSuffix == $portVersion base
	devel:libabsl_cord$secondaryArchSuffix
	devel:libabsl_status$secondaryArchSuffix
	devel:libabsl_strings$secondaryArchSuffix
	devel:libabsl_synchronization$secondaryArchSuffix
	devel:libabsl_time$secondaryArchSuffix
	devel:libcares$secondaryArchSuffix
	devel:libprotobuf$secondaryArchSuffix
	devel:libre2$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libabsl_cord$secondaryArchSuffix
	devel:libabsl_status$secondaryArchSuffix
	devel:libabsl_strings$secondaryArchSuffix
	devel:libabsl_synchronization$secondaryArchSuffix
	devel:libabsl_time$secondaryArchSuffix
	devel:libbenchmark$secondaryArchSuffix
	devel:libcares$secondaryArchSuffix
	devel:libprotobuf$secondaryArchSuffix >= 28
	devel:libre2$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:ninja
	cmd:pkg_config$secondaryArchSuffix
	"
TEST_REQUIRES="
	cmd:python2
	cmd:python3
	six_python3
	"

BUILD()
{
	ln -sf $sourceDir2/googlemock $sourceDir2/googletest \
		third_party/googletest
	cmake -Bbuild -S . \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=$prefix \
		-DCMAKE_CXX_STANDARD=17 \
		-DCMAKE_POSITION_INDEPENDENT_CODE=NO \
		-DBUILD_SHARED_LIBS=ON \
		-DgRPC_INSTALL_BINDIR=$relativeBinDir \
		-DgRPC_INSTALL_INCLUDEDIR=$relativeIncludeDir \
		-DgRPC_INSTALL_LIBDIR=$relativeLibDir \
		-DgRPC_INSTALL_CMAKEDIR=$relativeLibDir/cmake/grpc \
		-DgRPC_INSTALL_SHAREDIR=$relativeDataDir/grpc \
		-DgRPC_ABSL_PROVIDER='package' \
		-DgRPC_BENCHMARK_PROVIDER='package' \
		-DgRPC_CARES_PROVIDER='package' \
		-DgRPC_ZLIB_PROVIDER='package' \
		-DgRPC_PROTOBUF_PROVIDER='package' \
		-DgRPC_PROTOBUF_PACKAGE_TYPE='MODULE' \
		-DgRPC_RE2_PROVIDER='package' \
		-DgRPC_SSL_PROVIDER='package' \
		-DgRPC_ZLIB_PROVIDER='package' \
		-DgRPC_USE_PROTO_LITE=OFF \
		-DgRPC_BUILD_GRPC_CPP_PLUGIN=ON \
		-DgRPC_BUILD_GRPC_CSHARP_PLUGIN=OFF \
		-DgRPC_BUILD_GRPC_NODE_PLUGIN=OFF \
		-DgRPC_BUILD_GRPC_OBJECTIVE_C_PLUGIN=OFF \
		-DgRPC_BUILD_GRPC_PHP_PLUGIN=OFF \
		-DgRPC_BUILD_GRPC_PYTHON_PLUGIN=OFF \
		-DgRPC_BUILD_GRPC_RUBY_PLUGIN=OFF \
		-DgRPC_BUILD_TESTS=ON \
		-GNinja
	cmake --build build -v $jobArgs
}

INSTALL()
{
	unset -f cmake
	cmake --install build

	prepareInstalledDevelLibs libaddress_sorting libgpr libgrpc \
		libgrpc_unsecure libgrpc++ libgrpc++_alts libgrpc++_error_details \
		libgrpc++_reflection libgrpc++_unsecure libgrpcpp_channelz libupb
	fixPkgconfig

	packageEntries devel \
		"$developDir" \
		"$libDir"/cmake
}

TEST()
{
	python3 tools/run_tests/run_tests.py -l c++
}
