1#!/usr/bin/env python3 2# 3# Copyright (C) 2021 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""" This script generates the Android.run-test.bp build file""" 18 19import glob 20import json 21import os 22import textwrap 23import sys 24 25def main(): 26 os.chdir(os.path.dirname(__file__)) 27 with open("Android.run-test.bp", mode="wt") as f: 28 f.write(textwrap.dedent(f""" 29 // This file was generated by {os.path.basename(__file__)} 30 // It is not necessary to regenerate it when tests are added/removed/modified. 31 32 TEST_BUILD_COMMON_ARGS = "$(location run_test_build.py) --out $(out) " + 33 "--bootclasspath $(location :art-run-test-bootclasspath) " + 34 "--d8 $(location d8) " + 35 "--jasmin $(location jasmin) " + 36 "--rewrapper $(location rewrapper) " + 37 "--smali $(location android-smali) " + 38 "--soong_zip $(location soong_zip) " + 39 "--zipalign $(location zipalign) " 40 """).lstrip()) 41 for mode in ["host", "target", "jvm"]: 42 names = [] 43 # Group the tests into shards based on the last two digits of the test number. 44 # This keeps the number of generated genrules low so we don't overwhelm soong, 45 # but it still allows iterating on single test without recompiling all tests. 46 for shard in ["{:02}".format(i) for i in range(100)]: 47 name = f"art-run-test-{mode}-data-shard{shard}" 48 names.append(name) 49 f.write(textwrap.dedent(f""" 50 java_genrule {{ 51 name: "{name}-tmp", 52 out: ["{name}.zip"], 53 srcs: [ 54 "?{shard}-*/**/*", 55 "??{shard}-*/**/*", 56 ], 57 cmd: TEST_BUILD_COMMON_ARGS + "--mode {mode} --test-dir-regex 'art/test/..?{shard}-' $(in)", 58 defaults: ["art-run-test-{mode}-data-defaults"], 59 }} 60 61 // This filegroup is so that the host prebuilt etc can depend on a device genrule, 62 // as prebuilt_etc doesn't have the equivalent of device_common_srcs. 63 filegroup {{ 64 name: "{name}-fg", 65 device_common_srcs: [":{name}-tmp"], 66 }} 67 68 // Install in the output directory to make it accessible for tests. 69 prebuilt_etc_host {{ 70 name: "{name}", 71 src: ":{name}-fg", 72 sub_dir: "art", 73 filename: "{name}.zip", 74 }} 75 """)) 76 77 # Build all hiddenapi tests in their own shard. 78 # This removes the dependency on hiddenapi from all other shards, 79 # which in turn removes dependency on ART C++ source code. 80 name = "art-run-test-{mode}-data-shardHiddenApi".format(mode=mode) 81 names.append(name) 82 f.write(textwrap.dedent(f""" 83 java_genrule {{ 84 name: "{name}-tmp", 85 out: ["{name}.zip"], 86 srcs: [ 87 "???-*hiddenapi*/**/*", 88 "????-*hiddenapi*/**/*", 89 ], 90 defaults: ["art-run-test-{mode}-data-defaults"], 91 tools: ["hiddenapi"], 92 cmd: TEST_BUILD_COMMON_ARGS + "--hiddenapi $(location hiddenapi) --mode {mode} --test-dir-regex 'art/test/....?-[^/]*hiddenapi' $(in)", 93 }} 94 95 // This filegroup is so that the host prebuilt etc can depend on a device genrule, 96 // as prebuilt_etc doesn't have the equivalent of device_common_srcs. 97 filegroup {{ 98 name: "{name}-fg", 99 device_common_srcs: [":{name}-tmp"], 100 }} 101 102 // Install in the output directory to make it accessible for tests. 103 prebuilt_etc_host {{ 104 name: "{name}", 105 src: ":{name}-fg", 106 sub_dir: "art", 107 filename: "{name}.zip", 108 }} 109 """)) 110 111 f.write(textwrap.dedent(f""" 112 genrule_defaults {{ 113 name: "art-run-test-{mode}-data-defaults", 114 srcs: [ 115 // Since genrules are sandboxed, all the sources they use must be listed in 116 // the Android.bp file. Some tests have symlinks to files from other tests, and 117 // those must also be listed to avoid a dangling symlink in the sandbox. 118 "jvmti-common/*.java", 119 "utils/python/**/*.py", 120 ":art-run-test-bootclasspath", 121 ":development_docs", 122 ":asm-9.6-filegroup", 123 ":ojluni-AbstractCollection", 124 "988-method-trace/expected-stdout.txt", 125 "988-method-trace/expected-stderr.txt", 126 "988-method-trace/src/art/Test988Intrinsics.java", 127 "988-method-trace/src/art/Test988.java", 128 "988-method-trace/trace_fib.cc", 129 "1953-pop-frame/src/art/Test1953.java", 130 "1953-pop-frame/src/art/SuspendEvents.java", 131 // Files needed to generate runner scripts. 132 "testrunner/*.py", 133 "knownfailures.json", 134 "default_run.py", 135 "globals.py", 136 "run-test", 137 "run_test_build.py", 138 ], 139 tools: [ 140 "android-smali", 141 "d8", 142 "jasmin", 143 "rewrapper", 144 "soong_zip", 145 "zipalign", 146 ], 147 }} 148 """)) 149 150 name = "art-run-test-{mode}-data-merged".format(mode=mode) 151 srcs = ("\n"+" "*16).join('":{}-tmp",'.format(n) for n in names) 152 deps = ("\n"+" "*16).join('"{}",'.format(n) for n in names) 153 f.write(textwrap.dedent(f""" 154 java_genrule {{ 155 name: "{name}-tmp", 156 out: ["{name}.tgz"], 157 srcs: [ 158 {srcs} 159 ], 160 tool_files: ["merge_zips_to_tgz.py"], 161 cmd: "$(location merge_zips_to_tgz.py) $(out) $(in)", 162 }} 163 164 // This filegroup is so that the host prebuilt etc can depend on a device genrule, 165 // as prebuilt_etc doesn't have the equivalent of device_common_srcs. 166 filegroup {{ 167 name: "{name}-fg", 168 device_common_srcs: [":{name}-tmp"], 169 }} 170 171 // Install in the output directory to make it accessible for tests. 172 prebuilt_etc_host {{ 173 name: "{name}", 174 src: ":{name}-fg", 175 required: [ 176 {deps} 177 ], 178 sub_dir: "art", 179 filename: "{name}.tgz", 180 }} 181 """)) 182 183 name = "art-run-test-{mode}-data".format(mode=mode) 184 srcs = ("\n"+" "*16).join('":{}-tmp",'.format(n) for n in names) 185 deps = ("\n"+" "*16).join('"{}",'.format(n) for n in names) 186 f.write(textwrap.dedent(f""" 187 // Phony target used to build all shards 188 java_genrule {{ 189 name: "{name}-tmp", 190 out: ["{name}.txt"], 191 srcs: [ 192 {srcs} 193 ], 194 cmd: "echo $(in) > $(out)", 195 }} 196 197 // This filegroup is so that the host prebuilt etc can depend on a device genrule, 198 // as prebuilt_etc doesn't have the equivalent of device_common_srcs. 199 filegroup {{ 200 name: "{name}-fg", 201 device_common_srcs: [":{name}-tmp"], 202 }} 203 204 // Phony target used to install all shards 205 prebuilt_etc_host {{ 206 name: "{name}", 207 src: ":{name}-fg", 208 required: [ 209 {deps} 210 ], 211 sub_dir: "art", 212 filename: "{name}.txt", 213 }} 214 """)) 215 216if __name__ == "__main__": 217 main() 218