blob: 3ab3414389733c6f8d655608d421db696fb507af [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
53 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
54
55 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
56 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
57
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010058 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080059
60 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070061 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080062 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
63 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
64 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
65 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
66
Colin Cross43f08db2018-11-12 10:13:39 -080067 IsEng bool // build is a eng variant
68 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
69
70 DefaultAppImages bool // build app images (TODO: .art files?) by default
71
Colin Cross800fe132019-02-11 14:21:24 -080072 Dex2oatXmx string // max heap size for dex2oat
73 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080074
75 EmptyDirectory string // path to an empty directory
76
Colin Cross74ba9622019-02-11 15:11:14 -080077 CpuVariant map[android.ArchType]string // cpu variant for each architecture
78 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080079
Colin Cross800fe132019-02-11 14:21:24 -080080 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070081 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
82 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
83 BootFlags string // extra flags to pass to dex2oat for the boot image
84 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
85 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross43f08db2018-11-12 10:13:39 -080086}
87
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000088// GlobalSoongConfig contains the global config that is generated from Soong,
89// stored in dexpreopt_soong.config.
90type GlobalSoongConfig struct {
91 // Paths to tools possibly used by the generated commands.
92 Profman android.Path
93 Dex2oat android.Path
94 Aapt android.Path
95 SoongZip android.Path
96 Zip2zip android.Path
97 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -070098 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -080099}
100
101type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800102 Name string
103 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800104 BuildPath android.OutputPath
105 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700106 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800107 UncompressedDex bool
108 HasApkLibraries bool
109 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800110
Colin Cross69f59a32019-02-15 10:39:37 -0800111 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800112 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100113 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800114
Colin Cross50ddcc42019-05-16 12:28:22 -0700115 EnforceUsesLibraries bool
116 PresentOptionalUsesLibraries []string
117 UsesLibraries []string
118 LibraryPaths map[string]android.Path
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 constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
167 ret := map[string]android.Path{}
168 for key, path := range paths {
169 ret[key] = constructPath(ctx, path)
170 }
171 return ret
172}
173
174func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
175 if path == "" {
176 return nil
177 }
178 return constructPath(ctx, path).(android.WritablePath)
179}
180
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000181// ParseGlobalConfig parses the given data assumed to be read from the global
182// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000183func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800184 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000185 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800186
187 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
188 // used to construct the real value manually below.
189 DirtyImageObjects string
Colin Cross69f59a32019-02-15 10:39:37 -0800190 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800191 }
192
193 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000194 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800195 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000196 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800197 }
198
199 // Construct paths that require a PathContext.
200 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
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{
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000245 DisablePreopt: true,
246 DisableGenerateProfile: true,
247 }, nil}
248 })
249 }).(globalConfigAndRaw)
250}
251
252// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
253// will return. It must be called before the first call to GetGlobalConfig for
254// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000255func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000256 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
257}
258
259// ParseModuleConfig parses a per-module dexpreopt.config file into a
260// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
261// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
262// from Make to read the module dexpreopt.config written in the Make config
263// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000264func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800265 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000266 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800267
268 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
269 // used to construct the real value manually below.
270 BuildPath string
271 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700272 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800273 ProfileClassListing string
274 LibraryPaths map[string]string
275 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000276 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800277 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800278 }
279
280 config := ModuleJSONConfig{}
281
Colin Cross988414c2020-01-11 01:11:46 +0000282 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800283 if err != nil {
284 return config.ModuleConfig, err
285 }
286
287 // Construct paths that require a PathContext.
288 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
289 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700290 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800291 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
292 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
293 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000294 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800295 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800296
Dan Willemsen0f416782019-06-13 21:44:53 +0000297 // 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 +0000298 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000299
Colin Cross69f59a32019-02-15 10:39:37 -0800300 return config.ModuleConfig, nil
301}
302
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000303// dex2oatModuleName returns the name of the module to use for the dex2oat host
304// tool. It should be a binary module with public visibility that is compiled
305// and installed for host.
306func dex2oatModuleName(config android.Config) string {
307 // Default to the debug variant of dex2oat to help find bugs.
308 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
309 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
310 return "dex2oat"
311 } else {
312 return "dex2oatd"
313 }
314}
315
316var dex2oatDepTag = struct {
317 blueprint.BaseDependencyTag
318}{}
319
320type DexPreoptModule interface {
321 dexPreoptModuleSignature() // Not called - only for type detection.
322}
323
324// RegisterToolDepsMutator registers a mutator that adds the necessary
325// dependencies to binary modules for tools that are required later when
326// Get(Cached)GlobalSoongConfig is called. It should be passed to
327// android.RegistrationContext.FinalDepsMutators, and module types that need
328// dependencies also need to embed DexPreoptModule.
329func RegisterToolDepsMutator(ctx android.RegisterMutatorsContext) {
330 ctx.BottomUp("dexpreopt_tool_deps", toolDepsMutator).Parallel()
331}
332
333func toolDepsMutator(ctx android.BottomUpMutatorContext) {
334 if GetGlobalConfig(ctx).DisablePreopt {
335 // Only register dependencies if dexpreopting is enabled. Necessary to avoid
336 // them in non-platform builds where dex2oat etc isn't available.
337 //
338 // It would be nice to not register this mutator at all then, but
339 // RegisterMutatorsContext available at registration doesn't have the state
340 // necessary to pass as PathContext to constructPath etc.
341 return
342 }
343 if _, ok := ctx.Module().(DexPreoptModule); !ok {
344 return
345 }
346 dex2oatBin := dex2oatModuleName(ctx.Config())
347 v := ctx.Config().BuildOSTarget.Variations()
348 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
349}
350
351func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
352 dex2oatBin := dex2oatModuleName(ctx.Config())
353
354 dex2oatModule := ctx.GetDirectDepWithTag(dex2oatBin, dex2oatDepTag)
355 if dex2oatModule == nil {
356 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
357 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
358 }
359
360 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
361 if !dex2oatPath.Valid() {
362 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
363 }
364
365 return dex2oatPath.Path()
366}
367
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000368// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000369// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000370func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000371 if ctx.Config().TestProductVariables != nil {
372 // If we're called in a test there'll be a confusing error from the path
373 // functions below that gets reported without a stack trace, so let's panic
374 // properly with a more helpful message.
375 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
376 }
377
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000378 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000379 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000380 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000381 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
382 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
383 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
384 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
385 ConstructContext: android.PathForSource(ctx, "build/make/core/construct_context.sh"),
386 }
387}
388
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000389// The main reason for this Once cache for GlobalSoongConfig is to make the
390// dex2oat path available to singletons. In ordinary modules we get it through a
391// dex2oatDepTag dependency, but in singletons there's no simple way to do the
392// same thing and ensure the right variant is selected, hence this cache to make
393// the resolved path available to singletons. This means we depend on there
394// being at least one ordinary module with a dex2oatDepTag dependency.
395//
396// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
397// and then possibly remove this cache altogether (but the use in
398// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000399var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
400
401// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
402// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000403func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000404 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
405 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000406 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000407
408 // Always resolve the tool path from the dependency, to ensure that every
409 // module has the dependency added properly.
410 myDex2oat := dex2oatPathFromDep(ctx)
411 if myDex2oat != globalSoong.Dex2oat {
412 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
413 }
414
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000415 return globalSoong
416}
417
418// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
419// earlier GetGlobalSoongConfig call. This function works with any context
420// compatible with a basic PathContext, since it doesn't try to create a
421// GlobalSoongConfig (which requires a full ModuleContext). It will panic if
422// called before the first GetGlobalSoongConfig call.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000423func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
424 return ctx.Config().Get(globalSoongConfigOnceKey).(*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 Stjernholmc52aaf12020-01-06 23:11:37 +0000467 jc := globalJsonSoongConfig{
468 Profman: config.Profman.String(),
469 Dex2oat: config.Dex2oat.String(),
470 Aapt: config.Aapt.String(),
471 SoongZip: config.SoongZip.String(),
472 Zip2zip: config.Zip2zip.String(),
473 ManifestCheck: config.ManifestCheck.String(),
474 ConstructContext: config.ConstructContext.String(),
475 }
476
477 data, err := json.Marshal(jc)
478 if err != nil {
479 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
480 return
481 }
482
483 ctx.Build(pctx, android.BuildParams{
484 Rule: android.WriteFile,
485 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
486 Args: map[string]string{
487 "content": string(data),
488 },
489 })
490}
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 Stjernholmc52aaf12020-01-06 23:11:37 +0000498
499 ctx.Strict("DEX2OAT", config.Dex2oat.String())
500 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
501 config.Profman.String(),
502 config.Dex2oat.String(),
503 config.Aapt.String(),
504 config.SoongZip.String(),
505 config.Zip2zip.String(),
506 config.ManifestCheck.String(),
507 config.ConstructContext.String(),
508 }, " "))
509}
510
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000511func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
512 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800513 DisablePreopt: false,
514 DisablePreoptModules: nil,
515 OnlyPreoptBootImageAndSystemServer: false,
516 HasSystemOther: false,
517 PatternsOnSystemOther: nil,
518 DisableGenerateProfile: false,
519 ProfileDir: "",
520 BootJars: nil,
Roshan Piusccc26ef2019-11-27 09:37:46 -0800521 UpdatableBootJars: nil,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100522 ArtApexJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800523 SystemServerJars: nil,
524 SystemServerApps: nil,
Roshan Pius9b51a402019-11-21 12:36:53 -0800525 UpdatableSystemServerJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800526 SpeedApps: nil,
527 PreoptFlags: nil,
528 DefaultCompilerFilter: "",
529 SystemServerCompilerFilter: "",
530 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800531 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700532 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800533 AlwaysSystemServerDebugInfo: false,
534 NeverSystemServerDebugInfo: false,
535 AlwaysOtherDebugInfo: false,
536 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800537 IsEng: false,
538 SanitizeLite: false,
539 DefaultAppImages: false,
540 Dex2oatXmx: "",
541 Dex2oatXms: "",
542 EmptyDirectory: "empty_dir",
543 CpuVariant: nil,
544 InstructionSetFeatures: nil,
545 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800546 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800547 BootFlags: "",
548 Dex2oatImageXmx: "",
549 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000550 }
551}
552
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000553func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000554 // Install the test GlobalSoongConfig in the Once cache so that later calls to
555 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
556 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000557 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700558 Profman: android.PathForTesting("profman"),
559 Dex2oat: android.PathForTesting("dex2oat"),
560 Aapt: android.PathForTesting("aapt"),
561 SoongZip: android.PathForTesting("soong_zip"),
562 Zip2zip: android.PathForTesting("zip2zip"),
563 ManifestCheck: android.PathForTesting("manifest_check"),
564 ConstructContext: android.PathForTesting("construct_context.sh"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000565 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000566 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800567}