Skip to content

Commit 554a502

Browse files
committed
feat: update bazel
Change-Id: I0dfcb558b3a41db5bcd9c6ced369b9a1aa90a464
1 parent 55bc9a6 commit 554a502

File tree

5 files changed

+195
-6
lines changed

5 files changed

+195
-6
lines changed

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.2.0
1+
6.4.0
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Copyright 2020 The Monogon Project Authors.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
15+
16+
# We need to patch the IntelliJ Bazel aspect that is injected via the @intellij_bazel repository
17+
# to:
18+
# - properly resolve go_library rules that have both source files and embeds.
19+
# - allow running test/sync against //... which contains cc_toolchain rules
20+
# (see https://github.com/bazelbuild/intellij/issues/1344 )
21+
--- a/intellij_info_impl_bundled.bzl
22+
+++ b/intellij_info_impl_bundled.bzl
23+
@@ -13,6 +13,7 @@
24+
":make_variables.bzl",
25+
"expand_make_variables",
26+
)
27+
+load("@io_bazel_rules_go//go:def.bzl", "GoLibrary")
28+
29+
# Defensive list of features that can appear in the C++ toolchain, but which we
30+
# definitely don't want to enable (when enabled, they'd contribute command line
31+
@@ -350,6 +351,8 @@
32+
"go_appengine_test",
33+
]:
34+
sources = [f for src in getattr(ctx.rule.attr, "srcs", []) for f in src.files.to_list()]
35+
+ sources += [f for embed in getattr(ctx.rule.attr, "embed", []) for f in _collect_generated_go_sources(embed, ctx, semantics) or []]
36+
+
37+
generated = [f for f in sources if not f.is_source]
38+
elif ctx.rule.kind == "go_wrap_cc":
39+
genfiles = target.files.to_list()
40+
@@ -372,6 +375,10 @@
41+
if go_semantics:
42+
import_path = go_semantics.get_import_path(ctx)
43+
44+
+ if import_path == None and getattr(ctx.rule.attr, "embed", None) != None and ctx.rule.kind == "go_library":
45+
+ embed_attr = getattr(ctx.rule.attr, "embed", None)
46+
+ import_path = embed_attr[0][GoLibrary].importpath
47+
+
48+
library_labels = []
49+
if ctx.rule.kind == "go_test" or ctx.rule.kind == "go_appengine_test":
50+
if getattr(ctx.rule.attr, "library", None) != None:
51+
@@ -457,6 +464,8 @@
52+
return False
53+
if cc_common.CcToolchainInfo not in target:
54+
return False
55+
+ if type(target[cc_common.CcToolchainInfo]) != 'CcToolchainInfo':
56+
+ return False
57+
58+
# cc toolchain to access compiler flags
59+
cpp_toolchain = target[cc_common.CcToolchainInfo]
60+

shell.nix

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,29 @@ let
1515
export NIX_SSL_CERT_FILE="${cacert}/etc/ssl/certs/ca-bundle.crt"
1616
export SSL_CERT_FILE="${cacert}/etc/ssl/certs/ca-bundle.crt"
1717
18+
# Let some downstream machinery know we're on NixOS. This is used mostly to
19+
# work around Bazel/NixOS interactions.
20+
export MONOGON_NIXOS=yep
21+
1822
# Convince rules_go to use /bin/bash and not a NixOS store bash which has
1923
# no idea how to resolve other things in the nix store once PATH is
2024
# stripped by (host_)action_env.
2125
export BAZEL_SH=/bin/bash
2226
23-
exec bash --noprofile --norc
27+
# Allow passing a custom command via env since nix-shell doesn't support
28+
# this yet: https://github.com/NixOS/nix/issues/534
29+
if [ ! -n "$COMMAND" ]; then
30+
COMMAND="bash --noprofile --norc"
31+
fi
32+
exec $COMMAND
2433
'';
2534
in
2635
(pkgs.buildFHSUserEnv {
2736
name = "monogon-nix";
2837
targetPkgs = pkgs: with pkgs; [
2938
git
3039
bazel_6
40+
openjdk21
3141
];
3242
runScript = wrapper;
3343
}).env

third_party/nix/sources.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"nixpkgs": {
3-
"branch": "release-23.05",
3+
"branch": "release-23.11",
44
"description": "Nix Packages collection",
55
"homepage": null,
66
"owner": "NixOS",
77
"repo": "nixpkgs",
8-
"rev": "b9fc1843e64c11665ef440d02ecb6b3980a62756",
9-
"sha256": "0krzc81cn1vfblhljw9wgx1wkfwlifh9dy3z3c64yhkh3xy6pfji",
8+
"rev": "d2003f2223cbb8cd95134e4a0541beea215c1073",
9+
"sha256": "0n2a72jb0j1iya7j05r6hbvmsnm4ai05g4lmxnfbah87gxl3d9qg",
1010
"type": "tarball",
11-
"url": "https://github.com/NixOS/nixpkgs/archive/b9fc1843e64c11665ef440d02ecb6b3980a62756.tar.gz",
11+
"url": "https://github.com/NixOS/nixpkgs/archive/d2003f2223cbb8cd95134e4a0541beea215c1073.tar.gz",
1212
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
1313
}
1414
}

tools/bazel

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env bash
2+
# Both bazelisk and bazel's native wrapper scripts will attempt to use the well-known executable
3+
# named "tools/bazel" to run Bazel. The path of the original executable is stored in BAZEL_REAL.
4+
set -euo pipefail
5+
shopt -s nullglob
6+
7+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
8+
9+
# Jump into nix-shell if BAZEL_REAL is set to a /nix/store path and we aren't
10+
# inside our shell yet.
11+
if [[ "${BAZEL_REAL:-}" == /nix/store/* && -z "${MONOGON_NIXOS:-}" ]]; then
12+
echo "Detected Nix based bazel installation and we are not in a nix-shell, overriding to nix-shell." >&2
13+
USE_NIX_SHELL=yes
14+
fi
15+
16+
# If the wrapper is called directly we check if nix-shell is available
17+
# to automagically switch into the nix-shell. Otherwise complain and
18+
# exit.
19+
if [[ -z "${BAZEL_REAL:-}" ]]; then
20+
if [[ -x $(command -v nix-shell) ]]; then
21+
echo "BAZEL_REAL is not set and nix-shell is available, overriding to nix-shell" >&2
22+
USE_NIX_SHELL=yes
23+
else
24+
echo "BAZEL_REAL is not set and nix-shell not available. Please check the setup guide." >&2
25+
exit 1
26+
fi
27+
fi
28+
29+
if [[ -n "${USE_NIX_SHELL:-}" ]]; then
30+
# Jump to project root since bwrap hangs if we aren't there
31+
cd "${DIR}/../"
32+
33+
export COMMAND="bazel $*"
34+
export PWD="$OLDPWD"
35+
exec nix-shell
36+
fi
37+
38+
39+
prechecks() {
40+
# Recommend using Bazelisk instead of Bazel's "bazel.sh" wrapper.
41+
# Skip if we're inside the Nix shell (which uses a customized Bazel build).
42+
if [[ -z "${BAZELISK_SKIP_WRAPPER:-}" && -z "${MONOGON_NIXOS:-}" ]]; then
43+
echo "############################################################" >&2
44+
echo "# Please use Bazelisk to build NetMeta. Using Bazel #" >&2
45+
echo "# directly may work, but is not recommended or supported. #" >&2
46+
echo "############################################################" >&2
47+
fi
48+
}
49+
50+
intellij_patch() {
51+
# When IntelliJ's Bazel plugin uses //scripts/bin/bazel to either build targets
52+
# or run syncs, it adds a --override_repository flag to the bazel command
53+
# line that points @intellij_aspect into a path on the filesystem. This
54+
# external repository contains a Bazel Aspect definition which Bazel
55+
# executes to provide the IntelliJ Bazel plugin with information about the
56+
# workspace / build targets / etc...
57+
#
58+
# We need to patch the aspect definition to fix a number of bugs
59+
# to make it work with the Monogon monorepo.
60+
# Find all IntelliJ installation/config directories.
61+
local ij_home_paths=("${HOME}/.local/share/JetBrains/IntelliJIdea"*)
62+
# Get the newest one, if any.
63+
local ij_home=""
64+
if ! [[ ${#ij_home_paths[@]} -eq 0 ]]; then
65+
# Reverse sort paths by name, with the first being the newest IntelliJ
66+
# installation.
67+
IFS=$'\n'
68+
local sorted=($(sort -r <<<"${ij_home_paths[*]}"))
69+
unset IFS
70+
ij_home="${sorted[0]}"
71+
fi
72+
# If we don't have or can't find ij_home, don't bother with attempting to patch anything.
73+
if [[ -d "${ij_home}" ]]; then
74+
# aspect_path is the path to the aspect external repository that IntelliJ will
75+
# inject into bazel via --override_repository.
76+
local aspect_path="${ij_home}/ijwb/aspect"
77+
# Our copy of it.
78+
local patched_path="${ij_home}/ijwb/aspect-monogon"
79+
# Checksum of the patch that was used to create patched_path.
80+
local checksum_file="${patched_path}/checksum"
81+
# The patch
82+
local patch_file="${DIR}/../intellij/patches/bazel_intellij_aspect_filter.patch"
83+
# The checksum of the patch we're about to apply.
84+
local checksum
85+
checksum=$(sha256sum "$patch_file" | cut -d' ' -f1)
86+
# If the patched aspect repository doesn't exist, or the checksum of the patch
87+
# we're about to apply doesn't match the checksum of the patch that was used
88+
# to create the patched aspect repository, apply the patch.
89+
if ! [[ -d "${patched_path}" ]] || ! [[ "$(cat "${checksum_file}")" == "${checksum}" ]]; then
90+
echo "IntelliJ found at ${ij_home}, patching aspect repository." >&2
91+
# Copy the aspect repository to the patched path.
92+
rm -rf "${patched_path}"
93+
cp -r "${aspect_path}" "${patched_path}"
94+
# Apply the patch.
95+
patch -d "${patched_path}" -p1 < "${patch_file}"
96+
# Write the checksum of the patch to the checksum file.
97+
echo "${checksum}" > "${checksum_file}"
98+
else
99+
echo "IntelliJ found at ${ij_home}, aspect repository already patched." >&2
100+
fi
101+
fi
102+
}
103+
104+
prechecks
105+
intellij_patch
106+
107+
# Find the --override_repository=intellij_aspect=[path]
108+
# argument in $@ and replace the path with the patched version.
109+
# This is surprisingly tricky - bash special-cases "$@" to expand
110+
# as "$1" "$2" ... "$n" so that argv is preserved, so we need to
111+
# modify the real $@ array.
112+
for i in $(seq 1 $#); do
113+
if [[ "${!i}" == "--override_repository=intellij_aspect="* ]]; then
114+
new_arg="${!i/\/aspect/\/aspect-monogon}"
115+
set -- "${@:1:$((i-1))}" "${new_arg}" "${@:$((i+1))}"
116+
fi
117+
done
118+
119+
exec -a "$0" "${BAZEL_REAL}" "$@"

0 commit comments

Comments
 (0)