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