Jan Tattermusch | 4d5c310 | 2017-06-07 10:23:56 +0200 | [diff] [blame] | 1 | # Copyright 2015 gRPC authors. |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 2 | # |
Jan Tattermusch | 4d5c310 | 2017-06-07 10:23:56 +0200 | [diff] [blame] | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 6 | # |
Jan Tattermusch | 4d5c310 | 2017-06-07 10:23:56 +0200 | [diff] [blame] | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 8 | # |
Jan Tattermusch | 4d5c310 | 2017-06-07 10:23:56 +0200 | [diff] [blame] | 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 14 | |
| 15 | """ |
| 16 | Bazel macros to declare gRPC libraries automatically generated from proto files. |
| 17 | |
| 18 | This file declares two macros: |
| 19 | - objc_proto_library |
| 20 | - objc_grpc_library |
| 21 | """ |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 22 | |
| 23 | def _lower_underscore_to_upper_camel(str): |
| 24 | humps = [] |
| 25 | for hump in str.split('_'): |
| 26 | humps += [hump[0].upper() + hump[1:]] |
| 27 | return "".join(humps) |
| 28 | |
Jorge Canizales | 164f633 | 2015-07-01 18:29:21 -0700 | [diff] [blame] | 29 | def _file_to_upper_camel(src): |
| 30 | elements = src.rpartition('/') |
| 31 | upper_camel = _lower_underscore_to_upper_camel(elements[-1]) |
| 32 | return "".join(elements[:-1] + [upper_camel]) |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 33 | |
Jorge Canizales | 164f633 | 2015-07-01 18:29:21 -0700 | [diff] [blame] | 34 | def _file_with_extension(src, ext): |
| 35 | elements = src.rpartition('/') |
| 36 | basename = elements[-1].partition('.')[0] |
| 37 | return "".join(elements[:-1] + [basename, ext]) |
| 38 | |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 39 | def _protoc_invocation(srcs, flags): |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 40 | """Returns a command line to invoke protoc from a genrule, on the given |
| 41 | sources, using the given flags. |
| 42 | """ |
Jorge Canizales | 8637ece | 2015-07-02 01:01:53 -0700 | [diff] [blame] | 43 | protoc_command = "$(location //external:protoc) -I . " |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 44 | srcs_params = "" |
| 45 | for src in srcs: |
| 46 | srcs_params += " $(location %s)" % (src) |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 47 | return protoc_command + flags + srcs_params |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 48 | |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 49 | def objc_proto_library(name, srcs, visibility=None): |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 50 | """Declares an objc_library for the code generated by protoc from the given |
| 51 | proto sources. This generated code doesn't include proto services. |
| 52 | """ |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 53 | h_files = [] |
| 54 | m_files = [] |
| 55 | for src in srcs: |
| 56 | src = _file_to_upper_camel(src) |
| 57 | h_files += [_file_with_extension(src, ".pbobjc.h")] |
| 58 | m_files += [_file_with_extension(src, ".pbobjc.m")] |
Jorge Canizales | 164f633 | 2015-07-01 18:29:21 -0700 | [diff] [blame] | 59 | |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 60 | protoc_flags = "--objc_out=$(GENDIR)" |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 61 | |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 62 | native.genrule( |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 63 | name = name + "_codegen", |
Jorge Canizales | 8637ece | 2015-07-02 01:01:53 -0700 | [diff] [blame] | 64 | srcs = srcs + ["//external:protoc"], |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 65 | outs = h_files + m_files, |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 66 | cmd = _protoc_invocation(srcs, protoc_flags), |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 67 | ) |
| 68 | native.objc_library( |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 69 | name = name, |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 70 | hdrs = h_files, |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 71 | includes = ["."], |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 72 | non_arc_srcs = m_files, |
| 73 | deps = ["//external:protobuf_objc"], |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 74 | visibility = visibility, |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 75 | ) |
| 76 | |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 77 | def objc_grpc_library(name, services, other_messages, visibility=None): |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 78 | """Declares an objc_library for the code generated by gRPC and protoc from the |
| 79 | given proto sources (services and other_messages). The generated code doesn't |
| 80 | include proto services of the files passed as other_messages. |
| 81 | """ |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 82 | objc_proto_library(name + "_messages", services + other_messages) |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 83 | |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 84 | h_files = [] |
| 85 | m_files = [] |
| 86 | for src in services: |
| 87 | src = _file_to_upper_camel(src) |
| 88 | h_files += [_file_with_extension(src, ".pbrpc.h")] |
| 89 | m_files += [_file_with_extension(src, ".pbrpc.m")] |
Jorge Canizales | d943527 | 2015-07-01 18:43:54 -0700 | [diff] [blame] | 90 | |
Jorge Canizales | 24b2f67 | 2015-07-02 01:29:37 -0700 | [diff] [blame] | 91 | protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" + |
| 92 | "protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)") |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 93 | |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 94 | native.genrule( |
| 95 | name = name + "_codegen", |
Jorge Canizales | 8637ece | 2015-07-02 01:01:53 -0700 | [diff] [blame] | 96 | srcs = services + [ |
| 97 | "//external:grpc_protoc_plugin_objc", |
| 98 | "//external:protoc", |
| 99 | ], |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 100 | outs = h_files + m_files, |
| 101 | cmd = _protoc_invocation(services, protoc_flags), |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 102 | ) |
| 103 | native.objc_library( |
| 104 | name = name, |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 105 | hdrs = h_files, |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 106 | includes = ["."], |
Jorge Canizales | 907fad9 | 2015-07-02 00:51:01 -0700 | [diff] [blame] | 107 | srcs = m_files, |
Jorge Canizales | 4bc4905 | 2015-07-01 14:18:04 -0700 | [diff] [blame] | 108 | deps = [ |
| 109 | ":" + name + "_messages", |
| 110 | "//external:proto_objc_rpc", |
| 111 | ], |
| 112 | visibility = visibility, |
| 113 | ) |