1ffe3c632Sopenharmony_ci# This BUILD file shows how to use protobuf with bazel. Before you can use 2ffe3c632Sopenharmony_ci# proto_library/<lang>_proto_library rules in a BUILD file, you need to 3ffe3c632Sopenharmony_ci# include protobuf repo as remote repositories in your WORKSPACE file. See 4ffe3c632Sopenharmony_ci# the WORKSPACE file in the same directory with this BUILD file for an 5ffe3c632Sopenharmony_ci# example. 6ffe3c632Sopenharmony_ci 7ffe3c632Sopenharmony_ciload("@rules_cc//cc:defs.bzl", "cc_binary", "cc_proto_library") 8ffe3c632Sopenharmony_ciload("@rules_java//java:defs.bzl", "java_binary", "java_lite_proto_library", "java_proto_library") 9ffe3c632Sopenharmony_ciload("@rules_proto//proto:defs.bzl", "proto_library") 10ffe3c632Sopenharmony_ci 11ffe3c632Sopenharmony_ci# For each .proto file, a proto_library target should be defined. This target 12ffe3c632Sopenharmony_ci# is not bound to any particular language. Instead, it defines the dependency 13ffe3c632Sopenharmony_ci# graph of the .proto files (i.e., proto imports) and serves as the provider 14ffe3c632Sopenharmony_ci# of .proto source files to the protocol compiler. 15ffe3c632Sopenharmony_ci# 16ffe3c632Sopenharmony_ci# Remote repository "com_google_protobuf" must be defined to use this rule. 17ffe3c632Sopenharmony_ciproto_library( 18ffe3c632Sopenharmony_ci name = "addressbook_proto", 19ffe3c632Sopenharmony_ci srcs = ["addressbook.proto"], 20ffe3c632Sopenharmony_ci deps = ["@com_google_protobuf//:timestamp_proto"], 21ffe3c632Sopenharmony_ci) 22ffe3c632Sopenharmony_ci 23ffe3c632Sopenharmony_ci# The cc_proto_library rule generates C++ code for a proto_library rule. It 24ffe3c632Sopenharmony_ci# must have exactly one proto_library dependency. If you want to use multiple 25ffe3c632Sopenharmony_ci# proto_library targets, create a separate cc_proto_library target for each 26ffe3c632Sopenharmony_ci# of them. 27ffe3c632Sopenharmony_ci# 28ffe3c632Sopenharmony_ci# Remote repository "com_google_protobuf_cc" must be defined to use this rule. 29ffe3c632Sopenharmony_cicc_proto_library( 30ffe3c632Sopenharmony_ci name = "addressbook_cc_proto", 31ffe3c632Sopenharmony_ci deps = [":addressbook_proto"], 32ffe3c632Sopenharmony_ci) 33ffe3c632Sopenharmony_ci 34ffe3c632Sopenharmony_ci# cc_library/cc_binary targets can depend on cc_proto_library targets. 35ffe3c632Sopenharmony_cicc_binary( 36ffe3c632Sopenharmony_ci name = "add_person_cpp", 37ffe3c632Sopenharmony_ci srcs = ["add_person.cc"], 38ffe3c632Sopenharmony_ci deps = [":addressbook_cc_proto"], 39ffe3c632Sopenharmony_ci) 40ffe3c632Sopenharmony_ci 41ffe3c632Sopenharmony_cicc_binary( 42ffe3c632Sopenharmony_ci name = "list_people_cpp", 43ffe3c632Sopenharmony_ci srcs = ["list_people.cc"], 44ffe3c632Sopenharmony_ci deps = [":addressbook_cc_proto"], 45ffe3c632Sopenharmony_ci) 46ffe3c632Sopenharmony_ci 47ffe3c632Sopenharmony_ci# Similar to cc_proto_library but for Java. 48ffe3c632Sopenharmony_ci# 49ffe3c632Sopenharmony_ci# Remote repository "com_google_protobuf_java" must be defined to use this rule. 50ffe3c632Sopenharmony_cijava_proto_library( 51ffe3c632Sopenharmony_ci name = "addressbook_java_proto", 52ffe3c632Sopenharmony_ci deps = [":addressbook_proto"], 53ffe3c632Sopenharmony_ci) 54ffe3c632Sopenharmony_ci 55ffe3c632Sopenharmony_cijava_binary( 56ffe3c632Sopenharmony_ci name = "add_person_java", 57ffe3c632Sopenharmony_ci srcs = ["AddPerson.java"], 58ffe3c632Sopenharmony_ci main_class = "AddPerson", 59ffe3c632Sopenharmony_ci deps = [":addressbook_java_proto"], 60ffe3c632Sopenharmony_ci) 61ffe3c632Sopenharmony_ci 62ffe3c632Sopenharmony_cijava_binary( 63ffe3c632Sopenharmony_ci name = "list_people_java", 64ffe3c632Sopenharmony_ci srcs = ["ListPeople.java"], 65ffe3c632Sopenharmony_ci main_class = "ListPeople", 66ffe3c632Sopenharmony_ci deps = [":addressbook_java_proto"], 67ffe3c632Sopenharmony_ci) 68ffe3c632Sopenharmony_ci 69ffe3c632Sopenharmony_ci# Java lite. 70ffe3c632Sopenharmony_ci# 71ffe3c632Sopenharmony_ci# Remote repository "com_google_protobuf_javalite" must be defined to use this 72ffe3c632Sopenharmony_ci# rule. 73ffe3c632Sopenharmony_cijava_lite_proto_library( 74ffe3c632Sopenharmony_ci name = "addressbook_java_lite_proto", 75ffe3c632Sopenharmony_ci deps = [":addressbook_proto"], 76ffe3c632Sopenharmony_ci) 77ffe3c632Sopenharmony_ci 78ffe3c632Sopenharmony_ci# Java lite API is a subset of the regular Java API so if you only uses this 79ffe3c632Sopenharmony_ci# subset in your code, you can actually compile your code against both (i.e., 80ffe3c632Sopenharmony_ci# share code between server build and Android build). 81ffe3c632Sopenharmony_ci# 82ffe3c632Sopenharmony_ci# The lite version has a smaller code size, and you can see that by comparing 83ffe3c632Sopenharmony_ci# the resulted .jar file: 84ffe3c632Sopenharmony_ci# 85ffe3c632Sopenharmony_ci# $ bazel build :add_person_java_deploy.jar :add_person_java_lite_deploy.jar 86ffe3c632Sopenharmony_ci# $ ls -l bazel-bin/*_deploy.jar 87ffe3c632Sopenharmony_ci# -r-xr-xr-x 1 xiaofeng eng 1230797 Sep 8 12:24 bazel-bin/add_person_java_deploy.jar 88ffe3c632Sopenharmony_ci# -r-xr-xr-x 1 xiaofeng eng 236166 Sep 8 12:24 bazel-bin/add_person_java_lite_deploy.jar 89ffe3c632Sopenharmony_ci# 90ffe3c632Sopenharmony_ci# In the above example, the lite .jar file is 6 times smaller. With proper 91ffe3c632Sopenharmony_ci# proguard inlining/stripping, the difference can be much more larger than 92ffe3c632Sopenharmony_ci# that. 93ffe3c632Sopenharmony_cijava_binary( 94ffe3c632Sopenharmony_ci name = "add_person_java_lite", 95ffe3c632Sopenharmony_ci srcs = ["AddPerson.java"], 96ffe3c632Sopenharmony_ci main_class = "AddPerson", 97ffe3c632Sopenharmony_ci deps = [":addressbook_java_lite_proto"], 98ffe3c632Sopenharmony_ci) 99ffe3c632Sopenharmony_ci 100ffe3c632Sopenharmony_cijava_binary( 101ffe3c632Sopenharmony_ci name = "list_people_java_lite", 102ffe3c632Sopenharmony_ci srcs = ["ListPeople.java"], 103ffe3c632Sopenharmony_ci main_class = "ListPeople", 104ffe3c632Sopenharmony_ci deps = [":addressbook_java_lite_proto"], 105ffe3c632Sopenharmony_ci) 106