blob: 29c73c11f41909605f41429a856e35fdf6ecd7cc [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
38 enforceUsesLibs bool
39 classLoaderContexts dexpreopt.ClassLoaderContextMap
Colin Cross50ddcc42019-05-16 12:28:22 -070040
Colin Crossdeabb942019-02-11 14:11:09 -080041 builtInstalled string
Ulya Trafimovich76b08522021-01-14 17:52:43 +000042
43 // A path to a dexpreopt.config file generated by Soong for libraries that may be used as a
44 // <uses-library> by Make modules. The path is passed to Make via LOCAL_SOONG_DEXPREOPT_CONFIG
45 // variable. If the path is nil, no config is generated (which is the case for apps and tests).
46 configPath android.WritablePath
Colin Cross43f08db2018-11-12 10:13:39 -080047}
48
49type DexpreoptProperties struct {
50 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010051 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -080052 Enabled *bool
53
54 // If true, generate an app image (.art file) for this module.
55 App_image *bool
56
57 // If true, use a checked-in profile to guide optimization. Defaults to false unless
58 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
59 // that matches the name of this module, in which case it is defaulted to true.
60 Profile_guided *bool
61
62 // If set, provides the path to profile relative to the Android.bp file. If not set,
63 // defaults to searching for a file that matches the name of this module in the default
64 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -070065 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -080066 }
67}
68
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +010069func init() {
70 dexpreopt.DexpreoptRunningInSoong = true
71}
72
Martin Stjernholm6d415272020-01-31 17:10:36 +000073func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000074 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross69f59a32019-02-15 10:39:37 -080075
76 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080077 return true
78 }
79
Colin Cross69f59a32019-02-15 10:39:37 -080080 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080081 return true
82 }
83
Colin Cross43f08db2018-11-12 10:13:39 -080084 if d.isTest {
85 return true
86 }
87
88 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
89 return true
90 }
91
Martin Stjernholm6d415272020-01-31 17:10:36 +000092 if !ctx.Module().(dexpreopterInterface).IsInstallable() {
93 return true
94 }
95
96 if ctx.Host() {
Colin Crossdc2da912019-01-05 22:13:05 -080097 return true
98 }
99
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800100 // Don't preopt APEX variant module
Colin Cross56a83212020-09-15 18:30:11 -0700101 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800102 return true
103 }
104
Colin Cross43f08db2018-11-12 10:13:39 -0800105 // TODO: contains no java code
106
107 return false
108}
109
Martin Stjernholm6d415272020-01-31 17:10:36 +0000110func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) {
111 if d, ok := ctx.Module().(dexpreopterInterface); !ok || d.dexpreoptDisabled(ctx) {
112 return
113 }
114 dexpreopt.RegisterToolDeps(ctx)
115}
116
Colin Cross70dda7e2019-10-01 22:05:35 -0700117func odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000118 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000119}
120
Paul Duffin612e6102021-02-02 13:38:13 +0000121func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.WritablePath) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000122 // TODO(b/148690468): The check on d.installPath is to bail out in cases where
123 // the dexpreopter struct hasn't been fully initialized before we're called,
124 // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
125 // disabled, even if installable is true.
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000126 if d.installPath.Base() == "." {
127 return
128 }
129
130 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
131
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000132 providesUsesLib := ctx.ModuleName()
133 if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
134 name := ulib.ProvidesUsesLib()
135 if name != nil {
136 providesUsesLib = *name
137 }
138 }
139
140 if !d.isApp && !d.isTest {
141 // Slim dexpreopt config is serialized to dexpreopt.config files and used by
142 // dex_preopt_config_merger.py to get information about <uses-library> dependencies.
143 // Note that it might be needed even if dexpreopt is disabled for this module.
144 slimDexpreoptConfig := &dexpreopt.ModuleConfig{
145 Name: ctx.ModuleName(),
146 DexLocation: dexLocation,
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000147 EnforceUsesLibraries: d.enforceUsesLibs,
148 ProvidesUsesLibrary: providesUsesLib,
149 ClassLoaderContexts: d.classLoaderContexts,
150 // The rest of the fields are not needed.
151 }
152 d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config")
153 dexpreopt.WriteSlimModuleConfigForMake(ctx, slimDexpreoptConfig, d.configPath)
154 }
155
156 if d.dexpreoptDisabled(ctx) {
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800157 return
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000158 }
159
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000160 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000161 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800162 bootImage := defaultBootImageConfig(ctx)
Vladimir Marko40139d62020-02-06 15:14:29 +0000163 dexFiles := bootImage.dexPathsDeps.Paths()
David Srbeckyab994982020-03-30 17:24:13 +0100164 // The dex locations for all Android variants are identical.
165 dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
Vladimir Marko40139d62020-02-06 15:14:29 +0000166 if global.UseArtImage {
167 bootImage = artBootImageConfig(ctx)
168 }
Colin Cross43f08db2018-11-12 10:13:39 -0800169
David Srbeckyc177ebe2020-02-18 20:43:06 +0000170 targets := ctx.MultiTargets()
171 if len(targets) == 0 {
Colin Cross43f08db2018-11-12 10:13:39 -0800172 // assume this is a java library, dexpreopt for all arches for now
173 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100174 if target.NativeBridge == android.NativeBridgeDisabled {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000175 targets = append(targets, target)
dimitry1f33e402019-03-26 12:39:31 +0100176 }
Colin Cross43f08db2018-11-12 10:13:39 -0800177 }
Colin Cross44df5812019-02-15 23:06:46 -0800178 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800179 // 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 +0000180 targets = targets[:1]
Colin Cross43f08db2018-11-12 10:13:39 -0800181 }
182 }
Colin Cross43f08db2018-11-12 10:13:39 -0800183
David Srbeckyc177ebe2020-02-18 20:43:06 +0000184 var archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800185 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000186 var imagesDeps []android.OutputPaths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000187 for _, target := range targets {
188 archs = append(archs, target.Arch.ArchType)
189 variant := bootImage.getVariant(target)
190 images = append(images, variant.images)
191 imagesDeps = append(imagesDeps, variant.imagesDeps)
Colin Crossc7e40aa2019-02-08 21:37:00 -0800192 }
David Srbeckyab994982020-03-30 17:24:13 +0100193 // The image locations for all Android variants are identical.
194 imageLocations := bootImage.getAnyAndroidVariant().imageLocations()
Colin Crossc7e40aa2019-02-08 21:37:00 -0800195
Colin Cross43f08db2018-11-12 10:13:39 -0800196 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100197 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800198 profileIsTextListing := false
199 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
200 // If dex_preopt.profile_guided is not set, default it based on the existence of the
201 // dexprepot.profile option or the profile class listing.
202 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
203 profileClassListing = android.OptionalPathForPath(
204 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100205 profileBootListing = android.ExistentPathForSource(ctx,
206 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800207 profileIsTextListing = true
Dan Willemsen78d51b02020-06-24 16:33:31 -0700208 } else if global.ProfileDir != "" {
Colin Cross43f08db2018-11-12 10:13:39 -0800209 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800210 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800211 }
212 }
213
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000214 // Full dexpreopt config, used to create dexpreopt build rules.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000215 dexpreoptConfig := &dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800216 Name: ctx.ModuleName(),
217 DexLocation: dexLocation,
Ulya Trafimovichc0f64792021-02-04 10:04:39 +0000218 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
Colin Cross69f59a32019-02-15 10:39:37 -0800219 DexPath: dexJarFile,
Colin Cross50ddcc42019-05-16 12:28:22 -0700220 ManifestPath: d.manifestFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800221 UncompressedDex: d.uncompressedDex,
222 HasApkLibraries: false,
223 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800224
Colin Cross69f59a32019-02-15 10:39:37 -0800225 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800226 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100227 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800228
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000229 EnforceUsesLibraries: d.enforceUsesLibs,
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000230 ProvidesUsesLibrary: providesUsesLib,
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000231 ClassLoaderContexts: d.classLoaderContexts,
Colin Cross43f08db2018-11-12 10:13:39 -0800232
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000233 Archs: archs,
234 DexPreoptImages: images,
235 DexPreoptImagesDeps: imagesDeps,
David Srbecky1aacc6c2020-03-26 11:10:45 +0000236 DexPreoptImageLocations: imageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800237
Vladimir Marko40139d62020-02-06 15:14:29 +0000238 PreoptBootClassPathDexFiles: dexFiles,
239 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800240
Colin Cross43f08db2018-11-12 10:13:39 -0800241 PreoptExtractedApk: false,
242
243 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
244 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
245
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700246 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800247 }
248
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000249 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800250 if err != nil {
251 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800252 return
Colin Cross43f08db2018-11-12 10:13:39 -0800253 }
254
Colin Crossf1a035e2020-11-16 17:32:30 -0800255 dexpreoptRule.Build("dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800256
Colin Crossdeabb942019-02-11 14:11:09 -0800257 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800258}