bazel_7: split tests for clarity

+160 -147
+12 -147
pkgs/development/tools/build-managers/bazel/bazel_7/default.nix
··· 1 1 { stdenv 2 + # nix tooling and utilities 2 3 , callPackage 3 4 , lib 4 5 , fetchurl 5 6 , fetchpatch 6 7 , fetchFromGitHub 7 - , installShellFiles 8 8 , runCommand 9 9 , runCommandCC 10 10 , makeWrapper 11 11 , recurseIntoAttrs 12 12 , newScope 13 + , writeTextFile 14 + , autoPatchelfHook 15 + , substituteAll 13 16 # this package (through the fixpoint glass) 14 17 , bazel_self 18 + # native build inputs 19 + , runtimeShell 15 20 , lr 16 21 , xe 17 22 , zip ··· 28 33 , findutils 29 34 , diffutils 30 35 , gnupatch 36 + , file 37 + , installShellFiles 31 38 # updater 32 39 , python3 33 40 , writeScript 34 41 # Apple dependencies 35 42 , cctools 36 43 , libcxx 44 + , sigtool 37 45 , CoreFoundation 38 46 , CoreServices 39 47 , Foundation ··· 41 49 # Allow to independently override the jdks used to build and run respectively 42 50 , buildJdk 43 51 , runJdk 44 - , runtimeShell 45 52 # Downstream packages for tests 46 53 , bazel-watcher 47 54 # Always assume all markers valid (this is needed because we remove markers; they are non-deterministic). 48 55 # Also, don't clean up environment variables (so that NIX_ environment variables are passed to compilers). 49 56 , enableNixHacks ? false 50 - , gcc-unwrapped 51 - , autoPatchelfHook 52 - , file 53 - , substituteAll 54 - , writeTextFile 55 - , writeText 56 - , darwin 57 - , jdk11_headless 58 - , jdk17_headless 59 - , openjdk8 60 - , ripgrep 61 - , sigtool 62 57 }: 63 58 64 59 let ··· 259 254 # nix-build . -A bazel_7.tests 260 255 # 261 256 # in the nixpkgs checkout root to exercise them locally. 262 - passthru.tests = 263 - let 264 - runLocal = name: attrs: script: 265 - let 266 - attrs' = removeAttrs attrs [ "buildInputs" ]; 267 - buildInputs = attrs.buildInputs or [ ]; 268 - in 269 - runCommandCC name 270 - ({ 271 - inherit buildInputs; 272 - preferLocalBuild = true; 273 - meta.platforms = platforms; 274 - } // attrs') 275 - script; 276 - 277 - # bazel wants to extract itself into $install_dir/install every time it runs, 278 - # so let’s do that only once. 279 - extracted = bazelPkg: 280 - let 281 - install_dir = 282 - # `install_base` field printed by `bazel info`, minus the hash. 283 - # yes, this path is kinda magic. Sorry. 284 - "$HOME/.cache/bazel/_bazel_nixbld"; 285 - in 286 - runLocal "bazel-extracted-homedir" { passthru.install_dir = install_dir; } '' 287 - export HOME=$(mktemp -d) 288 - touch WORKSPACE # yeah, everything sucks 289 - install_base="$(${bazelPkg}/bin/bazel info install_base)" 290 - # assert it’s actually below install_dir 291 - [[ "$install_base" =~ ${install_dir} ]] \ 292 - || (echo "oh no! $install_base but we are \ 293 - trying to copy ${install_dir} to $out instead!"; exit 1) 294 - cp -R ${install_dir} $out 295 - ''; 296 - 297 - bazelTest = { name, bazelScript, workspaceDir, bazelPkg, buildInputs ? [ ] }: 298 - let 299 - be = extracted bazelPkg; 300 - in 301 - runLocal name 302 - { 303 - inherit buildInputs; 304 - # Necessary for the tests to pass on Darwin with sandbox enabled. 305 - __darwinAllowLocalNetworking = true; 306 - } 307 - '' 308 - # Bazel needs a real home for self-extraction and internal cache 309 - export HOME=$(mktemp -d) 310 - 311 - ${# Concurrent bazel invocations have the same workspace path. 312 - # On darwin, for some reason, it means they access and corrupt the same execroot. 313 - # Having a different workspace path ensures we use different execroots. 314 - # A different user seems to be enough for a different bazel cache root. 315 - lib.optionalString isDarwin '' 316 - export USER=$(basename $HOME) 317 - # cd $(mktemp --tmpdir=. -d) 318 - '' 319 - } 320 - ${# Speed-up tests by caching bazel extraction. 321 - # Except on Darwin, because nobody knows how Darwin works. 322 - lib.optionalString (!isDarwin) '' 323 - mkdir -p ${be.install_dir} 324 - cp -R ${be}/install ${be.install_dir} 325 - 326 - # https://stackoverflow.com/questions/47775668/bazel-how-to-skip-corrupt-installation-on-centos6 327 - # Bazel checks whether the mtime of the install dir files 328 - # is >9 years in the future, otherwise it extracts itself again. 329 - # see PosixFileMTime::IsUntampered in src/main/cpp/util 330 - # What the hell bazel. 331 - ${lr}/bin/lr -0 -U ${be.install_dir} | ${xe}/bin/xe -N0 -0 touch --date="9 years 6 months" {} 332 - '' 333 - } 334 - ${# Note https://github.com/bazelbuild/bazel/issues/5763#issuecomment-456374609 335 - # about why to create a subdir for the workspace. 336 - '' cp -r ${workspaceDir} wd && chmod u+w wd && cd wd '' 337 - } 338 - ${# run the actual test snippet 339 - bazelScript 340 - } 341 - ${# Try to keep darwin clean of our garbage 342 - lib.optionalString isDarwin '' 343 - rm -rf $HOME || true 344 - '' 345 - } 346 - 347 - touch $out 348 - ''; 349 - 350 - bazel-examples = fetchFromGitHub { 351 - owner = "bazelbuild"; 352 - repo = "examples"; 353 - rev = "93564e1f1e7a3c39d6a94acee12b8d7b74de3491"; 354 - hash = "sha256-DaPKp7Sn5uvfZRjdDx6grot3g3B7trqCyL0TRIdwg98="; 355 - }; 356 - 357 - callBazelTest = newScope { 358 - inherit runLocal bazelTest bazel-examples distDir; 359 - inherit Foundation; 360 - extraBazelArgs = '' 361 - --repository_cache=${repoCache} \ 362 - --repo_env=JAVA_HOME=${runJdk}${if isDarwin then "/zulu-17.jdk/Contents/Home" else "/lib/openjdk"} \ 363 - ''; 364 - bazel = bazel_self; 365 - }; 366 - 367 - bazelWithNixHacks = bazel_self.override { enableNixHacks = true; }; 368 - 369 - in 370 - (lib.optionalAttrs (!stdenv.hostPlatform.isDarwin) { 371 - # `extracted` doesn’t work on darwin 372 - shebang = callBazelTest ../shebang-test.nix { inherit extracted; }; 373 - }) // { 374 - bashTools = callBazelTest ../bash-tools-test.nix { }; 375 - cpp = callBazelTest ../cpp-test.nix { }; 376 - java = callBazelTest ../java-test.nix { }; 377 - # TODO: protobuf tests just fail for now. 378 - #protobuf = callBazelTest ../protobuf-test.nix { }; 379 - pythonBinPath = callBazelTest ../python-bin-path-test.nix { }; 380 - 381 - bashToolsWithNixHacks = callBazelTest ../bash-tools-test.nix { bazel = bazelWithNixHacks; }; 382 - 383 - cppWithNixHacks = callBazelTest ../cpp-test.nix { bazel = bazelWithNixHacks; }; 384 - javaWithNixHacks = callBazelTest ../java-test.nix { bazel = bazelWithNixHacks; }; 385 - #protobufWithNixHacks = callBazelTest ../protobuf-test.nix { bazel = bazelWithNixHacks; }; 386 - pythonBinPathWithNixHacks = callBazelTest ../python-bin-path-test.nix { bazel = bazelWithNixHacks; }; 387 - 388 - # downstream packages using buildBazelPackage 389 - # fixed-output hashes of the fetch phase need to be spot-checked manually 390 - # TODO 391 - #downstream = recurseIntoAttrs ({ 392 - # inherit bazel-watcher; 393 - #}); 394 - }; 257 + passthru.tests = callPackage ./tests.nix { 258 + inherit Foundation bazel_self distDir repoCache runJdk; 259 + }; 395 260 396 261 src_for_updater = stdenv.mkDerivation rec { 397 262 name = "updater-sources";
+148
pkgs/development/tools/build-managers/bazel/bazel_7/tests.nix
··· 1 + { lib 2 + # tooling 3 + , fetchFromGitHub 4 + , newScope 5 + , runCommandCC 6 + , stdenv 7 + # inputs 8 + , Foundation 9 + , bazel_self 10 + , distDir 11 + , lr 12 + , repoCache 13 + , runJdk 14 + , xe 15 + }: 16 + let 17 + inherit (stdenv.hostPlatform) isDarwin; 18 + 19 + runLocal = name: attrs: script: 20 + let 21 + attrs' = removeAttrs attrs [ "buildInputs" ]; 22 + buildInputs = attrs.buildInputs or [ ]; 23 + in 24 + runCommandCC name 25 + ({ 26 + inherit buildInputs; 27 + preferLocalBuild = true; 28 + meta.platforms = bazel_self.meta.platforms; 29 + } // attrs') 30 + script; 31 + 32 + # bazel wants to extract itself into $install_dir/install every time it runs, 33 + # so let’s do that only once. 34 + extracted = bazelPkg: 35 + let 36 + install_dir = 37 + # `install_base` field printed by `bazel info`, minus the hash. 38 + # yes, this path is kinda magic. Sorry. 39 + "$HOME/.cache/bazel/_bazel_nixbld"; 40 + in 41 + runLocal "bazel-extracted-homedir" { passthru.install_dir = install_dir; } '' 42 + export HOME=$(mktemp -d) 43 + touch WORKSPACE # yeah, everything sucks 44 + install_base="$(${bazelPkg}/bin/bazel info install_base)" 45 + # assert it’s actually below install_dir 46 + [[ "$install_base" =~ ${install_dir} ]] \ 47 + || (echo "oh no! $install_base but we are \ 48 + trying to copy ${install_dir} to $out instead!"; exit 1) 49 + cp -R ${install_dir} $out 50 + ''; 51 + 52 + bazelTest = { name, bazelScript, workspaceDir, bazelPkg, buildInputs ? [ ] }: 53 + runLocal name 54 + { 55 + inherit buildInputs; 56 + # Necessary for the tests to pass on Darwin with sandbox enabled. 57 + __darwinAllowLocalNetworking = true; 58 + } 59 + '' 60 + # Bazel needs a real home for self-extraction and internal cache 61 + export HOME=$(mktemp -d) 62 + 63 + ${# Concurrent bazel invocations have the same workspace path. 64 + # On darwin, for some reason, it means they access and corrupt the same execroot. 65 + # Having a different workspace path ensures we use different execroots. 66 + # A different user seems to be enough for a different bazel cache root. 67 + lib.optionalString isDarwin '' 68 + export USER=$(basename $HOME) 69 + # cd $(mktemp --tmpdir=. -d) 70 + '' 71 + } 72 + ${# Speed-up tests by caching bazel extraction. 73 + # Except on Darwin, because nobody knows how Darwin works. 74 + let bazelExtracted = extracted bazelPkg; 75 + in lib.optionalString (!isDarwin) '' 76 + mkdir -p ${bazelExtracted.install_dir} 77 + cp -R ${bazelExtracted}/install ${bazelExtracted.install_dir} 78 + 79 + # https://stackoverflow.com/questions/47775668/bazel-how-to-skip-corrupt-installation-on-centos6 80 + # Bazel checks whether the mtime of the install dir files 81 + # is >9 years in the future, otherwise it extracts itself again. 82 + # see PosixFileMTime::IsUntampered in src/main/cpp/util 83 + # What the hell bazel. 84 + ${lr}/bin/lr -0 -U ${bazelExtracted.install_dir} | ${xe}/bin/xe -N0 -0 touch --date="9 years 6 months" {} 85 + '' 86 + } 87 + ${# Note https://github.com/bazelbuild/bazel/issues/5763#issuecomment-456374609 88 + # about why to create a subdir for the workspace. 89 + '' cp -r ${workspaceDir} wd && chmod u+w wd && cd wd '' 90 + } 91 + ${# run the actual test snippet 92 + bazelScript 93 + } 94 + ${# Try to keep darwin clean of our garbage 95 + lib.optionalString isDarwin '' 96 + rm -rf $HOME || true 97 + '' 98 + } 99 + 100 + touch $out 101 + ''; 102 + 103 + bazel-examples = fetchFromGitHub { 104 + owner = "bazelbuild"; 105 + repo = "examples"; 106 + rev = "93564e1f1e7a3c39d6a94acee12b8d7b74de3491"; 107 + hash = "sha256-DaPKp7Sn5uvfZRjdDx6grot3g3B7trqCyL0TRIdwg98="; 108 + }; 109 + 110 + callBazelTest = newScope { 111 + inherit runLocal bazelTest bazel-examples distDir; 112 + inherit Foundation; 113 + extraBazelArgs = '' 114 + --repository_cache=${repoCache} \ 115 + --repo_env=JAVA_HOME=${runJdk}${if isDarwin then "/zulu-17.jdk/Contents/Home" else "/lib/openjdk"} \ 116 + ''; 117 + bazel = bazel_self; 118 + }; 119 + 120 + bazelWithNixHacks = bazel_self.override { enableNixHacks = true; }; 121 + 122 + in 123 + (lib.optionalAttrs (!stdenv.hostPlatform.isDarwin) { 124 + # `extracted` doesn’t work on darwin 125 + shebang = callBazelTest ../shebang-test.nix { inherit extracted; }; 126 + }) // { 127 + bashTools = callBazelTest ../bash-tools-test.nix { }; 128 + cpp = callBazelTest ../cpp-test.nix { }; 129 + java = callBazelTest ../java-test.nix { }; 130 + # TODO: protobuf tests just fail for now. 131 + #protobuf = callBazelTest ../protobuf-test.nix { }; 132 + pythonBinPath = callBazelTest ../python-bin-path-test.nix { }; 133 + 134 + bashToolsWithNixHacks = callBazelTest ../bash-tools-test.nix { bazel = bazelWithNixHacks; }; 135 + 136 + cppWithNixHacks = callBazelTest ../cpp-test.nix { bazel = bazelWithNixHacks; }; 137 + javaWithNixHacks = callBazelTest ../java-test.nix { bazel = bazelWithNixHacks; }; 138 + #protobufWithNixHacks = callBazelTest ../protobuf-test.nix { bazel = bazelWithNixHacks; }; 139 + pythonBinPathWithNixHacks = callBazelTest ../python-bin-path-test.nix { bazel = bazelWithNixHacks; }; 140 + 141 + # downstream packages using buildBazelPackage 142 + # fixed-output hashes of the fetch phase need to be spot-checked manually 143 + # TODO 144 + #downstream = recurseIntoAttrs ({ 145 + # inherit bazel-watcher; 146 + #}); 147 + } 148 +