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