• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# This BUILD file shows how to use protobuf with bazel. Before you can use
2# proto_library/<lang>_proto_library rules in a BUILD file, you need to
3# include protobuf repo as remote repositories in your WORKSPACE file. See
4# the WORKSPACE file in the same directory with this BUILD file for an
5# example.
6
7load("@bazel_skylib//rules:build_test.bzl", "build_test")
8load("@com_google_protobuf//bazel:java_lite_proto_library.bzl", "java_lite_proto_library")
9load("@com_google_protobuf//bazel:java_proto_library.bzl", "java_proto_library")
10load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
11load("@com_google_protobuf//bazel:py_proto_library.bzl", "py_proto_library")
12load("@rules_cc//cc:defs.bzl", "cc_binary")
13load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
14load("@rules_python//python:py_binary.bzl", "py_binary")
15
16# cc_proto_library is intentionally not loaded, to test Bazel's built-in implementation
17# against Protobuf's implementation (already used building protoc)
18
19# For each .proto file, a proto_library target should be defined. This target
20# is not bound to any particular language. Instead, it defines the dependency
21# graph of the .proto files (i.e., proto imports) and serves as the provider
22# of .proto source files to the protocol compiler.
23#
24# Remote repository "protobuf" must be defined to use this rule.
25proto_library(
26    name = "addressbook_proto",
27    srcs = ["addressbook.proto"],
28    deps = ["@com_google_protobuf//:timestamp_proto"],
29)
30
31# The cc_proto_library rule generates C++ code for a proto_library rule. It
32# must have exactly one proto_library dependency. If you want to use multiple
33# proto_library targets, create a separate cc_proto_library target for each
34# of them.
35#
36# Remote repository "com_google_protobuf_cc" must be defined to use this rule.
37cc_proto_library(
38    name = "addressbook_cc_proto",
39    deps = [":addressbook_proto"],
40)
41
42# cc_library/cc_binary targets can depend on cc_proto_library targets.
43cc_binary(
44    name = "add_person_cpp",
45    srcs = ["add_person.cc"],
46    deps = [
47        ":addressbook_cc_proto",
48        "@com_google_protobuf//:protobuf",
49        "@com_google_protobuf//src/google/protobuf/util:time_util",
50    ],
51)
52
53cc_binary(
54    name = "list_people_cpp",
55    srcs = ["list_people.cc"],
56    deps = [
57        ":addressbook_cc_proto",
58        "@com_google_protobuf//:protobuf",
59        "@com_google_protobuf//src/google/protobuf/util:time_util",
60    ],
61)
62
63# Similar to cc_proto_library but for Java.
64#
65# Remote repository "com_google_protobuf_java" must be defined to use this rule.
66java_proto_library(
67    name = "addressbook_java_proto",
68    deps = [":addressbook_proto"],
69)
70
71java_binary(
72    name = "add_person_java",
73    srcs = ["AddPerson.java"],
74    main_class = "AddPerson",
75    deps = [":addressbook_java_proto"],
76)
77
78java_binary(
79    name = "list_people_java",
80    srcs = ["ListPeople.java"],
81    main_class = "ListPeople",
82    deps = [":addressbook_java_proto"],
83)
84
85# Java lite.
86#
87# Remote repository "com_google_protobuf_javalite" must be defined to use this
88# rule.
89java_lite_proto_library(
90    name = "addressbook_java_lite_proto",
91    deps = [":addressbook_proto"],
92)
93
94# Java lite API is a subset of the regular Java API so if you only uses this
95# subset in your code, you can actually compile your code against both (i.e.,
96# share code between server build and Android build).
97#
98# The lite version has a smaller code size, and you can see that by comparing
99# the resulted .jar file:
100#
101#   $ bazel build :add_person_java_deploy.jar :add_person_java_lite_deploy.jar
102#   $ ls -l bazel-bin/*_deploy.jar
103#   -r-xr-xr-x 1 xiaofeng eng 1230797 Sep  8 12:24 bazel-bin/add_person_java_deploy.jar
104#   -r-xr-xr-x 1 xiaofeng eng  236166 Sep  8 12:24 bazel-bin/add_person_java_lite_deploy.jar
105#
106# In the above example, the lite .jar file is 6 times smaller. With proper
107# proguard inlining/stripping, the difference can be much more larger than
108# that.
109java_binary(
110    name = "add_person_java_lite",
111    srcs = ["AddPerson.java"],
112    main_class = "AddPerson",
113    deps = [":addressbook_java_lite_proto"],
114)
115
116java_binary(
117    name = "list_people_java_lite",
118    srcs = ["ListPeople.java"],
119    main_class = "ListPeople",
120    deps = [":addressbook_java_lite_proto"],
121)
122
123# Python
124
125py_proto_library(
126    name = "addressbook_py_pb2",
127    visibility = ["//visibility:public"],
128    deps = [":addressbook_proto"],
129)
130
131py_binary(
132    name = "add_person",
133    srcs = ["add_person.py"],
134    python_version = "PY3",
135    deps = [
136        ":addressbook_py_pb2",
137    ],
138)
139
140py_binary(
141    name = "list_people",
142    srcs = ["list_people.py"],
143    python_version = "PY3",
144    deps = [
145        ":addressbook_py_pb2",
146    ],
147)
148
149build_test(
150    name = "test",
151    targets = [
152        ":add_person_cpp",
153        ":add_person_java",
154        ":add_person",  # Python
155    ],
156)
157
158# Files included in all source distributions
159pkg_files(
160    name = "dist_files",
161    srcs = [
162        "AddPerson.java",
163        "BUILD.bazel",
164        "CMakeLists.txt",
165        "ListPeople.java",
166        "Makefile",
167        "README.md",
168        "WORKSPACE",
169        "add_person.cc",
170        "add_person.dart",
171        "add_person.py",
172        "addressbook.proto",
173        "go/cmd/add_person/add_person.go",
174        "go/cmd/add_person/add_person_test.go",
175        "go/cmd/list_people/list_people.go",
176        "go/cmd/list_people/list_people_test.go",
177        "go/go.mod",
178        "go/go.sum",
179        "list_people.cc",
180        "list_people.dart",
181        "list_people.py",
182        "pubspec.yaml",
183    ],
184    prefix = "examples/",
185    strip_prefix = strip_prefix.from_root(""),
186    visibility = ["//visibility:public"],
187)
188