5A simple wrapper for protoc.
6Script for //third_party/protobuf/proto_library.gni .
8- Inserts #include for extra header automatically.
9- Prevents bad proto names.
12from __future__
import print_function
21PROTOC_INCLUDE_POINT =
"// @@protoc_insertion_point(includes)"
27 if options.endswith(
":"):
33 for filename
in protos:
35 raise RuntimeError(
"Proto file names must not contain hyphens "
36 "(see http://crbug.com/386125 for more information).")
40 if not filename.endswith(
".proto"):
41 raise RuntimeError(
"Invalid proto filename extension: "
43 return filename.rsplit(
".", 1)[0]
47 for filename
in headers:
48 include_point_found =
False
50 with open(filename)
as f:
52 stripped_line = line.strip()
53 contents.append(stripped_line)
54 if stripped_line == PROTOC_INCLUDE_POINT:
55 if include_point_found:
56 raise RuntimeError(
"Multiple include points found.")
57 include_point_found =
True
58 extra_statement =
"#include \"{0}\"".
format(include)
59 contents.append(extra_statement)
61 if not include_point_found:
62 raise RuntimeError(
"Include point not found in header: "
65 with open(filename,
"w")
as f:
71 os.makedirs(os.path.dirname(depfile), exist_ok=
True)
72 with open(depfile,
'w')
as f:
77 with open(outputs)
as f:
78 outs = [line.strip()
for line
in f]
79 with open(dependencies)
as f:
80 deps = [line.strip()
for line
in f]
85 with open(outputs)
as f:
86 outs = [line.strip()
for line
in f]
91 filename = os.path.join(proto_dir, proto)
94 with open(filename)
as f:
97 match = re.match(
r'^\s*import(?:\s+public)?\s+"([^"]+)"\s*;\s*$', line)
102 for candidate_dir
in [proto_dir] + import_dirs:
103 candidate_path = os.path.join(candidate_dir, imported)
104 if os.path.exists(candidate_path):
105 imports.add(candidate_path)
107 imports.update(
ExtractImports(imported, candidate_dir, import_dirs))
114 parser = argparse.ArgumentParser()
115 parser.add_argument(
"--protoc",
116 help=
"Relative path to compiler.")
118 parser.add_argument(
"--proto-in-dir",
119 help=
"Base directory with source protos.")
120 parser.add_argument(
"--cc-out-dir",
121 help=
"Output directory for standard C++ generator.")
122 parser.add_argument(
"--py-out-dir",
123 help=
"Output directory for standard Python generator.")
124 parser.add_argument(
"--plugin-out-dir",
125 help=
"Output directory for custom generator plugin.")
127 parser.add_argument(
"--depfile",
128 help=
"Output location for the protoc depfile.")
129 parser.add_argument(
"--depfile-outputs",
130 help=
"File containing a list of files to be generated.")
132 parser.add_argument(
"--plugin",
133 help=
"Relative path to custom generator plugin.")
134 parser.add_argument(
"--plugin-depfile",
135 help=
"Output location for the plugin depfile.")
136 parser.add_argument(
"--plugin-depfile-deps",
137 help=
"File containing plugin deps not set as other input.")
138 parser.add_argument(
"--plugin-depfile-outputs",
139 help=
"File containing a list of files that will be generated.")
140 parser.add_argument(
"--plugin-options",
141 help=
"Custom generator plugin options.")
142 parser.add_argument(
"--cc-options",
143 help=
"Standard C++ generator options.")
144 parser.add_argument(
"--include",
145 help=
"Name of include to insert into generated headers.")
146 parser.add_argument(
"--import-dir", action=
"append", default=[],
147 help=
"Extra import directory for protos, can be repeated."
149 parser.add_argument(
"--descriptor-set-out",
150 help=
"Passed through to protoc as --descriptor_set_out")
152 parser.add_argument(
"protos", nargs=
"+",
153 help=
"Input protobuf definition file(s).")
155 options = parser.parse_args()
157 proto_dir = os.path.relpath(options.proto_in_dir)
158 protoc_cmd = [os.path.realpath(options.protoc)]
160 protos = options.protos
164 if options.descriptor_set_out:
165 protoc_cmd += [
"--descriptor_set_out", options.descriptor_set_out]
167 if options.py_out_dir:
168 protoc_cmd += [
"--python_out", options.py_out_dir]
170 if options.cc_out_dir:
171 cc_out_dir = options.cc_out_dir
173 protoc_cmd += [
"--cpp_out", cc_options + cc_out_dir]
174 for filename
in protos:
176 headers.append(os.path.join(cc_out_dir, stripped_name +
".pb.h"))
178 if options.plugin_out_dir:
181 "--plugin",
"protoc-gen-plugin=" + os.path.relpath(options.plugin),
182 "--plugin_out", plugin_options + options.plugin_out_dir
185 if options.plugin_depfile:
186 if not options.plugin_depfile_deps
or not options.plugin_depfile_outputs:
187 raise RuntimeError(
"If plugin depfile is supplied, then the plugin "
188 "depfile deps and outputs must be set.")
189 depfile = options.plugin_depfile
190 dep_info = options.plugin_depfile_deps
191 outputs = options.plugin_depfile_outputs
195 if not options.depfile_outputs:
196 raise RuntimeError(
"If depfile is supplied, depfile outputs must also"
199 depfile = options.depfile
200 outputs = options.depfile_outputs
207 protoc_cmd += [
"--proto_path", proto_dir]
208 for path
in options.import_dir:
209 protoc_cmd += [
"--proto_path", path]
211 protoc_cmd += [os.path.join(proto_dir, name)
for name
in protos]
213 ret = subprocess.call(protoc_cmd)
215 raise RuntimeError(
"Protoc has returned non-zero status: "
222if __name__ ==
"__main__":
225 except RuntimeError
as e:
uint32_t uint32_t * format
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not set
def StripProtoExtension(filename)
def FormatGeneratorOptions(options)
def WriteProtocDepfile(depfile, outputs, deps_list)
def VerifyProtoNames(protos)
def WritePluginDepfile(depfile, outputs, dependencies)
def WriteIncludes(headers, include)
def WriteDepfile(depfile, out_list, dep_list)
def ExtractImports(proto, proto_dir, import_dirs)
def print(*args, **kwargs)
static SkString join(const CommandLineFlags::StringArray &)