Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 18 | "path/filepath" |
| 19 | "strings" |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/dexpreopt" |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
Hans Boehm | 453bf09 | 2020-01-25 01:44:30 +0000 | [diff] [blame] | 25 | // dexpreoptGlobalConfig returns the global dexpreopt.config. It is loaded once the first time it is called for any |
| 26 | // ctx.Config(), and returns the same data for all future calls with the same ctx.Config(). A value can be inserted |
| 27 | // for tests using setDexpreoptTestGlobalConfig. |
| 28 | func dexpreoptGlobalConfig(ctx android.PathContext) dexpreopt.GlobalConfig { |
| 29 | return dexpreoptGlobalConfigRaw(ctx).global |
| 30 | } |
| 31 | |
| 32 | type globalConfigAndRaw struct { |
| 33 | global dexpreopt.GlobalConfig |
| 34 | data []byte |
| 35 | } |
| 36 | |
| 37 | func dexpreoptGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw { |
| 38 | return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} { |
| 39 | if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil { |
| 40 | panic(err) |
| 41 | } else if data != nil { |
Hans Boehm | e4b5342 | 2020-01-25 01:44:30 +0000 | [diff] [blame] | 42 | soongConfig := dexpreopt.CreateGlobalSoongConfig(ctx) |
| 43 | globalConfig, err := dexpreopt.LoadGlobalConfig(ctx, data, soongConfig) |
Hans Boehm | 453bf09 | 2020-01-25 01:44:30 +0000 | [diff] [blame] | 44 | if err != nil { |
| 45 | panic(err) |
| 46 | } |
| 47 | return globalConfigAndRaw{globalConfig, data} |
| 48 | } |
| 49 | |
| 50 | // No global config filename set, see if there is a test config set |
| 51 | return ctx.Config().Once(dexpreoptTestGlobalConfigKey, func() interface{} { |
| 52 | // Nope, return a config with preopting disabled |
| 53 | return globalConfigAndRaw{dexpreopt.GlobalConfig{ |
| 54 | DisablePreopt: true, |
| 55 | DisableGenerateProfile: true, |
| 56 | }, nil} |
| 57 | }) |
| 58 | }).(globalConfigAndRaw) |
| 59 | } |
| 60 | |
| 61 | // setDexpreoptTestGlobalConfig sets a GlobalConfig that future calls to dexpreoptGlobalConfig will return. It must |
| 62 | // be called before the first call to dexpreoptGlobalConfig for the config. |
| 63 | func setDexpreoptTestGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) { |
| 64 | config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} }) |
| 65 | } |
| 66 | |
| 67 | var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig") |
| 68 | var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig") |
| 69 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 70 | // systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed |
| 71 | // once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same |
| 72 | // ctx.Config(). |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame^] | 73 | func systemServerClasspath(ctx android.MakeVarsContext) []string { |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 74 | return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string { |
Hans Boehm | 453bf09 | 2020-01-25 01:44:30 +0000 | [diff] [blame] | 75 | global := dexpreoptGlobalConfig(ctx) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 76 | var systemServerClasspathLocations []string |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame^] | 77 | for _, m := range *DexpreoptedSystemServerJars(ctx.Config()) { |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 78 | systemServerClasspathLocations = append(systemServerClasspathLocations, |
| 79 | filepath.Join("/system/framework", m+".jar")) |
| 80 | } |
Roshan Pius | 9b51a40 | 2019-11-21 12:36:53 -0800 | [diff] [blame] | 81 | for _, m := range global.UpdatableSystemServerJars { |
Roshan Pius | 9b51a40 | 2019-11-21 12:36:53 -0800 | [diff] [blame] | 82 | systemServerClasspathLocations = append(systemServerClasspathLocations, |
Roshan Pius | ccc26ef | 2019-11-27 09:37:46 -0800 | [diff] [blame] | 83 | dexpreopt.GetJarLocationFromApexJarPair(m)) |
Roshan Pius | 9b51a40 | 2019-11-21 12:36:53 -0800 | [diff] [blame] | 84 | } |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 85 | return systemServerClasspathLocations |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath") |
| 90 | |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 91 | // dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures |
| 92 | // supported through native bridge. |
| 93 | func dexpreoptTargets(ctx android.PathContext) []android.Target { |
| 94 | var targets []android.Target |
Colin Cross | 3b19f5d | 2019-09-17 14:45:31 -0700 | [diff] [blame] | 95 | for _, target := range ctx.Config().Targets[android.Android] { |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 96 | if target.NativeBridge == android.NativeBridgeDisabled { |
| 97 | targets = append(targets, target) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return targets |
| 102 | } |
| 103 | |
Jiyong Park | 0b23875 | 2019-10-29 11:23:10 +0900 | [diff] [blame] | 104 | func stemOf(moduleName string) string { |
| 105 | // b/139391334: the stem of framework-minus-apex is framework |
| 106 | // This is hard coded here until we find a good way to query the stem |
| 107 | // of a module before any other mutators are run |
| 108 | if moduleName == "framework-minus-apex" { |
| 109 | return "framework" |
| 110 | } |
| 111 | return moduleName |
| 112 | } |
| 113 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 114 | var ( |
Ulya Trafimovich | 4cdada2 | 2020-02-10 15:29:28 +0000 | [diff] [blame] | 115 | bootImageConfigKey = android.NewOnceKey("bootImageConfig") |
| 116 | artBootImageName = "art" |
| 117 | frameworkBootImageName = "boot" |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 118 | ) |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 119 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 120 | // Construct the global boot image configs. |
| 121 | func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig { |
| 122 | return ctx.Config().Once(bootImageConfigKey, func() interface{} { |
| 123 | |
Hans Boehm | 453bf09 | 2020-01-25 01:44:30 +0000 | [diff] [blame] | 124 | global := dexpreoptGlobalConfig(ctx) |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 125 | targets := dexpreoptTargets(ctx) |
| 126 | deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName()) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 127 | |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame] | 128 | artModules := global.ArtApexJars |
Ulya Trafimovich | 4456188 | 2020-01-03 13:25:54 +0000 | [diff] [blame] | 129 | // With EMMA_INSTRUMENT_FRAMEWORK=true the Core libraries depend on jacoco. |
| 130 | if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") { |
| 131 | artModules = append(artModules, "jacocoagent") |
| 132 | } |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 133 | frameworkModules := android.RemoveListFromList(global.BootJars, |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame^] | 134 | concat(artModules, dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars))) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 135 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 136 | artSubdir := "apex/com.android.art/javalib" |
| 137 | frameworkSubdir := "system/framework" |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 138 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 139 | var artLocations, frameworkLocations []string |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame] | 140 | for _, m := range artModules { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 141 | artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar")) |
| 142 | } |
| 143 | for _, m := range frameworkModules { |
| 144 | frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar")) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 147 | // ART config for the primary boot image in the ART apex. |
| 148 | // It includes the Core Libraries. |
| 149 | artCfg := bootImageConfig{ |
| 150 | extension: false, |
| 151 | name: artBootImageName, |
| 152 | stem: "boot", |
| 153 | installSubdir: artSubdir, |
| 154 | modules: artModules, |
| 155 | dexLocations: artLocations, |
| 156 | dexLocationsDeps: artLocations, |
| 157 | } |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 158 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 159 | // Framework config for the boot image extension. |
Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 160 | // It includes framework libraries and depends on the ART config. |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 161 | frameworkCfg := bootImageConfig{ |
Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 162 | extension: true, |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 163 | name: frameworkBootImageName, |
| 164 | stem: "boot", |
| 165 | installSubdir: frameworkSubdir, |
Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 166 | modules: frameworkModules, |
| 167 | dexLocations: frameworkLocations, |
| 168 | dexLocationsDeps: append(artLocations, frameworkLocations...), |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 171 | configs := map[string]*bootImageConfig{ |
Ulya Trafimovich | 4cdada2 | 2020-02-10 15:29:28 +0000 | [diff] [blame] | 172 | artBootImageName: &artCfg, |
| 173 | frameworkBootImageName: &frameworkCfg, |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // common to all configs |
| 177 | for _, c := range configs { |
| 178 | c.targets = targets |
| 179 | |
| 180 | c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars") |
| 181 | c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped") |
| 182 | |
| 183 | // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension |
| 184 | imageName := c.firstModuleNameOrStem() + ".art" |
| 185 | |
| 186 | c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()} |
| 187 | |
| 188 | // The path to bootclasspath dex files needs to be known at module |
| 189 | // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled. |
| 190 | // Set up known paths for them, the singleton rules will copy them there. |
| 191 | // TODO(b/143682396): use module dependencies instead |
| 192 | inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input") |
| 193 | for _, m := range c.modules { |
| 194 | c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar")) |
| 195 | } |
| 196 | c.dexPathsDeps = c.dexPaths |
| 197 | |
| 198 | c.images = make(map[android.ArchType]android.OutputPath) |
| 199 | c.imagesDeps = make(map[android.ArchType]android.OutputPaths) |
| 200 | |
| 201 | for _, target := range targets { |
| 202 | arch := target.Arch.ArchType |
| 203 | imageDir := c.dir.Join(ctx, c.installSubdir, arch.String()) |
| 204 | c.images[arch] = imageDir.Join(ctx, imageName) |
| 205 | c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 206 | } |
Colin Cross | 31bf00d | 2019-12-04 13:16:01 -0800 | [diff] [blame] | 207 | |
| 208 | c.zip = c.dir.Join(ctx, c.name+".zip") |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 211 | // specific to the framework config |
| 212 | frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...) |
Ulya Trafimovich | b0a2d37 | 2020-01-28 14:42:41 +0000 | [diff] [blame] | 213 | frameworkCfg.primaryImages = artCfg.images |
Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 214 | frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...) |
| 215 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 216 | return configs |
| 217 | }).(map[string]*bootImageConfig) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 220 | func artBootImageConfig(ctx android.PathContext) bootImageConfig { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 221 | return *genBootImageConfigs(ctx)[artBootImageName] |
| 222 | } |
| 223 | |
Lingfeng Yang | 54191fa | 2019-12-19 16:40:09 +0000 | [diff] [blame] | 224 | func defaultBootImageConfig(ctx android.PathContext) bootImageConfig { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 225 | return *genBootImageConfigs(ctx)[frameworkBootImageName] |
| 226 | } |
| 227 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 228 | func defaultBootclasspath(ctx android.PathContext) []string { |
| 229 | return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string { |
Hans Boehm | 453bf09 | 2020-01-25 01:44:30 +0000 | [diff] [blame] | 230 | global := dexpreoptGlobalConfig(ctx) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 231 | image := defaultBootImageConfig(ctx) |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 232 | |
Roshan Pius | ccc26ef | 2019-11-27 09:37:46 -0800 | [diff] [blame] | 233 | updatableBootclasspath := make([]string, len(global.UpdatableBootJars)) |
| 234 | for i, p := range global.UpdatableBootJars { |
| 235 | updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p) |
| 236 | } |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 237 | |
| 238 | bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 239 | return bootclasspath |
| 240 | }) |
| 241 | } |
| 242 | |
| 243 | var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath") |
| 244 | |
| 245 | var copyOf = android.CopyOf |
| 246 | |
| 247 | func init() { |
| 248 | android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars) |
| 249 | } |
| 250 | |
| 251 | func dexpreoptConfigMakevars(ctx android.MakeVarsContext) { |
| 252 | ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":")) |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 253 | ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 254 | ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":")) |
Colin Cross | 9be4152 | 2019-02-20 10:40:13 -0800 | [diff] [blame] | 255 | |
| 256 | ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 257 | } |