blob: 6e46bc91a5e7413d78a9e03628320b102933cfec [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 (
18 "path/filepath"
19 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
25 "android/soong/dexpreopt"
26)
27
28type dexpreopter struct {
29 dexpreoptProperties DexpreoptProperties
30
Colin Cross2fc72f62018-12-21 12:59:54 -080031 installPath android.OutputPath
32 uncompressedDex bool
33 isSDKLibrary bool
34 isTest bool
35 isInstallable bool
Colin Cross43f08db2018-11-12 10:13:39 -080036
37 builtInstalled []string
38}
39
40type DexpreoptProperties struct {
41 Dex_preopt struct {
42 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
43 // true.
44 Enabled *bool
45
Colin Cross8c6d2502019-01-09 21:09:14 -080046 // If true, never strip the dex files from the final jar when dexpreopting. Defaults to false.
47 No_stripping *bool
48
Colin Cross43f08db2018-11-12 10:13:39 -080049 // If true, generate an app image (.art file) for this module.
50 App_image *bool
51
52 // If true, use a checked-in profile to guide optimization. Defaults to false unless
53 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
54 // that matches the name of this module, in which case it is defaulted to true.
55 Profile_guided *bool
56
57 // If set, provides the path to profile relative to the Android.bp file. If not set,
58 // defaults to searching for a file that matches the name of this module in the default
59 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
60 Profile *string
61 }
62}
63
64func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
65 if ctx.Config().DisableDexPreopt(ctx.ModuleName()) {
66 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
90func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
91 if d.dexpreoptDisabled(ctx) {
92 return dexJarFile
93 }
94
95 globalConfig := ctx.Config().Once("DexpreoptGlobalConfig", func() interface{} {
96 if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
97 ctx.AddNinjaFileDeps(f)
98 globalConfig, err := dexpreopt.LoadGlobalConfig(f)
99 if err != nil {
100 panic(err)
101 }
102 return globalConfig
103 }
104 return dexpreopt.GlobalConfig{}
105 }).(dexpreopt.GlobalConfig)
106
107 var archs []string
108 for _, a := range ctx.MultiTargets() {
109 archs = append(archs, a.Arch.ArchType.String())
110 }
111 if len(archs) == 0 {
112 // assume this is a java library, dexpreopt for all arches for now
113 for _, target := range ctx.Config().Targets[android.Android] {
114 archs = append(archs, target.Arch.ArchType.String())
115 }
116 if inList(ctx.ModuleName(), globalConfig.SystemServerJars) && !d.isSDKLibrary {
117 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
118 archs = archs[:1]
119 }
120 }
121 if ctx.Config().SecondArchIsTranslated() {
122 // Only preopt primary arch for translated arch since there is only an image there.
123 archs = archs[:1]
124 }
125
126 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
127
128 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
129
130 deps := android.Paths{dexJarFile}
131
132 var profileClassListing android.OptionalPath
133 profileIsTextListing := false
134 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
135 // If dex_preopt.profile_guided is not set, default it based on the existence of the
136 // dexprepot.profile option or the profile class listing.
137 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
138 profileClassListing = android.OptionalPathForPath(
139 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
140 profileIsTextListing = true
141 } else {
142 profileClassListing = android.ExistentPathForSource(ctx,
143 ctx.Config().DexPreoptProfileDir(), ctx.ModuleName()+".prof")
144 }
145 }
146
147 if profileClassListing.Valid() {
148 deps = append(deps, profileClassListing.Path())
149 }
150
Colin Cross43f08db2018-11-12 10:13:39 -0800151 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800152 Name: ctx.ModuleName(),
153 DexLocation: dexLocation,
154 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
155 DexPath: dexJarFile.String(),
156 UseEmbeddedDex: false,
157 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
169 Archs: archs,
170 DexPreoptImageLocation: "",
171
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
188 var inputs android.Paths
189 for _, input := range dexpreoptRule.Inputs() {
190 if input == "" {
191 // Tests sometimes have empty configuration values that lead to empty inputs
192 continue
193 }
194 rel, isRel := android.MaybeRel(ctx, android.PathForModuleOut(ctx).String(), input)
195 if isRel {
196 inputs = append(inputs, android.PathForModuleOut(ctx, rel))
197 } else {
198 // TODO: use PathForOutput once boot image is moved to where PathForOutput can find it.
199 inputs = append(inputs, &bootImagePath{input})
200 }
201 }
202
203 var outputs android.WritablePaths
204 for _, output := range dexpreoptRule.Outputs() {
205 rel := android.Rel(ctx, android.PathForModuleOut(ctx).String(), output)
206 outputs = append(outputs, android.PathForModuleOut(ctx, rel))
207 }
208
209 for _, install := range dexpreoptRule.Installs() {
210 d.builtInstalled = append(d.builtInstalled, install.From+":"+install.To)
211 }
212
213 if len(dexpreoptRule.Commands()) > 0 {
214 ctx.Build(pctx, android.BuildParams{
215 Rule: ctx.Rule(pctx, "dexpreopt", blueprint.RuleParams{
216 Command: strings.Join(proptools.NinjaEscape(dexpreoptRule.Commands()), " && "),
217 CommandDeps: dexpreoptRule.Tools(),
218 }),
219 Implicits: inputs,
220 Outputs: outputs,
221 Description: "dexpreopt",
222 })
223 }
224
225 stripRule, err := dexpreopt.GenerateStripRule(globalConfig, dexpreoptConfig)
226 if err != nil {
227 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
228 return dexJarFile
229 }
230
231 ctx.Build(pctx, android.BuildParams{
232 Rule: ctx.Rule(pctx, "dexpreopt_strip", blueprint.RuleParams{
233 Command: strings.Join(proptools.NinjaEscape(stripRule.Commands()), " && "),
234 CommandDeps: stripRule.Tools(),
235 }),
236 Input: dexJarFile,
237 Output: strippedDexJarFile,
238 Description: "dexpreopt strip",
239 })
240
241 return strippedDexJarFile
242}
243
244type bootImagePath struct {
245 path string
246}
247
248var _ android.Path = (*bootImagePath)(nil)
249
250func (p *bootImagePath) String() string { return p.path }
251func (p *bootImagePath) Ext() string { return filepath.Ext(p.path) }
252func (p *bootImagePath) Base() string { return filepath.Base(p.path) }
253func (p *bootImagePath) Rel() string { return p.path }