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