···5454 # Always assume all markers valid (this is needed because we remove markers; they are non-deterministic).
5555 # Also, don't clean up environment variables (so that NIX_ environment variables are passed to compilers).
5656, enableNixHacks ? false
5757-}:
5757+}@args:
58585959let
6060 version = "7.0.0-pre.20230917.3";
···136136 # on aarch64 Darwin, `uname -m` returns "arm64"
137137 arch = with stdenv.hostPlatform; if isDarwin && isAarch64 then "arm64" else parsed.cpu.name;
138138139139+ #build --extra_toolchains=@local_jdk//:all
140140+ #build --tool_java_runtime_version=local_jdk
141141+ #build --java_runtime_version=local_jdk
142142+ #build --repo_env=JAVA_HOME=${buildJdk}${if isDarwin then "/zulu-11.jdk/Contents/Home" else "/lib/openjdk"}
143143+139144 bazelRC = writeTextFile {
140145 name = "bazel-rc";
141146 text = ''
142147 startup --server_javabase=${buildJdk}
143148144144- build --extra_toolchains=@local_jdk//:all
149149+ # Register nix-specific nonprebuilt java toolchains
150150+ build --extra_toolchains=@bazel_tools//tools/jdk:all
151151+ # and set bazel to use them by default
145152 build --tool_java_runtime_version=local_jdk
146153 build --java_runtime_version=local_jdk
147147- build --repo_env=JAVA_HOME=${buildJdk}${if isDarwin then "/zulu-11.jdk/Contents/Home" else "/lib/openjdk"}
148154149155 # load default location for the system wide configuration
150156 try-import /etc/bazel.bazelrc
151157 '';
152158 };
153159154154- bazelNixFlagsScript = writeScript "bazel-nix-flags" ''
155155- cat << EOF
156156- common --announce_rc
157157- build --toolchain_resolution_debug=".*"
158158- build --local_ram_resources=HOST_RAM*.5
159159- build --local_cpu_resources=HOST_CPUS*.75
160160- build --copt=$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ / --copt=/g')
161161- build --host_copt=$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ / --host_copt=/g')
162162- build --linkopt=$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ / --linkopt=/g')
163163- build --host_linkopt=$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ / --host_linkopt=/g')
164164- build --linkopt=-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ / --linkopt=-Wl,/g')
165165- build --host_linkopt=-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ / --host_linkopt=-Wl,/g')
166166- build --extra_toolchains=@bazel_tools//tools/jdk:nonprebuilt_toolchain_definition
167167- build --verbose_failures
168168- build --curses=no
169169- build --features=-layering_check
170170- build --experimental_strict_java_deps=off
171171- build --strict_proto_deps=off
172172- build --extra_toolchains=@bazel_tools//tools/jdk:nonprebuilt_toolchain_java11_definition
173173- build --extra_toolchains=@local_jdk//:all
174174- build --tool_java_runtime_version=local_jdk_11
175175- build --java_runtime_version=local_jdk_11
176176- build --repo_env=JAVA_HOME=${buildJdk}${if isDarwin then "/zulu-11.jdk/Contents/Home" else "/lib/openjdk"}
177177- EOF
178178- '';
179160in
180161stdenv.mkDerivation rec {
181162 pname = "bazel";
182182- inherit version;
163163+ inherit version src;
164164+ inherit sourceRoot;
183165184184- meta = with lib; {
185185- homepage = "https://github.com/bazelbuild/bazel/";
186186- description = "Build tool that builds code quickly and reliably";
187187- sourceProvenance = with sourceTypes; [
188188- fromSource
189189- binaryBytecode # source bundles dependencies as jars
190190- ];
191191- license = licenses.asl20;
192192- maintainers = lib.teams.bazel.members;
193193- inherit platforms;
194194- };
195195-196196- inherit src;
197197- inherit sourceRoot;
198166 patches = [
199167 # TODO: Make GSON work,
200168 # In particular, our bazel build cannot emit MODULE.bazel.lock
201169 # it only produces an empty json object `{ }`.
202170 ./serialize_nulls.patch
203171172172+ # --extra_toolchains defined later should come before the ones defined earlier.
173173+ # As-is, this patch also inverts the order of extra_toolchains lists, but it's just a hack
174174+ ./extra_toolchains_precedence.patch
175175+176176+ #./toolchain_better_debug.patch
177177+ ./toolchain_group_debug.patch
178178+179179+ # Remote java toolchains do not work on NixOS because they download binaries,
180180+ # so we need to use the @local_jdk//:jdk
181181+ # It could in theory be done by registering @local_jdk//:all toolchains,
182182+ # but these java toolchains still bundle binaries for ijar and stuff. So we
183183+ # need a nonprebult java toolchain (where ijar and stuff is built from
184184+ # sources).
185185+ # There is no such java toolchain, so we introduce one here.
186186+ # By providing no version information, the toolchain will set itself to the
187187+ # version of $JAVA_HOME/bin/java, just like the local_jdk does.
188188+ # To ensure this toolchain gets used, we can set
189189+ # --{,tool_}java_runtime_version=local_jdk and rely on the fact no java
190190+ # toolchain registered by default uses the local_jdk, making the selection
191191+ # unambiguous.
192192+ # This toolchain has the advantage that it can use any ambiant java jdk,
193193+ # not only a given, fixed version. It allows bazel to work correctly in any
194194+ # environment where JAVA_HOME is set to the right java version, like inside
195195+ # nix derivations.
196196+ # However, this patch breaks bazel hermeticity, by picking the ambiant java
197197+ # version instead of the more hermetic remote_jdk prebuilt binaries that
198198+ # rules_java provide by default. It also requires the user to have a
199199+ # JAVA_HOME set to the exact version required by the project.
200200+ # With more code, we could define java toolchains for all the java versions
201201+ # supported by the jdk as in rules_java's
202202+ # toolchains/local_java_repository.bzl, but this is not implemented here.
203203+ # To recover vanilla behavior, non NixOS users can set
204204+ # --{,tool_}java_runtime_version=remote_jdk, effectively reverting the
205205+ # effect of this patch and the fake system bazelrc.
206206+ ./java_toolchain.patch
207207+204208 # Bazel integrates with apple IOKit to inhibit and track system sleep.
205209 # Inside the darwin sandbox, these API calls are blocked, and bazel
206210 # crashes. It seems possible to allow these APIs inside the sandbox, but it
···239243 # This patch removes using the -fobjc-arc compiler option and makes the code
240244 # compile without automatic reference counting. Caveat: this leaks memory, but
241245 # we accept this fact because xcode_locator is only a short-lived process used during the build.
242242- (substituteAll {
243243- src = ./no-arc.patch;
244244- multiBinPatch = if stdenv.hostPlatform.system == "aarch64-darwin" then "arm64" else "x86_64";
245245- })
246246+ ./no-arc.patch
246247247248 # --experimental_strict_action_env (which may one day become the default
248249 # see bazelbuild/bazel#2574) hardcodes the default
···269270 ]
270271 # See enableNixHacks argument above.
271272 ++ lib.optional enableNixHacks ./nix-hacks.patch;
272272-273273-274274- # Bazel starts a local server and needs to bind a local address.
275275- __darwinAllowLocalNetworking = true;
276273277274 postPatch =
278275 let
279276 darwinPatches = ''
277277+280278 bazelLinkFlags () {
281279 eval set -- "$NIX_LDFLAGS"
282280 local flag
···284282 printf ' -Wl,%s' "$flag"
285283 done
286284 }
287287-288288- # Disable Bazel's Xcode toolchain detection which would configure compilers
289289- # and linkers from Xcode instead of from PATH
290290- export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
291285292286 # Explicitly configure gcov since we don't have it on Darwin, so autodetection fails
293287 export GCOV=${coreutils}/bin/false
···300294 # https://github.com/NixOS/nixpkgs/pull/41589
301295 export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -isystem ${lib.getDev libcxx}/include/c++/v1"
302296297297+ # This variable is used by bazel to propagate env vars for homebrew,
298298+ # which is exactly what we need too.
299299+ export HOMEBREW_RUBY_PATH="foo"
300300+303301 # don't use system installed Xcode to run clang, use Nix clang instead
304302 sed -i -E \
305303 -e "s;/usr/bin/xcrun (--sdk macosx )?clang;${stdenv.cc}/bin/clang $NIX_CFLAGS_COMPILE $(bazelLinkFlags) -framework CoreFoundation;g" \
···307305 scripts/bootstrap/compile.sh \
308306 tools/osx/BUILD
309307310310- substituteInPlace scripts/bootstrap/compile.sh --replace ' -mmacosx-version-min=10.9' ""
311311-312308 # nixpkgs's libSystem cannot use pthread headers directly, must import GCD headers instead
313309 sed -i -e "/#include <pthread\/spawn.h>/i #include <dispatch/dispatch.h>" src/main/cpp/blaze_util_darwin.cc
314310···316312 # invocations of gcc to clang, but vanilla clang doesn't
317313 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
318314315315+ # This is necessary to avoid:
316316+ # "error: no visible @interface for 'NSDictionary' declares the selector
317317+ # 'initWithContentsOfURL:error:'"
318318+ # This can be removed when the apple_sdk is upgraded beyond 10.13+
319319+ sedVerbose tools/osx/xcode_locator.m \
320320+ -e '/initWithContentsOfURL:versionPlistUrl/ {
321321+ N
322322+ s/error:nil\];/\];/
323323+ }'
324324+319325 sed -i -e 's;"/usr/bin/libtool";_find_generic(repository_ctx, "libtool", "LIBTOOL", overriden_tools);g' tools/cpp/unix_cc_configure.bzl
320326 wrappers=( tools/cpp/osx_cc_wrapper.sh.tpl )
321327 for wrapper in "''${wrappers[@]}"; do
322322- sed -i -e "s,/usr/bin/gcc,${stdenv.cc}/bin/clang,g" $wrapper
323323- sed -i -e "s,/usr/bin/install_name_tool,${cctools}/bin/install_name_tool,g" $wrapper
324324- sed -i -e "s,/usr/bin/xcrun install_name_tool,${cctools}/bin/install_name_tool,g" $wrapper
328328+ sedVerbose $wrapper \
329329+ -e "s,/usr/bin/xcrun install_name_tool,${cctools}/bin/install_name_tool,g"
325330 done
326331 '';
327332333333+ # -e "s,%{cc},${stdenv.cc}/bin/clang,g" \
328334 genericPatches = ''
329329- function sedVerbose() {
330330- local path=$1; shift;
331331- sed -i".bak-nix" "$path" "$@"
332332- diff -U0 "$path.bak-nix" "$path" | sed "s/^/ /" || true
333333- rm -f "$path.bak-nix"
334334- }
335335-336335 # unzip builtins_bzl.zip so the contents get patched
337336 builtins_bzl=src/main/java/com/google/devtools/build/lib/bazel/rules/builtins_bzl
338337 unzip ''${builtins_bzl}.zip -d ''${builtins_bzl}_zip >/dev/null
···348347 grep -rlZ /bin/ \
349348 src/main/java/com/google/devtools \
350349 src/main/starlark/builtins_bzl/common/python \
351351- tools/python \
352352- tools/cpp \
353353- tools/build_rules \
354354- tools/osx/BUILD \
350350+ tools \
355351 | while IFS="" read -r -d "" path; do
356352 # If you add more replacements here, you must change the grep above!
357353 # Only files containing /bin are taken into account.
···377373 # Passing EXTRA_BAZEL_ARGS is tricky due to quoting.
378374379375 sedVerbose compile.sh \
380380- -e "/bazel_build /a\ --copt=\"$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ /" --copt=\"/g')\" \\\\" \
381381- -e "/bazel_build /a\ --host_copt=\"$(echo $NIX_CFLAGS_COMPILE | sed -e 's/ /" --host_copt=\"/g')\" \\\\" \
382382- -e "/bazel_build /a\ --linkopt=\"$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ /" --linkopt=\"/g')\" \\\\" \
383383- -e "/bazel_build /a\ --host_linkopt=\"$(echo $(< ${stdenv.cc}/nix-support/libcxx-ldflags) | sed -e 's/ /" --host_linkopt=\"/g')\" \\\\" \
384384- -e "/bazel_build /a\ --linkopt=\"-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ /" --linkopt=\"-Wl,/g')\" \\\\" \
385385- -e "/bazel_build /a\ --host_linkopt=\"-Wl,$(echo $NIX_LDFLAGS | sed -e 's/ /" --host_linkopt=\"-Wl,/g')\" \\\\" \
386376 -e "/bazel_build /a\ --verbose_failures \\\\" \
387377 -e "/bazel_build /a\ --curses=no \\\\" \
388378 -e "/bazel_build /a\ --features=-layering_check \\\\" \
389379 -e "/bazel_build /a\ --experimental_strict_java_deps=off \\\\" \
390380 -e "/bazel_build /a\ --strict_proto_deps=off \\\\" \
381381+ -e "/bazel_build /a\ --action_env=NIX_CFLAGS_COMPILE=\"$NIX_CFLAGS_COMPILE\" \\\\" \
382382+ -e "/bazel_build /a\ --action_env=NIX_LDFLAGS=\"$NIX_LDFLAGS\" \\\\" \
383383+ -e "/bazel_build /a\ --host_action_env=NIX_CFLAGS_COMPILE=\"$NIX_CFLAGS_COMPILE\" \\\\" \
384384+ -e "/bazel_build /a\ --host_action_env=NIX_LDFLAGS=\"$NIX_LDFLAGS\" \\\\" \
385385+ -e "/bazel_build /a\ --toolchain_resolution_debug='@bazel_tools//tools/jdk:(runtime_)?toolchain_type' \\\\" \
391386 -e "/bazel_build /a\ --tool_java_runtime_version=local_jdk \\\\" \
392387 -e "/bazel_build /a\ --java_runtime_version=local_jdk \\\\" \
393393- -e "/bazel_build /a\ --repo_env=JAVA_HOME=${buildJdk}/${if isDarwin then "/zulu-11.jdk/Contents/Home" else "/lib/openjdk"} \\\\" \
394394- -e "/bazel_build /a\ --extra_toolchains=@local_jdk//:all \\\\" \
395395- -e "/bazel_build /a\ --toolchain_resolution_debug=@bazel_tools//tools/jdk:runtime_toolchain_type \\\\" \
396396- -e "/bazel_build /a\ --sandbox_debug --verbose_failures \\\\" \
388388+ -e "/bazel_build /a\ --extra_toolchains=@bazel_tools//tools/jdk:all \\\\" \
389389+ -e "/bazel_build /a\ --distdir=${distDir} \\\\" \
390390+391391+ #-e "/bazel_build /a\ --action_env=NIX_BINTOOLS=\"$NIX_BINTOOLS\" \\\\" \
392392+ #-e "/bazel_build /a\ --action_env=NIX_CC=\"$NIX_CC\" \\\\" \
393393+ #-e "/bazel_build /a\ --action_env=nativeBuildInputs=\"$nativeBuildInputs\" \\\\" \
397394398395 # Also build parser_deploy.jar with bootstrap bazel
399396 # TODO: Turn into a proper patch
···401398 -e 's!bazel_build !bazel_build src/tools/execlog:parser_deploy.jar !' \
402399 -e 's!clear_log!cp $(get_bazel_bin_path)/src/tools/execlog/parser_deploy.jar output\nclear_log!'
403400404404-405405- # This is necessary to avoid:
406406- # "error: no visible @interface for 'NSDictionary' declares the selector
407407- # 'initWithContentsOfURL:error:'"
408408- # This can be removed when the apple_sdk is upgraded beyond 10.13+
409409- sedVerbose tools/osx/xcode_locator.m \
410410- -e '/initWithContentsOfURL:versionPlistUrl/ {
411411- N
412412- s/error:nil\];/\];/
413413- }'
414414-415401 # append the PATH with defaultShellPath in tools/bash/runfiles/runfiles.bash
416402 echo "PATH=\$PATH:${defaultShellPath}" >> runfiles.bash.tmp
417403 cat tools/bash/runfiles/runfiles.bash >> runfiles.bash.tmp
···427413 patchShebangs . >/dev/null
428414 '';
429415 in
430430- lib.optionalString isDarwin darwinPatches + genericPatches;
416416+ ''
417417+ function sedVerbose() {
418418+ local path=$1; shift;
419419+ sed -i".bak-nix" "$path" "$@"
420420+ diff -U0 "$path.bak-nix" "$path" | sed "s/^/ /" || true
421421+ rm -f "$path.bak-nix"
422422+ }
423423+ ''
424424+ + lib.optionalString stdenv.hostPlatform.isDarwin darwinPatches
425425+ + genericPatches;
426426+427427+ meta = with lib; {
428428+ homepage = "https://github.com/bazelbuild/bazel/";
429429+ description = "Build tool that builds code quickly and reliably";
430430+ sourceProvenance = with sourceTypes; [
431431+ fromSource
432432+ binaryBytecode # source bundles dependencies as jars
433433+ ];
434434+ license = licenses.asl20;
435435+ maintainers = lib.teams.bazel.members;
436436+ inherit platforms;
437437+ };
438438+439439+ # Bazel starts a local server and needs to bind a local address.
440440+ __darwinAllowLocalNetworking = true;
431441432442 buildInputs = [ buildJdk ] ++ defaultShellUtils;
433443···444454 ] ++ lib.optionals (stdenv.isDarwin) [
445455 cctools
446456 libcxx
457457+ Foundation
447458 CoreFoundation
448459 CoreServices
449449- Foundation
450460 ];
451461452462 # Bazel makes extensive use of symlinks in the WORKSPACE.
···11diff --git a/tools/jdk/BUILD.tools b/tools/jdk/BUILD.tools
22-index a8af76e90c..af2540f838 100644
22+index a8af76e90c..7f8b030f63 100644
33--- a/tools/jdk/BUILD.tools
44+++ b/tools/jdk/BUILD.tools
55-@@ -146,6 +146,16 @@ py_test(
55+@@ -146,6 +146,25 @@ py_test(
66 ],
77 )
8899++##### Nonprebuilt toolchains definitions for NixOS and nix build sandboxes ####
1010++
911+load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain", "NONPREBUILT_TOOLCHAIN_CONFIGURATION")
1012+
1111-+default_java_toolchain(
1212-+ name = "nonprebuilt_toolchain_java11",
1313-+ configuration = NONPREBUILT_TOOLCHAIN_CONFIGURATION,
1414-+ java_runtime = "@local_jdk//:jdk",
1515-+ source_version = "11",
1616-+ target_version = "11",
1717-+)
1313++[
1414++ default_java_toolchain(
1515++ name = "nonprebuilt_toolchain_java" + str(version),
1616++ configuration = NONPREBUILT_TOOLCHAIN_CONFIGURATION,
1717++ java_runtime = "@local_jdk//:jdk",
1818++ source_version = str(version),
1919++ target_version = str(version),
2020++ )
2121++ # Ideally we would only define toolchains for the java versions that the
2222++ # local jdk supports. But we cannot access this information in a BUILD
2323++ # file, and this is a hack anyway, so just pick a large enough upper bound.
2424++ # At the current pace, java <= 30 should cover all realeases until 2028.
2525++ for version in range(8, 31)
2626++]
1827+
1928 #### Aliases to rules_java to keep backward-compatibility (begin) ####
2029