blob: b502d07c5f9e1b23e1ed60e2a3d7031f9f62f2d2 [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 Cross2fc72f62018-12-21 12:59:54 -080025 installPath android.OutputPath
26 uncompressedDex bool
27 isSDKLibrary bool
28 isTest bool
29 isInstallable bool
Colin Cross43f08db2018-11-12 10:13:39 -080030
Colin Crossdeabb942019-02-11 14:11:09 -080031 builtInstalled string
Colin Cross43f08db2018-11-12 10:13:39 -080032}
33
34type DexpreoptProperties struct {
35 Dex_preopt struct {
36 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
37 // true.
38 Enabled *bool
39
Colin Cross8c6d2502019-01-09 21:09:14 -080040 // If true, never strip the dex files from the final jar when dexpreopting. Defaults to false.
41 No_stripping *bool
42
Colin Cross43f08db2018-11-12 10:13:39 -080043 // If true, generate an app image (.art file) for this module.
44 App_image *bool
45
46 // If true, use a checked-in profile to guide optimization. Defaults to false unless
47 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
48 // that matches the name of this module, in which case it is defaulted to true.
49 Profile_guided *bool
50
51 // If set, provides the path to profile relative to the Android.bp file. If not set,
52 // defaults to searching for a file that matches the name of this module in the default
53 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -070054 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -080055 }
56}
57
58func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
Colin Cross69f59a32019-02-15 10:39:37 -080059 global := dexpreoptGlobalConfig(ctx)
60
61 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080062 return true
63 }
64
Colin Cross69f59a32019-02-15 10:39:37 -080065 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080066 return true
67 }
68
69 if ctx.Config().UnbundledBuild() {
70 return true
71 }
72
73 if d.isTest {
74 return true
75 }
76
77 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
78 return true
79 }
80
Colin Crossdc2da912019-01-05 22:13:05 -080081 if !d.isInstallable {
82 return true
83 }
84
Colin Cross43f08db2018-11-12 10:13:39 -080085 // TODO: contains no java code
86
87 return false
88}
89
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000090func odexOnSystemOther(ctx android.ModuleContext, installPath android.OutputPath) bool {
Colin Cross800fe132019-02-11 14:21:24 -080091 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000092}
93
94func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
95 if d.dexpreoptDisabled(ctx) {
96 return dexJarFile
97 }
98
Colin Cross44df5812019-02-15 23:06:46 -080099 global := dexpreoptGlobalConfig(ctx)
100 bootImage := defaultBootImageConfig(ctx)
Nicolas Geoffray06758a72019-04-08 17:19:15 +0100101 defaultBootImage := bootImage
Nicolas Geoffray25c0e032019-04-04 18:45:20 +0100102 if global.UseApexImage {
103 bootImage = apexBootImageConfig(ctx)
104 }
Colin Cross43f08db2018-11-12 10:13:39 -0800105
Colin Cross74ba9622019-02-11 15:11:14 -0800106 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800107 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800108 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800109 }
110 if len(archs) == 0 {
111 // assume this is a java library, dexpreopt for all arches for now
112 for _, target := range ctx.Config().Targets[android.Android] {
Colin Cross74ba9622019-02-11 15:11:14 -0800113 archs = append(archs, target.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800114 }
Colin Cross44df5812019-02-15 23:06:46 -0800115 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800116 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
117 archs = archs[:1]
118 }
119 }
120 if ctx.Config().SecondArchIsTranslated() {
121 // Only preopt primary arch for translated arch since there is only an image there.
122 archs = archs[:1]
123 }
124
Colin Cross69f59a32019-02-15 10:39:37 -0800125 var images android.Paths
Colin Crossc7e40aa2019-02-08 21:37:00 -0800126 for _, arch := range archs {
Colin Cross44df5812019-02-15 23:06:46 -0800127 images = append(images, bootImage.images[arch])
Colin Crossc7e40aa2019-02-08 21:37:00 -0800128 }
129
Colin Cross43f08db2018-11-12 10:13:39 -0800130 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
131
132 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
133
Colin Cross43f08db2018-11-12 10:13:39 -0800134 var profileClassListing android.OptionalPath
135 profileIsTextListing := false
136 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
137 // If dex_preopt.profile_guided is not set, default it based on the existence of the
138 // dexprepot.profile option or the profile class listing.
139 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
140 profileClassListing = android.OptionalPathForPath(
141 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
142 profileIsTextListing = true
143 } else {
144 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800145 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800146 }
147 }
148
Colin Cross43f08db2018-11-12 10:13:39 -0800149 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800150 Name: ctx.ModuleName(),
151 DexLocation: dexLocation,
Colin Cross69f59a32019-02-15 10:39:37 -0800152 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
153 DexPath: dexJarFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800154 UncompressedDex: d.uncompressedDex,
155 HasApkLibraries: false,
156 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800157
Colin Cross69f59a32019-02-15 10:39:37 -0800158 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800159 ProfileIsTextListing: profileIsTextListing,
160
161 EnforceUsesLibraries: false,
162 OptionalUsesLibraries: nil,
163 UsesLibraries: nil,
164 LibraryPaths: nil,
165
Colin Crossc7e40aa2019-02-08 21:37:00 -0800166 Archs: archs,
167 DexPreoptImages: images,
Colin Cross43f08db2018-11-12 10:13:39 -0800168
Nicolas Geoffray06758a72019-04-08 17:19:15 +0100169 // We use the dex paths and dex locations of the default boot image, as it
170 // contains the full dexpreopt boot classpath. Other images may just contain a subset of
171 // the dexpreopt boot classpath.
172 PreoptBootClassPathDexFiles: defaultBootImage.dexPaths.Paths(),
173 PreoptBootClassPathDexLocations: defaultBootImage.dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800174
Colin Cross43f08db2018-11-12 10:13:39 -0800175 PreoptExtractedApk: false,
176
177 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
178 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
179
Colin Cross8c6d2502019-01-09 21:09:14 -0800180 NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
Colin Cross69f59a32019-02-15 10:39:37 -0800181 StripInputPath: dexJarFile,
182 StripOutputPath: strippedDexJarFile.OutputPath,
Colin Cross43f08db2018-11-12 10:13:39 -0800183 }
184
Colin Cross44df5812019-02-15 23:06:46 -0800185 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800186 if err != nil {
187 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
188 return dexJarFile
189 }
190
Colin Crossfeec25b2019-01-30 17:32:39 -0800191 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800192
Colin Crossdeabb942019-02-11 14:11:09 -0800193 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800194
Colin Cross44df5812019-02-15 23:06:46 -0800195 stripRule, err := dexpreopt.GenerateStripRule(global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800196 if err != nil {
197 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
198 return dexJarFile
199 }
200
Colin Crossfeec25b2019-01-30 17:32:39 -0800201 stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip")
Colin Cross43f08db2018-11-12 10:13:39 -0800202
203 return strippedDexJarFile
204}