Skip to content

Commit

Permalink
bazel_7: fix protobuf test on darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
layus committed Dec 14, 2023
1 parent a30a2c9 commit 459e800
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
20 changes: 5 additions & 15 deletions pkgs/development/tools/build-managers/bazel/bazel_7/cpp-test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ let
export LD='${darwin.cctools}/bin/ld'
export LIBTOOL='${darwin.cctools}/bin/libtool'
export CC='${stdenv.cc}/bin/clang'
export BAZEL_LINKLIBS='-lstdc++:-lm'
export BAZEL_LINKOPTS='-x:c++'
# XXX: hack for macosX, this flags disable bazel usage of xcode
# See: https://github.com/bazelbuild/bazel/issues/4231
Expand All @@ -67,33 +65,25 @@ let
inherit workspaceDir;
bazelPkg = bazel;
bazelScript = ''
set -x
env | grep c..abi
${bazel}/bin/bazel build //... \
--enable_bzlmod \
--verbose_failures \
--repository_cache=${mergedDistDir} \
--action_env=NIX_DEBUG=1 \
--announce_rc \
--curses=no \
'' + lib.optionalString (stdenv.isDarwin) ''
--repo_env=BAZEL_LINKLIBS \
--repo_env=BAZEL_LINKOPTS \
--cxxopt=-x --cxxopt=c++ \
--host_cxxopt=-x --host_cxxopt=c++ \
--action_env=NIX_CFLAGS_COMPILE \
--action_env=NIX_LDFLAGS \
'' + lib.optionalString (stdenv.cc.isClang && stdenv ? cc.libcxx.cxxabi.libName) ''
--linkopt=-Wl,-l${stdenv.cc.libcxx.cxxabi.libName} \
--linkopt=-L${stdenv.cc.libcxx.cxxabi}/lib \
--host_linkopt=-Wl,-l${stdenv.cc.libcxx.cxxabi.libName} \
--host_linkopt=-L${stdenv.cc.libcxx.cxxabi}/lib \
'' + lib.optionalString (stdenv.isDarwin && Foundation != null) ''
--linkopt=-Wl,-F${Foundation}/Library/Frameworks \
--linkopt=-L${darwin.libobjc}/lib \
'' + ''
'';
#'' + lib.optionalString (stdenv.isDarwin) ''
# --repo_env=BAZEL_LINKLIBS='-lstdc++:-lm' \
#'' + lib.optionalString (stdenv.isDarwin) ''
# --cxxopt=-x --cxxopt=c++ --host_cxxopt=-x --host_cxxopt=c++ \
# --linkopt=-Wl,-lstdc++ --host_linkopt=-Wl,-lstdc++ \
};

in testBazel
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
{ bazel
, Foundation
, bazelTest
, fetchFromGitHub
, fetchurl
, stdenv
, callPackage
, darwin
, distDir
, extraBazelArgs ? ""
, fetchFromGitHub
, fetchurl
, jdk11_headless
, lib
, libtool
, lndir
, openjdk8
, jdk11_headless
, repoCache
, runLocal
, runtimeShell
, stdenv
, symlinkJoin
, tree
, writeScript
, writeText
, distDir
, Foundation
, callPackage
, libtool
, lndir
, repoCache
, tree
}:

# This test uses bzlmod because I could not make it work without it.
Expand All @@ -35,7 +36,7 @@ let
# [nix-shell]$ cp MODULE.bazel.lock $HERE/protobuf-test.MODULE.bazel.lock
lockfile = ./protobuf-test.MODULE.bazel.lock;

protocbufRepoCache = callPackage ./bazel-repository-cache.nix {
protobufRepoCache = callPackage ./bazel-repository-cache.nix {
# We are somewhat lucky that bazel's own lockfile works for our tests.
# Use extraDeps if the tests need things that are not in that lockfile.
# But most test dependencies are bazel's builtin deps, so that at least aligns.
Expand All @@ -46,6 +47,11 @@ let
null == builtins.match ".*(macos|osx|linux|win|android|maven).*" name;
};

mergedRepoCache = symlinkJoin {
name = "mergedDistDir";
paths = [ protobufRepoCache distDir ];
};

MODULE = writeText "MODULE.bazel" ''
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
bazel_dep(name = "protobuf", version = "21.7")
Expand Down Expand Up @@ -137,29 +143,26 @@ let
];

bazelScript = ''
# Augment bundled repository_cache with our extra paths
mkdir -p $PWD/.repository_cache/content_addressable
${lndir}/bin/lndir ${repoCache}/content_addressable \
$PWD/.repository_cache/content_addressable
${lndir}/bin/lndir ${protocbufRepoCache}/content_addressable \
$PWD/.repository_cache/content_addressable
tree $PWD/.repository_cache
${bazel}/bin/bazel \
build \
--repository_cache=$PWD/.repository_cache \
--repository_cache=${mergedRepoCache} \
${extraBazelArgs} \
--enable_bzlmod \
--verbose_failures \
//... \
'' + lib.optionalString (lib.strings.versionOlder bazel.version "5.0.0") ''
--host_javabase='@local_jdk//:jdk' \
--java_toolchain='@bazel_tools//tools/jdk:toolchain_hostjdk8' \
--javabase='@local_jdk//:jdk' \
--host_javabase='@local_jdk//:jdk' \
--java_toolchain='@bazel_tools//tools/jdk:toolchain_hostjdk8' \
--javabase='@local_jdk//:jdk' \
'' + lib.optionalString (stdenv.isDarwin) ''
--cxxopt=-x --cxxopt=c++ --host_cxxopt=-x --host_cxxopt=c++ \
--linkopt=-stdlib=libc++ --host_linkopt=-stdlib=libc++ \
--cxxopt=-x --cxxopt=c++ --host_cxxopt=-x --host_cxxopt=c++ \
'' + lib.optionalString (stdenv.cc.isClang && stdenv ? cc.libcxx.cxxabi.libName) ''
--linkopt=-Wl,-l${stdenv.cc.libcxx.cxxabi.libName} \
--linkopt=-L${stdenv.cc.libcxx.cxxabi}/lib \
--host_linkopt=-Wl,-l${stdenv.cc.libcxx.cxxabi.libName} \
--host_linkopt=-L${stdenv.cc.libcxx.cxxabi}/lib \
'' + ''
'';
};

Expand Down
26 changes: 16 additions & 10 deletions pkgs/development/tools/build-managers/bazel/bazel_7/tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,32 @@ let
hash = "sha256-DaPKp7Sn5uvfZRjdDx6grot3g3B7trqCyL0TRIdwg98=";
};

callBazelTests = args:
callBazelTests = bazel:
let
callBazelTest = newScope {
inherit runLocal bazelTest bazel-examples;
inherit Foundation;
bazel = bazel_self;
inherit bazel;
distDir = testsDistDir;
extraBazelArgs = "";
repoCache = testsRepoCache;
};
in
recurseIntoAttrs (
(lib.optionalAttrs (!isDarwin) {
# `extracted` doesn’t work on darwin
shebang = callBazelTest ../shebang-test.nix (args // { inherit extracted; });
shebang = callBazelTest ../shebang-test.nix {
inherit extracted;
extraBazelArgs = "--noenable_bzlmod";
};
}) // {
bashTools = callBazelTest ../bash-tools-test.nix args;
cpp = callBazelTest ./cpp-test.nix args;
java = callBazelTest ../java-test.nix args;
pythonBinPath = callBazelTest ../python-bin-path-test.nix args;
protobuf = callBazelTest ./protobuf-test.nix (args // { repoCache = testsRepoCache; });
bashTools = callBazelTest ../bash-tools-test.nix { };
cpp = callBazelTest ./cpp-test.nix { };
java = callBazelTest ../java-test.nix {
extraBazelArgs = "--noenable_bzlmod";
};
pythonBinPath = callBazelTest ../python-bin-path-test.nix { };
protobuf = callBazelTest ./protobuf-test.nix { };
}
);

Expand All @@ -156,8 +162,8 @@ recurseIntoAttrs {
distDir = testsDistDir;
testsRepoCache = testsRepoCache;

vanilla = callBazelTests { };
withNixHacks = callBazelTests { bazel = bazelWithNixHacks; };
vanilla = callBazelTests bazel_self;
withNixHacks = callBazelTests bazelWithNixHacks;

# add some downstream packages using buildBazelPackage
downstream = recurseIntoAttrs ({
Expand Down

0 comments on commit 459e800

Please sign in to comment.