blob: 26c11054471cfe093d1798c630e4c0e331a654fd [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 (
Colin Cross44df5812019-02-15 23:06:46 -080018 "path/filepath"
19 "strings"
Colin Cross2d00f0d2019-05-09 21:50:00 -070020
21 "android/soong/android"
22 "android/soong/dexpreopt"
Colin Cross44df5812019-02-15 23:06:46 -080023)
24
Colin Crossc11e0c52019-05-08 15:18:22 -070025// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
26// supported through native bridge.
27func dexpreoptTargets(ctx android.PathContext) []android.Target {
28 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070029 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070030 if target.NativeBridge == android.NativeBridgeDisabled {
31 targets = append(targets, target)
32 }
33 }
David Srbecky7f8dac12020-02-13 16:00:45 +000034 // We may also need the images on host in order to run host-based tests.
Colin Cross0c66bc62021-07-20 09:47:41 -070035 for _, target := range ctx.Config().Targets[ctx.Config().BuildOS] {
David Srbecky7f8dac12020-02-13 16:00:45 +000036 targets = append(targets, target)
37 }
Colin Crossc11e0c52019-05-08 15:18:22 -070038
39 return targets
40}
41
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000042var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +000043 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
44 artBootImageName = "art"
45 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000046)
Ulyana Trafimovichde534412019-11-08 10:51:01 +000047
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000048// Construct the global boot image configs.
49func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
50 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
51
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000052 global := dexpreopt.GetGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000053 targets := dexpreoptTargets(ctx)
54 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +000055
Paul Duffin7d584e92020-10-23 18:26:03 +010056 artModules := global.ArtApexJars
Paul Duffin7d584e92020-10-23 18:26:03 +010057 frameworkModules := global.BootJars.RemoveList(artModules)
Nicolas Geoffray72892f12019-02-22 15:34:40 +000058
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000059 // ART config for the primary boot image in the ART apex.
60 // It includes the Core Libraries.
61 artCfg := bootImageConfig{
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000062 name: artBootImageName,
63 stem: "boot",
64 installDirOnHost: "apex/art_boot_images/javalib",
65 installDirOnDevice: "apex/com.android.art/javalib",
66 profileInstallPathInApex: "etc/boot-image.prof",
67 modules: artModules,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000068 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +000069
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000070 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +000071 // It includes framework libraries and depends on the ART config.
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000072 frameworkSubdir := "system/framework"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000073 frameworkCfg := bootImageConfig{
Jeongik Cha4dda75e2021-04-27 23:56:44 +090074 extends: &artCfg,
75 name: frameworkBootImageName,
76 stem: "boot",
77 installDirOnHost: frameworkSubdir,
78 installDirOnDevice: frameworkSubdir,
79 modules: frameworkModules,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000080 }
81
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000082 configs := map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +000083 artBootImageName: &artCfg,
84 frameworkBootImageName: &frameworkCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000085 }
86
87 // common to all configs
88 for _, c := range configs {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000089 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
90 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
91
92 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
Ulya Trafimovich8640ab92020-05-11 18:06:15 +010093 imageName := c.firstModuleNameOrStem(ctx) + ".art"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000094
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000095 // The path to bootclasspath dex files needs to be known at module
96 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
97 // Set up known paths for them, the singleton rules will copy them there.
98 // TODO(b/143682396): use module dependencies instead
99 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100100 c.dexPaths = c.modules.BuildPaths(ctx, inputDir)
Paul Duffin5f148ca2021-06-02 17:24:22 +0100101 c.dexPathsByModule = c.modules.BuildPathsByModule(ctx, inputDir)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000102 c.dexPathsDeps = c.dexPaths
103
David Srbeckyc177ebe2020-02-18 20:43:06 +0000104 // Create target-specific variants.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000105 for _, target := range targets {
106 arch := target.Arch.ArchType
Jeongik Chaa5969092021-05-07 18:53:21 +0900107 imageDir := c.dir.Join(ctx, target.Os.String(), c.installDirOnHost, arch.String())
David Srbeckyc177ebe2020-02-18 20:43:06 +0000108 variant := &bootImageVariant{
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900109 bootImageConfig: c,
110 target: target,
111 imagePathOnHost: imageDir.Join(ctx, imageName),
112 imagePathOnDevice: filepath.Join("/", c.installDirOnDevice, arch.String(), imageName),
113 imagesDeps: c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"),
114 dexLocations: c.modules.DevicePaths(ctx.Config(), target.Os),
David Srbeckyab994982020-03-30 17:24:13 +0100115 }
116 variant.dexLocationsDeps = variant.dexLocations
David Srbeckyc177ebe2020-02-18 20:43:06 +0000117 c.variants = append(c.variants, variant)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000118 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800119
120 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100121 }
122
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000123 // specific to the framework config
124 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000125 for i := range targets {
Jeongik Chaa5969092021-05-07 18:53:21 +0900126 frameworkCfg.variants[i].primaryImages = artCfg.variants[i].imagePathOnHost
Paul Duffinbff50e22021-06-04 17:25:28 +0100127 frameworkCfg.variants[i].primaryImagesDeps = artCfg.variants[i].imagesDeps.Paths()
David Srbeckyab994982020-03-30 17:24:13 +0100128 frameworkCfg.variants[i].dexLocationsDeps = append(artCfg.variants[i].dexLocations, frameworkCfg.variants[i].dexLocationsDeps...)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000129 }
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000130
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000131 return configs
132 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000133}
134
David Srbeckyc177ebe2020-02-18 20:43:06 +0000135func artBootImageConfig(ctx android.PathContext) *bootImageConfig {
136 return genBootImageConfigs(ctx)[artBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000137}
138
David Srbeckyc177ebe2020-02-18 20:43:06 +0000139func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig {
140 return genBootImageConfigs(ctx)[frameworkBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000141}
142
satayevd604b212021-07-21 14:23:52 +0100143// Apex boot config allows to access build/install paths of apex boot jars without going
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000144// through the usual trouble of registering dependencies on those modules and extracting build paths
145// from those dependencies.
satayevd604b212021-07-21 14:23:52 +0100146type apexBootConfig struct {
147 // A list of apex boot jars.
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000148 modules android.ConfiguredJarList
149
satayevd604b212021-07-21 14:23:52 +0100150 // A list of predefined build paths to apex boot jars. They are configured very early,
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000151 // before the modules for these jars are processed and the actual paths are generated, and
152 // later on a singleton adds commands to copy actual jars to the predefined paths.
153 dexPaths android.WritablePaths
154
Paul Duffin5f148ca2021-06-02 17:24:22 +0100155 // Map from module name (without prebuilt_ prefix) to the predefined build path.
156 dexPathsByModule map[string]android.WritablePath
157
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000158 // A list of dex locations (a.k.a. on-device paths) to the boot jars.
159 dexLocations []string
160}
161
satayevd604b212021-07-21 14:23:52 +0100162var updatableBootConfigKey = android.NewOnceKey("apexBootConfig")
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000163
satayevd604b212021-07-21 14:23:52 +0100164// Returns apex boot config.
165func GetApexBootConfig(ctx android.PathContext) apexBootConfig {
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000166 return ctx.Config().Once(updatableBootConfigKey, func() interface{} {
satayevd604b212021-07-21 14:23:52 +0100167 apexBootJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000168
satayevd604b212021-07-21 14:23:52 +0100169 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "apex_bootjars")
170 dexPaths := apexBootJars.BuildPaths(ctx, dir)
171 dexPathsByModuleName := apexBootJars.BuildPathsByModule(ctx, dir)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000172
satayevd604b212021-07-21 14:23:52 +0100173 dexLocations := apexBootJars.DevicePaths(ctx.Config(), android.Android)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000174
satayevd604b212021-07-21 14:23:52 +0100175 return apexBootConfig{apexBootJars, dexPaths, dexPathsByModuleName, dexLocations}
176 }).(apexBootConfig)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000177}
178
179// Returns a list of paths and a list of locations for the boot jars used in dexpreopt (to be
180// passed in -Xbootclasspath and -Xbootclasspath-locations arguments for dex2oat).
181func bcpForDexpreopt(ctx android.PathContext, withUpdatable bool) (android.WritablePaths, []string) {
182 // Non-updatable boot jars (they are used both in the boot image and in dexpreopt).
183 bootImage := defaultBootImageConfig(ctx)
184 dexPaths := bootImage.dexPathsDeps
185 // The dex locations for all Android variants are identical.
186 dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
187
188 if withUpdatable {
satayevd604b212021-07-21 14:23:52 +0100189 // Apex boot jars (they are used only in dexpreopt, but not in the boot image).
190 apexBootConfig := GetApexBootConfig(ctx)
191 dexPaths = append(dexPaths, apexBootConfig.dexPaths...)
192 dexLocations = append(dexLocations, apexBootConfig.dexLocations...)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000193 }
194
195 return dexPaths, dexLocations
196}
197
Colin Cross44df5812019-02-15 23:06:46 -0800198var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
199
200var copyOf = android.CopyOf
201
202func init() {
203 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
204}
205
206func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100207 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules.CopyOfApexJarPairs(), ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800208}