upb_proto_library() works for local project!

Doesn't work for remote projects yet though.
pull/13171/head
Josh Haberman 6 years ago
parent 02e26c5004
commit f3c560122a
  1. 26
      BUILD
  2. 131
      build_defs.bzl
  3. 9
      tests/json/enum_from_separate_file.proto

26
BUILD

@ -196,6 +196,7 @@ cc_binary(
map_dep("@com_google_protobuf//:protoc_lib"), map_dep("@com_google_protobuf//:protoc_lib"),
], ],
copts = CPPOPTS, copts = CPPOPTS,
visibility = ["//visibility:public"],
) )
# We strip the tests and remaining rules from google3 until the upb_proto_library() # We strip the tests and remaining rules from google3 until the upb_proto_library()
@ -299,12 +300,15 @@ cc_test(
copts = CPPOPTS, copts = CPPOPTS,
) )
proto_library(
name = "test_json_enum_from_separate",
srcs = ["tests/json/enum_from_separate_file.proto"],
deps = [":test_json_proto"],
)
proto_library( proto_library(
name = "test_json_proto", name = "test_json_proto",
srcs = [ srcs = ["tests/json/test.proto"],
"tests/json/test.proto",
# "tests/json/enum_from_separate_file.proto",
],
) )
upb_proto_reflection_library( upb_proto_reflection_library(
@ -313,9 +317,14 @@ upb_proto_reflection_library(
deps = ["test_json_proto"], deps = ["test_json_proto"],
) )
upb_proto_library(
name = "test_json_enum_from_separate_upbproto",
deps = [":test_json_enum_from_separate"],
)
upb_proto_library( upb_proto_library(
name = "test_json_upbproto", name = "test_json_upbproto",
deps = ["test_json_proto"], deps = [":test_json_proto"],
) )
cc_test( cc_test(
@ -335,6 +344,12 @@ upb_proto_library(
name = "conformance_proto_upb", name = "conformance_proto_upb",
deps = [ deps = [
"@com_google_protobuf//:conformance_proto", "@com_google_protobuf//:conformance_proto",
],
)
upb_proto_library(
name = "test_messages_proto3_proto_upb",
deps = [
"@com_google_protobuf//:test_messages_proto3_proto", "@com_google_protobuf//:test_messages_proto3_proto",
], ],
) )
@ -347,6 +362,7 @@ cc_binary(
copts = COPTS + ["-Ibazel-out/k8-fastbuild/bin"], copts = COPTS + ["-Ibazel-out/k8-fastbuild/bin"],
deps = [ deps = [
":conformance_proto_upb", ":conformance_proto_upb",
":test_messages_proto3_proto_upb",
":upb", ":upb",
], ],
) )

@ -46,12 +46,12 @@ def lua_cclibrary(name, srcs, hdrs = [], deps = [], luadeps = []):
name = so_rule, name = so_rule,
linkshared = True, linkshared = True,
deps = [_librule(name)], deps = [_librule(name)],
linkopts = select({ linkopts = select({
":darwin": [ ":darwin": [
"-undefined dynamic_lookup", "-undefined dynamic_lookup",
], ],
"//conditions:default": [], "//conditions:default": [],
}) }),
) )
native.genrule( native.genrule(
@ -289,34 +289,30 @@ def cc_library_func(ctx, hdrs, srcs, deps):
compilation_contexts = [] compilation_contexts = []
linking_contexts = [] linking_contexts = []
for dep in deps: for dep in deps:
if CcInfo in dep: if CcInfo in dep:
compilation_contexts.append(dep[CcInfo].compilation_context) compilation_contexts.append(dep[CcInfo].compilation_context)
linking_contexts.append(dep[CcInfo].linking_context) linking_contexts.append(dep[CcInfo].linking_context)
toolchain = find_cpp_toolchain(ctx) toolchain = find_cpp_toolchain(ctx)
feature_configuration = cc_common.configure_features( feature_configuration = cc_common.configure_features(
cc_toolchain = toolchain, cc_toolchain = toolchain,
requested_features = ctx.features, requested_features = ctx.features,
unsupported_features = ctx.disabled_features, unsupported_features = ctx.disabled_features,
) )
compilation_info = cc_common.compile( compilation_info = cc_common.compile(
ctx = ctx, ctx = ctx,
feature_configuration = feature_configuration, feature_configuration = feature_configuration,
cc_toolchain = toolchain, cc_toolchain = toolchain,
srcs = srcs, srcs = srcs,
hdrs = hdrs, hdrs = hdrs,
compilation_contexts = compilation_contexts, compilation_contexts = compilation_contexts,
) )
output_file = ctx.new_file(ctx.bin_dir, "lib" + ctx.rule.attr.name + ".a") output_file = ctx.new_file(ctx.bin_dir, "lib" + ctx.rule.attr.name + ".a")
library_to_link = cc_common.create_library_to_link( library_to_link = cc_common.create_library_to_link(
actions = ctx.actions, actions = ctx.actions,
feature_configuration = feature_configuration, feature_configuration = feature_configuration,
cc_toolchain = toolchain, cc_toolchain = toolchain,
static_library = output_file, static_library = output_file,
)
linking_context = cc_common.create_linking_context(
libraries_to_link = [library_to_link],
) )
archiver_path = cc_common.get_tool_for_action( archiver_path = cc_common.get_tool_for_action(
feature_configuration = feature_configuration, feature_configuration = feature_configuration,
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME, 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, action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
variables = archiver_variables, 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 = ctx.actions.args()
args.add_all(command_line) args.add_all(command_line)
args.add_all(object_files) args.add_all(object_files)
@ -357,39 +359,44 @@ def cc_library_func(ctx, hdrs, srcs, deps):
), ),
outputs = [output_file], outputs = [output_file],
) )
linking_context = cc_common.create_linking_context(
libraries_to_link = [library_to_link],
)
return [ return [
CcInfo( CcInfo(
compilation_context = compilation_info.compilation_context, compilation_context = compilation_info.compilation_context,
linking_context = linking_context, linking_context = linking_context,
) ),
] ]
def _upb_proto_library_aspect_impl(target, ctx): def _upb_proto_library_aspect_impl(target, ctx):
proto_sources = target[ProtoInfo].direct_sources proto_sources = target[ProtoInfo].direct_sources
types = { types = {
"srcs": ".upb.c", "srcs": ".upb.c",
"hdrs": ".upb.h", "hdrs": ".upb.h",
} }
files = _generate_output_files( files = _generate_output_files(
ctx = ctx, ctx = ctx,
package = ctx.label.package, package = ctx.label.package,
file_names = proto_sources, file_names = proto_sources,
file_types = types, file_types = types,
) )
ctx.actions.run( ctx.actions.run(
inputs = [ctx.executable._upbc] + proto_sources, inputs = depset(
outputs = files["srcs"] + files["hdrs"], direct = target[ProtoInfo].direct_sources + [ctx.executable._upbc],
executable = ctx.executable._protoc, transitive = [target[ProtoInfo].transitive_sources],
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", outputs = files["srcs"] + files["hdrs"],
) executable = ctx.executable._protoc,
return cc_library_func( arguments = ["--upb_out=" + ctx.genfiles_dir.path, "--plugin=protoc-gen-upb=" + ctx.executable._upbc.path] + [file.path for file in proto_sources],
ctx = ctx, progress_message = "Generating upb protos",
hdrs = files["hdrs"], )
srcs = files["srcs"], return cc_library_func(
deps = ctx.rule.attr.deps ctx = ctx,
) hdrs = files["hdrs"],
srcs = files["srcs"],
deps = ctx.rule.attr.deps + [ctx.attr._upb],
)
_upb_proto_library_aspect = aspect( _upb_proto_library_aspect = aspect(
attrs = { attrs = {
@ -416,14 +423,20 @@ def _upb_proto_library_impl(ctx):
dep = ctx.attr.deps[0] dep = ctx.attr.deps[0]
if CcInfo not in dep: if CcInfo not in dep:
fail("proto_library rule must generate CcInfo (have cc_api_version>0).") 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( upb_proto_library = rule(
output_to_genfiles = True, output_to_genfiles = True,
implementation = _upb_proto_library_impl, implementation = _upb_proto_library_impl,
attrs = { attrs = {
'deps': attr.label_list( "deps": attr.label_list(
aspects = [_upb_proto_library_aspect], aspects = [_upb_proto_library_aspect],
allow_rules = ["proto_library"], allow_rules = ["proto_library"],
providers = [ProtoInfo], providers = [ProtoInfo],

@ -0,0 +1,9 @@
syntax = "proto2";
import "tests/json/test.proto";
package upb.test.json;
message ImportEnum {
optional MyEnum e = 1;
}
Loading…
Cancel
Save