Skip to content

Commit 74f3fb9

Browse files
committed
fix: use ABI compatible grpc and protobuf
1 parent 735985e commit 74f3fb9

File tree

3 files changed

+142
-13
lines changed

3 files changed

+142
-13
lines changed

nix/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
ninja,
88
pkg-config,
99
llvmPackages_19,
10-
grpc,
1110
callPackage,
1211
lib,
1312
}:
1413
let
15-
protobuf = callPackage ./protobuf_29_2.nix { };
14+
protobuf = callPackage ./protobuf_29_2.nix { inherit grpc; };
15+
grpc = callPackage ./grpc.nix { inherit protobuf; };
1616
in
1717
llvmPackages_19.stdenv.mkDerivation {
1818
pname = "WebSocketWithOpenCV";

nix/grpc.nix

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
lib,
3+
stdenv,
4+
fetchFromGitHub,
5+
fetchpatch,
6+
buildPackages,
7+
cmake,
8+
zlib,
9+
c-ares,
10+
pkg-config,
11+
re2,
12+
openssl,
13+
protobuf,
14+
grpc,
15+
abseil-cpp,
16+
libnsl,
17+
18+
# tests
19+
python3,
20+
arrow-cpp,
21+
}:
22+
23+
# This package should be updated together with all related python grpc packages
24+
# to ensure compatibility.
25+
# nixpkgs-update: no auto update
26+
stdenv.mkDerivation rec {
27+
pname = "grpc";
28+
version = "1.69.0"; # N.B: if you change this, please update:
29+
# pythonPackages.grpcio
30+
# pythonPackages.grpcio-channelz
31+
# pythonPackages.grpcio-health-checking
32+
# pythonPackages.grpcio-reflection
33+
# pythonPackages.grpcio-status
34+
# pythonPackages.grpcio-testing
35+
# pythonPackages.grpcio-tools
36+
37+
src = fetchFromGitHub {
38+
owner = "grpc";
39+
repo = "grpc";
40+
rev = "v${version}";
41+
hash = "sha256-rRiPUcr6of/sjqB+qr7eOkKVFGTnu8UjluULmcn8df4=";
42+
fetchSubmodules = true;
43+
};
44+
45+
patches = [
46+
(fetchpatch {
47+
# armv6l support, https://github.com/grpc/grpc/pull/21341
48+
name = "grpc-link-libatomic.patch";
49+
url = "https://github.com/lopsided98/grpc/commit/a9b917666234f5665c347123d699055d8c2537b2.patch";
50+
hash = "sha256-Lm0GQsz/UjBbXXEE14lT0dcRzVmCKycrlrdBJj+KLu8=";
51+
})
52+
# fix build of 1.63.0 and newer on darwin: https://github.com/grpc/grpc/issues/36654
53+
] ++ (lib.optional stdenv.hostPlatform.isDarwin ./dynamic-lookup-darwin.patch);
54+
55+
nativeBuildInputs = [
56+
cmake
57+
pkg-config
58+
] ++ lib.optional (stdenv.hostPlatform != stdenv.buildPlatform) grpc;
59+
propagatedBuildInputs = [
60+
c-ares
61+
re2
62+
zlib
63+
abseil-cpp
64+
];
65+
buildInputs = [
66+
openssl
67+
protobuf
68+
] ++ lib.optionals stdenv.hostPlatform.isLinux [ libnsl ];
69+
70+
cmakeFlags =
71+
[
72+
"-DgRPC_ZLIB_PROVIDER=package"
73+
"-DgRPC_CARES_PROVIDER=package"
74+
"-DgRPC_RE2_PROVIDER=package"
75+
"-DgRPC_SSL_PROVIDER=package"
76+
"-DgRPC_PROTOBUF_PROVIDER=package"
77+
"-DgRPC_ABSL_PROVIDER=package"
78+
"-DBUILD_SHARED_LIBS=ON"
79+
]
80+
++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
81+
"-D_gRPC_PROTOBUF_PROTOC_EXECUTABLE=${buildPackages.protobuf}/bin/protoc"
82+
"-D_gRPC_CPP_PLUGIN=${buildPackages.grpc}/bin/grpc_cpp_plugin"
83+
]
84+
# The build scaffold defaults to c++14 on darwin, even when the compiler uses
85+
# a more recent c++ version by default [1]. However, downgrades are
86+
# problematic, because the compatibility types in abseil will have different
87+
# interface definitions than the ones used for building abseil itself.
88+
# [1] https://github.com/grpc/grpc/blob/v1.57.0/CMakeLists.txt#L239-L243
89+
++ (
90+
let
91+
defaultCxxIsOlderThan17 =
92+
(stdenv.cc.isClang && lib.versionAtLeast stdenv.cc.cc.version "16.0")
93+
|| (stdenv.cc.isGNU && lib.versionAtLeast stdenv.cc.cc.version "11.0");
94+
in
95+
lib.optionals (stdenv.hostPlatform.isDarwin && defaultCxxIsOlderThan17) [
96+
"-DCMAKE_CXX_STANDARD=17"
97+
]
98+
);
99+
100+
# CMake creates a build directory by default, this conflicts with the
101+
# basel BUILD file on case-insensitive filesystems.
102+
preConfigure = ''
103+
rm -vf BUILD
104+
'';
105+
106+
# When natively compiling, grpc_cpp_plugin is executed from the build directory,
107+
# needing to load dynamic libraries from the build directory, so we set
108+
# LD_LIBRARY_PATH to enable this. When cross compiling we need to avoid this,
109+
# since it can cause the grpc_cpp_plugin executable from buildPackages to
110+
# crash if build and host architecture are compatible (e. g. pkgsLLVM).
111+
preBuild = lib.optionalString (stdenv.hostPlatform == stdenv.buildPlatform) ''
112+
export LD_LIBRARY_PATH=$(pwd)''${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH
113+
'';
114+
115+
env.NIX_CFLAGS_COMPILE = toString (
116+
[
117+
"-Wno-error"
118+
]
119+
++ lib.optionals stdenv.hostPlatform.isDarwin [
120+
# Workaround for https://github.com/llvm/llvm-project/issues/48757
121+
"-Wno-elaborated-enum-base"
122+
]
123+
);
124+
125+
enableParallelBuilding = true;
126+
127+
passthru.tests = {
128+
inherit (python3.pkgs) grpcio-status grpcio-tools jaxlib;
129+
inherit arrow-cpp;
130+
};
131+
132+
meta = with lib; {
133+
description = "C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)";
134+
license = licenses.asl20;
135+
maintainers = with maintainers; [ lnl7 ];
136+
homepage = "https://grpc.io/";
137+
platforms = platforms.all;
138+
changelog = "https://github.com/grpc/grpc/releases/tag/v${version}";
139+
};
140+
}

nix/protobuf_29_2.nix

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
enableShared ? !stdenv.hostPlatform.isStatic,
1717

1818
testers,
19-
protobuf,
2019
...
2120
}:
2221

@@ -93,16 +92,6 @@ stdenv.mkDerivation (finalAttrs: {
9392
versionCheckProgramArg = [ "--version" ];
9493
doInstallCheck = true;
9594

96-
passthru = {
97-
tests = {
98-
pythonProtobuf = python3.pkgs.protobuf;
99-
inherit grpc;
100-
version = testers.testVersion { package = protobuf; };
101-
};
102-
103-
inherit abseil-cpp;
104-
};
105-
10695
meta = {
10796
description = "Google's data interchange format";
10897
longDescription = ''

0 commit comments

Comments
 (0)