From 2655ed9cf5e448e6ce7195e9afb9361c0a901df0 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 3 Nov 2018 13:04:10 -0700 Subject: [PATCH] Bazel builds everything except conformance_upb. --- BUILD | 275 +++++++++++++++++++++++++++++++++++++++++++-- WORKSPACE | 19 ++++ build_defs.bzl | 106 ++++++++++++++++- upb/json/parser.c | 26 ++--- upb/json/parser.rl | 2 +- 5 files changed, 398 insertions(+), 30 deletions(-) diff --git a/BUILD b/BUILD index 2dd5818ac0..6f295a0e39 100644 --- a/BUILD +++ b/BUILD @@ -1,4 +1,14 @@ -load(":build_defs.bzl", "lua_cclibrary", "lua_library", "lua_binary") +load( + ":build_defs.bzl", + "lua_cclibrary", + "lua_library", + "lua_binary", + "lua_test", + "generated_file_staleness_test", + "upb_amalgamation", +) + +# C/C++ rules ################################################################## cc_library( name = "upb", @@ -33,6 +43,7 @@ cc_library( "upb/sink.h", "upb/upb.h", ], + copts = ["-std=c89", "-pedantic", "-Wno-long-long"], ) cc_library( @@ -46,6 +57,7 @@ cc_library( "upb/descriptor/reader.h", ], deps = [":upb"], + copts = ["-std=c89", "-pedantic", "-Wno-long-long"], ) cc_library( @@ -70,6 +82,7 @@ cc_library( ":upb", ":upb_descriptor", ], + copts = ["-std=c89", "-pedantic", "-Wno-long-long"], ) cc_library( @@ -83,8 +96,114 @@ cc_library( "upb/json/printer.h", ], deps = [":upb"], + copts = ["-std=c89", "-pedantic", "-Wno-long-long"], ) +cc_library( + name = "upb_cc_bindings", + hdrs = [ + "upb/bindings/stdc++/string.h", + ], + deps = [":upb"], +) + +# Amalgamation ################################################################# + +py_binary( + name = "amalgamate", + srcs = ["tools/amalgamate.py"], +) + +upb_amalgamation( + name = "gen_amalgamation", + amalgamator = ":amalgamate", + libs = [ + ":upb", + ":upb_descriptor", + ":upb_pb", + ":upb_json", + ], + outs = [ + "upb.h", + "upb.c", + ], +) + +cc_library( + name = "amalgamation", + hdrs = ["upb.h"], + srcs = ["upb.c"], +) + +# C/C++ tests ################################################################## + +cc_library( + testonly = 1, + name = "upb_test", + hdrs = [ + "tests/upb_test.h", + "tests/test_util.h", + ], + srcs = [ + "tests/testmain.cc", + ], +) + +cc_test( + name = "test_varint", + srcs = ["tests/pb/test_varint.c"], + deps = [":upb_pb", ":upb_test"], +) + +cc_test( + name = "test_def", + srcs = ["tests/test_def.c"], + deps = [":upb_pb", ":upb_test"], +) + +cc_test( + name = "test_handlers", + srcs = ["tests/test_handlers.c"], + deps = [":upb_pb", ":upb_test"], +) + +cc_test( + name = "test_decoder", + srcs = ["tests/pb/test_decoder.cc"], + deps = [":upb_pb", ":upb_test"], +) + +cc_test( + name = "test_encoder", + srcs = ["tests/pb/test_encoder.cc"], + deps = [":upb_pb", ":upb_test", ":upb_cc_bindings"], + data = ["upb/descriptor/descriptor.pb"], +) + +cc_test( + name = "test_cpp", + srcs = ["tests/test_cpp.cc"], + deps = [":upb_descriptor", ":upb", ":upb_pb", ":upb_test"], +) + +cc_test( + name = "test_table", + srcs = ["tests/test_table.cc"], + deps = [":upb", ":upb_test"], +) + +cc_test( + name = "test_json", + srcs = [ + "tests/json/test_json.cc", + "tests/json/test.upbdefs.h", + "tests/json/test.upbdefs.c", + ], + deps = [":upb_json", ":upb_test"], +) + +# Lua libraries. ############################################################### + lua_cclibrary( name = "lua/upb_c", srcs = [ @@ -103,10 +222,10 @@ lua_cclibrary( lua_library( name = "lua/upb", - base = "upb/bindings/lua", srcs = [ - "upb/bindings/lua/upb.lua" + "upb/bindings/lua/upb.lua", ], + strip_prefix = "upb/bindings/lua", luadeps = [ "lua/upb_c", ], @@ -127,10 +246,10 @@ lua_cclibrary( lua_library( name = "lua/upb/table", - base = "upb/bindings/lua", srcs = [ "upb/bindings/lua/upb/table.lua", ], + strip_prefix = "upb/bindings/lua", luadeps = [ "lua/upb", "lua/upb/table_c", @@ -152,10 +271,10 @@ lua_cclibrary( lua_library( name = "lua/upb/pb", - base = "upb/bindings/lua", srcs = [ "upb/bindings/lua/upb/pb.lua", ], + strip_prefix = "upb/bindings/lua", luadeps = [ "lua/upb", "lua/upb/pb_c", @@ -164,20 +283,158 @@ lua_library( lua_library( name = "lua/upbc_lib", - base = "tools", srcs = [ "tools/dump_cinit.lua", "tools/make_c_api.lua", ], + strip_prefix = "tools", luadeps = [ "lua/upb", - ] + "lua/upb/table", + ], +) + +# Lua tests. ################################################################### + +lua_test( + name = "lua/test_upb", + luamain = "tests/bindings/lua/test_upb.lua", + luadeps = ["lua/upb"], +) + +lua_test( + name = "lua/test_upb_pb", + luamain = "tests/bindings/lua/test_upb.pb.lua", + luadeps = ["lua/upb/pb"], ) +# upb compiler ################################################################# + lua_binary( name = "upbc", - main = "tools/upbc.lua", luadeps = [ "lua/upbc_lib", - ] + ], + luamain = "tools/upbc.lua", +) + +# Generated files ############################################################## + +exports_files(["staleness_test.py"]) + +py_library( + name = "staleness_test_lib", + testonly = 1, + srcs = ["staleness_test_lib.py"], +) + +genrule( + name = "make_dynasm_decoder", + srcs = [ + "third_party/dynasm/dynasm.lua", + "third_party/dynasm/dasm_x64.lua", + "third_party/dynasm/dasm_x86.lua", + "upb/pb/compile_decoder_x64.dasc", + ], + outs = ["generated/upb/pb/compile_decoder_x64.h"], + cmd = "LUA_PATH=third_party/dynasm/?.lua $(location @lua//:lua) third_party/dynasm/dynasm.lua -c upb/pb/compile_decoder_x64.dasc > $@", + tools = ["@lua"], +) + +proto_library( + name = "upb_descriptor_proto", + srcs = [ + "upb/descriptor/descriptor.proto" + ], +) + +genrule( + name = "copy_upb_descriptor_pb", + outs = ["generated/upb/descriptor/descriptor.pb"], + srcs = [":upb_descriptor_proto"], + cmd = "cp $< $@", +) + +genrule( + name = "generate_old_upbdefs", + srcs = ["generated/upb/descriptor/descriptor.pb"], + tools = [":upbc"], + outs = [ + "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", +) + +proto_library( + name = "google_descriptor_proto", + srcs = [ + "google/protobuf/descriptor.proto" + ], +) + +genrule( + name = "copy_google_descriptor_pb", + outs = ["generated/google/protobuf/descriptor.pb"], + srcs = [":google_descriptor_proto"], + cmd = "cp $< $@", +) + +genrule( + name = "generate_descriptor_c", + srcs = ["generated/google/protobuf/descriptor.pb"], + tools = [":upbc"], + 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", +) + +proto_library( + name = "json_test_proto", + srcs = ["tests/json/test.proto"], +) + +genrule( + name = "copy_json_test_proto", + srcs = [":json_test_proto"], + outs = ["generated/tests/json/test.proto.pb"], + cmd = "cp $< $@", +) + +genrule( + name = "generated_json_test_proto_upbdefs", + srcs = ["generated/tests/json/test.proto.pb"], + tools = [":upbc"], + outs = [ + "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", +) + +genrule( + name = "generate_json_ragel", + srcs = ["upb/json/parser.rl"], + outs = ["generated/upb/json/parser.c"], + tools = ["@ragel//:ragel"], + cmd = "$(location @ragel//:ragel) -C -o upb/json/parser.c $< && mv upb/json/parser.c $@", +) + +generated_file_staleness_test( + name = "test_generated_files", + outs = [ + "google/protobuf/descriptor.upb.h", + "google/protobuf/descriptor.upb.c", + "upb/pb/compile_decoder_x64.h", + "upb/descriptor/descriptor.upbdefs.c", + "upb/descriptor/descriptor.pb", + "upb/descriptor/descriptor.upbdefs.h", + "upb/json/parser.c", + "tests/json/test.upbdefs.c", + "tests/json/test.upbdefs.h", + "tests/json/test.proto.pb", + ], + generated_pattern = "generated/%s", ) diff --git a/WORKSPACE b/WORKSPACE index b03116f8d9..299bfa7567 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -13,3 +13,22 @@ http_archive( "https://www.lua.org/ftp/lua-5.2.4.tar.gz", ], ) + +http_archive( + name = "com_google_protobuf", + sha256 = "d7a221b3d4fb4f05b7473795ccea9e05dab3b8721f6286a95fffbffc2d926f8b", + strip_prefix = "protobuf-3.6.1", + urls = [ + "https://github.com/protocolbuffers/protobuf/archive/v3.6.1.zip" + ], +) + +http_archive( + name = "ragel", + sha256 = "5f156edb65d20b856d638dd9ee2dfb43285914d9aa2b6ec779dac0270cd56c3f", + build_file = "//:ragel.BUILD", + strip_prefix = "ragel-6.10", + urls = [ + "http://www.colm.net/files/ragel/ragel-6.10.tar.gz" + ], +) diff --git a/build_defs.bzl b/build_defs.bzl index 6023bf6936..f9cadd4874 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -62,8 +62,8 @@ def _remove_prefix(str, prefix): fail("%s doesn't start with %s" % (str, prefix)) return str[len(prefix):] -def lua_library(name, srcs, base, luadeps = []): - outs = [_remove_prefix(src, base + "/") for src in srcs] +def lua_library(name, srcs, strip_prefix, luadeps = []): + outs = [_remove_prefix(src, strip_prefix + "/") for src in srcs] native.genrule( name = name + "_copy", srcs = srcs, @@ -76,7 +76,7 @@ def lua_library(name, srcs, base, luadeps = []): data = outs + luadeps, ) -def lua_binary(name, main, luadeps=[]): +def _lua_binary_or_test(name, luamain, luadeps, rule): script = name + ".sh" script_contents = (shell_find_runfiles + """ @@ -85,16 +85,110 @@ export LUA_CPATH="$BASE/?.so" export LUA_PATH="$BASE/?.lua" $(rlocation lua/lua) $(rlocation upb/tools/upbc.lua) "$@" """).replace("$", "$$") - print(native.repository_name()) native.genrule( name = "gen_" + name, outs = [script], cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % script_contents, ) - native.sh_binary( + rule( name = name, srcs = [script], - data = ["@lua//:lua", "@bazel_tools//tools/bash/runfiles", main] + luadeps, + data = ["@lua//:lua", "@bazel_tools//tools/bash/runfiles", luamain] + luadeps, ) +def lua_binary(name, luamain, luadeps=[]): + _lua_binary_or_test(name, luamain, luadeps, native.sh_binary) + +def lua_test(name, luamain, luadeps=[]): + _lua_binary_or_test(name, luamain, luadeps, native.sh_test) + +def generated_file_staleness_test(name, outs, generated_pattern): + """Tests that checked-in file(s) match the contents of generated file(s). + + The resulting test will verify that all output files exist and have the + correct contents. If the test fails, it can be invoked with --fix to + bring the checked-in files up to date. + + Args: + name: Name of the rule. + outs: the checked-in files that are copied from generated files. + generated_pattern: the pattern for transforming each "out" file into a + generated file. For example, if generated_pattern="generated/%s" then + a file foo.txt will look for generated file generated/foo.txt. + """ + + script_name = name + ".py" + script_src = "//:staleness_test.py" + + # Filter out non-existing rules so Blaze doesn't error out before we even + # run the test. + existing_outs = native.glob(include = outs) + + # The file list contains a few extra bits of information at the end. + # These get unpacked by the Config class in staleness_test_lib.py. + file_list = outs + [generated_pattern, native.package_name() or ".", name] + + native.genrule( + name = name + "_makescript", + outs = [script_name], + srcs = [script_src], + testonly = 1, + cmd = "cat $(location " + script_src + ") > $@; " + + "sed -i 's|INSERT_FILE_LIST_HERE|" + "\\n ".join(file_list) + "|' $@", + ) + + native.py_test( + name = name, + srcs = [script_name], + data = existing_outs + [generated_pattern % file for file in outs], + deps = [ + "//:staleness_test_lib", + ], + ) + +SrcList = provider( + fields = { + 'srcs' : 'list of srcs', + 'hdrs' : 'list of hdrs', + } +) + +def _file_list_aspect_impl(target, ctx): + srcs = [] + hdrs = [] + for src in ctx.rule.attr.srcs: + srcs += src.files.to_list() + for hdr in ctx.rule.attr.hdrs: + hdrs += hdr.files.to_list() + return [SrcList(srcs = srcs, hdrs = hdrs)] + +_file_list_aspect = aspect( + implementation = _file_list_aspect_impl, +) + +def _upb_amalgamation(ctx): + srcs = [] + hdrs = [] + for lib in ctx.attr.libs: + srcs += lib[SrcList].srcs + hdrs += lib[SrcList].hdrs + ctx.actions.run( + inputs = srcs + hdrs, + outputs = ctx.outputs.outs, + arguments = ["", ctx.bin_dir.path + "/"] + [f.path for f in srcs], + progress_message = "Making amalgamation", + executable = ctx.executable.amalgamator, + ) + +upb_amalgamation = rule( + implementation = _upb_amalgamation, + attrs = { + "amalgamator": attr.label( + executable = True, + cfg = "host", + ), + "libs": attr.label_list(aspects = [_file_list_aspect]), + "outs": attr.output_list(), + } +) diff --git a/upb/json/parser.c b/upb/json/parser.c index 3ae4722377..be743662b2 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -2366,7 +2366,7 @@ _match: break; case 2: #line 2023 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 24; goto _again;} } + { p--; {stack[top++] = cs; cs = 24;goto _again;} } break; case 3: #line 2027 "upb/json/parser.rl" @@ -2440,17 +2440,17 @@ _match: #line 2082 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { - {stack[top++] = cs; cs = 48; goto _again;} + {stack[top++] = cs; cs = 48;goto _again;} } else if (is_wellknown_msg(parser, UPB_WELLKNOWN_DURATION)) { - {stack[top++] = cs; cs = 41; goto _again;} + {stack[top++] = cs; cs = 41;goto _again;} } else { - {stack[top++] = cs; cs = 33; goto _again;} + {stack[top++] = cs; cs = 33;goto _again;} } } break; case 21: #line 2093 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 76; goto _again;} } + { p--; {stack[top++] = cs; cs = 76;goto _again;} } break; case 22: #line 2098 "upb/json/parser.rl" @@ -2538,7 +2538,9 @@ _again: switch ( *__acts++ ) { case 0: #line 2019 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } + { p--; {cs = stack[--top]; if ( p == pe ) + goto _test_eof; +goto _again;} } break; case 26: #line 2111 "upb/json/parser.rl" @@ -2564,7 +2566,7 @@ _again: #line 2139 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 2568 "upb/json/parser.c" +#line 2570 "upb/json/parser.c" } } } @@ -2589,7 +2591,7 @@ error: return p - buf; } -bool end(void *closure, const void *hd) { +static bool end(void *closure, const void *hd) { upb_json_parser *parser = closure; /* Prevent compile warning on unused static constants. */ @@ -2603,11 +2605,7 @@ bool end(void *closure, const void *hd) { parse(parser, hd, &eof_ch, 0, NULL); - return parser->current_state >= -#line 2608 "upb/json/parser.c" -105 -#line 2202 "upb/json/parser.rl" -; + return parser->current_state >= 105; } static void json_parser_reset(upb_json_parser *p) { @@ -2622,7 +2620,7 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 2626 "upb/json/parser.c" +#line 2624 "upb/json/parser.c" { cs = json_start; top = 0; diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 29efd09dae..0d59082763 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -2185,7 +2185,7 @@ error: return p - buf; } -bool end(void *closure, const void *hd) { +static bool end(void *closure, const void *hd) { upb_json_parser *parser = closure; /* Prevent compile warning on unused static constants. */