blob: 5faec08174707ffc467f1c770ecd215d6be7a091 [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
Hans Boehm453bf092020-01-25 01:44:30 +0000107 global := dexpreoptGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800108 bootImage := defaultBootImageConfig(ctx)
Vladimir Marko40139d62020-02-06 15:14:29 +0000109 dexFiles := bootImage.dexPathsDeps.Paths()
110 dexLocations := bootImage.dexLocationsDeps
111 if global.UseArtImage {
112 bootImage = artBootImageConfig(ctx)
113 }
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000114 if global.UseApexImage {
Ulya Trafimovich57547452019-12-09 15:40:17 +0000115 bootImage = frameworkJZBootImageConfig(ctx)
Vladimir Marko40139d62020-02-06 15:14:29 +0000116 dexFiles = bootImage.dexPathsDeps.Paths()
117 dexLocations = bootImage.dexLocationsDeps
118 if global.UseArtImage {
119 bootImage = artJZBootImageConfig(ctx)
120 }
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000121 }
Colin Cross43f08db2018-11-12 10:13:39 -0800122
Colin Cross74ba9622019-02-11 15:11:14 -0800123 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800124 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800125 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800126 }
127 if len(archs) == 0 {
128 // assume this is a java library, dexpreopt for all arches for now
129 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100130 if target.NativeBridge == android.NativeBridgeDisabled {
131 archs = append(archs, target.Arch.ArchType)
132 }
Colin Cross43f08db2018-11-12 10:13:39 -0800133 }
Colin Cross44df5812019-02-15 23:06:46 -0800134 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800135 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
136 archs = archs[:1]
137 }
138 }
Colin Cross43f08db2018-11-12 10:13:39 -0800139
Colin Cross69f59a32019-02-15 10:39:37 -0800140 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000141 var imagesDeps []android.OutputPaths
Colin Crossc7e40aa2019-02-08 21:37:00 -0800142 for _, arch := range archs {
Colin Cross44df5812019-02-15 23:06:46 -0800143 images = append(images, bootImage.images[arch])
Dan Willemsen0f416782019-06-13 21:44:53 +0000144 imagesDeps = append(imagesDeps, bootImage.imagesDeps[arch])
Colin Crossc7e40aa2019-02-08 21:37:00 -0800145 }
146
Colin Cross43f08db2018-11-12 10:13:39 -0800147 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
148
Colin Cross43f08db2018-11-12 10:13:39 -0800149 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100150 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800151 profileIsTextListing := false
152 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
153 // If dex_preopt.profile_guided is not set, default it based on the existence of the
154 // dexprepot.profile option or the profile class listing.
155 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
156 profileClassListing = android.OptionalPathForPath(
157 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100158 profileBootListing = android.ExistentPathForSource(ctx,
159 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800160 profileIsTextListing = true
161 } else {
162 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800163 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800164 }
165 }
166
Colin Cross43f08db2018-11-12 10:13:39 -0800167 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800168 Name: ctx.ModuleName(),
169 DexLocation: dexLocation,
Colin Cross69f59a32019-02-15 10:39:37 -0800170 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
171 DexPath: dexJarFile,
Colin Cross50ddcc42019-05-16 12:28:22 -0700172 ManifestPath: d.manifestFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800173 UncompressedDex: d.uncompressedDex,
174 HasApkLibraries: false,
175 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800176
Colin Cross69f59a32019-02-15 10:39:37 -0800177 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800178 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100179 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800180
Colin Cross50ddcc42019-05-16 12:28:22 -0700181 EnforceUsesLibraries: d.enforceUsesLibs,
182 PresentOptionalUsesLibraries: d.optionalUsesLibs,
183 UsesLibraries: d.usesLibs,
184 LibraryPaths: d.libraryPaths,
Colin Cross43f08db2018-11-12 10:13:39 -0800185
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000186 Archs: archs,
187 DexPreoptImages: images,
188 DexPreoptImagesDeps: imagesDeps,
189 DexPreoptImageLocations: bootImage.imageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800190
Vladimir Marko40139d62020-02-06 15:14:29 +0000191 PreoptBootClassPathDexFiles: dexFiles,
192 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800193
Colin Cross43f08db2018-11-12 10:13:39 -0800194 PreoptExtractedApk: false,
195
196 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
197 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
198
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700199 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800200 }
201
Hans Boehme4b53422020-01-25 01:44:30 +0000202 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800203 if err != nil {
204 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
205 return dexJarFile
206 }
207
Colin Crossfeec25b2019-01-30 17:32:39 -0800208 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800209
Colin Crossdeabb942019-02-11 14:11:09 -0800210 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800211
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100212 return dexJarFile
Colin Cross43f08db2018-11-12 10:13:39 -0800213}