blob: 3113eb386fc121761f78c9ebdd81fbe0baf48e7c [file] [log] [blame]
Colin Cross43f08db2018-11-12 10:13:39 -08001// Copyright 2018 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 Cross43f08db2018-11-12 10:13:39 -080018 "android/soong/android"
19 "android/soong/dexpreopt"
20)
21
Martin Stjernholm6d415272020-01-31 17:10:36 +000022type dexpreopterInterface interface {
23 IsInstallable() bool // Structs that embed dexpreopter must implement this.
24 dexpreoptDisabled(ctx android.BaseModuleContext) bool
25}
26
Colin Cross43f08db2018-11-12 10:13:39 -080027type dexpreopter struct {
28 dexpreoptProperties DexpreoptProperties
29
Colin Cross70dda7e2019-10-01 22:05:35 -070030 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -070031 uncompressedDex bool
32 isSDKLibrary bool
Ulya Trafimovich76b08522021-01-14 17:52:43 +000033 isApp bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -070034 isTest bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -070035 isPresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -080036
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000037 manifestFile android.Path
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000038 statusFile android.WritablePath
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000039 enforceUsesLibs bool
40 classLoaderContexts dexpreopt.ClassLoaderContextMap
Colin Cross50ddcc42019-05-16 12:28:22 -070041
Colin Crossdeabb942019-02-11 14:11:09 -080042 builtInstalled string
Ulya Trafimovich76b08522021-01-14 17:52:43 +000043
Jeongik Chac6246672021-04-08 00:00:19 +090044 // The config is used for two purposes:
45 // - Passing dexpreopt information about libraries from Soong to Make. This is needed when
46 // a <uses-library> is defined in Android.bp, but used in Android.mk (see dex_preopt_config_merger.py).
47 // Note that dexpreopt.config might be needed even if dexpreopt is disabled for the library itself.
48 // - Dexpreopt post-processing (using dexpreopt artifacts from a prebuilt system image to incrementally
49 // dexpreopt another partition).
Ulya Trafimovich76b08522021-01-14 17:52:43 +000050 configPath android.WritablePath
Colin Cross43f08db2018-11-12 10:13:39 -080051}
52
53type DexpreoptProperties struct {
54 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010055 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -080056 Enabled *bool
57
58 // If true, generate an app image (.art file) for this module.
59 App_image *bool
60
61 // If true, use a checked-in profile to guide optimization. Defaults to false unless
62 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
63 // that matches the name of this module, in which case it is defaulted to true.
64 Profile_guided *bool
65
66 // If set, provides the path to profile relative to the Android.bp file. If not set,
67 // defaults to searching for a file that matches the name of this module in the default
68 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -070069 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -080070 }
71}
72
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +010073func init() {
74 dexpreopt.DexpreoptRunningInSoong = true
75}
76
Martin Stjernholm6d415272020-01-31 17:10:36 +000077func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000078 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross69f59a32019-02-15 10:39:37 -080079
80 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080081 return true
82 }
83
Colin Cross69f59a32019-02-15 10:39:37 -080084 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080085 return true
86 }
87
Colin Cross43f08db2018-11-12 10:13:39 -080088 if d.isTest {
89 return true
90 }
91
92 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
93 return true
94 }
95
Martin Stjernholm6d415272020-01-31 17:10:36 +000096 if !ctx.Module().(dexpreopterInterface).IsInstallable() {
97 return true
98 }
99
100 if ctx.Host() {
Colin Crossdc2da912019-01-05 22:13:05 -0800101 return true
102 }
103
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800104 // Don't preopt APEX variant module
Colin Cross56a83212020-09-15 18:30:11 -0700105 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800106 return true
107 }
108
Colin Cross43f08db2018-11-12 10:13:39 -0800109 // TODO: contains no java code
110
111 return false
112}
113
Martin Stjernholm6d415272020-01-31 17:10:36 +0000114func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) {
115 if d, ok := ctx.Module().(dexpreopterInterface); !ok || d.dexpreoptDisabled(ctx) {
116 return
117 }
118 dexpreopt.RegisterToolDeps(ctx)
119}
120
Colin Cross70dda7e2019-10-01 22:05:35 -0700121func odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000122 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000123}
124
Paul Duffin612e6102021-02-02 13:38:13 +0000125func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.WritablePath) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000126 // TODO(b/148690468): The check on d.installPath is to bail out in cases where
127 // the dexpreopter struct hasn't been fully initialized before we're called,
128 // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
129 // disabled, even if installable is true.
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000130 if d.installPath.Base() == "." {
131 return
132 }
133
134 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
135
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000136 providesUsesLib := ctx.ModuleName()
137 if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
138 name := ulib.ProvidesUsesLib()
139 if name != nil {
140 providesUsesLib = *name
141 }
142 }
143
Jeongik Chac6246672021-04-08 00:00:19 +0900144 // If it is neither app nor test, make config files regardless of its dexpreopt setting.
145 // The config files are required for apps defined in make which depend on the lib.
146 // TODO(b/158843648): The config for apps should be generated as well regardless of setting.
147 if (d.isApp || d.isTest) && d.dexpreoptDisabled(ctx) {
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800148 return
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000149 }
150
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000151 global := dexpreopt.GetGlobalConfig(ctx)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000152
satayev9a6f87e2021-05-04 16:14:48 +0100153 isSystemServerJar := global.SystemServerJars.ContainsJar(ctx.ModuleName())
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000154
Colin Cross44df5812019-02-15 23:06:46 -0800155 bootImage := defaultBootImageConfig(ctx)
Vladimir Marko40139d62020-02-06 15:14:29 +0000156 if global.UseArtImage {
157 bootImage = artBootImageConfig(ctx)
158 }
Colin Cross43f08db2018-11-12 10:13:39 -0800159
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000160 // System server jars are an exception: they are dexpreopted without updatable bootclasspath.
161 dexFiles, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp && !isSystemServerJar)
162
David Srbeckyc177ebe2020-02-18 20:43:06 +0000163 targets := ctx.MultiTargets()
164 if len(targets) == 0 {
Colin Cross43f08db2018-11-12 10:13:39 -0800165 // assume this is a java library, dexpreopt for all arches for now
166 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100167 if target.NativeBridge == android.NativeBridgeDisabled {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000168 targets = append(targets, target)
dimitry1f33e402019-03-26 12:39:31 +0100169 }
Colin Cross43f08db2018-11-12 10:13:39 -0800170 }
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000171 if isSystemServerJar && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800172 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
David Srbeckyc177ebe2020-02-18 20:43:06 +0000173 targets = targets[:1]
Colin Cross43f08db2018-11-12 10:13:39 -0800174 }
175 }
Colin Cross43f08db2018-11-12 10:13:39 -0800176
David Srbeckyc177ebe2020-02-18 20:43:06 +0000177 var archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800178 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000179 var imagesDeps []android.OutputPaths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000180 for _, target := range targets {
181 archs = append(archs, target.Arch.ArchType)
182 variant := bootImage.getVariant(target)
183 images = append(images, variant.images)
184 imagesDeps = append(imagesDeps, variant.imagesDeps)
Colin Crossc7e40aa2019-02-08 21:37:00 -0800185 }
David Srbeckyab994982020-03-30 17:24:13 +0100186 // The image locations for all Android variants are identical.
187 imageLocations := bootImage.getAnyAndroidVariant().imageLocations()
Colin Crossc7e40aa2019-02-08 21:37:00 -0800188
Colin Cross43f08db2018-11-12 10:13:39 -0800189 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100190 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800191 profileIsTextListing := false
192 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
193 // If dex_preopt.profile_guided is not set, default it based on the existence of the
194 // dexprepot.profile option or the profile class listing.
195 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
196 profileClassListing = android.OptionalPathForPath(
197 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100198 profileBootListing = android.ExistentPathForSource(ctx,
199 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800200 profileIsTextListing = true
Dan Willemsen78d51b02020-06-24 16:33:31 -0700201 } else if global.ProfileDir != "" {
Colin Cross43f08db2018-11-12 10:13:39 -0800202 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800203 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800204 }
205 }
206
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000207 // Full dexpreopt config, used to create dexpreopt build rules.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000208 dexpreoptConfig := &dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800209 Name: ctx.ModuleName(),
210 DexLocation: dexLocation,
Ulya Trafimovichc0f64792021-02-04 10:04:39 +0000211 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
Colin Cross69f59a32019-02-15 10:39:37 -0800212 DexPath: dexJarFile,
Jeongik Cha33a3a812021-04-15 09:12:49 +0900213 ManifestPath: android.OptionalPathForPath(d.manifestFile),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800214 UncompressedDex: d.uncompressedDex,
215 HasApkLibraries: false,
216 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800217
Colin Cross69f59a32019-02-15 10:39:37 -0800218 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800219 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100220 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800221
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000222 EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx),
223 EnforceUsesLibraries: d.enforceUsesLibs,
224 ProvidesUsesLibrary: providesUsesLib,
225 ClassLoaderContexts: d.classLoaderContexts,
Colin Cross43f08db2018-11-12 10:13:39 -0800226
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000227 Archs: archs,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000228 DexPreoptImagesDeps: imagesDeps,
David Srbecky1aacc6c2020-03-26 11:10:45 +0000229 DexPreoptImageLocations: imageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800230
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000231 PreoptBootClassPathDexFiles: dexFiles.Paths(),
Vladimir Marko40139d62020-02-06 15:14:29 +0000232 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800233
Colin Cross43f08db2018-11-12 10:13:39 -0800234 PreoptExtractedApk: false,
235
236 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
237 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
238
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700239 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800240 }
241
Jeongik Chac6246672021-04-08 00:00:19 +0900242 d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config")
243 dexpreopt.WriteModuleConfig(ctx, dexpreoptConfig, d.configPath)
244
245 if d.dexpreoptDisabled(ctx) {
246 return
247 }
248
249 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
250
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000251 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800252 if err != nil {
253 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800254 return
Colin Cross43f08db2018-11-12 10:13:39 -0800255 }
256
Colin Crossf1a035e2020-11-16 17:32:30 -0800257 dexpreoptRule.Build("dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800258
Colin Crossdeabb942019-02-11 14:11:09 -0800259 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800260}