Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 stdenv,
3 callPackage,
4 lib,
5 fetchurl,
6 fetchFromGitHub,
7 installShellFiles,
8 runCommand,
9 runCommandCC,
10 makeWrapper,
11 recurseIntoAttrs,
12 # this package (through the fixpoint glass)
13 bazel_self,
14 lr,
15 xe,
16 zip,
17 unzip,
18 bash,
19 coreutils,
20 which,
21 gawk,
22 gnused,
23 gnutar,
24 gnugrep,
25 gzip,
26 findutils,
27 diffutils,
28 gnupatch,
29 # updater
30 python3,
31 writeScript,
32 # Apple dependencies
33 cctools,
34 sigtool,
35 # Allow to independently override the jdks used to build and run respectively
36 buildJdk,
37 runJdk,
38 runtimeShell,
39 # Always assume all markers valid (this is needed because we remove markers; they are non-deterministic).
40 # Also, don't clean up environment variables (so that NIX_ environment variables are passed to compilers).
41 enableNixHacks ? false,
42 file,
43 replaceVars,
44 writeTextFile,
45 writeShellApplication,
46 makeBinaryWrapper,
47}:
48
49let
50 version = "6.5.0";
51 sourceRoot = ".";
52
53 src = fetchurl {
54 url = "https://github.com/bazelbuild/bazel/releases/download/${version}/bazel-${version}-dist.zip";
55 hash = "sha256-/InakZQVKJ8p5P8YpeAScOzppv6Dy2CWchi6xKO7PtI=";
56 };
57
58 # Update with
59 # 1. export BAZEL_SELF=$(nix-build -A bazel_6)
60 # 2. update version and hash for sources above
61 # 3. `eval $(nix-build -A bazel_6.updater)`
62 # 4. add new dependencies from the dict in ./src-deps.json if required by failing build
63 srcDeps = lib.attrsets.attrValues srcDepsSet;
64 srcDepsSet =
65 let
66 srcs = lib.importJSON ./src-deps.json;
67 toFetchurl =
68 d:
69 lib.attrsets.nameValuePair d.name (fetchurl {
70 urls = d.urls or [ d.url ];
71 sha256 = d.sha256;
72 });
73 in
74 builtins.listToAttrs (
75 map toFetchurl [
76 srcs.desugar_jdk_libs
77 srcs.io_bazel_skydoc
78 srcs.bazel_skylib
79 srcs.bazelci_rules
80 srcs.io_bazel_rules_sass
81 srcs.platforms
82 srcs.remote_java_tools_for_testing
83 srcs."coverage_output_generator-v2.6.zip"
84 srcs.build_bazel_rules_nodejs
85 srcs.android_tools_for_testing
86 srcs.openjdk_linux_vanilla
87 srcs.bazel_toolchains
88 srcs.com_github_grpc_grpc
89 srcs.upb
90 srcs.com_google_protobuf
91 srcs.rules_pkg
92 srcs.rules_cc
93 srcs.rules_java
94 srcs.rules_proto
95 srcs.rules_nodejs
96 srcs.rules_license
97 srcs.com_google_absl
98 srcs.com_googlesource_code_re2
99 srcs.com_github_cares_cares
100 srcs.com_envoyproxy_protoc_gen_validate
101 srcs.com_google_googleapis
102 srcs.bazel_gazelle
103 ]
104 );
105
106 distDir = runCommand "bazel-deps" { } ''
107 mkdir -p $out
108 for i in ${builtins.toString srcDeps}; do cp $i $out/$(stripHash $i); done
109 '';
110
111 defaultShellUtils =
112 # Keep this list conservative. For more exotic tools, prefer to use
113 # @rules_nixpkgs to pull in tools from the nix repository. Example:
114 #
115 # WORKSPACE:
116 #
117 # nixpkgs_git_repository(
118 # name = "nixpkgs",
119 # revision = "def5124ec8367efdba95a99523dd06d918cb0ae8",
120 # )
121 #
122 # # This defines an external Bazel workspace.
123 # nixpkgs_package(
124 # name = "bison",
125 # repositories = { "nixpkgs": "@nixpkgs//:default.nix" },
126 # )
127 #
128 # some/BUILD.bazel:
129 #
130 # genrule(
131 # ...
132 # cmd = "$(location @bison//:bin/bison) -other -args",
133 # tools = [
134 # ...
135 # "@bison//:bin/bison",
136 # ],
137 # )
138 [
139 bash
140 coreutils
141 diffutils
142 file
143 findutils
144 gawk
145 gnugrep
146 gnupatch
147 gnused
148 gnutar
149 gzip
150 python3
151 unzip
152 which
153 zip
154 ];
155
156 defaultShellPath = lib.makeBinPath defaultShellUtils;
157
158 bashWithDefaultShellUtilsSh = writeShellApplication {
159 name = "bash";
160 runtimeInputs = defaultShellUtils;
161 text = ''
162 if [[ "$PATH" == "/no-such-path" ]]; then
163 export PATH=${defaultShellPath}
164 fi
165 exec ${bash}/bin/bash "$@"
166 '';
167 };
168
169 # Script-based interpreters in shebangs aren't guaranteed to work,
170 # especially on MacOS. So let's produce a binary
171 bashWithDefaultShellUtils = stdenv.mkDerivation {
172 name = "bash";
173 src = bashWithDefaultShellUtilsSh;
174 nativeBuildInputs = [ makeBinaryWrapper ];
175 buildPhase = ''
176 makeWrapper ${bashWithDefaultShellUtilsSh}/bin/bash $out/bin/bash
177 '';
178 };
179
180 platforms = lib.platforms.linux ++ lib.platforms.darwin;
181
182 system = if stdenv.hostPlatform.isDarwin then "darwin" else "linux";
183
184 # on aarch64 Darwin, `uname -m` returns "arm64"
185 arch = with stdenv.hostPlatform; if isDarwin && isAarch64 then "arm64" else parsed.cpu.name;
186
187 bazelRC = writeTextFile {
188 name = "bazel-rc";
189 text = ''
190 startup --server_javabase=${runJdk}
191
192 # Can't use 'common'; https://github.com/bazelbuild/bazel/issues/3054
193 # Most commands inherit from 'build' anyway.
194 build --distdir=${distDir}
195 fetch --distdir=${distDir}
196 query --distdir=${distDir}
197
198 build --extra_toolchains=@bazel_tools//tools/jdk:nonprebuilt_toolchain_definition
199 build --tool_java_runtime_version=local_jdk_11
200 build --java_runtime_version=local_jdk_11
201
202 # load default location for the system wide configuration
203 try-import /etc/bazel.bazelrc
204 '';
205 };
206
207in
208stdenv.mkDerivation rec {
209 pname = "bazel${lib.optionalString enableNixHacks "-hacks"}";
210 inherit version;
211
212 meta = with lib; {
213 homepage = "https://github.com/bazelbuild/bazel/";
214 description = "Build tool that builds code quickly and reliably";
215 sourceProvenance = with sourceTypes; [
216 fromSource
217 binaryBytecode # source bundles dependencies as jars
218 ];
219 license = licenses.asl20;
220 teams = [ lib.teams.bazel ];
221 mainProgram = "bazel";
222 inherit platforms;
223 };
224
225 inherit src;
226 inherit sourceRoot;
227 patches = [
228 # upb definition inside bazel sets its own copts that take precedence
229 # over flags we set externally, so need to patch them at the source
230 ./upb-clang16.patch
231
232 # Force usage of the _non_ prebuilt java toolchain.
233 # the prebuilt one does not work in nix world.
234 ./java_toolchain.patch
235
236 # Bazel integrates with apple IOKit to inhibit and track system sleep.
237 # Inside the darwin sandbox, these API calls are blocked, and bazel
238 # crashes. It seems possible to allow these APIs inside the sandbox, but it
239 # feels simpler to patch bazel not to use it at all. So our bazel is
240 # incapable of preventing system sleep, which is a small price to pay to
241 # guarantee that it will always run in any nix context.
242 #
243 # See also ./bazel_darwin_sandbox.patch in bazel_5. That patch uses
244 # NIX_BUILD_TOP env var to conditionally disable sleep features inside the
245 # sandbox.
246 #
247 # If you want to investigate the sandbox profile path,
248 # IORegisterForSystemPower can be allowed with
249 #
250 # propagatedSandboxProfile = ''
251 # (allow iokit-open (iokit-user-client-class "RootDomainUserClient"))
252 # '';
253 #
254 # I do not know yet how to allow IOPMAssertion{CreateWithName,Release}
255 ./darwin_sleep.patch
256
257 # On Darwin, the last argument to gcc is coming up as an empty string. i.e: ''
258 # This is breaking the build of any C target. This patch removes the last
259 # argument if it's found to be an empty string.
260 ../trim-last-argument-to-gcc-if-empty.patch
261
262 # `java_proto_library` ignores `strict_proto_deps`
263 # https://github.com/bazelbuild/bazel/pull/16146
264 ./strict_proto_deps.patch
265
266 # On Darwin, using clang 6 to build fails because of a linker error (see #105573),
267 # but using clang 7 fails because libarclite_macosx.a cannot be found when linking
268 # the xcode_locator tool.
269 # This patch removes using the -fobjc-arc compiler option and makes the code
270 # compile without automatic reference counting. Caveat: this leaks memory, but
271 # we accept this fact because xcode_locator is only a short-lived process used during the build.
272 (replaceVars ./no-arc.patch {
273 multiBinPatch = if stdenv.hostPlatform.system == "aarch64-darwin" then "arm64" else "x86_64";
274 })
275
276 # --experimental_strict_action_env (which may one day become the default
277 # see bazelbuild/bazel#2574) hardcodes the default
278 # action environment to a non hermetic value (e.g. "/usr/local/bin").
279 # This is non hermetic on non-nixos systems. On NixOS, bazel cannot find the required binaries.
280 # So we are replacing this bazel paths by defaultShellPath,
281 # improving hermeticity and making it work in nixos.
282 (replaceVars ../strict_action_env.patch {
283 strictActionEnvPatch = defaultShellPath;
284 })
285
286 (replaceVars ./actions_path.patch {
287 actionsPathPatch = defaultShellPath;
288 })
289
290 # bazel reads its system bazelrc in /etc
291 # override this path to a builtin one
292 (replaceVars ../bazel_rc.patch {
293 bazelSystemBazelRCPath = bazelRC;
294 })
295 ]
296 ++ lib.optional enableNixHacks ./nix-hacks.patch;
297
298 # Additional tests that check bazel’s functionality. Execute
299 #
300 # nix-build . -A bazel_6.tests
301 #
302 # in the nixpkgs checkout root to exercise them locally.
303 passthru.tests =
304 let
305 runLocal =
306 name: attrs: script:
307 let
308 attrs' = removeAttrs attrs [ "buildInputs" ];
309 buildInputs = attrs.buildInputs or [ ];
310 in
311 runCommandCC name (
312 {
313 inherit buildInputs;
314 preferLocalBuild = true;
315 meta.platforms = platforms;
316 }
317 // attrs'
318 ) script;
319
320 # bazel wants to extract itself into $install_dir/install every time it runs,
321 # so let’s do that only once.
322 extracted =
323 bazelPkg:
324 let
325 install_dir =
326 # `install_base` field printed by `bazel info`, minus the hash.
327 # yes, this path is kinda magic. Sorry.
328 "$HOME/.cache/bazel/_bazel_nixbld";
329 in
330 runLocal "bazel-extracted-homedir" { passthru.install_dir = install_dir; } ''
331 export HOME=$(mktemp -d)
332 touch WORKSPACE # yeah, everything sucks
333 install_base="$(${bazelPkg}/bin/bazel info | grep install_base)"
334 # assert it’s actually below install_dir
335 [[ "$install_base" =~ ${install_dir} ]] \
336 || (echo "oh no! $install_base but we are \
337 trying to copy ${install_dir} to $out instead!"; exit 1)
338 cp -R ${install_dir} $out
339 '';
340
341 bazelTest =
342 {
343 name,
344 bazelScript,
345 workspaceDir,
346 bazelPkg,
347 buildInputs ? [ ],
348 }:
349 let
350 be = extracted bazelPkg;
351 in
352 runLocal name
353 {
354 inherit buildInputs;
355 # Necessary for the tests to pass on Darwin with sandbox enabled.
356 __darwinAllowLocalNetworking = true;
357 }
358 (
359 # skip extraction caching on Darwin, because nobody knows how Darwin works
360 (lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
361 # set up home with pre-unpacked bazel
362 export HOME=$(mktemp -d)
363 mkdir -p ${be.install_dir}
364 cp -R ${be}/install ${be.install_dir}
365
366 # https://stackoverflow.com/questions/47775668/bazel-how-to-skip-corrupt-installation-on-centos6
367 # Bazel checks whether the mtime of the install dir files
368 # is >9 years in the future, otherwise it extracts itself again.
369 # see PosixFileMTime::IsUntampered in src/main/cpp/util
370 # What the hell bazel.
371 ${lr}/bin/lr -0 -U ${be.install_dir} | ${xe}/bin/xe -N0 -0 touch --date="9 years 6 months" {}
372 '')
373 + ''
374 # Note https://github.com/bazelbuild/bazel/issues/5763#issuecomment-456374609
375 # about why to create a subdir for the workspace.
376 cp -r ${workspaceDir} wd && chmod u+w wd && cd wd
377
378 ${bazelScript}
379
380 touch $out
381 ''
382 );
383
384 bazelWithNixHacks = bazel_self.override { enableNixHacks = true; };
385
386 bazel-examples = fetchFromGitHub {
387 owner = "bazelbuild";
388 repo = "examples";
389 rev = "4183fc709c26a00366665e2d60d70521dc0b405d";
390 sha256 = "1mm4awx6sa0myiz9j4hwp71rpr7yh8vihf3zm15n2ii6xb82r31k";
391 };
392
393 in
394 (lib.optionalAttrs (!stdenv.hostPlatform.isDarwin) {
395 # `extracted` doesn’t work on darwin
396 shebang = callPackage ../shebang-test.nix {
397 inherit
398 runLocal
399 extracted
400 bazelTest
401 distDir
402 ;
403 bazel = bazel_self;
404 };
405 })
406 // {
407 bashTools = callPackage ../bash-tools-test.nix {
408 inherit runLocal bazelTest distDir;
409 bazel = bazel_self;
410 };
411 cpp = callPackage ../cpp-test.nix {
412 inherit
413 runLocal
414 bazelTest
415 bazel-examples
416 distDir
417 ;
418 bazel = bazel_self;
419 };
420 java = callPackage ../java-test.nix {
421 inherit
422 runLocal
423 bazelTest
424 bazel-examples
425 distDir
426 ;
427 bazel = bazel_self;
428 };
429 protobuf = callPackage ../protobuf-test.nix {
430 inherit runLocal bazelTest distDir;
431 bazel = bazel_self;
432 };
433 pythonBinPath = callPackage ../python-bin-path-test.nix {
434 inherit runLocal bazelTest distDir;
435 bazel = bazel_self;
436 };
437
438 bashToolsWithNixHacks = callPackage ../bash-tools-test.nix {
439 inherit runLocal bazelTest distDir;
440 bazel = bazelWithNixHacks;
441 };
442
443 cppWithNixHacks = callPackage ../cpp-test.nix {
444 inherit
445 runLocal
446 bazelTest
447 bazel-examples
448 distDir
449 ;
450 bazel = bazelWithNixHacks;
451 };
452 javaWithNixHacks = callPackage ../java-test.nix {
453 inherit
454 runLocal
455 bazelTest
456 bazel-examples
457 distDir
458 ;
459 bazel = bazelWithNixHacks;
460 };
461 protobufWithNixHacks = callPackage ../protobuf-test.nix {
462 inherit runLocal bazelTest distDir;
463 bazel = bazelWithNixHacks;
464 };
465 pythonBinPathWithNixHacks = callPackage ../python-bin-path-test.nix {
466 inherit runLocal bazelTest distDir;
467 bazel = bazelWithNixHacks;
468 };
469 };
470
471 src_for_updater = stdenv.mkDerivation {
472 name = "updater-sources";
473 inherit src;
474 nativeBuildInputs = [ unzip ];
475 inherit sourceRoot;
476 installPhase = ''
477 runHook preInstall
478
479 # prevent bazel version check failing in the updater
480 rm .bazelversion
481 cp -r . "$out"
482
483 runHook postInstall
484 '';
485 };
486 # update the list of workspace dependencies
487 passthru.updater = writeScript "update-bazel-deps.sh" ''
488 #!${runtimeShell}
489 (cd "${src_for_updater}" &&
490 BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \
491 "$BAZEL_SELF"/bin/bazel \
492 query 'kind(http_archive, //external:*) + kind(http_file, //external:*) + kind(distdir_tar, //external:*) + kind(git_repository, //external:*)' \
493 --loading_phase_threads=1 \
494 --output build) \
495 | "${python3}"/bin/python3 "${./update-srcDeps.py}" \
496 "${builtins.toString ./src-deps.json}"
497 '';
498
499 # Necessary for the tests to pass on Darwin with sandbox enabled.
500 # Bazel starts a local server and needs to bind a local address.
501 __darwinAllowLocalNetworking = true;
502
503 postPatch =
504 let
505
506 darwinPatches = ''
507 bazelLinkFlags () {
508 eval set -- "$NIX_LDFLAGS"
509 local flag
510 for flag in "$@"; do
511 printf ' -Wl,%s' "$flag"
512 done
513 }
514
515 # Disable Bazel's Xcode toolchain detection which would configure compilers
516 # and linkers from Xcode instead of from PATH
517 export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
518
519 # Explicitly configure gcov since we don't have it on Darwin, so autodetection fails
520 export GCOV=${coreutils}/bin/false
521
522 # libcxx includes aren't added by libcxx hook
523 # https://github.com/NixOS/nixpkgs/pull/41589
524 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -isystem ${lib.getInclude stdenv.cc.libcxx}/include/c++/v1"
525 # for CLang 16 compatibility in external/{absl,upb} dependencies
526 export NIX_CFLAGS_COMPILE+=" -Wno-deprecated-builtins -Wno-gnu-offsetof-extensions"
527
528 # don't use system installed Xcode to run clang, use Nix clang instead
529 sed -i -E \
530 -e "s;/usr/bin/xcrun (--sdk macosx )?clang;${stdenv.cc}/bin/clang $NIX_CFLAGS_COMPILE $(bazelLinkFlags) -framework CoreFoundation;g" \
531 -e "s;/usr/bin/codesign;CODESIGN_ALLOCATE=${cctools}/bin/${cctools.targetPrefix}codesign_allocate ${sigtool}/bin/codesign;" \
532 -e "s;env -i codesign;env -i CODESIGN_ALLOCATE=${cctools}/bin/${cctools.targetPrefix}codesign_allocate ${sigtool}/bin/codesign;" \
533 scripts/bootstrap/compile.sh \
534 tools/osx/BUILD
535
536 substituteInPlace scripts/bootstrap/compile.sh --replace ' -mmacosx-version-min=10.9' ""
537
538 # nixpkgs's libSystem cannot use pthread headers directly, must import GCD headers instead
539 sed -i -e "/#include <pthread\/spawn.h>/i #include <dispatch/dispatch.h>" src/main/cpp/blaze_util_darwin.cc
540
541 # clang installed from Xcode has a compatibility wrapper that forwards
542 # invocations of gcc to clang, but vanilla clang doesn't
543 sed -i -e 's;_find_generic(repository_ctx, "gcc", "CC", overriden_tools);_find_generic(repository_ctx, "clang", "CC", overriden_tools);g' tools/cpp/unix_cc_configure.bzl
544
545 sed -i -e 's;"/usr/bin/libtool";_find_generic(repository_ctx, "libtool", "LIBTOOL", overriden_tools);g' tools/cpp/unix_cc_configure.bzl
546 wrappers=( tools/cpp/osx_cc_wrapper.sh.tpl )
547 for wrapper in "''${wrappers[@]}"; do
548 sed -i -e "s,/usr/bin/gcc,${stdenv.cc}/bin/clang,g" $wrapper
549 sed -i -e "s,/usr/bin/install_name_tool,${cctools}/bin/install_name_tool,g" $wrapper
550 sed -i -e "s,/usr/bin/xcrun install_name_tool,${cctools}/bin/install_name_tool,g" $wrapper
551 done
552 '';
553
554 genericPatches = ''
555 # md5sum is part of coreutils
556 sed -i 's|/sbin/md5|md5sum|g' \
557 src/BUILD third_party/ijar/test/testenv.sh tools/objc/libtool.sh
558
559 # replace initial value of pythonShebang variable in BazelPythonSemantics.java
560 substituteInPlace src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java \
561 --replace '"#!/usr/bin/env " + pythonExecutableName' "\"#!${python3}/bin/python\""
562
563 substituteInPlace src/main/java/com/google/devtools/build/lib/starlarkbuildapi/python/PyRuntimeInfoApi.java \
564 --replace '"#!/usr/bin/env python3"' "\"#!${python3}/bin/python\""
565
566 # substituteInPlace is rather slow, so prefilter the files with grep
567 grep -rlZ /bin/ src/main/java/com/google/devtools | while IFS="" read -r -d "" path; do
568 # If you add more replacements here, you must change the grep above!
569 # Only files containing /bin are taken into account.
570 substituteInPlace "$path" \
571 --replace /bin/bash ${bashWithDefaultShellUtils}/bin/bash \
572 --replace "/usr/bin/env bash" ${bashWithDefaultShellUtils}/bin/bash \
573 --replace "/usr/bin/env python" ${python3}/bin/python \
574 --replace /usr/bin/env ${coreutils}/bin/env \
575 --replace /bin/true ${coreutils}/bin/true
576 done
577
578 grep -rlZ /bin/ tools/python | while IFS="" read -r -d "" path; do
579 substituteInPlace "$path" \
580 --replace "/usr/bin/env python2" ${python3.interpreter} \
581 --replace "/usr/bin/env python3" ${python3}/bin/python \
582 --replace /usr/bin/env ${coreutils}/bin/env
583 done
584
585 # bazel test runner include references to /bin/bash
586 substituteInPlace tools/build_rules/test_rules.bzl \
587 --replace /bin/bash ${bashWithDefaultShellUtils}/bin/bash
588
589 for i in $(find tools/cpp/ -type f)
590 do
591 substituteInPlace $i \
592 --replace /bin/bash ${bashWithDefaultShellUtils}/bin/bash
593 done
594
595 # Fixup scripts that generate scripts. Not fixed up by patchShebangs below.
596 substituteInPlace scripts/bootstrap/compile.sh \
597 --replace /bin/bash ${bashWithDefaultShellUtils}/bin/bash
598
599 # add nix environment vars to .bazelrc
600 cat >> .bazelrc <<EOF
601 # Limit the resources Bazel is allowed to use during the build to 1/2 the
602 # available RAM and 3/4 the available CPU cores. This should help avoid
603 # overwhelming the build machine.
604 build --toolchain_resolution_debug=".*"
605 build --local_ram_resources=HOST_RAM*.5
606 build --local_cpu_resources=HOST_CPUS*.75
607
608 build --distdir=${distDir}
609 fetch --distdir=${distDir}
610 build --copt="$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ /" --copt="/g')"
611 build --host_copt="$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ /" --host_copt="/g')"
612 build --linkopt="$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ /" --linkopt="/g')"
613 build --host_linkopt="$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ /" --host_linkopt="/g')"
614 build --linkopt="-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ /" --linkopt="-Wl,/g')"
615 build --host_linkopt="-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ /" --host_linkopt="-Wl,/g')"
616 build --extra_toolchains=@bazel_tools//tools/jdk:nonprebuilt_toolchain_definition
617 build --verbose_failures
618 build --curses=no
619 build --features=-layering_check
620 build --experimental_strict_java_deps=off
621 build --strict_proto_deps=off
622 EOF
623
624 cat >> third_party/grpc/bazel_1.41.0.patch <<EOF
625 diff --git a/third_party/grpc/BUILD b/third_party/grpc/BUILD
626 index 39ee9f97c6..9128d20c85 100644
627 --- a/third_party/grpc/BUILD
628 +++ b/third_party/grpc/BUILD
629 @@ -28,7 +28,6 @@ licenses(["notice"])
630 package(
631 default_visibility = ["//visibility:public"],
632 features = [
633 - "layering_check",
634 "-parse_headers",
635 ],
636 )
637 EOF
638
639 # add the same environment vars to compile.sh
640 sed -e "/\$command \\\\$/a --copt=\"$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ /" --copt=\"/g')\" \\\\" \
641 -e "/\$command \\\\$/a --host_copt=\"$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ /" --host_copt=\"/g')\" \\\\" \
642 -e "/\$command \\\\$/a --linkopt=\"$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ /" --linkopt=\"/g')\" \\\\" \
643 -e "/\$command \\\\$/a --host_linkopt=\"$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ /" --host_linkopt=\"/g')\" \\\\" \
644 -e "/\$command \\\\$/a --linkopt=\"-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ /" --linkopt=\"-Wl,/g')\" \\\\" \
645 -e "/\$command \\\\$/a --host_linkopt=\"-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ /" --host_linkopt=\"-Wl,/g')\" \\\\" \
646 -e "/\$command \\\\$/a --tool_java_runtime_version=local_jdk_11 \\\\" \
647 -e "/\$command \\\\$/a --java_runtime_version=local_jdk_11 \\\\" \
648 -e "/\$command \\\\$/a --verbose_failures \\\\" \
649 -e "/\$command \\\\$/a --curses=no \\\\" \
650 -e "/\$command \\\\$/a --features=-layering_check \\\\" \
651 -e "/\$command \\\\$/a --experimental_strict_java_deps=off \\\\" \
652 -e "/\$command \\\\$/a --strict_proto_deps=off \\\\" \
653 -i scripts/bootstrap/compile.sh
654
655 # This is necessary to avoid:
656 # "error: no visible @interface for 'NSDictionary' declares the selector
657 # 'initWithContentsOfURL:error:'"
658 # This can be removed when the apple_sdk is upgraded beyond 10.13+
659 sed -i '/initWithContentsOfURL:versionPlistUrl/ {
660 N
661 s/error:nil\];/\];/
662 }' tools/osx/xcode_locator.m
663
664 # append the PATH with defaultShellPath in tools/bash/runfiles/runfiles.bash
665 echo "PATH=\$PATH:${defaultShellPath}" >> runfiles.bash.tmp
666 cat tools/bash/runfiles/runfiles.bash >> runfiles.bash.tmp
667 mv runfiles.bash.tmp tools/bash/runfiles/runfiles.bash
668
669 patchShebangs .
670 '';
671 in
672 lib.optionalString stdenv.hostPlatform.isDarwin darwinPatches + genericPatches;
673
674 buildInputs = [
675 buildJdk
676 bashWithDefaultShellUtils
677 ]
678 ++ defaultShellUtils;
679
680 # when a command can’t be found in a bazel build, you might also
681 # need to add it to `defaultShellPath`.
682 nativeBuildInputs = [
683 installShellFiles
684 makeWrapper
685 python3
686 unzip
687 which
688 zip
689 python3.pkgs.absl-py # Needed to build fish completion
690 ]
691 ++ lib.optionals (stdenv.hostPlatform.isDarwin) [
692 cctools
693 sigtool
694 ];
695
696 # Bazel makes extensive use of symlinks in the WORKSPACE.
697 # This causes problems with infinite symlinks if the build output is in the same location as the
698 # Bazel WORKSPACE. This is why before executing the build, the source code is moved into a
699 # subdirectory.
700 # Failing to do this causes "infinite symlink expansion detected"
701 preBuildPhases = [ "preBuildPhase" ];
702 preBuildPhase = ''
703 mkdir bazel_src
704 shopt -s dotglob extglob
705 mv !(bazel_src) bazel_src
706 '';
707 buildPhase = ''
708 runHook preBuild
709
710 # Increasing memory during compilation might be necessary.
711 # export BAZEL_JAVAC_OPTS="-J-Xmx2g -J-Xms200m"
712
713 # If EMBED_LABEL isn't set, it'd be auto-detected from CHANGELOG.md
714 # and `git rev-parse --short HEAD` which would result in
715 # "3.7.0- (@non-git)" due to non-git build and incomplete changelog.
716 # Actual bazel releases use scripts/release/common.sh which is based
717 # on branch/tag information which we don't have with tarball releases.
718 # Note that .bazelversion is always correct and is based on bazel-*
719 # executable name, version checks should work fine
720 export EMBED_LABEL="${version}- (@non-git)"
721 ${bash}/bin/bash ./bazel_src/compile.sh
722 ./bazel_src/scripts/generate_bash_completion.sh \
723 --bazel=./bazel_src/output/bazel \
724 --output=./bazel_src/output/bazel-complete.bash \
725 --prepend=./bazel_src/scripts/bazel-complete-header.bash \
726 --prepend=./bazel_src/scripts/bazel-complete-template.bash
727 ${python3}/bin/python3 ./bazel_src/scripts/generate_fish_completion.py \
728 --bazel=./bazel_src/output/bazel \
729 --output=./bazel_src/output/bazel-complete.fish
730 ''
731 +
732 # disable execlog parser on darwin, since it fails to build
733 # see https://github.com/NixOS/nixpkgs/pull/273774#issuecomment-1865322055
734 lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
735 # need to change directory for bazel to find the workspace
736 cd ./bazel_src
737 # build execlog tooling
738 export HOME=$(mktemp -d)
739 ./output/bazel build src/tools/execlog:parser_deploy.jar
740 cd -
741
742 runHook postBuild
743 '';
744
745 installPhase = ''
746 runHook preInstall
747
748 mkdir -p $out/bin
749
750 # official wrapper scripts that searches for $WORKSPACE_ROOT/tools/bazel
751 # if it can’t find something in tools, it calls $out/bin/bazel-{version}-{os_arch}
752 # The binary _must_ exist with this naming if your project contains a .bazelversion
753 # file.
754 cp ./bazel_src/scripts/packages/bazel.sh $out/bin/bazel
755 wrapProgram $out/bin/bazel $wrapperfile --suffix PATH : ${defaultShellPath}
756 mv ./bazel_src/output/bazel $out/bin/bazel-${version}-${system}-${arch}
757
758 ''
759 +
760 # disable execlog parser on darwin, since it fails to build
761 # see https://github.com/NixOS/nixpkgs/pull/273774#issuecomment-1865322055
762 (lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
763 mkdir $out/share
764 cp ./bazel_src/bazel-bin/src/tools/execlog/parser_deploy.jar $out/share/parser_deploy.jar
765 cat <<EOF > $out/bin/bazel-execlog
766 #!${runtimeShell} -e
767 ${runJdk}/bin/java -jar $out/share/parser_deploy.jar \$@
768 EOF
769 chmod +x $out/bin/bazel-execlog
770 '')
771 + ''
772 # shell completion files
773 installShellCompletion --bash \
774 --name bazel.bash \
775 ./bazel_src/output/bazel-complete.bash
776 installShellCompletion --zsh \
777 --name _bazel \
778 ./bazel_src/scripts/zsh_completion/_bazel
779 installShellCompletion --fish \
780 --name bazel.fish \
781 ./bazel_src/output/bazel-complete.fish
782
783 runHook postInstall
784 '';
785
786 # Install check fails on `aarch64-darwin`
787 # https://github.com/NixOS/nixpkgs/issues/145587
788 doInstallCheck = stdenv.hostPlatform.system != "aarch64-darwin";
789 installCheckPhase = ''
790 runHook preInstallCheck
791
792 export TEST_TMPDIR=$(pwd)
793
794 hello_test () {
795 $out/bin/bazel test \
796 --test_output=errors \
797 examples/cpp:hello-success_test \
798 examples/java-native/src/test/java/com/example/myproject:hello
799 }
800
801 cd ./bazel_src
802
803 # If .bazelversion file is present in dist files and doesn't match `bazel` version
804 # running `bazel` command within bazel_src will fail.
805 # Let's remove .bazelversion within the test, if present it is meant to indicate bazel version
806 # to compile bazel with, not version of bazel to be built and tested.
807 rm -f .bazelversion
808
809 # test whether $WORKSPACE_ROOT/tools/bazel works
810
811 mkdir -p tools
812 cat > tools/bazel <<"EOF"
813 #!${runtimeShell} -e
814 exit 1
815 EOF
816 chmod +x tools/bazel
817
818 # first call should fail if tools/bazel is used
819 ! hello_test
820
821 cat > tools/bazel <<"EOF"
822 #!${runtimeShell} -e
823 exec "$BAZEL_REAL" "$@"
824 EOF
825
826 # second call succeeds because it defers to $out/bin/bazel-{version}-{os_arch}
827 hello_test
828
829 ## Test that the GSON serialisation files are present
830 gson_classes=$(unzip -l $($out/bin/bazel info install_base)/A-server.jar | grep -F -c _GsonTypeAdapter.class)
831 if [ "$gson_classes" -lt 10 ]; then
832 echo "Missing GsonTypeAdapter classes in A-server.jar. Lockfile generation will not work"
833 exit 1
834 fi
835
836 runHook postInstallCheck
837 '';
838
839 # Save paths to hardcoded dependencies so Nix can detect them.
840 # This is needed because the templates get tar’d up into a .jar.
841 postFixup = ''
842 mkdir -p $out/nix-support
843 echo "${defaultShellPath}" >> $out/nix-support/depends
844 # The string literal specifying the path to the bazel-rc file is sometimes
845 # stored non-contiguously in the binary due to gcc optimisations, which leads
846 # Nix to miss the hash when scanning for dependencies
847 echo "${bazelRC}" >> $out/nix-support/depends
848 ''
849 + lib.optionalString stdenv.hostPlatform.isDarwin ''
850 echo "${cctools}" >> $out/nix-support/depends
851 '';
852
853 dontStrip = true;
854 dontPatchELF = true;
855}