blob: d77d3e144e8a819b7791f8f7879c24fb57c25709 [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
Nicolas Geoffray47cbfcd2020-02-17 14:55:06 +000033 var dexpreoptJars = *DexpreoptedSystemServerJars(ctx.Config())
34 // 1) The jars that are dexpreopted.
35 for _, m := range dexpreoptJars {
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 // 3) The jars from make (which are not updatable, not preopted).
45 for _, m := range dexpreopt.NonUpdatableSystemServerJars(ctx, global) {
46 if !android.InList(m, dexpreoptJars) {
47 systemServerClasspathLocations = append(systemServerClasspathLocations,
48 filepath.Join("/system/framework", m+".jar"))
49 }
50 }
51 if len(systemServerClasspathLocations) != len(global.SystemServerJars)+len(global.UpdatableSystemServerJars) {
52 panic(fmt.Errorf("Wrong number of system server jars, got %d, expected %d",
53 len(systemServerClasspathLocations),
54 len(global.SystemServerJars)+len(global.UpdatableSystemServerJars)))
55 }
Colin Cross44df5812019-02-15 23:06:46 -080056 return systemServerClasspathLocations
57 })
58}
59
60var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
61
Colin Crossc11e0c52019-05-08 15:18:22 -070062// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
63// supported through native bridge.
64func dexpreoptTargets(ctx android.PathContext) []android.Target {
65 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070066 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070067 if target.NativeBridge == android.NativeBridgeDisabled {
68 targets = append(targets, target)
69 }
70 }
David Srbecky7f8dac12020-02-13 16:00:45 +000071 // We may also need the images on host in order to run host-based tests.
72 for _, target := range ctx.Config().Targets[android.BuildOs] {
73 targets = append(targets, target)
74 }
Colin Crossc11e0c52019-05-08 15:18:22 -070075
76 return targets
77}
78
Jiyong Park0b238752019-10-29 11:23:10 +090079func stemOf(moduleName string) string {
80 // b/139391334: the stem of framework-minus-apex is framework
81 // This is hard coded here until we find a good way to query the stem
82 // of a module before any other mutators are run
83 if moduleName == "framework-minus-apex" {
84 return "framework"
85 }
86 return moduleName
87}
88
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000089var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +000090 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
91 artBootImageName = "art"
92 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000093)
Ulyana Trafimovichde534412019-11-08 10:51:01 +000094
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000095// Construct the global boot image configs.
96func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
97 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
98
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000099 global := dexpreopt.GetGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000100 targets := dexpreoptTargets(ctx)
101 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000102
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100103 artModules := global.ArtApexJars
Ulya Trafimovich44561882020-01-03 13:25:54 +0000104 // With EMMA_INSTRUMENT_FRAMEWORK=true the Core libraries depend on jacoco.
105 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
106 artModules = append(artModules, "jacocoagent")
107 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000108 frameworkModules := android.RemoveListFromList(global.BootJars,
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000109 concat(artModules, dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000110
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000111 artSubdir := "apex/com.android.art/javalib"
112 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000113
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000114 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100115 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000116 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
117 }
118 for _, m := range frameworkModules {
119 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000120 }
121
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000122 // ART config for the primary boot image in the ART apex.
123 // It includes the Core Libraries.
124 artCfg := bootImageConfig{
125 extension: false,
126 name: artBootImageName,
127 stem: "boot",
128 installSubdir: artSubdir,
129 modules: artModules,
130 dexLocations: artLocations,
131 dexLocationsDeps: artLocations,
132 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000133
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000134 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000135 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000136 frameworkCfg := bootImageConfig{
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000137 extension: true,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000138 name: frameworkBootImageName,
139 stem: "boot",
140 installSubdir: frameworkSubdir,
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000141 modules: frameworkModules,
142 dexLocations: frameworkLocations,
143 dexLocationsDeps: append(artLocations, frameworkLocations...),
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
David Srbecky7f8dac12020-02-13 16:00:45 +0000159 c.imageLocations = []string{c.dir.Join(ctx, "android", c.installSubdir, imageName).String()}
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000160
161 // The path to bootclasspath dex files needs to be known at module
162 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
163 // Set up known paths for them, the singleton rules will copy them there.
164 // TODO(b/143682396): use module dependencies instead
165 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
166 for _, m := range c.modules {
167 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
168 }
169 c.dexPathsDeps = c.dexPaths
170
David Srbeckyc177ebe2020-02-18 20:43:06 +0000171 // Create target-specific variants.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000172 for _, target := range targets {
173 arch := target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000174 imageDir := c.dir.Join(ctx, target.Os.String(), c.installSubdir, arch.String())
David Srbeckyc177ebe2020-02-18 20:43:06 +0000175 variant := &bootImageVariant{
176 bootImageConfig: c,
177 target: target,
178 images: imageDir.Join(ctx, imageName),
179 imagesDeps: c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"),
180 }
181 c.variants = append(c.variants, variant)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000182 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800183
184 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100185 }
186
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000187 // specific to the framework config
188 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000189 for i := range targets {
190 frameworkCfg.variants[i].primaryImages = artCfg.variants[i].images
191 }
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000192 frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...)
193
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000194 return configs
195 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000196}
197
David Srbeckyc177ebe2020-02-18 20:43:06 +0000198func artBootImageConfig(ctx android.PathContext) *bootImageConfig {
199 return genBootImageConfigs(ctx)[artBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000200}
201
David Srbeckyc177ebe2020-02-18 20:43:06 +0000202func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig {
203 return genBootImageConfigs(ctx)[frameworkBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000204}
205
Colin Cross44df5812019-02-15 23:06:46 -0800206func defaultBootclasspath(ctx android.PathContext) []string {
207 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000208 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800209 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000210
Roshan Piusccc26ef2019-11-27 09:37:46 -0800211 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
212 for i, p := range global.UpdatableBootJars {
213 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
214 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000215
216 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800217 return bootclasspath
218 })
219}
220
221var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
222
223var copyOf = android.CopyOf
224
225func init() {
226 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
227}
228
229func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
230 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000231 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800232 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800233
234 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800235}