blob: 830a1e78c8de1133bde16a873a997f3049bc524d [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 {
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +000030 DisablePreopt bool // disable preopt for all modules (excluding boot images)
31 DisablePreoptBootImages bool // disable prepot for boot images
32 DisablePreoptModules []string // modules with preopt disabled by product-specific config
Colin Cross43f08db2018-11-12 10:13:39 -080033
34 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
35
Ulya Trafimovich9023b022021-03-22 16:02:28 +000036 PreoptWithUpdatableBcp bool // If updatable boot jars are included in dexpreopt or not.
37
Vladimir Marko40139d62020-02-06 15:14:29 +000038 UseArtImage bool // use the art image (use other boot class path dex files without image)
39
Colin Cross43f08db2018-11-12 10:13:39 -080040 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
41 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
42
Colin Cross69f59a32019-02-15 10:39:37 -080043 DisableGenerateProfile bool // don't generate profiles
44 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080045
Ulya Trafimovich249386a2020-07-01 14:31:13 +010046 BootJars android.ConfiguredJarList // modules for jars that form the boot class path
47 UpdatableBootJars android.ConfiguredJarList // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000048
Ulya Trafimovich249386a2020-07-01 14:31:13 +010049 ArtApexJars android.ConfiguredJarList // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080050
Ulya Trafimovich249386a2020-07-01 14:31:13 +010051 SystemServerJars []string // jars that form the system server
52 SystemServerApps []string // apps that are loaded into system server
53 UpdatableSystemServerJars android.ConfiguredJarList // jars within apex that are loaded into system server
54 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080055
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +000056 BrokenSuboptimalOrderOfSystemServerJars bool // if true, sub-optimal order does not cause a build error
57
Colin Cross43f08db2018-11-12 10:13:39 -080058 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
59
60 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
61 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
62
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010063 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080064
65 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070066 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080067 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
68 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
69 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
70 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
71
Colin Cross43f08db2018-11-12 10:13:39 -080072 IsEng bool // build is a eng variant
73 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
74
75 DefaultAppImages bool // build app images (TODO: .art files?) by default
76
Colin Cross800fe132019-02-11 14:21:24 -080077 Dex2oatXmx string // max heap size for dex2oat
78 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080079
80 EmptyDirectory string // path to an empty directory
81
Colin Cross74ba9622019-02-11 15:11:14 -080082 CpuVariant map[android.ArchType]string // cpu variant for each architecture
83 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080084
Nicolas Geoffray1086e602021-01-20 14:30:40 +000085 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
86 BootFlags string // extra flags to pass to dex2oat for the boot image
87 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
88 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000089
Ulya Trafimovich4a13acb2021-03-02 12:25:02 +000090 // If true, downgrade the compiler filter of dexpreopt to "verify" when verify_uses_libraries
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000091 // check fails, instead of failing the build. This will disable any AOT-compilation.
92 //
93 // The intended use case for this flag is to have a smoother migration path for the Java
94 // modules that need to add <uses-library> information in their build files. The flag allows to
95 // quickly silence build errors. This flag should be used with caution and only as a temporary
96 // measure, as it masks real errors and affects performance.
97 RelaxUsesLibraryCheck bool
Colin Cross43f08db2018-11-12 10:13:39 -080098}
99
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000100// GlobalSoongConfig contains the global config that is generated from Soong,
101// stored in dexpreopt_soong.config.
102type GlobalSoongConfig struct {
103 // Paths to tools possibly used by the generated commands.
104 Profman android.Path
105 Dex2oat android.Path
106 Aapt android.Path
107 SoongZip android.Path
108 Zip2zip android.Path
109 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700110 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800111}
112
113type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800114 Name string
115 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800116 BuildPath android.OutputPath
117 DexPath android.Path
Jeongik Cha33a3a812021-04-15 09:12:49 +0900118 ManifestPath android.OptionalPath
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800119 UncompressedDex bool
120 HasApkLibraries bool
121 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800122
Colin Cross69f59a32019-02-15 10:39:37 -0800123 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800124 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100125 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800126
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000127 EnforceUsesLibraries bool // turn on build-time verify_uses_libraries check
128 EnforceUsesLibrariesStatusFile android.Path // a file with verify_uses_libraries errors (if any)
129 ProvidesUsesLibrary string // library name (usually the same as module name)
130 ClassLoaderContexts ClassLoaderContextMap
Colin Cross43f08db2018-11-12 10:13:39 -0800131
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000132 Archs []android.ArchType
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000133 DexPreoptImagesDeps []android.OutputPaths
134 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800135
Colin Cross69f59a32019-02-15 10:39:37 -0800136 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
137 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800138
Colin Cross43f08db2018-11-12 10:13:39 -0800139 PreoptExtractedApk bool // Overrides OnlyPreoptModules
140
141 NoCreateAppImage bool
142 ForceCreateAppImage bool
143
144 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800145}
146
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000147type globalSoongConfigSingleton struct{}
148
149var pctx = android.NewPackageContext("android/soong/dexpreopt")
150
151func init() {
152 pctx.Import("android/soong/android")
153 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
154 return &globalSoongConfigSingleton{}
155 })
156}
157
Colin Cross69f59a32019-02-15 10:39:37 -0800158func constructPath(ctx android.PathContext, path string) android.Path {
159 buildDirPrefix := ctx.Config().BuildDir() + "/"
160 if path == "" {
161 return nil
162 } else if strings.HasPrefix(path, buildDirPrefix) {
163 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
164 } else {
165 return android.PathForSource(ctx, path)
166 }
Colin Cross43f08db2018-11-12 10:13:39 -0800167}
168
Colin Cross69f59a32019-02-15 10:39:37 -0800169func constructPaths(ctx android.PathContext, paths []string) android.Paths {
170 var ret android.Paths
171 for _, path := range paths {
172 ret = append(ret, constructPath(ctx, path))
173 }
174 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800175}
176
Colin Cross69f59a32019-02-15 10:39:37 -0800177func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
178 if path == "" {
179 return nil
180 }
181 return constructPath(ctx, path).(android.WritablePath)
182}
183
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000184// ParseGlobalConfig parses the given data assumed to be read from the global
185// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000186func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800187 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000188 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800189
190 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
191 // used to construct the real value manually below.
Paul Duffin7ccacae2020-10-23 21:14:20 +0100192 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800193 }
194
195 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000196 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800197 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000198 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800199 }
200
201 // Construct paths that require a PathContext.
Colin Cross69f59a32019-02-15 10:39:37 -0800202 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
203
Colin Cross988414c2020-01-11 01:11:46 +0000204 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800205}
206
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000207type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000208 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000209 data []byte
210}
211
212// GetGlobalConfig returns the global dexpreopt.config that's created in the
213// make config phase. It is loaded once the first time it is called for any
214// ctx.Config(), and returns the same data for all future calls with the same
215// ctx.Config(). A value can be inserted for tests using
216// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000217func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000218 return getGlobalConfigRaw(ctx).global
219}
220
221// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
222// the literal content of dexpreopt.config.
223func GetGlobalConfigRawData(ctx android.PathContext) []byte {
224 return getGlobalConfigRaw(ctx).data
225}
226
227var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
228var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
229
230func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
231 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
232 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
233 panic(err)
234 } else if data != nil {
235 globalConfig, err := ParseGlobalConfig(ctx, data)
236 if err != nil {
237 panic(err)
238 }
239 return globalConfigAndRaw{globalConfig, data}
240 }
241
242 // No global config filename set, see if there is a test config set
243 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
244 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000245 return globalConfigAndRaw{&GlobalConfig{
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000246 DisablePreopt: true,
247 DisablePreoptBootImages: true,
248 DisableGenerateProfile: true,
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000249 }, nil}
250 })
251 }).(globalConfigAndRaw)
252}
253
254// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
255// will return. It must be called before the first call to GetGlobalConfig for
256// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000257func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000258 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
259}
260
Jeongik Chac6246672021-04-08 00:00:19 +0900261// This struct is required to convert ModuleConfig from/to JSON.
262// The types of fields in ModuleConfig are not convertible,
263// so moduleJSONConfig has those fields as a convertible type.
264type moduleJSONConfig struct {
265 *ModuleConfig
266
267 BuildPath string
268 DexPath string
269 ManifestPath string
270
271 ProfileClassListing string
272 ProfileBootListing string
273
274 EnforceUsesLibrariesStatusFile string
275 ClassLoaderContexts jsonClassLoaderContextMap
276
Jeongik Chac6246672021-04-08 00:00:19 +0900277 DexPreoptImagesDeps [][]string
278
279 PreoptBootClassPathDexFiles []string
280}
281
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000282// ParseModuleConfig parses a per-module dexpreopt.config file into a
283// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
284// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
285// from Make to read the module dexpreopt.config written in the Make config
286// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000287func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Jeongik Chac6246672021-04-08 00:00:19 +0900288 config := moduleJSONConfig{}
Colin Cross69f59a32019-02-15 10:39:37 -0800289
Colin Cross988414c2020-01-11 01:11:46 +0000290 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800291 if err != nil {
292 return config.ModuleConfig, err
293 }
294
295 // Construct paths that require a PathContext.
296 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
297 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Jeongik Cha33a3a812021-04-15 09:12:49 +0900298 config.ModuleConfig.ManifestPath = android.OptionalPathForPath(constructPath(ctx, config.ManifestPath))
Colin Cross69f59a32019-02-15 10:39:37 -0800299 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000300 config.ModuleConfig.EnforceUsesLibrariesStatusFile = constructPath(ctx, config.EnforceUsesLibrariesStatusFile)
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000301 config.ModuleConfig.ClassLoaderContexts = fromJsonClassLoaderContext(ctx, config.ClassLoaderContexts)
Colin Cross69f59a32019-02-15 10:39:37 -0800302 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800303
Dan Willemsen0f416782019-06-13 21:44:53 +0000304 // This needs to exist, but dependencies are already handled in Make, so we don't need to pass them through JSON.
Jeongik Chab19b58a2021-04-26 22:57:27 +0900305 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.Archs))
Dan Willemsen0f416782019-06-13 21:44:53 +0000306
Colin Cross69f59a32019-02-15 10:39:37 -0800307 return config.ModuleConfig, nil
308}
309
Jeongik Chac6246672021-04-08 00:00:19 +0900310func pathsListToStringLists(pathsList []android.OutputPaths) [][]string {
311 ret := make([][]string, 0, len(pathsList))
312 for _, paths := range pathsList {
313 ret = append(ret, paths.Strings())
314 }
315 return ret
316}
317
318func moduleConfigToJSON(config *ModuleConfig) ([]byte, error) {
319 return json.MarshalIndent(&moduleJSONConfig{
320 BuildPath: config.BuildPath.String(),
321 DexPath: config.DexPath.String(),
322 ManifestPath: config.ManifestPath.String(),
323 ProfileClassListing: config.ProfileClassListing.String(),
324 ProfileBootListing: config.ProfileBootListing.String(),
325 EnforceUsesLibrariesStatusFile: config.EnforceUsesLibrariesStatusFile.String(),
326 ClassLoaderContexts: toJsonClassLoaderContext(config.ClassLoaderContexts),
Jeongik Chac6246672021-04-08 00:00:19 +0900327 DexPreoptImagesDeps: pathsListToStringLists(config.DexPreoptImagesDeps),
328 PreoptBootClassPathDexFiles: config.PreoptBootClassPathDexFiles.Strings(),
329 ModuleConfig: config,
330 }, "", " ")
331}
332
333// WriteModuleConfig serializes a ModuleConfig into a per-module dexpreopt.config JSON file.
334// These config files are used for post-processing.
335func WriteModuleConfig(ctx android.ModuleContext, config *ModuleConfig, path android.WritablePath) {
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000336 if path == nil {
337 return
338 }
339
Jeongik Chac6246672021-04-08 00:00:19 +0900340 data, err := moduleConfigToJSON(config)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000341 if err != nil {
342 ctx.ModuleErrorf("failed to JSON marshal module dexpreopt.config: %v", err)
343 return
344 }
345
346 android.WriteFileRule(ctx, path, string(data))
347}
348
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000349// dex2oatModuleName returns the name of the module to use for the dex2oat host
350// tool. It should be a binary module with public visibility that is compiled
351// and installed for host.
352func dex2oatModuleName(config android.Config) string {
353 // Default to the debug variant of dex2oat to help find bugs.
354 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
355 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
356 return "dex2oat"
357 } else {
358 return "dex2oatd"
359 }
360}
361
Paul Duffinb506c9d2021-03-24 14:34:40 +0000362type dex2oatDependencyTag struct {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000363 blueprint.BaseDependencyTag
Paul Duffinb506c9d2021-03-24 14:34:40 +0000364}
365
366func (d dex2oatDependencyTag) ExcludeFromVisibilityEnforcement() {
367}
368
369func (d dex2oatDependencyTag) ExcludeFromApexContents() {
370}
371
372// Dex2oatDepTag represents the dependency onto the dex2oatd module. It is added to any module that
373// needs dexpreopting and so it makes no sense for it to be checked for visibility or included in
374// the apex.
375var Dex2oatDepTag = dex2oatDependencyTag{}
376
377var _ android.ExcludeFromVisibilityEnforcementTag = Dex2oatDepTag
378var _ android.ExcludeFromApexContentsTag = Dex2oatDepTag
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000379
Martin Stjernholm6d415272020-01-31 17:10:36 +0000380// RegisterToolDeps adds the necessary dependencies to binary modules for tools
381// that are required later when Get(Cached)GlobalSoongConfig is called. It
382// should be called from a mutator that's registered with
383// android.RegistrationContext.FinalDepsMutators.
384func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000385 dex2oatBin := dex2oatModuleName(ctx.Config())
386 v := ctx.Config().BuildOSTarget.Variations()
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000387 ctx.AddFarVariationDependencies(v, Dex2oatDepTag, dex2oatBin)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000388}
389
390func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
391 dex2oatBin := dex2oatModuleName(ctx.Config())
392
Martin Stjernholmc0048622020-08-18 17:37:41 +0100393 // Find the right dex2oat module, trying to follow PrebuiltDepTag from source
394 // to prebuilt if there is one. We wouldn't have to do this if the
395 // prebuilt_postdeps mutator that replaces source deps with prebuilt deps was
396 // run after RegisterToolDeps above, but changing that leads to ordering
397 // problems between mutators (RegisterToolDeps needs to run late to act on
398 // final variants, while prebuilt_postdeps needs to run before many of the
399 // PostDeps mutators, like the APEX mutators). Hence we need to dig out the
400 // prebuilt explicitly here instead.
401 var dex2oatModule android.Module
402 ctx.WalkDeps(func(child, parent android.Module) bool {
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000403 if parent == ctx.Module() && ctx.OtherModuleDependencyTag(child) == Dex2oatDepTag {
Martin Stjernholmc0048622020-08-18 17:37:41 +0100404 // Found the source module, or prebuilt module that has replaced the source.
405 dex2oatModule = child
406 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil {
407 return false // If it's the prebuilt we're done.
408 } else {
409 return true // Recurse to check if the source has a prebuilt dependency.
410 }
411 }
412 if parent == dex2oatModule && ctx.OtherModuleDependencyTag(child) == android.PrebuiltDepTag {
413 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil && p.Prebuilt().UsePrebuilt() {
414 dex2oatModule = child // Found a prebuilt that should be used.
415 }
416 }
417 return false
418 })
419
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000420 if dex2oatModule == nil {
421 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
422 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
423 }
424
425 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
426 if !dex2oatPath.Valid() {
427 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
428 }
429
430 return dex2oatPath.Path()
431}
432
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000433// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000434// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000435func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000436 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000437 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000438 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000439 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
440 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
441 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
442 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100443 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000444 }
445}
446
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000447// The main reason for this Once cache for GlobalSoongConfig is to make the
448// dex2oat path available to singletons. In ordinary modules we get it through a
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000449// Dex2oatDepTag dependency, but in singletons there's no simple way to do the
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000450// same thing and ensure the right variant is selected, hence this cache to make
451// the resolved path available to singletons. This means we depend on there
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000452// being at least one ordinary module with a Dex2oatDepTag dependency.
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000453//
454// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
Paul Duffin9f045242021-01-21 15:05:11 +0000455// and then possibly remove this cache altogether.
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000456var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
457
458// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
459// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000460func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000461 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
462 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000463 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000464
465 // Always resolve the tool path from the dependency, to ensure that every
466 // module has the dependency added properly.
467 myDex2oat := dex2oatPathFromDep(ctx)
468 if myDex2oat != globalSoong.Dex2oat {
469 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
470 }
471
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000472 return globalSoong
473}
474
475// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
476// earlier GetGlobalSoongConfig call. This function works with any context
477// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000478// GlobalSoongConfig with the proper paths (which requires a full
479// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
480// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000481func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000482 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
483 return (*GlobalSoongConfig)(nil)
484 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000485}
486
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000487type globalJsonSoongConfig struct {
488 Profman string
489 Dex2oat string
490 Aapt string
491 SoongZip string
492 Zip2zip string
493 ManifestCheck string
494 ConstructContext string
495}
496
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000497// ParseGlobalSoongConfig parses the given data assumed to be read from the
498// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
499// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000500func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000501 var jc globalJsonSoongConfig
502
Colin Cross988414c2020-01-11 01:11:46 +0000503 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000504 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000505 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000506 }
507
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000508 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000509 Profman: constructPath(ctx, jc.Profman),
510 Dex2oat: constructPath(ctx, jc.Dex2oat),
511 Aapt: constructPath(ctx, jc.Aapt),
512 SoongZip: constructPath(ctx, jc.SoongZip),
513 Zip2zip: constructPath(ctx, jc.Zip2zip),
514 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
515 ConstructContext: constructPath(ctx, jc.ConstructContext),
516 }
517
518 return config, nil
519}
520
521func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000522 if GetGlobalConfig(ctx).DisablePreopt {
523 return
524 }
525
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000526 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000527 if config == nil {
528 // No module has enabled dexpreopting, so we assume there will be no calls
529 // to dexpreopt_gen.
530 return
531 }
532
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000533 jc := globalJsonSoongConfig{
534 Profman: config.Profman.String(),
535 Dex2oat: config.Dex2oat.String(),
536 Aapt: config.Aapt.String(),
537 SoongZip: config.SoongZip.String(),
538 Zip2zip: config.Zip2zip.String(),
539 ManifestCheck: config.ManifestCheck.String(),
540 ConstructContext: config.ConstructContext.String(),
541 }
542
543 data, err := json.Marshal(jc)
544 if err != nil {
545 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
546 return
547 }
548
Colin Crosscf371cc2020-11-13 11:48:42 -0800549 android.WriteFileRule(ctx, android.PathForOutput(ctx, "dexpreopt_soong.config"), string(data))
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000550}
551
552func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000553 if GetGlobalConfig(ctx).DisablePreopt {
554 return
555 }
556
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000557 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000558 if config == nil {
559 return
560 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000561
562 ctx.Strict("DEX2OAT", config.Dex2oat.String())
563 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
564 config.Profman.String(),
565 config.Dex2oat.String(),
566 config.Aapt.String(),
567 config.SoongZip.String(),
568 config.Zip2zip.String(),
569 config.ManifestCheck.String(),
570 config.ConstructContext.String(),
571 }, " "))
572}
573
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000574func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
575 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800576 DisablePreopt: false,
577 DisablePreoptModules: nil,
578 OnlyPreoptBootImageAndSystemServer: false,
579 HasSystemOther: false,
580 PatternsOnSystemOther: nil,
581 DisableGenerateProfile: false,
582 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100583 BootJars: android.EmptyConfiguredJarList(),
584 UpdatableBootJars: android.EmptyConfiguredJarList(),
585 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800586 SystemServerJars: nil,
587 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100588 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800589 SpeedApps: nil,
590 PreoptFlags: nil,
591 DefaultCompilerFilter: "",
592 SystemServerCompilerFilter: "",
593 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800594 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700595 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800596 AlwaysSystemServerDebugInfo: false,
597 NeverSystemServerDebugInfo: false,
598 AlwaysOtherDebugInfo: false,
599 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800600 IsEng: false,
601 SanitizeLite: false,
602 DefaultAppImages: false,
603 Dex2oatXmx: "",
604 Dex2oatXms: "",
605 EmptyDirectory: "empty_dir",
606 CpuVariant: nil,
607 InstructionSetFeatures: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800608 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800609 BootFlags: "",
610 Dex2oatImageXmx: "",
611 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000612 }
613}
614
Paul Duffin9f045242021-01-21 15:05:11 +0000615func globalSoongConfigForTests() *GlobalSoongConfig {
616 return &GlobalSoongConfig{
617 Profman: android.PathForTesting("profman"),
618 Dex2oat: android.PathForTesting("dex2oat"),
619 Aapt: android.PathForTesting("aapt"),
620 SoongZip: android.PathForTesting("soong_zip"),
621 Zip2zip: android.PathForTesting("zip2zip"),
622 ManifestCheck: android.PathForTesting("manifest_check"),
623 ConstructContext: android.PathForTesting("construct_context"),
624 }
Colin Cross69f59a32019-02-15 10:39:37 -0800625}