blob: cb6427b8dc5d368413b29477f1d53d2db2311332 [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
31 builtInstalled []string
32}
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 {
59 if ctx.Config().DisableDexPreopt(ctx.ModuleName()) {
60 return true
61 }
62
63 if ctx.Config().UnbundledBuild() {
64 return true
65 }
66
67 if d.isTest {
68 return true
69 }
70
71 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
72 return true
73 }
74
Colin Crossdc2da912019-01-05 22:13:05 -080075 if !d.isInstallable {
76 return true
77 }
78
Colin Cross43f08db2018-11-12 10:13:39 -080079 // TODO: contains no java code
80
81 return false
82}
83
Colin Cross571cccf2019-02-04 11:22:08 -080084var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
85
Colin Cross43f08db2018-11-12 10:13:39 -080086func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
87 if d.dexpreoptDisabled(ctx) {
88 return dexJarFile
89 }
90
Colin Cross571cccf2019-02-04 11:22:08 -080091 globalConfig := ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
Colin Cross43f08db2018-11-12 10:13:39 -080092 if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
93 ctx.AddNinjaFileDeps(f)
94 globalConfig, err := dexpreopt.LoadGlobalConfig(f)
95 if err != nil {
96 panic(err)
97 }
98 return globalConfig
99 }
100 return dexpreopt.GlobalConfig{}
101 }).(dexpreopt.GlobalConfig)
102
Colin Cross74ba9622019-02-11 15:11:14 -0800103 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800104 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800105 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800106 }
107 if len(archs) == 0 {
108 // assume this is a java library, dexpreopt for all arches for now
109 for _, target := range ctx.Config().Targets[android.Android] {
Colin Cross74ba9622019-02-11 15:11:14 -0800110 archs = append(archs, target.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800111 }
112 if inList(ctx.ModuleName(), globalConfig.SystemServerJars) && !d.isSDKLibrary {
113 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
114 archs = archs[:1]
115 }
116 }
117 if ctx.Config().SecondArchIsTranslated() {
118 // Only preopt primary arch for translated arch since there is only an image there.
119 archs = archs[:1]
120 }
121
Colin Crossc7e40aa2019-02-08 21:37:00 -0800122 var images []string
123 for _, arch := range archs {
124 images = append(images, globalConfig.DefaultDexPreoptImage[arch])
125 }
126
Colin Cross43f08db2018-11-12 10:13:39 -0800127 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
128
129 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
130
131 deps := android.Paths{dexJarFile}
132
133 var profileClassListing android.OptionalPath
134 profileIsTextListing := false
135 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
136 // If dex_preopt.profile_guided is not set, default it based on the existence of the
137 // dexprepot.profile option or the profile class listing.
138 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
139 profileClassListing = android.OptionalPathForPath(
140 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
141 profileIsTextListing = true
142 } else {
143 profileClassListing = android.ExistentPathForSource(ctx,
144 ctx.Config().DexPreoptProfileDir(), ctx.ModuleName()+".prof")
145 }
146 }
147
148 if profileClassListing.Valid() {
149 deps = append(deps, profileClassListing.Path())
150 }
151
Colin Cross43f08db2018-11-12 10:13:39 -0800152 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800153 Name: ctx.ModuleName(),
154 DexLocation: dexLocation,
155 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
156 DexPath: dexJarFile.String(),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800157 UncompressedDex: d.uncompressedDex,
158 HasApkLibraries: false,
159 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800160
161 ProfileClassListing: profileClassListing.String(),
162 ProfileIsTextListing: profileIsTextListing,
163
164 EnforceUsesLibraries: false,
165 OptionalUsesLibraries: nil,
166 UsesLibraries: nil,
167 LibraryPaths: nil,
168
Colin Crossc7e40aa2019-02-08 21:37:00 -0800169 Archs: archs,
170 DexPreoptImages: images,
Colin Cross43f08db2018-11-12 10:13:39 -0800171
172 PreoptExtractedApk: false,
173
174 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
175 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
176
Colin Cross8c6d2502019-01-09 21:09:14 -0800177 NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
Colin Cross43f08db2018-11-12 10:13:39 -0800178 StripInputPath: dexJarFile.String(),
179 StripOutputPath: strippedDexJarFile.String(),
180 }
181
182 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(globalConfig, dexpreoptConfig)
183 if err != nil {
184 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
185 return dexJarFile
186 }
187
Colin Crossfeec25b2019-01-30 17:32:39 -0800188 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800189
190 for _, install := range dexpreoptRule.Installs() {
191 d.builtInstalled = append(d.builtInstalled, install.From+":"+install.To)
192 }
193
Colin Cross43f08db2018-11-12 10:13:39 -0800194 stripRule, err := dexpreopt.GenerateStripRule(globalConfig, dexpreoptConfig)
195 if err != nil {
196 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
197 return dexJarFile
198 }
199
Colin Crossfeec25b2019-01-30 17:32:39 -0800200 stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip")
Colin Cross43f08db2018-11-12 10:13:39 -0800201
202 return strippedDexJarFile
203}