1#!/usr/bin/env python
2# Copyright 2021 the V8 project 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"""Script to wrap protoc execution.
6
7This script exists to work-around the bad depfile generation by protoc when
8generating descriptors."""
9
10from __future__ import print_function
11import argparse
12import os
13import sys
14import subprocess
15import tempfile
16import uuid
17
18from codecs import open
19
20
21def main():
22  parser = argparse.ArgumentParser()
23  parser.add_argument('--descriptor_set_out', default=None)
24  parser.add_argument('--dependency_out', default=None)
25  parser.add_argument('protoc')
26  args, remaining = parser.parse_known_args()
27
28  if args.dependency_out and args.descriptor_set_out:
29    tmp_path = os.path.join(tempfile.gettempdir(), str(uuid.uuid4()))
30    custom = [
31        '--descriptor_set_out', args.descriptor_set_out, '--dependency_out',
32        tmp_path
33    ]
34    try:
35      cmd = [args.protoc] + custom + remaining
36      subprocess.check_call(cmd)
37      with open(tmp_path, 'rb') as tmp_rd:
38        dependency_data = tmp_rd.read().decode('utf-8')
39    finally:
40      if os.path.exists(tmp_path):
41        os.unlink(tmp_path)
42
43    with open(args.dependency_out, 'w', encoding='utf-8') as f:
44      f.write(args.descriptor_set_out + ":")
45      f.write(dependency_data)
46  else:
47    subprocess.check_call(sys.argv[1:])
48
49
50if __name__ == '__main__':
51  sys.exit(main())
52