OLD | NEW |
(Empty) | |
| 1 # Copyright 2015, Google Inc. |
| 2 # All rights reserved. |
| 3 # |
| 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are |
| 6 # met: |
| 7 # |
| 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above |
| 11 # copyright notice, this list of conditions and the following disclaimer |
| 12 # in the documentation and/or other materials provided with the |
| 13 # distribution. |
| 14 # * Neither the name of Google Inc. nor the names of its |
| 15 # contributors may be used to endorse or promote products derived from |
| 16 # this software without specific prior written permission. |
| 17 # |
| 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 """ |
| 31 Bazel macros to declare gRPC libraries automatically generated from proto files. |
| 32 |
| 33 This file declares two macros: |
| 34 - objc_proto_library |
| 35 - objc_grpc_library |
| 36 """ |
| 37 |
| 38 def _lower_underscore_to_upper_camel(str): |
| 39 humps = [] |
| 40 for hump in str.split('_'): |
| 41 humps += [hump[0].upper() + hump[1:]] |
| 42 return "".join(humps) |
| 43 |
| 44 def _file_to_upper_camel(src): |
| 45 elements = src.rpartition('/') |
| 46 upper_camel = _lower_underscore_to_upper_camel(elements[-1]) |
| 47 return "".join(elements[:-1] + [upper_camel]) |
| 48 |
| 49 def _file_with_extension(src, ext): |
| 50 elements = src.rpartition('/') |
| 51 basename = elements[-1].partition('.')[0] |
| 52 return "".join(elements[:-1] + [basename, ext]) |
| 53 |
| 54 def _protoc_invocation(srcs, flags): |
| 55 """Returns a command line to invoke protoc from a genrule, on the given |
| 56 sources, using the given flags. |
| 57 """ |
| 58 protoc_command = "$(location //external:protoc) -I . " |
| 59 srcs_params = "" |
| 60 for src in srcs: |
| 61 srcs_params += " $(location %s)" % (src) |
| 62 return protoc_command + flags + srcs_params |
| 63 |
| 64 def objc_proto_library(name, srcs, visibility=None): |
| 65 """Declares an objc_library for the code generated by protoc from the given |
| 66 proto sources. This generated code doesn't include proto services. |
| 67 """ |
| 68 h_files = [] |
| 69 m_files = [] |
| 70 for src in srcs: |
| 71 src = _file_to_upper_camel(src) |
| 72 h_files += [_file_with_extension(src, ".pbobjc.h")] |
| 73 m_files += [_file_with_extension(src, ".pbobjc.m")] |
| 74 |
| 75 protoc_flags = "--objc_out=$(GENDIR)" |
| 76 |
| 77 native.genrule( |
| 78 name = name + "_codegen", |
| 79 srcs = srcs + ["//external:protoc"], |
| 80 outs = h_files + m_files, |
| 81 cmd = _protoc_invocation(srcs, protoc_flags), |
| 82 ) |
| 83 native.objc_library( |
| 84 name = name, |
| 85 hdrs = h_files, |
| 86 includes = ["."], |
| 87 non_arc_srcs = m_files, |
| 88 deps = ["//external:protobuf_objc"], |
| 89 visibility = visibility, |
| 90 ) |
| 91 |
| 92 def objc_grpc_library(name, services, other_messages, visibility=None): |
| 93 """Declares an objc_library for the code generated by gRPC and protoc from the |
| 94 given proto sources (services and other_messages). The generated code doesn't |
| 95 include proto services of the files passed as other_messages. |
| 96 """ |
| 97 objc_proto_library(name + "_messages", services + other_messages) |
| 98 |
| 99 h_files = [] |
| 100 m_files = [] |
| 101 for src in services: |
| 102 src = _file_to_upper_camel(src) |
| 103 h_files += [_file_with_extension(src, ".pbrpc.h")] |
| 104 m_files += [_file_with_extension(src, ".pbrpc.m")] |
| 105 |
| 106 protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" + |
| 107 "protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)") |
| 108 |
| 109 native.genrule( |
| 110 name = name + "_codegen", |
| 111 srcs = services + [ |
| 112 "//external:grpc_protoc_plugin_objc", |
| 113 "//external:protoc", |
| 114 ], |
| 115 outs = h_files + m_files, |
| 116 cmd = _protoc_invocation(services, protoc_flags), |
| 117 ) |
| 118 native.objc_library( |
| 119 name = name, |
| 120 hdrs = h_files, |
| 121 includes = ["."], |
| 122 srcs = m_files, |
| 123 deps = [ |
| 124 ":" + name + "_messages", |
| 125 "//external:proto_objc_rpc", |
| 126 ], |
| 127 visibility = visibility, |
| 128 ) |
OLD | NEW |