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 subprocess |
| 9 import sys |
| 10 |
| 11 PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' |
| 12 |
| 13 def ModifyHeader(header_file, extra_header): |
| 14 """Adds |extra_header| to |header_file|. Returns 0 on success. |
| 15 |
| 16 |extra_header| is the name of the header file to include. |
| 17 |header_file| is a generated protobuf cpp header. |
| 18 """ |
| 19 include_point_found = False |
| 20 header_contents = [] |
| 21 with open(header_file) as f: |
| 22 for line in f: |
| 23 header_contents.append(line) |
| 24 if line == PROTOC_INCLUDE_POINT: |
| 25 extra_header_msg = '#include "%s"\n' % extra_header |
| 26 header_contents.append(extra_header_msg) |
| 27 include_point_found = True; |
| 28 if not include_point_found: |
| 29 return 1 |
| 30 |
| 31 with open(header_file, 'wb') as f: |
| 32 f.write(''.join(header_contents)) |
| 33 return 0 |
| 34 |
| 35 |
| 36 def main(argv): |
| 37 # Expected to be called as: |
| 38 # protoc_wrapper header_to_include:/path/to/cpp.pb.h /path/to/protoc \ |
| 39 # [protoc args] |
| 40 if len(argv) < 3: |
| 41 return 1 |
| 42 |
| 43 # Parse the first argument: |
| 44 combined_wrapper_arg = argv[1] |
| 45 if combined_wrapper_arg.count(':') > 1: |
| 46 return 1 |
| 47 (extra_header, generated_header) = combined_wrapper_arg.split(':') |
| 48 if not generated_header: |
| 49 return 1 |
| 50 |
| 51 # Run what is hopefully protoc. |
| 52 try: |
| 53 ret = subprocess.call(argv[2:]) |
| 54 if ret != 0: |
| 55 return ret |
| 56 except: |
| 57 return 1 |
| 58 |
| 59 # protoc succeeded, check to see if the generated cpp header needs editing. |
| 60 if not extra_header: |
| 61 return 0 |
| 62 return ModifyHeader(generated_header, extra_header) |
| 63 |
| 64 |
| 65 if __name__ == '__main__': |
| 66 sys.exit(main(sys.argv)) |
OLD | NEW |