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("@rules_cc//cc:defs.bzl", "cc_binary", "cc_proto_library") 8load("@rules_java//java:defs.bzl", "java_binary", "java_lite_proto_library", "java_proto_library") 9load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") 10load("@rules_proto//proto:defs.bzl", "proto_library") 11 12# For each .proto file, a proto_library target should be defined. This target 13# is not bound to any particular language. Instead, it defines the dependency 14# graph of the .proto files (i.e., proto imports) and serves as the provider 15# of .proto source files to the protocol compiler. 16# 17# Remote repository "com_google_protobuf" must be defined to use this rule. 18proto_library( 19 name = "addressbook_proto", 20 srcs = ["addressbook.proto"], 21 deps = ["@com_google_protobuf//:timestamp_proto"], 22) 23 24# The cc_proto_library rule generates C++ code for a proto_library rule. It 25# must have exactly one proto_library dependency. If you want to use multiple 26# proto_library targets, create a separate cc_proto_library target for each 27# of them. 28# 29# Remote repository "com_google_protobuf_cc" must be defined to use this rule. 30cc_proto_library( 31 name = "addressbook_cc_proto", 32 deps = [":addressbook_proto"], 33) 34 35# cc_library/cc_binary targets can depend on cc_proto_library targets. 36cc_binary( 37 name = "add_person_cpp", 38 srcs = ["add_person.cc"], 39 deps = [":addressbook_cc_proto"], 40) 41 42cc_binary( 43 name = "list_people_cpp", 44 srcs = ["list_people.cc"], 45 deps = [":addressbook_cc_proto"], 46) 47 48# Similar to cc_proto_library but for Java. 49# 50# Remote repository "com_google_protobuf_java" must be defined to use this rule. 51java_proto_library( 52 name = "addressbook_java_proto", 53 deps = [":addressbook_proto"], 54) 55 56java_binary( 57 name = "add_person_java", 58 srcs = ["AddPerson.java"], 59 main_class = "AddPerson", 60 deps = [":addressbook_java_proto"], 61) 62 63java_binary( 64 name = "list_people_java", 65 srcs = ["ListPeople.java"], 66 main_class = "ListPeople", 67 deps = [":addressbook_java_proto"], 68) 69 70# Java lite. 71# 72# Remote repository "com_google_protobuf_javalite" must be defined to use this 73# rule. 74java_lite_proto_library( 75 name = "addressbook_java_lite_proto", 76 deps = [":addressbook_proto"], 77) 78 79# Java lite API is a subset of the regular Java API so if you only uses this 80# subset in your code, you can actually compile your code against both (i.e., 81# share code between server build and Android build). 82# 83# The lite version has a smaller code size, and you can see that by comparing 84# the resulted .jar file: 85# 86# $ bazel build :add_person_java_deploy.jar :add_person_java_lite_deploy.jar 87# $ ls -l bazel-bin/*_deploy.jar 88# -r-xr-xr-x 1 xiaofeng eng 1230797 Sep 8 12:24 bazel-bin/add_person_java_deploy.jar 89# -r-xr-xr-x 1 xiaofeng eng 236166 Sep 8 12:24 bazel-bin/add_person_java_lite_deploy.jar 90# 91# In the above example, the lite .jar file is 6 times smaller. With proper 92# proguard inlining/stripping, the difference can be much more larger than 93# that. 94java_binary( 95 name = "add_person_java_lite", 96 srcs = ["AddPerson.java"], 97 main_class = "AddPerson", 98 deps = [":addressbook_java_lite_proto"], 99) 100 101java_binary( 102 name = "list_people_java_lite", 103 srcs = ["ListPeople.java"], 104 main_class = "ListPeople", 105 deps = [":addressbook_java_lite_proto"], 106) 107 108# Files included in all source distributions 109pkg_files( 110 name = "dist_files", 111 srcs = [ 112 "AddPerson.java", 113 "BUILD.bazel", 114 "CMakeLists.txt", 115 "ListPeople.java", 116 "Makefile", 117 "README.md", 118 "WORKSPACE", 119 "add_person.cc", 120 "add_person.dart", 121 "add_person.py", 122 "addressbook.proto", 123 "go/cmd/add_person/add_person.go", 124 "go/cmd/add_person/add_person_test.go", 125 "go/cmd/list_people/list_people.go", 126 "go/cmd/list_people/list_people_test.go", 127 "go/go.mod", 128 "go/go.sum", 129 "list_people.cc", 130 "list_people.dart", 131 "list_people.py", 132 "pubspec.yaml", 133 ], 134 prefix = "examples/", 135 strip_prefix = strip_prefix.from_root(""), 136 visibility = ["//visibility:public"], 137) 138