BUILD 3.5 KB

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