xref: /aosp_15_r20/external/angle/build/android/gyp/trace_event_bytecode_rewriter.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1#!/usr/bin/env python3
2# Copyright 2021 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Wrapper script around TraceEventAdder script."""
6
7import argparse
8import sys
9import tempfile
10import os
11
12from util import build_utils
13import action_helpers  # build_utils adds //build to sys.path.
14
15
16# The real limit is generally >100kb, but 10k seems like a reasonable "it's big"
17# threshold.
18_MAX_CMDLINE = 10000
19
20
21def main(argv):
22  argv = build_utils.ExpandFileArgs(argv[1:])
23  parser = argparse.ArgumentParser()
24  action_helpers.add_depfile_arg(parser)
25  parser.add_argument('--script',
26                      required=True,
27                      help='Path to the java binary wrapper script.')
28  parser.add_argument('--stamp', help='Path to stamp to mark when finished.')
29  parser.add_argument('--classpath', action='append', nargs='+')
30  parser.add_argument('--input-jars', action='append', nargs='+')
31  parser.add_argument('--output-jars', action='append', nargs='+')
32  args = parser.parse_args(argv)
33
34  args.classpath = action_helpers.parse_gn_list(args.classpath)
35  args.input_jars = action_helpers.parse_gn_list(args.input_jars)
36  args.output_jars = action_helpers.parse_gn_list(args.output_jars)
37
38  for output_jar in args.output_jars:
39    jar_dir = os.path.dirname(output_jar)
40    if not os.path.exists(jar_dir):
41      os.makedirs(jar_dir)
42
43  cmd = [
44      args.script, '--classpath', ':'.join(args.classpath),
45      ':'.join(args.input_jars), ':'.join(args.output_jars)
46  ]
47  if sum(len(x) for x in cmd) > _MAX_CMDLINE:
48    # Cannot put --classpath in the args file because that is consumed by the
49    # wrapper script.
50    args_file = tempfile.NamedTemporaryFile(mode='w')
51    args_file.write('\n'.join(cmd[3:]))
52    args_file.flush()
53    cmd[3:] = ['@' + args_file.name]
54
55  build_utils.CheckOutput(cmd, print_stdout=True)
56
57  build_utils.Touch(args.stamp)
58
59  all_input_jars = args.input_jars + args.classpath
60  action_helpers.write_depfile(args.depfile, args.stamp, inputs=all_input_jars)
61
62
63if __name__ == '__main__':
64  sys.exit(main(sys.argv))
65