blob: a89731ac8e20c8baf45b67952665431d6ccbd0ee [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
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000086func getGlobalConfig(ctx android.ModuleContext) dexpreopt.GlobalConfig {
Colin Cross571cccf2019-02-04 11:22:08 -080087 globalConfig := ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
Colin Cross43f08db2018-11-12 10:13:39 -080088 if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
89 ctx.AddNinjaFileDeps(f)
90 globalConfig, err := dexpreopt.LoadGlobalConfig(f)
91 if err != nil {
92 panic(err)
93 }
94 return globalConfig
95 }
96 return dexpreopt.GlobalConfig{}
97 }).(dexpreopt.GlobalConfig)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000098 return globalConfig
99}
100
101func odexOnSystemOther(ctx android.ModuleContext, installPath android.OutputPath) bool {
102 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), getGlobalConfig(ctx))
103}
104
105func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
106 if d.dexpreoptDisabled(ctx) {
107 return dexJarFile
108 }
109
110 globalConfig := getGlobalConfig(ctx)
Colin Cross43f08db2018-11-12 10:13:39 -0800111
Colin Cross74ba9622019-02-11 15:11:14 -0800112 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800113 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800114 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800115 }
116 if len(archs) == 0 {
117 // assume this is a java library, dexpreopt for all arches for now
118 for _, target := range ctx.Config().Targets[android.Android] {
Colin Cross74ba9622019-02-11 15:11:14 -0800119 archs = append(archs, target.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800120 }
121 if inList(ctx.ModuleName(), globalConfig.SystemServerJars) && !d.isSDKLibrary {
122 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
123 archs = archs[:1]
124 }
125 }
126 if ctx.Config().SecondArchIsTranslated() {
127 // Only preopt primary arch for translated arch since there is only an image there.
128 archs = archs[:1]
129 }
130
Colin Crossc7e40aa2019-02-08 21:37:00 -0800131 var images []string
132 for _, arch := range archs {
133 images = append(images, globalConfig.DefaultDexPreoptImage[arch])
134 }
135
Colin Cross43f08db2018-11-12 10:13:39 -0800136 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
137
138 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
139
140 deps := android.Paths{dexJarFile}
141
142 var profileClassListing android.OptionalPath
143 profileIsTextListing := false
144 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
145 // If dex_preopt.profile_guided is not set, default it based on the existence of the
146 // dexprepot.profile option or the profile class listing.
147 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
148 profileClassListing = android.OptionalPathForPath(
149 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
150 profileIsTextListing = true
151 } else {
152 profileClassListing = android.ExistentPathForSource(ctx,
153 ctx.Config().DexPreoptProfileDir(), ctx.ModuleName()+".prof")
154 }
155 }
156
157 if profileClassListing.Valid() {
158 deps = append(deps, profileClassListing.Path())
159 }
160
Colin Cross43f08db2018-11-12 10:13:39 -0800161 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800162 Name: ctx.ModuleName(),
163 DexLocation: dexLocation,
164 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
165 DexPath: dexJarFile.String(),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800166 UncompressedDex: d.uncompressedDex,
167 HasApkLibraries: false,
168 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800169
170 ProfileClassListing: profileClassListing.String(),
171 ProfileIsTextListing: profileIsTextListing,
172
173 EnforceUsesLibraries: false,
174 OptionalUsesLibraries: nil,
175 UsesLibraries: nil,
176 LibraryPaths: nil,
177
Colin Crossc7e40aa2019-02-08 21:37:00 -0800178 Archs: archs,
179 DexPreoptImages: images,
Colin Cross43f08db2018-11-12 10:13:39 -0800180
181 PreoptExtractedApk: false,
182
183 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
184 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
185
Colin Cross8c6d2502019-01-09 21:09:14 -0800186 NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
Colin Cross43f08db2018-11-12 10:13:39 -0800187 StripInputPath: dexJarFile.String(),
188 StripOutputPath: strippedDexJarFile.String(),
189 }
190
191 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(globalConfig, dexpreoptConfig)
192 if err != nil {
193 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
194 return dexJarFile
195 }
196
Colin Crossfeec25b2019-01-30 17:32:39 -0800197 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800198
199 for _, install := range dexpreoptRule.Installs() {
200 d.builtInstalled = append(d.builtInstalled, install.From+":"+install.To)
201 }
202
Colin Cross43f08db2018-11-12 10:13:39 -0800203 stripRule, err := dexpreopt.GenerateStripRule(globalConfig, dexpreoptConfig)
204 if err != nil {
205 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
206 return dexJarFile
207 }
208
Colin Crossfeec25b2019-01-30 17:32:39 -0800209 stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip")
Colin Cross43f08db2018-11-12 10:13:39 -0800210
211 return strippedDexJarFile
212}