OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """A simple wrapper for protoc to add includes in generated cpp headers.""" |
| 7 |
| 8 import optparse |
| 9 import subprocess |
| 10 import sys |
| 11 |
| 12 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' |
| 13 |
| 14 def ModifyHeader(header_file, extra_header): |
| 15 """Adds |extra_header| to |header_file|. Returns 0 on success. |
| 16 |
| 17 |extra_header| is the name of the header file to include. |
| 18 |header_file| is a generated protobuf cpp header. |
| 19 """ |
| 20 include_point_found = False |
| 21 header_contents = [] |
| 22 with open(header_file) as f: |
| 23 for line in f: |
| 24 header_contents.append(line) |
| 25 if line == PROTOC_INCLUDE_POINT: |
| 26 extra_header_msg = '#include "%s"\n' % extra_header |
| 27 header_contents.append(extra_header_msg) |
| 28 include_point_found = True; |
| 29 if not include_point_found: |
| 30 return 1 |
| 31 |
| 32 with open(header_file, 'wb') as f: |
| 33 f.write(''.join(header_contents)) |
| 34 return 0 |
| 35 |
| 36 |
| 37 def main(argv): |
| 38 parser = optparse.OptionParser() |
| 39 parser.add_option('--include', dest='extra_header', |
| 40 help='The extra header to include. This must be specified ' |
| 41 'along with --protobuf.') |
| 42 parser.add_option('--protobuf', dest='generated_header', |
| 43 help='The c++ protobuf header to add the extra header to. ' |
| 44 'This must be specified along with --include.') |
| 45 (options, args) = parser.parse_args(sys.argv) |
| 46 if len(args) < 2: |
| 47 return 1 |
| 48 |
| 49 # Run what is hopefully protoc. |
| 50 ret = subprocess.call(args[1:]) |
| 51 if ret != 0: |
| 52 return ret |
| 53 |
| 54 # protoc succeeded, check to see if the generated cpp header needs editing. |
| 55 if not options.extra_header or not options.generated_header: |
| 56 return 0 |
| 57 return ModifyHeader(options.generated_header, options.extra_header) |
| 58 |
| 59 |
| 60 if __name__ == '__main__': |
| 61 sys.exit(main(sys.argv)) |
OLD | NEW |