nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1# The cmake version of this build is meant to enable both cmake and .pc being exported
2# this is important because grpc exports a .cmake file which also expects for protobuf
3# to have been exported through cmake as well.
4{
5 lib,
6 stdenv,
7 abseil-cpp,
8 buildPackages,
9 cmake,
10 fetchFromGitHub,
11 fetchpatch,
12 gtest,
13 zlib,
14 version,
15 hash,
16 versionCheckHook,
17
18 # downstream dependencies
19 python3,
20 grpc,
21 enableShared ? !stdenv.hostPlatform.isStatic,
22
23 testers,
24 protobuf,
25 ...
26}:
27
28stdenv.mkDerivation (finalAttrs: {
29 pname = "protobuf";
30 inherit version;
31
32 src = fetchFromGitHub {
33 owner = "protocolbuffers";
34 repo = "protobuf";
35 tag = "v${version}";
36 inherit hash;
37 };
38
39 patches =
40 lib.optionals (lib.versionOlder version "22") [
41 # fix protobuf-targets.cmake installation paths, and allow for CMAKE_INSTALL_LIBDIR to be absolute
42 # https://github.com/protocolbuffers/protobuf/pull/10090
43 (fetchpatch {
44 url = "https://github.com/protocolbuffers/protobuf/commit/a7324f88e92bc16b57f3683403b6c993bf68070b.patch";
45 hash = "sha256-SmwaUjOjjZulg/wgNmR/F5b8rhYA2wkKAjHIOxjcQdQ=";
46 })
47 ]
48 ++ lib.optionals (lib.versions.major version == "29") [
49 # fix temporary directory handling to avoid test failures on darwin
50 # https://github.com/NixOS/nixpkgs/issues/464439
51 (fetchpatch {
52 url = "https://github.com/protocolbuffers/protobuf/commit/0e9d0f6e77280b7a597ebe8361156d6bb1971dca.patch";
53 hash = "sha256-rIP+Ft/SWVwh9Oy8y8GSUBgP6CtLCLvGmr6nOqmyHhY=";
54 })
55 ]
56 ++ lib.optionals (lib.versionAtLeast version "30") [
57 # workaround nvcc bug in message_lite.h
58 # https://github.com/protocolbuffers/protobuf/issues/21542
59 # Caused by: https://github.com/protocolbuffers/protobuf/commit/8f7aab29b21afb89ea0d6e2efeafd17ca71486a9
60 #
61 # A specific consequence of this bug is a test failure when building onnxruntime with cudaSupport
62 # See https://github.com/NixOS/nixpkgs/pull/450587#discussion_r2698215974
63 (fetchpatch {
64 url = "https://github.com/protocolbuffers/protobuf/commit/211f52431b9ec30d4d4a1c76aafd64bd78d93c43.patch";
65 hash = "sha256-2/vc4anc+kH7otfLHfBtW8dRowPyObiXZn0+HtQktak=";
66 })
67 ]
68 ++ lib.optionals (lib.versionAtLeast version "34") [
69 # upb linker-array fix for newer toolchains (notably GCC 15):
70 # `UPB_linkarr_internal_empty_upb_AllExts` can conflict with extension
71 # entries in `linkarr_upb_AllExts` during test builds.
72 # Context: https://github.com/protocolbuffers/protobuf/issues/21021
73 ./fix-upb-linkarr-sentinel-init.patch
74 ];
75
76 postPatch =
77 lib.optionalString (stdenv.hostPlatform.isDarwin && lib.versionOlder version "29") ''
78 substituteInPlace src/google/protobuf/testing/googletest.cc \
79 --replace-fail 'tmpnam(b)' '"'$TMPDIR'/foo"'
80 ''
81 # Keep the sentinel macro non-retained for GCC 15+ to match generated
82 # extension objects in linker arrays and avoid section type conflicts.
83 + lib.optionalString (lib.versionAtLeast version "34") ''
84 substituteInPlace upb/port/def.inc \
85 --replace-fail \
86 '#define UPB_LINKARR_SENTINEL UPB_RETAIN __attribute__((weak, used))' \
87 '#define UPB_LINKARR_SENTINEL __attribute__((weak, used))'
88 ''
89 # Fix gcc15 build failures due to missing <cstring>
90 + lib.optionalString ((lib.versions.major version) == "25") ''
91 sed -i '1i #include <cstring>' third_party/utf8_range/utf8_validity.cc
92 '';
93
94 preHook = ''
95 export build_protobuf=${
96 if (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) then
97 buildPackages."protobuf_${lib.versions.major version}"
98 else
99 (placeholder "out")
100 };
101 '';
102
103 # hook to provide the path to protoc executable, used at build time
104 setupHook = ./setup-hook.sh;
105
106 nativeBuildInputs = [
107 cmake
108 ];
109
110 buildInputs = [
111 gtest
112 zlib
113 ];
114
115 propagatedBuildInputs = [
116 abseil-cpp
117 ];
118
119 strictDeps = true;
120
121 separateDebugInfo = true;
122
123 cmakeDir = if lib.versionOlder version "22" then "../cmake" else null;
124 cmakeFlags = [
125 (lib.cmakeBool "protobuf_USE_EXTERNAL_GTEST" true)
126 (lib.cmakeFeature "protobuf_ABSL_PROVIDER" "package")
127 (lib.cmakeBool "protobuf_BUILD_TESTS" finalAttrs.finalPackage.doCheck)
128 ]
129 ++ lib.optionals enableShared [
130 (lib.cmakeBool "protobuf_BUILD_SHARED_LIBS" true)
131 ];
132
133 doCheck =
134 # Tests fail to build on 32-bit platforms; fixed in 22.x
135 # https://github.com/protocolbuffers/protobuf/issues/10418
136 # Also AnyTest.TestPackFromSerializationExceedsSizeLimit fails on 32-bit platforms
137 # https://github.com/protocolbuffers/protobuf/issues/8460
138 !stdenv.hostPlatform.is32bit;
139
140 nativeInstallCheckInputs = [
141 versionCheckHook
142 ];
143 doInstallCheck = true;
144
145 env = lib.optionalAttrs (lib.versions.major version == "29") {
146 GTEST_DEATH_TEST_STYLE = "threadsafe";
147 };
148
149 passthru = {
150 tests = {
151 pythonProtobuf = python3.pkgs.protobuf;
152 inherit grpc;
153 inherit (python3.pkgs) celery;
154
155 version = testers.testVersion { package = protobuf; };
156 };
157
158 inherit abseil-cpp;
159 };
160
161 meta = {
162 description = "Google's data interchange format";
163 longDescription = ''
164 Protocol Buffers are a way of encoding structured data in an efficient
165 yet extensible format. Google uses Protocol Buffers for almost all of
166 its internal RPC protocols and file formats.
167 '';
168 license = lib.licenses.bsd3;
169 platforms = lib.platforms.all;
170 homepage = "https://protobuf.dev/";
171 maintainers = with lib.maintainers; [ GaetanLepage ];
172 mainProgram = "protoc";
173 };
174})