blob: bb72b7dec95d0bfc02678312e06790ff6ac848d1 [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 (
Colin Cross43f08db2018-11-12 10:13:39 -080018 "android/soong/android"
19 "android/soong/dexpreopt"
20)
21
22type dexpreopter struct {
23 dexpreoptProperties DexpreoptProperties
24
Colin Cross2fc72f62018-12-21 12:59:54 -080025 installPath android.OutputPath
26 uncompressedDex bool
27 isSDKLibrary bool
28 isTest bool
29 isInstallable bool
Colin Cross43f08db2018-11-12 10:13:39 -080030
31 builtInstalled []string
32}
33
34type DexpreoptProperties struct {
35 Dex_preopt struct {
36 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
37 // true.
38 Enabled *bool
39
Colin Cross8c6d2502019-01-09 21:09:14 -080040 // If true, never strip the dex files from the final jar when dexpreopting. Defaults to false.
41 No_stripping *bool
42
Colin Cross43f08db2018-11-12 10:13:39 -080043 // If true, generate an app image (.art file) for this module.
44 App_image *bool
45
46 // If true, use a checked-in profile to guide optimization. Defaults to false unless
47 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
48 // that matches the name of this module, in which case it is defaulted to true.
49 Profile_guided *bool
50
51 // If set, provides the path to profile relative to the Android.bp file. If not set,
52 // defaults to searching for a file that matches the name of this module in the default
53 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
54 Profile *string
55 }
56}
57
58func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
59 if ctx.Config().DisableDexPreopt(ctx.ModuleName()) {
60 return true
61 }
62
63 if ctx.Config().UnbundledBuild() {
64 return true
65 }
66
67 if d.isTest {
68 return true
69 }
70
71 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
72 return true
73 }
74
Colin Crossdc2da912019-01-05 22:13:05 -080075 if !d.isInstallable {
76 return true
77 }
78
Colin Cross43f08db2018-11-12 10:13:39 -080079 // TODO: contains no java code
80
81 return false
82}
83
84func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
85 if d.dexpreoptDisabled(ctx) {
86 return dexJarFile
87 }
88
89 globalConfig := ctx.Config().Once("DexpreoptGlobalConfig", func() interface{} {
90 if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
91 ctx.AddNinjaFileDeps(f)
92 globalConfig, err := dexpreopt.LoadGlobalConfig(f)
93 if err != nil {
94 panic(err)
95 }
96 return globalConfig
97 }
98 return dexpreopt.GlobalConfig{}
99 }).(dexpreopt.GlobalConfig)
100
101 var archs []string
102 for _, a := range ctx.MultiTargets() {
103 archs = append(archs, a.Arch.ArchType.String())
104 }
105 if len(archs) == 0 {
106 // assume this is a java library, dexpreopt for all arches for now
107 for _, target := range ctx.Config().Targets[android.Android] {
108 archs = append(archs, target.Arch.ArchType.String())
109 }
110 if inList(ctx.ModuleName(), globalConfig.SystemServerJars) && !d.isSDKLibrary {
111 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
112 archs = archs[:1]
113 }
114 }
115 if ctx.Config().SecondArchIsTranslated() {
116 // Only preopt primary arch for translated arch since there is only an image there.
117 archs = archs[:1]
118 }
119
120 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
121
122 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
123
124 deps := android.Paths{dexJarFile}
125
126 var profileClassListing android.OptionalPath
127 profileIsTextListing := false
128 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
129 // If dex_preopt.profile_guided is not set, default it based on the existence of the
130 // dexprepot.profile option or the profile class listing.
131 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
132 profileClassListing = android.OptionalPathForPath(
133 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
134 profileIsTextListing = true
135 } else {
136 profileClassListing = android.ExistentPathForSource(ctx,
137 ctx.Config().DexPreoptProfileDir(), ctx.ModuleName()+".prof")
138 }
139 }
140
141 if profileClassListing.Valid() {
142 deps = append(deps, profileClassListing.Path())
143 }
144
Colin Cross43f08db2018-11-12 10:13:39 -0800145 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800146 Name: ctx.ModuleName(),
147 DexLocation: dexLocation,
148 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
149 DexPath: dexJarFile.String(),
150 UseEmbeddedDex: false,
151 UncompressedDex: d.uncompressedDex,
152 HasApkLibraries: false,
153 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800154
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
Colin Cross8c6d2502019-01-09 21:09:14 -0800171 NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
Colin Cross43f08db2018-11-12 10:13:39 -0800172 StripInputPath: dexJarFile.String(),
173 StripOutputPath: strippedDexJarFile.String(),
174 }
175
176 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(globalConfig, dexpreoptConfig)
177 if err != nil {
178 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
179 return dexJarFile
180 }
181
Colin Crossfeec25b2019-01-30 17:32:39 -0800182 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800183
184 for _, install := range dexpreoptRule.Installs() {
185 d.builtInstalled = append(d.builtInstalled, install.From+":"+install.To)
186 }
187
Colin Cross43f08db2018-11-12 10:13:39 -0800188 stripRule, err := dexpreopt.GenerateStripRule(globalConfig, dexpreoptConfig)
189 if err != nil {
190 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
191 return dexJarFile
192 }
193
Colin Crossfeec25b2019-01-30 17:32:39 -0800194 stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip")
Colin Cross43f08db2018-11-12 10:13:39 -0800195
196 return strippedDexJarFile
197}