blob: ce51abaa5ac241340bee791b0df166beb8746407 [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
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
Colin Cross43f08db2018-11-12 10:13:39 -0800148 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsieha2c16c12019-01-02 14:50:56 -0800149 Name: ctx.ModuleName(),
150 DexLocation: dexLocation,
151 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
152 DexPath: dexJarFile.String(),
153 PreferCodeIntegrity: false,
Colin Cross2fc72f62018-12-21 12:59:54 -0800154 UncompressedDex: d.uncompressedDex,
Victor Hsieha2c16c12019-01-02 14:50:56 -0800155 HasApkLibraries: false,
156 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800157
158 ProfileClassListing: profileClassListing.String(),
159 ProfileIsTextListing: profileIsTextListing,
160
161 EnforceUsesLibraries: false,
162 OptionalUsesLibraries: nil,
163 UsesLibraries: nil,
164 LibraryPaths: nil,
165
166 Archs: archs,
167 DexPreoptImageLocation: "",
168
169 PreoptExtractedApk: false,
170
171 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
172 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
173
174 StripInputPath: dexJarFile.String(),
175 StripOutputPath: strippedDexJarFile.String(),
176 }
177
178 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(globalConfig, dexpreoptConfig)
179 if err != nil {
180 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
181 return dexJarFile
182 }
183
184 var inputs android.Paths
185 for _, input := range dexpreoptRule.Inputs() {
186 if input == "" {
187 // Tests sometimes have empty configuration values that lead to empty inputs
188 continue
189 }
190 rel, isRel := android.MaybeRel(ctx, android.PathForModuleOut(ctx).String(), input)
191 if isRel {
192 inputs = append(inputs, android.PathForModuleOut(ctx, rel))
193 } else {
194 // TODO: use PathForOutput once boot image is moved to where PathForOutput can find it.
195 inputs = append(inputs, &bootImagePath{input})
196 }
197 }
198
199 var outputs android.WritablePaths
200 for _, output := range dexpreoptRule.Outputs() {
201 rel := android.Rel(ctx, android.PathForModuleOut(ctx).String(), output)
202 outputs = append(outputs, android.PathForModuleOut(ctx, rel))
203 }
204
205 for _, install := range dexpreoptRule.Installs() {
206 d.builtInstalled = append(d.builtInstalled, install.From+":"+install.To)
207 }
208
209 if len(dexpreoptRule.Commands()) > 0 {
210 ctx.Build(pctx, android.BuildParams{
211 Rule: ctx.Rule(pctx, "dexpreopt", blueprint.RuleParams{
212 Command: strings.Join(proptools.NinjaEscape(dexpreoptRule.Commands()), " && "),
213 CommandDeps: dexpreoptRule.Tools(),
214 }),
215 Implicits: inputs,
216 Outputs: outputs,
217 Description: "dexpreopt",
218 })
219 }
220
221 stripRule, err := dexpreopt.GenerateStripRule(globalConfig, dexpreoptConfig)
222 if err != nil {
223 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
224 return dexJarFile
225 }
226
227 ctx.Build(pctx, android.BuildParams{
228 Rule: ctx.Rule(pctx, "dexpreopt_strip", blueprint.RuleParams{
229 Command: strings.Join(proptools.NinjaEscape(stripRule.Commands()), " && "),
230 CommandDeps: stripRule.Tools(),
231 }),
232 Input: dexJarFile,
233 Output: strippedDexJarFile,
234 Description: "dexpreopt strip",
235 })
236
237 return strippedDexJarFile
238}
239
240type bootImagePath struct {
241 path string
242}
243
244var _ android.Path = (*bootImagePath)(nil)
245
246func (p *bootImagePath) String() string { return p.path }
247func (p *bootImagePath) Ext() string { return filepath.Ext(p.path) }
248func (p *bootImagePath) Base() string { return filepath.Base(p.path) }
249func (p *bootImagePath) Rel() string { return p.path }