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 """ | 11 """ |
12 | 12 |
13 import fnmatch | |
13 import optparse | 14 import optparse |
14 import os.path | 15 import os.path |
15 import shutil | 16 import shutil |
16 import subprocess | 17 import subprocess |
17 import sys | 18 import sys |
18 import tempfile | 19 import tempfile |
19 | 20 |
20 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' | 21 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' |
21 | 22 |
22 def ModifyHeader(header_file, extra_header): | 23 def ModifyHeader(header_file, extra_header): |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 parser.add_option('--proto-in-dir', | 78 parser.add_option('--proto-in-dir', |
78 help='The directory containing .proto files.') | 79 help='The directory containing .proto files.') |
79 parser.add_option('--proto-in-file', help='Input file to compile.') | 80 parser.add_option('--proto-in-file', help='Input file to compile.') |
80 parser.add_option('--use-system-protobuf', type=int, default=0, | 81 parser.add_option('--use-system-protobuf', type=int, default=0, |
81 help='Option to use system-installed protobuf ' | 82 help='Option to use system-installed protobuf ' |
82 'instead of bundled one.') | 83 'instead of bundled one.') |
83 (options, args) = parser.parse_args(sys.argv) | 84 (options, args) = parser.parse_args(sys.argv) |
84 if len(args) < 2: | 85 if len(args) < 2: |
85 return 1 | 86 return 1 |
86 | 87 |
88 # Scan for bad file names, see crbug/386125 for details. | |
cjhopman
2014/06/19 20:19:33
Move this check into a function.
| |
89 badname = False | |
90 real_proto_in_dir = os.path.realpath(options.proto_in_dir) | |
91 for dirpath, dirnames, filenames in os.walk(real_proto_in_dir): | |
92 matches = fnmatch.filter(filenames, '*-*.proto') | |
93 if len(matches) > 0: | |
94 if not badname: | |
95 badname = True | |
96 sys.stderr.write('proto files must not have hyphens in their names (' | |
97 'see issue 386125 for more information):\n') | |
98 for filename in matches: | |
99 sys.stderr.write(' ' + os.path.join(real_proto_in_dir, | |
100 dirpath, filename) + '\n') | |
101 if badname: | |
102 return 1 | |
103 | |
87 proto_path = options.proto_in_dir | 104 proto_path = options.proto_in_dir |
88 if options.use_system_protobuf == 1: | 105 if options.use_system_protobuf == 1: |
89 proto_path = RewriteProtoFilesForSystemProtobuf(proto_path) | 106 proto_path = RewriteProtoFilesForSystemProtobuf(proto_path) |
90 try: | 107 try: |
91 # Run what is hopefully protoc. | 108 # Run what is hopefully protoc. |
92 protoc_args = args[1:] | 109 protoc_args = args[1:] |
93 protoc_args += ['--proto_path=%s' % proto_path, | 110 protoc_args += ['--proto_path=%s' % proto_path, |
94 os.path.join(proto_path, options.proto_in_file)] | 111 os.path.join(proto_path, options.proto_in_file)] |
95 ret = subprocess.call(protoc_args) | 112 ret = subprocess.call(protoc_args) |
96 if ret != 0: | 113 if ret != 0: |
97 return ret | 114 return ret |
98 finally: | 115 finally: |
99 if options.use_system_protobuf == 1: | 116 if options.use_system_protobuf == 1: |
100 # Remove temporary directory holding re-written files. | 117 # Remove temporary directory holding re-written files. |
101 shutil.rmtree(proto_path) | 118 shutil.rmtree(proto_path) |
102 | 119 |
103 # protoc succeeded, check to see if the generated cpp header needs editing. | 120 # protoc succeeded, check to see if the generated cpp header needs editing. |
104 if not options.extra_header or not options.generated_header: | 121 if not options.extra_header or not options.generated_header: |
105 return 0 | 122 return 0 |
106 return ModifyHeader(options.generated_header, options.extra_header) | 123 return ModifyHeader(options.generated_header, options.extra_header) |
107 | 124 |
108 | 125 |
109 if __name__ == '__main__': | 126 if __name__ == '__main__': |
110 sys.exit(main(sys.argv)) | 127 sys.exit(main(sys.argv)) |
OLD | NEW |