Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 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 | """ |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 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 | |
Jorge Canizales | 164f633 | 2015-07-01 18:29:21 -0700 | [diff] [blame] | 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]) |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 48 | |
Jorge Canizales | 164f633 | 2015-07-01 18:29:21 -0700 | [diff] [blame] | 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 | |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 54 | def _protoc_invocation(srcs, flags): |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 55 | """Returns a command line to invoke protoc from a genrule, on the given |
| 56 | sources, using the given flags. |
| 57 | """ |
Jorge Canizales | 8637ece | 2015-07-02 01:01:53 -0700 | [diff] [blame] | 58 | protoc_command = "$(location //external:protoc) -I . " |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 59 | srcs_params = "" |
| 60 | for src in srcs: |
| 61 | srcs_params += " $(location %s)" % (src) |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 62 | return protoc_command + flags + srcs_params |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 63 | |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 64 | def objc_proto_library(name, srcs, visibility=None): |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 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 | """ |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 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")] |
Jorge Canizales | 164f633 | 2015-07-01 18:29:21 -0700 | [diff] [blame] | 74 | |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 75 | protoc_flags = "--objc_out=$(GENDIR)" |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 76 | |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 77 | native.genrule( |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 78 | name = name + "_codegen", |
Jorge Canizales | 8637ece | 2015-07-02 01:01:53 -0700 | [diff] [blame] | 79 | srcs = srcs + ["//external:protoc"], |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 80 | outs = h_files + m_files, |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 81 | cmd = _protoc_invocation(srcs, protoc_flags), |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 82 | ) |
| 83 | native.objc_library( |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 84 | name = name, |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 85 | hdrs = h_files, |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 86 | includes = ["."], |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 87 | non_arc_srcs = m_files, |
| 88 | deps = ["//external:protobuf_objc"], |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 89 | visibility = visibility, |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 90 | ) |
| 91 | |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 92 | def objc_grpc_library(name, services, other_messages, visibility=None): |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 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 | """ |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 97 | objc_proto_library(name + "_messages", services + other_messages) |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 98 | |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 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")] |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 105 | |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 106 | protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" + |
| 107 | "protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)") |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 108 | |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 109 | native.genrule( |
| 110 | name = name + "_codegen", |
Jorge Canizales | 8637ece | 2015-07-02 01:01:53 -0700 | [diff] [blame] | 111 | srcs = services + [ |
| 112 | "//external:grpc_protoc_plugin_objc", |
| 113 | "//external:protoc", |
| 114 | ], |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 115 | outs = h_files + m_files, |
| 116 | cmd = _protoc_invocation(services, protoc_flags), |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 117 | ) |
| 118 | native.objc_library( |
| 119 | name = name, |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 120 | hdrs = h_files, |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 121 | includes = ["."], |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 122 | srcs = m_files, |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 123 | deps = [ |
| 124 | ":" + name + "_messages", |
| 125 | "//external:proto_objc_rpc", |
| 126 | ], |
| 127 | visibility = visibility, |
| 128 | ) |