From e9d79d2441732264e2b990a5b2dc76d13724db07 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Wed, 16 Mar 2016 18:07:32 -0700 Subject: Added upb::FileDef, which represents the file defs are declared in. It is entirely optional: MessageDef/EnumDef can still exist on their own. But this can represent a def's file when it is desirable to do so (eg. for code generators). This approach will require that we change the way we handle extensions. But I think it will be a good change overall. Specifically, we previously handled extensions by duplicating the extended message and then adding the extension as a regular field to the duplicated message. This required also duplicating any messages that could reach the extended message. In the new world we will need a way of declaring and looking up extensions separately from the message being extended. This change also involves some notable changes to the generated code: - files are now called foo.upbdefs.h instead of foo.upb.h. This reflects the fact that we might possibly generate several different output files for a .proto file, and this one is just for defs. - we no longer generate selectors in the .h file. - the upbdefs.c no longer vends a SymbolTable. Now it vends the individual messages (and possibly a FileDef later). I think this will compose better once we can generate files where one generated files imports another. We also make the descriptor reader vend a list of FileDefs now. This is the best conceptual match for parsing a FileDescriptorSet. --- tools/upbc.lua | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) (limited to 'tools/upbc.lua') diff --git a/tools/upbc.lua b/tools/upbc.lua index a248318..5db5fba 100644 --- a/tools/upbc.lua +++ b/tools/upbc.lua @@ -12,40 +12,47 @@ local dump_cinit = require "dump_cinit" local upb = require "upb" local src = arg[1] -local outbase = arg[2] -local basename = arg[3] -if not (src and outbase and basename) then - print("Usage: upbc ") +if not src then + print("Usage: upbc ") return 1 end -local hfilename = outbase .. ".upb.h" -local cfilename = outbase .. ".upb.c" - -if os.getenv("UPBC_VERBOSE") then - print("upbc:") - print(string.format(" source file=%s", src)) - print(string.format(" output file base=%s", outbase)) - print(string.format(" hfilename=%s", hfilename)) - print(string.format(" cfilename=%s", cfilename)) +function strip_proto(filename) + return string.gsub(filename, '%.proto$','') end -- Open input/output files. local f = assert(io.open(src, "r"), "couldn't open input file " .. src) local descriptor = f:read("*all") +local files = upb.load_descriptor(descriptor) local symtab = upb.SymbolTable() -symtab:load_descriptor(descriptor) -os.execute(string.format("mkdir -p `dirname %s`", outbase)) -local hfile = assert(io.open(hfilename, "w"), "couldn't open " .. hfilename) -local cfile = assert(io.open(cfilename, "w"), "couldn't open " .. cfilename) +for _, file in ipairs(files) do + symtab:add_file(file) + local outbase = strip_proto(file:name()) + + local hfilename = outbase .. ".upbdefs.h" + local cfilename = outbase .. ".upbdefs.c" + + if os.getenv("UPBC_VERBOSE") then + print("upbc:") + print(string.format(" source file=%s", src)) + print(string.format(" output file base=%s", outbase)) + print(string.format(" hfilename=%s", hfilename)) + print(string.format(" cfilename=%s", cfilename)) + end -local happend = dump_cinit.file_appender(hfile) -local cappend = dump_cinit.file_appender(cfile) + os.execute(string.format("mkdir -p `dirname %s`", outbase)) + local hfile = assert(io.open(hfilename, "w"), "couldn't open " .. hfilename) + local cfile = assert(io.open(cfilename, "w"), "couldn't open " .. cfilename) --- Dump defs -dump_cinit.dump_defs(symtab, basename, happend, cappend) + local happend = dump_cinit.file_appender(hfile) + local cappend = dump_cinit.file_appender(cfile) -hfile:close() -cfile:close() + -- Dump defs + dump_cinit.dump_defs(file, happend, cappend) + + hfile:close() + cfile:close() +end -- cgit v1.2.3