blob: d849dc2a8bcfb0ec4f6e4849399e05c318415b63 [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.
Roshan Pius9b51a402019-11-21 12:36:53 -080040 for _, m := range global.UpdatableSystemServerJars {
Roshan Pius9b51a402019-11-21 12:36:53 -080041 systemServerClasspathLocations = append(systemServerClasspathLocations,
Roshan Piusccc26ef2019-11-27 09:37:46 -080042 dexpreopt.GetJarLocationFromApexJarPair(m))
Roshan Pius9b51a402019-11-21 12:36:53 -080043 }
Nicolas Geoffray47cbfcd2020-02-17 14:55:06 +000044 if len(systemServerClasspathLocations) != len(global.SystemServerJars)+len(global.UpdatableSystemServerJars) {
45 panic(fmt.Errorf("Wrong number of system server jars, got %d, expected %d",
46 len(systemServerClasspathLocations),
47 len(global.SystemServerJars)+len(global.UpdatableSystemServerJars)))
48 }
Colin Cross44df5812019-02-15 23:06:46 -080049 return systemServerClasspathLocations
50 })
51}
52
53var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
54
Colin Crossc11e0c52019-05-08 15:18:22 -070055// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
56// supported through native bridge.
57func dexpreoptTargets(ctx android.PathContext) []android.Target {
58 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070059 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070060 if target.NativeBridge == android.NativeBridgeDisabled {
61 targets = append(targets, target)
62 }
63 }
David Srbecky7f8dac12020-02-13 16:00:45 +000064 // We may also need the images on host in order to run host-based tests.
65 for _, target := range ctx.Config().Targets[android.BuildOs] {
66 targets = append(targets, target)
67 }
Colin Crossc11e0c52019-05-08 15:18:22 -070068
69 return targets
70}
71
Jiyong Park0b238752019-10-29 11:23:10 +090072func stemOf(moduleName string) string {
73 // b/139391334: the stem of framework-minus-apex is framework
74 // This is hard coded here until we find a good way to query the stem
75 // of a module before any other mutators are run
76 if moduleName == "framework-minus-apex" {
77 return "framework"
78 }
79 return moduleName
80}
81
Ulya Trafimovich50c4a4b2020-04-21 15:36:33 +010082func getDexLocation(ctx android.PathContext, target android.Target, module string) string {
83 apex, jar := android.SplitApexJarPair(module)
84
85 name := stemOf(jar) + ".jar"
86
87 var subdir string
88 if apex == "platform" {
89 // Special apex name "platform" denotes jars do not come from an apex, but are part
90 // of the platform. Such jars are installed on the /system partition on device.
91 subdir = "system/framework"
92 } else {
93 subdir = filepath.Join("apex", apex, "javalib")
94 }
95
David Srbeckyab994982020-03-30 17:24:13 +010096 if target.Os.Class == android.Host {
David Srbecky0f6fdf52020-04-06 18:48:22 +010097 return filepath.Join(ctx.Config().Getenv("OUT_DIR"), "host", ctx.Config().PrebuiltOS(), subdir, name)
David Srbeckyab994982020-03-30 17:24:13 +010098 } else {
99 return filepath.Join("/", subdir, name)
100 }
101}
102
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000103var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000104 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
105 artBootImageName = "art"
106 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000107)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000108
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000109// Construct the global boot image configs.
110func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
111 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
112
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000113 global := dexpreopt.GetGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000114 targets := dexpreoptTargets(ctx)
115 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000116
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100117 artModules := global.ArtApexJars
Ulya Trafimovich44561882020-01-03 13:25:54 +0000118 // With EMMA_INSTRUMENT_FRAMEWORK=true the Core libraries depend on jacoco.
119 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
120 artModules = append(artModules, "jacocoagent")
121 }
Ulya Trafimovich50c4a4b2020-04-21 15:36:33 +0100122 frameworkModules := android.RemoveListFromList(global.BootJars, artModules)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000123
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000124 artSubdir := "apex/com.android.art/javalib"
125 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000126
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000127 // ART config for the primary boot image in the ART apex.
128 // It includes the Core Libraries.
129 artCfg := bootImageConfig{
David Srbeckyab994982020-03-30 17:24:13 +0100130 name: artBootImageName,
131 stem: "boot",
132 installSubdir: artSubdir,
133 modules: artModules,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000134 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000135
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000136 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000137 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000138 frameworkCfg := bootImageConfig{
David Srbeckyab994982020-03-30 17:24:13 +0100139 extends: &artCfg,
140 name: frameworkBootImageName,
141 stem: "boot",
142 installSubdir: frameworkSubdir,
143 modules: frameworkModules,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000144 }
145
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000146 configs := map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000147 artBootImageName: &artCfg,
148 frameworkBootImageName: &frameworkCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000149 }
150
151 // common to all configs
152 for _, c := range configs {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000153 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
154 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
155
156 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
157 imageName := c.firstModuleNameOrStem() + ".art"
158
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000159 // The path to bootclasspath dex files needs to be known at module
160 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
161 // Set up known paths for them, the singleton rules will copy them there.
162 // TODO(b/143682396): use module dependencies instead
163 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
164 for _, m := range c.modules {
Ulya Trafimovich50c4a4b2020-04-21 15:36:33 +0100165 _, jar := android.SplitApexJarPair(m)
166 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(jar)+".jar"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000167 }
168 c.dexPathsDeps = c.dexPaths
169
David Srbeckyc177ebe2020-02-18 20:43:06 +0000170 // Create target-specific variants.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000171 for _, target := range targets {
172 arch := target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000173 imageDir := c.dir.Join(ctx, target.Os.String(), c.installSubdir, arch.String())
David Srbeckyc177ebe2020-02-18 20:43:06 +0000174 variant := &bootImageVariant{
175 bootImageConfig: c,
176 target: target,
177 images: imageDir.Join(ctx, imageName),
178 imagesDeps: c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"),
179 }
David Srbeckyab994982020-03-30 17:24:13 +0100180 for _, m := range c.modules {
Ulya Trafimovich50c4a4b2020-04-21 15:36:33 +0100181 variant.dexLocations = append(variant.dexLocations, getDexLocation(ctx, target, m))
David Srbeckyab994982020-03-30 17:24:13 +0100182 }
183 variant.dexLocationsDeps = variant.dexLocations
David Srbeckyc177ebe2020-02-18 20:43:06 +0000184 c.variants = append(c.variants, variant)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000185 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800186
187 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100188 }
189
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000190 // specific to the framework config
191 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000192 for i := range targets {
193 frameworkCfg.variants[i].primaryImages = artCfg.variants[i].images
David Srbeckyab994982020-03-30 17:24:13 +0100194 frameworkCfg.variants[i].dexLocationsDeps = append(artCfg.variants[i].dexLocations, frameworkCfg.variants[i].dexLocationsDeps...)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000195 }
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000196
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000197 return configs
198 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000199}
200
David Srbeckyc177ebe2020-02-18 20:43:06 +0000201func artBootImageConfig(ctx android.PathContext) *bootImageConfig {
202 return genBootImageConfigs(ctx)[artBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000203}
204
David Srbeckyc177ebe2020-02-18 20:43:06 +0000205func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig {
206 return genBootImageConfigs(ctx)[frameworkBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000207}
208
Colin Cross44df5812019-02-15 23:06:46 -0800209func defaultBootclasspath(ctx android.PathContext) []string {
210 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000211 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800212 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000213
Roshan Piusccc26ef2019-11-27 09:37:46 -0800214 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
215 for i, p := range global.UpdatableBootJars {
216 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
217 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000218
David Srbeckyab994982020-03-30 17:24:13 +0100219 bootclasspath := append(copyOf(image.getAnyAndroidVariant().dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800220 return bootclasspath
221 })
222}
223
224var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
225
226var copyOf = android.CopyOf
227
228func init() {
229 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
230}
231
232func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
233 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
David Srbeckyab994982020-03-30 17:24:13 +0100234 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).getAnyAndroidVariant().dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800235 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800236
237 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800238}