blob: 429bbdba6f874de8c41c571c6a723f5f3c4528f3 [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
25// dexpreoptGlobalConfig returns the global dexpreopt.config. It is loaded once the first time it is called for any
26// ctx.Config(), and returns the same data for all future calls with the same ctx.Config(). A value can be inserted
27// for tests using setDexpreoptTestGlobalConfig.
28func dexpreoptGlobalConfig(ctx android.PathContext) dexpreopt.GlobalConfig {
Colin Cross2d00f0d2019-05-09 21:50:00 -070029 return dexpreoptGlobalConfigRaw(ctx).global
30}
31
32type globalConfigAndRaw struct {
33 global dexpreopt.GlobalConfig
34 data []byte
35}
36
37func dexpreoptGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
Colin Cross44df5812019-02-15 23:06:46 -080038 return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
39 if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
40 ctx.AddNinjaFileDeps(f)
Colin Cross2d00f0d2019-05-09 21:50:00 -070041 globalConfig, data, err := dexpreopt.LoadGlobalConfig(ctx, f)
Colin Cross44df5812019-02-15 23:06:46 -080042 if err != nil {
43 panic(err)
44 }
Colin Cross2d00f0d2019-05-09 21:50:00 -070045 return globalConfigAndRaw{globalConfig, data}
Colin Cross44df5812019-02-15 23:06:46 -080046 }
47
48 // No global config filename set, see if there is a test config set
49 return ctx.Config().Once(dexpreoptTestGlobalConfigKey, func() interface{} {
50 // Nope, return a config with preopting disabled
Colin Cross2d00f0d2019-05-09 21:50:00 -070051 return globalConfigAndRaw{dexpreopt.GlobalConfig{
Mathieu Chartier6adeee12019-06-26 10:01:36 -070052 DisablePreopt: true,
53 DisableGenerateProfile: true,
Colin Cross2d00f0d2019-05-09 21:50:00 -070054 }, nil}
Colin Cross44df5812019-02-15 23:06:46 -080055 })
Colin Cross2d00f0d2019-05-09 21:50:00 -070056 }).(globalConfigAndRaw)
Colin Cross44df5812019-02-15 23:06:46 -080057}
58
59// setDexpreoptTestGlobalConfig sets a GlobalConfig that future calls to dexpreoptGlobalConfig will return. It must
60// be called before the first call to dexpreoptGlobalConfig for the config.
61func setDexpreoptTestGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) {
Colin Cross2d00f0d2019-05-09 21:50:00 -070062 config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
Colin Cross44df5812019-02-15 23:06:46 -080063}
64
65var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
66var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
67
68// systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed
69// once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
70// ctx.Config().
71func systemServerClasspath(ctx android.PathContext) []string {
72 return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
73 global := dexpreoptGlobalConfig(ctx)
74
75 var systemServerClasspathLocations []string
76 for _, m := range global.SystemServerJars {
77 systemServerClasspathLocations = append(systemServerClasspathLocations,
78 filepath.Join("/system/framework", m+".jar"))
79 }
80 return systemServerClasspathLocations
81 })
82}
83
84var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
85
Colin Crossc11e0c52019-05-08 15:18:22 -070086// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
87// supported through native bridge.
88func dexpreoptTargets(ctx android.PathContext) []android.Target {
89 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070090 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070091 if target.NativeBridge == android.NativeBridgeDisabled {
92 targets = append(targets, target)
93 }
94 }
95
96 return targets
97}
98
Ulya Trafimovich18263382019-10-23 15:56:32 +010099func getBootImageConfig(ctx android.PathContext, key android.OnceKey, name string,
Nicolas Geoffray24babe32019-10-30 11:26:52 +0000100 needZip bool) bootImageConfig {
Ulya Trafimovich18263382019-10-23 15:56:32 +0100101 return ctx.Config().Once(key, func() interface{} {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000102 global := dexpreoptGlobalConfig(ctx)
103
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100104 artModules := global.ArtApexJars
Nicolas Geoffray24babe32019-10-30 11:26:52 +0000105 nonFrameworkModules := concat(artModules, global.ProductUpdatableBootModules)
106 frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules)
107 imageModules := concat(artModules, frameworkModules)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000108
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100109 var bootLocations []string
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000110
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100111 for _, m := range artModules {
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100112 bootLocations = append(bootLocations,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100113 filepath.Join("/apex/com.android.art/javalib", m+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000114 }
115
Nicolas Geoffray24babe32019-10-30 11:26:52 +0000116 for _, m := range frameworkModules {
117 bootLocations = append(bootLocations,
118 filepath.Join("/system/framework", m+".jar"))
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100119 }
120
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000121 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
122 // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy
123 // them there.
124 // TODO: use module dependencies instead
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100125 var bootDexPaths android.WritablePaths
126 for _, m := range imageModules {
127 bootDexPaths = append(bootDexPaths,
Nicolas Geoffray24babe32019-10-30 11:26:52 +0000128 android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_"+name+"jars_input", m+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000129 }
130
Nicolas Geoffray24babe32019-10-30 11:26:52 +0000131 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_"+name+"jars")
132 symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_"+name+"jars_unstripped")
Ulya Trafimovich18263382019-10-23 15:56:32 +0100133
134 var zip android.WritablePath
135 if needZip {
136 zip = dir.Join(ctx, name+".zip")
137 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000138
Colin Crossc11e0c52019-05-08 15:18:22 -0700139 targets := dexpreoptTargets(ctx)
140
Dan Willemsen0f416782019-06-13 21:44:53 +0000141 imageConfig := bootImageConfig{
Ulya Trafimovich18263382019-10-23 15:56:32 +0100142 name: name,
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100143 modules: imageModules,
144 dexLocations: bootLocations,
145 dexPaths: bootDexPaths,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000146 dir: dir,
147 symbolsDir: symbolsDir,
Colin Crossc11e0c52019-05-08 15:18:22 -0700148 targets: targets,
Dan Willemsen0f416782019-06-13 21:44:53 +0000149 images: make(map[android.ArchType]android.OutputPath),
150 imagesDeps: make(map[android.ArchType]android.Paths),
Ulya Trafimovich18263382019-10-23 15:56:32 +0100151 zip: zip,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000152 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000153
154 for _, target := range targets {
155 imageDir := dir.Join(ctx, "system/framework", target.Arch.ArchType.String())
Ulya Trafimovich18263382019-10-23 15:56:32 +0100156 imageConfig.images[target.Arch.ArchType] = imageDir.Join(ctx, name+".art")
Dan Willemsen0f416782019-06-13 21:44:53 +0000157
158 imagesDeps := make([]android.Path, 0, len(imageConfig.modules)*3)
159 for _, dep := range imageConfig.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") {
160 imagesDeps = append(imagesDeps, dep)
161 }
162 imageConfig.imagesDeps[target.Arch.ArchType] = imagesDeps
163 }
164
165 return imageConfig
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000166 }).(bootImageConfig)
167}
168
Ulya Trafimovich18263382019-10-23 15:56:32 +0100169var defaultBootImageConfigKey = android.NewOnceKey("defaultBootImageConfig")
Ulya Trafimovichd5df9492019-10-24 17:29:50 +0100170var apexBootImageConfigKey = android.NewOnceKey("apexBootImageConfig")
171
Nicolas Geoffray24babe32019-10-30 11:26:52 +0000172func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
173 return getBootImageConfig(ctx, defaultBootImageConfigKey, "boot", true)
174}
175
Ulya Trafimovich18263382019-10-23 15:56:32 +0100176func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
Nicolas Geoffray24babe32019-10-30 11:26:52 +0000177 return getBootImageConfig(ctx, apexBootImageConfigKey, "apex", false)
Ulya Trafimovich18263382019-10-23 15:56:32 +0100178}
179
Colin Cross44df5812019-02-15 23:06:46 -0800180func defaultBootclasspath(ctx android.PathContext) []string {
181 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
182 global := dexpreoptGlobalConfig(ctx)
183 image := defaultBootImageConfig(ctx)
184 bootclasspath := append(copyOf(image.dexLocations), global.ProductUpdatableBootLocations...)
185 return bootclasspath
186 })
187}
188
189var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
190
191var copyOf = android.CopyOf
192
193func init() {
194 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
195}
196
197func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
198 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Nicolas Geoffray07b40072019-02-22 16:57:42 +0000199 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocations, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800200 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800201
202 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800203}