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