Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 bazel,
3 bazelTest,
4 fetchFromGitHub,
5 fetchurl,
6 stdenv,
7 cctools,
8 lib,
9 openjdk8,
10 jdk11_headless,
11 runLocal,
12 runtimeShell,
13 writeScript,
14 writeText,
15 distDir,
16}:
17
18let
19 com_google_protobuf = fetchFromGitHub {
20 owner = "protocolbuffers";
21 repo = "protobuf";
22 rev = "v3.13.0";
23 sha256 = "1nqsvi2yfr93kiwlinz8z7c68ilg1j75b2vcpzxzvripxx5h6xhd";
24 };
25
26 bazel_skylib = fetchFromGitHub {
27 owner = "bazelbuild";
28 repo = "bazel-skylib";
29 rev = "2ec2e6d715e993d96ad6222770805b5bd25399ae";
30 sha256 = "1z2r2vx6kj102zvp3j032djyv99ski1x1sl4i3p6mswnzrzna86s";
31 };
32
33 rules_python = fetchFromGitHub {
34 owner = "bazelbuild";
35 repo = "rules_python";
36 rev = "c8c79aae9aa1b61d199ad03d5fe06338febd0774";
37 sha256 = "1zn58wv5wcylpi0xj7riw34i1jjpqahanxx8y9srwrv0v93b6pqz";
38 };
39
40 rules_proto = fetchFromGitHub {
41 owner = "bazelbuild";
42 repo = "rules_proto";
43 rev = "a0761ed101b939e19d83b2da5f59034bffc19c12";
44 sha256 = "09lqfj5fxm1fywxr5w8pnpqd859gb6751jka9fhxjxjzs33glhqf";
45 };
46
47 net_zlib = fetchurl rec {
48 url = "https://zlib.net/zlib-1.2.11.tar.gz";
49 sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1";
50
51 passthru.sha256 = sha256;
52 };
53
54 WORKSPACE = writeText "WORKSPACE" ''
55 workspace(name = "our_workspace")
56
57 load("//:proto-support.bzl", "protobuf_deps")
58 protobuf_deps()
59 load("@rules_proto//proto:repositories.bzl", "rules_proto_toolchains")
60 rules_proto_toolchains()
61 '';
62
63 protoSupport = writeText "proto-support.bzl" ''
64 """Load dependencies needed to compile the protobuf library as a 3rd-party consumer."""
65
66 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
67
68 def protobuf_deps():
69 """Loads common dependencies needed to compile the protobuf library."""
70
71 if "zlib" not in native.existing_rules():
72 # proto_library, cc_proto_library, and java_proto_library rules implicitly
73 # depend on @com_google_protobuf for protoc and proto runtimes.
74 # This statement defines the @com_google_protobuf repo.
75 native.local_repository(
76 name = "com_google_protobuf",
77 path = "${com_google_protobuf}",
78 )
79 native.local_repository(
80 name = "bazel_skylib",
81 path = "${bazel_skylib}",
82 )
83 native.local_repository(
84 name = "rules_proto",
85 path = "${rules_proto}",
86 )
87 native.local_repository(
88 name = "rules_python",
89 path = "${rules_python}",
90 )
91
92 http_archive(
93 name = "zlib",
94 build_file = "@com_google_protobuf//:third_party/zlib.BUILD",
95 sha256 = "${net_zlib.sha256}",
96 strip_prefix = "zlib-1.2.11",
97 urls = ["file://${net_zlib}"],
98 )
99 '';
100
101 personProto = writeText "person.proto" ''
102 syntax = "proto3";
103
104 package person;
105
106 message Person {
107 string name = 1;
108 int32 id = 2;
109 string email = 3;
110 }
111 '';
112
113 personBUILD = writeText "BUILD" ''
114 load("@rules_proto//proto:defs.bzl", "proto_library")
115
116 proto_library(
117 name = "person_proto",
118 srcs = ["person.proto"],
119 visibility = ["//visibility:public"],
120 )
121
122 java_proto_library(
123 name = "person_java_proto",
124 deps = [":person_proto"],
125 )
126
127 cc_proto_library(
128 name = "person_cc_proto",
129 deps = [":person_proto"],
130 )
131 '';
132
133 toolsBazel = writeScript "bazel" ''
134 #! ${runtimeShell}
135
136 export CXX='${stdenv.cc}/bin/clang++'
137 export LD='${cctools}/bin/ld'
138 export LIBTOOL='${cctools}/bin/libtool'
139 export CC='${stdenv.cc}/bin/clang'
140
141 # XXX: hack for macosX, this flags disable bazel usage of xcode
142 # See: https://github.com/bazelbuild/bazel/issues/4231
143 export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
144
145 exec "$BAZEL_REAL" "$@"
146 '';
147
148 workspaceDir = runLocal "our_workspace" { } (
149 ''
150 mkdir $out
151 cp ${WORKSPACE} $out/WORKSPACE
152 touch $out/BUILD.bazel
153 cp ${protoSupport} $out/proto-support.bzl
154 mkdir $out/person
155 cp ${personProto} $out/person/person.proto
156 cp ${personBUILD} $out/person/BUILD.bazel
157 ''
158 + (lib.optionalString stdenv.hostPlatform.isDarwin ''
159 mkdir $out/tools
160 cp ${toolsBazel} $out/tools/bazel
161 '')
162 );
163
164 testBazel = bazelTest {
165 name = "${bazel.pname}-test-protocol-buffers";
166 inherit workspaceDir;
167 bazelPkg = bazel;
168 buildInputs = [
169 (if lib.strings.versionOlder bazel.version "5.0.0" then openjdk8 else jdk11_headless)
170 ];
171 bazelScript = ''
172 ${bazel}/bin/bazel \
173 build \
174 --distdir=${distDir} \
175 --verbose_failures \
176 --curses=no \
177 --subcommands \
178 --strict_java_deps=off \
179 --strict_proto_deps=off \
180 //... \
181 ''
182 + lib.optionalString (lib.strings.versionOlder bazel.version "5.0.0") ''
183 --host_javabase='@local_jdk//:jdk' \
184 --java_toolchain='@bazel_tools//tools/jdk:toolchain_hostjdk8' \
185 --javabase='@local_jdk//:jdk' \
186 ''
187 + lib.optionalString (stdenv.hostPlatform.isDarwin) ''
188 --cxxopt=-x --cxxopt=c++ --host_cxxopt=-x --host_cxxopt=c++ \
189 --linkopt=-stdlib=libc++ --host_linkopt=-stdlib=libc++ \
190 '';
191 };
192
193in
194testBazel