blob: 5d74b21350b7c87933103309608fd539a2515aee [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 Trafimovicha4a83b02020-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 }
64
65 return targets
66}
67
Jiyong Park0b238752019-10-29 11:23:10 +090068func stemOf(moduleName string) string {
69 // b/139391334: the stem of framework-minus-apex is framework
70 // This is hard coded here until we find a good way to query the stem
71 // of a module before any other mutators are run
72 if moduleName == "framework-minus-apex" {
73 return "framework"
74 }
75 return moduleName
76}
77
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000078var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +000079 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
80 artBootImageName = "art"
81 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000082)
Ulyana Trafimovichde534412019-11-08 10:51:01 +000083
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000084// Construct the global boot image configs.
85func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
86 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
87
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000088 global := dexpreopt.GetGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000089 targets := dexpreoptTargets(ctx)
90 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +000091
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +010092 artModules := global.ArtApexJars
Ulya Trafimovich44561882020-01-03 13:25:54 +000093 // With EMMA_INSTRUMENT_FRAMEWORK=true the Core libraries depend on jacoco.
94 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
95 artModules = append(artModules, "jacocoagent")
96 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000097 frameworkModules := android.RemoveListFromList(global.BootJars,
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000098 concat(artModules, dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +000099
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000100 artSubdir := "apex/com.android.art/javalib"
101 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000102
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000103 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100104 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000105 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
106 }
107 for _, m := range frameworkModules {
108 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000109 }
110
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000111 // ART config for the primary boot image in the ART apex.
112 // It includes the Core Libraries.
113 artCfg := bootImageConfig{
114 extension: false,
115 name: artBootImageName,
116 stem: "boot",
117 installSubdir: artSubdir,
118 modules: artModules,
119 dexLocations: artLocations,
120 dexLocationsDeps: artLocations,
121 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000122
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000123 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000124 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000125 frameworkCfg := bootImageConfig{
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000126 extension: true,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000127 name: frameworkBootImageName,
128 stem: "boot",
129 installSubdir: frameworkSubdir,
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000130 modules: frameworkModules,
131 dexLocations: frameworkLocations,
132 dexLocationsDeps: append(artLocations, frameworkLocations...),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000133 }
134
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000135 configs := map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000136 artBootImageName: &artCfg,
137 frameworkBootImageName: &frameworkCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000138 }
139
140 // common to all configs
141 for _, c := range configs {
142 c.targets = targets
143
144 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
145 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
146
147 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
148 imageName := c.firstModuleNameOrStem() + ".art"
149
150 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
151
152 // The path to bootclasspath dex files needs to be known at module
153 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
154 // Set up known paths for them, the singleton rules will copy them there.
155 // TODO(b/143682396): use module dependencies instead
156 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
157 for _, m := range c.modules {
158 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
159 }
160 c.dexPathsDeps = c.dexPaths
161
162 c.images = make(map[android.ArchType]android.OutputPath)
163 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
164
165 for _, target := range targets {
166 arch := target.Arch.ArchType
167 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
168 c.images[arch] = imageDir.Join(ctx, imageName)
169 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000170 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800171
172 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100173 }
174
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000175 // specific to the framework config
176 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
Ulya Trafimovichb0a2d372020-01-28 14:42:41 +0000177 frameworkCfg.primaryImages = artCfg.images
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000178 frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...)
179
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000180 return configs
181 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000182}
183
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000184func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000185 return *genBootImageConfigs(ctx)[artBootImageName]
186}
187
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000188func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000189 return *genBootImageConfigs(ctx)[frameworkBootImageName]
190}
191
Colin Cross44df5812019-02-15 23:06:46 -0800192func defaultBootclasspath(ctx android.PathContext) []string {
193 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000194 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800195 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000196
Roshan Piusccc26ef2019-11-27 09:37:46 -0800197 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
198 for i, p := range global.UpdatableBootJars {
199 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
200 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000201
202 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800203 return bootclasspath
204 })
205}
206
207var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
208
209var copyOf = android.CopyOf
210
211func init() {
212 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
213}
214
215func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
216 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000217 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800218 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800219
220 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800221}