blob: d55204b0988f4fbfa4f36bcc0f3fdfcf00e56cf1 [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
Nicolas Geoffray1086e602021-01-20 14:30:40 +000083 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
84 BootFlags string // extra flags to pass to dex2oat for the boot image
85 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
86 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross43f08db2018-11-12 10:13:39 -080087}
88
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000089// GlobalSoongConfig contains the global config that is generated from Soong,
90// stored in dexpreopt_soong.config.
91type GlobalSoongConfig struct {
92 // Paths to tools possibly used by the generated commands.
93 Profman android.Path
94 Dex2oat android.Path
95 Aapt android.Path
96 SoongZip android.Path
97 Zip2zip android.Path
98 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -070099 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800100}
101
102type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800103 Name string
104 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800105 BuildPath android.OutputPath
106 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700107 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800108 UncompressedDex bool
109 HasApkLibraries bool
110 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800111
Colin Cross69f59a32019-02-15 10:39:37 -0800112 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800113 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100114 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800115
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000116 EnforceUsesLibraries bool
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000117 ProvidesUsesLibrary string // the name of the <uses-library> (usually the same as its module)
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000118 ClassLoaderContexts ClassLoaderContextMap
Colin Cross43f08db2018-11-12 10:13:39 -0800119
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000120 Archs []android.ArchType
121 DexPreoptImages []android.Path
122 DexPreoptImagesDeps []android.OutputPaths
123 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800124
Colin Cross69f59a32019-02-15 10:39:37 -0800125 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
126 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800127
Colin Cross43f08db2018-11-12 10:13:39 -0800128 PreoptExtractedApk bool // Overrides OnlyPreoptModules
129
130 NoCreateAppImage bool
131 ForceCreateAppImage bool
132
133 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800134}
135
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000136type globalSoongConfigSingleton struct{}
137
138var pctx = android.NewPackageContext("android/soong/dexpreopt")
139
140func init() {
141 pctx.Import("android/soong/android")
142 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
143 return &globalSoongConfigSingleton{}
144 })
145}
146
Colin Cross69f59a32019-02-15 10:39:37 -0800147func constructPath(ctx android.PathContext, path string) android.Path {
148 buildDirPrefix := ctx.Config().BuildDir() + "/"
149 if path == "" {
150 return nil
151 } else if strings.HasPrefix(path, buildDirPrefix) {
152 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
153 } else {
154 return android.PathForSource(ctx, path)
155 }
Colin Cross43f08db2018-11-12 10:13:39 -0800156}
157
Colin Cross69f59a32019-02-15 10:39:37 -0800158func constructPaths(ctx android.PathContext, paths []string) android.Paths {
159 var ret android.Paths
160 for _, path := range paths {
161 ret = append(ret, constructPath(ctx, path))
162 }
163 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800164}
165
Colin Cross69f59a32019-02-15 10:39:37 -0800166func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
167 if path == "" {
168 return nil
169 }
170 return constructPath(ctx, path).(android.WritablePath)
171}
172
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000173// ParseGlobalConfig parses the given data assumed to be read from the global
174// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000175func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800176 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000177 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800178
179 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
180 // used to construct the real value manually below.
Paul Duffin7ccacae2020-10-23 21:14:20 +0100181 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800182 }
183
184 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000185 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800186 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000187 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800188 }
189
190 // Construct paths that require a PathContext.
Colin Cross69f59a32019-02-15 10:39:37 -0800191 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
192
Colin Cross988414c2020-01-11 01:11:46 +0000193 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800194}
195
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000196type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000197 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000198 data []byte
199}
200
201// GetGlobalConfig returns the global dexpreopt.config that's created in the
202// make config phase. It is loaded once the first time it is called for any
203// ctx.Config(), and returns the same data for all future calls with the same
204// ctx.Config(). A value can be inserted for tests using
205// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000206func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000207 return getGlobalConfigRaw(ctx).global
208}
209
210// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
211// the literal content of dexpreopt.config.
212func GetGlobalConfigRawData(ctx android.PathContext) []byte {
213 return getGlobalConfigRaw(ctx).data
214}
215
216var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
217var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
218
219func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
220 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
221 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
222 panic(err)
223 } else if data != nil {
224 globalConfig, err := ParseGlobalConfig(ctx, data)
225 if err != nil {
226 panic(err)
227 }
228 return globalConfigAndRaw{globalConfig, data}
229 }
230
231 // No global config filename set, see if there is a test config set
232 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
233 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000234 return globalConfigAndRaw{&GlobalConfig{
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000235 DisablePreopt: true,
236 DisablePreoptBootImages: true,
237 DisableGenerateProfile: true,
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000238 }, nil}
239 })
240 }).(globalConfigAndRaw)
241}
242
243// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
244// will return. It must be called before the first call to GetGlobalConfig for
245// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000246func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000247 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
248}
249
250// ParseModuleConfig parses a per-module dexpreopt.config file into a
251// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
252// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
253// from Make to read the module dexpreopt.config written in the Make config
254// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000255func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800256 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000257 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800258
259 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
260 // used to construct the real value manually below.
261 BuildPath string
262 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700263 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800264 ProfileClassListing string
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000265 ClassLoaderContexts jsonClassLoaderContextMap
Colin Cross69f59a32019-02-15 10:39:37 -0800266 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000267 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800268 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800269 }
270
271 config := ModuleJSONConfig{}
272
Colin Cross988414c2020-01-11 01:11:46 +0000273 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800274 if err != nil {
275 return config.ModuleConfig, err
276 }
277
278 // Construct paths that require a PathContext.
279 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
280 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700281 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800282 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000283 config.ModuleConfig.ClassLoaderContexts = fromJsonClassLoaderContext(ctx, config.ClassLoaderContexts)
Colin Cross69f59a32019-02-15 10:39:37 -0800284 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000285 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800286 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800287
Dan Willemsen0f416782019-06-13 21:44:53 +0000288 // 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 +0000289 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000290
Colin Cross69f59a32019-02-15 10:39:37 -0800291 return config.ModuleConfig, nil
292}
293
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000294// WriteSlimModuleConfigForMake serializes a subset of ModuleConfig into a per-module
295// dexpreopt.config JSON file. It is a way to pass dexpreopt information about Soong modules to
296// Make, which is needed when a Make module has a <uses-library> dependency on a Soong module.
297func WriteSlimModuleConfigForMake(ctx android.ModuleContext, config *ModuleConfig, path android.WritablePath) {
298 if path == nil {
299 return
300 }
301
302 // JSON representation of the slim module dexpreopt.config.
303 type slimModuleJSONConfig struct {
304 Name string
305 DexLocation string
306 BuildPath string
307 EnforceUsesLibraries bool
308 ProvidesUsesLibrary string
309 ClassLoaderContexts jsonClassLoaderContextMap
310 }
311
312 jsonConfig := &slimModuleJSONConfig{
313 Name: config.Name,
314 DexLocation: config.DexLocation,
315 BuildPath: config.BuildPath.String(),
316 EnforceUsesLibraries: config.EnforceUsesLibraries,
317 ProvidesUsesLibrary: config.ProvidesUsesLibrary,
318 ClassLoaderContexts: toJsonClassLoaderContext(config.ClassLoaderContexts),
319 }
320
321 data, err := json.MarshalIndent(jsonConfig, "", " ")
322 if err != nil {
323 ctx.ModuleErrorf("failed to JSON marshal module dexpreopt.config: %v", err)
324 return
325 }
326
327 android.WriteFileRule(ctx, path, string(data))
328}
329
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000330// dex2oatModuleName returns the name of the module to use for the dex2oat host
331// tool. It should be a binary module with public visibility that is compiled
332// and installed for host.
333func dex2oatModuleName(config android.Config) string {
334 // Default to the debug variant of dex2oat to help find bugs.
335 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
336 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
337 return "dex2oat"
338 } else {
339 return "dex2oatd"
340 }
341}
342
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000343var Dex2oatDepTag = struct {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000344 blueprint.BaseDependencyTag
345}{}
346
Martin Stjernholm6d415272020-01-31 17:10:36 +0000347// RegisterToolDeps adds the necessary dependencies to binary modules for tools
348// that are required later when Get(Cached)GlobalSoongConfig is called. It
349// should be called from a mutator that's registered with
350// android.RegistrationContext.FinalDepsMutators.
351func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000352 dex2oatBin := dex2oatModuleName(ctx.Config())
353 v := ctx.Config().BuildOSTarget.Variations()
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000354 ctx.AddFarVariationDependencies(v, Dex2oatDepTag, dex2oatBin)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000355}
356
357func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
358 dex2oatBin := dex2oatModuleName(ctx.Config())
359
Martin Stjernholmc0048622020-08-18 17:37:41 +0100360 // Find the right dex2oat module, trying to follow PrebuiltDepTag from source
361 // to prebuilt if there is one. We wouldn't have to do this if the
362 // prebuilt_postdeps mutator that replaces source deps with prebuilt deps was
363 // run after RegisterToolDeps above, but changing that leads to ordering
364 // problems between mutators (RegisterToolDeps needs to run late to act on
365 // final variants, while prebuilt_postdeps needs to run before many of the
366 // PostDeps mutators, like the APEX mutators). Hence we need to dig out the
367 // prebuilt explicitly here instead.
368 var dex2oatModule android.Module
369 ctx.WalkDeps(func(child, parent android.Module) bool {
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000370 if parent == ctx.Module() && ctx.OtherModuleDependencyTag(child) == Dex2oatDepTag {
Martin Stjernholmc0048622020-08-18 17:37:41 +0100371 // Found the source module, or prebuilt module that has replaced the source.
372 dex2oatModule = child
373 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil {
374 return false // If it's the prebuilt we're done.
375 } else {
376 return true // Recurse to check if the source has a prebuilt dependency.
377 }
378 }
379 if parent == dex2oatModule && ctx.OtherModuleDependencyTag(child) == android.PrebuiltDepTag {
380 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil && p.Prebuilt().UsePrebuilt() {
381 dex2oatModule = child // Found a prebuilt that should be used.
382 }
383 }
384 return false
385 })
386
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000387 if dex2oatModule == nil {
388 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
389 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
390 }
391
392 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
393 if !dex2oatPath.Valid() {
394 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
395 }
396
397 return dex2oatPath.Path()
398}
399
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000400// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000401// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000402func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000403 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000404 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000405 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000406 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
407 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
408 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
409 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100410 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000411 }
412}
413
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000414// The main reason for this Once cache for GlobalSoongConfig is to make the
415// dex2oat path available to singletons. In ordinary modules we get it through a
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000416// Dex2oatDepTag dependency, but in singletons there's no simple way to do the
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000417// same thing and ensure the right variant is selected, hence this cache to make
418// the resolved path available to singletons. This means we depend on there
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000419// being at least one ordinary module with a Dex2oatDepTag dependency.
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000420//
421// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
Paul Duffin9f045242021-01-21 15:05:11 +0000422// and then possibly remove this cache altogether.
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000423var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
424
425// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
426// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000427func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000428 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
429 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000430 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000431
432 // Always resolve the tool path from the dependency, to ensure that every
433 // module has the dependency added properly.
434 myDex2oat := dex2oatPathFromDep(ctx)
435 if myDex2oat != globalSoong.Dex2oat {
436 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
437 }
438
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000439 return globalSoong
440}
441
442// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
443// earlier GetGlobalSoongConfig call. This function works with any context
444// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000445// GlobalSoongConfig with the proper paths (which requires a full
446// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
447// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000448func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000449 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
450 return (*GlobalSoongConfig)(nil)
451 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000452}
453
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000454type globalJsonSoongConfig struct {
455 Profman string
456 Dex2oat string
457 Aapt string
458 SoongZip string
459 Zip2zip string
460 ManifestCheck string
461 ConstructContext string
462}
463
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000464// ParseGlobalSoongConfig parses the given data assumed to be read from the
465// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
466// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000467func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000468 var jc globalJsonSoongConfig
469
Colin Cross988414c2020-01-11 01:11:46 +0000470 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000471 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000472 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000473 }
474
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000475 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000476 Profman: constructPath(ctx, jc.Profman),
477 Dex2oat: constructPath(ctx, jc.Dex2oat),
478 Aapt: constructPath(ctx, jc.Aapt),
479 SoongZip: constructPath(ctx, jc.SoongZip),
480 Zip2zip: constructPath(ctx, jc.Zip2zip),
481 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
482 ConstructContext: constructPath(ctx, jc.ConstructContext),
483 }
484
485 return config, nil
486}
487
488func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000489 if GetGlobalConfig(ctx).DisablePreopt {
490 return
491 }
492
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000493 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000494 if config == nil {
495 // No module has enabled dexpreopting, so we assume there will be no calls
496 // to dexpreopt_gen.
497 return
498 }
499
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000500 jc := globalJsonSoongConfig{
501 Profman: config.Profman.String(),
502 Dex2oat: config.Dex2oat.String(),
503 Aapt: config.Aapt.String(),
504 SoongZip: config.SoongZip.String(),
505 Zip2zip: config.Zip2zip.String(),
506 ManifestCheck: config.ManifestCheck.String(),
507 ConstructContext: config.ConstructContext.String(),
508 }
509
510 data, err := json.Marshal(jc)
511 if err != nil {
512 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
513 return
514 }
515
Colin Crosscf371cc2020-11-13 11:48:42 -0800516 android.WriteFileRule(ctx, android.PathForOutput(ctx, "dexpreopt_soong.config"), string(data))
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000517}
518
519func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000520 if GetGlobalConfig(ctx).DisablePreopt {
521 return
522 }
523
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000524 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000525 if config == nil {
526 return
527 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000528
529 ctx.Strict("DEX2OAT", config.Dex2oat.String())
530 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
531 config.Profman.String(),
532 config.Dex2oat.String(),
533 config.Aapt.String(),
534 config.SoongZip.String(),
535 config.Zip2zip.String(),
536 config.ManifestCheck.String(),
537 config.ConstructContext.String(),
538 }, " "))
539}
540
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000541func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
542 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800543 DisablePreopt: false,
544 DisablePreoptModules: nil,
545 OnlyPreoptBootImageAndSystemServer: false,
546 HasSystemOther: false,
547 PatternsOnSystemOther: nil,
548 DisableGenerateProfile: false,
549 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100550 BootJars: android.EmptyConfiguredJarList(),
551 UpdatableBootJars: android.EmptyConfiguredJarList(),
552 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800553 SystemServerJars: nil,
554 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100555 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800556 SpeedApps: nil,
557 PreoptFlags: nil,
558 DefaultCompilerFilter: "",
559 SystemServerCompilerFilter: "",
560 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800561 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700562 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800563 AlwaysSystemServerDebugInfo: false,
564 NeverSystemServerDebugInfo: false,
565 AlwaysOtherDebugInfo: false,
566 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800567 IsEng: false,
568 SanitizeLite: false,
569 DefaultAppImages: false,
570 Dex2oatXmx: "",
571 Dex2oatXms: "",
572 EmptyDirectory: "empty_dir",
573 CpuVariant: nil,
574 InstructionSetFeatures: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800575 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800576 BootFlags: "",
577 Dex2oatImageXmx: "",
578 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000579 }
580}
581
Paul Duffin9f045242021-01-21 15:05:11 +0000582func globalSoongConfigForTests() *GlobalSoongConfig {
583 return &GlobalSoongConfig{
584 Profman: android.PathForTesting("profman"),
585 Dex2oat: android.PathForTesting("dex2oat"),
586 Aapt: android.PathForTesting("aapt"),
587 SoongZip: android.PathForTesting("soong_zip"),
588 Zip2zip: android.PathForTesting("zip2zip"),
589 ManifestCheck: android.PathForTesting("manifest_check"),
590 ConstructContext: android.PathForTesting("construct_context"),
591 }
Colin Cross69f59a32019-02-15 10:39:37 -0800592}