blob: 01481615f45ef138c50aa040df21720f42f6155d [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
Roshan Piusccc26ef2019-11-27 09:37:46 -080043 BootJars []string // modules for jars that form the boot class path
44 UpdatableBootJars []string // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000045
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000046 ArtApexJars []string // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080047
Roshan Pius9b51a402019-11-21 12:36:53 -080048 SystemServerJars []string // jars that form the system server
49 SystemServerApps []string // apps that are loaded into system server
50 UpdatableSystemServerJars []string // 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
Colin Cross50ddcc42019-05-16 12:28:22 -0700117 EnforceUsesLibraries bool
118 PresentOptionalUsesLibraries []string
119 UsesLibraries []string
120 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800121
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000122 Archs []android.ArchType
123 DexPreoptImages []android.Path
124 DexPreoptImagesDeps []android.OutputPaths
125 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800126
Colin Cross69f59a32019-02-15 10:39:37 -0800127 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
128 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800129
Colin Cross43f08db2018-11-12 10:13:39 -0800130 PreoptExtractedApk bool // Overrides OnlyPreoptModules
131
132 NoCreateAppImage bool
133 ForceCreateAppImage bool
134
135 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800136}
137
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000138type globalSoongConfigSingleton struct{}
139
140var pctx = android.NewPackageContext("android/soong/dexpreopt")
141
142func init() {
143 pctx.Import("android/soong/android")
144 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
145 return &globalSoongConfigSingleton{}
146 })
147}
148
Colin Cross69f59a32019-02-15 10:39:37 -0800149func constructPath(ctx android.PathContext, path string) android.Path {
150 buildDirPrefix := ctx.Config().BuildDir() + "/"
151 if path == "" {
152 return nil
153 } else if strings.HasPrefix(path, buildDirPrefix) {
154 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
155 } else {
156 return android.PathForSource(ctx, path)
157 }
Colin Cross43f08db2018-11-12 10:13:39 -0800158}
159
Colin Cross69f59a32019-02-15 10:39:37 -0800160func constructPaths(ctx android.PathContext, paths []string) android.Paths {
161 var ret android.Paths
162 for _, path := range paths {
163 ret = append(ret, constructPath(ctx, path))
164 }
165 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800166}
167
Colin Cross69f59a32019-02-15 10:39:37 -0800168func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
169 ret := map[string]android.Path{}
170 for key, path := range paths {
171 ret[key] = constructPath(ctx, path)
172 }
173 return ret
174}
175
176func 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.
191 DirtyImageObjects string
Colin Cross69f59a32019-02-15 10:39:37 -0800192 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800193 }
194
195 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000196 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800197 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000198 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800199 }
200
201 // Construct paths that require a PathContext.
202 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800203 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
204
Colin Cross988414c2020-01-11 01:11:46 +0000205 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800206}
207
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000208type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000209 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000210 data []byte
211}
212
213// GetGlobalConfig returns the global dexpreopt.config that's created in the
214// make config phase. It is loaded once the first time it is called for any
215// ctx.Config(), and returns the same data for all future calls with the same
216// ctx.Config(). A value can be inserted for tests using
217// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000218func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000219 return getGlobalConfigRaw(ctx).global
220}
221
222// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
223// the literal content of dexpreopt.config.
224func GetGlobalConfigRawData(ctx android.PathContext) []byte {
225 return getGlobalConfigRaw(ctx).data
226}
227
228var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
229var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
230
231func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
232 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
233 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
234 panic(err)
235 } else if data != nil {
236 globalConfig, err := ParseGlobalConfig(ctx, data)
237 if err != nil {
238 panic(err)
239 }
240 return globalConfigAndRaw{globalConfig, data}
241 }
242
243 // No global config filename set, see if there is a test config set
244 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
245 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000246 return globalConfigAndRaw{&GlobalConfig{
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000247 DisablePreopt: true,
248 DisableGenerateProfile: true,
249 }, nil}
250 })
251 }).(globalConfigAndRaw)
252}
253
254// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
255// will return. It must be called before the first call to GetGlobalConfig for
256// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000257func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000258 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
259}
260
261// ParseModuleConfig parses a per-module dexpreopt.config file into a
262// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
263// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
264// from Make to read the module dexpreopt.config written in the Make config
265// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000266func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800267 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000268 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800269
270 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
271 // used to construct the real value manually below.
272 BuildPath string
273 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700274 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800275 ProfileClassListing string
276 LibraryPaths map[string]string
277 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000278 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800279 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))
294 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
295 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000296 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800297 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800298
Dan Willemsen0f416782019-06-13 21:44:53 +0000299 // 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 +0000300 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000301
Colin Cross69f59a32019-02-15 10:39:37 -0800302 return config.ModuleConfig, nil
303}
304
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000305// dex2oatModuleName returns the name of the module to use for the dex2oat host
306// tool. It should be a binary module with public visibility that is compiled
307// and installed for host.
308func dex2oatModuleName(config android.Config) string {
309 // Default to the debug variant of dex2oat to help find bugs.
310 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
311 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
312 return "dex2oat"
313 } else {
314 return "dex2oatd"
315 }
316}
317
318var dex2oatDepTag = struct {
319 blueprint.BaseDependencyTag
320}{}
321
Martin Stjernholm6d415272020-01-31 17:10:36 +0000322// RegisterToolDeps adds the necessary dependencies to binary modules for tools
323// that are required later when Get(Cached)GlobalSoongConfig is called. It
324// should be called from a mutator that's registered with
325// android.RegistrationContext.FinalDepsMutators.
326func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000327 dex2oatBin := dex2oatModuleName(ctx.Config())
328 v := ctx.Config().BuildOSTarget.Variations()
329 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
330}
331
332func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
333 dex2oatBin := dex2oatModuleName(ctx.Config())
334
335 dex2oatModule := ctx.GetDirectDepWithTag(dex2oatBin, dex2oatDepTag)
336 if dex2oatModule == nil {
337 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
338 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
339 }
340
341 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
342 if !dex2oatPath.Valid() {
343 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
344 }
345
346 return dex2oatPath.Path()
347}
348
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000349// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000350// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000351func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000352 if ctx.Config().TestProductVariables != nil {
353 // If we're called in a test there'll be a confusing error from the path
354 // functions below that gets reported without a stack trace, so let's panic
355 // properly with a more helpful message.
356 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
357 }
358
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000359 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000360 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000361 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000362 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
363 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
364 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
365 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
366 ConstructContext: android.PathForSource(ctx, "build/make/core/construct_context.sh"),
367 }
368}
369
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000370// The main reason for this Once cache for GlobalSoongConfig is to make the
371// dex2oat path available to singletons. In ordinary modules we get it through a
372// dex2oatDepTag dependency, but in singletons there's no simple way to do the
373// same thing and ensure the right variant is selected, hence this cache to make
374// the resolved path available to singletons. This means we depend on there
375// being at least one ordinary module with a dex2oatDepTag dependency.
376//
377// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
378// and then possibly remove this cache altogether (but the use in
379// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000380var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
381
382// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
383// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000384func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000385 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
386 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000387 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000388
389 // Always resolve the tool path from the dependency, to ensure that every
390 // module has the dependency added properly.
391 myDex2oat := dex2oatPathFromDep(ctx)
392 if myDex2oat != globalSoong.Dex2oat {
393 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
394 }
395
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000396 return globalSoong
397}
398
399// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
400// earlier GetGlobalSoongConfig call. This function works with any context
401// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000402// GlobalSoongConfig with the proper paths (which requires a full
403// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
404// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000405func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000406 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
407 return (*GlobalSoongConfig)(nil)
408 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000409}
410
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000411type globalJsonSoongConfig struct {
412 Profman string
413 Dex2oat string
414 Aapt string
415 SoongZip string
416 Zip2zip string
417 ManifestCheck string
418 ConstructContext string
419}
420
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000421// ParseGlobalSoongConfig parses the given data assumed to be read from the
422// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
423// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000424func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000425 var jc globalJsonSoongConfig
426
Colin Cross988414c2020-01-11 01:11:46 +0000427 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000428 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000429 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000430 }
431
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000432 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000433 Profman: constructPath(ctx, jc.Profman),
434 Dex2oat: constructPath(ctx, jc.Dex2oat),
435 Aapt: constructPath(ctx, jc.Aapt),
436 SoongZip: constructPath(ctx, jc.SoongZip),
437 Zip2zip: constructPath(ctx, jc.Zip2zip),
438 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
439 ConstructContext: constructPath(ctx, jc.ConstructContext),
440 }
441
442 return config, nil
443}
444
445func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000446 if GetGlobalConfig(ctx).DisablePreopt {
447 return
448 }
449
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000450 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000451 if config == nil {
452 // No module has enabled dexpreopting, so we assume there will be no calls
453 // to dexpreopt_gen.
454 return
455 }
456
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000457 jc := globalJsonSoongConfig{
458 Profman: config.Profman.String(),
459 Dex2oat: config.Dex2oat.String(),
460 Aapt: config.Aapt.String(),
461 SoongZip: config.SoongZip.String(),
462 Zip2zip: config.Zip2zip.String(),
463 ManifestCheck: config.ManifestCheck.String(),
464 ConstructContext: config.ConstructContext.String(),
465 }
466
467 data, err := json.Marshal(jc)
468 if err != nil {
469 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
470 return
471 }
472
473 ctx.Build(pctx, android.BuildParams{
474 Rule: android.WriteFile,
475 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
476 Args: map[string]string{
477 "content": string(data),
478 },
479 })
480}
481
482func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000483 if GetGlobalConfig(ctx).DisablePreopt {
484 return
485 }
486
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000487 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000488 if config == nil {
489 return
490 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000491
492 ctx.Strict("DEX2OAT", config.Dex2oat.String())
493 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
494 config.Profman.String(),
495 config.Dex2oat.String(),
496 config.Aapt.String(),
497 config.SoongZip.String(),
498 config.Zip2zip.String(),
499 config.ManifestCheck.String(),
500 config.ConstructContext.String(),
501 }, " "))
502}
503
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000504func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
505 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800506 DisablePreopt: false,
507 DisablePreoptModules: nil,
508 OnlyPreoptBootImageAndSystemServer: false,
509 HasSystemOther: false,
510 PatternsOnSystemOther: nil,
511 DisableGenerateProfile: false,
512 ProfileDir: "",
513 BootJars: nil,
Roshan Piusccc26ef2019-11-27 09:37:46 -0800514 UpdatableBootJars: nil,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100515 ArtApexJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800516 SystemServerJars: nil,
517 SystemServerApps: nil,
Roshan Pius9b51a402019-11-21 12:36:53 -0800518 UpdatableSystemServerJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800519 SpeedApps: nil,
520 PreoptFlags: nil,
521 DefaultCompilerFilter: "",
522 SystemServerCompilerFilter: "",
523 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800524 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700525 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800526 AlwaysSystemServerDebugInfo: false,
527 NeverSystemServerDebugInfo: false,
528 AlwaysOtherDebugInfo: false,
529 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800530 IsEng: false,
531 SanitizeLite: false,
532 DefaultAppImages: false,
533 Dex2oatXmx: "",
534 Dex2oatXms: "",
535 EmptyDirectory: "empty_dir",
536 CpuVariant: nil,
537 InstructionSetFeatures: nil,
538 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800539 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800540 BootFlags: "",
541 Dex2oatImageXmx: "",
542 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000543 }
544}
545
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000546func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000547 // Install the test GlobalSoongConfig in the Once cache so that later calls to
548 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
549 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000550 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700551 Profman: android.PathForTesting("profman"),
552 Dex2oat: android.PathForTesting("dex2oat"),
553 Aapt: android.PathForTesting("aapt"),
554 SoongZip: android.PathForTesting("soong_zip"),
555 Zip2zip: android.PathForTesting("zip2zip"),
556 ManifestCheck: android.PathForTesting("manifest_check"),
557 ConstructContext: android.PathForTesting("construct_context.sh"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000558 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000559 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800560}