summaryrefslogtreecommitdiff
path: root/tools/upbc.lua
blob: 9d9531c00c926c0a5c054422ac4c0c768a819680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
--[[

  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 make_c_api = require "make_c_api"
local upb = require "upb"

local generate_upbdefs = false

for _, argument in ipairs(arg) do
  if argument.sub(argument, 1, 2) == "--" then
    if argument == "--generate-upbdefs" then
      generate_upbdefs = true
    else
      print("Unknown flag: " .. argument)
      return 1
    end
  else
    src = argument
  end
end

if not src then
  print("Usage: upbc [--generate-upbdefs] <binary descriptor>")
  return 1
end

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()

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))

  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 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

  -- Write C API.
  hfilename = outbase .. ".upb.h"
  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))
  end

  local hfile = assert(io.open(hfilename, "w"), "couldn't open " .. hfilename)
  local cfile = assert(io.open(cfilename, "w"), "couldn't open " .. cfilename)

  local happend = dump_cinit.file_appender(hfile)
  local cappend = dump_cinit.file_appender(cfile)

  make_c_api.write_gencode(file, hfilename, happend, cappend)

  hfile:close()
  cfile:close()
end
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback