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
|
# This BUILD file shows how to use protobuf with bazel. Before you can use
# proto_library/<lang>_proto_library rules in a BUILD file, you need to
# include protobuf repo as remote repositories in your WORKSPACE file. See
# the WORKSPACE file in the same directory with this BUILD file for an
# example.
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@com_google_protobuf//bazel:java_lite_proto_library.bzl", "java_lite_proto_library")
load("@com_google_protobuf//bazel:java_proto_library.bzl", "java_proto_library")
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
load("@com_google_protobuf//bazel:py_proto_library.bzl", "py_proto_library")
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@rules_java//java:java_binary.bzl", "java_binary")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_python//python:py_binary.bzl", "py_binary")
# cc_proto_library is intentionally not loaded, to test Bazel's built-in implementation
# against Protobuf's implementation (already used building protoc)
# For each .proto file, a proto_library target should be defined. This target
# is not bound to any particular language. Instead, it defines the dependency
# graph of the .proto files (i.e., proto imports) and serves as the provider
# of .proto source files to the protocol compiler.
#
# Remote repository "protobuf" must be defined to use this rule.
proto_library(
name = "addressbook_proto",
srcs = ["addressbook.proto"],
deps = ["@com_google_protobuf//:timestamp_proto"],
)
# The cc_proto_library rule generates C++ code for a proto_library rule. It
# must have exactly one proto_library dependency. If you want to use multiple
# proto_library targets, create a separate cc_proto_library target for each
# of them.
#
# Remote repository "com_google_protobuf_cc" must be defined to use this rule.
cc_proto_library(
name = "addressbook_cc_proto",
deps = [":addressbook_proto"],
)
# cc_library/cc_binary targets can depend on cc_proto_library targets.
cc_binary(
name = "add_person_cpp",
srcs = ["add_person.cc"],
deps = [
":addressbook_cc_proto",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//src/google/protobuf/util:time_util",
],
)
cc_binary(
name = "list_people_cpp",
srcs = ["list_people.cc"],
deps = [
":addressbook_cc_proto",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//src/google/protobuf/util:time_util",
],
)
# Similar to cc_proto_library but for Java.
#
# Remote repository "com_google_protobuf_java" must be defined to use this rule.
java_proto_library(
name = "addressbook_java_proto",
deps = [":addressbook_proto"],
)
java_binary(
name = "add_person_java",
srcs = ["AddPerson.java"],
main_class = "AddPerson",
deps = [
":addressbook_java_proto",
"@com_google_protobuf//java/util",
],
)
java_binary(
name = "list_people_java",
srcs = ["ListPeople.java"],
main_class = "ListPeople",
deps = [":addressbook_java_proto"],
)
# Java lite.
#
# Remote repository "com_google_protobuf_javalite" must be defined to use this
# rule.
java_lite_proto_library(
name = "addressbook_java_lite_proto",
deps = [":addressbook_proto"],
)
# Java lite API is a subset of the regular Java API so if you only uses this
# subset in your code, you can actually compile your code against both (i.e.,
# share code between server build and Android build).
#
# The lite version has a smaller code size, and you can see that by comparing
# the resulted .jar file:
#
# $ bazel build :add_person_java_deploy.jar :add_person_java_lite_deploy.jar
# $ ls -l bazel-bin/*_deploy.jar
# -r-xr-xr-x 1 xiaofeng eng 1230797 Sep 8 12:24 bazel-bin/add_person_java_deploy.jar
# -r-xr-xr-x 1 xiaofeng eng 236166 Sep 8 12:24 bazel-bin/add_person_java_lite_deploy.jar
#
# In the above example, the lite .jar file is 6 times smaller. With proper
# proguard inlining/stripping, the difference can be much more larger than
# that.
java_binary(
name = "add_person_java_lite",
srcs = ["AddPerson.java"],
main_class = "AddPerson",
deps = [
":addressbook_java_lite_proto",
"@com_google_protobuf//java/util",
],
)
java_binary(
name = "list_people_java_lite",
srcs = ["ListPeople.java"],
main_class = "ListPeople",
deps = [":addressbook_java_lite_proto"],
)
# Python
py_proto_library(
name = "addressbook_py_pb2",
visibility = ["//visibility:public"],
deps = [":addressbook_proto"],
)
py_binary(
name = "add_person",
srcs = ["add_person.py"],
python_version = "PY3",
deps = [
":addressbook_py_pb2",
],
)
py_binary(
name = "list_people",
srcs = ["list_people.py"],
python_version = "PY3",
deps = [
":addressbook_py_pb2",
],
)
build_test(
name = "test",
targets = [
":add_person_cpp",
":add_person_java",
":add_person", # Python
],
)
# Files included in all source distributions
pkg_files(
name = "dist_files",
srcs = [
"AddPerson.java",
"BUILD.bazel",
"CMakeLists.txt",
"ListPeople.java",
"Makefile",
"README.md",
"WORKSPACE",
"add_person.cc",
"add_person.dart",
"add_person.py",
"addressbook.proto",
"go/cmd/add_person/add_person.go",
"go/cmd/add_person/add_person_test.go",
"go/cmd/list_people/list_people.go",
"go/cmd/list_people/list_people_test.go",
"go/go.mod",
"go/go.sum",
"list_people.cc",
"list_people.dart",
"list_people.py",
"pubspec.yaml",
],
prefix = "examples/",
strip_prefix = strip_prefix.from_root(""),
visibility = ["//visibility:public"],
)
platform(
name = "x64_windows-clang-cl",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
# This is necessary for Bazel 7 compatibility with a MODULE.bazel file that still works in
# Bazel 8. Using cc_configure_extension from rules_cc produces a @local_config_cc
# repository that's not compatible with @bazel_tools//tools/cpp:clang-cl from before
# Bazel 8. See https://github.com/bazelbuild/rules_cc/issues/330.
"@rules_cc//cc/private/toolchain:clang-cl",
],
)
|