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 logging 9import sys 10import tempfile 11import os 12 13from util import build_utils 14import action_helpers # build_utils adds //build to sys.path. 15 16 17# The real limit is generally >100kb, but 10k seems like a reasonable "it's big" 18# threshold. 19_MAX_CMDLINE = 10000 20 21 22def main(argv): 23 build_utils.InitLogging('TRACE_EVENT_REWRITER_DEBUG') 24 argv = build_utils.ExpandFileArgs(argv[1:]) 25 parser = argparse.ArgumentParser() 26 action_helpers.add_depfile_arg(parser) 27 parser.add_argument('--script', 28 required=True, 29 help='Path to the java binary wrapper script.') 30 parser.add_argument('--stamp', help='Path to stamp to mark when finished.') 31 parser.add_argument('--classpath', action='append', nargs='+') 32 parser.add_argument('--input-jars', action='append', nargs='+') 33 parser.add_argument('--output-jars', action='append', nargs='+') 34 args = parser.parse_args(argv) 35 36 args.classpath = action_helpers.parse_gn_list(args.classpath) 37 args.input_jars = action_helpers.parse_gn_list(args.input_jars) 38 args.output_jars = action_helpers.parse_gn_list(args.output_jars) 39 40 for output_jar in args.output_jars: 41 jar_dir = os.path.dirname(output_jar) 42 if not os.path.exists(jar_dir): 43 os.makedirs(jar_dir) 44 45 cmd = [ 46 args.script, '--classpath', ':'.join(args.classpath), 47 ':'.join(args.input_jars), ':'.join(args.output_jars) 48 ] 49 if sum(len(x) for x in cmd) > _MAX_CMDLINE: 50 # Cannot put --classpath in the args file because that is consumed by the 51 # wrapper script. Keep the args file on disk when debugging. 52 is_debug = logging.getLogger().isEnabledFor(logging.DEBUG) 53 args_file = tempfile.NamedTemporaryFile(mode='w', delete=not is_debug) 54 args_file.write('\n'.join(cmd[3:])) 55 args_file.flush() 56 cmd[3:] = ['@' + args_file.name] 57 58 logging.debug(' '.join(cmd)) 59 60 build_utils.CheckOutput(cmd, print_stdout=True) 61 62 build_utils.Touch(args.stamp) 63 64 all_input_jars = args.input_jars + args.classpath 65 action_helpers.write_depfile(args.depfile, args.stamp, inputs=all_input_jars) 66 67 68if __name__ == '__main__': 69 sys.exit(main(sys.argv)) 70