blob: ceed9cb8e57ef4d09943a5b1adae6a5b9e25e769 [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 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
Colin Cross571cccf2019-02-04 11:22:08 -080090var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
Colin Cross69f59a32019-02-15 10:39:37 -080091var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
92
93func setDexpreoptGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) {
94 config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfig })
95}
Colin Cross571cccf2019-02-04 11:22:08 -080096
Colin Cross800fe132019-02-11 14:21:24 -080097func dexpreoptGlobalConfig(ctx android.PathContext) dexpreopt.GlobalConfig {
98 return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
Colin Cross43f08db2018-11-12 10:13:39 -080099 if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
100 ctx.AddNinjaFileDeps(f)
Colin Cross69f59a32019-02-15 10:39:37 -0800101 globalConfig, err := dexpreopt.LoadGlobalConfig(ctx, f)
Colin Cross43f08db2018-11-12 10:13:39 -0800102 if err != nil {
103 panic(err)
104 }
105 return globalConfig
106 }
Colin Cross69f59a32019-02-15 10:39:37 -0800107
108 // No global config filename set, see if there is a test config set
109 return ctx.Config().Once(dexpreoptTestGlobalConfigKey, func() interface{} {
110 // Nope, return a config with preopting disabled
111 return dexpreopt.GlobalConfig{
112 DisablePreopt: true,
113 }
114 })
Colin Cross43f08db2018-11-12 10:13:39 -0800115 }).(dexpreopt.GlobalConfig)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000116}
117
118func odexOnSystemOther(ctx android.ModuleContext, installPath android.OutputPath) bool {
Colin Cross800fe132019-02-11 14:21:24 -0800119 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000120}
121
122func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
123 if d.dexpreoptDisabled(ctx) {
124 return dexJarFile
125 }
126
Colin Cross800fe132019-02-11 14:21:24 -0800127 info := dexpreoptBootJarsInfo(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -0800128
Colin Cross74ba9622019-02-11 15:11:14 -0800129 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800130 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800131 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800132 }
133 if len(archs) == 0 {
134 // assume this is a java library, dexpreopt for all arches for now
135 for _, target := range ctx.Config().Targets[android.Android] {
Colin Cross74ba9622019-02-11 15:11:14 -0800136 archs = append(archs, target.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800137 }
Colin Cross800fe132019-02-11 14:21:24 -0800138 if inList(ctx.ModuleName(), info.global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800139 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
140 archs = archs[:1]
141 }
142 }
143 if ctx.Config().SecondArchIsTranslated() {
144 // Only preopt primary arch for translated arch since there is only an image there.
145 archs = archs[:1]
146 }
147
Colin Cross69f59a32019-02-15 10:39:37 -0800148 var images android.Paths
Colin Crossc7e40aa2019-02-08 21:37:00 -0800149 for _, arch := range archs {
Colin Cross69f59a32019-02-15 10:39:37 -0800150 images = append(images, info.images[arch])
Colin Crossc7e40aa2019-02-08 21:37:00 -0800151 }
152
Colin Cross43f08db2018-11-12 10:13:39 -0800153 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
154
155 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
156
Colin Cross43f08db2018-11-12 10:13:39 -0800157 var profileClassListing android.OptionalPath
158 profileIsTextListing := false
159 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
160 // If dex_preopt.profile_guided is not set, default it based on the existence of the
161 // dexprepot.profile option or the profile class listing.
162 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
163 profileClassListing = android.OptionalPathForPath(
164 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
165 profileIsTextListing = true
166 } else {
167 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross69f59a32019-02-15 10:39:37 -0800168 info.global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800169 }
170 }
171
Colin Cross43f08db2018-11-12 10:13:39 -0800172 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800173 Name: ctx.ModuleName(),
174 DexLocation: dexLocation,
Colin Cross69f59a32019-02-15 10:39:37 -0800175 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
176 DexPath: dexJarFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800177 UncompressedDex: d.uncompressedDex,
178 HasApkLibraries: false,
179 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800180
Colin Cross69f59a32019-02-15 10:39:37 -0800181 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800182 ProfileIsTextListing: profileIsTextListing,
183
184 EnforceUsesLibraries: false,
185 OptionalUsesLibraries: nil,
186 UsesLibraries: nil,
187 LibraryPaths: nil,
188
Colin Crossc7e40aa2019-02-08 21:37:00 -0800189 Archs: archs,
190 DexPreoptImages: images,
Colin Cross43f08db2018-11-12 10:13:39 -0800191
Colin Cross69f59a32019-02-15 10:39:37 -0800192 PreoptBootClassPathDexFiles: info.preoptBootDex.Paths(),
Colin Cross800fe132019-02-11 14:21:24 -0800193 PreoptBootClassPathDexLocations: info.preoptBootLocations,
194
Colin Cross43f08db2018-11-12 10:13:39 -0800195 PreoptExtractedApk: false,
196
197 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
198 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
199
Colin Cross8c6d2502019-01-09 21:09:14 -0800200 NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
Colin Cross69f59a32019-02-15 10:39:37 -0800201 StripInputPath: dexJarFile,
202 StripOutputPath: strippedDexJarFile.OutputPath,
Colin Cross43f08db2018-11-12 10:13:39 -0800203 }
204
Colin Cross69f59a32019-02-15 10:39:37 -0800205 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, info.global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800206 if err != nil {
207 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
208 return dexJarFile
209 }
210
Colin Crossfeec25b2019-01-30 17:32:39 -0800211 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800212
Colin Crossdeabb942019-02-11 14:11:09 -0800213 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800214
Colin Cross800fe132019-02-11 14:21:24 -0800215 stripRule, err := dexpreopt.GenerateStripRule(info.global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800216 if err != nil {
217 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
218 return dexJarFile
219 }
220
Colin Crossfeec25b2019-01-30 17:32:39 -0800221 stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip")
Colin Cross43f08db2018-11-12 10:13:39 -0800222
223 return strippedDexJarFile
224}