2019-07-25 11:43:45 +02:00
|
|
|
{ stdenv
|
|
|
|
, bazel
|
|
|
|
, cacert
|
|
|
|
, lib
|
|
|
|
}:
|
2017-11-10 18:06:26 +01:00
|
|
|
|
2019-07-18 13:49:00 +02:00
|
|
|
args@{ name, bazelFlags ? [], bazelBuildFlags ? [], bazelFetchFlags ? [], bazelTarget, buildAttrs, fetchAttrs, ... }:
|
2017-11-10 18:06:26 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
fArgs = removeAttrs args [ "buildAttrs" "fetchAttrs" ];
|
|
|
|
fBuildAttrs = fArgs // buildAttrs;
|
|
|
|
fFetchAttrs = fArgs // removeAttrs fetchAttrs [ "sha256" ];
|
|
|
|
|
|
|
|
in stdenv.mkDerivation (fBuildAttrs // {
|
2019-07-18 13:49:00 +02:00
|
|
|
inherit name bazelFlags bazelBuildFlags bazelFetchFlags bazelTarget;
|
2017-11-10 18:06:26 +01:00
|
|
|
|
|
|
|
deps = stdenv.mkDerivation (fFetchAttrs // {
|
|
|
|
name = "${name}-deps";
|
2019-07-18 13:49:00 +02:00
|
|
|
inherit bazelFlags bazelBuildFlags bazelFetchFlags bazelTarget;
|
2017-11-10 18:06:26 +01:00
|
|
|
|
|
|
|
nativeBuildInputs = fFetchAttrs.nativeBuildInputs or [] ++ [ bazel ];
|
|
|
|
|
|
|
|
preHook = fFetchAttrs.preHook or "" + ''
|
2018-07-10 00:38:45 +02:00
|
|
|
export bazelOut="$(echo ''${NIX_BUILD_TOP}/output | sed -e 's,//,/,g')"
|
|
|
|
export bazelUserRoot="$(echo ''${NIX_BUILD_TOP}/tmp | sed -e 's,//,/,g')"
|
2017-11-10 18:06:26 +01:00
|
|
|
export HOME="$NIX_BUILD_TOP"
|
2019-02-01 07:30:36 +01:00
|
|
|
# This is needed for git_repository with https remotes
|
|
|
|
export GIT_SSL_CAINFO="${cacert}/etc/ssl/certs/ca-bundle.crt"
|
2017-11-10 18:06:26 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = fFetchAttrs.buildPhase or ''
|
|
|
|
runHook preBuild
|
|
|
|
|
2018-09-11 07:30:22 +02:00
|
|
|
# Bazel computes the default value of output_user_root before parsing the
|
|
|
|
# flag. The computation of the default value involves getting the $USER
|
|
|
|
# from the environment. I don't have that variable when building with
|
|
|
|
# sandbox enabled. Code here
|
|
|
|
# https://github.com/bazelbuild/bazel/blob/9323c57607d37f9c949b60e293b573584906da46/src/main/cpp/startup_options.cc#L123-L124
|
|
|
|
#
|
2019-02-19 17:44:05 +01:00
|
|
|
# On macOS Bazel will use the system installed Xcode or CLT toolchain instead of the one in the PATH unless we pass BAZEL_USE_CPP_ONLY_TOOLCHAIN
|
2019-06-20 20:01:22 +02:00
|
|
|
|
|
|
|
# We disable multithreading for the fetching phase since it can lead to timeouts with many dependencies/threads:
|
|
|
|
# https://github.com/bazelbuild/bazel/issues/6502
|
2019-07-13 21:40:22 +02:00
|
|
|
BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \
|
|
|
|
USER=homeless-shelter \
|
|
|
|
bazel \
|
|
|
|
--output_base="$bazelOut" \
|
|
|
|
--output_user_root="$bazelUserRoot" \
|
|
|
|
fetch \
|
|
|
|
--loading_phase_threads=1 \
|
|
|
|
$bazelFlags \
|
2019-07-18 13:49:00 +02:00
|
|
|
$bazelFetchFlags \
|
2019-07-13 21:40:22 +02:00
|
|
|
$bazelTarget
|
2017-11-10 18:06:26 +01:00
|
|
|
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = fFetchAttrs.installPhase or ''
|
|
|
|
runHook preInstall
|
|
|
|
|
2018-09-11 07:36:11 +02:00
|
|
|
# Remove all built in external workspaces, Bazel will recreate them when building
|
|
|
|
rm -rf $bazelOut/external/{bazel_tools,\@bazel_tools.marker}
|
2019-10-01 23:41:03 +02:00
|
|
|
rm -rf $bazelOut/external/{rules_cc,\@rules_cc.marker}
|
2018-09-11 07:36:11 +02:00
|
|
|
rm -rf $bazelOut/external/{embedded_jdk,\@embedded_jdk.marker}
|
2019-07-25 13:18:50 +02:00
|
|
|
rm -rf $bazelOut/external/{local_*,\@local_*.marker}
|
2018-09-11 07:36:11 +02:00
|
|
|
|
2019-07-25 13:18:50 +02:00
|
|
|
# Clear markers
|
|
|
|
find $bazelOut/external -name '@*\.marker' -exec sh -c 'echo > {}' \;
|
2018-09-11 07:36:11 +02:00
|
|
|
|
2018-09-11 23:12:31 +02:00
|
|
|
# Remove all vcs files
|
|
|
|
rm -rf $(find $bazelOut/external -type d -name .git)
|
|
|
|
rm -rf $(find $bazelOut/external -type d -name .svn)
|
|
|
|
rm -rf $(find $bazelOut/external -type d -name .hg)
|
2017-11-10 18:06:26 +01:00
|
|
|
|
2019-08-19 10:21:37 +02:00
|
|
|
# Removing top-level symlinks along with their markers.
|
|
|
|
# This is needed because they sometimes point to temporary paths (?).
|
|
|
|
# For example, in Tensorflow-gpu build:
|
|
|
|
# platforms -> NIX_BUILD_TOP/tmp/install/35282f5123611afa742331368e9ae529/_embedded_binaries/platforms
|
|
|
|
find $bazelOut/external -maxdepth 1 -type l | while read symlink; do
|
|
|
|
name="$(basename "$symlink")"
|
|
|
|
rm "$symlink" "$bazelOut/external/@$name.marker"
|
|
|
|
done
|
|
|
|
|
2018-09-11 07:36:11 +02:00
|
|
|
# Patching symlinks to remove build directory reference
|
|
|
|
find $bazelOut/external -type l | while read symlink; do
|
2019-06-20 18:35:16 +02:00
|
|
|
new_target="$(readlink "$symlink" | sed "s,$NIX_BUILD_TOP,NIX_BUILD_TOP,")"
|
|
|
|
rm "$symlink"
|
|
|
|
ln -sf "$new_target" "$symlink"
|
2018-09-11 07:36:11 +02:00
|
|
|
done
|
|
|
|
|
2017-11-10 18:06:26 +01:00
|
|
|
cp -r $bazelOut/external $out
|
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
|
|
|
dontFixup = true;
|
2019-07-13 21:40:22 +02:00
|
|
|
allowedRequisites = [];
|
|
|
|
|
2017-11-10 18:06:26 +01:00
|
|
|
outputHashMode = "recursive";
|
|
|
|
outputHashAlgo = "sha256";
|
|
|
|
outputHash = fetchAttrs.sha256;
|
|
|
|
});
|
|
|
|
|
2019-07-13 21:40:22 +02:00
|
|
|
nativeBuildInputs = fBuildAttrs.nativeBuildInputs or [] ++ [ (bazel.override { enableNixHacks = true; }) ];
|
2017-11-10 18:06:26 +01:00
|
|
|
|
|
|
|
preHook = fBuildAttrs.preHook or "" + ''
|
|
|
|
export bazelOut="$NIX_BUILD_TOP/output"
|
2018-07-10 00:38:45 +02:00
|
|
|
export bazelUserRoot="$NIX_BUILD_TOP/tmp"
|
2017-11-10 18:06:26 +01:00
|
|
|
export HOME="$NIX_BUILD_TOP"
|
|
|
|
'';
|
|
|
|
|
|
|
|
preConfigure = ''
|
2019-06-26 15:23:50 +02:00
|
|
|
mkdir -p "$bazelOut"
|
|
|
|
cp -r $deps $bazelOut/external
|
2017-11-10 18:06:26 +01:00
|
|
|
chmod -R +w $bazelOut
|
|
|
|
find $bazelOut -type l | while read symlink; do
|
|
|
|
ln -sf $(readlink "$symlink" | sed "s,NIX_BUILD_TOP,$NIX_BUILD_TOP,") "$symlink"
|
|
|
|
done
|
|
|
|
'' + fBuildAttrs.preConfigure or "";
|
|
|
|
|
|
|
|
buildPhase = fBuildAttrs.buildPhase or ''
|
|
|
|
runHook preBuild
|
|
|
|
|
2019-07-25 11:43:45 +02:00
|
|
|
'' + lib.optionalString stdenv.isDarwin ''
|
|
|
|
# Bazel sandboxes the execution of the tools it invokes, so even though we are
|
|
|
|
# calling the correct nix wrappers, the values of the environment variables
|
|
|
|
# the wrappers are expecting will not be set. So instead of relying on the
|
|
|
|
# wrappers picking them up, pass them in explicitly via `--copt`, `--linkopt`
|
|
|
|
# and related flags.
|
|
|
|
#
|
|
|
|
copts=()
|
|
|
|
host_copts=()
|
|
|
|
for flag in $NIX_CFLAGS_COMPILE; do
|
|
|
|
copts+=( "--copt=$flag" )
|
|
|
|
host_copts+=( "--host_copt=$flag" )
|
|
|
|
done
|
|
|
|
for flag in $NIX_CXXSTDLIB_COMPILE; do
|
|
|
|
copts+=( "--copt=$flag" )
|
|
|
|
host_copts+=( "--host_copt=$flag" )
|
|
|
|
done
|
|
|
|
linkopts=()
|
|
|
|
host_linkopts=()
|
|
|
|
for flag in $NIX_LD_FLAGS; do
|
|
|
|
linkopts+=( "--linkopt=$flag" )
|
|
|
|
host_linkopts+=( "--host_linkopt=$flag" )
|
|
|
|
done
|
|
|
|
'' + ''
|
|
|
|
|
2019-02-19 17:44:05 +01:00
|
|
|
BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \
|
|
|
|
USER=homeless-shelter \
|
|
|
|
bazel \
|
|
|
|
--output_base="$bazelOut" \
|
|
|
|
--output_user_root="$bazelUserRoot" \
|
|
|
|
build \
|
|
|
|
-j $NIX_BUILD_CORES \
|
2019-07-25 11:43:45 +02:00
|
|
|
'' + lib.optionalString stdenv.isDarwin ''
|
|
|
|
"''${copts[@]}" \
|
|
|
|
"''${host_copts[@]}" \
|
|
|
|
"''${linkopts[@]}" \
|
|
|
|
"''${host_linkopts[@]}" \
|
|
|
|
'' + ''
|
2019-02-19 17:44:05 +01:00
|
|
|
$bazelFlags \
|
2019-07-18 13:49:00 +02:00
|
|
|
$bazelBuildFlags \
|
2019-02-19 17:44:05 +01:00
|
|
|
$bazelTarget
|
2017-11-10 18:06:26 +01:00
|
|
|
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
})
|