blob: 0a56529772c20195dd56afc5ffaadf78466454f5 [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.
54 Profile *string
55 }
56}
57
58func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
Colin Cross800fe132019-02-11 14:21:24 -080059 if ctx.Config().DisableDexPreopt() {
60 return true
61 }
62
63 if ctx.Config().DisableDexPreoptForModule(ctx.ModuleName()) {
Colin Cross43f08db2018-11-12 10:13:39 -080064 return true
65 }
66
67 if ctx.Config().UnbundledBuild() {
68 return true
69 }
70
71 if d.isTest {
72 return true
73 }
74
75 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
76 return true
77 }
78
Colin Crossdc2da912019-01-05 22:13:05 -080079 if !d.isInstallable {
80 return true
81 }
82
Colin Cross43f08db2018-11-12 10:13:39 -080083 // TODO: contains no java code
84
85 return false
86}
87
Colin Cross571cccf2019-02-04 11:22:08 -080088var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
89
Colin Cross800fe132019-02-11 14:21:24 -080090func dexpreoptGlobalConfig(ctx android.PathContext) dexpreopt.GlobalConfig {
91 return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
Colin Cross43f08db2018-11-12 10:13:39 -080092 if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
93 ctx.AddNinjaFileDeps(f)
Colin Crossab898dc2019-02-21 05:03:00 +000094 globalConfig, err := dexpreopt.LoadGlobalConfig(f)
Colin Cross43f08db2018-11-12 10:13:39 -080095 if err != nil {
96 panic(err)
97 }
98 return globalConfig
99 }
Colin Crossab898dc2019-02-21 05:03:00 +0000100 return dexpreopt.GlobalConfig{}
Colin Cross43f08db2018-11-12 10:13:39 -0800101 }).(dexpreopt.GlobalConfig)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000102}
103
104func odexOnSystemOther(ctx android.ModuleContext, installPath android.OutputPath) bool {
Colin Cross800fe132019-02-11 14:21:24 -0800105 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000106}
107
108func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
109 if d.dexpreoptDisabled(ctx) {
110 return dexJarFile
111 }
112
Colin Cross800fe132019-02-11 14:21:24 -0800113 info := dexpreoptBootJarsInfo(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -0800114
Colin Cross74ba9622019-02-11 15:11:14 -0800115 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800116 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800117 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800118 }
119 if len(archs) == 0 {
120 // assume this is a java library, dexpreopt for all arches for now
121 for _, target := range ctx.Config().Targets[android.Android] {
Colin Cross74ba9622019-02-11 15:11:14 -0800122 archs = append(archs, target.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800123 }
Colin Cross800fe132019-02-11 14:21:24 -0800124 if inList(ctx.ModuleName(), info.global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800125 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
126 archs = archs[:1]
127 }
128 }
129 if ctx.Config().SecondArchIsTranslated() {
130 // Only preopt primary arch for translated arch since there is only an image there.
131 archs = archs[:1]
132 }
133
Colin Crossab898dc2019-02-21 05:03:00 +0000134 var images []string
Colin Crossc7e40aa2019-02-08 21:37:00 -0800135 for _, arch := range archs {
Colin Crossab898dc2019-02-21 05:03:00 +0000136 images = append(images, info.images[arch].String())
Colin Crossc7e40aa2019-02-08 21:37:00 -0800137 }
138
Colin Cross43f08db2018-11-12 10:13:39 -0800139 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
140
141 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
142
Colin Crossab898dc2019-02-21 05:03:00 +0000143 deps := android.Paths{dexJarFile}
144
Colin Cross43f08db2018-11-12 10:13:39 -0800145 var profileClassListing android.OptionalPath
146 profileIsTextListing := false
147 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
148 // If dex_preopt.profile_guided is not set, default it based on the existence of the
149 // dexprepot.profile option or the profile class listing.
150 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
151 profileClassListing = android.OptionalPathForPath(
152 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
153 profileIsTextListing = true
154 } else {
155 profileClassListing = android.ExistentPathForSource(ctx,
156 ctx.Config().DexPreoptProfileDir(), ctx.ModuleName()+".prof")
157 }
158 }
159
Colin Crossab898dc2019-02-21 05:03:00 +0000160 if profileClassListing.Valid() {
161 deps = append(deps, profileClassListing.Path())
162 }
163
Colin Cross43f08db2018-11-12 10:13:39 -0800164 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800165 Name: ctx.ModuleName(),
166 DexLocation: dexLocation,
Colin Crossab898dc2019-02-21 05:03:00 +0000167 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
168 DexPath: dexJarFile.String(),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800169 UncompressedDex: d.uncompressedDex,
170 HasApkLibraries: false,
171 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800172
Colin Crossab898dc2019-02-21 05:03:00 +0000173 ProfileClassListing: profileClassListing.String(),
Colin Cross43f08db2018-11-12 10:13:39 -0800174 ProfileIsTextListing: profileIsTextListing,
175
176 EnforceUsesLibraries: false,
177 OptionalUsesLibraries: nil,
178 UsesLibraries: nil,
179 LibraryPaths: nil,
180
Colin Crossc7e40aa2019-02-08 21:37:00 -0800181 Archs: archs,
182 DexPreoptImages: images,
Colin Cross43f08db2018-11-12 10:13:39 -0800183
Colin Crossab898dc2019-02-21 05:03:00 +0000184 PreoptBootClassPathDexFiles: info.preoptBootDex.Strings(),
Colin Cross800fe132019-02-11 14:21:24 -0800185 PreoptBootClassPathDexLocations: info.preoptBootLocations,
186
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
Colin Cross8c6d2502019-01-09 21:09:14 -0800192 NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
Colin Crossab898dc2019-02-21 05:03:00 +0000193 StripInputPath: dexJarFile.String(),
194 StripOutputPath: strippedDexJarFile.String(),
Colin Cross43f08db2018-11-12 10:13:39 -0800195 }
196
Colin Crossab898dc2019-02-21 05:03:00 +0000197 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(info.global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800198 if err != nil {
199 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
200 return dexJarFile
201 }
202
Colin Crossfeec25b2019-01-30 17:32:39 -0800203 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800204
Colin Crossdeabb942019-02-11 14:11:09 -0800205 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800206
Colin Cross800fe132019-02-11 14:21:24 -0800207 stripRule, err := dexpreopt.GenerateStripRule(info.global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800208 if err != nil {
209 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
210 return dexJarFile
211 }
212
Colin Crossfeec25b2019-01-30 17:32:39 -0800213 stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip")
Colin Cross43f08db2018-11-12 10:13:39 -0800214
215 return strippedDexJarFile
216}