forked from indygreg/lua-protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotoc-gen-lua
More file actions
executable file
·85 lines (70 loc) · 2.71 KB
/
protoc-gen-lua
File metadata and controls
executable file
·85 lines (70 loc) · 2.71 KB
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
#!/usr/bin/python
# Copyright 2010 Gregory Szorc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This is a compiler plugin for protoc
# see http://code.google.com/apis/protocolbuffers/docs/reference/other.html
#
# It produces C++ code that defines Lua userdata types for protocol buffer
# messages. This C++ code makes calls to the C++ code generated by Google's
# C++ generator that ships with Protocol Buffers.
#
# Like the C++ generator, we put all messages from the same file into the
# same output file.
#
# Yes, it is currently written in Python. That's how bootstrapping works,
# people.
from lua_protobuf.generator import file_source, file_header, lua_protobuf_header, lua_protobuf_source
from google.protobuf.descriptor import FieldDescriptor
from google.protobuf.compiler.plugin_pb2 import CodeGeneratorRequest, CodeGeneratorResponse
from sys import stdin, stdout, stderr
# Python 3 should use binary buffer object
try:
stdin = stdin.buffer
except AttributeError:
pass
try:
stdout = stdout.buffer
except AttributeError:
pass
serialized = stdin.read()
request = CodeGeneratorRequest()
request.ParseFromString(serialized)
response = CodeGeneratorResponse()
# each input file to the compiler
for i in range(0, len(request.proto_file)):
file_descriptor = request.proto_file[i]
filename = file_descriptor.name
package = file_descriptor.package
# for now, we require package, which is bad
# TODO fix this
if not package:
response.error = 'file seen without package. lua-protobuf currently requires a package on every proto file: %s' % filename
break
define_value = package.replace('.', '_').upper()
cpp_header = '%s.pb.h' % package.replace('.', '/')
cpp_namespace = '::%s' % package.replace('.', '::')
f = response.file.add()
f.name = '%s.pb-lua.h' % package.replace('.', '/')
f.content = file_header(file_descriptor)
f = response.file.add()
f.name = '%s.pb-lua.cc' % package.replace('.', '/')
f.content = file_source(file_descriptor)
f = response.file.add()
f.name = 'lua-protobuf.h'
f.content = lua_protobuf_header()
f = response.file.add()
f.name = 'lua-protobuf.cc'
f.content = lua_protobuf_source()
stdout.write(response.SerializeToString())
exit(0)