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