blob: ec0b5c6599c7e9d23d498448dd336856c027648b [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
22type dexpreopter struct {
23 dexpreoptProperties DexpreoptProperties
24
Colin Cross70dda7e2019-10-01 22:05:35 -070025 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -070026 uncompressedDex bool
27 isSDKLibrary bool
28 isTest bool
29 isInstallable bool
30 isPresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -080031
Colin Cross50ddcc42019-05-16 12:28:22 -070032 manifestFile android.Path
33 usesLibs []string
34 optionalUsesLibs []string
35 enforceUsesLibs bool
36 libraryPaths map[string]android.Path
37
Colin Crossdeabb942019-02-11 14:11:09 -080038 builtInstalled string
Colin Cross43f08db2018-11-12 10:13:39 -080039}
40
41type DexpreoptProperties struct {
42 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010043 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -080044 Enabled *bool
45
46 // If true, generate an app image (.art file) for this module.
47 App_image *bool
48
49 // If true, use a checked-in profile to guide optimization. Defaults to false unless
50 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
51 // that matches the name of this module, in which case it is defaulted to true.
52 Profile_guided *bool
53
54 // If set, provides the path to profile relative to the Android.bp file. If not set,
55 // defaults to searching for a file that matches the name of this module in the default
56 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -070057 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -080058 }
59}
60
61func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
Hans Boehm453bf092020-01-25 01:44:30 +000062 global := dexpreoptGlobalConfig(ctx)
Colin Cross69f59a32019-02-15 10:39:37 -080063
64 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080065 return true
66 }
67
Colin Cross69f59a32019-02-15 10:39:37 -080068 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080069 return true
70 }
71
72 if ctx.Config().UnbundledBuild() {
73 return true
74 }
75
76 if d.isTest {
77 return true
78 }
79
80 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
81 return true
82 }
83
Colin Crossdc2da912019-01-05 22:13:05 -080084 if !d.isInstallable {
85 return true
86 }
87
Yo Chiangdbdf8f92020-01-09 19:00:27 +080088 // Don't preopt APEX variant module
89 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
90 return true
91 }
92
Colin Cross43f08db2018-11-12 10:13:39 -080093 // TODO: contains no java code
94
95 return false
96}
97
Colin Cross70dda7e2019-10-01 22:05:35 -070098func odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool {
Hans Boehm453bf092020-01-25 01:44:30 +000099 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000100}
101
102func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
103 if d.dexpreoptDisabled(ctx) {
104 return dexJarFile
105 }
106
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000107 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
Hans Boehm453bf092020-01-25 01:44:30 +0000108 global := dexpreoptGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800109 bootImage := defaultBootImageConfig(ctx)
Vladimir Marko40139d62020-02-06 15:14:29 +0000110 dexFiles := bootImage.dexPathsDeps.Paths()
111 dexLocations := bootImage.dexLocationsDeps
112 if global.UseArtImage {
113 bootImage = artBootImageConfig(ctx)
114 }
Colin Cross43f08db2018-11-12 10:13:39 -0800115
Colin Cross74ba9622019-02-11 15:11:14 -0800116 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800117 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800118 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800119 }
120 if len(archs) == 0 {
121 // assume this is a java library, dexpreopt for all arches for now
122 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100123 if target.NativeBridge == android.NativeBridgeDisabled {
124 archs = append(archs, target.Arch.ArchType)
125 }
Colin Cross43f08db2018-11-12 10:13:39 -0800126 }
Colin Cross44df5812019-02-15 23:06:46 -0800127 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800128 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
129 archs = archs[:1]
130 }
131 }
Colin Cross43f08db2018-11-12 10:13:39 -0800132
Colin Cross69f59a32019-02-15 10:39:37 -0800133 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000134 var imagesDeps []android.OutputPaths
Colin Crossc7e40aa2019-02-08 21:37:00 -0800135 for _, arch := range archs {
Colin Cross44df5812019-02-15 23:06:46 -0800136 images = append(images, bootImage.images[arch])
Dan Willemsen0f416782019-06-13 21:44:53 +0000137 imagesDeps = append(imagesDeps, bootImage.imagesDeps[arch])
Colin Crossc7e40aa2019-02-08 21:37:00 -0800138 }
139
Colin Cross43f08db2018-11-12 10:13:39 -0800140 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
141
Colin Cross43f08db2018-11-12 10:13:39 -0800142 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100143 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800144 profileIsTextListing := false
145 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
146 // If dex_preopt.profile_guided is not set, default it based on the existence of the
147 // dexprepot.profile option or the profile class listing.
148 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
149 profileClassListing = android.OptionalPathForPath(
150 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100151 profileBootListing = android.ExistentPathForSource(ctx,
152 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800153 profileIsTextListing = true
154 } else {
155 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800156 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800157 }
158 }
159
Colin Cross43f08db2018-11-12 10:13:39 -0800160 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800161 Name: ctx.ModuleName(),
162 DexLocation: dexLocation,
Colin Cross69f59a32019-02-15 10:39:37 -0800163 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
164 DexPath: dexJarFile,
Colin Cross50ddcc42019-05-16 12:28:22 -0700165 ManifestPath: d.manifestFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800166 UncompressedDex: d.uncompressedDex,
167 HasApkLibraries: false,
168 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800169
Colin Cross69f59a32019-02-15 10:39:37 -0800170 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800171 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100172 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800173
Colin Cross50ddcc42019-05-16 12:28:22 -0700174 EnforceUsesLibraries: d.enforceUsesLibs,
175 PresentOptionalUsesLibraries: d.optionalUsesLibs,
176 UsesLibraries: d.usesLibs,
177 LibraryPaths: d.libraryPaths,
Colin Cross43f08db2018-11-12 10:13:39 -0800178
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000179 Archs: archs,
180 DexPreoptImages: images,
181 DexPreoptImagesDeps: imagesDeps,
182 DexPreoptImageLocations: bootImage.imageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800183
Vladimir Marko40139d62020-02-06 15:14:29 +0000184 PreoptBootClassPathDexFiles: dexFiles,
185 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800186
Colin Cross43f08db2018-11-12 10:13:39 -0800187 PreoptExtractedApk: false,
188
189 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
190 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
191
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700192 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800193 }
194
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000195 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800196 if err != nil {
197 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
198 return dexJarFile
199 }
200
Colin Crossfeec25b2019-01-30 17:32:39 -0800201 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800202
Colin Crossdeabb942019-02-11 14:11:09 -0800203 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800204
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100205 return dexJarFile
Colin Cross43f08db2018-11-12 10:13:39 -0800206}