From f3c560122a8ef25f3391207205c7e55c897fd27f Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 13 Apr 2019 14:05:57 -0700 Subject: [PATCH] upb_proto_library() works for local project! Doesn't work for remote projects yet though. --- BUILD | 26 ++++- build_defs.bzl | 131 +++++++++++++---------- tests/json/enum_from_separate_file.proto | 9 ++ 3 files changed, 102 insertions(+), 64 deletions(-) create mode 100644 tests/json/enum_from_separate_file.proto diff --git a/BUILD b/BUILD index b723a2f313..b5df29434e 100644 --- a/BUILD +++ b/BUILD @@ -196,6 +196,7 @@ cc_binary( map_dep("@com_google_protobuf//:protoc_lib"), ], copts = CPPOPTS, + visibility = ["//visibility:public"], ) # We strip the tests and remaining rules from google3 until the upb_proto_library() @@ -299,12 +300,15 @@ cc_test( copts = CPPOPTS, ) +proto_library( + name = "test_json_enum_from_separate", + srcs = ["tests/json/enum_from_separate_file.proto"], + deps = [":test_json_proto"], +) + proto_library( name = "test_json_proto", - srcs = [ - "tests/json/test.proto", - # "tests/json/enum_from_separate_file.proto", - ], + srcs = ["tests/json/test.proto"], ) upb_proto_reflection_library( @@ -313,9 +317,14 @@ upb_proto_reflection_library( deps = ["test_json_proto"], ) +upb_proto_library( + name = "test_json_enum_from_separate_upbproto", + deps = [":test_json_enum_from_separate"], +) + upb_proto_library( name = "test_json_upbproto", - deps = ["test_json_proto"], + deps = [":test_json_proto"], ) cc_test( @@ -335,6 +344,12 @@ upb_proto_library( name = "conformance_proto_upb", deps = [ "@com_google_protobuf//:conformance_proto", + ], +) + +upb_proto_library( + name = "test_messages_proto3_proto_upb", + deps = [ "@com_google_protobuf//:test_messages_proto3_proto", ], ) @@ -347,6 +362,7 @@ cc_binary( copts = COPTS + ["-Ibazel-out/k8-fastbuild/bin"], deps = [ ":conformance_proto_upb", + ":test_messages_proto3_proto_upb", ":upb", ], ) diff --git a/build_defs.bzl b/build_defs.bzl index e8d89871db..ef96885b41 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -46,12 +46,12 @@ def lua_cclibrary(name, srcs, hdrs = [], deps = [], luadeps = []): name = so_rule, linkshared = True, deps = [_librule(name)], - linkopts = select({ - ":darwin": [ - "-undefined dynamic_lookup", - ], - "//conditions:default": [], - }) + linkopts = select({ + ":darwin": [ + "-undefined dynamic_lookup", + ], + "//conditions:default": [], + }), ) native.genrule( @@ -289,34 +289,30 @@ def cc_library_func(ctx, hdrs, srcs, deps): compilation_contexts = [] linking_contexts = [] for dep in deps: - if CcInfo in dep: - compilation_contexts.append(dep[CcInfo].compilation_context) - linking_contexts.append(dep[CcInfo].linking_context) + if CcInfo in dep: + compilation_contexts.append(dep[CcInfo].compilation_context) + linking_contexts.append(dep[CcInfo].linking_context) toolchain = find_cpp_toolchain(ctx) feature_configuration = cc_common.configure_features( - cc_toolchain = toolchain, - requested_features = ctx.features, - unsupported_features = ctx.disabled_features, + cc_toolchain = toolchain, + requested_features = ctx.features, + unsupported_features = ctx.disabled_features, ) compilation_info = cc_common.compile( - ctx = ctx, - feature_configuration = feature_configuration, - cc_toolchain = toolchain, - srcs = srcs, - hdrs = hdrs, - compilation_contexts = compilation_contexts, + ctx = ctx, + feature_configuration = feature_configuration, + cc_toolchain = toolchain, + srcs = srcs, + hdrs = hdrs, + compilation_contexts = compilation_contexts, ) output_file = ctx.new_file(ctx.bin_dir, "lib" + ctx.rule.attr.name + ".a") library_to_link = cc_common.create_library_to_link( - actions = ctx.actions, - feature_configuration = feature_configuration, - cc_toolchain = toolchain, - static_library = output_file, - ) - linking_context = cc_common.create_linking_context( - libraries_to_link = [library_to_link], + actions = ctx.actions, + feature_configuration = feature_configuration, + cc_toolchain = toolchain, + static_library = output_file, ) - archiver_path = cc_common.get_tool_for_action( feature_configuration = feature_configuration, action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME, @@ -332,7 +328,13 @@ def cc_library_func(ctx, hdrs, srcs, deps): action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME, variables = archiver_variables, ) - object_files = compilation_info.cc_compilation_outputs.object_files(use_pic = False) + + # Non-PIC objects only get emitted in opt builds. + use_pic = True + if ctx.var.get("COMPILATION_MODE") == "opt": + use_pic = False + + object_files = compilation_info.cc_compilation_outputs.object_files(use_pic = use_pic) args = ctx.actions.args() args.add_all(command_line) args.add_all(object_files) @@ -357,39 +359,44 @@ def cc_library_func(ctx, hdrs, srcs, deps): ), outputs = [output_file], ) + linking_context = cc_common.create_linking_context( + libraries_to_link = [library_to_link], + ) return [ - CcInfo( - compilation_context = compilation_info.compilation_context, - linking_context = linking_context, - ) + CcInfo( + compilation_context = compilation_info.compilation_context, + linking_context = linking_context, + ), ] def _upb_proto_library_aspect_impl(target, ctx): - proto_sources = target[ProtoInfo].direct_sources - types = { - "srcs": ".upb.c", - "hdrs": ".upb.h", - } - files = _generate_output_files( - ctx = ctx, - package = ctx.label.package, - file_names = proto_sources, - file_types = types, - ) - ctx.actions.run( - inputs = [ctx.executable._upbc] + proto_sources, - outputs = files["srcs"] + files["hdrs"], - executable = ctx.executable._protoc, - arguments = ["--upb_out"], #, ctx.genfiles_dir, "--plugin=protoc-gen-upb=" + ctx.executable._upbc.path] + [file.path for file in files["srcs"]], - progress_message = "Generating upb protos", - ) - return cc_library_func( - ctx = ctx, - hdrs = files["hdrs"], - srcs = files["srcs"], - deps = ctx.rule.attr.deps - ) - + proto_sources = target[ProtoInfo].direct_sources + types = { + "srcs": ".upb.c", + "hdrs": ".upb.h", + } + files = _generate_output_files( + ctx = ctx, + package = ctx.label.package, + file_names = proto_sources, + file_types = types, + ) + ctx.actions.run( + inputs = depset( + direct = target[ProtoInfo].direct_sources + [ctx.executable._upbc], + transitive = [target[ProtoInfo].transitive_sources], + ), + outputs = files["srcs"] + files["hdrs"], + executable = ctx.executable._protoc, + arguments = ["--upb_out=" + ctx.genfiles_dir.path, "--plugin=protoc-gen-upb=" + ctx.executable._upbc.path] + [file.path for file in proto_sources], + progress_message = "Generating upb protos", + ) + return cc_library_func( + ctx = ctx, + hdrs = files["hdrs"], + srcs = files["srcs"], + deps = ctx.rule.attr.deps + [ctx.attr._upb], + ) _upb_proto_library_aspect = aspect( attrs = { @@ -416,14 +423,20 @@ def _upb_proto_library_impl(ctx): dep = ctx.attr.deps[0] if CcInfo not in dep: fail("proto_library rule must generate CcInfo (have cc_api_version>0).") - return [dep[CcInfo]] - + return [ + DefaultInfo( + files = depset( + [dep[CcInfo].linking_context.libraries_to_link[0].static_library], + ), + ), + dep[CcInfo], + ] upb_proto_library = rule( output_to_genfiles = True, implementation = _upb_proto_library_impl, attrs = { - 'deps': attr.label_list( + "deps": attr.label_list( aspects = [_upb_proto_library_aspect], allow_rules = ["proto_library"], providers = [ProtoInfo], diff --git a/tests/json/enum_from_separate_file.proto b/tests/json/enum_from_separate_file.proto new file mode 100644 index 0000000000..ceb9b42aa1 --- /dev/null +++ b/tests/json/enum_from_separate_file.proto @@ -0,0 +1,9 @@ +syntax = "proto2"; + +import "tests/json/test.proto"; + +package upb.test.json; + +message ImportEnum { + optional MyEnum e = 1; +}