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 | } |
| 80 | return systemServerClasspathLocations |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath") |
| 85 | |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 86 | // dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures |
| 87 | // supported through native bridge. |
| 88 | func dexpreoptTargets(ctx android.PathContext) []android.Target { |
| 89 | var targets []android.Target |
| 90 | for i, target := range ctx.Config().Targets[android.Android] { |
| 91 | if ctx.Config().SecondArchIsTranslated() && i > 0 { |
| 92 | break |
| 93 | } |
| 94 | |
| 95 | if target.NativeBridge == android.NativeBridgeDisabled { |
| 96 | targets = append(targets, target) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return targets |
| 101 | } |
| 102 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 103 | // defaultBootImageConfig returns the bootImageConfig that will be used to dexpreopt modules. It is computed once the |
| 104 | // first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same |
| 105 | // ctx.Config(). |
| 106 | func defaultBootImageConfig(ctx android.PathContext) bootImageConfig { |
| 107 | return ctx.Config().Once(defaultBootImageConfigKey, func() interface{} { |
| 108 | global := dexpreoptGlobalConfig(ctx) |
| 109 | |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame^] | 110 | artModules := global.ArtApexJars |
| 111 | nonFrameworkModules := concat(artModules, global.ProductUpdatableBootModules) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 112 | frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules) |
| 113 | |
| 114 | var nonUpdatableBootModules []string |
| 115 | var nonUpdatableBootLocations []string |
| 116 | |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame^] | 117 | for _, m := range artModules { |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 118 | nonUpdatableBootModules = append(nonUpdatableBootModules, m) |
| 119 | nonUpdatableBootLocations = append(nonUpdatableBootLocations, |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame^] | 120 | filepath.Join("/apex/com.android.art/javalib", m+".jar")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | for _, m := range frameworkModules { |
| 124 | nonUpdatableBootModules = append(nonUpdatableBootModules, m) |
| 125 | nonUpdatableBootLocations = append(nonUpdatableBootLocations, |
| 126 | filepath.Join("/system/framework", m+".jar")) |
| 127 | } |
| 128 | |
| 129 | // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before |
| 130 | // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy |
| 131 | // them there. |
| 132 | // TODO: use module dependencies instead |
| 133 | var nonUpdatableBootDexPaths android.WritablePaths |
| 134 | for _, m := range nonUpdatableBootModules { |
| 135 | nonUpdatableBootDexPaths = append(nonUpdatableBootDexPaths, |
| 136 | android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_input", m+".jar")) |
| 137 | } |
| 138 | |
| 139 | dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars") |
| 140 | symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_unstripped") |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 141 | zip := dir.Join(ctx, "boot.zip") |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 142 | |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 143 | targets := dexpreoptTargets(ctx) |
| 144 | |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 145 | imageConfig := bootImageConfig{ |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 146 | name: "boot", |
| 147 | modules: nonUpdatableBootModules, |
| 148 | dexLocations: nonUpdatableBootLocations, |
| 149 | dexPaths: nonUpdatableBootDexPaths, |
| 150 | dir: dir, |
| 151 | symbolsDir: symbolsDir, |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 152 | images: make(map[android.ArchType]android.OutputPath), |
| 153 | imagesDeps: make(map[android.ArchType]android.Paths), |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 154 | targets: targets, |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 155 | zip: zip, |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 156 | } |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 157 | |
| 158 | for _, target := range targets { |
| 159 | imageDir := dir.Join(ctx, "system/framework", target.Arch.ArchType.String()) |
| 160 | imageConfig.images[target.Arch.ArchType] = imageDir.Join(ctx, "boot.art") |
| 161 | |
| 162 | imagesDeps := make([]android.Path, 0, len(imageConfig.modules)*3) |
| 163 | for _, dep := range imageConfig.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") { |
| 164 | imagesDeps = append(imagesDeps, dep) |
| 165 | } |
| 166 | imageConfig.imagesDeps[target.Arch.ArchType] = imagesDeps |
| 167 | } |
| 168 | |
| 169 | return imageConfig |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 170 | }).(bootImageConfig) |
| 171 | } |
| 172 | |
| 173 | var defaultBootImageConfigKey = android.NewOnceKey("defaultBootImageConfig") |
| 174 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 175 | func apexBootImageConfig(ctx android.PathContext) bootImageConfig { |
| 176 | return ctx.Config().Once(apexBootImageConfigKey, func() interface{} { |
| 177 | global := dexpreoptGlobalConfig(ctx) |
| 178 | |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame^] | 179 | artModules := global.ArtApexJars |
| 180 | nonFrameworkModules := concat(artModules, global.ProductUpdatableBootModules) |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 181 | frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules) |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame^] | 182 | imageModules := concat(artModules, frameworkModules) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 183 | |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 184 | var bootLocations []string |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 185 | |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame^] | 186 | for _, m := range artModules { |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 187 | bootLocations = append(bootLocations, |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame^] | 188 | filepath.Join("/apex/com.android.art/javalib", m+".jar")) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 191 | for _, m := range frameworkModules { |
| 192 | bootLocations = append(bootLocations, |
| 193 | filepath.Join("/system/framework", m+".jar")) |
| 194 | } |
| 195 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 196 | // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before |
| 197 | // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy |
| 198 | // them there. |
| 199 | // TODO: use module dependencies instead |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 200 | var bootDexPaths android.WritablePaths |
| 201 | for _, m := range imageModules { |
| 202 | bootDexPaths = append(bootDexPaths, |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 203 | android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_input", m+".jar")) |
| 204 | } |
| 205 | |
| 206 | dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars") |
| 207 | symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_unstripped") |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 208 | |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 209 | targets := dexpreoptTargets(ctx) |
| 210 | |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 211 | imageConfig := bootImageConfig{ |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 212 | name: "apex", |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 213 | modules: imageModules, |
| 214 | dexLocations: bootLocations, |
| 215 | dexPaths: bootDexPaths, |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 216 | dir: dir, |
| 217 | symbolsDir: symbolsDir, |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 218 | targets: targets, |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 219 | images: make(map[android.ArchType]android.OutputPath), |
| 220 | imagesDeps: make(map[android.ArchType]android.Paths), |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 221 | } |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 222 | |
| 223 | for _, target := range targets { |
| 224 | imageDir := dir.Join(ctx, "system/framework", target.Arch.ArchType.String()) |
| 225 | imageConfig.images[target.Arch.ArchType] = imageDir.Join(ctx, "apex.art") |
| 226 | |
| 227 | imagesDeps := make([]android.Path, 0, len(imageConfig.modules)*3) |
| 228 | for _, dep := range imageConfig.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") { |
| 229 | imagesDeps = append(imagesDeps, dep) |
| 230 | } |
| 231 | imageConfig.imagesDeps[target.Arch.ArchType] = imagesDeps |
| 232 | } |
| 233 | |
| 234 | return imageConfig |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 235 | }).(bootImageConfig) |
| 236 | } |
| 237 | |
| 238 | var apexBootImageConfigKey = android.NewOnceKey("apexBootImageConfig") |
| 239 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 240 | func defaultBootclasspath(ctx android.PathContext) []string { |
| 241 | return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string { |
| 242 | global := dexpreoptGlobalConfig(ctx) |
| 243 | image := defaultBootImageConfig(ctx) |
| 244 | bootclasspath := append(copyOf(image.dexLocations), global.ProductUpdatableBootLocations...) |
| 245 | return bootclasspath |
| 246 | }) |
| 247 | } |
| 248 | |
| 249 | var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath") |
| 250 | |
| 251 | var copyOf = android.CopyOf |
| 252 | |
| 253 | func init() { |
| 254 | android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars) |
| 255 | } |
| 256 | |
| 257 | func dexpreoptConfigMakevars(ctx android.MakeVarsContext) { |
| 258 | ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":")) |
Nicolas Geoffray | 07b4007 | 2019-02-22 16:57:42 +0000 | [diff] [blame] | 259 | ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocations, ":")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 260 | ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":")) |
Colin Cross | 9be4152 | 2019-02-20 10:40:13 -0800 | [diff] [blame] | 261 | |
| 262 | ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 263 | } |