blob: 37248609d60c614b733156ac3724c89e039a4471 [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().
Artur Satayev97259dc2021-04-07 15:17:14 +010029func systemServerClasspath(ctx android.PathContext) []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)...)
satayev9a6f87e2021-05-04 16:14:48 +010042
43 if expectedLen := global.SystemServerJars.Len() + global.UpdatableSystemServerJars.Len(); expectedLen != len(systemServerClasspathLocations) {
44 panic(fmt.Errorf("wrong number of system server jars, got %d, expected %d", len(systemServerClasspathLocations), expectedLen))
Nicolas Geoffray47cbfcd2020-02-17 14:55:06 +000045 }
Colin Cross44df5812019-02-15 23:06:46 -080046 return systemServerClasspathLocations
47 })
48}
49
50var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
51
Colin Crossc11e0c52019-05-08 15:18:22 -070052// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
53// supported through native bridge.
54func dexpreoptTargets(ctx android.PathContext) []android.Target {
55 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070056 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070057 if target.NativeBridge == android.NativeBridgeDisabled {
58 targets = append(targets, target)
59 }
60 }
David Srbecky7f8dac12020-02-13 16:00:45 +000061 // We may also need the images on host in order to run host-based tests.
62 for _, target := range ctx.Config().Targets[android.BuildOs] {
63 targets = append(targets, target)
64 }
Colin Crossc11e0c52019-05-08 15:18:22 -070065
66 return targets
67}
68
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000069var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +000070 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
71 artBootImageName = "art"
72 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000073)
Ulyana Trafimovichde534412019-11-08 10:51:01 +000074
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000075// Construct the global boot image configs.
76func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
77 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
78
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000079 global := dexpreopt.GetGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000080 targets := dexpreoptTargets(ctx)
81 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +000082
Paul Duffin7d584e92020-10-23 18:26:03 +010083 artModules := global.ArtApexJars
Paul Duffin7d584e92020-10-23 18:26:03 +010084 frameworkModules := global.BootJars.RemoveList(artModules)
Nicolas Geoffray72892f12019-02-22 15:34:40 +000085
Jeongik Chaa5969092021-05-07 18:53:21 +090086 artDirOnHost := "apex/art_boot_images/javalib"
Jeongik Cha4dda75e2021-04-27 23:56:44 +090087 artDirOnDevice := "apex/com.android.art/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{
Jeongik Cha4dda75e2021-04-27 23:56:44 +090093 name: artBootImageName,
94 stem: "boot",
95 installDirOnHost: artDirOnHost,
96 installDirOnDevice: artDirOnDevice,
97 modules: artModules,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000098 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +000099
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000100 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000101 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000102 frameworkCfg := bootImageConfig{
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900103 extends: &artCfg,
104 name: frameworkBootImageName,
105 stem: "boot",
106 installDirOnHost: frameworkSubdir,
107 installDirOnDevice: frameworkSubdir,
108 modules: frameworkModules,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000109 }
110
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000111 configs := map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000112 artBootImageName: &artCfg,
113 frameworkBootImageName: &frameworkCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000114 }
115
116 // common to all configs
117 for _, c := range configs {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000118 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
119 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
120
121 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
Ulya Trafimovich8640ab92020-05-11 18:06:15 +0100122 imageName := c.firstModuleNameOrStem(ctx) + ".art"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000123
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000124 // The path to bootclasspath dex files needs to be known at module
125 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
126 // Set up known paths for them, the singleton rules will copy them there.
127 // TODO(b/143682396): use module dependencies instead
128 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100129 c.dexPaths = c.modules.BuildPaths(ctx, inputDir)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000130 c.dexPathsDeps = c.dexPaths
131
David Srbeckyc177ebe2020-02-18 20:43:06 +0000132 // Create target-specific variants.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000133 for _, target := range targets {
134 arch := target.Arch.ArchType
Jeongik Chaa5969092021-05-07 18:53:21 +0900135 imageDir := c.dir.Join(ctx, target.Os.String(), c.installDirOnHost, arch.String())
David Srbeckyc177ebe2020-02-18 20:43:06 +0000136 variant := &bootImageVariant{
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900137 bootImageConfig: c,
138 target: target,
139 imagePathOnHost: imageDir.Join(ctx, imageName),
140 imagePathOnDevice: filepath.Join("/", c.installDirOnDevice, arch.String(), imageName),
141 imagesDeps: c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"),
142 dexLocations: c.modules.DevicePaths(ctx.Config(), target.Os),
David Srbeckyab994982020-03-30 17:24:13 +0100143 }
144 variant.dexLocationsDeps = variant.dexLocations
David Srbeckyc177ebe2020-02-18 20:43:06 +0000145 c.variants = append(c.variants, variant)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000146 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800147
148 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100149 }
150
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000151 // specific to the framework config
152 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000153 for i := range targets {
Jeongik Chaa5969092021-05-07 18:53:21 +0900154 frameworkCfg.variants[i].primaryImages = artCfg.variants[i].imagePathOnHost
David Srbeckyab994982020-03-30 17:24:13 +0100155 frameworkCfg.variants[i].dexLocationsDeps = append(artCfg.variants[i].dexLocations, frameworkCfg.variants[i].dexLocationsDeps...)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000156 }
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000157
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000158 return configs
159 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000160}
161
David Srbeckyc177ebe2020-02-18 20:43:06 +0000162func artBootImageConfig(ctx android.PathContext) *bootImageConfig {
163 return genBootImageConfigs(ctx)[artBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000164}
165
David Srbeckyc177ebe2020-02-18 20:43:06 +0000166func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig {
167 return genBootImageConfigs(ctx)[frameworkBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000168}
169
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000170// Updatable boot config allows to access build/install paths of updatable boot jars without going
171// through the usual trouble of registering dependencies on those modules and extracting build paths
172// from those dependencies.
173type updatableBootConfig struct {
174 // A list of updatable boot jars.
175 modules android.ConfiguredJarList
176
177 // A list of predefined build paths to updatable boot jars. They are configured very early,
178 // before the modules for these jars are processed and the actual paths are generated, and
179 // later on a singleton adds commands to copy actual jars to the predefined paths.
180 dexPaths android.WritablePaths
181
182 // A list of dex locations (a.k.a. on-device paths) to the boot jars.
183 dexLocations []string
184}
185
186var updatableBootConfigKey = android.NewOnceKey("updatableBootConfig")
187
188// Returns updatable boot config.
189func GetUpdatableBootConfig(ctx android.PathContext) updatableBootConfig {
190 return ctx.Config().Once(updatableBootConfigKey, func() interface{} {
191 updatableBootJars := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars
192
193 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "updatable_bootjars")
194 dexPaths := updatableBootJars.BuildPaths(ctx, dir)
195
196 dexLocations := updatableBootJars.DevicePaths(ctx.Config(), android.Android)
197
198 return updatableBootConfig{updatableBootJars, dexPaths, dexLocations}
199 }).(updatableBootConfig)
200}
201
202// Returns a list of paths and a list of locations for the boot jars used in dexpreopt (to be
203// passed in -Xbootclasspath and -Xbootclasspath-locations arguments for dex2oat).
204func bcpForDexpreopt(ctx android.PathContext, withUpdatable bool) (android.WritablePaths, []string) {
205 // Non-updatable boot jars (they are used both in the boot image and in dexpreopt).
206 bootImage := defaultBootImageConfig(ctx)
207 dexPaths := bootImage.dexPathsDeps
208 // The dex locations for all Android variants are identical.
209 dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
210
211 if withUpdatable {
212 // Updatable boot jars (they are used only in dexpreopt, but not in the boot image).
213 updBootConfig := GetUpdatableBootConfig(ctx)
214 dexPaths = append(dexPaths, updBootConfig.dexPaths...)
215 dexLocations = append(dexLocations, updBootConfig.dexLocations...)
216 }
217
218 return dexPaths, dexLocations
219}
220
Colin Cross44df5812019-02-15 23:06:46 -0800221var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
222
223var copyOf = android.CopyOf
224
225func init() {
226 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
227}
228
229func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100230 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules.CopyOfApexJarPairs(), ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800231}