Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 1 | # Copyright (C) 2019 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 14 | """Script to wrap protoc execution. |
| 15 | |
| 16 | This script exists to work-around the bad depfile generation by protoc when |
| 17 | generating descriptors.""" |
| 18 | |
| 19 | from __future__ import print_function |
| 20 | import argparse |
| 21 | import os |
| 22 | import sys |
| 23 | import subprocess |
| 24 | import tempfile |
Primiano Tucci | 42433ab | 2020-11-30 18:42:01 +0100 | [diff] [blame] | 25 | import uuid |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 26 | |
Matthew Clarkson | 9a5dfa5 | 2019-10-03 09:54:04 +0100 | [diff] [blame] | 27 | from codecs import open |
| 28 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 29 | |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 30 | def main(): |
| 31 | parser = argparse.ArgumentParser() |
| 32 | parser.add_argument('--descriptor_set_out', default=None) |
| 33 | parser.add_argument('--dependency_out', default=None) |
| 34 | parser.add_argument('protoc') |
| 35 | args, remaining = parser.parse_known_args() |
| 36 | |
| 37 | if args.dependency_out and args.descriptor_set_out: |
Primiano Tucci | 42433ab | 2020-11-30 18:42:01 +0100 | [diff] [blame] | 38 | tmp_path = os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) |
| 39 | custom = [ |
| 40 | '--descriptor_set_out', args.descriptor_set_out, '--dependency_out', |
| 41 | tmp_path |
| 42 | ] |
| 43 | try: |
| 44 | cmd = [args.protoc] + custom + remaining |
| 45 | subprocess.check_call(cmd) |
| 46 | with open(tmp_path, 'rb') as tmp_rd: |
| 47 | dependency_data = tmp_rd.read().decode('utf-8') |
| 48 | finally: |
| 49 | if os.path.exists(tmp_path): |
| 50 | os.unlink(tmp_path) |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 51 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 52 | with open(args.dependency_out, 'w', encoding='utf-8') as f: |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 53 | f.write(args.descriptor_set_out + ":") |
| 54 | f.write(dependency_data) |
| 55 | else: |
Matthew Clarkson | 9a5dfa5 | 2019-10-03 09:54:04 +0100 | [diff] [blame] | 56 | subprocess.check_call(sys.argv[1:]) |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 57 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 58 | |
Lalit Maganti | e87838f | 2019-06-25 18:31:49 +0100 | [diff] [blame] | 59 | if __name__ == '__main__': |
| 60 | sys.exit(main()) |