about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/tools/build-managers/bazel/bazel_7/protobuf-test.nix
blob: d50de32d4a3e8965e6c40a17bf1191d25d34f11a (plain) (blame)
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
{ bazel
, Foundation
, bazelTest
, callPackage
, darwin
, distDir
, extraBazelArgs ? ""
, fetchFromGitHub
, fetchurl
, jdk11_headless
, lib
, libtool
, lndir
, openjdk8
, repoCache
, runLocal
, runtimeShell
, stdenv
, symlinkJoin
, tree
, writeScript
, writeText
}:

# This test uses bzlmod because I could not make it work without it.
# This is good, because we have at least one test with bzlmod enabled.
# However, we have to create our own lockfile, wich is quite a big file by
# itself.

let
  # To update the lockfile, run
  #    $ nix-shell -A bazel_7.tests.vanilla.protobuf
  #    [nix-shell]$ genericBuild # (wait a bit for failure, or kill it)
  #    [nix-shell]$ rm -f MODULE.bazel.lock
  #    [nix-shell]$ bazel mod deps --lockfile_mode=update
  #    [nix-shell]$ cp MODULE.bazel.lock $HERE/protobuf-test.MODULE.bazel.lock
  lockfile = ./protobuf-test.MODULE.bazel.lock;

  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.
    inherit lockfile;

    # Remove platform-specific binaries, as they are large and useless.
    requiredDepNamePredicate = name:
      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")
    bazel_dep(name = "zlib", version = "1.3")
  '';

  WORKSPACE = writeText "WORKSPACE" ''
    # Empty, we use bzlmod instead
  '';

  personProto = writeText "person.proto" ''
    syntax = "proto3";

    package person;

    message Person {
      string name = 1;
      int32 id = 2;
      string email = 3;
    }
  '';

  personBUILD = writeText "BUILD" ''
    load("@rules_proto//proto:defs.bzl", "proto_library")

    proto_library(
        name = "person_proto",
        srcs = ["person.proto"],
        visibility = ["//visibility:public"],
    )

    java_proto_library(
        name = "person_java_proto",
        deps = [":person_proto"],
    )

    cc_proto_library(
        name = "person_cc_proto",
        deps = [":person_proto"],
    )
  '';

  toolsBazel = writeScript "bazel" ''
    #! ${runtimeShell}

    export CXX='${stdenv.cc}/bin/clang++'
    export LD='${darwin.cctools}/bin/ld'
    export LIBTOOL='${darwin.cctools}/bin/libtool'
    export CC='${stdenv.cc}/bin/clang'

    # XXX: hack for macosX, this flags disable bazel usage of xcode
    # See: https://github.com/bazelbuild/bazel/issues/4231
    export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1

    export HOMEBREW_RUBY_PATH="foo"

    exec "$BAZEL_REAL" "$@"
  '';

  workspaceDir = runLocal "our_workspace" { } (''
    mkdir $out
    cp ${MODULE} $out/MODULE.bazel
    cp ${./protobuf-test.MODULE.bazel.lock} $out/MODULE.bazel.lock
    #cp ${WORKSPACE} $out/WORKSPACE
    touch $out/WORKSPACE
    touch $out/BUILD.bazel
    mkdir $out/person
    cp ${personProto} $out/person/person.proto
    cp ${personBUILD} $out/person/BUILD.bazel
  ''
  + (lib.optionalString stdenv.isDarwin ''
    echo 'tools bazel created'
    mkdir $out/tools
    install ${toolsBazel} $out/tools/bazel
  ''));

  testBazel = bazelTest {
    name = "bazel-test-protocol-buffers";
    inherit workspaceDir;
    bazelPkg = bazel;
    buildInputs = [
      (if lib.strings.versionOlder bazel.version "5.0.0" then openjdk8 else jdk11_headless)
      tree
      bazel
    ]
    ++ lib.optionals stdenv.hostPlatform.isDarwin [
      Foundation
      darwin.objc4
    ];

    bazelScript = ''
      ${bazel}/bin/bazel \
        build \
        --repository_cache=${mergedRepoCache} \
        ${extraBazelArgs} \
        --enable_bzlmod \
        --lockfile_mode=error \
        --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' \
    '' + lib.optionalString (stdenv.isDarwin) ''
        --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 \
    '' + ''

    '';
  };

in
testBazel