blob: 479dec60be64e0aa3daeb7514fee83ceb44574f5 [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 Cross70dda7e2019-10-01 22:05:35 -070025 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -070026 uncompressedDex bool
27 isSDKLibrary bool
28 isTest bool
29 isInstallable bool
30 isPresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -080031
Colin Cross50ddcc42019-05-16 12:28:22 -070032 manifestFile android.Path
33 usesLibs []string
34 optionalUsesLibs []string
35 enforceUsesLibs bool
36 libraryPaths map[string]android.Path
37
Colin Crossdeabb942019-02-11 14:11:09 -080038 builtInstalled string
Colin Cross43f08db2018-11-12 10:13:39 -080039}
40
41type DexpreoptProperties struct {
42 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010043 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -080044 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.
Colin Crossde4e4e62019-04-26 10:52:32 -070057 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -080058 }
59}
60
61func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool {
Colin Cross69f59a32019-02-15 10:39:37 -080062 global := dexpreoptGlobalConfig(ctx)
63
64 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080065 return true
66 }
67
Colin Cross69f59a32019-02-15 10:39:37 -080068 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080069 return true
70 }
71
72 if ctx.Config().UnbundledBuild() {
73 return true
74 }
75
76 if d.isTest {
77 return true
78 }
79
80 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
81 return true
82 }
83
Colin Crossdc2da912019-01-05 22:13:05 -080084 if !d.isInstallable {
85 return true
86 }
87
Colin Cross43f08db2018-11-12 10:13:39 -080088 // TODO: contains no java code
89
90 return false
91}
92
Colin Cross70dda7e2019-10-01 22:05:35 -070093func odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool {
Colin Cross800fe132019-02-11 14:21:24 -080094 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000095}
96
97func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath {
98 if d.dexpreoptDisabled(ctx) {
99 return dexJarFile
100 }
101
Colin Cross44df5812019-02-15 23:06:46 -0800102 global := dexpreoptGlobalConfig(ctx)
103 bootImage := defaultBootImageConfig(ctx)
Nicolas Geoffray06758a72019-04-08 17:19:15 +0100104 defaultBootImage := bootImage
Nicolas Geoffray25c0e032019-04-04 18:45:20 +0100105 if global.UseApexImage {
106 bootImage = apexBootImageConfig(ctx)
107 }
Colin Cross43f08db2018-11-12 10:13:39 -0800108
Colin Cross74ba9622019-02-11 15:11:14 -0800109 var archs []android.ArchType
Colin Cross43f08db2018-11-12 10:13:39 -0800110 for _, a := range ctx.MultiTargets() {
Colin Cross74ba9622019-02-11 15:11:14 -0800111 archs = append(archs, a.Arch.ArchType)
Colin Cross43f08db2018-11-12 10:13:39 -0800112 }
113 if len(archs) == 0 {
114 // assume this is a java library, dexpreopt for all arches for now
115 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100116 if target.NativeBridge == android.NativeBridgeDisabled {
117 archs = append(archs, target.Arch.ArchType)
118 }
Colin Cross43f08db2018-11-12 10:13:39 -0800119 }
Colin Cross44df5812019-02-15 23:06:46 -0800120 if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800121 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
122 archs = archs[:1]
123 }
124 }
Colin Cross43f08db2018-11-12 10:13:39 -0800125
Colin Cross69f59a32019-02-15 10:39:37 -0800126 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000127 var imagesDeps []android.OutputPaths
Colin Crossc7e40aa2019-02-08 21:37:00 -0800128 for _, arch := range archs {
Colin Cross44df5812019-02-15 23:06:46 -0800129 images = append(images, bootImage.images[arch])
Dan Willemsen0f416782019-06-13 21:44:53 +0000130 imagesDeps = append(imagesDeps, bootImage.imagesDeps[arch])
Colin Crossc7e40aa2019-02-08 21:37:00 -0800131 }
132
Colin Cross43f08db2018-11-12 10:13:39 -0800133 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
134
Colin Cross43f08db2018-11-12 10:13:39 -0800135 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100136 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800137 profileIsTextListing := false
138 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
139 // If dex_preopt.profile_guided is not set, default it based on the existence of the
140 // dexprepot.profile option or the profile class listing.
141 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
142 profileClassListing = android.OptionalPathForPath(
143 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100144 profileBootListing = android.ExistentPathForSource(ctx,
145 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800146 profileIsTextListing = true
147 } else {
148 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800149 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800150 }
151 }
152
Colin Cross43f08db2018-11-12 10:13:39 -0800153 dexpreoptConfig := dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800154 Name: ctx.ModuleName(),
155 DexLocation: dexLocation,
Colin Cross69f59a32019-02-15 10:39:37 -0800156 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
157 DexPath: dexJarFile,
Colin Cross50ddcc42019-05-16 12:28:22 -0700158 ManifestPath: d.manifestFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800159 UncompressedDex: d.uncompressedDex,
160 HasApkLibraries: false,
161 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800162
Colin Cross69f59a32019-02-15 10:39:37 -0800163 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800164 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100165 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800166
Colin Cross50ddcc42019-05-16 12:28:22 -0700167 EnforceUsesLibraries: d.enforceUsesLibs,
168 PresentOptionalUsesLibraries: d.optionalUsesLibs,
169 UsesLibraries: d.usesLibs,
170 LibraryPaths: d.libraryPaths,
Colin Cross43f08db2018-11-12 10:13:39 -0800171
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000172 Archs: archs,
173 DexPreoptImages: images,
174 DexPreoptImagesDeps: imagesDeps,
175 DexPreoptImageLocations: bootImage.imageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800176
Nicolas Geoffray06758a72019-04-08 17:19:15 +0100177 // We use the dex paths and dex locations of the default boot image, as it
178 // contains the full dexpreopt boot classpath. Other images may just contain a subset of
179 // the dexpreopt boot classpath.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000180 PreoptBootClassPathDexFiles: defaultBootImage.dexPathsDeps.Paths(),
181 PreoptBootClassPathDexLocations: defaultBootImage.dexLocationsDeps,
Colin Cross800fe132019-02-11 14:21:24 -0800182
Colin Cross43f08db2018-11-12 10:13:39 -0800183 PreoptExtractedApk: false,
184
185 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
186 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
187
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700188 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800189 }
190
Colin Cross44df5812019-02-15 23:06:46 -0800191 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800192 if err != nil {
193 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
194 return dexJarFile
195 }
196
Colin Crossfeec25b2019-01-30 17:32:39 -0800197 dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800198
Colin Crossdeabb942019-02-11 14:11:09 -0800199 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800200
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100201 return dexJarFile
Colin Cross43f08db2018-11-12 10:13:39 -0800202}