blob: 98850e51ac9f2608de1347ce9d6911696e427e4c [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
Martin Stjernholm6d415272020-01-31 17:10:36 +0000320// RegisterToolDeps adds the necessary dependencies to binary modules for tools
321// that are required later when Get(Cached)GlobalSoongConfig is called. It
322// should be called from a mutator that's registered with
323// android.RegistrationContext.FinalDepsMutators.
324func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000325 dex2oatBin := dex2oatModuleName(ctx.Config())
326 v := ctx.Config().BuildOSTarget.Variations()
327 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
328}
329
330func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
331 dex2oatBin := dex2oatModuleName(ctx.Config())
332
333 dex2oatModule := ctx.GetDirectDepWithTag(dex2oatBin, dex2oatDepTag)
334 if dex2oatModule == nil {
335 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
336 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
337 }
338
339 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
340 if !dex2oatPath.Valid() {
341 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
342 }
343
344 return dex2oatPath.Path()
345}
346
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000347// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000348// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000349func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000350 if ctx.Config().TestProductVariables != nil {
351 // If we're called in a test there'll be a confusing error from the path
352 // functions below that gets reported without a stack trace, so let's panic
353 // properly with a more helpful message.
354 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
355 }
356
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000357 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000358 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000359 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000360 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
361 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
362 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
363 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
364 ConstructContext: android.PathForSource(ctx, "build/make/core/construct_context.sh"),
365 }
366}
367
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000368// The main reason for this Once cache for GlobalSoongConfig is to make the
369// dex2oat path available to singletons. In ordinary modules we get it through a
370// dex2oatDepTag dependency, but in singletons there's no simple way to do the
371// same thing and ensure the right variant is selected, hence this cache to make
372// the resolved path available to singletons. This means we depend on there
373// being at least one ordinary module with a dex2oatDepTag dependency.
374//
375// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
376// and then possibly remove this cache altogether (but the use in
377// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000378var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
379
380// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
381// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000382func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000383 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
384 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000385 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000386
387 // Always resolve the tool path from the dependency, to ensure that every
388 // module has the dependency added properly.
389 myDex2oat := dex2oatPathFromDep(ctx)
390 if myDex2oat != globalSoong.Dex2oat {
391 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
392 }
393
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000394 return globalSoong
395}
396
397// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
398// earlier GetGlobalSoongConfig call. This function works with any context
399// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000400// GlobalSoongConfig with the proper paths (which requires a full
401// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
402// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000403func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000404 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
405 return (*GlobalSoongConfig)(nil)
406 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000407}
408
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000409type globalJsonSoongConfig struct {
410 Profman string
411 Dex2oat string
412 Aapt string
413 SoongZip string
414 Zip2zip string
415 ManifestCheck string
416 ConstructContext string
417}
418
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000419// ParseGlobalSoongConfig parses the given data assumed to be read from the
420// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
421// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000422func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000423 var jc globalJsonSoongConfig
424
Colin Cross988414c2020-01-11 01:11:46 +0000425 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000426 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000427 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000428 }
429
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000430 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000431 Profman: constructPath(ctx, jc.Profman),
432 Dex2oat: constructPath(ctx, jc.Dex2oat),
433 Aapt: constructPath(ctx, jc.Aapt),
434 SoongZip: constructPath(ctx, jc.SoongZip),
435 Zip2zip: constructPath(ctx, jc.Zip2zip),
436 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
437 ConstructContext: constructPath(ctx, jc.ConstructContext),
438 }
439
440 return config, nil
441}
442
443func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000444 if GetGlobalConfig(ctx).DisablePreopt {
445 return
446 }
447
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000448 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000449 if config == nil {
450 // No module has enabled dexpreopting, so we assume there will be no calls
451 // to dexpreopt_gen.
452 return
453 }
454
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000455 jc := globalJsonSoongConfig{
456 Profman: config.Profman.String(),
457 Dex2oat: config.Dex2oat.String(),
458 Aapt: config.Aapt.String(),
459 SoongZip: config.SoongZip.String(),
460 Zip2zip: config.Zip2zip.String(),
461 ManifestCheck: config.ManifestCheck.String(),
462 ConstructContext: config.ConstructContext.String(),
463 }
464
465 data, err := json.Marshal(jc)
466 if err != nil {
467 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
468 return
469 }
470
471 ctx.Build(pctx, android.BuildParams{
472 Rule: android.WriteFile,
473 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
474 Args: map[string]string{
475 "content": string(data),
476 },
477 })
478}
479
480func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000481 if GetGlobalConfig(ctx).DisablePreopt {
482 return
483 }
484
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000485 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000486 if config == nil {
487 return
488 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000489
490 ctx.Strict("DEX2OAT", config.Dex2oat.String())
491 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
492 config.Profman.String(),
493 config.Dex2oat.String(),
494 config.Aapt.String(),
495 config.SoongZip.String(),
496 config.Zip2zip.String(),
497 config.ManifestCheck.String(),
498 config.ConstructContext.String(),
499 }, " "))
500}
501
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000502func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
503 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800504 DisablePreopt: false,
505 DisablePreoptModules: nil,
506 OnlyPreoptBootImageAndSystemServer: false,
507 HasSystemOther: false,
508 PatternsOnSystemOther: nil,
509 DisableGenerateProfile: false,
510 ProfileDir: "",
511 BootJars: nil,
Roshan Piusccc26ef2019-11-27 09:37:46 -0800512 UpdatableBootJars: nil,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100513 ArtApexJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800514 SystemServerJars: nil,
515 SystemServerApps: nil,
Roshan Pius9b51a402019-11-21 12:36:53 -0800516 UpdatableSystemServerJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800517 SpeedApps: nil,
518 PreoptFlags: nil,
519 DefaultCompilerFilter: "",
520 SystemServerCompilerFilter: "",
521 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800522 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700523 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800524 AlwaysSystemServerDebugInfo: false,
525 NeverSystemServerDebugInfo: false,
526 AlwaysOtherDebugInfo: false,
527 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800528 IsEng: false,
529 SanitizeLite: false,
530 DefaultAppImages: false,
531 Dex2oatXmx: "",
532 Dex2oatXms: "",
533 EmptyDirectory: "empty_dir",
534 CpuVariant: nil,
535 InstructionSetFeatures: nil,
536 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800537 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800538 BootFlags: "",
539 Dex2oatImageXmx: "",
540 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000541 }
542}
543
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000544func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000545 // Install the test GlobalSoongConfig in the Once cache so that later calls to
546 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
547 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000548 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700549 Profman: android.PathForTesting("profman"),
550 Dex2oat: android.PathForTesting("dex2oat"),
551 Aapt: android.PathForTesting("aapt"),
552 SoongZip: android.PathForTesting("soong_zip"),
553 Zip2zip: android.PathForTesting("zip2zip"),
554 ManifestCheck: android.PathForTesting("manifest_check"),
555 ConstructContext: android.PathForTesting("construct_context.sh"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000556 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000557 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800558}