blob: 64b2656403d9fb0623a3fcdbc29b4a8aa4dd23f3 [file] [log] [blame]
Colin Cross44df5812019-02-15 23:06:46 -08001// 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
15package java
16
17import (
Nicolas Geoffray47cbfcd2020-02-17 14:55:06 +000018 "fmt"
Colin Cross44df5812019-02-15 23:06:46 -080019 "path/filepath"
20 "strings"
Colin Cross2d00f0d2019-05-09 21:50:00 -070021
22 "android/soong/android"
23 "android/soong/dexpreopt"
Colin Cross44df5812019-02-15 23:06:46 -080024)
25
Colin Cross44df5812019-02-15 23:06:46 -080026// systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed
27// once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
28// ctx.Config().
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000029func systemServerClasspath(ctx android.MakeVarsContext) []string {
Colin Cross44df5812019-02-15 23:06:46 -080030 return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000031 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -080032 var systemServerClasspathLocations []string
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +000033 nonUpdatable := dexpreopt.NonUpdatableSystemServerJars(ctx, global)
34 // 1) Non-updatable jars.
35 for _, m := range nonUpdatable {
Colin Cross44df5812019-02-15 23:06:46 -080036 systemServerClasspathLocations = append(systemServerClasspathLocations,
37 filepath.Join("/system/framework", m+".jar"))
38 }
Nicolas Geoffray47cbfcd2020-02-17 14:55:06 +000039 // 2) The jars that are from an updatable apex.
Ulya Trafimovich249386a2020-07-01 14:31:13 +010040 systemServerClasspathLocations = append(systemServerClasspathLocations,
41 global.UpdatableSystemServerJars.DevicePaths(ctx.Config(), android.Android)...)
42 if len(systemServerClasspathLocations) != len(global.SystemServerJars)+global.UpdatableSystemServerJars.Len() {
Nicolas Geoffray47cbfcd2020-02-17 14:55:06 +000043 panic(fmt.Errorf("Wrong number of system server jars, got %d, expected %d",
44 len(systemServerClasspathLocations),
Ulya Trafimovich249386a2020-07-01 14:31:13 +010045 len(global.SystemServerJars)+global.UpdatableSystemServerJars.Len()))
Nicolas Geoffray47cbfcd2020-02-17 14:55:06 +000046 }
Colin Cross44df5812019-02-15 23:06:46 -080047 return systemServerClasspathLocations
48 })
49}
50
51var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
52
Colin Crossc11e0c52019-05-08 15:18:22 -070053// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
54// supported through native bridge.
55func dexpreoptTargets(ctx android.PathContext) []android.Target {
56 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070057 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070058 if target.NativeBridge == android.NativeBridgeDisabled {
59 targets = append(targets, target)
60 }
61 }
David Srbecky7f8dac12020-02-13 16:00:45 +000062 // We may also need the images on host in order to run host-based tests.
63 for _, target := range ctx.Config().Targets[android.BuildOs] {
64 targets = append(targets, target)
65 }
Colin Crossc11e0c52019-05-08 15:18:22 -070066
67 return targets
68}
69
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000070var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +000071 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
72 artBootImageName = "art"
73 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000074)
Ulyana Trafimovichde534412019-11-08 10:51:01 +000075
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000076// Construct the global boot image configs.
77func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
78 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
79
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000080 global := dexpreopt.GetGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000081 targets := dexpreoptTargets(ctx)
82 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +000083
Paul Duffin7d584e92020-10-23 18:26:03 +010084 artModules := global.ArtApexJars
Paul Duffin7d584e92020-10-23 18:26:03 +010085 frameworkModules := global.BootJars.RemoveList(artModules)
Nicolas Geoffray72892f12019-02-22 15:34:40 +000086
Martin Stjernholmea581fc2020-10-14 23:29:49 +010087 artSubdir := "apex/art_boot_images/javalib"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000088 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +000089
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000090 // ART config for the primary boot image in the ART apex.
91 // It includes the Core Libraries.
92 artCfg := bootImageConfig{
David Srbeckyab994982020-03-30 17:24:13 +010093 name: artBootImageName,
94 stem: "boot",
95 installSubdir: artSubdir,
96 modules: artModules,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000097 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +000098
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000099 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000100 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000101 frameworkCfg := bootImageConfig{
David Srbeckyab994982020-03-30 17:24:13 +0100102 extends: &artCfg,
103 name: frameworkBootImageName,
104 stem: "boot",
105 installSubdir: frameworkSubdir,
106 modules: frameworkModules,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000107 }
108
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000109 configs := map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000110 artBootImageName: &artCfg,
111 frameworkBootImageName: &frameworkCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000112 }
113
114 // common to all configs
115 for _, c := range configs {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000116 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
117 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
118
119 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
Ulya Trafimovich8640ab92020-05-11 18:06:15 +0100120 imageName := c.firstModuleNameOrStem(ctx) + ".art"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000121
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000122 // The path to bootclasspath dex files needs to be known at module
123 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
124 // Set up known paths for them, the singleton rules will copy them there.
125 // TODO(b/143682396): use module dependencies instead
126 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100127 c.dexPaths = c.modules.BuildPaths(ctx, inputDir)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000128 c.dexPathsDeps = c.dexPaths
129
David Srbeckyc177ebe2020-02-18 20:43:06 +0000130 // Create target-specific variants.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000131 for _, target := range targets {
132 arch := target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000133 imageDir := c.dir.Join(ctx, target.Os.String(), c.installSubdir, arch.String())
David Srbeckyc177ebe2020-02-18 20:43:06 +0000134 variant := &bootImageVariant{
135 bootImageConfig: c,
136 target: target,
137 images: imageDir.Join(ctx, imageName),
138 imagesDeps: c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"),
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100139 dexLocations: c.modules.DevicePaths(ctx.Config(), target.Os),
David Srbeckyab994982020-03-30 17:24:13 +0100140 }
141 variant.dexLocationsDeps = variant.dexLocations
David Srbeckyc177ebe2020-02-18 20:43:06 +0000142 c.variants = append(c.variants, variant)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000143 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800144
145 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100146 }
147
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000148 // specific to the framework config
149 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000150 for i := range targets {
151 frameworkCfg.variants[i].primaryImages = artCfg.variants[i].images
David Srbeckyab994982020-03-30 17:24:13 +0100152 frameworkCfg.variants[i].dexLocationsDeps = append(artCfg.variants[i].dexLocations, frameworkCfg.variants[i].dexLocationsDeps...)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000153 }
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000154
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000155 return configs
156 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000157}
158
David Srbeckyc177ebe2020-02-18 20:43:06 +0000159func artBootImageConfig(ctx android.PathContext) *bootImageConfig {
160 return genBootImageConfigs(ctx)[artBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000161}
162
David Srbeckyc177ebe2020-02-18 20:43:06 +0000163func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig {
164 return genBootImageConfigs(ctx)[frameworkBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000165}
166
Colin Cross44df5812019-02-15 23:06:46 -0800167func defaultBootclasspath(ctx android.PathContext) []string {
168 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000169 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800170 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000171
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100172 updatableBootclasspath := global.UpdatableBootJars.DevicePaths(ctx.Config(), android.Android)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000173
David Srbeckyab994982020-03-30 17:24:13 +0100174 bootclasspath := append(copyOf(image.getAnyAndroidVariant().dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800175 return bootclasspath
176 })
177}
178
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000179// Updatable boot config allows to access build/install paths of updatable boot jars without going
180// through the usual trouble of registering dependencies on those modules and extracting build paths
181// from those dependencies.
182type updatableBootConfig struct {
183 // A list of updatable boot jars.
184 modules android.ConfiguredJarList
185
186 // A list of predefined build paths to updatable boot jars. They are configured very early,
187 // before the modules for these jars are processed and the actual paths are generated, and
188 // later on a singleton adds commands to copy actual jars to the predefined paths.
189 dexPaths android.WritablePaths
190
191 // A list of dex locations (a.k.a. on-device paths) to the boot jars.
192 dexLocations []string
193}
194
195var updatableBootConfigKey = android.NewOnceKey("updatableBootConfig")
196
197// Returns updatable boot config.
198func GetUpdatableBootConfig(ctx android.PathContext) updatableBootConfig {
199 return ctx.Config().Once(updatableBootConfigKey, func() interface{} {
200 updatableBootJars := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars
201
202 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "updatable_bootjars")
203 dexPaths := updatableBootJars.BuildPaths(ctx, dir)
204
205 dexLocations := updatableBootJars.DevicePaths(ctx.Config(), android.Android)
206
207 return updatableBootConfig{updatableBootJars, dexPaths, dexLocations}
208 }).(updatableBootConfig)
209}
210
211// Returns a list of paths and a list of locations for the boot jars used in dexpreopt (to be
212// passed in -Xbootclasspath and -Xbootclasspath-locations arguments for dex2oat).
213func bcpForDexpreopt(ctx android.PathContext, withUpdatable bool) (android.WritablePaths, []string) {
214 // Non-updatable boot jars (they are used both in the boot image and in dexpreopt).
215 bootImage := defaultBootImageConfig(ctx)
216 dexPaths := bootImage.dexPathsDeps
217 // The dex locations for all Android variants are identical.
218 dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
219
220 if withUpdatable {
221 // Updatable boot jars (they are used only in dexpreopt, but not in the boot image).
222 updBootConfig := GetUpdatableBootConfig(ctx)
223 dexPaths = append(dexPaths, updBootConfig.dexPaths...)
224 dexLocations = append(dexLocations, updBootConfig.dexLocations...)
225 }
226
227 return dexPaths, dexLocations
228}
229
Colin Cross44df5812019-02-15 23:06:46 -0800230var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
231
232var copyOf = android.CopyOf
233
234func init() {
235 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
236}
237
238func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
239 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
David Srbeckyab994982020-03-30 17:24:13 +0100240 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).getAnyAndroidVariant().dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800241 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800242
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100243 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules.CopyOfApexJarPairs(), ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800244}