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{ |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 52 | DisablePreopt: true, |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame^] | 53 | }, nil} |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 54 | }) |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame^] | 55 | }).(globalConfigAndRaw) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // setDexpreoptTestGlobalConfig sets a GlobalConfig that future calls to dexpreoptGlobalConfig will return. It must |
| 59 | // be called before the first call to dexpreoptGlobalConfig for the config. |
| 60 | func setDexpreoptTestGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) { |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame^] | 61 | config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} }) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig") |
| 65 | var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig") |
| 66 | |
| 67 | // systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed |
| 68 | // once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same |
| 69 | // ctx.Config(). |
| 70 | func systemServerClasspath(ctx android.PathContext) []string { |
| 71 | return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string { |
| 72 | global := dexpreoptGlobalConfig(ctx) |
| 73 | |
| 74 | var systemServerClasspathLocations []string |
| 75 | for _, m := range global.SystemServerJars { |
| 76 | systemServerClasspathLocations = append(systemServerClasspathLocations, |
| 77 | filepath.Join("/system/framework", m+".jar")) |
| 78 | } |
| 79 | return systemServerClasspathLocations |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath") |
| 84 | |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 85 | // dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures |
| 86 | // supported through native bridge. |
| 87 | func dexpreoptTargets(ctx android.PathContext) []android.Target { |
| 88 | var targets []android.Target |
| 89 | for i, target := range ctx.Config().Targets[android.Android] { |
| 90 | if ctx.Config().SecondArchIsTranslated() && i > 0 { |
| 91 | break |
| 92 | } |
| 93 | |
| 94 | if target.NativeBridge == android.NativeBridgeDisabled { |
| 95 | targets = append(targets, target) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return targets |
| 100 | } |
| 101 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 102 | // defaultBootImageConfig returns the bootImageConfig that will be used to dexpreopt modules. It is computed once the |
| 103 | // first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same |
| 104 | // ctx.Config(). |
| 105 | func defaultBootImageConfig(ctx android.PathContext) bootImageConfig { |
| 106 | return ctx.Config().Once(defaultBootImageConfigKey, func() interface{} { |
| 107 | global := dexpreoptGlobalConfig(ctx) |
| 108 | |
| 109 | runtimeModules := global.RuntimeApexJars |
| 110 | nonFrameworkModules := concat(runtimeModules, global.ProductUpdatableBootModules) |
| 111 | frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules) |
| 112 | |
| 113 | var nonUpdatableBootModules []string |
| 114 | var nonUpdatableBootLocations []string |
| 115 | |
| 116 | for _, m := range runtimeModules { |
| 117 | nonUpdatableBootModules = append(nonUpdatableBootModules, m) |
| 118 | nonUpdatableBootLocations = append(nonUpdatableBootLocations, |
| 119 | filepath.Join("/apex/com.android.runtime/javalib", m+".jar")) |
| 120 | } |
| 121 | |
| 122 | for _, m := range frameworkModules { |
| 123 | nonUpdatableBootModules = append(nonUpdatableBootModules, m) |
| 124 | nonUpdatableBootLocations = append(nonUpdatableBootLocations, |
| 125 | filepath.Join("/system/framework", m+".jar")) |
| 126 | } |
| 127 | |
| 128 | // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before |
| 129 | // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy |
| 130 | // them there. |
| 131 | // TODO: use module dependencies instead |
| 132 | var nonUpdatableBootDexPaths android.WritablePaths |
| 133 | for _, m := range nonUpdatableBootModules { |
| 134 | nonUpdatableBootDexPaths = append(nonUpdatableBootDexPaths, |
| 135 | android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_input", m+".jar")) |
| 136 | } |
| 137 | |
| 138 | dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars") |
| 139 | symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_unstripped") |
| 140 | images := make(map[android.ArchType]android.OutputPath) |
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 | |
| 145 | for _, target := range targets { |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 146 | images[target.Arch.ArchType] = dir.Join(ctx, |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 147 | "system/framework", target.Arch.ArchType.String()).Join(ctx, "boot.art") |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return bootImageConfig{ |
| 151 | name: "boot", |
| 152 | modules: nonUpdatableBootModules, |
| 153 | dexLocations: nonUpdatableBootLocations, |
| 154 | dexPaths: nonUpdatableBootDexPaths, |
| 155 | dir: dir, |
| 156 | symbolsDir: symbolsDir, |
| 157 | images: images, |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 158 | targets: targets, |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 159 | zip: zip, |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 160 | } |
| 161 | }).(bootImageConfig) |
| 162 | } |
| 163 | |
| 164 | var defaultBootImageConfigKey = android.NewOnceKey("defaultBootImageConfig") |
| 165 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 166 | func apexBootImageConfig(ctx android.PathContext) bootImageConfig { |
| 167 | return ctx.Config().Once(apexBootImageConfigKey, func() interface{} { |
| 168 | global := dexpreoptGlobalConfig(ctx) |
| 169 | |
| 170 | runtimeModules := global.RuntimeApexJars |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 171 | nonFrameworkModules := concat(runtimeModules, global.ProductUpdatableBootModules) |
| 172 | frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules) |
| 173 | imageModules := concat(runtimeModules, frameworkModules) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 174 | |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 175 | var bootLocations []string |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 176 | |
| 177 | for _, m := range runtimeModules { |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 178 | bootLocations = append(bootLocations, |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 179 | filepath.Join("/apex/com.android.runtime/javalib", m+".jar")) |
| 180 | } |
| 181 | |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 182 | for _, m := range frameworkModules { |
| 183 | bootLocations = append(bootLocations, |
| 184 | filepath.Join("/system/framework", m+".jar")) |
| 185 | } |
| 186 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 187 | // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before |
| 188 | // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy |
| 189 | // them there. |
| 190 | // TODO: use module dependencies instead |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 191 | var bootDexPaths android.WritablePaths |
| 192 | for _, m := range imageModules { |
| 193 | bootDexPaths = append(bootDexPaths, |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 194 | android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_input", m+".jar")) |
| 195 | } |
| 196 | |
| 197 | dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars") |
| 198 | symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_unstripped") |
| 199 | images := make(map[android.ArchType]android.OutputPath) |
| 200 | |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 201 | targets := dexpreoptTargets(ctx) |
| 202 | |
| 203 | for _, target := range targets { |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 204 | images[target.Arch.ArchType] = dir.Join(ctx, |
| 205 | "system/framework", target.Arch.ArchType.String(), "apex.art") |
| 206 | } |
| 207 | |
| 208 | return bootImageConfig{ |
| 209 | name: "apex", |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 210 | modules: imageModules, |
| 211 | dexLocations: bootLocations, |
| 212 | dexPaths: bootDexPaths, |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 213 | dir: dir, |
| 214 | symbolsDir: symbolsDir, |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 215 | targets: targets, |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 216 | images: images, |
| 217 | } |
| 218 | }).(bootImageConfig) |
| 219 | } |
| 220 | |
| 221 | var apexBootImageConfigKey = android.NewOnceKey("apexBootImageConfig") |
| 222 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 223 | func defaultBootclasspath(ctx android.PathContext) []string { |
| 224 | return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string { |
| 225 | global := dexpreoptGlobalConfig(ctx) |
| 226 | image := defaultBootImageConfig(ctx) |
| 227 | bootclasspath := append(copyOf(image.dexLocations), global.ProductUpdatableBootLocations...) |
| 228 | return bootclasspath |
| 229 | }) |
| 230 | } |
| 231 | |
| 232 | var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath") |
| 233 | |
| 234 | var copyOf = android.CopyOf |
| 235 | |
| 236 | func init() { |
| 237 | android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars) |
| 238 | } |
| 239 | |
| 240 | func dexpreoptConfigMakevars(ctx android.MakeVarsContext) { |
| 241 | ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":")) |
Nicolas Geoffray | 07b4007 | 2019-02-22 16:57:42 +0000 | [diff] [blame] | 242 | ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocations, ":")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 243 | ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":")) |
Colin Cross | 9be4152 | 2019-02-20 10:40:13 -0800 | [diff] [blame] | 244 | |
| 245 | ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 246 | } |