Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.1 KiB

--[[
The upb compiler. It can write two different kinds of output
files:
- generated code for a C API (foo.upb.h, foo.upb.c)
- (obsolete): definitions of upb defs. (foo.upbdefs.h, foo.upbdefs.c)
--]]
local dump_cinit = require "dump_cinit"
local upb = require "upb"
local generate_upbdefs = false
local outdir = "."
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
end
else
if src then
print("upbc can only handle one input file at a time.")
return 1
end
src = argument
end
i = i + 1
end
Changes to Lua module loading, and file generation. This change has several parts: 1. Resurrected tools/upbc. The code was all there but the build was broken for open-source. Now you can type "make tools/upbc" and it will build all necessary Lua modules and create a robust shell script for running upbc. 2. Changed Lua module loading to no longer rely on OS-level .so dependencies. The net effect of this is that you now only need to set LUA_PATH and LUA_CPATH; setting LD_LIBRARY_PATH or rpaths is no longer required. Downside: this drops compatibility with Lua 5.1, since it depends on a feature that only exists in Lua >=5.2 (and LuaJIT). 3. Since upbc works again, I fixed the re-generation of the descriptor files (descriptor.upb.h, descriptor.upb.c). "make genfiles" will re-generate these as well as the JIT code generator. 4. Added a Travis test target that ensures that the checked-in generated files are not out of date. I would do this for the Ragel generated file also, but we can't count on all versions of Ragel to necessarily generate identical output. 5. Changed Makefile to no longer automatically run Ragel to regenerate the JSON parser. This is unfortuante, because it's convenient when you're developing the JSON parser. However, "git clone" sometimes skews the timestamps a little bit so that "make" thinks it needs to regenerate these files for a fresh "git clone." This would normally be harmless, but if the user doesn't have Ragel installed, it makes the build fail completely. So now you have to explicitly regenerate the Ragel output. If you want to you can uncomment the auto-generation during development.
10 years ago
if not src then
print("Usage: upbc [--generate-upbdefs] <binary descriptor>")
Changes to Lua module loading, and file generation. This change has several parts: 1. Resurrected tools/upbc. The code was all there but the build was broken for open-source. Now you can type "make tools/upbc" and it will build all necessary Lua modules and create a robust shell script for running upbc. 2. Changed Lua module loading to no longer rely on OS-level .so dependencies. The net effect of this is that you now only need to set LUA_PATH and LUA_CPATH; setting LD_LIBRARY_PATH or rpaths is no longer required. Downside: this drops compatibility with Lua 5.1, since it depends on a feature that only exists in Lua >=5.2 (and LuaJIT). 3. Since upbc works again, I fixed the re-generation of the descriptor files (descriptor.upb.h, descriptor.upb.c). "make genfiles" will re-generate these as well as the JIT code generator. 4. Added a Travis test target that ensures that the checked-in generated files are not out of date. I would do this for the Ragel generated file also, but we can't count on all versions of Ragel to necessarily generate identical output. 5. Changed Makefile to no longer automatically run Ragel to regenerate the JSON parser. This is unfortuante, because it's convenient when you're developing the JSON parser. However, "git clone" sometimes skews the timestamps a little bit so that "make" thinks it needs to regenerate these files for a fresh "git clone." This would normally be harmless, but if the user doesn't have Ragel installed, it makes the build fail completely. So now you have to explicitly regenerate the Ragel output. If you want to you can uncomment the auto-generation during development.
10 years ago
return 1
end
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")
local files = upb.load_descriptor(descriptor)
local symtab = upb.SymbolTable()
for _, file in ipairs(files) do
symtab:add_file(file)
local outbase = strip_proto(file:name())
-- Write upbdefs.
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
os.execute(string.format("mkdir -p `dirname %s`", outbase))
assert(generate_upbdefs)
-- Legacy generated defs.
local hfile = open(hfilename)
local cfile = open(cfilename)
local happend = dump_cinit.file_appender(hfile)
local cappend = dump_cinit.file_appender(cfile)
dump_cinit.dump_defs(file, happend, cappend)
hfile:close()
cfile:close()
end