| 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 |  | 
| Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 25 | // dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures | 
|  | 26 | // supported through native bridge. | 
|  | 27 | func dexpreoptTargets(ctx android.PathContext) []android.Target { | 
|  | 28 | var targets []android.Target | 
| Colin Cross | 3b19f5d | 2019-09-17 14:45:31 -0700 | [diff] [blame] | 29 | for _, target := range ctx.Config().Targets[android.Android] { | 
| Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 30 | if target.NativeBridge == android.NativeBridgeDisabled { | 
|  | 31 | targets = append(targets, target) | 
|  | 32 | } | 
|  | 33 | } | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 34 | // We may also need the images on host in order to run host-based tests. | 
|  | 35 | for _, target := range ctx.Config().Targets[android.BuildOs] { | 
|  | 36 | targets = append(targets, target) | 
|  | 37 | } | 
| Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 38 |  | 
|  | 39 | return targets | 
|  | 40 | } | 
|  | 41 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 42 | var ( | 
| Ulya Trafimovich | 4cdada2 | 2020-02-10 15:29:28 +0000 | [diff] [blame] | 43 | bootImageConfigKey     = android.NewOnceKey("bootImageConfig") | 
|  | 44 | artBootImageName       = "art" | 
|  | 45 | frameworkBootImageName = "boot" | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 46 | ) | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 47 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 48 | // Construct the global boot image configs. | 
|  | 49 | func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig { | 
|  | 50 | return ctx.Config().Once(bootImageConfigKey, func() interface{} { | 
|  | 51 |  | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 52 | global := dexpreopt.GetGlobalConfig(ctx) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 53 | targets := dexpreoptTargets(ctx) | 
|  | 54 | deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName()) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 55 |  | 
| Paul Duffin | 7d584e9 | 2020-10-23 18:26:03 +0100 | [diff] [blame] | 56 | artModules := global.ArtApexJars | 
| Paul Duffin | 7d584e9 | 2020-10-23 18:26:03 +0100 | [diff] [blame] | 57 | frameworkModules := global.BootJars.RemoveList(artModules) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 58 |  | 
| Jeongik Cha | a596909 | 2021-05-07 18:53:21 +0900 | [diff] [blame] | 59 | artDirOnHost := "apex/art_boot_images/javalib" | 
| Jeongik Cha | 036f8f5 | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 60 | artDirOnDevice := "apex/com.android.art/javalib" | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 61 | frameworkSubdir := "system/framework" | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 62 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 63 | // ART config for the primary boot image in the ART apex. | 
|  | 64 | // It includes the Core Libraries. | 
|  | 65 | artCfg := bootImageConfig{ | 
| Jeongik Cha | 036f8f5 | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 66 | name:               artBootImageName, | 
|  | 67 | stem:               "boot", | 
|  | 68 | installDirOnHost:   artDirOnHost, | 
|  | 69 | installDirOnDevice: artDirOnDevice, | 
|  | 70 | modules:            artModules, | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 71 | } | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 72 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 73 | // Framework config for the boot image extension. | 
| Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 74 | // It includes framework libraries and depends on the ART config. | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 75 | frameworkCfg := bootImageConfig{ | 
| Jeongik Cha | 036f8f5 | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 76 | extends:            &artCfg, | 
|  | 77 | name:               frameworkBootImageName, | 
|  | 78 | stem:               "boot", | 
|  | 79 | installDirOnHost:   frameworkSubdir, | 
|  | 80 | installDirOnDevice: frameworkSubdir, | 
|  | 81 | modules:            frameworkModules, | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 84 | configs := map[string]*bootImageConfig{ | 
| Ulya Trafimovich | 4cdada2 | 2020-02-10 15:29:28 +0000 | [diff] [blame] | 85 | artBootImageName:       &artCfg, | 
|  | 86 | frameworkBootImageName: &frameworkCfg, | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
|  | 89 | // common to all configs | 
|  | 90 | for _, c := range configs { | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 91 | c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars") | 
|  | 92 | c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped") | 
|  | 93 |  | 
|  | 94 | // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension | 
| Ulya Trafimovich | 8640ab9 | 2020-05-11 18:06:15 +0100 | [diff] [blame] | 95 | imageName := c.firstModuleNameOrStem(ctx) + ".art" | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 96 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 97 | // The path to bootclasspath dex files needs to be known at module | 
|  | 98 | // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled. | 
|  | 99 | // Set up known paths for them, the singleton rules will copy them there. | 
|  | 100 | // TODO(b/143682396): use module dependencies instead | 
|  | 101 | inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input") | 
| Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 102 | c.dexPaths = c.modules.BuildPaths(ctx, inputDir) | 
| Paul Duffin | 3e2db5c | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 103 | c.dexPathsByModule = c.modules.BuildPathsByModule(ctx, inputDir) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 104 | c.dexPathsDeps = c.dexPaths | 
|  | 105 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 106 | // Create target-specific variants. | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 107 | for _, target := range targets { | 
|  | 108 | arch := target.Arch.ArchType | 
| Jeongik Cha | a596909 | 2021-05-07 18:53:21 +0900 | [diff] [blame] | 109 | imageDir := c.dir.Join(ctx, target.Os.String(), c.installDirOnHost, arch.String()) | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 110 | variant := &bootImageVariant{ | 
| Jeongik Cha | 036f8f5 | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 111 | bootImageConfig:   c, | 
|  | 112 | target:            target, | 
|  | 113 | imagePathOnHost:   imageDir.Join(ctx, imageName), | 
|  | 114 | imagePathOnDevice: filepath.Join("/", c.installDirOnDevice, arch.String(), imageName), | 
|  | 115 | imagesDeps:        c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"), | 
|  | 116 | dexLocations:      c.modules.DevicePaths(ctx.Config(), target.Os), | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 117 | } | 
|  | 118 | variant.dexLocationsDeps = variant.dexLocations | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 119 | c.variants = append(c.variants, variant) | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 120 | } | 
| Colin Cross | 31bf00d | 2019-12-04 13:16:01 -0800 | [diff] [blame] | 121 |  | 
|  | 122 | c.zip = c.dir.Join(ctx, c.name+".zip") | 
| Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
| Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 125 | // specific to the framework config | 
|  | 126 | frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...) | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 127 | for i := range targets { | 
| Jeongik Cha | a596909 | 2021-05-07 18:53:21 +0900 | [diff] [blame] | 128 | frameworkCfg.variants[i].primaryImages = artCfg.variants[i].imagePathOnHost | 
| Paul Duffin | 8c666a3 | 2021-06-04 17:25:28 +0100 | [diff] [blame] | 129 | frameworkCfg.variants[i].primaryImagesDeps = artCfg.variants[i].imagesDeps.Paths() | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 130 | frameworkCfg.variants[i].dexLocationsDeps = append(artCfg.variants[i].dexLocations, frameworkCfg.variants[i].dexLocationsDeps...) | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 131 | } | 
| Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 132 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 133 | return configs | 
|  | 134 | }).(map[string]*bootImageConfig) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 137 | func artBootImageConfig(ctx android.PathContext) *bootImageConfig { | 
|  | 138 | return genBootImageConfigs(ctx)[artBootImageName] | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 141 | func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig { | 
|  | 142 | return genBootImageConfigs(ctx)[frameworkBootImageName] | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 145 | // Apex boot config allows to access build/install paths of apex boot jars without going | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 146 | // through the usual trouble of registering dependencies on those modules and extracting build paths | 
|  | 147 | // from those dependencies. | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 148 | type apexBootConfig struct { | 
|  | 149 | // A list of apex boot jars. | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 150 | modules android.ConfiguredJarList | 
|  | 151 |  | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 152 | // A list of predefined build paths to apex boot jars. They are configured very early, | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 153 | // before the modules for these jars are processed and the actual paths are generated, and | 
|  | 154 | // later on a singleton adds commands to copy actual jars to the predefined paths. | 
|  | 155 | dexPaths android.WritablePaths | 
|  | 156 |  | 
| Paul Duffin | 3e2db5c | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 157 | // Map from module name (without prebuilt_ prefix) to the predefined build path. | 
|  | 158 | dexPathsByModule map[string]android.WritablePath | 
|  | 159 |  | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 160 | // A list of dex locations (a.k.a. on-device paths) to the boot jars. | 
|  | 161 | dexLocations []string | 
|  | 162 | } | 
|  | 163 |  | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 164 | var updatableBootConfigKey = android.NewOnceKey("apexBootConfig") | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 165 |  | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 166 | // Returns apex boot config. | 
|  | 167 | func GetApexBootConfig(ctx android.PathContext) apexBootConfig { | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 168 | return ctx.Config().Once(updatableBootConfigKey, func() interface{} { | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 169 | apexBootJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 170 |  | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 171 | dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "apex_bootjars") | 
|  | 172 | dexPaths := apexBootJars.BuildPaths(ctx, dir) | 
|  | 173 | dexPathsByModuleName := apexBootJars.BuildPathsByModule(ctx, dir) | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 174 |  | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 175 | dexLocations := apexBootJars.DevicePaths(ctx.Config(), android.Android) | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 176 |  | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 177 | return apexBootConfig{apexBootJars, dexPaths, dexPathsByModuleName, dexLocations} | 
|  | 178 | }).(apexBootConfig) | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
|  | 181 | // Returns a list of paths and a list of locations for the boot jars used in dexpreopt (to be | 
|  | 182 | // passed in -Xbootclasspath and -Xbootclasspath-locations arguments for dex2oat). | 
|  | 183 | func bcpForDexpreopt(ctx android.PathContext, withUpdatable bool) (android.WritablePaths, []string) { | 
|  | 184 | // Non-updatable boot jars (they are used both in the boot image and in dexpreopt). | 
|  | 185 | bootImage := defaultBootImageConfig(ctx) | 
|  | 186 | dexPaths := bootImage.dexPathsDeps | 
|  | 187 | // The dex locations for all Android variants are identical. | 
|  | 188 | dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps | 
|  | 189 |  | 
|  | 190 | if withUpdatable { | 
| satayev | 7a552ba | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 191 | // Apex boot jars (they are used only in dexpreopt, but not in the boot image). | 
|  | 192 | apexBootConfig := GetApexBootConfig(ctx) | 
|  | 193 | dexPaths = append(dexPaths, apexBootConfig.dexPaths...) | 
|  | 194 | dexLocations = append(dexLocations, apexBootConfig.dexLocations...) | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
|  | 197 | return dexPaths, dexLocations | 
|  | 198 | } | 
|  | 199 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 200 | var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath") | 
|  | 201 |  | 
|  | 202 | var copyOf = android.CopyOf | 
|  | 203 |  | 
|  | 204 | func init() { | 
|  | 205 | android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars) | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | func dexpreoptConfigMakevars(ctx android.MakeVarsContext) { | 
| Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 209 | ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules.CopyOfApexJarPairs(), ":")) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 210 | } |