blob: 33c46f4fe52d1552d30b90b7416abbc98eefce06 [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
Colin Cross571cccf2019-02-04 11:22:08 -080084var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
85
Colin Cross43f08db2018-11-12 10:13:39 -080086func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
87 if d.dexpreoptDisabled(ctx) {
88 return dexJarFile
89 }
90
Colin Cross571cccf2019-02-04 11:22:08 -080091 globalConfig := ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
Colin Cross43f08db2018-11-12 10:13:39 -080092 if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
93 ctx.AddNinjaFileDeps(f)
94 globalConfig, err := dexpreopt.LoadGlobalConfig(f)
95 if err != nil {
96 panic(err)
97 }
98 return globalConfig
99 }
100 return dexpreopt.GlobalConfig{}
101 }).(dexpreopt.GlobalConfig)
102
103 var archs []string
104 for _, a := range ctx.MultiTargets() {
105 archs = append(archs, a.Arch.ArchType.String())
106 }
107 if len(archs) == 0 {
108 // assume this is a java library, dexpreopt for all arches for now
109 for _, target := range ctx.Config().Targets[android.Android] {
110 archs = append(archs, target.Arch.ArchType.String())
111 }
112 if inList(ctx.ModuleName(), globalConfig.SystemServerJars) && !d.isSDKLibrary {
113 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
114 archs = archs[:1]
115 }
116 }
117 if ctx.Config().SecondArchIsTranslated() {
118 // Only preopt primary arch for translated arch since there is only an image there.
119 archs = archs[:1]
120 }
121
122 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
123
124 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
125
126 deps := android.Paths{dexJarFile}
127
128 var profileClassListing android.OptionalPath
129 profileIsTextListing := false
130 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
131 // If dex_preopt.profile_guided is not set, default it based on the existence of the
132 // dexprepot.profile option or the profile class listing.
133 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
134 profileClassListing = android.OptionalPathForPath(
135 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
136 profileIsTextListing = true
137 } else {
138 profileClassListing = android.ExistentPathForSource(ctx,
139 ctx.Config().DexPreoptProfileDir(), ctx.ModuleName()+".prof")
140 }
141 }
142
143 if profileClassListing.Valid() {
144 deps = append(deps, profileClassListing.Path())
145 }
146
Colin Cross43f08db2018-11-12 10:13:39 -0800147 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800148 Name: ctx.ModuleName(),
149 DexLocation: dexLocation,
150 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").String(),
151 DexPath: dexJarFile.String(),
152 UseEmbeddedDex: false,
153 UncompressedDex: d.uncompressedDex,
154 HasApkLibraries: false,
155 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800156
157 ProfileClassListing: profileClassListing.String(),
158 ProfileIsTextListing: profileIsTextListing,
159
160 EnforceUsesLibraries: false,
161 OptionalUsesLibraries: nil,
162 UsesLibraries: nil,
163 LibraryPaths: nil,
164
165 Archs: archs,
166 DexPreoptImageLocation: "",
167
168 PreoptExtractedApk: false,
169
170 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
171 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
172
Colin Cross8c6d2502019-01-09 21:09:14 -0800173 NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
Colin Cross43f08db2018-11-12 10:13:39 -0800174 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
Colin Crossfeec25b2019-01-30 17:32:39 -0800184 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800185
186 for _, install := range dexpreoptRule.Installs() {
187 d.builtInstalled = append(d.builtInstalled, install.From+":"+install.To)
188 }
189
Colin Cross43f08db2018-11-12 10:13:39 -0800190 stripRule, err := dexpreopt.GenerateStripRule(globalConfig, dexpreoptConfig)
191 if err != nil {
192 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
193 return dexJarFile
194 }
195
Colin Crossfeec25b2019-01-30 17:32:39 -0800196 stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip")
Colin Cross43f08db2018-11-12 10:13:39 -0800197
198 return strippedDexJarFile
199}