blob: ae8d369548afce77ac2532182338c8da431c8927 [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
31 installPath android.OutputPath
32 isPrivApp bool
33 isSDKLibrary bool
34 isTest bool
35
36 builtInstalled []string
37}
38
39type DexpreoptProperties struct {
40 Dex_preopt struct {
41 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
42 // true.
43 Enabled *bool
44
45 // If true, generate an app image (.art file) for this module.
46 App_image *bool
47
48 // If true, use a checked-in profile to guide optimization. Defaults to false unless
49 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
50 // that matches the name of this module, in which case it is defaulted to true.
51 Profile_guided *bool
52
53 // If set, provides the path to profile relative to the Android.bp file. If not set,
54 // defaults to searching for a file that matches the name of this module in the default
55 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
56 Profile *string
57 }
58}
59
60func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
61 if ctx.Config().DisableDexPreopt(ctx.ModuleName()) {
62 return true
63 }
64
65 if ctx.Config().UnbundledBuild() {
66 return true
67 }
68
69 if d.isTest {
70 return true
71 }
72
73 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
74 return true
75 }
76
77 // TODO: contains no java code
78
79 return false
80}
81
82func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
83 if d.dexpreoptDisabled(ctx) {
84 return dexJarFile
85 }
86
87 globalConfig := ctx.Config().Once("DexpreoptGlobalConfig", func() interface{} {
88 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)
98
99 var archs []string
100 for _, a := range ctx.MultiTargets() {
101 archs = append(archs, a.Arch.ArchType.String())
102 }
103 if len(archs) == 0 {
104 // assume this is a java library, dexpreopt for all arches for now
105 for _, target := range ctx.Config().Targets[android.Android] {
106 archs = append(archs, target.Arch.ArchType.String())
107 }
108 if inList(ctx.ModuleName(), globalConfig.SystemServerJars) && !d.isSDKLibrary {
109 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
110 archs = archs[:1]
111 }
112 }
113 if ctx.Config().SecondArchIsTranslated() {
114 // Only preopt primary arch for translated arch since there is only an image there.
115 archs = archs[:1]
116 }
117
118 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
119
120 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
121
122 deps := android.Paths{dexJarFile}
123
124 var profileClassListing android.OptionalPath
125 profileIsTextListing := false
126 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
127 // If dex_preopt.profile_guided is not set, default it based on the existence of the
128 // dexprepot.profile option or the profile class listing.
129 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
130 profileClassListing = android.OptionalPathForPath(
131 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
132 profileIsTextListing = true
133 } else {
134 profileClassListing = android.ExistentPathForSource(ctx,
135 ctx.Config().DexPreoptProfileDir(), ctx.ModuleName()+".prof")
136 }
137 }
138
139 if profileClassListing.Valid() {
140 deps = append(deps, profileClassListing.Path())
141 }
142
143 uncompressedDex := false
144 if ctx.Config().UncompressPrivAppDex() &&
145 (d.isPrivApp || inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules())) {
146 uncompressedDex = true
147 }
148
149 dexpreoptConfig := dexpreopt.ModuleConfig{
150 Name: ctx.ModuleName(),
151 DexLocation: dexLocation,
152 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
153 DexPath: dexJarFile.String(),
154 PreferIntegrity: false,
155 UncompressedDex: uncompressedDex,
156 HasApkLibraries: false,
157 PreoptFlags: nil,
158
159 ProfileClassListing: profileClassListing.String(),
160 ProfileIsTextListing: profileIsTextListing,
161
162 EnforceUsesLibraries: false,
163 OptionalUsesLibraries: nil,
164 UsesLibraries: nil,
165 LibraryPaths: nil,
166
167 Archs: archs,
168 DexPreoptImageLocation: "",
169
170 PreoptExtractedApk: false,
171
172 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
173 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
174
175 StripInputPath: dexJarFile.String(),
176 StripOutputPath: strippedDexJarFile.String(),
177 }
178
179 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(globalConfig, dexpreoptConfig)
180 if err != nil {
181 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
182 return dexJarFile
183 }
184
185 var inputs android.Paths
186 for _, input := range dexpreoptRule.Inputs() {
187 if input == "" {
188 // Tests sometimes have empty configuration values that lead to empty inputs
189 continue
190 }
191 rel, isRel := android.MaybeRel(ctx, android.PathForModuleOut(ctx).String(), input)
192 if isRel {
193 inputs = append(inputs, android.PathForModuleOut(ctx, rel))
194 } else {
195 // TODO: use PathForOutput once boot image is moved to where PathForOutput can find it.
196 inputs = append(inputs, &bootImagePath{input})
197 }
198 }
199
200 var outputs android.WritablePaths
201 for _, output := range dexpreoptRule.Outputs() {
202 rel := android.Rel(ctx, android.PathForModuleOut(ctx).String(), output)
203 outputs = append(outputs, android.PathForModuleOut(ctx, rel))
204 }
205
206 for _, install := range dexpreoptRule.Installs() {
207 d.builtInstalled = append(d.builtInstalled, install.From+":"+install.To)
208 }
209
210 if len(dexpreoptRule.Commands()) > 0 {
211 ctx.Build(pctx, android.BuildParams{
212 Rule: ctx.Rule(pctx, "dexpreopt", blueprint.RuleParams{
213 Command: strings.Join(proptools.NinjaEscape(dexpreoptRule.Commands()), " && "),
214 CommandDeps: dexpreoptRule.Tools(),
215 }),
216 Implicits: inputs,
217 Outputs: outputs,
218 Description: "dexpreopt",
219 })
220 }
221
222 stripRule, err := dexpreopt.GenerateStripRule(globalConfig, dexpreoptConfig)
223 if err != nil {
224 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
225 return dexJarFile
226 }
227
228 ctx.Build(pctx, android.BuildParams{
229 Rule: ctx.Rule(pctx, "dexpreopt_strip", blueprint.RuleParams{
230 Command: strings.Join(proptools.NinjaEscape(stripRule.Commands()), " && "),
231 CommandDeps: stripRule.Tools(),
232 }),
233 Input: dexJarFile,
234 Output: strippedDexJarFile,
235 Description: "dexpreopt strip",
236 })
237
238 return strippedDexJarFile
239}
240
241type bootImagePath struct {
242 path string
243}
244
245var _ android.Path = (*bootImagePath)(nil)
246
247func (p *bootImagePath) String() string { return p.path }
248func (p *bootImagePath) Ext() string { return filepath.Ext(p.path) }
249func (p *bootImagePath) Base() string { return filepath.Base(p.path) }
250func (p *bootImagePath) Rel() string { return p.path }