blob: 28a2c8ae6fee77e37e3b54edb642b6f622115d59 [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
Colin Cross50ddcc42019-05-16 12:28:22 -070036 manifestFile android.Path
37 usesLibs []string
38 optionalUsesLibs []string
39 enforceUsesLibs bool
40 libraryPaths map[string]android.Path
41
Colin Crossdeabb942019-02-11 14:11:09 -080042 builtInstalled string
Colin Cross43f08db2018-11-12 10:13:39 -080043}
44
45type DexpreoptProperties struct {
46 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010047 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -080048 Enabled *bool
49
50 // If true, generate an app image (.art file) for this module.
51 App_image *bool
52
53 // If true, use a checked-in profile to guide optimization. Defaults to false unless
54 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
55 // that matches the name of this module, in which case it is defaulted to true.
56 Profile_guided *bool
57
58 // If set, provides the path to profile relative to the Android.bp file. If not set,
59 // defaults to searching for a file that matches the name of this module in the default
60 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -070061 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -080062 }
63}
64
Ulya Trafimovichde962bd2020-04-24 12:15:20 +010065func init() {
66 dexpreopt.DexpreoptRunningInSoong = true
67}
68
Martin Stjernholm6d415272020-01-31 17:10:36 +000069func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000070 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross69f59a32019-02-15 10:39:37 -080071
72 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080073 return true
74 }
75
Colin Cross69f59a32019-02-15 10:39:37 -080076 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080077 return true
78 }
79
80 if ctx.Config().UnbundledBuild() {
81 return true
82 }
83
84 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
101 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
102 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
121func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
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.
126 if d.dexpreoptDisabled(ctx) || d.installPath.Base() == "." {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000127 return dexJarFile
128 }
129
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000130 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000131 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800132 bootImage := defaultBootImageConfig(ctx)
Vladimir Marko40139d62020-02-06 15:14:29 +0000133 dexFiles := bootImage.dexPathsDeps.Paths()
134 dexLocations := bootImage.dexLocationsDeps
135 if global.UseArtImage {
136 bootImage = artBootImageConfig(ctx)
137 }
Colin Cross43f08db2018-11-12 10:13:39 -0800138
David Srbecky163bda62020-02-18 20:43:06 +0000139 targets := ctx.MultiTargets()
140 if len(targets) == 0 {
Colin Cross43f08db2018-11-12 10:13:39 -0800141 // assume this is a java library, dexpreopt for all arches for now
142 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100143 if target.NativeBridge == android.NativeBridgeDisabled {
David Srbecky163bda62020-02-18 20:43:06 +0000144 targets = append(targets, target)
dimitry1f33e402019-03-26 12:39:31 +0100145 }
Colin Cross43f08db2018-11-12 10:13:39 -0800146 }
Colin Cross44df5812019-02-15 23:06:46 -0800147 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800148 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
David Srbecky163bda62020-02-18 20:43:06 +0000149 targets = targets[:1]
Colin Cross43f08db2018-11-12 10:13:39 -0800150 }
151 }
Colin Cross43f08db2018-11-12 10:13:39 -0800152
David Srbecky163bda62020-02-18 20:43:06 +0000153 var archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800154 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000155 var imagesDeps []android.OutputPaths
David Srbecky163bda62020-02-18 20:43:06 +0000156 for _, target := range targets {
157 archs = append(archs, target.Arch.ArchType)
158 variant := bootImage.getVariant(target)
159 images = append(images, variant.images)
160 imagesDeps = append(imagesDeps, variant.imagesDeps)
Colin Crossc7e40aa2019-02-08 21:37:00 -0800161 }
162
Colin Cross43f08db2018-11-12 10:13:39 -0800163 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
164
Colin Cross43f08db2018-11-12 10:13:39 -0800165 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100166 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800167 profileIsTextListing := false
168 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
169 // If dex_preopt.profile_guided is not set, default it based on the existence of the
170 // dexprepot.profile option or the profile class listing.
171 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
172 profileClassListing = android.OptionalPathForPath(
173 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100174 profileBootListing = android.ExistentPathForSource(ctx,
175 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800176 profileIsTextListing = true
177 } else {
178 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800179 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800180 }
181 }
182
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000183 dexpreoptConfig := &dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800184 Name: ctx.ModuleName(),
185 DexLocation: dexLocation,
Colin Cross69f59a32019-02-15 10:39:37 -0800186 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
187 DexPath: dexJarFile,
Colin Cross50ddcc42019-05-16 12:28:22 -0700188 ManifestPath: d.manifestFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800189 UncompressedDex: d.uncompressedDex,
190 HasApkLibraries: false,
191 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800192
Colin Cross69f59a32019-02-15 10:39:37 -0800193 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800194 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100195 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800196
Colin Cross50ddcc42019-05-16 12:28:22 -0700197 EnforceUsesLibraries: d.enforceUsesLibs,
198 PresentOptionalUsesLibraries: d.optionalUsesLibs,
199 UsesLibraries: d.usesLibs,
200 LibraryPaths: d.libraryPaths,
Colin Cross43f08db2018-11-12 10:13:39 -0800201
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000202 Archs: archs,
203 DexPreoptImages: images,
204 DexPreoptImagesDeps: imagesDeps,
205 DexPreoptImageLocations: bootImage.imageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800206
Vladimir Marko40139d62020-02-06 15:14:29 +0000207 PreoptBootClassPathDexFiles: dexFiles,
208 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800209
Colin Cross43f08db2018-11-12 10:13:39 -0800210 PreoptExtractedApk: false,
211
212 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
213 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
214
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700215 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800216 }
217
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000218 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800219 if err != nil {
220 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
221 return dexJarFile
222 }
223
Colin Crossfeec25b2019-01-30 17:32:39 -0800224 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800225
Colin Crossdeabb942019-02-11 14:11:09 -0800226 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800227
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100228 return dexJarFile
Colin Cross43f08db2018-11-12 10:13:39 -0800229}