blob: 21f7bb3eef4309dd6652303106343e1a4e5ba9b5 [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 dexpreopt
16
17import (
18 "encoding/json"
Martin Stjernholmd90676f2020-01-11 00:37:30 +000019 "fmt"
Colin Cross69f59a32019-02-15 10:39:37 -080020 "strings"
Colin Cross74ba9622019-02-11 15:11:14 -080021
Martin Stjernholmd90676f2020-01-11 00:37:30 +000022 "github.com/google/blueprint"
23
Colin Cross74ba9622019-02-11 15:11:14 -080024 "android/soong/android"
Colin Cross43f08db2018-11-12 10:13:39 -080025)
26
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000027// GlobalConfig stores the configuration for dex preopting. The fields are set
Martin Stjernholm75a48d82020-01-10 20:32:59 +000028// from product variables via dex_preopt_config.mk.
Colin Cross43f08db2018-11-12 10:13:39 -080029type GlobalConfig struct {
Colin Cross69f59a32019-02-15 10:39:37 -080030 DisablePreopt bool // disable preopt for all modules
Colin Cross43f08db2018-11-12 10:13:39 -080031 DisablePreoptModules []string // modules with preopt disabled by product-specific config
32
33 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
34
Vladimir Marko40139d62020-02-06 15:14:29 +000035 UseArtImage bool // use the art image (use other boot class path dex files without image)
36
Colin Cross43f08db2018-11-12 10:13:39 -080037 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
38 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
39
Colin Cross69f59a32019-02-15 10:39:37 -080040 DisableGenerateProfile bool // don't generate profiles
41 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080042
Ulya Trafimovich249386a2020-07-01 14:31:13 +010043 BootJars android.ConfiguredJarList // modules for jars that form the boot class path
44 UpdatableBootJars android.ConfiguredJarList // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000045
Ulya Trafimovich249386a2020-07-01 14:31:13 +010046 ArtApexJars android.ConfiguredJarList // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080047
Ulya Trafimovich249386a2020-07-01 14:31:13 +010048 SystemServerJars []string // jars that form the system server
49 SystemServerApps []string // apps that are loaded into system server
50 UpdatableSystemServerJars android.ConfiguredJarList // jars within apex that are loaded into system server
51 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080052
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +000053 BrokenSuboptimalOrderOfSystemServerJars bool // if true, sub-optimal order does not cause a build error
54
Colin Cross43f08db2018-11-12 10:13:39 -080055 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
56
57 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
58 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
59
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010060 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080061
62 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070063 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080064 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
65 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
66 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
67 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
68
Colin Cross43f08db2018-11-12 10:13:39 -080069 IsEng bool // build is a eng variant
70 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
71
72 DefaultAppImages bool // build app images (TODO: .art files?) by default
73
Colin Cross800fe132019-02-11 14:21:24 -080074 Dex2oatXmx string // max heap size for dex2oat
75 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080076
77 EmptyDirectory string // path to an empty directory
78
Colin Cross74ba9622019-02-11 15:11:14 -080079 CpuVariant map[android.ArchType]string // cpu variant for each architecture
80 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080081
Colin Cross800fe132019-02-11 14:21:24 -080082 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070083 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
84 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
85 BootFlags string // extra flags to pass to dex2oat for the boot image
86 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
87 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross43f08db2018-11-12 10:13:39 -080088}
89
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000090// GlobalSoongConfig contains the global config that is generated from Soong,
91// stored in dexpreopt_soong.config.
92type GlobalSoongConfig struct {
93 // Paths to tools possibly used by the generated commands.
94 Profman android.Path
95 Dex2oat android.Path
96 Aapt android.Path
97 SoongZip android.Path
98 Zip2zip android.Path
99 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700100 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800101}
102
Ulya Trafimoviche3bb01d2020-09-11 12:17:29 +0100103// These libs are added as optional dependencies (<uses-library> with android:required set to false).
104// This is because they haven't existed prior to certain SDK version, but classes in them were in
105// bootclasspath jars, etc. So making them hard dependencies (android:required=true) would prevent
106// apps from being installed to such legacy devices.
Ulya Trafimovich663dc532020-09-10 12:48:53 +0100107var OptionalCompatUsesLibs = []string{
Ulya Trafimoviche3bb01d2020-09-11 12:17:29 +0100108 "org.apache.http.legacy",
Ulya Trafimovich663dc532020-09-10 12:48:53 +0100109 "android.test.base",
110 "android.test.mock",
111}
112
113var CompatUsesLibs = []string{
Ulya Trafimovich663dc532020-09-10 12:48:53 +0100114 "android.hidl.base-V1.0-java",
115 "android.hidl.manager-V1.0-java",
116}
117
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100118const UnknownInstallLibraryPath = "error"
119
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100120// LibraryPath contains paths to the library DEX jar on host and on device.
121type LibraryPath struct {
122 Host android.Path
123 Device string
124}
125
126// LibraryPaths is a map from library name to on-host and on-device paths to its DEX jar.
127type LibraryPaths map[string]*LibraryPath
128
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100129// Add a new library path to the map, unless a path for this library already exists.
Ulya Trafimovich663dc532020-09-10 12:48:53 +0100130// If necessary, check that the build and install paths exist.
131func (libPaths LibraryPaths) addLibraryPath(ctx android.ModuleContext, lib string,
132 hostPath, installPath android.Path, strict bool) {
133
134 // If missing dependencies are allowed, the build shouldn't fail when a <uses-library> is
135 // not found. However, this is likely to result is disabling dexpreopt, as it won't be
136 // possible to construct class loader context without on-host and on-device library paths.
137 strict = strict && !ctx.Config().AllowMissingDependencies()
138
139 if hostPath == nil && strict {
140 android.ReportPathErrorf(ctx, "unknown build path to <uses-library> '%s'", lib)
141 }
142
143 if installPath == nil {
144 if android.InList(lib, CompatUsesLibs) || android.InList(lib, OptionalCompatUsesLibs) {
145 // Assume that compatibility libraries are installed in /system/framework.
146 installPath = android.PathForModuleInstall(ctx, "framework", lib+".jar")
147 } else if strict {
148 android.ReportPathErrorf(ctx, "unknown install path to <uses-library> '%s'", lib)
149 }
150 }
151
152 // Add a library only if the build and install path to it is known.
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100153 if _, present := libPaths[lib]; !present {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100154 var devicePath string
155 if installPath != nil {
156 devicePath = android.InstallPathToOnDevicePath(ctx, installPath.(android.InstallPath))
157 } else {
158 // For some stub libraries the only known thing is the name of their implementation
159 // library, but the library itself is unavailable (missing or part of a prebuilt). In
160 // such cases we still need to add the library to <uses-library> tags in the manifest,
161 // but we cannot use if for dexpreopt.
162 devicePath = UnknownInstallLibraryPath
163 }
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100164 libPaths[lib] = &LibraryPath{hostPath, devicePath}
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100165 }
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100166}
167
Ulya Trafimovich663dc532020-09-10 12:48:53 +0100168// Add a new library path to the map. Enforce checks that the library paths exist.
169func (libPaths LibraryPaths) AddLibraryPath(ctx android.ModuleContext, lib string, hostPath, installPath android.Path) {
170 libPaths.addLibraryPath(ctx, lib, hostPath, installPath, true)
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100171}
172
173// Add a new library path to the map, if the library exists (name is not nil).
Ulya Trafimovich663dc532020-09-10 12:48:53 +0100174// Don't enforce checks that the library paths exist. Some libraries may be missing from the build,
175// but their names still need to be added to <uses-library> tags in the manifest.
176func (libPaths LibraryPaths) MaybeAddLibraryPath(ctx android.ModuleContext, lib *string, hostPath, installPath android.Path) {
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100177 if lib != nil {
Ulya Trafimovich663dc532020-09-10 12:48:53 +0100178 libPaths.addLibraryPath(ctx, *lib, hostPath, installPath, false)
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100179 }
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100180}
181
182// Add library paths from the second map to the first map (do not override existing entries).
183func (libPaths LibraryPaths) AddLibraryPaths(otherPaths LibraryPaths) {
184 for lib, path := range otherPaths {
185 if _, present := libPaths[lib]; !present {
186 libPaths[lib] = path
187 }
188 }
189}
190
Colin Cross43f08db2018-11-12 10:13:39 -0800191type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800192 Name string
193 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800194 BuildPath android.OutputPath
195 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700196 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800197 UncompressedDex bool
198 HasApkLibraries bool
199 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800200
Colin Cross69f59a32019-02-15 10:39:37 -0800201 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800202 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100203 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800204
Ulya Trafimovich6e827482020-06-12 14:32:24 +0100205 EnforceUsesLibraries bool
206 OptionalUsesLibraries []string
207 UsesLibraries []string
208 LibraryPaths LibraryPaths
Colin Cross43f08db2018-11-12 10:13:39 -0800209
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000210 Archs []android.ArchType
211 DexPreoptImages []android.Path
212 DexPreoptImagesDeps []android.OutputPaths
213 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800214
Colin Cross69f59a32019-02-15 10:39:37 -0800215 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
216 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800217
Colin Cross43f08db2018-11-12 10:13:39 -0800218 PreoptExtractedApk bool // Overrides OnlyPreoptModules
219
220 NoCreateAppImage bool
221 ForceCreateAppImage bool
222
223 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800224}
225
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000226type globalSoongConfigSingleton struct{}
227
228var pctx = android.NewPackageContext("android/soong/dexpreopt")
229
230func init() {
231 pctx.Import("android/soong/android")
232 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
233 return &globalSoongConfigSingleton{}
234 })
235}
236
Colin Cross69f59a32019-02-15 10:39:37 -0800237func constructPath(ctx android.PathContext, path string) android.Path {
238 buildDirPrefix := ctx.Config().BuildDir() + "/"
239 if path == "" {
240 return nil
241 } else if strings.HasPrefix(path, buildDirPrefix) {
242 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
243 } else {
244 return android.PathForSource(ctx, path)
245 }
Colin Cross43f08db2018-11-12 10:13:39 -0800246}
247
Colin Cross69f59a32019-02-15 10:39:37 -0800248func constructPaths(ctx android.PathContext, paths []string) android.Paths {
249 var ret android.Paths
250 for _, path := range paths {
251 ret = append(ret, constructPath(ctx, path))
252 }
253 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800254}
255
Colin Cross69f59a32019-02-15 10:39:37 -0800256func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
257 if path == "" {
258 return nil
259 }
260 return constructPath(ctx, path).(android.WritablePath)
261}
262
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000263// ParseGlobalConfig parses the given data assumed to be read from the global
264// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000265func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800266 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000267 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800268
269 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
270 // used to construct the real value manually below.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100271 BootJars []string
272 UpdatableBootJars []string
273 ArtApexJars []string
274 UpdatableSystemServerJars []string
275 DirtyImageObjects string
276 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800277 }
278
279 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000280 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800281 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000282 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800283 }
284
285 // Construct paths that require a PathContext.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100286 config.GlobalConfig.BootJars = android.CreateConfiguredJarList(ctx, config.BootJars)
287 config.GlobalConfig.UpdatableBootJars = android.CreateConfiguredJarList(ctx, config.UpdatableBootJars)
288 config.GlobalConfig.ArtApexJars = android.CreateConfiguredJarList(ctx, config.ArtApexJars)
289 config.GlobalConfig.UpdatableSystemServerJars = android.CreateConfiguredJarList(ctx, config.UpdatableSystemServerJars)
Colin Cross69f59a32019-02-15 10:39:37 -0800290 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800291 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
292
Colin Cross988414c2020-01-11 01:11:46 +0000293 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800294}
295
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000296type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000297 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000298 data []byte
299}
300
301// GetGlobalConfig returns the global dexpreopt.config that's created in the
302// make config phase. It is loaded once the first time it is called for any
303// ctx.Config(), and returns the same data for all future calls with the same
304// ctx.Config(). A value can be inserted for tests using
305// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000306func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000307 return getGlobalConfigRaw(ctx).global
308}
309
310// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
311// the literal content of dexpreopt.config.
312func GetGlobalConfigRawData(ctx android.PathContext) []byte {
313 return getGlobalConfigRaw(ctx).data
314}
315
316var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
317var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
318
319func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
320 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
321 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
322 panic(err)
323 } else if data != nil {
324 globalConfig, err := ParseGlobalConfig(ctx, data)
325 if err != nil {
326 panic(err)
327 }
328 return globalConfigAndRaw{globalConfig, data}
329 }
330
331 // No global config filename set, see if there is a test config set
332 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
333 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000334 return globalConfigAndRaw{&GlobalConfig{
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000335 DisablePreopt: true,
336 DisableGenerateProfile: true,
337 }, nil}
338 })
339 }).(globalConfigAndRaw)
340}
341
342// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
343// will return. It must be called before the first call to GetGlobalConfig for
344// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000345func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000346 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
347}
348
349// ParseModuleConfig parses a per-module dexpreopt.config file into a
350// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
351// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
352// from Make to read the module dexpreopt.config written in the Make config
353// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000354func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100355 type jsonLibraryPath struct {
356 Host string
357 Device string
358 }
359
360 type jsonLibraryPaths map[string]jsonLibraryPath
361
Colin Cross69f59a32019-02-15 10:39:37 -0800362 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000363 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800364
365 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
366 // used to construct the real value manually below.
367 BuildPath string
368 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700369 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800370 ProfileClassListing string
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100371 LibraryPaths jsonLibraryPaths
Colin Cross69f59a32019-02-15 10:39:37 -0800372 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000373 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800374 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800375 }
376
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100377 // convert JSON map of library paths to LibraryPaths
378 constructLibraryPaths := func(ctx android.PathContext, paths jsonLibraryPaths) LibraryPaths {
379 m := LibraryPaths{}
380 for lib, path := range paths {
381 m[lib] = &LibraryPath{
382 constructPath(ctx, path.Host),
383 path.Device,
384 }
385 }
386 return m
387 }
388
Colin Cross69f59a32019-02-15 10:39:37 -0800389 config := ModuleJSONConfig{}
390
Colin Cross988414c2020-01-11 01:11:46 +0000391 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800392 if err != nil {
393 return config.ModuleConfig, err
394 }
395
396 // Construct paths that require a PathContext.
397 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
398 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700399 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800400 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100401 config.ModuleConfig.LibraryPaths = constructLibraryPaths(ctx, config.LibraryPaths)
Colin Cross69f59a32019-02-15 10:39:37 -0800402 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000403 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800404 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800405
Dan Willemsen0f416782019-06-13 21:44:53 +0000406 // This needs to exist, but dependencies are already handled in Make, so we don't need to pass them through JSON.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000407 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000408
Colin Cross69f59a32019-02-15 10:39:37 -0800409 return config.ModuleConfig, nil
410}
411
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000412// dex2oatModuleName returns the name of the module to use for the dex2oat host
413// tool. It should be a binary module with public visibility that is compiled
414// and installed for host.
415func dex2oatModuleName(config android.Config) string {
416 // Default to the debug variant of dex2oat to help find bugs.
417 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
418 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
419 return "dex2oat"
420 } else {
421 return "dex2oatd"
422 }
423}
424
425var dex2oatDepTag = struct {
426 blueprint.BaseDependencyTag
427}{}
428
Martin Stjernholm6d415272020-01-31 17:10:36 +0000429// RegisterToolDeps adds the necessary dependencies to binary modules for tools
430// that are required later when Get(Cached)GlobalSoongConfig is called. It
431// should be called from a mutator that's registered with
432// android.RegistrationContext.FinalDepsMutators.
433func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000434 dex2oatBin := dex2oatModuleName(ctx.Config())
435 v := ctx.Config().BuildOSTarget.Variations()
436 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
437}
438
439func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
440 dex2oatBin := dex2oatModuleName(ctx.Config())
441
Martin Stjernholmc0048622020-08-18 17:37:41 +0100442 // Find the right dex2oat module, trying to follow PrebuiltDepTag from source
443 // to prebuilt if there is one. We wouldn't have to do this if the
444 // prebuilt_postdeps mutator that replaces source deps with prebuilt deps was
445 // run after RegisterToolDeps above, but changing that leads to ordering
446 // problems between mutators (RegisterToolDeps needs to run late to act on
447 // final variants, while prebuilt_postdeps needs to run before many of the
448 // PostDeps mutators, like the APEX mutators). Hence we need to dig out the
449 // prebuilt explicitly here instead.
450 var dex2oatModule android.Module
451 ctx.WalkDeps(func(child, parent android.Module) bool {
452 if parent == ctx.Module() && ctx.OtherModuleDependencyTag(child) == dex2oatDepTag {
453 // Found the source module, or prebuilt module that has replaced the source.
454 dex2oatModule = child
455 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil {
456 return false // If it's the prebuilt we're done.
457 } else {
458 return true // Recurse to check if the source has a prebuilt dependency.
459 }
460 }
461 if parent == dex2oatModule && ctx.OtherModuleDependencyTag(child) == android.PrebuiltDepTag {
462 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil && p.Prebuilt().UsePrebuilt() {
463 dex2oatModule = child // Found a prebuilt that should be used.
464 }
465 }
466 return false
467 })
468
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000469 if dex2oatModule == nil {
470 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
471 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
472 }
473
474 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
475 if !dex2oatPath.Valid() {
476 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
477 }
478
479 return dex2oatPath.Path()
480}
481
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000482// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000483// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000484func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000485 if ctx.Config().TestProductVariables != nil {
486 // If we're called in a test there'll be a confusing error from the path
487 // functions below that gets reported without a stack trace, so let's panic
488 // properly with a more helpful message.
489 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
490 }
491
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000492 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000493 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000494 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000495 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
496 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
497 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
498 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100499 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000500 }
501}
502
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000503// The main reason for this Once cache for GlobalSoongConfig is to make the
504// dex2oat path available to singletons. In ordinary modules we get it through a
505// dex2oatDepTag dependency, but in singletons there's no simple way to do the
506// same thing and ensure the right variant is selected, hence this cache to make
507// the resolved path available to singletons. This means we depend on there
508// being at least one ordinary module with a dex2oatDepTag dependency.
509//
510// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
511// and then possibly remove this cache altogether (but the use in
512// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000513var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
514
515// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
516// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000517func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000518 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
519 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000520 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000521
522 // Always resolve the tool path from the dependency, to ensure that every
523 // module has the dependency added properly.
524 myDex2oat := dex2oatPathFromDep(ctx)
525 if myDex2oat != globalSoong.Dex2oat {
526 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
527 }
528
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000529 return globalSoong
530}
531
532// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
533// earlier GetGlobalSoongConfig call. This function works with any context
534// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000535// GlobalSoongConfig with the proper paths (which requires a full
536// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
537// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000538func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000539 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
540 return (*GlobalSoongConfig)(nil)
541 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000542}
543
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000544type globalJsonSoongConfig struct {
545 Profman string
546 Dex2oat string
547 Aapt string
548 SoongZip string
549 Zip2zip string
550 ManifestCheck string
551 ConstructContext string
552}
553
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000554// ParseGlobalSoongConfig parses the given data assumed to be read from the
555// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
556// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000557func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000558 var jc globalJsonSoongConfig
559
Colin Cross988414c2020-01-11 01:11:46 +0000560 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000561 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000562 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000563 }
564
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000565 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000566 Profman: constructPath(ctx, jc.Profman),
567 Dex2oat: constructPath(ctx, jc.Dex2oat),
568 Aapt: constructPath(ctx, jc.Aapt),
569 SoongZip: constructPath(ctx, jc.SoongZip),
570 Zip2zip: constructPath(ctx, jc.Zip2zip),
571 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
572 ConstructContext: constructPath(ctx, jc.ConstructContext),
573 }
574
575 return config, nil
576}
577
578func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000579 if GetGlobalConfig(ctx).DisablePreopt {
580 return
581 }
582
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000583 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000584 if config == nil {
585 // No module has enabled dexpreopting, so we assume there will be no calls
586 // to dexpreopt_gen.
587 return
588 }
589
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000590 jc := globalJsonSoongConfig{
591 Profman: config.Profman.String(),
592 Dex2oat: config.Dex2oat.String(),
593 Aapt: config.Aapt.String(),
594 SoongZip: config.SoongZip.String(),
595 Zip2zip: config.Zip2zip.String(),
596 ManifestCheck: config.ManifestCheck.String(),
597 ConstructContext: config.ConstructContext.String(),
598 }
599
600 data, err := json.Marshal(jc)
601 if err != nil {
602 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
603 return
604 }
605
606 ctx.Build(pctx, android.BuildParams{
607 Rule: android.WriteFile,
608 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
609 Args: map[string]string{
610 "content": string(data),
611 },
612 })
613}
614
615func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000616 if GetGlobalConfig(ctx).DisablePreopt {
617 return
618 }
619
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000620 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000621 if config == nil {
622 return
623 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000624
625 ctx.Strict("DEX2OAT", config.Dex2oat.String())
626 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
627 config.Profman.String(),
628 config.Dex2oat.String(),
629 config.Aapt.String(),
630 config.SoongZip.String(),
631 config.Zip2zip.String(),
632 config.ManifestCheck.String(),
633 config.ConstructContext.String(),
634 }, " "))
635}
636
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000637func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
638 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800639 DisablePreopt: false,
640 DisablePreoptModules: nil,
641 OnlyPreoptBootImageAndSystemServer: false,
642 HasSystemOther: false,
643 PatternsOnSystemOther: nil,
644 DisableGenerateProfile: false,
645 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100646 BootJars: android.EmptyConfiguredJarList(),
647 UpdatableBootJars: android.EmptyConfiguredJarList(),
648 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800649 SystemServerJars: nil,
650 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100651 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800652 SpeedApps: nil,
653 PreoptFlags: nil,
654 DefaultCompilerFilter: "",
655 SystemServerCompilerFilter: "",
656 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800657 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700658 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800659 AlwaysSystemServerDebugInfo: false,
660 NeverSystemServerDebugInfo: false,
661 AlwaysOtherDebugInfo: false,
662 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800663 IsEng: false,
664 SanitizeLite: false,
665 DefaultAppImages: false,
666 Dex2oatXmx: "",
667 Dex2oatXms: "",
668 EmptyDirectory: "empty_dir",
669 CpuVariant: nil,
670 InstructionSetFeatures: nil,
671 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800672 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800673 BootFlags: "",
674 Dex2oatImageXmx: "",
675 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000676 }
677}
678
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000679func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000680 // Install the test GlobalSoongConfig in the Once cache so that later calls to
681 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
682 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000683 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700684 Profman: android.PathForTesting("profman"),
685 Dex2oat: android.PathForTesting("dex2oat"),
686 Aapt: android.PathForTesting("aapt"),
687 SoongZip: android.PathForTesting("soong_zip"),
688 Zip2zip: android.PathForTesting("zip2zip"),
689 ManifestCheck: android.PathForTesting("manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100690 ConstructContext: android.PathForTesting("construct_context"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000691 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000692 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800693}