Get bazel building on all major platforms, with tests

+641 -144
+24 -31
pkgs/development/tools/build-managers/bazel/bazel_7/bazel-repository-cache.nix
··· 1 { lib 2 - , nix 3 , runCommand 4 , fetchurl 5 # The path to the right MODULE.bazel.lock ··· 29 # like foldl', force evaluation of intermediate results 30 builtins.seq acc' children; 31 32 - extract_source = acc: value: 33 # We take any "attributes" object that has a "sha256" field. Every value 34 # under "attributes" is assumed to be an object, and all the "attributes" 35 # with a "sha256" field are assumed to have either a "urls" or "url" field. ··· 47 # }; 48 # } 49 let 50 - # remove the "--" prefix, abusing undocumented negative substring length 51 - sanitize = builtins.substring 2 (-1); 52 attrs = value.attributes; 53 entry = hash: urls: { 54 ${hash} = fetchurl { ··· 58 passthru.sha256 = hash; 59 }; 60 }; 61 - insert = acc: hash: urls: acc // entry (sanitize hash) (map sanitize urls); 62 in 63 if builtins.isAttrs value && value ? attributes 64 && (attrs ? sha256 || attrs ? integrity) 65 - then 66 - #builtins.trace attrs 67 - ( 68 - insert 69 - acc 70 - (attrs.integrity or attrs.sha256) 71 - (attrs.urls or [ attrs.url ]) 72 - ) 73 - else if builtins.isAttrs value && value ? remote_patches 74 - && builtins.isAttrs value.remote_patches 75 - then 76 - #builtins.trace value.remote_patches 77 - ( 78 - lib.foldlAttrs 79 - (acc: url: hash: insert acc hash [ url ]) 80 - acc 81 - value.remote_patches 82 - ) 83 - else acc; 84 85 - inputs = foldlJSON extract_source { } modules; 86 87 requiredHashes = builtins.fromJSON (builtins.readFile requiredDeps); 88 requiredAttrs = lib.genAttrs requiredHashes throw; ··· 94 95 command = '' 96 mkdir -p $out/content_addressable/sha256 97 - cd $out/content_addressable/sha256 98 '' + lib.concatMapStrings 99 # TODO: Do not re-hash. Use nix-hash to convert hashes 100 (drv: '' 101 filename=$(basename "${lib.head drv.urls}") 102 - echo Caching $filename 103 - hash=$(${nix}/bin/nix-hash --type sha256 --to-base16 ${drv.sha256}) 104 - mkdir -p $hash 105 - ln -sfn ${drv} $hash/file 106 - ln -sfn ${drv} $filename 107 '') 108 (builtins.attrValues requiredInputs ++ extraInputs) 109 ;
··· 1 { lib 2 + , rnix-hashes 3 , runCommand 4 , fetchurl 5 # The path to the right MODULE.bazel.lock ··· 29 # like foldl', force evaluation of intermediate results 30 builtins.seq acc' children; 31 32 + extract_source = f: acc: value: 33 # We take any "attributes" object that has a "sha256" field. Every value 34 # under "attributes" is assumed to be an object, and all the "attributes" 35 # with a "sha256" field are assumed to have either a "urls" or "url" field. ··· 47 # }; 48 # } 49 let 50 attrs = value.attributes; 51 entry = hash: urls: { 52 ${hash} = fetchurl { ··· 56 passthru.sha256 = hash; 57 }; 58 }; 59 + insert = acc: hash: urls: 60 + acc // entry (sanitize hash) (map sanitize urls); 61 + accWithRemotePatches = lib.foldlAttrs 62 + (acc: url: hash: insert acc hash [ url ]) 63 + acc 64 + (attrs.remote_patches or { }); 65 + accWithNewSource = insert 66 + accWithRemotePatches 67 + (attrs.integrity or attrs.sha256) 68 + (attrs.urls or [ attrs.url ]); 69 in 70 if builtins.isAttrs value && value ? attributes 71 && (attrs ? sha256 || attrs ? integrity) 72 + && f attrs.name 73 + then accWithNewSource else acc; 74 + 75 + # remove the "--" prefix, abusing undocumented negative substring length 76 + sanitize = builtins.substring 2 (-1); 77 78 + inputs = foldlJSON (extract_source (n: builtins.trace (sanitize n) true)) { } modules; 79 80 requiredHashes = builtins.fromJSON (builtins.readFile requiredDeps); 81 requiredAttrs = lib.genAttrs requiredHashes throw; ··· 87 88 command = '' 89 mkdir -p $out/content_addressable/sha256 90 + cd $out 91 '' + lib.concatMapStrings 92 # TODO: Do not re-hash. Use nix-hash to convert hashes 93 (drv: '' 94 filename=$(basename "${lib.head drv.urls}") 95 + hash=$(${rnix-hashes}/bin/rnix-hashes --encoding BASE16 ${drv.sha256} | cut -f 2) 96 + mkdir -p content_addressable/sha256/$hash 97 + ln -sfn ${drv} content_addressable/sha256/$hash/file 98 + # Expect file name conflicts 99 + ln -sn ${drv} $filename || true 100 '') 101 (builtins.attrValues requiredInputs ++ extraInputs) 102 ;
+104 -94
pkgs/development/tools/build-managers/bazel/bazel_7/default.nix
··· 54 # Always assume all markers valid (this is needed because we remove markers; they are non-deterministic). 55 # Also, don't clean up environment variables (so that NIX_ environment variables are passed to compilers). 56 , enableNixHacks ? false 57 - }: 58 59 let 60 version = "7.0.0-pre.20230917.3"; ··· 136 # on aarch64 Darwin, `uname -m` returns "arm64" 137 arch = with stdenv.hostPlatform; if isDarwin && isAarch64 then "arm64" else parsed.cpu.name; 138 139 bazelRC = writeTextFile { 140 name = "bazel-rc"; 141 text = '' 142 startup --server_javabase=${buildJdk} 143 144 - build --extra_toolchains=@local_jdk//:all 145 build --tool_java_runtime_version=local_jdk 146 build --java_runtime_version=local_jdk 147 - build --repo_env=JAVA_HOME=${buildJdk}${if isDarwin then "/zulu-11.jdk/Contents/Home" else "/lib/openjdk"} 148 149 # load default location for the system wide configuration 150 try-import /etc/bazel.bazelrc 151 ''; 152 }; 153 154 - bazelNixFlagsScript = writeScript "bazel-nix-flags" '' 155 - cat << EOF 156 - common --announce_rc 157 - build --toolchain_resolution_debug=".*" 158 - build --local_ram_resources=HOST_RAM*.5 159 - build --local_cpu_resources=HOST_CPUS*.75 160 - build --copt=$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ / --copt=/g') 161 - build --host_copt=$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ / --host_copt=/g') 162 - build --linkopt=$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ / --linkopt=/g') 163 - build --host_linkopt=$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ / --host_linkopt=/g') 164 - build --linkopt=-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ / --linkopt=-Wl,/g') 165 - build --host_linkopt=-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ / --host_linkopt=-Wl,/g') 166 - build --extra_toolchains=@bazel_tools//tools/jdk:nonprebuilt_toolchain_definition 167 - build --verbose_failures 168 - build --curses=no 169 - build --features=-layering_check 170 - build --experimental_strict_java_deps=off 171 - build --strict_proto_deps=off 172 - build --extra_toolchains=@bazel_tools//tools/jdk:nonprebuilt_toolchain_java11_definition 173 - build --extra_toolchains=@local_jdk//:all 174 - build --tool_java_runtime_version=local_jdk_11 175 - build --java_runtime_version=local_jdk_11 176 - build --repo_env=JAVA_HOME=${buildJdk}${if isDarwin then "/zulu-11.jdk/Contents/Home" else "/lib/openjdk"} 177 - EOF 178 - ''; 179 in 180 stdenv.mkDerivation rec { 181 pname = "bazel"; 182 - inherit version; 183 184 - meta = with lib; { 185 - homepage = "https://github.com/bazelbuild/bazel/"; 186 - description = "Build tool that builds code quickly and reliably"; 187 - sourceProvenance = with sourceTypes; [ 188 - fromSource 189 - binaryBytecode # source bundles dependencies as jars 190 - ]; 191 - license = licenses.asl20; 192 - maintainers = lib.teams.bazel.members; 193 - inherit platforms; 194 - }; 195 - 196 - inherit src; 197 - inherit sourceRoot; 198 patches = [ 199 # TODO: Make GSON work, 200 # In particular, our bazel build cannot emit MODULE.bazel.lock 201 # it only produces an empty json object `{ }`. 202 ./serialize_nulls.patch 203 204 # Bazel integrates with apple IOKit to inhibit and track system sleep. 205 # Inside the darwin sandbox, these API calls are blocked, and bazel 206 # crashes. It seems possible to allow these APIs inside the sandbox, but it ··· 239 # This patch removes using the -fobjc-arc compiler option and makes the code 240 # compile without automatic reference counting. Caveat: this leaks memory, but 241 # we accept this fact because xcode_locator is only a short-lived process used during the build. 242 - (substituteAll { 243 - src = ./no-arc.patch; 244 - multiBinPatch = if stdenv.hostPlatform.system == "aarch64-darwin" then "arm64" else "x86_64"; 245 - }) 246 247 # --experimental_strict_action_env (which may one day become the default 248 # see bazelbuild/bazel#2574) hardcodes the default ··· 269 ] 270 # See enableNixHacks argument above. 271 ++ lib.optional enableNixHacks ./nix-hacks.patch; 272 - 273 - 274 - # Bazel starts a local server and needs to bind a local address. 275 - __darwinAllowLocalNetworking = true; 276 277 postPatch = 278 let 279 darwinPatches = '' 280 bazelLinkFlags () { 281 eval set -- "$NIX_LDFLAGS" 282 local flag ··· 284 printf ' -Wl,%s' "$flag" 285 done 286 } 287 - 288 - # Disable Bazel's Xcode toolchain detection which would configure compilers 289 - # and linkers from Xcode instead of from PATH 290 - export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 291 292 # Explicitly configure gcov since we don't have it on Darwin, so autodetection fails 293 export GCOV=${coreutils}/bin/false ··· 300 # https://github.com/NixOS/nixpkgs/pull/41589 301 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -isystem ${lib.getDev libcxx}/include/c++/v1" 302 303 # don't use system installed Xcode to run clang, use Nix clang instead 304 sed -i -E \ 305 -e "s;/usr/bin/xcrun (--sdk macosx )?clang;${stdenv.cc}/bin/clang $NIX_CFLAGS_COMPILE $(bazelLinkFlags) -framework CoreFoundation;g" \ ··· 307 scripts/bootstrap/compile.sh \ 308 tools/osx/BUILD 309 310 - substituteInPlace scripts/bootstrap/compile.sh --replace ' -mmacosx-version-min=10.9' "" 311 - 312 # nixpkgs's libSystem cannot use pthread headers directly, must import GCD headers instead 313 sed -i -e "/#include <pthread\/spawn.h>/i #include <dispatch/dispatch.h>" src/main/cpp/blaze_util_darwin.cc 314 ··· 316 # invocations of gcc to clang, but vanilla clang doesn't 317 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 318 319 sed -i -e 's;"/usr/bin/libtool";_find_generic(repository_ctx, "libtool", "LIBTOOL", overriden_tools);g' tools/cpp/unix_cc_configure.bzl 320 wrappers=( tools/cpp/osx_cc_wrapper.sh.tpl ) 321 for wrapper in "''${wrappers[@]}"; do 322 - sed -i -e "s,/usr/bin/gcc,${stdenv.cc}/bin/clang,g" $wrapper 323 - sed -i -e "s,/usr/bin/install_name_tool,${cctools}/bin/install_name_tool,g" $wrapper 324 - sed -i -e "s,/usr/bin/xcrun install_name_tool,${cctools}/bin/install_name_tool,g" $wrapper 325 done 326 ''; 327 328 genericPatches = '' 329 - function sedVerbose() { 330 - local path=$1; shift; 331 - sed -i".bak-nix" "$path" "$@" 332 - diff -U0 "$path.bak-nix" "$path" | sed "s/^/ /" || true 333 - rm -f "$path.bak-nix" 334 - } 335 - 336 # unzip builtins_bzl.zip so the contents get patched 337 builtins_bzl=src/main/java/com/google/devtools/build/lib/bazel/rules/builtins_bzl 338 unzip ''${builtins_bzl}.zip -d ''${builtins_bzl}_zip >/dev/null ··· 348 grep -rlZ /bin/ \ 349 src/main/java/com/google/devtools \ 350 src/main/starlark/builtins_bzl/common/python \ 351 - tools/python \ 352 - tools/cpp \ 353 - tools/build_rules \ 354 - tools/osx/BUILD \ 355 | while IFS="" read -r -d "" path; do 356 # If you add more replacements here, you must change the grep above! 357 # Only files containing /bin are taken into account. ··· 377 # Passing EXTRA_BAZEL_ARGS is tricky due to quoting. 378 379 sedVerbose compile.sh \ 380 - -e "/bazel_build /a\ --copt=\"$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ /" --copt=\"/g')\" \\\\" \ 381 - -e "/bazel_build /a\ --host_copt=\"$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ /" --host_copt=\"/g')\" \\\\" \ 382 - -e "/bazel_build /a\ --linkopt=\"$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ /" --linkopt=\"/g')\" \\\\" \ 383 - -e "/bazel_build /a\ --host_linkopt=\"$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ /" --host_linkopt=\"/g')\" \\\\" \ 384 - -e "/bazel_build /a\ --linkopt=\"-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ /" --linkopt=\"-Wl,/g')\" \\\\" \ 385 - -e "/bazel_build /a\ --host_linkopt=\"-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ /" --host_linkopt=\"-Wl,/g')\" \\\\" \ 386 -e "/bazel_build /a\ --verbose_failures \\\\" \ 387 -e "/bazel_build /a\ --curses=no \\\\" \ 388 -e "/bazel_build /a\ --features=-layering_check \\\\" \ 389 -e "/bazel_build /a\ --experimental_strict_java_deps=off \\\\" \ 390 -e "/bazel_build /a\ --strict_proto_deps=off \\\\" \ 391 -e "/bazel_build /a\ --tool_java_runtime_version=local_jdk \\\\" \ 392 -e "/bazel_build /a\ --java_runtime_version=local_jdk \\\\" \ 393 - -e "/bazel_build /a\ --repo_env=JAVA_HOME=${buildJdk}/${if isDarwin then "/zulu-11.jdk/Contents/Home" else "/lib/openjdk"} \\\\" \ 394 - -e "/bazel_build /a\ --extra_toolchains=@local_jdk//:all \\\\" \ 395 - -e "/bazel_build /a\ --toolchain_resolution_debug=@bazel_tools//tools/jdk:runtime_toolchain_type \\\\" \ 396 - -e "/bazel_build /a\ --sandbox_debug --verbose_failures \\\\" \ 397 398 # Also build parser_deploy.jar with bootstrap bazel 399 # TODO: Turn into a proper patch ··· 401 -e 's!bazel_build !bazel_build src/tools/execlog:parser_deploy.jar !' \ 402 -e 's!clear_log!cp $(get_bazel_bin_path)/src/tools/execlog/parser_deploy.jar output\nclear_log!' 403 404 - 405 - # This is necessary to avoid: 406 - # "error: no visible @interface for 'NSDictionary' declares the selector 407 - # 'initWithContentsOfURL:error:'" 408 - # This can be removed when the apple_sdk is upgraded beyond 10.13+ 409 - sedVerbose tools/osx/xcode_locator.m \ 410 - -e '/initWithContentsOfURL:versionPlistUrl/ { 411 - N 412 - s/error:nil\];/\];/ 413 - }' 414 - 415 # append the PATH with defaultShellPath in tools/bash/runfiles/runfiles.bash 416 echo "PATH=\$PATH:${defaultShellPath}" >> runfiles.bash.tmp 417 cat tools/bash/runfiles/runfiles.bash >> runfiles.bash.tmp ··· 427 patchShebangs . >/dev/null 428 ''; 429 in 430 - lib.optionalString isDarwin darwinPatches + genericPatches; 431 432 buildInputs = [ buildJdk ] ++ defaultShellUtils; 433 ··· 444 ] ++ lib.optionals (stdenv.isDarwin) [ 445 cctools 446 libcxx 447 CoreFoundation 448 CoreServices 449 - Foundation 450 ]; 451 452 # Bazel makes extensive use of symlinks in the WORKSPACE.
··· 54 # Always assume all markers valid (this is needed because we remove markers; they are non-deterministic). 55 # Also, don't clean up environment variables (so that NIX_ environment variables are passed to compilers). 56 , enableNixHacks ? false 57 + }@args: 58 59 let 60 version = "7.0.0-pre.20230917.3"; ··· 136 # on aarch64 Darwin, `uname -m` returns "arm64" 137 arch = with stdenv.hostPlatform; if isDarwin && isAarch64 then "arm64" else parsed.cpu.name; 138 139 + #build --extra_toolchains=@local_jdk//:all 140 + #build --tool_java_runtime_version=local_jdk 141 + #build --java_runtime_version=local_jdk 142 + #build --repo_env=JAVA_HOME=${buildJdk}${if isDarwin then "/zulu-11.jdk/Contents/Home" else "/lib/openjdk"} 143 + 144 bazelRC = writeTextFile { 145 name = "bazel-rc"; 146 text = '' 147 startup --server_javabase=${buildJdk} 148 149 + # Register nix-specific nonprebuilt java toolchains 150 + build --extra_toolchains=@bazel_tools//tools/jdk:all 151 + # and set bazel to use them by default 152 build --tool_java_runtime_version=local_jdk 153 build --java_runtime_version=local_jdk 154 155 # load default location for the system wide configuration 156 try-import /etc/bazel.bazelrc 157 ''; 158 }; 159 160 in 161 stdenv.mkDerivation rec { 162 pname = "bazel"; 163 + inherit version src; 164 + inherit sourceRoot; 165 166 patches = [ 167 # TODO: Make GSON work, 168 # In particular, our bazel build cannot emit MODULE.bazel.lock 169 # it only produces an empty json object `{ }`. 170 ./serialize_nulls.patch 171 172 + # --extra_toolchains defined later should come before the ones defined earlier. 173 + # As-is, this patch also inverts the order of extra_toolchains lists, but it's just a hack 174 + ./extra_toolchains_precedence.patch 175 + 176 + #./toolchain_better_debug.patch 177 + ./toolchain_group_debug.patch 178 + 179 + # Remote java toolchains do not work on NixOS because they download binaries, 180 + # so we need to use the @local_jdk//:jdk 181 + # It could in theory be done by registering @local_jdk//:all toolchains, 182 + # but these java toolchains still bundle binaries for ijar and stuff. So we 183 + # need a nonprebult java toolchain (where ijar and stuff is built from 184 + # sources). 185 + # There is no such java toolchain, so we introduce one here. 186 + # By providing no version information, the toolchain will set itself to the 187 + # version of $JAVA_HOME/bin/java, just like the local_jdk does. 188 + # To ensure this toolchain gets used, we can set 189 + # --{,tool_}java_runtime_version=local_jdk and rely on the fact no java 190 + # toolchain registered by default uses the local_jdk, making the selection 191 + # unambiguous. 192 + # This toolchain has the advantage that it can use any ambiant java jdk, 193 + # not only a given, fixed version. It allows bazel to work correctly in any 194 + # environment where JAVA_HOME is set to the right java version, like inside 195 + # nix derivations. 196 + # However, this patch breaks bazel hermeticity, by picking the ambiant java 197 + # version instead of the more hermetic remote_jdk prebuilt binaries that 198 + # rules_java provide by default. It also requires the user to have a 199 + # JAVA_HOME set to the exact version required by the project. 200 + # With more code, we could define java toolchains for all the java versions 201 + # supported by the jdk as in rules_java's 202 + # toolchains/local_java_repository.bzl, but this is not implemented here. 203 + # To recover vanilla behavior, non NixOS users can set 204 + # --{,tool_}java_runtime_version=remote_jdk, effectively reverting the 205 + # effect of this patch and the fake system bazelrc. 206 + ./java_toolchain.patch 207 + 208 # Bazel integrates with apple IOKit to inhibit and track system sleep. 209 # Inside the darwin sandbox, these API calls are blocked, and bazel 210 # crashes. It seems possible to allow these APIs inside the sandbox, but it ··· 243 # This patch removes using the -fobjc-arc compiler option and makes the code 244 # compile without automatic reference counting. Caveat: this leaks memory, but 245 # we accept this fact because xcode_locator is only a short-lived process used during the build. 246 + ./no-arc.patch 247 248 # --experimental_strict_action_env (which may one day become the default 249 # see bazelbuild/bazel#2574) hardcodes the default ··· 270 ] 271 # See enableNixHacks argument above. 272 ++ lib.optional enableNixHacks ./nix-hacks.patch; 273 274 postPatch = 275 let 276 darwinPatches = '' 277 + 278 bazelLinkFlags () { 279 eval set -- "$NIX_LDFLAGS" 280 local flag ··· 282 printf ' -Wl,%s' "$flag" 283 done 284 } 285 286 # Explicitly configure gcov since we don't have it on Darwin, so autodetection fails 287 export GCOV=${coreutils}/bin/false ··· 294 # https://github.com/NixOS/nixpkgs/pull/41589 295 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -isystem ${lib.getDev libcxx}/include/c++/v1" 296 297 + # This variable is used by bazel to propagate env vars for homebrew, 298 + # which is exactly what we need too. 299 + export HOMEBREW_RUBY_PATH="foo" 300 + 301 # don't use system installed Xcode to run clang, use Nix clang instead 302 sed -i -E \ 303 -e "s;/usr/bin/xcrun (--sdk macosx )?clang;${stdenv.cc}/bin/clang $NIX_CFLAGS_COMPILE $(bazelLinkFlags) -framework CoreFoundation;g" \ ··· 305 scripts/bootstrap/compile.sh \ 306 tools/osx/BUILD 307 308 # nixpkgs's libSystem cannot use pthread headers directly, must import GCD headers instead 309 sed -i -e "/#include <pthread\/spawn.h>/i #include <dispatch/dispatch.h>" src/main/cpp/blaze_util_darwin.cc 310 ··· 312 # invocations of gcc to clang, but vanilla clang doesn't 313 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 314 315 + # This is necessary to avoid: 316 + # "error: no visible @interface for 'NSDictionary' declares the selector 317 + # 'initWithContentsOfURL:error:'" 318 + # This can be removed when the apple_sdk is upgraded beyond 10.13+ 319 + sedVerbose tools/osx/xcode_locator.m \ 320 + -e '/initWithContentsOfURL:versionPlistUrl/ { 321 + N 322 + s/error:nil\];/\];/ 323 + }' 324 + 325 sed -i -e 's;"/usr/bin/libtool";_find_generic(repository_ctx, "libtool", "LIBTOOL", overriden_tools);g' tools/cpp/unix_cc_configure.bzl 326 wrappers=( tools/cpp/osx_cc_wrapper.sh.tpl ) 327 for wrapper in "''${wrappers[@]}"; do 328 + sedVerbose $wrapper \ 329 + -e "s,/usr/bin/xcrun install_name_tool,${cctools}/bin/install_name_tool,g" 330 done 331 ''; 332 333 + # -e "s,%{cc},${stdenv.cc}/bin/clang,g" \ 334 genericPatches = '' 335 # unzip builtins_bzl.zip so the contents get patched 336 builtins_bzl=src/main/java/com/google/devtools/build/lib/bazel/rules/builtins_bzl 337 unzip ''${builtins_bzl}.zip -d ''${builtins_bzl}_zip >/dev/null ··· 347 grep -rlZ /bin/ \ 348 src/main/java/com/google/devtools \ 349 src/main/starlark/builtins_bzl/common/python \ 350 + tools \ 351 | while IFS="" read -r -d "" path; do 352 # If you add more replacements here, you must change the grep above! 353 # Only files containing /bin are taken into account. ··· 373 # Passing EXTRA_BAZEL_ARGS is tricky due to quoting. 374 375 sedVerbose compile.sh \ 376 -e "/bazel_build /a\ --verbose_failures \\\\" \ 377 -e "/bazel_build /a\ --curses=no \\\\" \ 378 -e "/bazel_build /a\ --features=-layering_check \\\\" \ 379 -e "/bazel_build /a\ --experimental_strict_java_deps=off \\\\" \ 380 -e "/bazel_build /a\ --strict_proto_deps=off \\\\" \ 381 + -e "/bazel_build /a\ --action_env=NIX_CFLAGS_COMPILE=\"$NIX_CFLAGS_COMPILE\" \\\\" \ 382 + -e "/bazel_build /a\ --action_env=NIX_LDFLAGS=\"$NIX_LDFLAGS\" \\\\" \ 383 + -e "/bazel_build /a\ --host_action_env=NIX_CFLAGS_COMPILE=\"$NIX_CFLAGS_COMPILE\" \\\\" \ 384 + -e "/bazel_build /a\ --host_action_env=NIX_LDFLAGS=\"$NIX_LDFLAGS\" \\\\" \ 385 + -e "/bazel_build /a\ --toolchain_resolution_debug='@bazel_tools//tools/jdk:(runtime_)?toolchain_type' \\\\" \ 386 -e "/bazel_build /a\ --tool_java_runtime_version=local_jdk \\\\" \ 387 -e "/bazel_build /a\ --java_runtime_version=local_jdk \\\\" \ 388 + -e "/bazel_build /a\ --extra_toolchains=@bazel_tools//tools/jdk:all \\\\" \ 389 + -e "/bazel_build /a\ --distdir=${distDir} \\\\" \ 390 + 391 + #-e "/bazel_build /a\ --action_env=NIX_BINTOOLS=\"$NIX_BINTOOLS\" \\\\" \ 392 + #-e "/bazel_build /a\ --action_env=NIX_CC=\"$NIX_CC\" \\\\" \ 393 + #-e "/bazel_build /a\ --action_env=nativeBuildInputs=\"$nativeBuildInputs\" \\\\" \ 394 395 # Also build parser_deploy.jar with bootstrap bazel 396 # TODO: Turn into a proper patch ··· 398 -e 's!bazel_build !bazel_build src/tools/execlog:parser_deploy.jar !' \ 399 -e 's!clear_log!cp $(get_bazel_bin_path)/src/tools/execlog/parser_deploy.jar output\nclear_log!' 400 401 # append the PATH with defaultShellPath in tools/bash/runfiles/runfiles.bash 402 echo "PATH=\$PATH:${defaultShellPath}" >> runfiles.bash.tmp 403 cat tools/bash/runfiles/runfiles.bash >> runfiles.bash.tmp ··· 413 patchShebangs . >/dev/null 414 ''; 415 in 416 + '' 417 + function sedVerbose() { 418 + local path=$1; shift; 419 + sed -i".bak-nix" "$path" "$@" 420 + diff -U0 "$path.bak-nix" "$path" | sed "s/^/ /" || true 421 + rm -f "$path.bak-nix" 422 + } 423 + '' 424 + + lib.optionalString stdenv.hostPlatform.isDarwin darwinPatches 425 + + genericPatches; 426 + 427 + meta = with lib; { 428 + homepage = "https://github.com/bazelbuild/bazel/"; 429 + description = "Build tool that builds code quickly and reliably"; 430 + sourceProvenance = with sourceTypes; [ 431 + fromSource 432 + binaryBytecode # source bundles dependencies as jars 433 + ]; 434 + license = licenses.asl20; 435 + maintainers = lib.teams.bazel.members; 436 + inherit platforms; 437 + }; 438 + 439 + # Bazel starts a local server and needs to bind a local address. 440 + __darwinAllowLocalNetworking = true; 441 442 buildInputs = [ buildJdk ] ++ defaultShellUtils; 443 ··· 454 ] ++ lib.optionals (stdenv.isDarwin) [ 455 cctools 456 libcxx 457 + Foundation 458 CoreFoundation 459 CoreServices 460 ]; 461 462 # Bazel makes extensive use of symlinks in the WORKSPACE.
+13
pkgs/development/tools/build-managers/bazel/bazel_7/extra_toolchains_precedence.patch
···
··· 1 + diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsFunction.java 2 + index 2b407202a3..7591e78f85 100644 3 + --- a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsFunction.java 4 + +++ b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsFunction.java 5 + @@ -88,7 +88,7 @@ public class RegisteredToolchainsFunction implements SkyFunction { 6 + try { 7 + targetPatternBuilder.addAll( 8 + TargetPatternUtil.parseAllSigned( 9 + - platformConfiguration.getExtraToolchains(), mainRepoParser)); 10 + + platformConfiguration.getExtraToolchains().reverse(), mainRepoParser)); 11 + } catch (InvalidTargetPatternException e) { 12 + throw new RegisteredToolchainsFunctionException( 13 + new InvalidToolchainLabelException(e), Transience.PERSISTENT);
+18 -9
pkgs/development/tools/build-managers/bazel/bazel_7/java_toolchain.patch
··· 1 diff --git a/tools/jdk/BUILD.tools b/tools/jdk/BUILD.tools 2 - index a8af76e90c..af2540f838 100644 3 --- a/tools/jdk/BUILD.tools 4 +++ b/tools/jdk/BUILD.tools 5 - @@ -146,6 +146,16 @@ py_test( 6 ], 7 ) 8 9 +load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain", "NONPREBUILT_TOOLCHAIN_CONFIGURATION") 10 + 11 - +default_java_toolchain( 12 - + name = "nonprebuilt_toolchain_java11", 13 - + configuration = NONPREBUILT_TOOLCHAIN_CONFIGURATION, 14 - + java_runtime = "@local_jdk//:jdk", 15 - + source_version = "11", 16 - + target_version = "11", 17 - +) 18 + 19 #### Aliases to rules_java to keep backward-compatibility (begin) #### 20
··· 1 diff --git a/tools/jdk/BUILD.tools b/tools/jdk/BUILD.tools 2 + index a8af76e90c..7f8b030f63 100644 3 --- a/tools/jdk/BUILD.tools 4 +++ b/tools/jdk/BUILD.tools 5 + @@ -146,6 +146,25 @@ py_test( 6 ], 7 ) 8 9 + +##### Nonprebuilt toolchains definitions for NixOS and nix build sandboxes #### 10 + + 11 +load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain", "NONPREBUILT_TOOLCHAIN_CONFIGURATION") 12 + 13 + +[ 14 + + default_java_toolchain( 15 + + name = "nonprebuilt_toolchain_java" + str(version), 16 + + configuration = NONPREBUILT_TOOLCHAIN_CONFIGURATION, 17 + + java_runtime = "@local_jdk//:jdk", 18 + + source_version = str(version), 19 + + target_version = str(version), 20 + + ) 21 + + # Ideally we would only define toolchains for the java versions that the 22 + + # local jdk supports. But we cannot access this information in a BUILD 23 + + # file, and this is a hack anyway, so just pick a large enough upper bound. 24 + + # At the current pace, java <= 30 should cover all realeases until 2028. 25 + + for version in range(8, 31) 26 + +] 27 + 28 #### Aliases to rules_java to keep backward-compatibility (begin) #### 29
+2 -3
pkgs/development/tools/build-managers/bazel/bazel_7/no-arc.patch
··· 2 index 0358fb0ffe..baae1bf65b 100644 3 --- a/tools/osx/BUILD 4 +++ b/tools/osx/BUILD 5 - @@ -27,9 +27,9 @@ exports_files([ 6 ]) 7 8 DARWIN_XCODE_LOCATOR_COMPILE_COMMAND = """ 9 - /usr/bin/xcrun --sdk macosx clang -mmacosx-version-min=10.13 -fobjc-arc -framework CoreServices \ 10 - -framework Foundation -arch arm64 -arch x86_64 -Wl,-no_adhoc_codesign -Wl,-no_uuid -o $@ $< && \ 11 - env -i codesign --identifier $@ --force --sign - $@ 12 - + /usr/bin/xcrun --sdk macosx clang -mmacosx-version-min=10.13 -framework CoreServices \ 13 - + -framework Foundation -Wl,-no_uuid -o $@ $< && \ 14 + /usr/bin/env -i /usr/bin/codesign --identifier $@ --force --sign - $@ 15 """ 16
··· 2 index 0358fb0ffe..baae1bf65b 100644 3 --- a/tools/osx/BUILD 4 +++ b/tools/osx/BUILD 5 + @@ -27,9 +27,8 @@ exports_files([ 6 ]) 7 8 DARWIN_XCODE_LOCATOR_COMPILE_COMMAND = """ 9 - /usr/bin/xcrun --sdk macosx clang -mmacosx-version-min=10.13 -fobjc-arc -framework CoreServices \ 10 - -framework Foundation -arch arm64 -arch x86_64 -Wl,-no_adhoc_codesign -Wl,-no_uuid -o $@ $< && \ 11 - env -i codesign --identifier $@ --force --sign - $@ 12 + + /usr/bin/xcrun --sdk macosx clang -framework CoreServices -framework Foundation -Wl,-no_uuid -o $@ $< && \ 13 + /usr/bin/env -i /usr/bin/codesign --identifier $@ --force --sign - $@ 14 """ 15
+40
pkgs/development/tools/build-managers/bazel/bazel_7/patched_sources.nix
···
··· 1 + { lib 2 + , stdenv 3 + , version 4 + , src 5 + , sourceRoot 6 + , substituteAll 7 + , defaultShellPath 8 + , bazelRC 9 + , enableNixHacks 10 + , coreutils 11 + , CoreFoundation 12 + , CoreServices 13 + , Foundation 14 + , IOKit 15 + , libcxx 16 + , cctools 17 + , sigtool 18 + , bash 19 + , python3 20 + , buildJdk 21 + , unzip 22 + , zip 23 + , ... 24 + }: 25 + 26 + stdenv.mkDerivation { 27 + name = "bazel-patched-sources"; 28 + inherit version; 29 + inherit src; 30 + 31 + dontBuild = true; 32 + 33 + nativeBuildInputs = [ zip unzip ]; 34 + 35 + installPhase = '' 36 + mkdir $out 37 + cp -r . $out 38 + ''; 39 + 40 + }
+2 -1
pkgs/development/tools/build-managers/bazel/bazel_7/tests.nix
··· 2 # tooling 3 , fetchFromGitHub 4 , newScope 5 , runCommandCC 6 , stdenv 7 # inputs ··· 12 , repoCache 13 , runJdk 14 , xe 15 }: 16 let 17 inherit (stdenv.hostPlatform) isDarwin; ··· 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 };
··· 2 # tooling 3 , fetchFromGitHub 4 , newScope 5 + , recurseIntoAttrs 6 , runCommandCC 7 , stdenv 8 # inputs ··· 13 , repoCache 14 , runJdk 15 , xe 16 + , bazel-watcher 17 }: 18 let 19 inherit (stdenv.hostPlatform) isDarwin; ··· 114 inherit Foundation; 115 extraBazelArgs = '' 116 --repository_cache=${repoCache} \ 117 ''; 118 bazel = bazel_self; 119 };
+157
pkgs/development/tools/build-managers/bazel/bazel_7/toolchain_better_debug.patch
···
··· 1 + diff --git a/src/main/java/com/google/devtools/build/lib/analysis/platform/DeclaredToolchainInfo.java b/src/main/java/com/google/devtools/build/lib/analysis/platform/DeclaredToolchainInfo.java 2 + index fa322cf6f0..4a94087a3c 100644 3 + --- a/src/main/java/com/google/devtools/build/lib/analysis/platform/DeclaredToolchainInfo.java 4 + +++ b/src/main/java/com/google/devtools/build/lib/analysis/platform/DeclaredToolchainInfo.java 5 + @@ -47,6 +47,9 @@ public abstract class DeclaredToolchainInfo implements TransitiveInfoProvider { 6 + /** The label of the toolchain to resolve for use in toolchain-aware rules. */ 7 + public abstract Label toolchainLabel(); 8 + 9 + + /** The label of the toolchain native rule invocation, for debugging. */ 10 + + public abstract Label label(); 11 + + 12 + /** Builder class to assist in creating {@link DeclaredToolchainInfo} instances. */ 13 + public static class Builder { 14 + private ToolchainTypeInfo toolchainType; 15 + @@ -55,6 +58,7 @@ public abstract class DeclaredToolchainInfo implements TransitiveInfoProvider { 16 + private ImmutableList.Builder<ConfigMatchingProvider> targetSettings = 17 + new ImmutableList.Builder<>(); 18 + private Label toolchainLabel; 19 + + private Label label; 20 + 21 + /** Sets the type of the toolchain being declared. */ 22 + @CanIgnoreReturnValue 23 + @@ -100,6 +104,13 @@ public abstract class DeclaredToolchainInfo implements TransitiveInfoProvider { 24 + return this; 25 + } 26 + 27 + + /** Sets the label of the toolchain to resolve for use in toolchain-aware rules. */ 28 + + @CanIgnoreReturnValue 29 + + public Builder label(Label label) { 30 + + this.label = label; 31 + + return this; 32 + + } 33 + + 34 + /** Returns the newly created {@link DeclaredToolchainInfo} instance. */ 35 + public DeclaredToolchainInfo build() throws DuplicateConstraintException { 36 + // Handle constraint duplication in attributes separately, so they can be reported correctly. 37 + @@ -128,7 +139,8 @@ public abstract class DeclaredToolchainInfo implements TransitiveInfoProvider { 38 + execConstraints, 39 + targetConstraints, 40 + targetSettings.build(), 41 + - toolchainLabel); 42 + + toolchainLabel, 43 + + label); 44 + } 45 + } 46 + 47 + diff --git a/src/main/java/com/google/devtools/build/lib/rules/platform/Toolchain.java b/src/main/java/com/google/devtools/build/lib/rules/platform/Toolchain.java 48 + index 16d14a5edd..93ad4e47ec 100644 49 + --- a/src/main/java/com/google/devtools/build/lib/rules/platform/Toolchain.java 50 + +++ b/src/main/java/com/google/devtools/build/lib/rules/platform/Toolchain.java 51 + @@ -57,6 +57,7 @@ public class Toolchain implements RuleConfiguredTargetFactory { 52 + .collect(toImmutableList()); 53 + Label toolchainLabel = 54 + ruleContext.attributes().get(ToolchainRule.TOOLCHAIN_ATTR, BuildType.NODEP_LABEL); 55 + + Label label = ruleContext.getLabel(); 56 + 57 + DeclaredToolchainInfo registeredToolchain; 58 + try { 59 + @@ -67,6 +68,7 @@ public class Toolchain implements RuleConfiguredTargetFactory { 60 + .addTargetConstraints(targetConstraints) 61 + .addTargetSettings(targetSettings) 62 + .toolchainLabel(toolchainLabel) 63 + + .label(label) 64 + .build(); 65 + } catch (DeclaredToolchainInfo.DuplicateConstraintException e) { 66 + if (e.execConstraintsException() != null) { 67 + diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java 68 + index 933c200471..5c61ebcb12 100644 69 + --- a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java 70 + +++ b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java 71 + @@ -182,11 +182,12 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 72 + if (!nonmatchingSettings.isEmpty()) { 73 + debugMessage( 74 + eventHandler, 75 + - " Type %s: %s platform %s: Rejected toolchain %s; mismatching config settings: %s", 76 + + " Type %s: %s platform %s: Rejected toolchain %s (from %s); mismatching config settings: %s", 77 + toolchainType.toolchainType(), 78 + "target", 79 + targetPlatform.label(), 80 + toolchain.toolchainLabel(), 81 + + toolchain.label(), 82 + String.join(", ", nonmatchingSettings)); 83 + continue; 84 + } 85 + @@ -198,7 +199,8 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 86 + "target", 87 + targetPlatform, 88 + toolchainType, 89 + - toolchain.toolchainLabel())) { 90 + + toolchain.toolchainLabel(), 91 + + toolchain.label())) { 92 + continue; 93 + } 94 + 95 + @@ -208,12 +210,13 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 96 + if (platformKeysSeen.contains(executionPlatformKey)) { 97 + debugMessage( 98 + eventHandler, 99 + - " Type %s: target platform %s: execution platform %s: Skipping toolchain %s;" 100 + + " Type %s: target platform %s: execution platform %s: Skipping toolchain %s (from %s);" 101 + + " execution platform already has selected toolchain", 102 + toolchainType.toolchainType(), 103 + targetPlatform.label(), 104 + executionPlatformKey.getLabel(), 105 + - toolchain.toolchainLabel()); 106 + + toolchain.toolchainLabel(), 107 + + toolchain.label()); 108 + continue; 109 + } 110 + 111 + @@ -224,17 +227,19 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 112 + "execution", 113 + executionPlatform, 114 + toolchainType, 115 + - toolchain.toolchainLabel())) { 116 + + toolchain.toolchainLabel(), 117 + + toolchain.label())) { 118 + continue; 119 + } 120 + 121 + debugMessage( 122 + eventHandler, 123 + - " Type %s: target platform %s: execution %s: Selected toolchain %s", 124 + + " Type %s: target platform %s: execution %s: Selected toolchain %s (from %s)", 125 + toolchainType.toolchainType(), 126 + targetPlatform.label(), 127 + executionPlatformKey.getLabel(), 128 + - toolchain.toolchainLabel()); 129 + + toolchain.toolchainLabel(), 130 + + toolchain.label()); 131 + builder.put(executionPlatformKey, toolchain.toolchainLabel()); 132 + platformKeysSeen.add(executionPlatformKey); 133 + } 134 + @@ -276,7 +281,8 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 135 + String platformType, 136 + PlatformInfo platform, 137 + ToolchainTypeRequirement toolchainType, 138 + - Label toolchainLabel) { 139 + + Label toolchainLabel, 140 + + Label label) { 141 + 142 + // Check every constraint_setting in either the toolchain or the platform. 143 + ImmutableSet<ConstraintSettingInfo> mismatchSettings = 144 + @@ -315,11 +321,12 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 145 + } 146 + debugMessage( 147 + eventHandler, 148 + - " Type %s: %s platform %s: Rejected toolchain %s%s%s", 149 + + " Type %s: %s platform %s: Rejected toolchain %s (from %s)%s%s", 150 + toolchainType.toolchainType(), 151 + platformType, 152 + platform.label(), 153 + toolchainLabel, 154 + + label, 155 + mismatchValues, 156 + missingSettings); 157 + }
+271
pkgs/development/tools/build-managers/bazel/bazel_7/toolchain_group_debug.patch
···
··· 1 + diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java 2 + index 933c200471..1e5bbaa44b 100644 3 + --- a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java 4 + +++ b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/SingleToolchainResolutionFunction.java 5 + @@ -32,7 +32,6 @@ import com.google.devtools.build.lib.analysis.platform.PlatformInfo; 6 + import com.google.devtools.build.lib.analysis.platform.ToolchainTypeInfo; 7 + import com.google.devtools.build.lib.cmdline.Label; 8 + import com.google.devtools.build.lib.events.Event; 9 + -import com.google.devtools.build.lib.events.EventHandler; 10 + import com.google.devtools.build.lib.server.FailureDetails.Toolchain.Code; 11 + import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey; 12 + import com.google.devtools.build.lib.skyframe.toolchains.PlatformLookupUtil.InvalidPlatformException; 13 + @@ -91,14 +90,21 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 14 + .debugToolchainResolution(key.toolchainType().toolchainType()); 15 + 16 + // Find the right one. 17 + - return resolveConstraints( 18 + + ArrayList<String> trace = debug ? new ArrayList<>() : null; 19 + + SingleToolchainResolutionValue toolchainResolution = resolveConstraints( 20 + key.toolchainType(), 21 + key.toolchainTypeInfo(), 22 + key.availableExecutionPlatformKeys(), 23 + key.targetPlatformKey(), 24 + toolchains.registeredToolchains(), 25 + env, 26 + - debug ? env.getListener() : null); 27 + + trace); 28 + + 29 + + if (debug) { 30 + + env.getListener().handle(Event.info(String.join("\n", trace))); 31 + + } 32 + + 33 + + return toolchainResolution; 34 + } 35 + 36 + /** 37 + @@ -114,7 +120,7 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 38 + ConfiguredTargetKey targetPlatformKey, 39 + ImmutableList<DeclaredToolchainInfo> toolchains, 40 + Environment env, 41 + - @Nullable EventHandler eventHandler) 42 + + @Nullable List<String> trace) 43 + throws ToolchainResolutionFunctionException, InterruptedException { 44 + 45 + // Load the PlatformInfo needed to check constraints. 46 + @@ -151,10 +157,20 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 47 + toolchain.toolchainType().typeLabel().equals(toolchainType.toolchainType())) 48 + .collect(toImmutableList()); 49 + 50 + + debugMessage( 51 + + trace, 52 + + "Performing resolution of %s for target platform %s", 53 + + toolchainType.toolchainType(), 54 + + targetPlatform.label()); 55 + + 56 + for (DeclaredToolchainInfo toolchain : filteredToolchains) { 57 + // Make sure the target setting matches but watch out for resolution errors. 58 + ArrayList<String> nonmatchingSettings = new ArrayList<>(); 59 + ArrayList<String> errors = new ArrayList<>(); 60 + + debugMessage( 61 + + trace, 62 + + " toolchain %s", 63 + + toolchain.toolchainLabel()); 64 + 65 + // TODO(blaze-configurability-team): If this pattern comes up more often, add a central 66 + // facility for merging multiple MatchResult 67 + @@ -181,11 +197,8 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 68 + } 69 + if (!nonmatchingSettings.isEmpty()) { 70 + debugMessage( 71 + - eventHandler, 72 + - " Type %s: %s platform %s: Rejected toolchain %s; mismatching config settings: %s", 73 + - toolchainType.toolchainType(), 74 + - "target", 75 + - targetPlatform.label(), 76 + + trace, 77 + + " Rejected toolchain %s; mismatching config settings: %s", 78 + toolchain.toolchainLabel(), 79 + String.join(", ", nonmatchingSettings)); 80 + continue; 81 + @@ -193,35 +206,36 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 82 + 83 + // Make sure the target platform matches. 84 + if (!checkConstraints( 85 + - eventHandler, 86 + + trace, 87 + toolchain.targetConstraints(), 88 + - "target", 89 + + " Rejected toolchain %1s%3s", 90 + targetPlatform, 91 + toolchainType, 92 + toolchain.toolchainLabel())) { 93 + continue; 94 + } 95 + + 96 + + debugMessage( 97 + + trace, 98 + + " Toolchain %s is compatible with target plaform, searching for execution platforms:", 99 + + toolchain.toolchainLabel()); 100 + 101 + // Find the matching execution platforms. 102 + for (ConfiguredTargetKey executionPlatformKey : availableExecutionPlatformKeys) { 103 + // Only check the toolchains if this is a new platform. 104 + if (platformKeysSeen.contains(executionPlatformKey)) { 105 + debugMessage( 106 + - eventHandler, 107 + - " Type %s: target platform %s: execution platform %s: Skipping toolchain %s;" 108 + - + " execution platform already has selected toolchain", 109 + - toolchainType.toolchainType(), 110 + - targetPlatform.label(), 111 + - executionPlatformKey.getLabel(), 112 + - toolchain.toolchainLabel()); 113 + + trace, 114 + + " Skipping execution platform %s; it has already selected a toolchain", 115 + + executionPlatformKey.getLabel()); 116 + continue; 117 + } 118 + 119 + PlatformInfo executionPlatform = platforms.get(executionPlatformKey); 120 + if (!checkConstraints( 121 + - eventHandler, 122 + + trace, 123 + toolchain.execConstraints(), 124 + - "execution", 125 + + " Incompatible execution platform %2s%3s", 126 + executionPlatform, 127 + toolchainType, 128 + toolchain.toolchainLabel())) { 129 + @@ -229,51 +243,64 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 130 + } 131 + 132 + debugMessage( 133 + - eventHandler, 134 + - " Type %s: target platform %s: execution %s: Selected toolchain %s", 135 + - toolchainType.toolchainType(), 136 + - targetPlatform.label(), 137 + - executionPlatformKey.getLabel(), 138 + - toolchain.toolchainLabel()); 139 + + trace, 140 + + " Compatible execution platform %s", 141 + + executionPlatformKey.getLabel()); 142 + builder.put(executionPlatformKey, toolchain.toolchainLabel()); 143 + platformKeysSeen.add(executionPlatformKey); 144 + } 145 + } 146 + 147 + ImmutableMap<ConfiguredTargetKey, Label> resolvedToolchainLabels = builder.buildOrThrow(); 148 + - if (resolvedToolchainLabels.isEmpty()) { 149 + - debugMessage( 150 + - eventHandler, 151 + - " Type %s: target platform %s: No toolchains found.", 152 + - toolchainType.toolchainType(), 153 + - targetPlatform.label()); 154 + + if (trace != null) { 155 + + if (resolvedToolchainLabels.isEmpty()) { 156 + + debugMessage( 157 + + trace, 158 + + "No %s toolchain found for target platform %s.", 159 + + toolchainType.toolchainType(), 160 + + targetPlatform.label()); 161 + + } else { 162 + + debugMessage( 163 + + trace, 164 + + "Recap of selected %s toolchains for target platform %s:", 165 + + toolchainType.toolchainType(), 166 + + targetPlatform.label()); 167 + + resolvedToolchainLabels.forEach((executionPlatformKey, toolchainLabel) -> 168 + + debugMessage( 169 + + trace, 170 + + " Selected toolchain %s to run on exec platform %s", 171 + + toolchainLabel, 172 + + executionPlatformKey.getLabel()) 173 + + ); 174 + + } 175 + } 176 + 177 + return SingleToolchainResolutionValue.create(toolchainTypeInfo, resolvedToolchainLabels); 178 + } 179 + 180 + /** 181 + - * Helper method to print a debugging message, if the given {@link EventHandler} is not {@code 182 + + * Helper method to print a debugging message, if the given {@link trace} is not {@code 183 + * null}. 184 + */ 185 + @FormatMethod 186 + private static void debugMessage( 187 + - @Nullable EventHandler eventHandler, @FormatString String template, Object... args) { 188 + - if (eventHandler == null) { 189 + + @Nullable List<String> trace, @FormatString String template, Object... args) { 190 + + if (trace == null) { 191 + return; 192 + } 193 + 194 + - eventHandler.handle(Event.info("ToolchainResolution: " + String.format(template, args))); 195 + + trace.add(String.format(template, args)); 196 + } 197 + 198 + /** 199 + * Returns {@code true} iff all constraints set by the toolchain and in the {@link PlatformInfo} 200 + * match. 201 + */ 202 + + @SuppressWarnings("FormatStringAnnotation") 203 + private static boolean checkConstraints( 204 + - @Nullable EventHandler eventHandler, 205 + + @Nullable List<String> trace, 206 + ConstraintCollection toolchainConstraints, 207 + - String platformType, 208 + + String debugMessage, 209 + PlatformInfo platform, 210 + ToolchainTypeRequirement toolchainType, 211 + Label toolchainLabel) { 212 + @@ -295,7 +322,7 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 213 + .filter(toolchainConstraints::hasWithoutDefault) 214 + .collect(ImmutableSet.toImmutableSet()); 215 + 216 + - if (!mismatchSettingsWithDefault.isEmpty()) { 217 + + if (trace != null && !mismatchSettingsWithDefault.isEmpty()) { 218 + String mismatchValues = 219 + mismatchSettingsWithDefault.stream() 220 + .filter(toolchainConstraints::has) 221 + @@ -314,14 +341,11 @@ public class SingleToolchainResolutionFunction implements SkyFunction { 222 + missingSettings = "; missing: " + missingSettings; 223 + } 224 + debugMessage( 225 + - eventHandler, 226 + - " Type %s: %s platform %s: Rejected toolchain %s%s%s", 227 + - toolchainType.toolchainType(), 228 + - platformType, 229 + - platform.label(), 230 + + trace, 231 + + debugMessage, 232 + toolchainLabel, 233 + - mismatchValues, 234 + - missingSettings); 235 + + platform.label(), 236 + + mismatchValues + missingSettings); 237 + } 238 + 239 + return mismatchSettingsWithDefault.isEmpty(); 240 + diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/ToolchainResolutionFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/ToolchainResolutionFunction.java 241 + index 3bdf2d0b23..fea848eca0 100644 242 + --- a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/ToolchainResolutionFunction.java 243 + +++ b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/ToolchainResolutionFunction.java 244 + @@ -80,14 +80,7 @@ public class ToolchainResolutionFunction implements SkyFunction { 245 + Preconditions.checkNotNull(configuration.getFragment(PlatformConfiguration.class)); 246 + 247 + // Check if debug output should be generated. 248 + - boolean debug = 249 + - key.debugTarget() 250 + - || configuration 251 + - .getFragment(PlatformConfiguration.class) 252 + - .debugToolchainResolution( 253 + - key.toolchainTypes().stream() 254 + - .map(ToolchainTypeRequirement::toolchainType) 255 + - .collect(toImmutableSet())); 256 + + boolean debug = key.debugTarget(); 257 + 258 + // Load the configured target for the toolchain types to ensure that they are valid and 259 + // resolve aliases. 260 + @@ -138,9 +131,10 @@ public class ToolchainResolutionFunction implements SkyFunction { 261 + .handle( 262 + Event.info( 263 + String.format( 264 + - "ToolchainResolution: Target platform %s: Selected execution platform %s," 265 + + "ToolchainResolution: Target platform %s (%s): Selected execution platform %s, " 266 + + " %s", 267 + unloadedToolchainContext.targetPlatform().label(), 268 + + configuration, 269 + unloadedToolchainContext.executionPlatform().label(), 270 + selectedToolchains))); 271 + }
+10 -6
pkgs/development/tools/build-managers/bazel/java-test.nix
··· 7 , lib 8 , openjdk8 9 , jdk11_headless 10 - , jdk17_headless 11 , runLocal 12 , runtimeShell 13 , writeScript ··· 46 inherit workspaceDir; 47 bazelPkg = bazel; 48 buildInputs = [ 49 - #(if lib.strings.versionOlder bazel.version "5.0.0" then openjdk8 50 - #else if lib.strings.versionOlder bazel.version "7.0.0" then jdk11_headless 51 - #else jdk17_headless) 52 - jdk17_headless 53 ]; 54 bazelScript = '' 55 ${bazel}/bin/bazel \ 56 run \ 57 --announce_rc \ 58 --distdir=${distDir} \ 59 --verbose_failures \ 60 --curses=no \ 61 - --sandbox_debug \ 62 --strict_java_deps=off \ 63 //:ProjectRunner \ 64 '' + lib.optionalString (lib.strings.versionOlder bazel.version "5.0.0") '' ··· 68 '' + extraBazelArgs; 69 }; 70 71 in 72 testBazel 73
··· 7 , lib 8 , openjdk8 9 , jdk11_headless 10 , runLocal 11 , runtimeShell 12 , writeScript ··· 45 inherit workspaceDir; 46 bazelPkg = bazel; 47 buildInputs = [ 48 + (if lib.strings.versionOlder bazel.version "5.0.0" then openjdk8 else jdk11_headless) 49 ]; 50 bazelScript = '' 51 ${bazel}/bin/bazel \ 52 run \ 53 --announce_rc \ 54 + --toolchain_resolution_debug='@bazel_tools//tools/jdk:(runtime_)?toolchain_type' \ 55 --distdir=${distDir} \ 56 --verbose_failures \ 57 --curses=no \ 58 --strict_java_deps=off \ 59 //:ProjectRunner \ 60 '' + lib.optionalString (lib.strings.versionOlder bazel.version "5.0.0") '' ··· 64 '' + extraBazelArgs; 65 }; 66 67 + # --repo_env=JAVA_HOME=${jdk11_headless}/${if stdenv.hostPlatform.isDarwin then "/zulu-17.jdk/Contents/Home" else "/lib/openjdk"} \ 68 + #--java_language_version=17 \ 69 + #--java_language_version=17 \ 70 + #--java_runtime_version=local_jdk \ 71 + # --java_language_version=11 \ 72 + # --tool_java_runtime_version=local_jdk_17 \ 73 + # --tool_java_language_version=17 \ 74 + #--java_runtime_version=local_jdk_11 \ 75 in 76 testBazel 77