Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: tools/protoc_wrapper/protoc_wrapper.py

Issue 11418310: Linux: update protoc wrapper to allow building with system protobuf. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « build/protoc.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 """A simple wrapper for protoc to add includes in generated cpp headers.""" 6 """
7 A simple wrapper for protoc.
8
9 - Adds includes in generated headers.
10 - Handles building with system protobuf as an option.
11 """
7 12
8 import optparse 13 import optparse
14 import os.path
15 import shutil
9 import subprocess 16 import subprocess
10 import sys 17 import sys
18 import tempfile
11 19
12 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' 20 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n'
13 21
14 def ModifyHeader(header_file, extra_header): 22 def ModifyHeader(header_file, extra_header):
15 """Adds |extra_header| to |header_file|. Returns 0 on success. 23 """Adds |extra_header| to |header_file|. Returns 0 on success.
16 24
17 |extra_header| is the name of the header file to include. 25 |extra_header| is the name of the header file to include.
18 |header_file| is a generated protobuf cpp header. 26 |header_file| is a generated protobuf cpp header.
19 """ 27 """
20 include_point_found = False 28 include_point_found = False
21 header_contents = [] 29 header_contents = []
22 with open(header_file) as f: 30 with open(header_file) as f:
23 for line in f: 31 for line in f:
24 header_contents.append(line) 32 header_contents.append(line)
25 if line == PROTOC_INCLUDE_POINT: 33 if line == PROTOC_INCLUDE_POINT:
26 extra_header_msg = '#include "%s"\n' % extra_header 34 extra_header_msg = '#include "%s"\n' % extra_header
27 header_contents.append(extra_header_msg) 35 header_contents.append(extra_header_msg)
28 include_point_found = True; 36 include_point_found = True;
29 if not include_point_found: 37 if not include_point_found:
30 return 1 38 return 1
31 39
32 with open(header_file, 'wb') as f: 40 with open(header_file, 'wb') as f:
33 f.write(''.join(header_contents)) 41 f.write(''.join(header_contents))
34 return 0 42 return 0
35 43
36 44
45 def RewriteProtoFilesForSystemProtobuf(path):
46 wrapper_dir = tempfile.mkdtemp()
47 try:
48 for filename in os.listdir(path):
49 if not filename.endswith('.proto'):
50 continue
51 with open(os.path.join(path, filename), 'r') as src_file:
52 with open(os.path.join(wrapper_dir, filename), 'w') as dst_file:
53 for line in src_file:
54 # Remove lines that break build with system protobuf.
55 # We cannot optimize for lite runtime, because system lite runtime
56 # does not have a Chromium-specific hack to retain unknown fields.
57 # Similarly, it does not understand corresponding option to control
58 # the usage of that hack.
59 if 'LITE_RUNTIME' in line or 'retain_unknown_fields' in line:
60 continue
61 dst_file.write(line)
62
63 return wrapper_dir
64 except:
65 shutil.rmtree(wrapper_dir)
66 raise
67
68
37 def main(argv): 69 def main(argv):
38 parser = optparse.OptionParser() 70 parser = optparse.OptionParser()
39 parser.add_option('--include', dest='extra_header', 71 parser.add_option('--include', dest='extra_header',
40 help='The extra header to include. This must be specified ' 72 help='The extra header to include. This must be specified '
41 'along with --protobuf.') 73 'along with --protobuf.')
42 parser.add_option('--protobuf', dest='generated_header', 74 parser.add_option('--protobuf', dest='generated_header',
43 help='The c++ protobuf header to add the extra header to. ' 75 help='The c++ protobuf header to add the extra header to. '
44 'This must be specified along with --include.') 76 'This must be specified along with --include.')
77 parser.add_option('--proto-in-dir',
78 help='The directory containing .proto files.')
79 parser.add_option('--proto-in-file', help='Input file to compile.')
80 parser.add_option('--use-system-protobuf', type=int, default=0,
81 help='Option to use system-installed protobuf '
82 'instead of bundled one.')
45 (options, args) = parser.parse_args(sys.argv) 83 (options, args) = parser.parse_args(sys.argv)
46 if len(args) < 2: 84 if len(args) < 2:
47 return 1 85 return 1
48 86
49 # Run what is hopefully protoc. 87 proto_path = options.proto_in_dir
50 ret = subprocess.call(args[1:]) 88 if options.use_system_protobuf == 1:
51 if ret != 0: 89 proto_path = RewriteProtoFilesForSystemProtobuf(proto_path)
52 return ret 90 try:
91 # Run what is hopefully protoc.
92 protoc_args = args[1:]
93 protoc_args += ['--proto_path=%s' % proto_path,
94 os.path.join(proto_path, options.proto_in_file)]
95 ret = subprocess.call(protoc_args)
96 if ret != 0:
97 return ret
98 finally:
99 if options.use_system_protobuf == 1:
100 # Remove temporary directory holding re-written files.
101 shutil.rmtree(proto_path)
53 102
54 # protoc succeeded, check to see if the generated cpp header needs editing. 103 # protoc succeeded, check to see if the generated cpp header needs editing.
55 if not options.extra_header or not options.generated_header: 104 if not options.extra_header or not options.generated_header:
56 return 0 105 return 0
57 return ModifyHeader(options.generated_header, options.extra_header) 106 return ModifyHeader(options.generated_header, options.extra_header)
58 107
59 108
60 if __name__ == '__main__': 109 if __name__ == '__main__':
61 sys.exit(main(sys.argv)) 110 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « build/protoc.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698