blob: 9f2693126af451f180598a3e31a674e6f9ef431f [file] [log] [blame]
Jorge Canizales24b2f672015-07-02 01:29:37 -07001# 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"""
31Bazel macros to declare gRPC libraries automatically generated from proto files.
32
33This file declares two macros:
34- objc_proto_library
35- objc_grpc_library
36"""
Jorge Canizales4bc49052015-07-01 14:18:04 -070037
38def _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 Canizales164f6332015-07-01 18:29:21 -070044def _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 Canizales4bc49052015-07-01 14:18:04 -070048
Jorge Canizales164f6332015-07-01 18:29:21 -070049def _file_with_extension(src, ext):
50 elements = src.rpartition('/')
51 basename = elements[-1].partition('.')[0]
52 return "".join(elements[:-1] + [basename, ext])
53
Jorge Canizalesd9435272015-07-01 18:43:54 -070054def _protoc_invocation(srcs, flags):
Jorge Canizales24b2f672015-07-02 01:29:37 -070055 """Returns a command line to invoke protoc from a genrule, on the given
56 sources, using the given flags.
57 """
Jorge Canizales8637ece2015-07-02 01:01:53 -070058 protoc_command = "$(location //external:protoc) -I . "
Jorge Canizales4bc49052015-07-01 14:18:04 -070059 srcs_params = ""
60 for src in srcs:
61 srcs_params += " $(location %s)" % (src)
Jorge Canizalesd9435272015-07-01 18:43:54 -070062 return protoc_command + flags + srcs_params
Jorge Canizales4bc49052015-07-01 14:18:04 -070063
Jorge Canizalesd9435272015-07-01 18:43:54 -070064def objc_proto_library(name, srcs, visibility=None):
Jorge Canizales24b2f672015-07-02 01:29:37 -070065 """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 Canizales907fad92015-07-02 00:51:01 -070068 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 Canizales164f6332015-07-01 18:29:21 -070074
Jorge Canizalesd9435272015-07-01 18:43:54 -070075 protoc_flags = "--objc_out=$(GENDIR)"
Jorge Canizales907fad92015-07-02 00:51:01 -070076
Jorge Canizales4bc49052015-07-01 14:18:04 -070077 native.genrule(
Jorge Canizalesd9435272015-07-01 18:43:54 -070078 name = name + "_codegen",
Jorge Canizales8637ece2015-07-02 01:01:53 -070079 srcs = srcs + ["//external:protoc"],
Jorge Canizales907fad92015-07-02 00:51:01 -070080 outs = h_files + m_files,
Jorge Canizalesd9435272015-07-01 18:43:54 -070081 cmd = _protoc_invocation(srcs, protoc_flags),
Jorge Canizales4bc49052015-07-01 14:18:04 -070082 )
83 native.objc_library(
Jorge Canizalesd9435272015-07-01 18:43:54 -070084 name = name,
Jorge Canizales907fad92015-07-02 00:51:01 -070085 hdrs = h_files,
Jorge Canizales4bc49052015-07-01 14:18:04 -070086 includes = ["."],
Jorge Canizales907fad92015-07-02 00:51:01 -070087 non_arc_srcs = m_files,
88 deps = ["//external:protobuf_objc"],
Jorge Canizalesd9435272015-07-01 18:43:54 -070089 visibility = visibility,
Jorge Canizales4bc49052015-07-01 14:18:04 -070090 )
91
Jorge Canizales907fad92015-07-02 00:51:01 -070092def objc_grpc_library(name, services, other_messages, visibility=None):
Jorge Canizales24b2f672015-07-02 01:29:37 -070093 """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 Canizales907fad92015-07-02 00:51:01 -070097 objc_proto_library(name + "_messages", services + other_messages)
Jorge Canizalesd9435272015-07-01 18:43:54 -070098
Jorge Canizales907fad92015-07-02 00:51:01 -070099 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 Canizalesd9435272015-07-01 18:43:54 -0700105
Jorge Canizales24b2f672015-07-02 01:29:37 -0700106 protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" +
107 "protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)")
Jorge Canizales907fad92015-07-02 00:51:01 -0700108
Jorge Canizales4bc49052015-07-01 14:18:04 -0700109 native.genrule(
110 name = name + "_codegen",
Jorge Canizales8637ece2015-07-02 01:01:53 -0700111 srcs = services + [
112 "//external:grpc_protoc_plugin_objc",
113 "//external:protoc",
114 ],
Jorge Canizales907fad92015-07-02 00:51:01 -0700115 outs = h_files + m_files,
116 cmd = _protoc_invocation(services, protoc_flags),
Jorge Canizales4bc49052015-07-01 14:18:04 -0700117 )
118 native.objc_library(
119 name = name,
Jorge Canizales907fad92015-07-02 00:51:01 -0700120 hdrs = h_files,
Jorge Canizales4bc49052015-07-01 14:18:04 -0700121 includes = ["."],
Jorge Canizales907fad92015-07-02 00:51:01 -0700122 srcs = m_files,
Jorge Canizales4bc49052015-07-01 14:18:04 -0700123 deps = [
124 ":" + name + "_messages",
125 "//external:proto_objc_rpc",
126 ],
127 visibility = visibility,
128 )