From 9ce4a77f61c134bbed28bfd5be5cd7dc0e80f5e3 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Wed, 14 Nov 2018 16:46:19 -0800 Subject: [PATCH] Updates to build system for upb C++ compiler. --- BUILD | 42 +++++++++++++++++++++++++++++++--------- WORKSPACE | 6 ++++++ build_defs.bzl | 28 +++++++++++++-------------- tools/make_cmakelists.py | 2 +- 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/BUILD b/BUILD index 458463e089..6503ed0b67 100644 --- a/BUILD +++ b/BUILD @@ -249,7 +249,7 @@ cc_test( upb_proto_library( name = "conformance_proto_upb", - upbc = ":upbc", + upbc = ":protoc-gen-upb", deps = [ "@com_google_protobuf//:conformance_proto", "@com_google_protobuf//:test_messages_proto3_proto", @@ -373,13 +373,33 @@ lua_test( # upb compiler ################################################################# lua_binary( - name = "upbc", + name = "lua_upbc", luadeps = [ "lua/upbc_lib", ], luamain = "tools/upbc.lua", ) +cc_library( + name = "upbc_generator", + hdrs = ["upbc/generator.h"], + srcs = ["upbc/generator.cc", "upbc/message_layout.h", "upbc/message_layout.cc"], + deps = [ + "@com_google_protobuf//:protobuf", + "@com_google_protobuf//:protoc_lib", + "@absl//absl/strings", + ], +) + +cc_binary( + name = "protoc-gen-upb", + srcs = ["upbc/main.cc"], + deps = [ + ":upbc_generator", + "@com_google_protobuf//:protoc_lib", + ], +) + # Test the CMake build ######################################################### make_shell_script( @@ -394,6 +414,7 @@ sh_test( data = glob([ "CMakeLists.txt", "google/**/*", + "upbc/**/*", "upb/**/*", "tests/**/*", ]) + [ @@ -458,8 +479,8 @@ genrule( "generated/upb/descriptor/descriptor.upbdefs.h", "generated/upb/descriptor/descriptor.upbdefs.c", ], - cmd = "UPBC=$$PWD/$(location :upbc); INFILE=$$PWD/$<; cd $(GENDIR)/generated && $$UPBC --generate-upbdefs $$INFILE", - tools = [":upbc"], + cmd = "UPBC=$$PWD/$(location :lua_upbc); INFILE=$$PWD/$<; cd $(GENDIR)/generated && $$UPBC --generate-upbdefs $$INFILE", + tools = [":lua_upbc"], ) proto_library( @@ -478,13 +499,16 @@ genrule( genrule( name = "generate_descriptor_c", - srcs = ["generated/google/protobuf/descriptor.pb"], + srcs = ["google/protobuf/descriptor.proto"], outs = [ "generated/google/protobuf/descriptor.upb.h", "generated/google/protobuf/descriptor.upb.c", ], - cmd = "UPBC=$$PWD/$(location :upbc); INFILE=$$PWD/$<; cd $(GENDIR)/generated && $$UPBC $$INFILE", - tools = [":upbc"], + cmd = "$(location @com_google_protobuf//:protoc) $< --upb_out=$(GENDIR)/generated --plugin=protoc-gen-upb=$(location :protoc-gen-upb)", + tools = [ + "@com_google_protobuf//:protoc", + ":protoc-gen-upb" + ], ) proto_library( @@ -506,8 +530,8 @@ genrule( "generated/tests/json/test.upbdefs.h", "generated/tests/json/test.upbdefs.c", ], - cmd = "UPBC=$$PWD/$(location :upbc); INFILE=$$PWD/$<; cd $(GENDIR)/generated && $$UPBC --generate-upbdefs $$INFILE", - tools = [":upbc"], + cmd = "UPBC=$$PWD/$(location :lua_upbc); INFILE=$$PWD/$<; cd $(GENDIR)/generated && $$UPBC --generate-upbdefs $$INFILE", + tools = [":lua_upbc"], ) genrule( diff --git a/WORKSPACE b/WORKSPACE index b4d19c50a8..8d208525c3 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -23,6 +23,12 @@ git_repository( #tag = "conformance-build-tag", ) +git_repository( + name = "absl", + commit = "070f6e47b33a2909d039e620c873204f78809492", + remote = "https://github.com/abseil/abseil-cpp.git", +) + http_archive( name = "ragel", sha256 = "5f156edb65d20b856d638dd9ee2dfb43285914d9aa2b6ec779dac0270cd56c3f", diff --git a/build_defs.bzl b/build_defs.bzl index 0e58bbbca4..c1dd4223d5 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -218,31 +218,26 @@ def _remove_up(string): return _remove_suffix(string, ".proto") def _upb_proto_library_srcs_impl(ctx): - descriptors = [] + sources = [] outs = [] + include_dirs = {} for dep in ctx.attr.deps: if hasattr(dep, 'proto'): - for desc in dep.proto.transitive_descriptor_sets: - descriptors.append(desc) for src in dep.proto.transitive_sources: + sources.append(src) + include_dirs[_remove_suffix(src.path, _remove_up(src.short_path) + "." + src.extension)] = True outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + ".upb.h")) outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + ".upb.c")) outdir = _remove_suffix(outs[-1].path, _remove_up(src.short_path) + ".upb.c") - concatenated = ctx.actions.declare_file(ctx.label.name + "_concatenated_descriptor.bin") - descriptor_paths = [d.path for d in descriptors] + source_paths = [d.path for d in sources] + include_args = ["-I" + root for root in include_dirs.keys()] - ctx.actions.run_shell( - inputs = descriptors, - outputs = [concatenated], - progress_message = "Concatenating descriptors", - command = "cat %s > %s" % (" ".join(descriptor_paths), concatenated.path), - ) ctx.actions.run( - inputs = [concatenated], + inputs = [ctx.executable.upbc] + sources, outputs = outs, - executable = ctx.executable.upbc, - arguments = ["--outdir", outdir, concatenated.path], + executable = ctx.executable.protoc, + arguments = ["--upb_out", outdir, "--plugin=protoc-gen-upb=" + ctx.executable.upbc.path] + include_args + source_paths, progress_message = "Generating upb protos", ) @@ -255,6 +250,11 @@ _upb_proto_library_srcs = rule( executable = True, cfg = "host", ), + "protoc": attr.label( + executable = True, + cfg = "host", + default = "@com_google_protobuf//:protoc", + ), "deps": attr.label_list(), } ) diff --git a/tools/make_cmakelists.py b/tools/make_cmakelists.py index 30e1b93dc6..d7996d8748 100755 --- a/tools/make_cmakelists.py +++ b/tools/make_cmakelists.py @@ -34,7 +34,7 @@ class BuildFileFunctions(object): pass def cc_library(self, **kwargs): - if kwargs["name"] == "amalgamation": + if kwargs["name"] == "amalgamation" or kwargs["name"] == "upbc_generator": return files = kwargs.get("srcs", []) + kwargs.get("hdrs", []) self.converter.toplevel += "add_library(%s\n %s)\n" % (