blob: a7938c82c5bbf48ad6054d122e950ed46a39882b [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
Jaewoong Jungccbb3932019-04-15 09:48:31 -070025 installPath android.OutputPath
26 uncompressedDex bool
27 isSDKLibrary bool
28 isTest bool
29 isInstallable bool
30 isPresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -080031
Colin Crossdeabb942019-02-11 14:11:09 -080032 builtInstalled string
Colin Cross43f08db2018-11-12 10:13:39 -080033}
34
35type DexpreoptProperties struct {
36 Dex_preopt struct {
37 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
38 // true.
39 Enabled *bool
40
Colin Cross8c6d2502019-01-09 21:09:14 -080041 // If true, never strip the dex files from the final jar when dexpreopting. Defaults to false.
42 No_stripping *bool
43
Colin Cross43f08db2018-11-12 10:13:39 -080044 // If true, generate an app image (.art file) for this module.
45 App_image *bool
46
47 // If true, use a checked-in profile to guide optimization. Defaults to false unless
48 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
49 // that matches the name of this module, in which case it is defaulted to true.
50 Profile_guided *bool
51
52 // If set, provides the path to profile relative to the Android.bp file. If not set,
53 // defaults to searching for a file that matches the name of this module in the default
54 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -070055 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -080056 }
57}
58
59func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
Colin Cross69f59a32019-02-15 10:39:37 -080060 global := dexpreoptGlobalConfig(ctx)
61
62 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080063 return true
64 }
65
Colin Cross69f59a32019-02-15 10:39:37 -080066 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080067 return true
68 }
69
70 if ctx.Config().UnbundledBuild() {
71 return true
72 }
73
74 if d.isTest {
75 return true
76 }
77
78 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
79 return true
80 }
81
Colin Crossdc2da912019-01-05 22:13:05 -080082 if !d.isInstallable {
83 return true
84 }
85
Colin Cross43f08db2018-11-12 10:13:39 -080086 // TODO: contains no java code
87
88 return false
89}
90
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000091func odexOnSystemOther(ctx android.ModuleContext, installPath android.OutputPath) bool {
Colin Cross800fe132019-02-11 14:21:24 -080092 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000093}
94
95func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
96 if d.dexpreoptDisabled(ctx) {
97 return dexJarFile
98 }
99
Colin Cross44df5812019-02-15 23:06:46 -0800100 global := dexpreoptGlobalConfig(ctx)
101 bootImage := defaultBootImageConfig(ctx)
Nicolas Geoffray06758a72019-04-08 17:19:15 +0100102 defaultBootImage := bootImage
Nicolas Geoffray25c0e032019-04-04 18:45:20 +0100103 if global.UseApexImage {
104 bootImage = apexBootImageConfig(ctx)
105 }
Colin Cross43f08db2018-11-12 10:13:39 -0800106
Colin Cross74ba9622019-02-11 15:11:14 -0800107 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800108 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800109 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800110 }
111 if len(archs) == 0 {
112 // assume this is a java library, dexpreopt for all arches for now
113 for _, target := range ctx.Config().Targets[android.Android] {
Colin Cross74ba9622019-02-11 15:11:14 -0800114 archs = append(archs, target.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800115 }
Colin Cross44df5812019-02-15 23:06:46 -0800116 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800117 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
118 archs = archs[:1]
119 }
120 }
121 if ctx.Config().SecondArchIsTranslated() {
122 // Only preopt primary arch for translated arch since there is only an image there.
123 archs = archs[:1]
124 }
125
Colin Cross69f59a32019-02-15 10:39:37 -0800126 var images android.Paths
Colin Crossc7e40aa2019-02-08 21:37:00 -0800127 for _, arch := range archs {
Colin Cross44df5812019-02-15 23:06:46 -0800128 images = append(images, bootImage.images[arch])
Colin Crossc7e40aa2019-02-08 21:37:00 -0800129 }
130
Colin Cross43f08db2018-11-12 10:13:39 -0800131 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
132
133 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
134
Colin Cross43f08db2018-11-12 10:13:39 -0800135 var profileClassListing android.OptionalPath
136 profileIsTextListing := false
137 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
138 // If dex_preopt.profile_guided is not set, default it based on the existence of the
139 // dexprepot.profile option or the profile class listing.
140 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
141 profileClassListing = android.OptionalPathForPath(
142 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
143 profileIsTextListing = true
144 } else {
145 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800146 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800147 }
148 }
149
Colin Cross43f08db2018-11-12 10:13:39 -0800150 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800151 Name: ctx.ModuleName(),
152 DexLocation: dexLocation,
Colin Cross69f59a32019-02-15 10:39:37 -0800153 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
154 DexPath: dexJarFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800155 UncompressedDex: d.uncompressedDex,
156 HasApkLibraries: false,
157 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800158
Colin Cross69f59a32019-02-15 10:39:37 -0800159 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800160 ProfileIsTextListing: profileIsTextListing,
161
162 EnforceUsesLibraries: false,
163 OptionalUsesLibraries: nil,
164 UsesLibraries: nil,
165 LibraryPaths: nil,
166
Colin Crossc7e40aa2019-02-08 21:37:00 -0800167 Archs: archs,
168 DexPreoptImages: images,
Colin Cross43f08db2018-11-12 10:13:39 -0800169
Nicolas Geoffray06758a72019-04-08 17:19:15 +0100170 // We use the dex paths and dex locations of the default boot image, as it
171 // contains the full dexpreopt boot classpath. Other images may just contain a subset of
172 // the dexpreopt boot classpath.
173 PreoptBootClassPathDexFiles: defaultBootImage.dexPaths.Paths(),
174 PreoptBootClassPathDexLocations: defaultBootImage.dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800175
Colin Cross43f08db2018-11-12 10:13:39 -0800176 PreoptExtractedApk: false,
177
178 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
179 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
180
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700181 PresignedPrebuilt: d.isPresignedPrebuilt,
182
Colin Cross8c6d2502019-01-09 21:09:14 -0800183 NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
Colin Cross69f59a32019-02-15 10:39:37 -0800184 StripInputPath: dexJarFile,
185 StripOutputPath: strippedDexJarFile.OutputPath,
Colin Cross43f08db2018-11-12 10:13:39 -0800186 }
187
Colin Cross44df5812019-02-15 23:06:46 -0800188 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800189 if err != nil {
190 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
191 return dexJarFile
192 }
193
Colin Crossfeec25b2019-01-30 17:32:39 -0800194 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800195
Colin Crossdeabb942019-02-11 14:11:09 -0800196 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800197
Colin Cross44df5812019-02-15 23:06:46 -0800198 stripRule, err := dexpreopt.GenerateStripRule(global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800199 if err != nil {
200 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
201 return dexJarFile
202 }
203
Colin Crossfeec25b2019-01-30 17:32:39 -0800204 stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip")
Colin Cross43f08db2018-11-12 10:13:39 -0800205
206 return strippedDexJarFile
207}