2
0

BUILD.bazel 4.3 KB

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