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 | |
| 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 { |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 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 { |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 38 | return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} { |
| 39 | if f := ctx.Config().DexpreoptGlobalConfig(); f != "" { |
| 40 | ctx.AddNinjaFileDeps(f) |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 41 | globalConfig, data, err := dexpreopt.LoadGlobalConfig(ctx, f) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 42 | if err != nil { |
| 43 | panic(err) |
| 44 | } |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 45 | return globalConfigAndRaw{globalConfig, data} |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // No global config filename set, see if there is a test config set |
| 49 | return ctx.Config().Once(dexpreoptTestGlobalConfigKey, func() interface{} { |
| 50 | // Nope, return a config with preopting disabled |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 51 | return globalConfigAndRaw{dexpreopt.GlobalConfig{ |
Mathieu Chartier | 6adeee1 | 2019-06-26 10:01:36 -0700 | [diff] [blame] | 52 | DisablePreopt: true, |
| 53 | DisableGenerateProfile: true, |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 54 | }, nil} |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 55 | }) |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 56 | }).(globalConfigAndRaw) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // setDexpreoptTestGlobalConfig sets a GlobalConfig that future calls to dexpreoptGlobalConfig will return. It must |
| 60 | // be called before the first call to dexpreoptGlobalConfig for the config. |
| 61 | func setDexpreoptTestGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) { |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 62 | config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} }) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig") |
| 66 | var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig") |
| 67 | |
| 68 | // systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed |
| 69 | // once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same |
| 70 | // ctx.Config(). |
| 71 | func systemServerClasspath(ctx android.PathContext) []string { |
| 72 | return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string { |
| 73 | global := dexpreoptGlobalConfig(ctx) |
| 74 | |
| 75 | var systemServerClasspathLocations []string |
| 76 | for _, m := range global.SystemServerJars { |
| 77 | systemServerClasspathLocations = append(systemServerClasspathLocations, |
| 78 | filepath.Join("/system/framework", m+".jar")) |
| 79 | } |
Roshan Pius | 9b51a40 | 2019-11-21 12:36:53 -0800 | [diff] [blame] | 80 | for _, m := range global.UpdatableSystemServerJars { |
Roshan Pius | 9b51a40 | 2019-11-21 12:36:53 -0800 | [diff] [blame] | 81 | systemServerClasspathLocations = append(systemServerClasspathLocations, |
Roshan Pius | ccc26ef | 2019-11-27 09:37:46 -0800 | [diff] [blame] | 82 | dexpreopt.GetJarLocationFromApexJarPair(m)) |
Roshan Pius | 9b51a40 | 2019-11-21 12:36:53 -0800 | [diff] [blame] | 83 | } |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 84 | return systemServerClasspathLocations |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath") |
| 89 | |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 90 | // dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures |
| 91 | // supported through native bridge. |
| 92 | func dexpreoptTargets(ctx android.PathContext) []android.Target { |
| 93 | var targets []android.Target |
Colin Cross | 3b19f5d | 2019-09-17 14:45:31 -0700 | [diff] [blame] | 94 | for _, target := range ctx.Config().Targets[android.Android] { |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 95 | if target.NativeBridge == android.NativeBridgeDisabled { |
| 96 | targets = append(targets, target) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return targets |
| 101 | } |
| 102 | |
Jiyong Park | 0b23875 | 2019-10-29 11:23:10 +0900 | [diff] [blame] | 103 | func stemOf(moduleName string) string { |
| 104 | // b/139391334: the stem of framework-minus-apex is framework |
| 105 | // This is hard coded here until we find a good way to query the stem |
| 106 | // of a module before any other mutators are run |
| 107 | if moduleName == "framework-minus-apex" { |
| 108 | return "framework" |
| 109 | } |
| 110 | return moduleName |
| 111 | } |
| 112 | |
Roshan Pius | ccc26ef | 2019-11-27 09:37:46 -0800 | [diff] [blame] | 113 | func getJarsFromApexJarPairs(apexJarPairs []string) []string { |
| 114 | modules := make([]string, len(apexJarPairs)) |
| 115 | for i, p := range apexJarPairs { |
| 116 | _, jar := dexpreopt.SplitApexJarPair(p) |
| 117 | modules[i] = jar |
| 118 | } |
| 119 | return modules |
| 120 | } |
| 121 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 122 | var ( |
| 123 | bootImageConfigKey = android.NewOnceKey("bootImageConfig") |
| 124 | artBootImageName = "art" |
| 125 | frameworkBootImageName = "boot" |
| 126 | apexBootImageName = "apex" |
| 127 | ) |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 128 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 129 | // Construct the global boot image configs. |
| 130 | func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig { |
| 131 | return ctx.Config().Once(bootImageConfigKey, func() interface{} { |
| 132 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 133 | global := dexpreoptGlobalConfig(ctx) |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 134 | targets := dexpreoptTargets(ctx) |
| 135 | deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName()) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 136 | |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame] | 137 | artModules := global.ArtApexJars |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 138 | // In coverage builds ART boot class path jars are instrumented and have additional dependencies. |
| 139 | if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") { |
| 140 | artModules = append(artModules, "jacocoagent") |
| 141 | } |
| 142 | frameworkModules := android.RemoveListFromList(global.BootJars, |
| 143 | concat(artModules, getJarsFromApexJarPairs(global.UpdatableBootJars))) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 144 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 145 | artSubdir := "apex/com.android.art/javalib" |
| 146 | frameworkSubdir := "system/framework" |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 147 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 148 | var artLocations, frameworkLocations []string |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame] | 149 | for _, m := range artModules { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 150 | artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar")) |
| 151 | } |
| 152 | for _, m := range frameworkModules { |
| 153 | frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar")) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 156 | // ART config for the primary boot image in the ART apex. |
| 157 | // It includes the Core Libraries. |
| 158 | artCfg := bootImageConfig{ |
| 159 | extension: false, |
| 160 | name: artBootImageName, |
| 161 | stem: "boot", |
| 162 | installSubdir: artSubdir, |
| 163 | modules: artModules, |
| 164 | dexLocations: artLocations, |
| 165 | dexLocationsDeps: artLocations, |
| 166 | } |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 167 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 168 | // Framework config for the boot image extension. |
| 169 | // It includes framework libraries and depends on the ART config. |
| 170 | frameworkCfg := bootImageConfig{ |
| 171 | extension: true, |
| 172 | name: frameworkBootImageName, |
| 173 | stem: "boot", |
| 174 | installSubdir: frameworkSubdir, |
| 175 | modules: frameworkModules, |
| 176 | dexLocations: frameworkLocations, |
| 177 | dexLocationsDeps: append(artLocations, frameworkLocations...), |
| 178 | } |
| 179 | |
| 180 | // Apex config for the boot image used in the JIT-zygote experiment. |
| 181 | // It includes both the Core libraries and framework. |
| 182 | apexCfg := bootImageConfig{ |
| 183 | extension: false, |
| 184 | name: apexBootImageName, |
| 185 | stem: "apex", |
| 186 | installSubdir: frameworkSubdir, |
| 187 | modules: concat(artModules, frameworkModules), |
| 188 | dexLocations: concat(artLocations, frameworkLocations), |
| 189 | dexLocationsDeps: concat(artLocations, frameworkLocations), |
| 190 | } |
| 191 | |
| 192 | configs := map[string]*bootImageConfig{ |
| 193 | artBootImageName: &artCfg, |
| 194 | frameworkBootImageName: &frameworkCfg, |
| 195 | apexBootImageName: &apexCfg, |
| 196 | } |
| 197 | |
| 198 | // common to all configs |
| 199 | for _, c := range configs { |
| 200 | c.targets = targets |
| 201 | |
| 202 | c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars") |
| 203 | c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped") |
| 204 | |
| 205 | // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension |
| 206 | imageName := c.firstModuleNameOrStem() + ".art" |
| 207 | |
| 208 | c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()} |
| 209 | |
| 210 | // The path to bootclasspath dex files needs to be known at module |
| 211 | // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled. |
| 212 | // Set up known paths for them, the singleton rules will copy them there. |
| 213 | // TODO(b/143682396): use module dependencies instead |
| 214 | inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input") |
| 215 | for _, m := range c.modules { |
| 216 | c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar")) |
| 217 | } |
| 218 | c.dexPathsDeps = c.dexPaths |
| 219 | |
| 220 | c.images = make(map[android.ArchType]android.OutputPath) |
| 221 | c.imagesDeps = make(map[android.ArchType]android.OutputPaths) |
| 222 | |
| 223 | for _, target := range targets { |
| 224 | arch := target.Arch.ArchType |
| 225 | imageDir := c.dir.Join(ctx, c.installSubdir, arch.String()) |
| 226 | c.images[arch] = imageDir.Join(ctx, imageName) |
| 227 | c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 228 | } |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 231 | // specific to the framework config |
| 232 | frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...) |
| 233 | frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...) |
| 234 | frameworkCfg.zip = frameworkCfg.dir.Join(ctx, frameworkCfg.stem+".zip") |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 235 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 236 | return configs |
| 237 | }).(map[string]*bootImageConfig) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 240 | func artBootImageConfig(ctx android.PathContext) bootImageConfig { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 241 | return *genBootImageConfigs(ctx)[artBootImageName] |
| 242 | } |
| 243 | |
| 244 | func defaultBootImageConfig(ctx android.PathContext) bootImageConfig { |
| 245 | return *genBootImageConfigs(ctx)[frameworkBootImageName] |
| 246 | } |
| 247 | |
| 248 | func apexBootImageConfig(ctx android.PathContext) bootImageConfig { |
| 249 | return *genBootImageConfigs(ctx)[apexBootImageName] |
Ulya Trafimovich | 1826338 | 2019-10-23 15:56:32 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 252 | func defaultBootclasspath(ctx android.PathContext) []string { |
| 253 | return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string { |
| 254 | global := dexpreoptGlobalConfig(ctx) |
| 255 | image := defaultBootImageConfig(ctx) |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 256 | |
Roshan Pius | ccc26ef | 2019-11-27 09:37:46 -0800 | [diff] [blame] | 257 | updatableBootclasspath := make([]string, len(global.UpdatableBootJars)) |
| 258 | for i, p := range global.UpdatableBootJars { |
| 259 | updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p) |
| 260 | } |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 261 | |
| 262 | bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 263 | return bootclasspath |
| 264 | }) |
| 265 | } |
| 266 | |
| 267 | var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath") |
| 268 | |
| 269 | var copyOf = android.CopyOf |
| 270 | |
| 271 | func init() { |
| 272 | android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars) |
| 273 | } |
| 274 | |
| 275 | func dexpreoptConfigMakevars(ctx android.MakeVarsContext) { |
| 276 | ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":")) |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 277 | ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 278 | ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":")) |
Colin Cross | 9be4152 | 2019-02-20 10:40:13 -0800 | [diff] [blame] | 279 | |
| 280 | ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 281 | } |