blob: 3ef8b8dd829c951f0689903b78715d7f22d79170 [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 Trafimovich31e444e2020-08-14 17:32:16 +0100103const UnknownInstallLibraryPath = "error"
104
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100105// LibraryPath contains paths to the library DEX jar on host and on device.
106type LibraryPath struct {
107 Host android.Path
108 Device string
109}
110
111// LibraryPaths is a map from library name to on-host and on-device paths to its DEX jar.
112type LibraryPaths map[string]*LibraryPath
113
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100114// Add a new library path to the map, unless a path for this library already exists.
115func (libPaths LibraryPaths) addLibraryPath(ctx android.PathContext, lib string, hostPath, installPath android.Path) {
116 if _, present := libPaths[lib]; !present {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100117 var devicePath string
118 if installPath != nil {
119 devicePath = android.InstallPathToOnDevicePath(ctx, installPath.(android.InstallPath))
120 } else {
121 // For some stub libraries the only known thing is the name of their implementation
122 // library, but the library itself is unavailable (missing or part of a prebuilt). In
123 // such cases we still need to add the library to <uses-library> tags in the manifest,
124 // but we cannot use if for dexpreopt.
125 devicePath = UnknownInstallLibraryPath
126 }
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100127 libPaths[lib] = &LibraryPath{hostPath, devicePath}
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100128 }
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100129}
130
131// Add a new library path to the map. Ensure that the build path to the library exists.
132func (libPaths LibraryPaths) AddLibraryPath(ctx android.PathContext, lib string, hostPath, installPath android.Path) {
Ulya Trafimovich045e11a2020-09-02 16:42:03 +0100133 if hostPath != nil && installPath != nil {
134 // Add a library only if the build and install path to it is known.
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100135 libPaths.addLibraryPath(ctx, lib, hostPath, installPath)
Ulya Trafimovich045e11a2020-09-02 16:42:03 +0100136 } else if ctx.Config().AllowMissingDependencies() {
137 // If missing dependencies are allowed, the build shouldn't fail when a <uses-library> is
138 // not found. However, this is likely to result is disabling dexpreopt, as it won't be
139 // possible to construct class loader context without on-host and on-device library paths.
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100140 } else {
Ulya Trafimovich045e11a2020-09-02 16:42:03 +0100141 // Error on libraries with unknown paths.
142 if hostPath == nil {
143 android.ReportPathErrorf(ctx, "unknown build path to <uses-library> '%s'", lib)
144 } else {
145 android.ReportPathErrorf(ctx, "unknown install path to <uses-library> '%s'", lib)
146 }
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100147 }
148}
149
150// Add a new library path to the map, if the library exists (name is not nil).
151func (libPaths LibraryPaths) MaybeAddLibraryPath(ctx android.PathContext, lib *string, hostPath, installPath android.Path) {
152 if lib != nil {
153 // Don't check the build paths, add in any case. Some libraries may be missing from the
154 // build, but their names still need to be added to <uses-library> tags in the manifest.
155 libPaths.addLibraryPath(ctx, *lib, hostPath, installPath)
156 }
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100157}
158
159// Add library paths from the second map to the first map (do not override existing entries).
160func (libPaths LibraryPaths) AddLibraryPaths(otherPaths LibraryPaths) {
161 for lib, path := range otherPaths {
162 if _, present := libPaths[lib]; !present {
163 libPaths[lib] = path
164 }
165 }
166}
167
Colin Cross43f08db2018-11-12 10:13:39 -0800168type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800169 Name string
170 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800171 BuildPath android.OutputPath
172 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700173 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800174 UncompressedDex bool
175 HasApkLibraries bool
176 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800177
Colin Cross69f59a32019-02-15 10:39:37 -0800178 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800179 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100180 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800181
Ulya Trafimovich6e827482020-06-12 14:32:24 +0100182 EnforceUsesLibraries bool
183 OptionalUsesLibraries []string
184 UsesLibraries []string
185 LibraryPaths LibraryPaths
Colin Cross43f08db2018-11-12 10:13:39 -0800186
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000187 Archs []android.ArchType
188 DexPreoptImages []android.Path
189 DexPreoptImagesDeps []android.OutputPaths
190 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800191
Colin Cross69f59a32019-02-15 10:39:37 -0800192 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
193 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800194
Colin Cross43f08db2018-11-12 10:13:39 -0800195 PreoptExtractedApk bool // Overrides OnlyPreoptModules
196
197 NoCreateAppImage bool
198 ForceCreateAppImage bool
199
200 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800201}
202
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000203type globalSoongConfigSingleton struct{}
204
205var pctx = android.NewPackageContext("android/soong/dexpreopt")
206
207func init() {
208 pctx.Import("android/soong/android")
209 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
210 return &globalSoongConfigSingleton{}
211 })
212}
213
Colin Cross69f59a32019-02-15 10:39:37 -0800214func constructPath(ctx android.PathContext, path string) android.Path {
215 buildDirPrefix := ctx.Config().BuildDir() + "/"
216 if path == "" {
217 return nil
218 } else if strings.HasPrefix(path, buildDirPrefix) {
219 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
220 } else {
221 return android.PathForSource(ctx, path)
222 }
Colin Cross43f08db2018-11-12 10:13:39 -0800223}
224
Colin Cross69f59a32019-02-15 10:39:37 -0800225func constructPaths(ctx android.PathContext, paths []string) android.Paths {
226 var ret android.Paths
227 for _, path := range paths {
228 ret = append(ret, constructPath(ctx, path))
229 }
230 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800231}
232
Colin Cross69f59a32019-02-15 10:39:37 -0800233func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
234 if path == "" {
235 return nil
236 }
237 return constructPath(ctx, path).(android.WritablePath)
238}
239
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000240// ParseGlobalConfig parses the given data assumed to be read from the global
241// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000242func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800243 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000244 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800245
246 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
247 // used to construct the real value manually below.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100248 BootJars []string
249 UpdatableBootJars []string
250 ArtApexJars []string
251 UpdatableSystemServerJars []string
252 DirtyImageObjects string
253 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800254 }
255
256 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000257 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800258 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000259 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800260 }
261
262 // Construct paths that require a PathContext.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100263 config.GlobalConfig.BootJars = android.CreateConfiguredJarList(ctx, config.BootJars)
264 config.GlobalConfig.UpdatableBootJars = android.CreateConfiguredJarList(ctx, config.UpdatableBootJars)
265 config.GlobalConfig.ArtApexJars = android.CreateConfiguredJarList(ctx, config.ArtApexJars)
266 config.GlobalConfig.UpdatableSystemServerJars = android.CreateConfiguredJarList(ctx, config.UpdatableSystemServerJars)
Colin Cross69f59a32019-02-15 10:39:37 -0800267 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800268 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
269
Colin Cross988414c2020-01-11 01:11:46 +0000270 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800271}
272
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000273type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000274 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000275 data []byte
276}
277
278// GetGlobalConfig returns the global dexpreopt.config that's created in the
279// make config phase. It is loaded once the first time it is called for any
280// ctx.Config(), and returns the same data for all future calls with the same
281// ctx.Config(). A value can be inserted for tests using
282// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000283func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000284 return getGlobalConfigRaw(ctx).global
285}
286
287// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
288// the literal content of dexpreopt.config.
289func GetGlobalConfigRawData(ctx android.PathContext) []byte {
290 return getGlobalConfigRaw(ctx).data
291}
292
293var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
294var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
295
296func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
297 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
298 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
299 panic(err)
300 } else if data != nil {
301 globalConfig, err := ParseGlobalConfig(ctx, data)
302 if err != nil {
303 panic(err)
304 }
305 return globalConfigAndRaw{globalConfig, data}
306 }
307
308 // No global config filename set, see if there is a test config set
309 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
310 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000311 return globalConfigAndRaw{&GlobalConfig{
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000312 DisablePreopt: true,
313 DisableGenerateProfile: true,
314 }, nil}
315 })
316 }).(globalConfigAndRaw)
317}
318
319// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
320// will return. It must be called before the first call to GetGlobalConfig for
321// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000322func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000323 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
324}
325
326// ParseModuleConfig parses a per-module dexpreopt.config file into a
327// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
328// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
329// from Make to read the module dexpreopt.config written in the Make config
330// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000331func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100332 type jsonLibraryPath struct {
333 Host string
334 Device string
335 }
336
337 type jsonLibraryPaths map[string]jsonLibraryPath
338
Colin Cross69f59a32019-02-15 10:39:37 -0800339 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000340 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800341
342 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
343 // used to construct the real value manually below.
344 BuildPath string
345 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700346 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800347 ProfileClassListing string
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100348 LibraryPaths jsonLibraryPaths
Colin Cross69f59a32019-02-15 10:39:37 -0800349 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000350 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800351 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800352 }
353
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100354 // convert JSON map of library paths to LibraryPaths
355 constructLibraryPaths := func(ctx android.PathContext, paths jsonLibraryPaths) LibraryPaths {
356 m := LibraryPaths{}
357 for lib, path := range paths {
358 m[lib] = &LibraryPath{
359 constructPath(ctx, path.Host),
360 path.Device,
361 }
362 }
363 return m
364 }
365
Colin Cross69f59a32019-02-15 10:39:37 -0800366 config := ModuleJSONConfig{}
367
Colin Cross988414c2020-01-11 01:11:46 +0000368 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800369 if err != nil {
370 return config.ModuleConfig, err
371 }
372
373 // Construct paths that require a PathContext.
374 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
375 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700376 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800377 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100378 config.ModuleConfig.LibraryPaths = constructLibraryPaths(ctx, config.LibraryPaths)
Colin Cross69f59a32019-02-15 10:39:37 -0800379 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000380 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800381 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800382
Dan Willemsen0f416782019-06-13 21:44:53 +0000383 // 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 +0000384 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000385
Colin Cross69f59a32019-02-15 10:39:37 -0800386 return config.ModuleConfig, nil
387}
388
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000389// dex2oatModuleName returns the name of the module to use for the dex2oat host
390// tool. It should be a binary module with public visibility that is compiled
391// and installed for host.
392func dex2oatModuleName(config android.Config) string {
393 // Default to the debug variant of dex2oat to help find bugs.
394 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
395 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
396 return "dex2oat"
397 } else {
398 return "dex2oatd"
399 }
400}
401
402var dex2oatDepTag = struct {
403 blueprint.BaseDependencyTag
404}{}
405
Martin Stjernholm6d415272020-01-31 17:10:36 +0000406// RegisterToolDeps adds the necessary dependencies to binary modules for tools
407// that are required later when Get(Cached)GlobalSoongConfig is called. It
408// should be called from a mutator that's registered with
409// android.RegistrationContext.FinalDepsMutators.
410func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000411 dex2oatBin := dex2oatModuleName(ctx.Config())
412 v := ctx.Config().BuildOSTarget.Variations()
413 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
414}
415
416func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
417 dex2oatBin := dex2oatModuleName(ctx.Config())
418
Martin Stjernholmc0048622020-08-18 17:37:41 +0100419 // Find the right dex2oat module, trying to follow PrebuiltDepTag from source
420 // to prebuilt if there is one. We wouldn't have to do this if the
421 // prebuilt_postdeps mutator that replaces source deps with prebuilt deps was
422 // run after RegisterToolDeps above, but changing that leads to ordering
423 // problems between mutators (RegisterToolDeps needs to run late to act on
424 // final variants, while prebuilt_postdeps needs to run before many of the
425 // PostDeps mutators, like the APEX mutators). Hence we need to dig out the
426 // prebuilt explicitly here instead.
427 var dex2oatModule android.Module
428 ctx.WalkDeps(func(child, parent android.Module) bool {
429 if parent == ctx.Module() && ctx.OtherModuleDependencyTag(child) == dex2oatDepTag {
430 // Found the source module, or prebuilt module that has replaced the source.
431 dex2oatModule = child
432 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil {
433 return false // If it's the prebuilt we're done.
434 } else {
435 return true // Recurse to check if the source has a prebuilt dependency.
436 }
437 }
438 if parent == dex2oatModule && ctx.OtherModuleDependencyTag(child) == android.PrebuiltDepTag {
439 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil && p.Prebuilt().UsePrebuilt() {
440 dex2oatModule = child // Found a prebuilt that should be used.
441 }
442 }
443 return false
444 })
445
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000446 if dex2oatModule == nil {
447 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
448 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
449 }
450
451 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
452 if !dex2oatPath.Valid() {
453 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
454 }
455
456 return dex2oatPath.Path()
457}
458
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000459// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000460// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000461func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000462 if ctx.Config().TestProductVariables != nil {
463 // If we're called in a test there'll be a confusing error from the path
464 // functions below that gets reported without a stack trace, so let's panic
465 // properly with a more helpful message.
466 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
467 }
468
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000469 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000470 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000471 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000472 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
473 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
474 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
475 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100476 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000477 }
478}
479
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000480// The main reason for this Once cache for GlobalSoongConfig is to make the
481// dex2oat path available to singletons. In ordinary modules we get it through a
482// dex2oatDepTag dependency, but in singletons there's no simple way to do the
483// same thing and ensure the right variant is selected, hence this cache to make
484// the resolved path available to singletons. This means we depend on there
485// being at least one ordinary module with a dex2oatDepTag dependency.
486//
487// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
488// and then possibly remove this cache altogether (but the use in
489// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000490var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
491
492// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
493// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000494func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000495 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
496 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000497 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000498
499 // Always resolve the tool path from the dependency, to ensure that every
500 // module has the dependency added properly.
501 myDex2oat := dex2oatPathFromDep(ctx)
502 if myDex2oat != globalSoong.Dex2oat {
503 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
504 }
505
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000506 return globalSoong
507}
508
509// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
510// earlier GetGlobalSoongConfig call. This function works with any context
511// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000512// GlobalSoongConfig with the proper paths (which requires a full
513// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
514// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000515func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000516 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
517 return (*GlobalSoongConfig)(nil)
518 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000519}
520
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000521type globalJsonSoongConfig struct {
522 Profman string
523 Dex2oat string
524 Aapt string
525 SoongZip string
526 Zip2zip string
527 ManifestCheck string
528 ConstructContext string
529}
530
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000531// ParseGlobalSoongConfig parses the given data assumed to be read from the
532// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
533// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000534func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000535 var jc globalJsonSoongConfig
536
Colin Cross988414c2020-01-11 01:11:46 +0000537 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000538 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000539 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000540 }
541
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000542 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000543 Profman: constructPath(ctx, jc.Profman),
544 Dex2oat: constructPath(ctx, jc.Dex2oat),
545 Aapt: constructPath(ctx, jc.Aapt),
546 SoongZip: constructPath(ctx, jc.SoongZip),
547 Zip2zip: constructPath(ctx, jc.Zip2zip),
548 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
549 ConstructContext: constructPath(ctx, jc.ConstructContext),
550 }
551
552 return config, nil
553}
554
555func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000556 if GetGlobalConfig(ctx).DisablePreopt {
557 return
558 }
559
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000560 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000561 if config == nil {
562 // No module has enabled dexpreopting, so we assume there will be no calls
563 // to dexpreopt_gen.
564 return
565 }
566
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000567 jc := globalJsonSoongConfig{
568 Profman: config.Profman.String(),
569 Dex2oat: config.Dex2oat.String(),
570 Aapt: config.Aapt.String(),
571 SoongZip: config.SoongZip.String(),
572 Zip2zip: config.Zip2zip.String(),
573 ManifestCheck: config.ManifestCheck.String(),
574 ConstructContext: config.ConstructContext.String(),
575 }
576
577 data, err := json.Marshal(jc)
578 if err != nil {
579 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
580 return
581 }
582
583 ctx.Build(pctx, android.BuildParams{
584 Rule: android.WriteFile,
585 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
586 Args: map[string]string{
587 "content": string(data),
588 },
589 })
590}
591
592func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000593 if GetGlobalConfig(ctx).DisablePreopt {
594 return
595 }
596
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000597 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000598 if config == nil {
599 return
600 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000601
602 ctx.Strict("DEX2OAT", config.Dex2oat.String())
603 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
604 config.Profman.String(),
605 config.Dex2oat.String(),
606 config.Aapt.String(),
607 config.SoongZip.String(),
608 config.Zip2zip.String(),
609 config.ManifestCheck.String(),
610 config.ConstructContext.String(),
611 }, " "))
612}
613
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000614func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
615 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800616 DisablePreopt: false,
617 DisablePreoptModules: nil,
618 OnlyPreoptBootImageAndSystemServer: false,
619 HasSystemOther: false,
620 PatternsOnSystemOther: nil,
621 DisableGenerateProfile: false,
622 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100623 BootJars: android.EmptyConfiguredJarList(),
624 UpdatableBootJars: android.EmptyConfiguredJarList(),
625 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800626 SystemServerJars: nil,
627 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100628 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800629 SpeedApps: nil,
630 PreoptFlags: nil,
631 DefaultCompilerFilter: "",
632 SystemServerCompilerFilter: "",
633 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800634 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700635 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800636 AlwaysSystemServerDebugInfo: false,
637 NeverSystemServerDebugInfo: false,
638 AlwaysOtherDebugInfo: false,
639 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800640 IsEng: false,
641 SanitizeLite: false,
642 DefaultAppImages: false,
643 Dex2oatXmx: "",
644 Dex2oatXms: "",
645 EmptyDirectory: "empty_dir",
646 CpuVariant: nil,
647 InstructionSetFeatures: nil,
648 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800649 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800650 BootFlags: "",
651 Dex2oatImageXmx: "",
652 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000653 }
654}
655
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000656func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000657 // Install the test GlobalSoongConfig in the Once cache so that later calls to
658 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
659 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000660 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700661 Profman: android.PathForTesting("profman"),
662 Dex2oat: android.PathForTesting("dex2oat"),
663 Aapt: android.PathForTesting("aapt"),
664 SoongZip: android.PathForTesting("soong_zip"),
665 Zip2zip: android.PathForTesting("zip2zip"),
666 ManifestCheck: android.PathForTesting("manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100667 ConstructContext: android.PathForTesting("construct_context"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000668 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000669 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800670}