xref: /aosp_15_r20/art/test/testrunner/run_build_test_target.py (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*795d594fSAndroid Build Coastguard Worker#
3*795d594fSAndroid Build Coastguard Worker# Copyright 2017, The Android Open Source Project
4*795d594fSAndroid Build Coastguard Worker#
5*795d594fSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*795d594fSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*795d594fSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*795d594fSAndroid Build Coastguard Worker#
9*795d594fSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*795d594fSAndroid Build Coastguard Worker#
11*795d594fSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*795d594fSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*795d594fSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*795d594fSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*795d594fSAndroid Build Coastguard Worker# limitations under the License.
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker"""Build and run go/ab/git_master-art-host target
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard WorkerThis script is executed by the android build server and must not be moved,
20*795d594fSAndroid Build Coastguard Workeror changed in an otherwise backwards-incompatible manner.
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard WorkerProvided with a target name, the script setup the environment for
23*795d594fSAndroid Build Coastguard Workerbuilding the test target by taking config information from
24*795d594fSAndroid Build Coastguard Workerfrom target_config.py.
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard WorkerSee target_config.py for the configuration syntax.
27*795d594fSAndroid Build Coastguard Worker"""
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard Workerimport argparse
30*795d594fSAndroid Build Coastguard Workerimport os
31*795d594fSAndroid Build Coastguard Workerimport pathlib
32*795d594fSAndroid Build Coastguard Workerimport re
33*795d594fSAndroid Build Coastguard Workerimport subprocess
34*795d594fSAndroid Build Coastguard Workerimport sys
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Workerfrom target_config import target_config
37*795d594fSAndroid Build Coastguard Workerimport env
38*795d594fSAndroid Build Coastguard Worker
39*795d594fSAndroid Build Coastguard Worker# Check that we are using reasonably recent version of python
40*795d594fSAndroid Build Coastguard Workerprint("Using", sys.executable, sys.version, flush=True)
41*795d594fSAndroid Build Coastguard Workerversion = tuple(map(int, re.match(r"(\d*)\.(\d*)", sys.version).groups()))
42*795d594fSAndroid Build Coastguard Workerassert (version >= (3, 9)), "Python version is too old"
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Workerparser = argparse.ArgumentParser()
45*795d594fSAndroid Build Coastguard Workerparser.add_argument('-j', default='1', dest='n_threads')
46*795d594fSAndroid Build Coastguard Worker# either -l/--list OR build-target is required (but not both).
47*795d594fSAndroid Build Coastguard Workergroup = parser.add_mutually_exclusive_group(required=True)
48*795d594fSAndroid Build Coastguard Workergroup.add_argument('build_target', nargs='?')
49*795d594fSAndroid Build Coastguard Workergroup.add_argument('-l', '--list', action='store_true', help='List all possible run-build targets.')
50*795d594fSAndroid Build Coastguard Workeroptions = parser.parse_args()
51*795d594fSAndroid Build Coastguard Worker
52*795d594fSAndroid Build Coastguard Worker##########
53*795d594fSAndroid Build Coastguard Worker
54*795d594fSAndroid Build Coastguard Workerif options.list:
55*795d594fSAndroid Build Coastguard Worker  print("List of all known build_target: ")
56*795d594fSAndroid Build Coastguard Worker  for k in sorted(target_config.keys()):
57*795d594fSAndroid Build Coastguard Worker    print(" * " + k)
58*795d594fSAndroid Build Coastguard Worker  # TODO: would be nice if this was the same order as the target config file.
59*795d594fSAndroid Build Coastguard Worker  sys.exit(1)
60*795d594fSAndroid Build Coastguard Worker
61*795d594fSAndroid Build Coastguard Workerif not target_config.get(options.build_target):
62*795d594fSAndroid Build Coastguard Worker  sys.stderr.write("error: invalid build_target, see -l/--list.\n")
63*795d594fSAndroid Build Coastguard Worker  sys.exit(1)
64*795d594fSAndroid Build Coastguard Worker
65*795d594fSAndroid Build Coastguard Workertarget = target_config[options.build_target]
66*795d594fSAndroid Build Coastguard Workern_threads = options.n_threads
67*795d594fSAndroid Build Coastguard Workercustom_env = target.get('env', {})
68*795d594fSAndroid Build Coastguard Workercustom_env['SOONG_ALLOW_MISSING_DEPENDENCIES'] = 'true'
69*795d594fSAndroid Build Coastguard Worker# Switch the build system to unbundled mode in the reduced manifest branch.
70*795d594fSAndroid Build Coastguard Workerif not os.path.isdir(env.ANDROID_BUILD_TOP + '/frameworks/base'):
71*795d594fSAndroid Build Coastguard Worker  custom_env['TARGET_BUILD_UNBUNDLED'] = 'true'
72*795d594fSAndroid Build Coastguard Workerprint(custom_env)
73*795d594fSAndroid Build Coastguard Workeros.environ.update(custom_env)
74*795d594fSAndroid Build Coastguard Worker
75*795d594fSAndroid Build Coastguard Worker# always run installclean first remove any old installed files from previous builds.
76*795d594fSAndroid Build Coastguard Worker# this does not remove intermediate files, so it still avoids recompilation.
77*795d594fSAndroid Build Coastguard Workerclean_command = 'build/soong/soong_ui.bash --make-mode installclean'
78*795d594fSAndroid Build Coastguard Workerif env.DIST_DIR:
79*795d594fSAndroid Build Coastguard Worker  clean_command += ' dist'
80*795d594fSAndroid Build Coastguard Workersys.stdout.write(str(clean_command) + '\n')
81*795d594fSAndroid Build Coastguard Workersys.stdout.flush()
82*795d594fSAndroid Build Coastguard Workerif subprocess.call(clean_command.split()):
83*795d594fSAndroid Build Coastguard Worker  sys.exit(1)
84*795d594fSAndroid Build Coastguard Worker
85*795d594fSAndroid Build Coastguard Worker# build is just a binary/script that is directly executed to build any artifacts needed for the
86*795d594fSAndroid Build Coastguard Worker# test.
87*795d594fSAndroid Build Coastguard Workerif 'build' in target:
88*795d594fSAndroid Build Coastguard Worker  build_command = target.get('build').format(
89*795d594fSAndroid Build Coastguard Worker      ANDROID_BUILD_TOP = env.ANDROID_BUILD_TOP,
90*795d594fSAndroid Build Coastguard Worker      MAKE_OPTIONS='D8= -j{threads}'.format(threads = n_threads))
91*795d594fSAndroid Build Coastguard Worker  sys.stdout.write(str(build_command) + '\n')
92*795d594fSAndroid Build Coastguard Worker  sys.stdout.flush()
93*795d594fSAndroid Build Coastguard Worker  if subprocess.call(build_command.split()):
94*795d594fSAndroid Build Coastguard Worker    sys.exit(1)
95*795d594fSAndroid Build Coastguard Worker
96*795d594fSAndroid Build Coastguard Worker# make runs soong/kati to build the target listed in the entry.
97*795d594fSAndroid Build Coastguard Workerif 'make' in target:
98*795d594fSAndroid Build Coastguard Worker  build_command = 'build/soong/soong_ui.bash --make-mode'
99*795d594fSAndroid Build Coastguard Worker  build_command += ' D8='
100*795d594fSAndroid Build Coastguard Worker  build_command += ' -j' + str(n_threads)
101*795d594fSAndroid Build Coastguard Worker  build_command += ' ' + target.get('make')
102*795d594fSAndroid Build Coastguard Worker  if env.DIST_DIR:
103*795d594fSAndroid Build Coastguard Worker    build_command += ' dist'
104*795d594fSAndroid Build Coastguard Worker  sys.stdout.write(str(build_command) + '\n')
105*795d594fSAndroid Build Coastguard Worker  sys.stdout.flush()
106*795d594fSAndroid Build Coastguard Worker  if subprocess.call(build_command.split()):
107*795d594fSAndroid Build Coastguard Worker    sys.exit(1)
108*795d594fSAndroid Build Coastguard Worker
109*795d594fSAndroid Build Coastguard Workerif 'golem' in target:
110*795d594fSAndroid Build Coastguard Worker  machine_type = target.get('golem')
111*795d594fSAndroid Build Coastguard Worker  # use art-opt-cc by default since it mimics the default preopt config.
112*795d594fSAndroid Build Coastguard Worker  default_golem_config = 'art-opt-cc'
113*795d594fSAndroid Build Coastguard Worker
114*795d594fSAndroid Build Coastguard Worker  os.chdir(env.ANDROID_BUILD_TOP)
115*795d594fSAndroid Build Coastguard Worker  cmd =  ['art/tools/golem/build-target.sh']
116*795d594fSAndroid Build Coastguard Worker  cmd += ['-j' + str(n_threads)]
117*795d594fSAndroid Build Coastguard Worker  cmd += ['--showcommands']
118*795d594fSAndroid Build Coastguard Worker  cmd += ['--machine-type=%s' %(machine_type)]
119*795d594fSAndroid Build Coastguard Worker  cmd += ['--golem=%s' %(default_golem_config)]
120*795d594fSAndroid Build Coastguard Worker  cmd += ['--tarball']
121*795d594fSAndroid Build Coastguard Worker  sys.stdout.write(str(cmd) + '\n')
122*795d594fSAndroid Build Coastguard Worker  sys.stdout.flush()
123*795d594fSAndroid Build Coastguard Worker
124*795d594fSAndroid Build Coastguard Worker  if subprocess.call(cmd):
125*795d594fSAndroid Build Coastguard Worker    sys.exit(1)
126*795d594fSAndroid Build Coastguard Worker
127*795d594fSAndroid Build Coastguard Workerif 'run-test' in target:
128*795d594fSAndroid Build Coastguard Worker  run_test_command = [sys.executable, # Use the same python as we are using now.
129*795d594fSAndroid Build Coastguard Worker                      os.path.join(env.ANDROID_BUILD_TOP,
130*795d594fSAndroid Build Coastguard Worker                                   'art/test/testrunner/testrunner.py')]
131*795d594fSAndroid Build Coastguard Worker  test_flags = target.get('run-test', [])
132*795d594fSAndroid Build Coastguard Worker  out_dir = pathlib.PurePath(env.SOONG_OUT_DIR)
133*795d594fSAndroid Build Coastguard Worker  if not out_dir.is_absolute():
134*795d594fSAndroid Build Coastguard Worker    out_dir = pathlib.PurePath(env.ANDROID_BUILD_TOP).joinpath(out_dir)
135*795d594fSAndroid Build Coastguard Worker  run_test_command += list(map(lambda a: a.format(SOONG_OUT_DIR=str(out_dir)), test_flags))
136*795d594fSAndroid Build Coastguard Worker  # Let testrunner compute concurrency based on #cpus.
137*795d594fSAndroid Build Coastguard Worker  # b/65822340
138*795d594fSAndroid Build Coastguard Worker  # run_test_command += ['-j', str(n_threads)]
139*795d594fSAndroid Build Coastguard Worker
140*795d594fSAndroid Build Coastguard Worker  # In the config assume everything will run with --host and on ART.
141*795d594fSAndroid Build Coastguard Worker  # However for only [--jvm] this is undesirable, so don't pass in ART-specific flags.
142*795d594fSAndroid Build Coastguard Worker  if ['--jvm'] != test_flags:
143*795d594fSAndroid Build Coastguard Worker    run_test_command += ['--host']
144*795d594fSAndroid Build Coastguard Worker    run_test_command += ['--dex2oat-jobs']
145*795d594fSAndroid Build Coastguard Worker    run_test_command += ['4']
146*795d594fSAndroid Build Coastguard Worker  if '--no-build-dependencies' not in test_flags:
147*795d594fSAndroid Build Coastguard Worker    run_test_command += ['-b']
148*795d594fSAndroid Build Coastguard Worker    if env.DIST_DIR:
149*795d594fSAndroid Build Coastguard Worker      run_test_command += ['--dist']
150*795d594fSAndroid Build Coastguard Worker  run_test_command += ['--verbose']
151*795d594fSAndroid Build Coastguard Worker
152*795d594fSAndroid Build Coastguard Worker  sys.stdout.write(str(run_test_command) + '\n')
153*795d594fSAndroid Build Coastguard Worker  sys.stdout.flush()
154*795d594fSAndroid Build Coastguard Worker  if subprocess.call(run_test_command):
155*795d594fSAndroid Build Coastguard Worker    sys.exit(1)
156*795d594fSAndroid Build Coastguard Worker
157*795d594fSAndroid Build Coastguard Workersys.exit(0)
158