From a8f174ff7ceb3228175eed982ad1e86749654b8c Mon Sep 17 00:00:00 2001 From: Wael Nasreddine Date: Thu, 25 Jul 2019 02:43:45 -0700 Subject: buildBazelPackage: autodetect nix toolchain instead of Xcode on Darwin (#65308) * buildBazelPackage: autodetect nix toolchain instead of Xcode on Darwin * do not export the variables outside of Darwin * remove unecessary parens * move comment within the darwin check --- pkgs/build-support/build-bazel-package/default.nix | 37 +++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'pkgs') diff --git a/pkgs/build-support/build-bazel-package/default.nix b/pkgs/build-support/build-bazel-package/default.nix index 4e19b244f3c..72725f9818c 100644 --- a/pkgs/build-support/build-bazel-package/default.nix +++ b/pkgs/build-support/build-bazel-package/default.nix @@ -1,4 +1,8 @@ -{ stdenv, bazel, cacert }: +{ stdenv +, bazel +, cacert +, lib +}: args@{ name, bazelFlags ? [], bazelTarget, buildAttrs, fetchAttrs, ... }: @@ -109,6 +113,31 @@ in stdenv.mkDerivation (fBuildAttrs // { buildPhase = fBuildAttrs.buildPhase or '' runHook preBuild + '' + 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 + '' + '' + BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \ USER=homeless-shelter \ bazel \ @@ -116,6 +145,12 @@ in stdenv.mkDerivation (fBuildAttrs // { --output_user_root="$bazelUserRoot" \ build \ -j $NIX_BUILD_CORES \ + '' + lib.optionalString stdenv.isDarwin '' + "''${copts[@]}" \ + "''${host_copts[@]}" \ + "''${linkopts[@]}" \ + "''${host_linkopts[@]}" \ + '' + '' $bazelFlags \ $bazelTarget -- cgit 1.4.1