| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 A simple wrapper for protoc. | 7 A simple wrapper for protoc. |
| 8 | 8 |
| 9 - Adds includes in generated headers. | 9 - Adds includes in generated headers. |
| 10 - Handles building with system protobuf as an option. | 10 - Handles building with system protobuf as an option. |
| 11 - Able to generate C++ target with protoc plugin. |
| 11 """ | 12 """ |
| 12 | 13 |
| 13 import fnmatch | 14 import fnmatch |
| 14 import optparse | 15 import optparse |
| 15 import os.path | 16 import os.path |
| 16 import shutil | 17 import shutil |
| 17 import subprocess | 18 import subprocess |
| 18 import sys | 19 import sys |
| 19 import tempfile | 20 import tempfile |
| 20 | 21 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 'along with --protobuf.') | 95 'along with --protobuf.') |
| 95 parser.add_option('--protobuf', dest='generated_header', | 96 parser.add_option('--protobuf', dest='generated_header', |
| 96 help='The c++ protobuf header to add the extra header to. ' | 97 help='The c++ protobuf header to add the extra header to. ' |
| 97 'This must be specified along with --include.') | 98 'This must be specified along with --include.') |
| 98 parser.add_option('--proto-in-dir', | 99 parser.add_option('--proto-in-dir', |
| 99 help='The directory containing .proto files.') | 100 help='The directory containing .proto files.') |
| 100 parser.add_option('--proto-in-file', help='Input file to compile.') | 101 parser.add_option('--proto-in-file', help='Input file to compile.') |
| 101 parser.add_option('--use-system-protobuf', type=int, default=0, | 102 parser.add_option('--use-system-protobuf', type=int, default=0, |
| 102 help='Option to use system-installed protobuf ' | 103 help='Option to use system-installed protobuf ' |
| 103 'instead of bundled one.') | 104 'instead of bundled one.') |
| 105 parser.add_option('--cc-generator-plugin', default='', |
| 106 help='Compile C++ target with protoc generator plugin. ' |
| 107 'Expected .pb.h and .pb.cc as output.') |
| 104 (options, args) = parser.parse_args(sys.argv) | 108 (options, args) = parser.parse_args(sys.argv) |
| 105 if len(args) < 2: | 109 if len(args) < 2: |
| 106 return 1 | 110 return 1 |
| 107 | 111 |
| 108 if ScanForBadFiles(options.proto_in_dir): | 112 if ScanForBadFiles(options.proto_in_dir): |
| 109 return 1 | 113 return 1 |
| 110 | 114 |
| 111 proto_path = options.proto_in_dir | 115 proto_path = options.proto_in_dir |
| 112 if options.use_system_protobuf == 1: | 116 if options.use_system_protobuf == 1: |
| 113 proto_path = RewriteProtoFilesForSystemProtobuf(proto_path) | 117 proto_path = RewriteProtoFilesForSystemProtobuf(proto_path) |
| 114 try: | 118 try: |
| 115 # Run what is hopefully protoc. | 119 # Run what is hopefully protoc. |
| 116 protoc_args = args[1:] | 120 protoc = args[1] |
| 121 protoc_args = args[2:] |
| 117 protoc_args += ['--proto_path=%s' % proto_path, | 122 protoc_args += ['--proto_path=%s' % proto_path, |
| 118 os.path.join(proto_path, options.proto_in_file)] | 123 os.path.join(proto_path, options.proto_in_file)] |
| 119 ret = subprocess.call(protoc_args) | 124 if options.cc_generator_plugin != '': |
| 125 args = [x.replace('--cpp_out', '--plugin_out') for x in protoc_args] |
| 126 protoc_args = ['--plugin', |
| 127 'protoc-gen-plugin=' + options.cc_generator_plugin] + args |
| 128 ret = subprocess.call([protoc] + protoc_args) |
| 120 if ret != 0: | 129 if ret != 0: |
| 121 return ret | 130 return ret |
| 122 finally: | 131 finally: |
| 123 if options.use_system_protobuf == 1: | 132 if options.use_system_protobuf == 1: |
| 124 # Remove temporary directory holding re-written files. | 133 # Remove temporary directory holding re-written files. |
| 125 shutil.rmtree(proto_path) | 134 shutil.rmtree(proto_path) |
| 126 | 135 |
| 127 # protoc succeeded, check to see if the generated cpp header needs editing. | 136 # protoc succeeded, check to see if the generated cpp header needs editing. |
| 128 if not options.extra_header or not options.generated_header: | 137 if not options.extra_header or not options.generated_header: |
| 129 return 0 | 138 return 0 |
| 130 return ModifyHeader(options.generated_header, options.extra_header) | 139 return ModifyHeader(options.generated_header, options.extra_header) |
| 131 | 140 |
| 132 | 141 |
| 133 if __name__ == '__main__': | 142 if __name__ == '__main__': |
| 134 sys.exit(main(sys.argv)) | 143 sys.exit(main(sys.argv)) |
| OLD | NEW |