blob: 6a806498b8213e611b0c56116bd8dca083f7a733 [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
Vladimir Marko40139d62020-02-06 15:14:29 +000036 UseArtImage bool // use the art image (use other boot class path dex files without image)
37
Colin Cross43f08db2018-11-12 10:13:39 -080038 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
39 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
40
Colin Cross69f59a32019-02-15 10:39:37 -080041 DisableGenerateProfile bool // don't generate profiles
42 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080043
Ulya Trafimovich249386a2020-07-01 14:31:13 +010044 BootJars android.ConfiguredJarList // modules for jars that form the boot class path
45 UpdatableBootJars android.ConfiguredJarList // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000046
Ulya Trafimovich249386a2020-07-01 14:31:13 +010047 ArtApexJars android.ConfiguredJarList // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080048
Ulya Trafimovich249386a2020-07-01 14:31:13 +010049 SystemServerJars []string // jars that form the system server
50 SystemServerApps []string // apps that are loaded into system server
51 UpdatableSystemServerJars android.ConfiguredJarList // jars within apex that are loaded into system server
52 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080053
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +000054 BrokenSuboptimalOrderOfSystemServerJars bool // if true, sub-optimal order does not cause a build error
55
Colin Cross43f08db2018-11-12 10:13:39 -080056 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
57
58 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
59 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
60
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010061 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080062
63 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070064 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080065 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
66 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
67 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
68 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
69
Colin Cross43f08db2018-11-12 10:13:39 -080070 IsEng bool // build is a eng variant
71 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
72
73 DefaultAppImages bool // build app images (TODO: .art files?) by default
74
Colin Cross800fe132019-02-11 14:21:24 -080075 Dex2oatXmx string // max heap size for dex2oat
76 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080077
78 EmptyDirectory string // path to an empty directory
79
Colin Cross74ba9622019-02-11 15:11:14 -080080 CpuVariant map[android.ArchType]string // cpu variant for each architecture
81 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080082
Colin Cross800fe132019-02-11 14:21:24 -080083 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070084 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
85 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
Colin Cross43f08db2018-11-12 10:13:39 -080089}
90
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000091// GlobalSoongConfig contains the global config that is generated from Soong,
92// stored in dexpreopt_soong.config.
93type GlobalSoongConfig struct {
94 // Paths to tools possibly used by the generated commands.
95 Profman android.Path
96 Dex2oat android.Path
97 Aapt android.Path
98 SoongZip android.Path
99 Zip2zip android.Path
100 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700101 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800102}
103
104type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800105 Name string
106 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800107 BuildPath android.OutputPath
108 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700109 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800110 UncompressedDex bool
111 HasApkLibraries bool
112 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800113
Colin Cross69f59a32019-02-15 10:39:37 -0800114 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800115 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100116 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800117
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000118 EnforceUsesLibraries bool
119 ClassLoaderContexts ClassLoaderContextMap
Colin Cross43f08db2018-11-12 10:13:39 -0800120
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000121 Archs []android.ArchType
122 DexPreoptImages []android.Path
123 DexPreoptImagesDeps []android.OutputPaths
124 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800125
Colin Cross69f59a32019-02-15 10:39:37 -0800126 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
127 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800128
Colin Cross43f08db2018-11-12 10:13:39 -0800129 PreoptExtractedApk bool // Overrides OnlyPreoptModules
130
131 NoCreateAppImage bool
132 ForceCreateAppImage bool
133
134 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800135}
136
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000137type globalSoongConfigSingleton struct{}
138
139var pctx = android.NewPackageContext("android/soong/dexpreopt")
140
141func init() {
142 pctx.Import("android/soong/android")
143 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
144 return &globalSoongConfigSingleton{}
145 })
146}
147
Colin Cross69f59a32019-02-15 10:39:37 -0800148func constructPath(ctx android.PathContext, path string) android.Path {
149 buildDirPrefix := ctx.Config().BuildDir() + "/"
150 if path == "" {
151 return nil
152 } else if strings.HasPrefix(path, buildDirPrefix) {
153 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
154 } else {
155 return android.PathForSource(ctx, path)
156 }
Colin Cross43f08db2018-11-12 10:13:39 -0800157}
158
Colin Cross69f59a32019-02-15 10:39:37 -0800159func constructPaths(ctx android.PathContext, paths []string) android.Paths {
160 var ret android.Paths
161 for _, path := range paths {
162 ret = append(ret, constructPath(ctx, path))
163 }
164 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800165}
166
Colin Cross69f59a32019-02-15 10:39:37 -0800167func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
168 if path == "" {
169 return nil
170 }
171 return constructPath(ctx, path).(android.WritablePath)
172}
173
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000174// ParseGlobalConfig parses the given data assumed to be read from the global
175// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000176func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800177 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000178 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800179
180 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
181 // used to construct the real value manually below.
Paul Duffin7ccacae2020-10-23 21:14:20 +0100182 DirtyImageObjects string
183 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800184 }
185
186 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000187 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800188 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000189 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800190 }
191
192 // Construct paths that require a PathContext.
193 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800194 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
195
Colin Cross988414c2020-01-11 01:11:46 +0000196 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800197}
198
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000199type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000200 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000201 data []byte
202}
203
204// GetGlobalConfig returns the global dexpreopt.config that's created in the
205// make config phase. It is loaded once the first time it is called for any
206// ctx.Config(), and returns the same data for all future calls with the same
207// ctx.Config(). A value can be inserted for tests using
208// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000209func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000210 return getGlobalConfigRaw(ctx).global
211}
212
213// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
214// the literal content of dexpreopt.config.
215func GetGlobalConfigRawData(ctx android.PathContext) []byte {
216 return getGlobalConfigRaw(ctx).data
217}
218
219var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
220var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
221
222func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
223 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
224 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
225 panic(err)
226 } else if data != nil {
227 globalConfig, err := ParseGlobalConfig(ctx, data)
228 if err != nil {
229 panic(err)
230 }
231 return globalConfigAndRaw{globalConfig, data}
232 }
233
234 // No global config filename set, see if there is a test config set
235 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
236 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000237 return globalConfigAndRaw{&GlobalConfig{
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000238 DisablePreopt: true,
239 DisablePreoptBootImages: true,
240 DisableGenerateProfile: true,
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000241 }, nil}
242 })
243 }).(globalConfigAndRaw)
244}
245
246// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
247// will return. It must be called before the first call to GetGlobalConfig for
248// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000249func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000250 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
251}
252
253// ParseModuleConfig parses a per-module dexpreopt.config file into a
254// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
255// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
256// from Make to read the module dexpreopt.config written in the Make config
257// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000258func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800259 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000260 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800261
262 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
263 // used to construct the real value manually below.
264 BuildPath string
265 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700266 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800267 ProfileClassListing string
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000268 ClassLoaderContexts jsonClassLoaderContextMap
Colin Cross69f59a32019-02-15 10:39:37 -0800269 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000270 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800271 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800272 }
273
274 config := ModuleJSONConfig{}
275
Colin Cross988414c2020-01-11 01:11:46 +0000276 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800277 if err != nil {
278 return config.ModuleConfig, err
279 }
280
281 // Construct paths that require a PathContext.
282 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
283 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700284 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800285 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000286 config.ModuleConfig.ClassLoaderContexts = fromJsonClassLoaderContext(ctx, config.ClassLoaderContexts)
Colin Cross69f59a32019-02-15 10:39:37 -0800287 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000288 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800289 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800290
Dan Willemsen0f416782019-06-13 21:44:53 +0000291 // 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 +0000292 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000293
Colin Cross69f59a32019-02-15 10:39:37 -0800294 return config.ModuleConfig, nil
295}
296
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000297// dex2oatModuleName returns the name of the module to use for the dex2oat host
298// tool. It should be a binary module with public visibility that is compiled
299// and installed for host.
300func dex2oatModuleName(config android.Config) string {
301 // Default to the debug variant of dex2oat to help find bugs.
302 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
303 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
304 return "dex2oat"
305 } else {
306 return "dex2oatd"
307 }
308}
309
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000310var Dex2oatDepTag = struct {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000311 blueprint.BaseDependencyTag
312}{}
313
Martin Stjernholm6d415272020-01-31 17:10:36 +0000314// RegisterToolDeps adds the necessary dependencies to binary modules for tools
315// that are required later when Get(Cached)GlobalSoongConfig is called. It
316// should be called from a mutator that's registered with
317// android.RegistrationContext.FinalDepsMutators.
318func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000319 dex2oatBin := dex2oatModuleName(ctx.Config())
320 v := ctx.Config().BuildOSTarget.Variations()
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000321 ctx.AddFarVariationDependencies(v, Dex2oatDepTag, dex2oatBin)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000322}
323
324func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
325 dex2oatBin := dex2oatModuleName(ctx.Config())
326
Martin Stjernholmc0048622020-08-18 17:37:41 +0100327 // Find the right dex2oat module, trying to follow PrebuiltDepTag from source
328 // to prebuilt if there is one. We wouldn't have to do this if the
329 // prebuilt_postdeps mutator that replaces source deps with prebuilt deps was
330 // run after RegisterToolDeps above, but changing that leads to ordering
331 // problems between mutators (RegisterToolDeps needs to run late to act on
332 // final variants, while prebuilt_postdeps needs to run before many of the
333 // PostDeps mutators, like the APEX mutators). Hence we need to dig out the
334 // prebuilt explicitly here instead.
335 var dex2oatModule android.Module
336 ctx.WalkDeps(func(child, parent android.Module) bool {
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000337 if parent == ctx.Module() && ctx.OtherModuleDependencyTag(child) == Dex2oatDepTag {
Martin Stjernholmc0048622020-08-18 17:37:41 +0100338 // Found the source module, or prebuilt module that has replaced the source.
339 dex2oatModule = child
340 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil {
341 return false // If it's the prebuilt we're done.
342 } else {
343 return true // Recurse to check if the source has a prebuilt dependency.
344 }
345 }
346 if parent == dex2oatModule && ctx.OtherModuleDependencyTag(child) == android.PrebuiltDepTag {
347 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil && p.Prebuilt().UsePrebuilt() {
348 dex2oatModule = child // Found a prebuilt that should be used.
349 }
350 }
351 return false
352 })
353
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000354 if dex2oatModule == nil {
355 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
356 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
357 }
358
359 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
360 if !dex2oatPath.Valid() {
361 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
362 }
363
364 return dex2oatPath.Path()
365}
366
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000367// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000368// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000369func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000370 if ctx.Config().TestProductVariables != nil {
371 // If we're called in a test there'll be a confusing error from the path
372 // functions below that gets reported without a stack trace, so let's panic
373 // properly with a more helpful message.
374 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
375 }
376
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000377 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000378 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000379 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000380 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
381 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
382 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
383 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100384 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000385 }
386}
387
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000388// The main reason for this Once cache for GlobalSoongConfig is to make the
389// dex2oat path available to singletons. In ordinary modules we get it through a
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000390// Dex2oatDepTag dependency, but in singletons there's no simple way to do the
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000391// same thing and ensure the right variant is selected, hence this cache to make
392// the resolved path available to singletons. This means we depend on there
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000393// being at least one ordinary module with a Dex2oatDepTag dependency.
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000394//
395// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
396// and then possibly remove this cache altogether (but the use in
397// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000398var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
399
400// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
401// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000402func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000403 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
404 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000405 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000406
407 // Always resolve the tool path from the dependency, to ensure that every
408 // module has the dependency added properly.
409 myDex2oat := dex2oatPathFromDep(ctx)
410 if myDex2oat != globalSoong.Dex2oat {
411 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
412 }
413
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000414 return globalSoong
415}
416
417// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
418// earlier GetGlobalSoongConfig call. This function works with any context
419// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000420// GlobalSoongConfig with the proper paths (which requires a full
421// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
422// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000423func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000424 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
425 return (*GlobalSoongConfig)(nil)
426 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000427}
428
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000429type globalJsonSoongConfig struct {
430 Profman string
431 Dex2oat string
432 Aapt string
433 SoongZip string
434 Zip2zip string
435 ManifestCheck string
436 ConstructContext string
437}
438
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000439// ParseGlobalSoongConfig parses the given data assumed to be read from the
440// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
441// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000442func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000443 var jc globalJsonSoongConfig
444
Colin Cross988414c2020-01-11 01:11:46 +0000445 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000446 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000447 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000448 }
449
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000450 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000451 Profman: constructPath(ctx, jc.Profman),
452 Dex2oat: constructPath(ctx, jc.Dex2oat),
453 Aapt: constructPath(ctx, jc.Aapt),
454 SoongZip: constructPath(ctx, jc.SoongZip),
455 Zip2zip: constructPath(ctx, jc.Zip2zip),
456 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
457 ConstructContext: constructPath(ctx, jc.ConstructContext),
458 }
459
460 return config, nil
461}
462
463func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000464 if GetGlobalConfig(ctx).DisablePreopt {
465 return
466 }
467
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000468 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000469 if config == nil {
470 // No module has enabled dexpreopting, so we assume there will be no calls
471 // to dexpreopt_gen.
472 return
473 }
474
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000475 jc := globalJsonSoongConfig{
476 Profman: config.Profman.String(),
477 Dex2oat: config.Dex2oat.String(),
478 Aapt: config.Aapt.String(),
479 SoongZip: config.SoongZip.String(),
480 Zip2zip: config.Zip2zip.String(),
481 ManifestCheck: config.ManifestCheck.String(),
482 ConstructContext: config.ConstructContext.String(),
483 }
484
485 data, err := json.Marshal(jc)
486 if err != nil {
487 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
488 return
489 }
490
Colin Crosscf371cc2020-11-13 11:48:42 -0800491 android.WriteFileRule(ctx, android.PathForOutput(ctx, "dexpreopt_soong.config"), string(data))
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000492}
493
494func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000495 if GetGlobalConfig(ctx).DisablePreopt {
496 return
497 }
498
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000499 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000500 if config == nil {
501 return
502 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000503
504 ctx.Strict("DEX2OAT", config.Dex2oat.String())
505 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
506 config.Profman.String(),
507 config.Dex2oat.String(),
508 config.Aapt.String(),
509 config.SoongZip.String(),
510 config.Zip2zip.String(),
511 config.ManifestCheck.String(),
512 config.ConstructContext.String(),
513 }, " "))
514}
515
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000516func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
517 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800518 DisablePreopt: false,
519 DisablePreoptModules: nil,
520 OnlyPreoptBootImageAndSystemServer: false,
521 HasSystemOther: false,
522 PatternsOnSystemOther: nil,
523 DisableGenerateProfile: false,
524 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100525 BootJars: android.EmptyConfiguredJarList(),
526 UpdatableBootJars: android.EmptyConfiguredJarList(),
527 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800528 SystemServerJars: nil,
529 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100530 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800531 SpeedApps: nil,
532 PreoptFlags: nil,
533 DefaultCompilerFilter: "",
534 SystemServerCompilerFilter: "",
535 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800536 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700537 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800538 AlwaysSystemServerDebugInfo: false,
539 NeverSystemServerDebugInfo: false,
540 AlwaysOtherDebugInfo: false,
541 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800542 IsEng: false,
543 SanitizeLite: false,
544 DefaultAppImages: false,
545 Dex2oatXmx: "",
546 Dex2oatXms: "",
547 EmptyDirectory: "empty_dir",
548 CpuVariant: nil,
549 InstructionSetFeatures: nil,
550 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800551 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800552 BootFlags: "",
553 Dex2oatImageXmx: "",
554 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000555 }
556}
557
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000558func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000559 // Install the test GlobalSoongConfig in the Once cache so that later calls to
560 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
561 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000562 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700563 Profman: android.PathForTesting("profman"),
564 Dex2oat: android.PathForTesting("dex2oat"),
565 Aapt: android.PathForTesting("aapt"),
566 SoongZip: android.PathForTesting("soong_zip"),
567 Zip2zip: android.PathForTesting("zip2zip"),
568 ManifestCheck: android.PathForTesting("manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100569 ConstructContext: android.PathForTesting("construct_context"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000570 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000571 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800572}