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

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: 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 """A simple wrapper for protoc to add includes in generated cpp headers."""
agl 2012/12/04 17:20:45 This comment may need updating to reflect the addi
Paweł Hajdan Jr. 2012/12/04 18:01:51 Done.
7 7
8 import optparse 8 import optparse
9 import os.path
10 import shutil
9 import subprocess 11 import subprocess
10 import sys 12 import sys
13 import tempfile
11 14
12 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' 15 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n'
13 16
14 def ModifyHeader(header_file, extra_header): 17 def ModifyHeader(header_file, extra_header):
15 """Adds |extra_header| to |header_file|. Returns 0 on success. 18 """Adds |extra_header| to |header_file|. Returns 0 on success.
16 19
17 |extra_header| is the name of the header file to include. 20 |extra_header| is the name of the header file to include.
18 |header_file| is a generated protobuf cpp header. 21 |header_file| is a generated protobuf cpp header.
19 """ 22 """
20 include_point_found = False 23 include_point_found = False
21 header_contents = [] 24 header_contents = []
22 with open(header_file) as f: 25 with open(header_file) as f:
23 for line in f: 26 for line in f:
24 header_contents.append(line) 27 header_contents.append(line)
25 if line == PROTOC_INCLUDE_POINT: 28 if line == PROTOC_INCLUDE_POINT:
26 extra_header_msg = '#include "%s"\n' % extra_header 29 extra_header_msg = '#include "%s"\n' % extra_header
27 header_contents.append(extra_header_msg) 30 header_contents.append(extra_header_msg)
28 include_point_found = True; 31 include_point_found = True;
29 if not include_point_found: 32 if not include_point_found:
30 return 1 33 return 1
31 34
32 with open(header_file, 'wb') as f: 35 with open(header_file, 'wb') as f:
33 f.write(''.join(header_contents)) 36 f.write(''.join(header_contents))
34 return 0 37 return 0
35 38
36 39
40 def RewriteProtoFilesForSystemProtobuf(path):
41 wrapper_dir = tempfile.mkdtemp()
42 try:
43 for filename in os.listdir(path):
44 if not filename.endswith('.proto'):
45 continue
46 with open(os.path.join(path, filename), 'r') as src_file:
agl 2012/12/04 17:20:45 The version of Python that's run on various builde
47 with open(os.path.join(wrapper_dir, filename), 'w') as dst_file:
48 for line in src_file:
49 # Remove lines that break build with system protobuf.
50 # We cannot optimize for lite runtime, because system lite runtime
51 # does not have a Chromium-specific hack to retain unknown fields.
52 # Similarly, it does not understand corresponding option to control
53 # the usage of that hack.
54 if 'LITE_RUNTIME' in line or 'retain_unknown_fields' in line:
55 continue
56 dst_file.write(line)
57
58 return wrapper_dir
59 except:
60 shutil.rmtree(wrapper_dir)
61 raise
62
63
37 def main(argv): 64 def main(argv):
38 parser = optparse.OptionParser() 65 parser = optparse.OptionParser()
39 parser.add_option('--include', dest='extra_header', 66 parser.add_option('--include', dest='extra_header',
40 help='The extra header to include. This must be specified ' 67 help='The extra header to include. This must be specified '
41 'along with --protobuf.') 68 'along with --protobuf.')
42 parser.add_option('--protobuf', dest='generated_header', 69 parser.add_option('--protobuf', dest='generated_header',
43 help='The c++ protobuf header to add the extra header to. ' 70 help='The c++ protobuf header to add the extra header to. '
44 'This must be specified along with --include.') 71 'This must be specified along with --include.')
72 parser.add_option('--proto-in-dir',
73 help='The directory containing .proto files.')
74 parser.add_option('--proto-in-file', help='Input file to compile.')
75 parser.add_option('--use-system-protobuf', type=int, default=0,
76 help='Option to use system-installed protobuf '
77 'instead of bundled one.')
45 (options, args) = parser.parse_args(sys.argv) 78 (options, args) = parser.parse_args(sys.argv)
46 if len(args) < 2: 79 if len(args) < 2:
47 return 1 80 return 1
48 81
49 # Run what is hopefully protoc. 82 proto_path = options.proto_in_dir
50 ret = subprocess.call(args[1:]) 83 if options.use_system_protobuf == 1:
51 if ret != 0: 84 proto_path = RewriteProtoFilesForSystemProtobuf(proto_path)
52 return ret 85 try:
86 # Run what is hopefully protoc.
87 protoc_args = args[1:]
88 protoc_args += ['--proto_path=%s' % proto_path,
89 os.path.join(proto_path, options.proto_in_file)]
90 ret = subprocess.call(protoc_args)
91 if ret != 0:
92 return ret
93 finally:
94 if options.use_system_protobuf == 1:
95 # Remove temporary directory holding re-written files.
96 shutil.rmtree(proto_path)
53 97
54 # protoc succeeded, check to see if the generated cpp header needs editing. 98 # protoc succeeded, check to see if the generated cpp header needs editing.
55 if not options.extra_header or not options.generated_header: 99 if not options.extra_header or not options.generated_header:
56 return 0 100 return 0
57 return ModifyHeader(options.generated_header, options.extra_header) 101 return ModifyHeader(options.generated_header, options.extra_header)
58 102
59 103
60 if __name__ == '__main__': 104 if __name__ == '__main__':
61 sys.exit(main(sys.argv)) 105 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