blob: 67738d49b5def0510b72ee1e4c5024de2d666ec1 [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
33 isTest bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -070034 isPresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -080035
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000036 manifestFile android.Path
37 enforceUsesLibs bool
38 classLoaderContexts dexpreopt.ClassLoaderContextMap
Colin Cross50ddcc42019-05-16 12:28:22 -070039
Colin Crossdeabb942019-02-11 14:11:09 -080040 builtInstalled string
Colin Cross43f08db2018-11-12 10:13:39 -080041}
42
43type DexpreoptProperties struct {
44 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010045 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -080046 Enabled *bool
47
48 // If true, generate an app image (.art file) for this module.
49 App_image *bool
50
51 // If true, use a checked-in profile to guide optimization. Defaults to false unless
52 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
53 // that matches the name of this module, in which case it is defaulted to true.
54 Profile_guided *bool
55
56 // If set, provides the path to profile relative to the Android.bp file. If not set,
57 // defaults to searching for a file that matches the name of this module in the default
58 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -070059 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -080060 }
61}
62
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +010063func init() {
64 dexpreopt.DexpreoptRunningInSoong = true
65}
66
Martin Stjernholm6d415272020-01-31 17:10:36 +000067func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000068 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross69f59a32019-02-15 10:39:37 -080069
70 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080071 return true
72 }
73
Colin Cross69f59a32019-02-15 10:39:37 -080074 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080075 return true
76 }
77
Colin Cross43f08db2018-11-12 10:13:39 -080078 if d.isTest {
79 return true
80 }
81
82 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
83 return true
84 }
85
Martin Stjernholm6d415272020-01-31 17:10:36 +000086 if !ctx.Module().(dexpreopterInterface).IsInstallable() {
87 return true
88 }
89
90 if ctx.Host() {
Colin Crossdc2da912019-01-05 22:13:05 -080091 return true
92 }
93
Yo Chiangdbdf8f92020-01-09 19:00:27 +080094 // Don't preopt APEX variant module
Colin Cross56a83212020-09-15 18:30:11 -070095 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Yo Chiangdbdf8f92020-01-09 19:00:27 +080096 return true
97 }
98
Colin Cross43f08db2018-11-12 10:13:39 -080099 // TODO: contains no java code
100
101 return false
102}
103
Martin Stjernholm6d415272020-01-31 17:10:36 +0000104func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) {
105 if d, ok := ctx.Module().(dexpreopterInterface); !ok || d.dexpreoptDisabled(ctx) {
106 return
107 }
108 dexpreopt.RegisterToolDeps(ctx)
109}
110
Colin Cross70dda7e2019-10-01 22:05:35 -0700111func odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000112 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000113}
114
115func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000116 // TODO(b/148690468): The check on d.installPath is to bail out in cases where
117 // the dexpreopter struct hasn't been fully initialized before we're called,
118 // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
119 // disabled, even if installable is true.
120 if d.dexpreoptDisabled(ctx) || d.installPath.Base() == "." {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000121 return dexJarFile
122 }
123
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000124 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000125 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800126 bootImage := defaultBootImageConfig(ctx)
Vladimir Marko40139d62020-02-06 15:14:29 +0000127 dexFiles := bootImage.dexPathsDeps.Paths()
David Srbeckyab994982020-03-30 17:24:13 +0100128 // The dex locations for all Android variants are identical.
129 dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
Vladimir Marko40139d62020-02-06 15:14:29 +0000130 if global.UseArtImage {
131 bootImage = artBootImageConfig(ctx)
132 }
Colin Cross43f08db2018-11-12 10:13:39 -0800133
David Srbeckyc177ebe2020-02-18 20:43:06 +0000134 targets := ctx.MultiTargets()
135 if len(targets) == 0 {
Colin Cross43f08db2018-11-12 10:13:39 -0800136 // assume this is a java library, dexpreopt for all arches for now
137 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100138 if target.NativeBridge == android.NativeBridgeDisabled {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000139 targets = append(targets, target)
dimitry1f33e402019-03-26 12:39:31 +0100140 }
Colin Cross43f08db2018-11-12 10:13:39 -0800141 }
Colin Cross44df5812019-02-15 23:06:46 -0800142 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800143 // 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 +0000144 targets = targets[:1]
Colin Cross43f08db2018-11-12 10:13:39 -0800145 }
146 }
Colin Cross43f08db2018-11-12 10:13:39 -0800147
David Srbeckyc177ebe2020-02-18 20:43:06 +0000148 var archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800149 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000150 var imagesDeps []android.OutputPaths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000151 for _, target := range targets {
152 archs = append(archs, target.Arch.ArchType)
153 variant := bootImage.getVariant(target)
154 images = append(images, variant.images)
155 imagesDeps = append(imagesDeps, variant.imagesDeps)
Colin Crossc7e40aa2019-02-08 21:37:00 -0800156 }
David Srbeckyab994982020-03-30 17:24:13 +0100157 // The image locations for all Android variants are identical.
158 imageLocations := bootImage.getAnyAndroidVariant().imageLocations()
Colin Crossc7e40aa2019-02-08 21:37:00 -0800159
Colin Cross43f08db2018-11-12 10:13:39 -0800160 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
161
Colin Cross43f08db2018-11-12 10:13:39 -0800162 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100163 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800164 profileIsTextListing := false
165 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
166 // If dex_preopt.profile_guided is not set, default it based on the existence of the
167 // dexprepot.profile option or the profile class listing.
168 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
169 profileClassListing = android.OptionalPathForPath(
170 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100171 profileBootListing = android.ExistentPathForSource(ctx,
172 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800173 profileIsTextListing = true
Dan Willemsen78d51b02020-06-24 16:33:31 -0700174 } else if global.ProfileDir != "" {
Colin Cross43f08db2018-11-12 10:13:39 -0800175 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800176 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800177 }
178 }
179
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000180 dexpreoptConfig := &dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800181 Name: ctx.ModuleName(),
182 DexLocation: dexLocation,
Colin Cross69f59a32019-02-15 10:39:37 -0800183 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
184 DexPath: dexJarFile,
Colin Cross50ddcc42019-05-16 12:28:22 -0700185 ManifestPath: d.manifestFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800186 UncompressedDex: d.uncompressedDex,
187 HasApkLibraries: false,
188 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800189
Colin Cross69f59a32019-02-15 10:39:37 -0800190 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800191 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100192 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800193
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000194 EnforceUsesLibraries: d.enforceUsesLibs,
195 ClassLoaderContexts: d.classLoaderContexts,
Colin Cross43f08db2018-11-12 10:13:39 -0800196
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000197 Archs: archs,
198 DexPreoptImages: images,
199 DexPreoptImagesDeps: imagesDeps,
David Srbecky1aacc6c2020-03-26 11:10:45 +0000200 DexPreoptImageLocations: imageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800201
Vladimir Marko40139d62020-02-06 15:14:29 +0000202 PreoptBootClassPathDexFiles: dexFiles,
203 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800204
Colin Cross43f08db2018-11-12 10:13:39 -0800205 PreoptExtractedApk: false,
206
207 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
208 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
209
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700210 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800211 }
212
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000213 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800214 if err != nil {
215 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
216 return dexJarFile
217 }
218
Colin Crossf1a035e2020-11-16 17:32:30 -0800219 dexpreoptRule.Build("dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800220
Colin Crossdeabb942019-02-11 14:11:09 -0800221 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800222
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100223 return dexJarFile
Colin Cross43f08db2018-11-12 10:13:39 -0800224}