From a7ebe54eb1cc09cc50cf97af8436835ead4cbbee Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 3 Nov 2018 18:18:47 -0700 Subject: [PATCH] Conformance tests work under Bazel. --- BUILD | 79 +++++++++++++++++++++------------- WORKSPACE | 24 +++++++---- build_defs.bzl | 95 +++++++++++++++++++++++++++++++++++++---- tests/conformance_upb.c | 4 +- tools/upbc.lua | 22 +++++++--- 5 files changed, 170 insertions(+), 54 deletions(-) diff --git a/BUILD b/BUILD index 6f295a0e39..b636e15518 100644 --- a/BUILD +++ b/BUILD @@ -5,7 +5,9 @@ load( "lua_binary", "lua_test", "generated_file_staleness_test", + "make_shell_script", "upb_amalgamation", + "upb_proto_library", ) # C/C++ rules ################################################################## @@ -202,6 +204,43 @@ cc_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_cclibrary( @@ -222,33 +261,21 @@ lua_cclibrary( lua_library( name = "lua/upb", - srcs = [ - "upb/bindings/lua/upb.lua", - ], + srcs = ["upb/bindings/lua/upb.lua"], strip_prefix = "upb/bindings/lua", - luadeps = [ - "lua/upb_c", - ], + luadeps = ["lua/upb_c"], ) lua_cclibrary( name = "lua/upb/table_c", - srcs = [ - "upb/bindings/lua/upb/table.c", - ], - luadeps = [ - "lua/upb_c", - ], - deps = [ - "upb", - ], + srcs = ["upb/bindings/lua/upb/table.c"], + luadeps = ["lua/upb_c"], + deps = ["upb"], ) lua_library( name = "lua/upb/table", - srcs = [ - "upb/bindings/lua/upb/table.lua", - ], + srcs = ["upb/bindings/lua/upb/table.lua"], strip_prefix = "upb/bindings/lua", luadeps = [ "lua/upb", @@ -258,22 +285,14 @@ lua_library( lua_cclibrary( name = "lua/upb/pb_c", - srcs = [ - "upb/bindings/lua/upb/pb.c", - ], - luadeps = [ - "lua/upb_c", - ], - deps = [ - "upb_pb", - ], + srcs = ["upb/bindings/lua/upb/pb.c"], + luadeps = ["lua/upb_c"], + deps = ["upb_pb"], ) lua_library( name = "lua/upb/pb", - srcs = [ - "upb/bindings/lua/upb/pb.lua", - ], + srcs = ["upb/bindings/lua/upb/pb.lua"], strip_prefix = "upb/bindings/lua", luadeps = [ "lua/upb", diff --git a/WORKSPACE b/WORKSPACE index 299bfa7567..b4d19c50a8 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -2,6 +2,7 @@ workspace(name = "upb") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") http_archive( name = "lua", @@ -14,13 +15,12 @@ http_archive( ], ) -http_archive( +git_repository( name = "com_google_protobuf", - sha256 = "d7a221b3d4fb4f05b7473795ccea9e05dab3b8721f6286a95fffbffc2d926f8b", - strip_prefix = "protobuf-3.6.1", - urls = [ - "https://github.com/protocolbuffers/protobuf/archive/v3.6.1.zip" - ], + commit = "25feb59620627b673df76813dfd66e3f565765e7", + #sha256 = "d7a221b3d4fb4f05b7473795ccea9e05dab3b8721f6286a95fffbffc2d926f8b", + remote = "https://github.com/haberman/protobuf.git", + #tag = "conformance-build-tag", ) http_archive( @@ -28,7 +28,13 @@ http_archive( sha256 = "5f156edb65d20b856d638dd9ee2dfb43285914d9aa2b6ec779dac0270cd56c3f", build_file = "//:ragel.BUILD", strip_prefix = "ragel-6.10", - urls = [ - "http://www.colm.net/files/ragel/ragel-6.10.tar.gz" - ], + urls = ["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"], ) diff --git a/build_defs.bzl b/build_defs.bzl index f9cadd4874..8df5660a64 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -1,5 +1,5 @@ -shell_find_runfiles = """ +_shell_find_runfiles = """ # --- begin runfiles.bash initialization --- # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). set -euo pipefail @@ -62,6 +62,11 @@ def _remove_prefix(str, prefix): fail("%s doesn't start with %s" % (str, 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 = []): outs = [_remove_prefix(src, strip_prefix + "/") for src in srcs] native.genrule( @@ -76,21 +81,28 @@ def lua_library(name, srcs, strip_prefix, 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): script = name + ".sh" - script_contents = (shell_find_runfiles + """ + make_shell_script( + name = "gen_" + name, + out = script, + contents = """ BASE=$(dirname $(rlocation upb/upb_c.so)) export LUA_CPATH="$BASE/?.so" export LUA_PATH="$BASE/?.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( name = name, srcs = [script], @@ -147,6 +159,8 @@ def generated_file_staleness_test(name, outs, generated_pattern): ], ) +# upb_amalgamation() rule, with file_list aspect. + SrcList = provider( fields = { 'srcs' : 'list of srcs', @@ -192,3 +206,68 @@ upb_amalgamation = rule( "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"], + ) diff --git a/tests/conformance_upb.c b/tests/conformance_upb.c index ccc8a94284..e0a7d8ccd4 100644 --- a/tests/conformance_upb.c +++ b/tests/conformance_upb.c @@ -8,8 +8,8 @@ #include #include -#include "conformance.upb.h" -#include "google/protobuf/test_messages_proto3.upb.h" +#include "conformance/conformance.upb.h" +#include "src/google/protobuf/test_messages_proto3.upb.h" int test_count = 0; diff --git a/tools/upbc.lua b/tools/upbc.lua index 8ac4f735ac..adea3734b6 100644 --- a/tools/upbc.lua +++ b/tools/upbc.lua @@ -13,11 +13,17 @@ local make_c_api = require "make_c_api" local upb = require "upb" 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 == "--generate-upbdefs" then generate_upbdefs = true + elseif argument == "--outdir" then + i = i + 1 + outdir = arg[i] else print("Unknown flag: " .. argument) return 1 @@ -29,6 +35,7 @@ for _, argument in ipairs(arg) do end src = argument end + i = i + 1 end if not src then @@ -40,6 +47,11 @@ function strip_proto(filename) return string.gsub(filename, '%.proto$','') 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. local f = assert(io.open(src, "r"), "couldn't open input file " .. src) local descriptor = f:read("*all") @@ -67,8 +79,8 @@ for _, file in ipairs(files) do if generate_upbdefs then -- Legacy generated defs. - local hfile = assert(io.open(hfilename, "w"), "couldn't open " .. hfilename) - local cfile = assert(io.open(cfilename, "w"), "couldn't open " .. cfilename) + local hfile = open(hfilename) + local cfile = open(cfilename) local happend = dump_cinit.file_appender(hfile) local cappend = dump_cinit.file_appender(cfile) @@ -90,8 +102,8 @@ for _, file in ipairs(files) do print(string.format(" cfilename=%s", cfilename)) end - local hfile = assert(io.open(hfilename, "w"), "couldn't open " .. hfilename) - local cfile = assert(io.open(cfilename, "w"), "couldn't open " .. cfilename) + local hfile = open(hfilename) + local cfile = open(cfilename) local happend = dump_cinit.file_appender(hfile) local cappend = dump_cinit.file_appender(cfile)