blob: 36a5e2aa29ce4e384c08a4c6eff6ceb0a21b7e7b [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
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000087
88 // If true, downgrade the compiler filter of dexpreopt to "extract" when verify_uses_libraries
89 // check fails, instead of failing the build. This will disable any AOT-compilation.
90 //
91 // The intended use case for this flag is to have a smoother migration path for the Java
92 // modules that need to add <uses-library> information in their build files. The flag allows to
93 // quickly silence build errors. This flag should be used with caution and only as a temporary
94 // measure, as it masks real errors and affects performance.
95 RelaxUsesLibraryCheck bool
Colin Cross43f08db2018-11-12 10:13:39 -080096}
97
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000098// GlobalSoongConfig contains the global config that is generated from Soong,
99// stored in dexpreopt_soong.config.
100type GlobalSoongConfig struct {
101 // Paths to tools possibly used by the generated commands.
102 Profman android.Path
103 Dex2oat android.Path
104 Aapt android.Path
105 SoongZip android.Path
106 Zip2zip android.Path
107 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700108 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800109}
110
111type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800112 Name string
113 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800114 BuildPath android.OutputPath
115 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700116 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800117 UncompressedDex bool
118 HasApkLibraries bool
119 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800120
Colin Cross69f59a32019-02-15 10:39:37 -0800121 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800122 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100123 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800124
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000125 EnforceUsesLibraries bool // turn on build-time verify_uses_libraries check
126 EnforceUsesLibrariesStatusFile android.Path // a file with verify_uses_libraries errors (if any)
127 ProvidesUsesLibrary string // library name (usually the same as module name)
128 ClassLoaderContexts ClassLoaderContextMap
Colin Cross43f08db2018-11-12 10:13:39 -0800129
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000130 Archs []android.ArchType
131 DexPreoptImages []android.Path
132 DexPreoptImagesDeps []android.OutputPaths
133 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800134
Colin Cross69f59a32019-02-15 10:39:37 -0800135 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
136 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800137
Colin Cross43f08db2018-11-12 10:13:39 -0800138 PreoptExtractedApk bool // Overrides OnlyPreoptModules
139
140 NoCreateAppImage bool
141 ForceCreateAppImage bool
142
143 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800144}
145
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000146type globalSoongConfigSingleton struct{}
147
148var pctx = android.NewPackageContext("android/soong/dexpreopt")
149
150func init() {
151 pctx.Import("android/soong/android")
152 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
153 return &globalSoongConfigSingleton{}
154 })
155}
156
Colin Cross69f59a32019-02-15 10:39:37 -0800157func constructPath(ctx android.PathContext, path string) android.Path {
158 buildDirPrefix := ctx.Config().BuildDir() + "/"
159 if path == "" {
160 return nil
161 } else if strings.HasPrefix(path, buildDirPrefix) {
162 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
163 } else {
164 return android.PathForSource(ctx, path)
165 }
Colin Cross43f08db2018-11-12 10:13:39 -0800166}
167
Colin Cross69f59a32019-02-15 10:39:37 -0800168func constructPaths(ctx android.PathContext, paths []string) android.Paths {
169 var ret android.Paths
170 for _, path := range paths {
171 ret = append(ret, constructPath(ctx, path))
172 }
173 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800174}
175
Colin Cross69f59a32019-02-15 10:39:37 -0800176func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
177 if path == "" {
178 return nil
179 }
180 return constructPath(ctx, path).(android.WritablePath)
181}
182
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000183// ParseGlobalConfig parses the given data assumed to be read from the global
184// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000185func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800186 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000187 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800188
189 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
190 // used to construct the real value manually below.
Paul Duffin7ccacae2020-10-23 21:14:20 +0100191 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800192 }
193
194 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000195 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800196 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000197 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800198 }
199
200 // Construct paths that require a PathContext.
Colin Cross69f59a32019-02-15 10:39:37 -0800201 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
202
Colin Cross988414c2020-01-11 01:11:46 +0000203 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800204}
205
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000206type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000207 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000208 data []byte
209}
210
211// GetGlobalConfig returns the global dexpreopt.config that's created in the
212// make config phase. It is loaded once the first time it is called for any
213// ctx.Config(), and returns the same data for all future calls with the same
214// ctx.Config(). A value can be inserted for tests using
215// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000216func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000217 return getGlobalConfigRaw(ctx).global
218}
219
220// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
221// the literal content of dexpreopt.config.
222func GetGlobalConfigRawData(ctx android.PathContext) []byte {
223 return getGlobalConfigRaw(ctx).data
224}
225
226var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
227var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
228
229func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
230 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
231 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
232 panic(err)
233 } else if data != nil {
234 globalConfig, err := ParseGlobalConfig(ctx, data)
235 if err != nil {
236 panic(err)
237 }
238 return globalConfigAndRaw{globalConfig, data}
239 }
240
241 // No global config filename set, see if there is a test config set
242 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
243 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000244 return globalConfigAndRaw{&GlobalConfig{
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000245 DisablePreopt: true,
246 DisablePreoptBootImages: true,
247 DisableGenerateProfile: true,
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000248 }, nil}
249 })
250 }).(globalConfigAndRaw)
251}
252
253// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
254// will return. It must be called before the first call to GetGlobalConfig for
255// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000256func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000257 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
258}
259
260// ParseModuleConfig parses a per-module dexpreopt.config file into a
261// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
262// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
263// from Make to read the module dexpreopt.config written in the Make config
264// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000265func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800266 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000267 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800268
269 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
270 // used to construct the real value manually below.
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000271 BuildPath string
272 DexPath string
273 ManifestPath string
274 ProfileClassListing string
275 EnforceUsesLibrariesStatusFile string
276 ClassLoaderContexts jsonClassLoaderContextMap
277 DexPreoptImages []string
278 DexPreoptImageLocations []string
279 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800280 }
281
282 config := ModuleJSONConfig{}
283
Colin Cross988414c2020-01-11 01:11:46 +0000284 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800285 if err != nil {
286 return config.ModuleConfig, err
287 }
288
289 // Construct paths that require a PathContext.
290 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
291 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700292 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800293 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000294 config.ModuleConfig.EnforceUsesLibrariesStatusFile = constructPath(ctx, config.EnforceUsesLibrariesStatusFile)
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000295 config.ModuleConfig.ClassLoaderContexts = fromJsonClassLoaderContext(ctx, config.ClassLoaderContexts)
Colin Cross69f59a32019-02-15 10:39:37 -0800296 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000297 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800298 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800299
Dan Willemsen0f416782019-06-13 21:44:53 +0000300 // 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 +0000301 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000302
Colin Cross69f59a32019-02-15 10:39:37 -0800303 return config.ModuleConfig, nil
304}
305
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000306// WriteSlimModuleConfigForMake serializes a subset of ModuleConfig into a per-module
307// dexpreopt.config JSON file. It is a way to pass dexpreopt information about Soong modules to
308// Make, which is needed when a Make module has a <uses-library> dependency on a Soong module.
309func WriteSlimModuleConfigForMake(ctx android.ModuleContext, config *ModuleConfig, path android.WritablePath) {
310 if path == nil {
311 return
312 }
313
314 // JSON representation of the slim module dexpreopt.config.
315 type slimModuleJSONConfig struct {
316 Name string
317 DexLocation string
318 BuildPath string
319 EnforceUsesLibraries bool
320 ProvidesUsesLibrary string
321 ClassLoaderContexts jsonClassLoaderContextMap
322 }
323
324 jsonConfig := &slimModuleJSONConfig{
325 Name: config.Name,
326 DexLocation: config.DexLocation,
327 BuildPath: config.BuildPath.String(),
328 EnforceUsesLibraries: config.EnforceUsesLibraries,
329 ProvidesUsesLibrary: config.ProvidesUsesLibrary,
330 ClassLoaderContexts: toJsonClassLoaderContext(config.ClassLoaderContexts),
331 }
332
333 data, err := json.MarshalIndent(jsonConfig, "", " ")
334 if err != nil {
335 ctx.ModuleErrorf("failed to JSON marshal module dexpreopt.config: %v", err)
336 return
337 }
338
339 android.WriteFileRule(ctx, path, string(data))
340}
341
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000342// dex2oatModuleName returns the name of the module to use for the dex2oat host
343// tool. It should be a binary module with public visibility that is compiled
344// and installed for host.
345func dex2oatModuleName(config android.Config) string {
346 // Default to the debug variant of dex2oat to help find bugs.
347 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
348 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
349 return "dex2oat"
350 } else {
351 return "dex2oatd"
352 }
353}
354
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000355var Dex2oatDepTag = struct {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000356 blueprint.BaseDependencyTag
357}{}
358
Martin Stjernholm6d415272020-01-31 17:10:36 +0000359// RegisterToolDeps adds the necessary dependencies to binary modules for tools
360// that are required later when Get(Cached)GlobalSoongConfig is called. It
361// should be called from a mutator that's registered with
362// android.RegistrationContext.FinalDepsMutators.
363func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000364 dex2oatBin := dex2oatModuleName(ctx.Config())
365 v := ctx.Config().BuildOSTarget.Variations()
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000366 ctx.AddFarVariationDependencies(v, Dex2oatDepTag, dex2oatBin)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000367}
368
369func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
370 dex2oatBin := dex2oatModuleName(ctx.Config())
371
Martin Stjernholmc0048622020-08-18 17:37:41 +0100372 // Find the right dex2oat module, trying to follow PrebuiltDepTag from source
373 // to prebuilt if there is one. We wouldn't have to do this if the
374 // prebuilt_postdeps mutator that replaces source deps with prebuilt deps was
375 // run after RegisterToolDeps above, but changing that leads to ordering
376 // problems between mutators (RegisterToolDeps needs to run late to act on
377 // final variants, while prebuilt_postdeps needs to run before many of the
378 // PostDeps mutators, like the APEX mutators). Hence we need to dig out the
379 // prebuilt explicitly here instead.
380 var dex2oatModule android.Module
381 ctx.WalkDeps(func(child, parent android.Module) bool {
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000382 if parent == ctx.Module() && ctx.OtherModuleDependencyTag(child) == Dex2oatDepTag {
Martin Stjernholmc0048622020-08-18 17:37:41 +0100383 // Found the source module, or prebuilt module that has replaced the source.
384 dex2oatModule = child
385 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil {
386 return false // If it's the prebuilt we're done.
387 } else {
388 return true // Recurse to check if the source has a prebuilt dependency.
389 }
390 }
391 if parent == dex2oatModule && ctx.OtherModuleDependencyTag(child) == android.PrebuiltDepTag {
392 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil && p.Prebuilt().UsePrebuilt() {
393 dex2oatModule = child // Found a prebuilt that should be used.
394 }
395 }
396 return false
397 })
398
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000399 if dex2oatModule == nil {
400 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
401 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
402 }
403
404 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
405 if !dex2oatPath.Valid() {
406 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
407 }
408
409 return dex2oatPath.Path()
410}
411
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000412// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000413// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000414func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000415 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000416 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000417 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000418 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
419 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
420 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
421 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100422 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000423 }
424}
425
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000426// The main reason for this Once cache for GlobalSoongConfig is to make the
427// dex2oat path available to singletons. In ordinary modules we get it through a
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000428// Dex2oatDepTag dependency, but in singletons there's no simple way to do the
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000429// same thing and ensure the right variant is selected, hence this cache to make
430// the resolved path available to singletons. This means we depend on there
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000431// being at least one ordinary module with a Dex2oatDepTag dependency.
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000432//
433// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
Paul Duffin9f045242021-01-21 15:05:11 +0000434// and then possibly remove this cache altogether.
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000435var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
436
437// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
438// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000439func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000440 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
441 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000442 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000443
444 // Always resolve the tool path from the dependency, to ensure that every
445 // module has the dependency added properly.
446 myDex2oat := dex2oatPathFromDep(ctx)
447 if myDex2oat != globalSoong.Dex2oat {
448 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
449 }
450
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000451 return globalSoong
452}
453
454// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
455// earlier GetGlobalSoongConfig call. This function works with any context
456// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000457// GlobalSoongConfig with the proper paths (which requires a full
458// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
459// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000460func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000461 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
462 return (*GlobalSoongConfig)(nil)
463 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000464}
465
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000466type globalJsonSoongConfig struct {
467 Profman string
468 Dex2oat string
469 Aapt string
470 SoongZip string
471 Zip2zip string
472 ManifestCheck string
473 ConstructContext string
474}
475
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000476// ParseGlobalSoongConfig parses the given data assumed to be read from the
477// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
478// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000479func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000480 var jc globalJsonSoongConfig
481
Colin Cross988414c2020-01-11 01:11:46 +0000482 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000483 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000484 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000485 }
486
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000487 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000488 Profman: constructPath(ctx, jc.Profman),
489 Dex2oat: constructPath(ctx, jc.Dex2oat),
490 Aapt: constructPath(ctx, jc.Aapt),
491 SoongZip: constructPath(ctx, jc.SoongZip),
492 Zip2zip: constructPath(ctx, jc.Zip2zip),
493 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
494 ConstructContext: constructPath(ctx, jc.ConstructContext),
495 }
496
497 return config, nil
498}
499
500func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000501 if GetGlobalConfig(ctx).DisablePreopt {
502 return
503 }
504
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000505 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000506 if config == nil {
507 // No module has enabled dexpreopting, so we assume there will be no calls
508 // to dexpreopt_gen.
509 return
510 }
511
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000512 jc := globalJsonSoongConfig{
513 Profman: config.Profman.String(),
514 Dex2oat: config.Dex2oat.String(),
515 Aapt: config.Aapt.String(),
516 SoongZip: config.SoongZip.String(),
517 Zip2zip: config.Zip2zip.String(),
518 ManifestCheck: config.ManifestCheck.String(),
519 ConstructContext: config.ConstructContext.String(),
520 }
521
522 data, err := json.Marshal(jc)
523 if err != nil {
524 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
525 return
526 }
527
Colin Crosscf371cc2020-11-13 11:48:42 -0800528 android.WriteFileRule(ctx, android.PathForOutput(ctx, "dexpreopt_soong.config"), string(data))
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000529}
530
531func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000532 if GetGlobalConfig(ctx).DisablePreopt {
533 return
534 }
535
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000536 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000537 if config == nil {
538 return
539 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000540
541 ctx.Strict("DEX2OAT", config.Dex2oat.String())
542 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
543 config.Profman.String(),
544 config.Dex2oat.String(),
545 config.Aapt.String(),
546 config.SoongZip.String(),
547 config.Zip2zip.String(),
548 config.ManifestCheck.String(),
549 config.ConstructContext.String(),
550 }, " "))
551}
552
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000553func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
554 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800555 DisablePreopt: false,
556 DisablePreoptModules: nil,
557 OnlyPreoptBootImageAndSystemServer: false,
558 HasSystemOther: false,
559 PatternsOnSystemOther: nil,
560 DisableGenerateProfile: false,
561 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100562 BootJars: android.EmptyConfiguredJarList(),
563 UpdatableBootJars: android.EmptyConfiguredJarList(),
564 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800565 SystemServerJars: nil,
566 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100567 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800568 SpeedApps: nil,
569 PreoptFlags: nil,
570 DefaultCompilerFilter: "",
571 SystemServerCompilerFilter: "",
572 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800573 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700574 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800575 AlwaysSystemServerDebugInfo: false,
576 NeverSystemServerDebugInfo: false,
577 AlwaysOtherDebugInfo: false,
578 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800579 IsEng: false,
580 SanitizeLite: false,
581 DefaultAppImages: false,
582 Dex2oatXmx: "",
583 Dex2oatXms: "",
584 EmptyDirectory: "empty_dir",
585 CpuVariant: nil,
586 InstructionSetFeatures: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800587 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800588 BootFlags: "",
589 Dex2oatImageXmx: "",
590 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000591 }
592}
593
Paul Duffin9f045242021-01-21 15:05:11 +0000594func globalSoongConfigForTests() *GlobalSoongConfig {
595 return &GlobalSoongConfig{
596 Profman: android.PathForTesting("profman"),
597 Dex2oat: android.PathForTesting("dex2oat"),
598 Aapt: android.PathForTesting("aapt"),
599 SoongZip: android.PathForTesting("soong_zip"),
600 Zip2zip: android.PathForTesting("zip2zip"),
601 ManifestCheck: android.PathForTesting("manifest_check"),
602 ConstructContext: android.PathForTesting("construct_context"),
603 }
Colin Cross69f59a32019-02-15 10:39:37 -0800604}