From 4bc49051ba823cfc9ddb342e08b1ab7638fa6396 Mon Sep 17 00:00:00 2001 From: Jorge Canizales Date: Wed, 1 Jul 2015 14:18:04 -0700 Subject: Skylark macro to generate an objc_library from protos Still needed: - Use compiled protoc. - Work with multiple proto sources. - Work with proto sources in nested dirs. --- grpc.bzl | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 grpc.bzl (limited to 'grpc.bzl') diff --git a/grpc.bzl b/grpc.bzl new file mode 100644 index 0000000000..9865bc734d --- /dev/null +++ b/grpc.bzl @@ -0,0 +1,68 @@ + +def _lower_underscore_to_upper_camel(str): + humps = [] + for hump in str.split('_'): + humps += [hump[0].upper() + hump[1:]] + return "".join(humps) + +def objc_grpc_library(name, srcs, visibility=None): + basename = srcs[0].split('/')[-1] + filename = basename[:-6] # remove .proto suffix + filename = _lower_underscore_to_upper_camel(filename) + + protoc_command = "protoc -I . " + srcs_params = "" + for src in srcs: + srcs_params += " $(location %s)" % (src) + + # Messages + protoc_messages_flags = "--objc_out=$(GENDIR)" + native.genrule( + name = name + "_mesages_codegen", + srcs = srcs, + outs = [ + filename + ".pbobjc.h", + filename + ".pbobjc.m", + ], + cmd = protoc_command + protoc_messages_flags + srcs_params, + ) + native.objc_library( + name = name + "_messages", + hdrs = [ + ":" + filename + ".pbobjc.h", + ], + includes = ["."], + non_arc_srcs = [ + ":" + filename + ".pbobjc.m", + ], + deps = [ + "//external:protobuf_objc", + ], + ) + + # Services + protoc_services_flags = "--grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)" + native.genrule( + name = name + "_codegen", + srcs = srcs + ["//external:grpc_protoc_plugin_objc"], + outs = [ + filename + ".pbrpc.h", + filename + ".pbrpc.m", + ], + cmd = protoc_command + protoc_services_flags + srcs_params, + ) + native.objc_library( + name = name, + hdrs = [ + ":" + filename + ".pbrpc.h", + ], + includes = ["."], + srcs = [ + ":" + filename + ".pbrpc.m", + ], + deps = [ + ":" + name + "_messages", + "//external:proto_objc_rpc", + ], + visibility = visibility, + ) -- cgit v1.2.3 From fc5fb879a6a05826f9b9078d172e2d59a1e8be43 Mon Sep 17 00:00:00 2001 From: Jorge Canizales Date: Wed, 1 Jul 2015 15:29:30 -0700 Subject: Support sources in nested dirs --- grpc.bzl | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) (limited to 'grpc.bzl') diff --git a/grpc.bzl b/grpc.bzl index 9865bc734d..2ad3b03b57 100644 --- a/grpc.bzl +++ b/grpc.bzl @@ -6,7 +6,9 @@ def _lower_underscore_to_upper_camel(str): return "".join(humps) def objc_grpc_library(name, srcs, visibility=None): - basename = srcs[0].split('/')[-1] + src_path_elements = srcs[0].split('/') + src_dir = '/'.join(src_path_elements[:-1]) + basename = src_path_elements[-1] filename = basename[:-6] # remove .proto suffix filename = _lower_underscore_to_upper_camel(filename) @@ -17,24 +19,19 @@ def objc_grpc_library(name, srcs, visibility=None): # Messages protoc_messages_flags = "--objc_out=$(GENDIR)" + message_header = src_dir + '/' + filename + ".pbobjc.h" + message_implementation = src_dir + '/' + filename + ".pbobjc.m" native.genrule( name = name + "_mesages_codegen", srcs = srcs, - outs = [ - filename + ".pbobjc.h", - filename + ".pbobjc.m", - ], + outs = [message_header, message_implementation], cmd = protoc_command + protoc_messages_flags + srcs_params, ) native.objc_library( name = name + "_messages", - hdrs = [ - ":" + filename + ".pbobjc.h", - ], + hdrs = [message_header], includes = ["."], - non_arc_srcs = [ - ":" + filename + ".pbobjc.m", - ], + non_arc_srcs = [message_implementation], deps = [ "//external:protobuf_objc", ], @@ -42,24 +39,19 @@ def objc_grpc_library(name, srcs, visibility=None): # Services protoc_services_flags = "--grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)" + service_header = src_dir + '/' + filename + ".pbrpc.h" + service_implementation = src_dir + '/' + filename + ".pbrpc.m" native.genrule( name = name + "_codegen", srcs = srcs + ["//external:grpc_protoc_plugin_objc"], - outs = [ - filename + ".pbrpc.h", - filename + ".pbrpc.m", - ], + outs = [service_header, service_implementation], cmd = protoc_command + protoc_services_flags + srcs_params, ) native.objc_library( name = name, - hdrs = [ - ":" + filename + ".pbrpc.h", - ], + hdrs = [service_header], includes = ["."], - srcs = [ - ":" + filename + ".pbrpc.m", - ], + srcs = [service_implementation], deps = [ ":" + name + "_messages", "//external:proto_objc_rpc", -- cgit v1.2.3 From 164f633283259a3f1306e3546910c9fcaaa56bbb Mon Sep 17 00:00:00 2001 From: Jorge Canizales Date: Wed, 1 Jul 2015 18:29:21 -0700 Subject: Extract filename transformations into functions --- grpc.bzl | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'grpc.bzl') diff --git a/grpc.bzl b/grpc.bzl index 2ad3b03b57..f31e188dd6 100644 --- a/grpc.bzl +++ b/grpc.bzl @@ -5,22 +5,28 @@ def _lower_underscore_to_upper_camel(str): humps += [hump[0].upper() + hump[1:]] return "".join(humps) -def objc_grpc_library(name, srcs, visibility=None): - src_path_elements = srcs[0].split('/') - src_dir = '/'.join(src_path_elements[:-1]) - basename = src_path_elements[-1] - filename = basename[:-6] # remove .proto suffix - filename = _lower_underscore_to_upper_camel(filename) +def _file_to_upper_camel(src): + elements = src.rpartition('/') + upper_camel = _lower_underscore_to_upper_camel(elements[-1]) + return "".join(elements[:-1] + [upper_camel]) + +def _file_with_extension(src, ext): + elements = src.rpartition('/') + basename = elements[-1].partition('.')[0] + return "".join(elements[:-1] + [basename, ext]) +def objc_grpc_library(name, srcs, visibility=None): protoc_command = "protoc -I . " srcs_params = "" for src in srcs: srcs_params += " $(location %s)" % (src) + src = _file_to_upper_camel(srcs[0]) + # Messages protoc_messages_flags = "--objc_out=$(GENDIR)" - message_header = src_dir + '/' + filename + ".pbobjc.h" - message_implementation = src_dir + '/' + filename + ".pbobjc.m" + message_header = _file_with_extension(src, ".pbobjc.h") + message_implementation = _file_with_extension(src, ".pbobjc.m") native.genrule( name = name + "_mesages_codegen", srcs = srcs, @@ -39,8 +45,8 @@ def objc_grpc_library(name, srcs, visibility=None): # Services protoc_services_flags = "--grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)" - service_header = src_dir + '/' + filename + ".pbrpc.h" - service_implementation = src_dir + '/' + filename + ".pbrpc.m" + service_header = _file_with_extension(src, ".pbrpc.h") + service_implementation = _file_with_extension(src, ".pbrpc.m") native.genrule( name = name + "_codegen", srcs = srcs + ["//external:grpc_protoc_plugin_objc"], -- cgit v1.2.3 From d9435277fbb686da64ac831bc085f2670d5c2977 Mon Sep 17 00:00:00 2001 From: Jorge Canizales Date: Wed, 1 Jul 2015 18:43:54 -0700 Subject: Split messages and services macros --- grpc.bzl | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'grpc.bzl') diff --git a/grpc.bzl b/grpc.bzl index f31e188dd6..2e4528cb2c 100644 --- a/grpc.bzl +++ b/grpc.bzl @@ -15,43 +15,49 @@ def _file_with_extension(src, ext): basename = elements[-1].partition('.')[0] return "".join(elements[:-1] + [basename, ext]) -def objc_grpc_library(name, srcs, visibility=None): +def _protoc_invocation(srcs, flags): protoc_command = "protoc -I . " srcs_params = "" for src in srcs: srcs_params += " $(location %s)" % (src) + return protoc_command + flags + srcs_params +def objc_proto_library(name, srcs, visibility=None): src = _file_to_upper_camel(srcs[0]) - # Messages - protoc_messages_flags = "--objc_out=$(GENDIR)" + protoc_flags = "--objc_out=$(GENDIR)" message_header = _file_with_extension(src, ".pbobjc.h") message_implementation = _file_with_extension(src, ".pbobjc.m") native.genrule( - name = name + "_mesages_codegen", + name = name + "_codegen", srcs = srcs, outs = [message_header, message_implementation], - cmd = protoc_command + protoc_messages_flags + srcs_params, + cmd = _protoc_invocation(srcs, protoc_flags), ) native.objc_library( - name = name + "_messages", + name = name, hdrs = [message_header], includes = ["."], non_arc_srcs = [message_implementation], deps = [ "//external:protobuf_objc", ], + visibility = visibility, ) - # Services - protoc_services_flags = "--grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)" +def objc_grpc_library(name, srcs, visibility=None): + objc_proto_library(name + "_messages", srcs, visibility) + + src = _file_to_upper_camel(srcs[0]) + + protoc_flags = "--grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)" service_header = _file_with_extension(src, ".pbrpc.h") service_implementation = _file_with_extension(src, ".pbrpc.m") native.genrule( name = name + "_codegen", srcs = srcs + ["//external:grpc_protoc_plugin_objc"], outs = [service_header, service_implementation], - cmd = protoc_command + protoc_services_flags + srcs_params, + cmd = _protoc_invocation(srcs, protoc_flags), ) native.objc_library( name = name, -- cgit v1.2.3 From 907fad9c4d03e081b670492a5b872628a0c7a8ac Mon Sep 17 00:00:00 2001 From: Jorge Canizales Date: Thu, 2 Jul 2015 00:51:01 -0700 Subject: Support multiple proto sources --- grpc.bzl | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) (limited to 'grpc.bzl') diff --git a/grpc.bzl b/grpc.bzl index 2e4528cb2c..0e94c4faf8 100644 --- a/grpc.bzl +++ b/grpc.bzl @@ -23,47 +23,53 @@ def _protoc_invocation(srcs, flags): return protoc_command + flags + srcs_params def objc_proto_library(name, srcs, visibility=None): - src = _file_to_upper_camel(srcs[0]) + h_files = [] + m_files = [] + for src in srcs: + src = _file_to_upper_camel(src) + h_files += [_file_with_extension(src, ".pbobjc.h")] + m_files += [_file_with_extension(src, ".pbobjc.m")] protoc_flags = "--objc_out=$(GENDIR)" - message_header = _file_with_extension(src, ".pbobjc.h") - message_implementation = _file_with_extension(src, ".pbobjc.m") + native.genrule( name = name + "_codegen", srcs = srcs, - outs = [message_header, message_implementation], + outs = h_files + m_files, cmd = _protoc_invocation(srcs, protoc_flags), ) native.objc_library( name = name, - hdrs = [message_header], + hdrs = h_files, includes = ["."], - non_arc_srcs = [message_implementation], - deps = [ - "//external:protobuf_objc", - ], + non_arc_srcs = m_files, + deps = ["//external:protobuf_objc"], visibility = visibility, ) -def objc_grpc_library(name, srcs, visibility=None): - objc_proto_library(name + "_messages", srcs, visibility) +def objc_grpc_library(name, services, other_messages, visibility=None): + objc_proto_library(name + "_messages", services + other_messages) - src = _file_to_upper_camel(srcs[0]) + h_files = [] + m_files = [] + for src in services: + src = _file_to_upper_camel(src) + h_files += [_file_with_extension(src, ".pbrpc.h")] + m_files += [_file_with_extension(src, ".pbrpc.m")] protoc_flags = "--grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)" - service_header = _file_with_extension(src, ".pbrpc.h") - service_implementation = _file_with_extension(src, ".pbrpc.m") + native.genrule( name = name + "_codegen", - srcs = srcs + ["//external:grpc_protoc_plugin_objc"], - outs = [service_header, service_implementation], - cmd = _protoc_invocation(srcs, protoc_flags), + srcs = services + ["//external:grpc_protoc_plugin_objc"], + outs = h_files + m_files, + cmd = _protoc_invocation(services, protoc_flags), ) native.objc_library( name = name, - hdrs = [service_header], + hdrs = h_files, includes = ["."], - srcs = [service_implementation], + srcs = m_files, deps = [ ":" + name + "_messages", "//external:proto_objc_rpc", -- cgit v1.2.3 From 8637ececfd15c7325cf99a965a54387c7d994ad8 Mon Sep 17 00:00:00 2001 From: Jorge Canizales Date: Thu, 2 Jul 2015 01:01:53 -0700 Subject: Use a protoc compiled from sources --- grpc.bzl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'grpc.bzl') diff --git a/grpc.bzl b/grpc.bzl index 0e94c4faf8..5b5735e1bc 100644 --- a/grpc.bzl +++ b/grpc.bzl @@ -16,7 +16,7 @@ def _file_with_extension(src, ext): return "".join(elements[:-1] + [basename, ext]) def _protoc_invocation(srcs, flags): - protoc_command = "protoc -I . " + protoc_command = "$(location //external:protoc) -I . " srcs_params = "" for src in srcs: srcs_params += " $(location %s)" % (src) @@ -34,7 +34,7 @@ def objc_proto_library(name, srcs, visibility=None): native.genrule( name = name + "_codegen", - srcs = srcs, + srcs = srcs + ["//external:protoc"], outs = h_files + m_files, cmd = _protoc_invocation(srcs, protoc_flags), ) @@ -61,7 +61,10 @@ def objc_grpc_library(name, services, other_messages, visibility=None): native.genrule( name = name + "_codegen", - srcs = services + ["//external:grpc_protoc_plugin_objc"], + srcs = services + [ + "//external:grpc_protoc_plugin_objc", + "//external:protoc", + ], outs = h_files + m_files, cmd = _protoc_invocation(services, protoc_flags), ) -- cgit v1.2.3 From 24b2f67b80320d1f215c6465ea0092b7b01caa04 Mon Sep 17 00:00:00 2001 From: Jorge Canizales Date: Thu, 2 Jul 2015 01:29:37 -0700 Subject: Add copyright notice and documentation to .bzl file. --- grpc.bzl | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'grpc.bzl') diff --git a/grpc.bzl b/grpc.bzl index 5b5735e1bc..9f2693126a 100644 --- a/grpc.bzl +++ b/grpc.bzl @@ -1,3 +1,39 @@ +# Copyright 2015, Google Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +""" +Bazel macros to declare gRPC libraries automatically generated from proto files. + +This file declares two macros: +- objc_proto_library +- objc_grpc_library +""" def _lower_underscore_to_upper_camel(str): humps = [] @@ -16,6 +52,9 @@ def _file_with_extension(src, ext): return "".join(elements[:-1] + [basename, ext]) def _protoc_invocation(srcs, flags): + """Returns a command line to invoke protoc from a genrule, on the given + sources, using the given flags. + """ protoc_command = "$(location //external:protoc) -I . " srcs_params = "" for src in srcs: @@ -23,6 +62,9 @@ def _protoc_invocation(srcs, flags): return protoc_command + flags + srcs_params def objc_proto_library(name, srcs, visibility=None): + """Declares an objc_library for the code generated by protoc from the given + proto sources. This generated code doesn't include proto services. + """ h_files = [] m_files = [] for src in srcs: @@ -48,6 +90,10 @@ def objc_proto_library(name, srcs, visibility=None): ) def objc_grpc_library(name, services, other_messages, visibility=None): + """Declares an objc_library for the code generated by gRPC and protoc from the + given proto sources (services and other_messages). The generated code doesn't + include proto services of the files passed as other_messages. + """ objc_proto_library(name + "_messages", services + other_messages) h_files = [] @@ -57,7 +103,8 @@ def objc_grpc_library(name, services, other_messages, visibility=None): h_files += [_file_with_extension(src, ".pbrpc.h")] m_files += [_file_with_extension(src, ".pbrpc.m")] - protoc_flags = "--grpc_out=$(GENDIR) --plugin=protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)" + protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" + + "protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)") native.genrule( name = name + "_codegen", -- cgit v1.2.3