blob: ecb242159a7600049dc8aa2e873048b4fee33020 [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)
Colin Cross43f08db2018-11-12 10:13:39 -0800101
Colin Cross74ba9622019-02-11 15:11:14 -0800102 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800103 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800104 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800105 }
106 if len(archs) == 0 {
107 // assume this is a java library, dexpreopt for all arches for now
108 for _, target := range ctx.Config().Targets[android.Android] {
Colin Cross74ba9622019-02-11 15:11:14 -0800109 archs = append(archs, target.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800110 }
Colin Cross44df5812019-02-15 23:06:46 -0800111 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800112 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
113 archs = archs[:1]
114 }
115 }
116 if ctx.Config().SecondArchIsTranslated() {
117 // Only preopt primary arch for translated arch since there is only an image there.
118 archs = archs[:1]
119 }
120
Colin Cross69f59a32019-02-15 10:39:37 -0800121 var images android.Paths
Colin Crossc7e40aa2019-02-08 21:37:00 -0800122 for _, arch := range archs {
Colin Cross44df5812019-02-15 23:06:46 -0800123 images = append(images, bootImage.images[arch])
Colin Crossc7e40aa2019-02-08 21:37:00 -0800124 }
125
Colin Cross43f08db2018-11-12 10:13:39 -0800126 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
127
128 strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base())
129
Colin Cross43f08db2018-11-12 10:13:39 -0800130 var profileClassListing android.OptionalPath
131 profileIsTextListing := false
132 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
133 // If dex_preopt.profile_guided is not set, default it based on the existence of the
134 // dexprepot.profile option or the profile class listing.
135 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
136 profileClassListing = android.OptionalPathForPath(
137 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
138 profileIsTextListing = true
139 } else {
140 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800141 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800142 }
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,
Colin Cross69f59a32019-02-15 10:39:37 -0800148 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
149 DexPath: dexJarFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800150 UncompressedDex: d.uncompressedDex,
151 HasApkLibraries: false,
152 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800153
Colin Cross69f59a32019-02-15 10:39:37 -0800154 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800155 ProfileIsTextListing: profileIsTextListing,
156
157 EnforceUsesLibraries: false,
158 OptionalUsesLibraries: nil,
159 UsesLibraries: nil,
160 LibraryPaths: nil,
161
Colin Crossc7e40aa2019-02-08 21:37:00 -0800162 Archs: archs,
163 DexPreoptImages: images,
Colin Cross43f08db2018-11-12 10:13:39 -0800164
Colin Cross44df5812019-02-15 23:06:46 -0800165 PreoptBootClassPathDexFiles: bootImage.dexPaths.Paths(),
166 PreoptBootClassPathDexLocations: bootImage.dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800167
Colin Cross43f08db2018-11-12 10:13:39 -0800168 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 Cross69f59a32019-02-15 10:39:37 -0800174 StripInputPath: dexJarFile,
175 StripOutputPath: strippedDexJarFile.OutputPath,
Colin Cross43f08db2018-11-12 10:13:39 -0800176 }
177
Colin Cross44df5812019-02-15 23:06:46 -0800178 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800179 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
Colin Crossdeabb942019-02-11 14:11:09 -0800186 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800187
Colin Cross44df5812019-02-15 23:06:46 -0800188 stripRule, err := dexpreopt.GenerateStripRule(global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800189 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}