-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.nix
369 lines (325 loc) · 11 KB
/
build.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
{ pkgs
, lib
, path
, llvmPackages_16
, buildBazelPackage
, writeTextFile
, writeShellApplication
, installShellFiles
, makeWrapper
, makeBinaryWrapper
, substituteAll
, bazel_7
, bash
, binutils-unwrapped
, coreutils
, diffutils
, file
, findutils
, gawk
, gnugrep
, gnupatch
, gnumake
, gnused
, gnutar
, gzip
, python3
, unzip
, which
, zip
, libtool
# Apple dependencies
, darwin
, libcxx
# explicit arguments
, src
, version
, rev
, deps-hash
, buildJdk
, runJdk
# see nixpkgs derivation
# required to use bazel in `buildBazelPackage`.
, enableNixHacks ? false
}:
let
apple_sdk = darwin.apple_sdk_11_0;
stdenv = if pkgs.stdenv.isDarwin then apple_sdk.stdenv else llvmPackages_16.libcxxStdenv;
bazel_path = "${path}/pkgs/development/tools/build-managers/bazel";
inherit (darwin) cctools sigtool;
inherit (apple_sdk.frameworks) CoreFoundation CoreServices Foundation IOKit;
defaultShellUtils =
[ libtool ]
++ lib.optionals pkgs.stdenv.isDarwin [ cctools ]
++ [
bash
binutils-unwrapped
coreutils
diffutils
file
findutils
gawk
gnugrep
gnupatch
gnumake
gnused
gnutar
gzip
python3
unzip
which
zip
];
defaultShellPath = lib.makeBinPath defaultShellUtils;
bashWithDefaultShellUtilsSh = writeShellApplication {
name = "bash";
runtimeInputs = defaultShellUtils;
text = ''
if [[ "$PATH" == "/no-such-path" ]]; then
export PATH=${defaultShellPath}
fi
exec ${bash}/bin/bash "$@"
'';
};
# see nixpkgs derivation
bashWithDefaultShellUtils = stdenv.mkDerivation {
name = "bash";
src = bashWithDefaultShellUtilsSh;
nativeBuildInputs = [ makeBinaryWrapper ];
buildPhase = ''
makeWrapper ${bashWithDefaultShellUtilsSh}/bin/bash $out/bin/bash
'';
};
bazelFlags = [
"--extra_toolchains=@bazel_tools//tools/jdk:all"
"--tool_java_runtime_version=local_jdk"
"--java_runtime_version=local_jdk"
];
bazelRC = writeTextFile {
name = "bazel-rc";
text = ''
startup --server_javabase=${runJdk.home}
${lib.concatStringsSep "\n" (map (flag: "build ${flag}") bazelFlags)}
# load default location for the system wide configuration
try-import /etc/bazel.bazelrc
'';
};
prePatch = ''
rm -f .bazelversion
'';
postPatch =
let
# Workaround for https://github.com/NixOS/nixpkgs/issues/166205
nixpkgs166205ldflag = lib.optionalString stdenv.cc.isClang "-l${stdenv.cc.libcxx.cxxabi.libName}";
darwinPatches = ''
bazelLinkFlags () {
eval set -- "$NIX_LDFLAGS"
local flag
for flag in "$@"; do
printf ' -Wl,%s' "$flag"
done
}
# Explicitly configure gcov since we don't have it on Darwin, so autodetection fails
export GCOV=${coreutils}/bin/false
# Framework search paths aren't added by bintools hook
# https://github.com/NixOS/nixpkgs/pull/41914
export NIX_LDFLAGS+=" -F${CoreFoundation}/Library/Frameworks -F${CoreServices}/Library/Frameworks -F${Foundation}/Library/Frameworks -F${IOKit}/Library/Frameworks ${nixpkgs166205ldflag}"
# libcxx includes aren't added by libcxx hook
# https://github.com/NixOS/nixpkgs/pull/41589
export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -isystem ${lib.getDev libcxx}/include/c++/v1"
# for CLang 16 compatibility in external/upb dependency
export NIX_CFLAGS_COMPILE+=" -Wno-gnu-offsetof-extensions"
# This variable is used by bazel to propagate env vars for homebrew,
# which is exactly what we need too.
export HOMEBREW_RUBY_PATH="foo"
# don't use system installed Xcode to run clang, use Nix clang instead
sed -i -E \
-e "s;/usr/bin/xcrun (--sdk macosx )?clang;${stdenv.cc}/bin/clang $NIX_CFLAGS_COMPILE $(bazelLinkFlags) -framework CoreFoundation;g" \
-e "s;/usr/bin/codesign;CODESIGN_ALLOCATE=${cctools}/bin/${cctools.targetPrefix}codesign_allocate ${sigtool}/bin/codesign;" \
scripts/bootstrap/compile.sh \
tools/osx/BUILD
# nixpkgs's libSystem cannot use pthread headers directly, must import GCD headers instead
sed -i -e "/#include <pthread\/spawn.h>/i #include <dispatch/dispatch.h>" src/main/cpp/blaze_util_darwin.cc
# XXX: What do these do ?
sed -i -e 's;"/usr/bin/libtool";_find_generic(repository_ctx, "libtool", "LIBTOOL", overriden_tools);g' tools/cpp/unix_cc_configure.bzl
wrappers=( tools/cpp/osx_cc_wrapper.sh.tpl )
for wrapper in "''${wrappers[@]}"; do
sedVerbose $wrapper \
-e "s,/usr/bin/xcrun install_name_tool,${cctools}/bin/install_name_tool,g"
done
'';
genericPatches = ''
# md5sum is part of coreutils
sed -i 's|/sbin/md5|md5sum|g' src/BUILD third_party/ijar/test/testenv.sh
echo
echo "Substituting */bin/* hardcoded paths in src/main/java/com/google/devtools"
# Prefilter the files with grep for speed
grep -rlZ /bin/ \
src/main/java/com/google/devtools \
src/main/starlark/builtins_bzl/common/python \
tools \
| while IFS="" read -r -d "" path; do
# If you add more replacements here, you must change the grep above!
# Only files containing /bin are taken into account.
sedVerbose "$path" \
-e 's!/usr/local/bin/bash!${bashWithDefaultShellUtils}/bin/bash!g' \
-e 's!/usr/bin/bash!${bashWithDefaultShellUtils}/bin/bash!g' \
-e 's!/bin/bash!${bashWithDefaultShellUtils}/bin/bash!g' \
-e 's!/usr/bin/env bash!${bashWithDefaultShellUtils}/bin/bash!g' \
-e 's!/usr/bin/env python2!${python3}/bin/python!g' \
-e 's!/usr/bin/env python!${python3}/bin/python!g' \
-e 's!/usr/bin/env!${coreutils}/bin/env!g' \
-e 's!/bin/true!${coreutils}/bin/true!g'
done
# append the PATH with defaultShellPath in tools/bash/runfiles/runfiles.bash
echo "PATH=\$PATH:${defaultShellPath}" >> runfiles.bash.tmp
cat tools/bash/runfiles/runfiles.bash >> runfiles.bash.tmp
mv runfiles.bash.tmp tools/bash/runfiles/runfiles.bash
patchShebangs . >/dev/null
'';
in
''
function sedVerbose() {
local path=$1; shift;
sed -i".bak-nix" "$path" "$@"
diff -U0 "$path.bak-nix" "$path" | sed "s/^/ /" || true
rm -f "$path.bak-nix"
}
''
+ lib.optionalString stdenv.hostPlatform.isDarwin darwinPatches
+ genericPatches;
buildInputs = [ buildJdk bashWithDefaultShellUtils ] ++ defaultShellUtils;
nativeBuildInputs = [
installShellFiles
makeWrapper
python3
unzip
which
zip
python3.pkgs.absl-py # Needed to build fish completion
] ++ lib.optionals stdenv.isDarwin [
cctools
libcxx
Foundation
CoreFoundation
CoreServices
];
in
buildBazelPackage {
inherit src version;
pname = "bazel";
bazel = bazel_7;
bazelTargets = [ "//src:bazel_nojdk" ];
# we only need this to fetch deps needed to run tests in installCheckPhase
bazelTestTargets = [
# "//examples/cpp:hello-success_test"
# "//examples/java-native/src/test/java/com/example/myproject:hello"
];
dontAddBazelOpts = true;
bazelFlags = bazelFlags ++ [
"--enable_bzlmod"
"--lockfile_mode=update"
"--loading_phase_threads=HOST_CPUS"
];
bazelBuildFlags = [
"-c opt"
"--extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain"
# add version information to the build
"--stamp"
"--embed_label='${version}- (@${lib.substring 0 7 rev})'"
];
removeRulesCC = false;
removeLocalConfigCc = false;
removeLocal = false;
fetchConfigured = true;
fetchAttrs = {
inherit prePatch postPatch nativeBuildInputs buildInputs;
sha256 = deps-hash;
preInstall = ''
mv $bazelUserRoot $bazelOut/external/cache
mv ./MODULE.bazel.lock $bazelOut/external/MODULE.bazel.lock
'';
};
buildAttrs = {
inherit prePatch postPatch nativeBuildInputs buildInputs;
postConfigure = ''
mkdir -p "$bazelUserRoot"
mv $bazelOut/external/cache $bazelUserRoot/
mv $bazelOut/external/MODULE.bazel.lock MODULE.bazel.lock
'';
# see nixpkgs derivation
patches = [
"${bazel_path}/bazel_7/java_toolchain.patch"
"${bazel_path}/bazel_7/darwin_sleep.patch"
"${bazel_path}/bazel_7/xcode_locator.patch"
"${bazel_path}/trim-last-argument-to-gcc-if-empty.patch"
(substituteAll {
src = "${bazel_path}/strict_action_env.patch";
strictActionEnvPatch = defaultShellPath;
})
(substituteAll {
src = "${bazel_path}/bazel_rc.patch";
bazelSystemBazelRCPath = bazelRC;
})
] ++ lib.optional enableNixHacks "${bazel_path}/nix-hacks.patch";
# see nixpkgs derivation
__darwinAllowLocalNetworking = true;
postBuild = ''
echo "Generate bazel completions"
mkdir -p completion
${bash}/bin/bash ./scripts/generate_bash_completion.sh \
--bazel=bazel \
--output=./completion/bazel-complete.bash \
--prepend=./scripts/bazel-complete-header.bash \
--prepend=./scripts/bazel-complete-template.bash
${python3}/bin/python3 ./scripts/generate_fish_completion.py \
--bazel=bazel \
--output=./completion/bazel-complete.fish
'';
installPhase = ''
mkdir -p $out/bin
mv bazel-bin/src/bazel_nojdk $out/bin/bazel
installShellCompletion --bash \
--name bazel.bash \
./completion/bazel-complete.bash
installShellCompletion --zsh \
--name _bazel \
./scripts/zsh_completion/_bazel
installShellCompletion --fish \
--name bazel.fish \
./completion/bazel-complete.fish
'';
# see nixpkgs derivation
doInstallCheck = false;
installCheckPhase = ''
export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
export TEST_TMPDIR=$(pwd)
# we don't use scripts/packages/bazel.sh wrapper, which means we don't
# need to test this as in nixpkgs derivation
$out/bin/bazel \
--batch \
--output_base="$bazelOut" \
--output_user_root="$bazelUserRoot" \
test \
--test_output=errors \
examples/cpp:hello-success_test \
examples/java-native/src/test/java/com/example/myproject:hello
## Test that the GSON serialisation files are present
gson_classes=$(unzip -l $(bazel info install_base)/A-server.jar | grep GsonTypeAdapter.class | wc -l)
if [ "$gson_classes" -lt 10 ]; then
echo "Missing GsonTypeAdapter classes in A-server.jar. Lockfile generation will not work"
exit 1
fi
runHook postInstall
'';
postFixup = ''
mkdir -p $out/nix-support
echo "${bashWithDefaultShellUtils}" >> $out/nix-support/depends
echo "${bazelRC}" >> $out/nix-support/depends
'' + lib.optionalString stdenv.isDarwin ''
echo "${cctools}" >> $out/nix-support/depends
'';
dontStrip = true;
dontPatchELF = true;
};
}