Conformance tests work under Bazel.

pull/13171/head
Josh Haberman 6 years ago
parent 2655ed9cf5
commit a7ebe54eb1
  1. 79
      BUILD
  2. 24
      WORKSPACE
  3. 95
      build_defs.bzl
  4. 4
      tests/conformance_upb.c
  5. 22
      tools/upbc.lua

79
BUILD

@ -5,7 +5,9 @@ load(
"lua_binary", "lua_binary",
"lua_test", "lua_test",
"generated_file_staleness_test", "generated_file_staleness_test",
"make_shell_script",
"upb_amalgamation", "upb_amalgamation",
"upb_proto_library",
) )
# C/C++ rules ################################################################## # C/C++ rules ##################################################################
@ -202,6 +204,43 @@ cc_test(
deps = [":upb_json", ":upb_test"], deps = [":upb_json", ":upb_test"],
) )
upb_proto_library(
name = "conformance_proto_upb",
deps = [
"@com_google_protobuf//:conformance_proto",
"@com_google_protobuf//:test_messages_proto3_proto",
],
upbc = ":upbc",
)
cc_binary(
name = "conformance_upb",
srcs = [
"tests/conformance_upb.c",
],
deps = [
":upb",
":conformance_proto_upb",
],
)
make_shell_script(
name = "gen_test_conformance_upb",
out = "test_conformance_upb.sh",
contents = "$(rlocation com_google_protobuf/conformance_test_runner) $(rlocation upb/conformance_upb)",
)
sh_test(
name = "test_conformance_upb",
srcs = ["test_conformance_upb.sh"],
data = [
":conformance_upb",
"@com_google_protobuf//:conformance_test_runner",
"tests/conformance_upb_failures.txt",
"@bazel_tools//tools/bash/runfiles"
]
)
# Lua libraries. ############################################################### # Lua libraries. ###############################################################
lua_cclibrary( lua_cclibrary(
@ -222,33 +261,21 @@ lua_cclibrary(
lua_library( lua_library(
name = "lua/upb", name = "lua/upb",
srcs = [ srcs = ["upb/bindings/lua/upb.lua"],
"upb/bindings/lua/upb.lua",
],
strip_prefix = "upb/bindings/lua", strip_prefix = "upb/bindings/lua",
luadeps = [ luadeps = ["lua/upb_c"],
"lua/upb_c",
],
) )
lua_cclibrary( lua_cclibrary(
name = "lua/upb/table_c", name = "lua/upb/table_c",
srcs = [ srcs = ["upb/bindings/lua/upb/table.c"],
"upb/bindings/lua/upb/table.c", luadeps = ["lua/upb_c"],
], deps = ["upb"],
luadeps = [
"lua/upb_c",
],
deps = [
"upb",
],
) )
lua_library( lua_library(
name = "lua/upb/table", name = "lua/upb/table",
srcs = [ srcs = ["upb/bindings/lua/upb/table.lua"],
"upb/bindings/lua/upb/table.lua",
],
strip_prefix = "upb/bindings/lua", strip_prefix = "upb/bindings/lua",
luadeps = [ luadeps = [
"lua/upb", "lua/upb",
@ -258,22 +285,14 @@ lua_library(
lua_cclibrary( lua_cclibrary(
name = "lua/upb/pb_c", name = "lua/upb/pb_c",
srcs = [ srcs = ["upb/bindings/lua/upb/pb.c"],
"upb/bindings/lua/upb/pb.c", luadeps = ["lua/upb_c"],
], deps = ["upb_pb"],
luadeps = [
"lua/upb_c",
],
deps = [
"upb_pb",
],
) )
lua_library( lua_library(
name = "lua/upb/pb", name = "lua/upb/pb",
srcs = [ srcs = ["upb/bindings/lua/upb/pb.lua"],
"upb/bindings/lua/upb/pb.lua",
],
strip_prefix = "upb/bindings/lua", strip_prefix = "upb/bindings/lua",
luadeps = [ luadeps = [
"lua/upb", "lua/upb",

@ -2,6 +2,7 @@
workspace(name = "upb") workspace(name = "upb")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
http_archive( http_archive(
name = "lua", name = "lua",
@ -14,13 +15,12 @@ http_archive(
], ],
) )
http_archive( git_repository(
name = "com_google_protobuf", name = "com_google_protobuf",
sha256 = "d7a221b3d4fb4f05b7473795ccea9e05dab3b8721f6286a95fffbffc2d926f8b", commit = "25feb59620627b673df76813dfd66e3f565765e7",
strip_prefix = "protobuf-3.6.1", #sha256 = "d7a221b3d4fb4f05b7473795ccea9e05dab3b8721f6286a95fffbffc2d926f8b",
urls = [ remote = "https://github.com/haberman/protobuf.git",
"https://github.com/protocolbuffers/protobuf/archive/v3.6.1.zip" #tag = "conformance-build-tag",
],
) )
http_archive( http_archive(
@ -28,7 +28,13 @@ http_archive(
sha256 = "5f156edb65d20b856d638dd9ee2dfb43285914d9aa2b6ec779dac0270cd56c3f", sha256 = "5f156edb65d20b856d638dd9ee2dfb43285914d9aa2b6ec779dac0270cd56c3f",
build_file = "//:ragel.BUILD", build_file = "//:ragel.BUILD",
strip_prefix = "ragel-6.10", strip_prefix = "ragel-6.10",
urls = [ urls = ["http://www.colm.net/files/ragel/ragel-6.10.tar.gz"],
"http://www.colm.net/files/ragel/ragel-6.10.tar.gz" )
],
# Used by protobuf.
http_archive(
name = "bazel_skylib",
sha256 = "bbccf674aa441c266df9894182d80de104cabd19be98be002f6d478aaa31574d",
strip_prefix = "bazel-skylib-2169ae1c374aab4a09aa90e65efe1a3aad4e279b",
urls = ["https://github.com/bazelbuild/bazel-skylib/archive/2169ae1c374aab4a09aa90e65efe1a3aad4e279b.tar.gz"],
) )

@ -1,5 +1,5 @@
shell_find_runfiles = """ _shell_find_runfiles = """
# --- begin runfiles.bash initialization --- # --- begin runfiles.bash initialization ---
# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
set -euo pipefail set -euo pipefail
@ -62,6 +62,11 @@ def _remove_prefix(str, prefix):
fail("%s doesn't start with %s" % (str, prefix)) fail("%s doesn't start with %s" % (str, prefix))
return str[len(prefix):] return str[len(prefix):]
def _remove_suffix(str, suffix):
if not str.endswith(suffix):
fail("%s doesn't end with %s" % (str, suffix))
return str[:-len(suffix)]
def lua_library(name, srcs, strip_prefix, luadeps = []): def lua_library(name, srcs, strip_prefix, luadeps = []):
outs = [_remove_prefix(src, strip_prefix + "/") for src in srcs] outs = [_remove_prefix(src, strip_prefix + "/") for src in srcs]
native.genrule( native.genrule(
@ -76,21 +81,28 @@ def lua_library(name, srcs, strip_prefix, luadeps = []):
data = outs + luadeps, data = outs + luadeps,
) )
def make_shell_script(name, contents, out):
script_contents = (_shell_find_runfiles + contents).replace("$", "$$")
native.genrule(
name = "gen_" + name,
outs = [out],
cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % script_contents,
)
def _lua_binary_or_test(name, luamain, luadeps, rule): def _lua_binary_or_test(name, luamain, luadeps, rule):
script = name + ".sh" script = name + ".sh"
script_contents = (shell_find_runfiles + """ make_shell_script(
name = "gen_" + name,
out = script,
contents = """
BASE=$(dirname $(rlocation upb/upb_c.so)) BASE=$(dirname $(rlocation upb/upb_c.so))
export LUA_CPATH="$BASE/?.so" export LUA_CPATH="$BASE/?.so"
export LUA_PATH="$BASE/?.lua" export LUA_PATH="$BASE/?.lua"
$(rlocation lua/lua) $(rlocation upb/tools/upbc.lua) "$@" $(rlocation lua/lua) $(rlocation upb/tools/upbc.lua) "$@"
""").replace("$", "$$") """
native.genrule(
name = "gen_" + name,
outs = [script],
cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % script_contents,
) )
rule( rule(
name = name, name = name,
srcs = [script], srcs = [script],
@ -147,6 +159,8 @@ def generated_file_staleness_test(name, outs, generated_pattern):
], ],
) )
# upb_amalgamation() rule, with file_list aspect.
SrcList = provider( SrcList = provider(
fields = { fields = {
'srcs' : 'list of srcs', 'srcs' : 'list of srcs',
@ -192,3 +206,68 @@ upb_amalgamation = rule(
"outs": attr.output_list(), "outs": attr.output_list(),
} }
) )
# upb_proto_library() rule
def _remove_up(string):
if string.startswith("../"):
string = string[3:]
pos = string.find("/")
string = string[pos + 1:]
return _remove_suffix(string, ".proto")
def _upb_proto_library_srcs_impl(ctx):
descriptors = []
outs = []
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:
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]
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],
outputs = outs,
executable = ctx.executable.upbc,
arguments = ["--outdir", outdir, concatenated.path],
progress_message = "Generating upb protos",
)
return [DefaultInfo(files = depset(outs))]
_upb_proto_library_srcs = rule(
implementation = _upb_proto_library_srcs_impl,
attrs = {
"upbc": attr.label(
executable = True,
cfg = "host",
),
"deps": attr.label_list(),
}
)
def upb_proto_library(name, deps, upbc):
srcs_rule = name + "_srcs.cc"
_upb_proto_library_srcs(
name = srcs_rule,
upbc = upbc,
deps = deps,
)
native.cc_library(
name = name,
srcs = [":" + srcs_rule],
deps = [":upb"],
)

@ -8,8 +8,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "conformance.upb.h" #include "conformance/conformance.upb.h"
#include "google/protobuf/test_messages_proto3.upb.h" #include "src/google/protobuf/test_messages_proto3.upb.h"
int test_count = 0; int test_count = 0;

@ -13,11 +13,17 @@ local make_c_api = require "make_c_api"
local upb = require "upb" local upb = require "upb"
local generate_upbdefs = false local generate_upbdefs = false
local outdir = "."
for _, argument in ipairs(arg) do i = 1
while i <= #arg do
argument = arg[i]
if argument.sub(argument, 1, 2) == "--" then if argument.sub(argument, 1, 2) == "--" then
if argument == "--generate-upbdefs" then if argument == "--generate-upbdefs" then
generate_upbdefs = true generate_upbdefs = true
elseif argument == "--outdir" then
i = i + 1
outdir = arg[i]
else else
print("Unknown flag: " .. argument) print("Unknown flag: " .. argument)
return 1 return 1
@ -29,6 +35,7 @@ for _, argument in ipairs(arg) do
end end
src = argument src = argument
end end
i = i + 1
end end
if not src then if not src then
@ -40,6 +47,11 @@ function strip_proto(filename)
return string.gsub(filename, '%.proto$','') return string.gsub(filename, '%.proto$','')
end end
local function open(filename)
local full_name = outdir .. "/" .. filename
return assert(io.open(full_name, "w"), "couldn't open " .. full_name)
end
-- Open input/output files. -- Open input/output files.
local f = assert(io.open(src, "r"), "couldn't open input file " .. src) local f = assert(io.open(src, "r"), "couldn't open input file " .. src)
local descriptor = f:read("*all") local descriptor = f:read("*all")
@ -67,8 +79,8 @@ for _, file in ipairs(files) do
if generate_upbdefs then if generate_upbdefs then
-- Legacy generated defs. -- Legacy generated defs.
local hfile = assert(io.open(hfilename, "w"), "couldn't open " .. hfilename) local hfile = open(hfilename)
local cfile = assert(io.open(cfilename, "w"), "couldn't open " .. cfilename) local cfile = open(cfilename)
local happend = dump_cinit.file_appender(hfile) local happend = dump_cinit.file_appender(hfile)
local cappend = dump_cinit.file_appender(cfile) local cappend = dump_cinit.file_appender(cfile)
@ -90,8 +102,8 @@ for _, file in ipairs(files) do
print(string.format(" cfilename=%s", cfilename)) print(string.format(" cfilename=%s", cfilename))
end end
local hfile = assert(io.open(hfilename, "w"), "couldn't open " .. hfilename) local hfile = open(hfilename)
local cfile = assert(io.open(cfilename, "w"), "couldn't open " .. cfilename) local cfile = open(cfilename)
local happend = dump_cinit.file_appender(hfile) local happend = dump_cinit.file_appender(hfile)
local cappend = dump_cinit.file_appender(cfile) local cappend = dump_cinit.file_appender(cfile)

Loading…
Cancel
Save