blob: 5a6b8eefb16c57b547b2a9992b8cede59d1c5448 [file] [log] [blame]
Mathias Ravdd6a6de2018-05-18 10:18:33 +02001# Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
2# for details. All rights reserved. Use of this source code is governed by a
3# BSD-style license that can be found in the LICENSE file.
4
Ian Zerny0f5fc732018-11-15 14:34:41 +01005import glob
Christoffer Quist Adamsend7a5b722022-02-24 18:12:02 +01006import time
Mathias Ravdd6a6de2018-05-18 10:18:33 +02007import subprocess
Jinseong Jeon158a3f12019-02-08 01:40:59 -08008from threading import Timer
Ian Zernye92325b2020-03-13 13:29:27 +01009
10import gradle
11import jdk
Mathias Ravdd6a6de2018-05-18 10:18:33 +020012import utils
13
Ian Zernye92325b2020-03-13 13:29:27 +010014
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020015def run(tool,
16 args,
17 build=None,
18 debug=True,
19 profile=False,
20 track_memory_file=None,
21 extra_args=None,
22 stderr=None,
23 stdout=None,
24 return_stdout=False,
25 timeout=0,
26 quiet=False,
27 cmd_prefix=None,
28 jar=None,
29 main=None,
30 time_consumer=None,
31 debug_agent=None,
32 worker_id=None):
33 cmd = []
34 if cmd_prefix:
35 cmd.extend(cmd_prefix)
36 if build is None:
37 build, args = extract_build_from_args(args)
38 if build:
39 gradle.RunGradle([
40 utils.GRADLE_TASK_R8LIB
41 if tool.startswith('r8lib') else utils.GRADLE_TASK_R8
42 ])
43 if track_memory_file:
44 cmd.extend(['tools/track_memory.sh', track_memory_file])
45 cmd.append(jdk.GetJavaExecutable())
46 if extra_args:
47 cmd.extend(extra_args)
48 if debug_agent is None:
49 debug_agent, args = extract_debug_agent_from_args(args)
50 if debug_agent:
51 cmd.append(
52 '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005'
53 )
54 if debug:
55 cmd.append('-ea')
56 if profile:
57 cmd.append('-agentlib:hprof=cpu=samples,interval=1,depth=8')
58 if jar:
59 cmd.extend(['-cp', jar, main])
60 elif tool == 'r8lib-d8':
61 cmd.extend(['-cp', utils.R8LIB_JAR, 'com.android.tools.r8.D8'])
62 elif tool == 'r8lib-l8':
63 cmd.extend(['-cp', utils.R8LIB_JAR, 'com.android.tools.r8.L8'])
64 elif tool == 'r8lib-r8':
65 cmd.extend(['-cp', utils.R8LIB_JAR, 'com.android.tools.r8.R8'])
66 elif tool == 'r8lib-tracereferences':
67 cmd.extend([
68 '-cp', utils.R8LIB_JAR,
69 'com.android.tools.r8.tracereferences.TraceReferences'
70 ])
71 else:
72 cmd.extend(['-jar', utils.R8_JAR, tool])
73 lib, args = extract_lib_from_args(args)
74 if lib:
75 cmd.extend(["--lib", lib])
76 cmd.extend(args)
77 utils.PrintCmd(cmd, quiet=quiet, worker_id=worker_id)
78 start = time.time()
79 if timeout > 0:
80 kill = lambda process: process.kill()
81 proc = subprocess.Popen(cmd,
82 stdout=subprocess.PIPE,
83 stderr=subprocess.PIPE)
84 timer = Timer(timeout, kill, [proc])
85 try:
86 timer.start()
87 stdout, stderr = proc.communicate()
88 finally:
89 timer.cancel()
90 result = stdout.decode('utf-8') if return_stdout else proc.returncode
91 else:
92 result = (subprocess.check_output(cmd).decode('utf-8') if return_stdout
93 else subprocess.call(cmd, stdout=stdout, stderr=stderr))
94 duration = int((time.time() - start) * 1000)
95 if time_consumer:
96 time_consumer(duration)
97 return result
98
Mathias Ravdd6a6de2018-05-18 10:18:33 +020099
100def extract_build_from_args(input_args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200101 build = True
102 args = []
103 for arg in input_args:
104 if arg in ("--build", "--no-build"):
105 build = arg == "--build"
106 else:
107 args.append(arg)
108 return build, args
109
Søren Gjessec4e6a282018-10-04 12:37:03 +0200110
111def extract_lib_from_args(input_args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200112 lib = None
113 args = []
114 for arg in input_args:
115 if arg == '--lib-android':
116 lib = utils.get_android_jar(28)
117 elif arg == '--lib-java':
118 lib = utils.RT_JAR
119 else:
120 args.append(arg)
121 return lib, args
122
Ian Zernyad5218a2021-01-21 16:07:58 +0100123
124def extract_debug_agent_from_args(input_args):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200125 agent = False
126 args = []
127 for arg in input_args:
128 if arg in ('--debug-agent', '--debug_agent'):
129 agent = True
130 else:
131 args.append(arg)
132 return agent, args