blob: f52ecb4090d9372be347393ff6b34480463fe6a1 [file] [log] [blame]
Colin Cross43f08db2018-11-12 10:13:39 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package dexpreopt
16
17import (
18 "encoding/json"
Martin Stjernholmd90676f2020-01-11 00:37:30 +000019 "fmt"
Colin Cross69f59a32019-02-15 10:39:37 -080020 "strings"
Colin Cross74ba9622019-02-11 15:11:14 -080021
Martin Stjernholmd90676f2020-01-11 00:37:30 +000022 "github.com/google/blueprint"
23
Colin Cross74ba9622019-02-11 15:11:14 -080024 "android/soong/android"
Colin Cross43f08db2018-11-12 10:13:39 -080025)
26
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000027// GlobalConfig stores the configuration for dex preopting. The fields are set
Martin Stjernholm75a48d82020-01-10 20:32:59 +000028// from product variables via dex_preopt_config.mk.
Colin Cross43f08db2018-11-12 10:13:39 -080029type GlobalConfig struct {
Colin Cross69f59a32019-02-15 10:39:37 -080030 DisablePreopt bool // disable preopt for all modules
Colin Cross43f08db2018-11-12 10:13:39 -080031 DisablePreoptModules []string // modules with preopt disabled by product-specific config
32
33 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
34
Vladimir Marko40139d62020-02-06 15:14:29 +000035 UseArtImage bool // use the art image (use other boot class path dex files without image)
36
Colin Cross43f08db2018-11-12 10:13:39 -080037 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
38 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
39
Colin Cross69f59a32019-02-15 10:39:37 -080040 DisableGenerateProfile bool // don't generate profiles
41 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080042
Ulya Trafimovich249386a2020-07-01 14:31:13 +010043 BootJars android.ConfiguredJarList // modules for jars that form the boot class path
44 UpdatableBootJars android.ConfiguredJarList // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000045
Ulya Trafimovich249386a2020-07-01 14:31:13 +010046 ArtApexJars android.ConfiguredJarList // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080047
Ulya Trafimovich249386a2020-07-01 14:31:13 +010048 SystemServerJars []string // jars that form the system server
49 SystemServerApps []string // apps that are loaded into system server
50 UpdatableSystemServerJars android.ConfiguredJarList // jars within apex that are loaded into system server
51 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080052
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +000053 BrokenSuboptimalOrderOfSystemServerJars bool // if true, sub-optimal order does not cause a build error
54
Colin Cross43f08db2018-11-12 10:13:39 -080055 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
56
57 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
58 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
59
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010060 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080061
62 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070063 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080064 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
65 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
66 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
67 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
68
Colin Cross43f08db2018-11-12 10:13:39 -080069 IsEng bool // build is a eng variant
70 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
71
72 DefaultAppImages bool // build app images (TODO: .art files?) by default
73
Colin Cross800fe132019-02-11 14:21:24 -080074 Dex2oatXmx string // max heap size for dex2oat
75 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080076
77 EmptyDirectory string // path to an empty directory
78
Colin Cross74ba9622019-02-11 15:11:14 -080079 CpuVariant map[android.ArchType]string // cpu variant for each architecture
80 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080081
Colin Cross800fe132019-02-11 14:21:24 -080082 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070083 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
84 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
85 BootFlags string // extra flags to pass to dex2oat for the boot image
86 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
87 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross43f08db2018-11-12 10:13:39 -080088}
89
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000090// GlobalSoongConfig contains the global config that is generated from Soong,
91// stored in dexpreopt_soong.config.
92type GlobalSoongConfig struct {
93 // Paths to tools possibly used by the generated commands.
94 Profman android.Path
95 Dex2oat android.Path
96 Aapt android.Path
97 SoongZip android.Path
98 Zip2zip android.Path
99 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700100 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800101}
102
103type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800104 Name string
105 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800106 BuildPath android.OutputPath
107 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700108 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800109 UncompressedDex bool
110 HasApkLibraries bool
111 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800112
Colin Cross69f59a32019-02-15 10:39:37 -0800113 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800114 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100115 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800116
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000117 EnforceUsesLibraries bool
118 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 DirtyImageObjects string
182 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800183 }
184
185 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000186 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800187 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000188 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800189 }
190
191 // Construct paths that require a PathContext.
192 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800193 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
194
Colin Cross988414c2020-01-11 01:11:46 +0000195 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800196}
197
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000198type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000199 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000200 data []byte
201}
202
203// GetGlobalConfig returns the global dexpreopt.config that's created in the
204// make config phase. It is loaded once the first time it is called for any
205// ctx.Config(), and returns the same data for all future calls with the same
206// ctx.Config(). A value can be inserted for tests using
207// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000208func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000209 return getGlobalConfigRaw(ctx).global
210}
211
212// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
213// the literal content of dexpreopt.config.
214func GetGlobalConfigRawData(ctx android.PathContext) []byte {
215 return getGlobalConfigRaw(ctx).data
216}
217
218var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
219var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
220
221func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
222 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
223 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
224 panic(err)
225 } else if data != nil {
226 globalConfig, err := ParseGlobalConfig(ctx, data)
227 if err != nil {
228 panic(err)
229 }
230 return globalConfigAndRaw{globalConfig, data}
231 }
232
233 // No global config filename set, see if there is a test config set
234 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
235 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000236 return globalConfigAndRaw{&GlobalConfig{
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000237 DisablePreopt: true,
238 DisableGenerateProfile: true,
239 }, nil}
240 })
241 }).(globalConfigAndRaw)
242}
243
244// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
245// will return. It must be called before the first call to GetGlobalConfig for
246// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000247func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000248 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
249}
250
251// ParseModuleConfig parses a per-module dexpreopt.config file into a
252// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
253// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
254// from Make to read the module dexpreopt.config written in the Make config
255// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000256func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800257 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000258 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800259
260 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
261 // used to construct the real value manually below.
262 BuildPath string
263 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700264 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800265 ProfileClassListing string
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000266 ClassLoaderContexts jsonClassLoaderContextMap
Colin Cross69f59a32019-02-15 10:39:37 -0800267 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000268 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800269 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800270 }
271
272 config := ModuleJSONConfig{}
273
Colin Cross988414c2020-01-11 01:11:46 +0000274 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800275 if err != nil {
276 return config.ModuleConfig, err
277 }
278
279 // Construct paths that require a PathContext.
280 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
281 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700282 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800283 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000284 config.ModuleConfig.ClassLoaderContexts = fromJsonClassLoaderContext(ctx, config.ClassLoaderContexts)
Colin Cross69f59a32019-02-15 10:39:37 -0800285 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000286 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800287 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800288
Dan Willemsen0f416782019-06-13 21:44:53 +0000289 // 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 +0000290 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000291
Colin Cross69f59a32019-02-15 10:39:37 -0800292 return config.ModuleConfig, nil
293}
294
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000295// dex2oatModuleName returns the name of the module to use for the dex2oat host
296// tool. It should be a binary module with public visibility that is compiled
297// and installed for host.
298func dex2oatModuleName(config android.Config) string {
299 // Default to the debug variant of dex2oat to help find bugs.
300 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
301 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
302 return "dex2oat"
303 } else {
304 return "dex2oatd"
305 }
306}
307
308var dex2oatDepTag = struct {
309 blueprint.BaseDependencyTag
310}{}
311
Martin Stjernholm6d415272020-01-31 17:10:36 +0000312// RegisterToolDeps adds the necessary dependencies to binary modules for tools
313// that are required later when Get(Cached)GlobalSoongConfig is called. It
314// should be called from a mutator that's registered with
315// android.RegistrationContext.FinalDepsMutators.
316func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000317 dex2oatBin := dex2oatModuleName(ctx.Config())
318 v := ctx.Config().BuildOSTarget.Variations()
319 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
320}
321
322func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
323 dex2oatBin := dex2oatModuleName(ctx.Config())
324
Martin Stjernholmc0048622020-08-18 17:37:41 +0100325 // Find the right dex2oat module, trying to follow PrebuiltDepTag from source
326 // to prebuilt if there is one. We wouldn't have to do this if the
327 // prebuilt_postdeps mutator that replaces source deps with prebuilt deps was
328 // run after RegisterToolDeps above, but changing that leads to ordering
329 // problems between mutators (RegisterToolDeps needs to run late to act on
330 // final variants, while prebuilt_postdeps needs to run before many of the
331 // PostDeps mutators, like the APEX mutators). Hence we need to dig out the
332 // prebuilt explicitly here instead.
333 var dex2oatModule android.Module
334 ctx.WalkDeps(func(child, parent android.Module) bool {
335 if parent == ctx.Module() && ctx.OtherModuleDependencyTag(child) == dex2oatDepTag {
336 // Found the source module, or prebuilt module that has replaced the source.
337 dex2oatModule = child
338 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil {
339 return false // If it's the prebuilt we're done.
340 } else {
341 return true // Recurse to check if the source has a prebuilt dependency.
342 }
343 }
344 if parent == dex2oatModule && ctx.OtherModuleDependencyTag(child) == android.PrebuiltDepTag {
345 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil && p.Prebuilt().UsePrebuilt() {
346 dex2oatModule = child // Found a prebuilt that should be used.
347 }
348 }
349 return false
350 })
351
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000352 if dex2oatModule == nil {
353 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
354 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
355 }
356
357 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
358 if !dex2oatPath.Valid() {
359 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
360 }
361
362 return dex2oatPath.Path()
363}
364
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000365// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000366// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000367func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000368 if ctx.Config().TestProductVariables != nil {
369 // If we're called in a test there'll be a confusing error from the path
370 // functions below that gets reported without a stack trace, so let's panic
371 // properly with a more helpful message.
372 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
373 }
374
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000375 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000376 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000377 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000378 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
379 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
380 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
381 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100382 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000383 }
384}
385
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000386// The main reason for this Once cache for GlobalSoongConfig is to make the
387// dex2oat path available to singletons. In ordinary modules we get it through a
388// dex2oatDepTag dependency, but in singletons there's no simple way to do the
389// same thing and ensure the right variant is selected, hence this cache to make
390// the resolved path available to singletons. This means we depend on there
391// being at least one ordinary module with a dex2oatDepTag dependency.
392//
393// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
394// and then possibly remove this cache altogether (but the use in
395// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000396var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
397
398// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
399// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000400func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000401 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
402 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000403 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000404
405 // Always resolve the tool path from the dependency, to ensure that every
406 // module has the dependency added properly.
407 myDex2oat := dex2oatPathFromDep(ctx)
408 if myDex2oat != globalSoong.Dex2oat {
409 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
410 }
411
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000412 return globalSoong
413}
414
415// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
416// earlier GetGlobalSoongConfig call. This function works with any context
417// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000418// GlobalSoongConfig with the proper paths (which requires a full
419// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
420// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000421func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000422 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
423 return (*GlobalSoongConfig)(nil)
424 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000425}
426
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000427type globalJsonSoongConfig struct {
428 Profman string
429 Dex2oat string
430 Aapt string
431 SoongZip string
432 Zip2zip string
433 ManifestCheck string
434 ConstructContext string
435}
436
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000437// ParseGlobalSoongConfig parses the given data assumed to be read from the
438// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
439// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000440func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000441 var jc globalJsonSoongConfig
442
Colin Cross988414c2020-01-11 01:11:46 +0000443 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000444 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000445 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000446 }
447
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000448 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000449 Profman: constructPath(ctx, jc.Profman),
450 Dex2oat: constructPath(ctx, jc.Dex2oat),
451 Aapt: constructPath(ctx, jc.Aapt),
452 SoongZip: constructPath(ctx, jc.SoongZip),
453 Zip2zip: constructPath(ctx, jc.Zip2zip),
454 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
455 ConstructContext: constructPath(ctx, jc.ConstructContext),
456 }
457
458 return config, nil
459}
460
461func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000462 if GetGlobalConfig(ctx).DisablePreopt {
463 return
464 }
465
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000466 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000467 if config == nil {
468 // No module has enabled dexpreopting, so we assume there will be no calls
469 // to dexpreopt_gen.
470 return
471 }
472
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000473 jc := globalJsonSoongConfig{
474 Profman: config.Profman.String(),
475 Dex2oat: config.Dex2oat.String(),
476 Aapt: config.Aapt.String(),
477 SoongZip: config.SoongZip.String(),
478 Zip2zip: config.Zip2zip.String(),
479 ManifestCheck: config.ManifestCheck.String(),
480 ConstructContext: config.ConstructContext.String(),
481 }
482
483 data, err := json.Marshal(jc)
484 if err != nil {
485 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
486 return
487 }
488
Colin Crosscf371cc2020-11-13 11:48:42 -0800489 android.WriteFileRule(ctx, android.PathForOutput(ctx, "dexpreopt_soong.config"), string(data))
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000490}
491
492func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000493 if GetGlobalConfig(ctx).DisablePreopt {
494 return
495 }
496
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000497 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000498 if config == nil {
499 return
500 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000501
502 ctx.Strict("DEX2OAT", config.Dex2oat.String())
503 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
504 config.Profman.String(),
505 config.Dex2oat.String(),
506 config.Aapt.String(),
507 config.SoongZip.String(),
508 config.Zip2zip.String(),
509 config.ManifestCheck.String(),
510 config.ConstructContext.String(),
511 }, " "))
512}
513
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000514func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
515 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800516 DisablePreopt: false,
517 DisablePreoptModules: nil,
518 OnlyPreoptBootImageAndSystemServer: false,
519 HasSystemOther: false,
520 PatternsOnSystemOther: nil,
521 DisableGenerateProfile: false,
522 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100523 BootJars: android.EmptyConfiguredJarList(),
524 UpdatableBootJars: android.EmptyConfiguredJarList(),
525 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800526 SystemServerJars: nil,
527 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100528 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800529 SpeedApps: nil,
530 PreoptFlags: nil,
531 DefaultCompilerFilter: "",
532 SystemServerCompilerFilter: "",
533 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800534 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700535 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800536 AlwaysSystemServerDebugInfo: false,
537 NeverSystemServerDebugInfo: false,
538 AlwaysOtherDebugInfo: false,
539 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800540 IsEng: false,
541 SanitizeLite: false,
542 DefaultAppImages: false,
543 Dex2oatXmx: "",
544 Dex2oatXms: "",
545 EmptyDirectory: "empty_dir",
546 CpuVariant: nil,
547 InstructionSetFeatures: nil,
548 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800549 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800550 BootFlags: "",
551 Dex2oatImageXmx: "",
552 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000553 }
554}
555
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000556func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000557 // Install the test GlobalSoongConfig in the Once cache so that later calls to
558 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
559 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000560 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700561 Profman: android.PathForTesting("profman"),
562 Dex2oat: android.PathForTesting("dex2oat"),
563 Aapt: android.PathForTesting("aapt"),
564 SoongZip: android.PathForTesting("soong_zip"),
565 Zip2zip: android.PathForTesting("zip2zip"),
566 ManifestCheck: android.PathForTesting("manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100567 ConstructContext: android.PathForTesting("construct_context"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000568 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000569 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800570}