blob: 34099ad9097ab63b8c950b55ba76eecedc9bdfac [file] [log] [blame]
Jan Tattermusch4d5c3102017-06-07 10:23:56 +02001# Copyright 2015 gRPC authors.
Jorge Canizales24b2f672015-07-02 01:29:37 -07002#
Jan Tattermusch4d5c3102017-06-07 10:23:56 +02003# 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 Canizales24b2f672015-07-02 01:29:37 -07006#
Jan Tattermusch4d5c3102017-06-07 10:23:56 +02007# http://www.apache.org/licenses/LICENSE-2.0
Jorge Canizales24b2f672015-07-02 01:29:37 -07008#
Jan Tattermusch4d5c3102017-06-07 10:23:56 +02009# 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 Canizales24b2f672015-07-02 01:29:37 -070014
15"""
16Bazel macros to declare gRPC libraries automatically generated from proto files.
17
18This file declares two macros:
19- objc_proto_library
20- objc_grpc_library
21"""
Jorge Canizales4bc49052015-07-01 14:18:04 -070022
23def _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 Canizales164f6332015-07-01 18:29:21 -070029def _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 Canizales4bc49052015-07-01 14:18:04 -070033
Jorge Canizales164f6332015-07-01 18:29:21 -070034def _file_with_extension(src, ext):
35 elements = src.rpartition('/')
36 basename = elements[-1].partition('.')[0]
37 return "".join(elements[:-1] + [basename, ext])
38
Jorge Canizalesd9435272015-07-01 18:43:54 -070039def _protoc_invocation(srcs, flags):
Jorge Canizales24b2f672015-07-02 01:29:37 -070040 """Returns a command line to invoke protoc from a genrule, on the given
41 sources, using the given flags.
42 """
Jorge Canizales8637ece2015-07-02 01:01:53 -070043 protoc_command = "$(location //external:protoc) -I . "
Jorge Canizales4bc49052015-07-01 14:18:04 -070044 srcs_params = ""
45 for src in srcs:
46 srcs_params += " $(location %s)" % (src)
Jorge Canizalesd9435272015-07-01 18:43:54 -070047 return protoc_command + flags + srcs_params
Jorge Canizales4bc49052015-07-01 14:18:04 -070048
Jorge Canizalesd9435272015-07-01 18:43:54 -070049def objc_proto_library(name, srcs, visibility=None):
Jorge Canizales24b2f672015-07-02 01:29:37 -070050 """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 Canizales907fad92015-07-02 00:51:01 -070053 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 Canizales164f6332015-07-01 18:29:21 -070059
Jorge Canizalesd9435272015-07-01 18:43:54 -070060 protoc_flags = "--objc_out=$(GENDIR)"
Jorge Canizales907fad92015-07-02 00:51:01 -070061
Jorge Canizales4bc49052015-07-01 14:18:04 -070062 native.genrule(
Jorge Canizalesd9435272015-07-01 18:43:54 -070063 name = name + "_codegen",
Jorge Canizales8637ece2015-07-02 01:01:53 -070064 srcs = srcs + ["//external:protoc"],
Jorge Canizales907fad92015-07-02 00:51:01 -070065 outs = h_files + m_files,
Jorge Canizalesd9435272015-07-01 18:43:54 -070066 cmd = _protoc_invocation(srcs, protoc_flags),
Jorge Canizales4bc49052015-07-01 14:18:04 -070067 )
68 native.objc_library(
Jorge Canizalesd9435272015-07-01 18:43:54 -070069 name = name,
Jorge Canizales907fad92015-07-02 00:51:01 -070070 hdrs = h_files,
Jorge Canizales4bc49052015-07-01 14:18:04 -070071 includes = ["."],
Jorge Canizales907fad92015-07-02 00:51:01 -070072 non_arc_srcs = m_files,
73 deps = ["//external:protobuf_objc"],
Jorge Canizalesd9435272015-07-01 18:43:54 -070074 visibility = visibility,
Jorge Canizales4bc49052015-07-01 14:18:04 -070075 )
76
Jorge Canizales907fad92015-07-02 00:51:01 -070077def objc_grpc_library(name, services, other_messages, visibility=None):
Jorge Canizales24b2f672015-07-02 01:29:37 -070078 """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 Canizales907fad92015-07-02 00:51:01 -070082 objc_proto_library(name + "_messages", services + other_messages)
Jorge Canizalesd9435272015-07-01 18:43:54 -070083
Jorge Canizales907fad92015-07-02 00:51:01 -070084 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 Canizalesd9435272015-07-01 18:43:54 -070090
Jorge Canizales24b2f672015-07-02 01:29:37 -070091 protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" +
92 "protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)")
Jorge Canizales907fad92015-07-02 00:51:01 -070093
Jorge Canizales4bc49052015-07-01 14:18:04 -070094 native.genrule(
95 name = name + "_codegen",
Jorge Canizales8637ece2015-07-02 01:01:53 -070096 srcs = services + [
97 "//external:grpc_protoc_plugin_objc",
98 "//external:protoc",
99 ],
Jorge Canizales907fad92015-07-02 00:51:01 -0700100 outs = h_files + m_files,
101 cmd = _protoc_invocation(services, protoc_flags),
Jorge Canizales4bc49052015-07-01 14:18:04 -0700102 )
103 native.objc_library(
104 name = name,
Jorge Canizales907fad92015-07-02 00:51:01 -0700105 hdrs = h_files,
Jorge Canizales4bc49052015-07-01 14:18:04 -0700106 includes = ["."],
Jorge Canizales907fad92015-07-02 00:51:01 -0700107 srcs = m_files,
Jorge Canizales4bc49052015-07-01 14:18:04 -0700108 deps = [
109 ":" + name + "_messages",
110 "//external:proto_objc_rpc",
111 ],
112 visibility = visibility,
113 )