blob: 6b93c10e3ec8200d0fea7c7a9af50edf3efa02a4 [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
Colin Crossdeabb942019-02-11 14:11:09 -080031 builtInstalled string
Colin Cross43f08db2018-11-12 10:13:39 -080032}
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 {
Colin Cross69f59a32019-02-15 10:39:37 -080059 global := dexpreoptGlobalConfig(ctx)
60
61 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080062 return true
63 }
64
Colin Cross69f59a32019-02-15 10:39:37 -080065 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080066 return true
67 }
68
69 if ctx.Config().UnbundledBuild() {
70 return true
71 }
72
73 if d.isTest {
74 return true
75 }
76
77 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
78 return true
79 }
80
Colin Crossdc2da912019-01-05 22:13:05 -080081 if !d.isInstallable {
82 return true
83 }
84
Colin Cross43f08db2018-11-12 10:13:39 -080085 // TODO: contains no java code
86
87 return false
88}
89
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000090func odexOnSystemOther(ctx android.ModuleContext, installPath android.OutputPath) bool {
Colin Cross800fe132019-02-11 14:21:24 -080091 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000092}
93
94func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
95 if d.dexpreoptDisabled(ctx) {
96 return dexJarFile
97 }
98
Colin Cross44df5812019-02-15 23:06:46 -080099 global := dexpreoptGlobalConfig(ctx)
100 bootImage := defaultBootImageConfig(ctx)
Nicolas Geoffray25c0e032019-04-04 18:45:20 +0100101 if global.UseApexImage {
102 bootImage = apexBootImageConfig(ctx)
103 }
Colin Cross43f08db2018-11-12 10:13:39 -0800104
Colin Cross74ba9622019-02-11 15:11:14 -0800105 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800106 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800107 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800108 }
109 if len(archs) == 0 {
110 // assume this is a java library, dexpreopt for all arches for now
111 for _, target := range ctx.Config().Targets[android.Android] {
Colin Cross74ba9622019-02-11 15:11:14 -0800112 archs = append(archs, target.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800113 }
Colin Cross44df5812019-02-15 23:06:46 -0800114 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800115 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
116 archs = archs[:1]
117 }
118 }
119 if ctx.Config().SecondArchIsTranslated() {
120 // Only preopt primary arch for translated arch since there is only an image there.
121 archs = archs[:1]
122 }
123
Colin Cross69f59a32019-02-15 10:39:37 -0800124 var images android.Paths
Colin Crossc7e40aa2019-02-08 21:37:00 -0800125 for _, arch := range archs {
Colin Cross44df5812019-02-15 23:06:46 -0800126 images = append(images, bootImage.images[arch])
Colin Crossc7e40aa2019-02-08 21:37:00 -0800127 }
128
Colin Cross43f08db2018-11-12 10:13:39 -0800129 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
130
131 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
132
Colin Cross43f08db2018-11-12 10:13:39 -0800133 var profileClassListing android.OptionalPath
134 profileIsTextListing := false
135 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
136 // If dex_preopt.profile_guided is not set, default it based on the existence of the
137 // dexprepot.profile option or the profile class listing.
138 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
139 profileClassListing = android.OptionalPathForPath(
140 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
141 profileIsTextListing = true
142 } else {
143 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800144 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800145 }
146 }
147
Colin Cross43f08db2018-11-12 10:13:39 -0800148 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800149 Name: ctx.ModuleName(),
150 DexLocation: dexLocation,
Colin Cross69f59a32019-02-15 10:39:37 -0800151 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
152 DexPath: dexJarFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800153 UncompressedDex: d.uncompressedDex,
154 HasApkLibraries: false,
155 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800156
Colin Cross69f59a32019-02-15 10:39:37 -0800157 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800158 ProfileIsTextListing: profileIsTextListing,
159
160 EnforceUsesLibraries: false,
161 OptionalUsesLibraries: nil,
162 UsesLibraries: nil,
163 LibraryPaths: nil,
164
Colin Crossc7e40aa2019-02-08 21:37:00 -0800165 Archs: archs,
166 DexPreoptImages: images,
Colin Cross43f08db2018-11-12 10:13:39 -0800167
Colin Cross44df5812019-02-15 23:06:46 -0800168 PreoptBootClassPathDexFiles: bootImage.dexPaths.Paths(),
169 PreoptBootClassPathDexLocations: bootImage.dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800170
Colin Cross43f08db2018-11-12 10:13:39 -0800171 PreoptExtractedApk: false,
172
173 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
174 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
175
Colin Cross8c6d2502019-01-09 21:09:14 -0800176 NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping),
Colin Cross69f59a32019-02-15 10:39:37 -0800177 StripInputPath: dexJarFile,
178 StripOutputPath: strippedDexJarFile.OutputPath,
Colin Cross43f08db2018-11-12 10:13:39 -0800179 }
180
Colin Cross44df5812019-02-15 23:06:46 -0800181 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800182 if err != nil {
183 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
184 return dexJarFile
185 }
186
Colin Crossfeec25b2019-01-30 17:32:39 -0800187 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800188
Colin Crossdeabb942019-02-11 14:11:09 -0800189 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800190
Colin Cross44df5812019-02-15 23:06:46 -0800191 stripRule, err := dexpreopt.GenerateStripRule(global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800192 if err != nil {
193 ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error())
194 return dexJarFile
195 }
196
Colin Crossfeec25b2019-01-30 17:32:39 -0800197 stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip")
Colin Cross43f08db2018-11-12 10:13:39 -0800198
199 return strippedDexJarFile
200}