1{ lib
2, stdenv
3, fetchFromGitHub
4, fetchpatch
5, c-ares
6, cmake
7, crc32c
8, curl
9, gbenchmark
10, grpc
11, gtest
12, ninja
13, nlohmann_json
14, openssl
15, pkg-config
16, protobuf
17 # default list of APIs: https://github.com/googleapis/google-cloud-cpp/blob/v1.32.1/CMakeLists.txt#L173
18, apis ? [ "*" ]
19, staticOnly ? stdenv.hostPlatform.isStatic
20}:
21let
22 # defined in cmake/GoogleapisConfig.cmake
23 googleapisRev = "85f8c758016c279fb7fa8f0d51ddc7ccc0dd5e05";
24 googleapis = fetchFromGitHub {
25 name = "googleapis-src";
26 owner = "googleapis";
27 repo = "googleapis";
28 rev = googleapisRev;
29 hash = "sha256-4Qiz0pBgW3OZi+Z8Zq6k9E94+8q6/EFMwPh8eQxDjdI=";
30 };
31in
32stdenv.mkDerivation rec {
33 pname = "google-cloud-cpp";
34 version = "2.14.0";
35
36 src = fetchFromGitHub {
37 owner = "googleapis";
38 repo = "google-cloud-cpp";
39 rev = "v${version}";
40 sha256 = "sha256-0SoOaAqvk8cVC5W3ejTfe4O/guhrro3uAzkeIpAkCpg=";
41 };
42
43 patches = [
44 # https://github.com/googleapis/google-cloud-cpp/pull/12554, tagged in 2.16.0
45 (fetchpatch {
46 name = "prepare-for-GCC-13.patch";
47 url = "https://github.com/googleapis/google-cloud-cpp/commit/ae30135c86982c36e82bb0f45f99baa48c6a780b.patch";
48 hash = "sha256-L0qZfdhP8Zt/gYBWvJafteVgBHR8Kup49RoOrLDtj3k=";
49 })
50 ];
51
52 postPatch = ''
53 substituteInPlace external/googleapis/CMakeLists.txt \
54 --replace "https://github.com/googleapis/googleapis/archive/\''${_GOOGLE_CLOUD_CPP_GOOGLEAPIS_COMMIT_SHA}.tar.gz" "file://${googleapis}"
55 sed -i '/https:\/\/storage.googleapis.com\/cloud-cpp-community-archive\/com_google_googleapis/d' external/googleapis/CMakeLists.txt
56 '';
57
58 nativeBuildInputs = [
59 cmake
60 ninja
61 pkg-config
62 ] ++ lib.optionals (!doInstallCheck) [
63 # enable these dependencies when doInstallCheck is false because we're
64 # unconditionally building tests and benchmarks
65 #
66 # when doInstallCheck is true, these deps are added to nativeInstallCheckInputs
67 gbenchmark
68 gtest
69 ];
70
71 buildInputs = [
72 c-ares
73 crc32c
74 (curl.override { inherit openssl; })
75 grpc
76 nlohmann_json
77 openssl
78 protobuf
79 ];
80
81 # https://hydra.nixos.org/build/222679737/nixlog/3/tail
82 env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.isAarch64 "-Wno-error=maybe-uninitialized";
83
84 doInstallCheck = true;
85
86 preInstallCheck =
87 let
88 # These paths are added to (DY)LD_LIBRARY_PATH because they contain
89 # testing-only shared libraries that do not need to be installed, but
90 # need to be loadable by the test executables.
91 #
92 # Setting (DY)LD_LIBRARY_PATH is only necessary when building shared libraries.
93 additionalLibraryPaths = [
94 "$PWD/google/cloud/bigtable"
95 "$PWD/google/cloud/bigtable/benchmarks"
96 "$PWD/google/cloud/pubsub"
97 "$PWD/google/cloud/spanner"
98 "$PWD/google/cloud/spanner/benchmarks"
99 "$PWD/google/cloud/storage"
100 "$PWD/google/cloud/storage/benchmarks"
101 "$PWD/google/cloud/testing_util"
102 ];
103 ldLibraryPathName = "${lib.optionalString stdenv.isDarwin "DY"}LD_LIBRARY_PATH";
104 in
105 lib.optionalString doInstallCheck (
106 lib.optionalString (!staticOnly) ''
107 export ${ldLibraryPathName}=${lib.concatStringsSep ":" additionalLibraryPaths}
108 ''
109 );
110
111 installCheckPhase = lib.optionalString doInstallCheck ''
112 runHook preInstallCheck
113
114 # Disable any integration tests, which need to contact the internet.
115 # Also disable the `storage_benchmark_*` tests.
116 # With Protobuf < 23.x they require -DGOOGLE_CLOUD_CPP_ENABLE_CTYPE_WORKAROUND=ON.
117 # With Protobuf >= 23.x they require They require setting -DGOOGLE_CLOUD_CPP_ENABLE_CTYPE_WORKAROUND=OFF
118 ctest --label-exclude integration-test --exclude-regex storage_benchmarks_
119
120 runHook postInstallCheck
121 '';
122
123 nativeInstallCheckInputs = lib.optionals doInstallCheck [
124 gbenchmark
125 gtest
126 ];
127
128 cmakeFlags = [
129 "-DBUILD_SHARED_LIBS:BOOL=${if staticOnly then "OFF" else "ON"}"
130 # unconditionally build tests to catch linker errors as early as possible
131 # this adds a good chunk of time to the build
132 "-DBUILD_TESTING:BOOL=ON"
133 "-DGOOGLE_CLOUD_CPP_ENABLE_EXAMPLES:BOOL=OFF"
134 ] ++ lib.optionals (apis != [ "*" ]) [
135 "-DGOOGLE_CLOUD_CPP_ENABLE=${lib.concatStringsSep ";" apis}"
136 ];
137
138 requiredSystemFeatures = [ "big-parallel" ];
139
140 meta = with lib; {
141 license = with licenses; [ asl20 ];
142 homepage = "https://github.com/googleapis/google-cloud-cpp";
143 description = "C++ Idiomatic Clients for Google Cloud Platform services";
144 platforms = [ "x86_64-linux" "aarch64-linux" ];
145 maintainers = with maintainers; [ cpcloud ];
146 };
147}