blob: 72c01d0b74d03b7f13ac6cb5f44fc83739d986db [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 Stjernholm335d5962020-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 Stjernholm335d5962020-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 Stjernholmbe9d0d22020-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
Nicolas Geoffray72892f12019-02-22 15:34:40 +000035 GenerateApexImage bool // generate an extra boot image only containing jars from the runtime apex
Nicolas Geoffray25c0e032019-04-04 18:45:20 +010036 UseApexImage bool // use the apex image by default
Nicolas Geoffray72892f12019-02-22 15:34:40 +000037
Colin Cross43f08db2018-11-12 10:13:39 -080038 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
39 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
40
Colin Cross69f59a32019-02-15 10:39:37 -080041 DisableGenerateProfile bool // don't generate profiles
42 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080043
Roshan Piusccc26ef2019-11-27 09:37:46 -080044 BootJars []string // modules for jars that form the boot class path
45 UpdatableBootJars []string // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000046
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000047 ArtApexJars []string // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080048
Roshan Pius9b51a402019-11-21 12:36:53 -080049 SystemServerJars []string // jars that form the system server
50 SystemServerApps []string // apps that are loaded into system server
51 UpdatableSystemServerJars []string // jars within apex that are loaded into system server
52 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080053
54 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
55
56 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
57 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
58
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010059 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080060
61 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070062 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080063 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
64 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
65 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
66 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
67
Colin Cross43f08db2018-11-12 10:13:39 -080068 IsEng bool // build is a eng variant
69 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
70
71 DefaultAppImages bool // build app images (TODO: .art files?) by default
72
Colin Cross800fe132019-02-11 14:21:24 -080073 Dex2oatXmx string // max heap size for dex2oat
74 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080075
76 EmptyDirectory string // path to an empty directory
77
Colin Cross74ba9622019-02-11 15:11:14 -080078 CpuVariant map[android.ArchType]string // cpu variant for each architecture
79 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080080
Colin Cross800fe132019-02-11 14:21:24 -080081 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070082 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
83 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
84 BootFlags string // extra flags to pass to dex2oat for the boot image
85 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
86 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross43f08db2018-11-12 10:13:39 -080087}
88
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000089// GlobalSoongConfig contains the global config that is generated from Soong,
90// stored in dexpreopt_soong.config.
91type GlobalSoongConfig struct {
92 // Paths to tools possibly used by the generated commands.
93 Profman android.Path
94 Dex2oat android.Path
95 Aapt android.Path
96 SoongZip android.Path
97 Zip2zip android.Path
98 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -070099 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800100}
101
102type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800103 Name string
104 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800105 BuildPath android.OutputPath
106 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700107 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800108 UncompressedDex bool
109 HasApkLibraries bool
110 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800111
Colin Cross69f59a32019-02-15 10:39:37 -0800112 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800113 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100114 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800115
Colin Cross50ddcc42019-05-16 12:28:22 -0700116 EnforceUsesLibraries bool
117 PresentOptionalUsesLibraries []string
118 UsesLibraries []string
119 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800120
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000121 Archs []android.ArchType
122 DexPreoptImages []android.Path
123 DexPreoptImagesDeps []android.OutputPaths
124 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800125
Colin Cross69f59a32019-02-15 10:39:37 -0800126 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
127 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800128
Colin Cross43f08db2018-11-12 10:13:39 -0800129 PreoptExtractedApk bool // Overrides OnlyPreoptModules
130
131 NoCreateAppImage bool
132 ForceCreateAppImage bool
133
134 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800135}
136
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000137type globalSoongConfigSingleton struct{}
138
139var pctx = android.NewPackageContext("android/soong/dexpreopt")
140
141func init() {
142 pctx.Import("android/soong/android")
143 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
144 return &globalSoongConfigSingleton{}
145 })
146}
147
Colin Cross69f59a32019-02-15 10:39:37 -0800148func constructPath(ctx android.PathContext, path string) android.Path {
149 buildDirPrefix := ctx.Config().BuildDir() + "/"
150 if path == "" {
151 return nil
152 } else if strings.HasPrefix(path, buildDirPrefix) {
153 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
154 } else {
155 return android.PathForSource(ctx, path)
156 }
Colin Cross43f08db2018-11-12 10:13:39 -0800157}
158
Colin Cross69f59a32019-02-15 10:39:37 -0800159func constructPaths(ctx android.PathContext, paths []string) android.Paths {
160 var ret android.Paths
161 for _, path := range paths {
162 ret = append(ret, constructPath(ctx, path))
163 }
164 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800165}
166
Colin Cross69f59a32019-02-15 10:39:37 -0800167func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
168 ret := map[string]android.Path{}
169 for key, path := range paths {
170 ret[key] = constructPath(ctx, path)
171 }
172 return ret
173}
174
175func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
176 if path == "" {
177 return nil
178 }
179 return constructPath(ctx, path).(android.WritablePath)
180}
181
Martin Stjernholmdae8a802020-01-20 18:12:23 +0000182// ParseGlobalConfig parses the given data assumed to be read from the global
183// dexpreopt.config file into a GlobalConfig struct.
184func ParseGlobalConfig(ctx android.PathContext, data []byte) (GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800185 type GlobalJSONConfig struct {
186 GlobalConfig
187
188 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
189 // used to construct the real value manually below.
190 DirtyImageObjects string
Colin Cross69f59a32019-02-15 10:39:37 -0800191 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800192 }
193
194 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000195 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800196 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000197 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800198 }
199
200 // Construct paths that require a PathContext.
201 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800202 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
203
Colin Cross988414c2020-01-11 01:11:46 +0000204 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800205}
206
Martin Stjernholmdae8a802020-01-20 18:12:23 +0000207type globalConfigAndRaw struct {
208 global GlobalConfig
209 data []byte
210}
211
212// GetGlobalConfig returns the global dexpreopt.config that's created in the
213// make config phase. It is loaded once the first time it is called for any
214// ctx.Config(), and returns the same data for all future calls with the same
215// ctx.Config(). A value can be inserted for tests using
216// setDexpreoptTestGlobalConfig.
217func GetGlobalConfig(ctx android.PathContext) GlobalConfig {
218 return getGlobalConfigRaw(ctx).global
219}
220
221// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
222// the literal content of dexpreopt.config.
223func GetGlobalConfigRawData(ctx android.PathContext) []byte {
224 return getGlobalConfigRaw(ctx).data
225}
226
227var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
228var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
229
230func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
231 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
232 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
233 panic(err)
234 } else if data != nil {
235 globalConfig, err := ParseGlobalConfig(ctx, data)
236 if err != nil {
237 panic(err)
238 }
239 return globalConfigAndRaw{globalConfig, data}
240 }
241
242 // No global config filename set, see if there is a test config set
243 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
244 // Nope, return a config with preopting disabled
245 return globalConfigAndRaw{GlobalConfig{
246 DisablePreopt: true,
247 DisableGenerateProfile: true,
248 }, nil}
249 })
250 }).(globalConfigAndRaw)
251}
252
253// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
254// will return. It must be called before the first call to GetGlobalConfig for
255// the config.
256func SetTestGlobalConfig(config android.Config, globalConfig GlobalConfig) {
257 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
258}
259
260// ParseModuleConfig parses a per-module dexpreopt.config file into a
261// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
262// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
263// from Make to read the module dexpreopt.config written in the Make config
264// stage.
265func ParseModuleConfig(ctx android.PathContext, data []byte) (ModuleConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800266 type ModuleJSONConfig struct {
267 ModuleConfig
268
269 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
270 // used to construct the real value manually below.
271 BuildPath string
272 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700273 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800274 ProfileClassListing string
275 LibraryPaths map[string]string
276 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000277 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800278 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800279 }
280
281 config := ModuleJSONConfig{}
282
Colin Cross988414c2020-01-11 01:11:46 +0000283 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800284 if err != nil {
285 return config.ModuleConfig, err
286 }
287
288 // Construct paths that require a PathContext.
289 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
290 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700291 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800292 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
293 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
294 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000295 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800296 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800297
Dan Willemsen0f416782019-06-13 21:44:53 +0000298 // 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 +0000299 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000300
Colin Cross69f59a32019-02-15 10:39:37 -0800301 return config.ModuleConfig, nil
302}
303
Martin Stjernholm335d5962020-01-11 00:37:30 +0000304// dex2oatModuleName returns the name of the module to use for the dex2oat host
305// tool. It should be a binary module with public visibility that is compiled
306// and installed for host.
307func dex2oatModuleName(config android.Config) string {
308 // Default to the debug variant of dex2oat to help find bugs.
309 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
310 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
311 return "dex2oat"
312 } else {
313 return "dex2oatd"
314 }
315}
316
317var dex2oatDepTag = struct {
318 blueprint.BaseDependencyTag
319}{}
320
321type DexPreoptModule interface {
322 dexPreoptModuleSignature() // Not called - only for type detection.
323}
324
325// RegisterToolDepsMutator registers a mutator that adds the necessary
326// dependencies to binary modules for tools that are required later when
327// Get(Cached)GlobalSoongConfig is called. It should be passed to
328// android.RegistrationContext.FinalDepsMutators, and module types that need
329// dependencies also need to embed DexPreoptModule.
330func RegisterToolDepsMutator(ctx android.RegisterMutatorsContext) {
331 ctx.BottomUp("dexpreopt_tool_deps", toolDepsMutator).Parallel()
332}
333
334func toolDepsMutator(ctx android.BottomUpMutatorContext) {
335 if GetGlobalConfig(ctx).DisablePreopt {
336 // Only register dependencies if dexpreopting is enabled. Necessary to avoid
337 // them in non-platform builds where dex2oat etc isn't available.
338 //
339 // It would be nice to not register this mutator at all then, but
340 // RegisterMutatorsContext available at registration doesn't have the state
341 // necessary to pass as PathContext to constructPath etc.
342 return
343 }
344 if _, ok := ctx.Module().(DexPreoptModule); !ok {
345 return
346 }
347 dex2oatBin := dex2oatModuleName(ctx.Config())
348 v := ctx.Config().BuildOSTarget.Variations()
349 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
350}
351
352func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
353 dex2oatBin := dex2oatModuleName(ctx.Config())
354
355 dex2oatModule := ctx.GetDirectDepWithTag(dex2oatBin, dex2oatDepTag)
356 if dex2oatModule == nil {
357 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
358 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
359 }
360
361 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
362 if !dex2oatPath.Valid() {
363 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
364 }
365
366 return dex2oatPath.Path()
367}
368
Martin Stjernholmbe9d0d22020-01-10 20:32:59 +0000369// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000370// Should not be used in dexpreopt_gen.
Martin Stjernholmbe9d0d22020-01-10 20:32:59 +0000371func createGlobalSoongConfig(ctx android.ModuleContext) GlobalSoongConfig {
372 if ctx.Config().TestProductVariables != nil {
373 // If we're called in a test there'll be a confusing error from the path
374 // functions below that gets reported without a stack trace, so let's panic
375 // properly with a more helpful message.
376 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
377 }
378
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000379 return GlobalSoongConfig{
380 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholm335d5962020-01-11 00:37:30 +0000381 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000382 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
383 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
384 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
385 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
386 ConstructContext: android.PathForSource(ctx, "build/make/core/construct_context.sh"),
387 }
388}
389
Martin Stjernholm335d5962020-01-11 00:37:30 +0000390// The main reason for this Once cache for GlobalSoongConfig is to make the
391// dex2oat path available to singletons. In ordinary modules we get it through a
392// dex2oatDepTag dependency, but in singletons there's no simple way to do the
393// same thing and ensure the right variant is selected, hence this cache to make
394// the resolved path available to singletons. This means we depend on there
395// being at least one ordinary module with a dex2oatDepTag dependency.
396//
397// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
398// and then possibly remove this cache altogether (but the use in
399// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholmbe9d0d22020-01-10 20:32:59 +0000400var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
401
402// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
403// and later returns the same cached instance.
404func GetGlobalSoongConfig(ctx android.ModuleContext) GlobalSoongConfig {
405 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
406 return createGlobalSoongConfig(ctx)
407 }).(GlobalSoongConfig)
Martin Stjernholm335d5962020-01-11 00:37:30 +0000408
409 // Always resolve the tool path from the dependency, to ensure that every
410 // module has the dependency added properly.
411 myDex2oat := dex2oatPathFromDep(ctx)
412 if myDex2oat != globalSoong.Dex2oat {
413 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
414 }
415
Martin Stjernholmbe9d0d22020-01-10 20:32:59 +0000416 return globalSoong
417}
418
419// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
420// earlier GetGlobalSoongConfig call. This function works with any context
421// compatible with a basic PathContext, since it doesn't try to create a
422// GlobalSoongConfig (which requires a full ModuleContext). It will panic if
423// called before the first GetGlobalSoongConfig call.
424func GetCachedGlobalSoongConfig(ctx android.PathContext) GlobalSoongConfig {
425 return ctx.Config().Get(globalSoongConfigOnceKey).(GlobalSoongConfig)
426}
427
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000428type globalJsonSoongConfig struct {
429 Profman string
430 Dex2oat string
431 Aapt string
432 SoongZip string
433 Zip2zip string
434 ManifestCheck string
435 ConstructContext string
436}
437
Martin Stjernholmdae8a802020-01-20 18:12:23 +0000438// ParseGlobalSoongConfig parses the given data assumed to be read from the
439// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
440// only used in dexpreopt_gen.
441func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000442 var jc globalJsonSoongConfig
443
Colin Cross988414c2020-01-11 01:11:46 +0000444 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000445 if err != nil {
446 return GlobalSoongConfig{}, err
447 }
448
449 config := GlobalSoongConfig{
450 Profman: constructPath(ctx, jc.Profman),
451 Dex2oat: constructPath(ctx, jc.Dex2oat),
452 Aapt: constructPath(ctx, jc.Aapt),
453 SoongZip: constructPath(ctx, jc.SoongZip),
454 Zip2zip: constructPath(ctx, jc.Zip2zip),
455 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
456 ConstructContext: constructPath(ctx, jc.ConstructContext),
457 }
458
459 return config, nil
460}
461
462func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholm335d5962020-01-11 00:37:30 +0000463 if GetGlobalConfig(ctx).DisablePreopt {
464 return
465 }
466
Martin Stjernholmbe9d0d22020-01-10 20:32:59 +0000467 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000468 jc := globalJsonSoongConfig{
469 Profman: config.Profman.String(),
470 Dex2oat: config.Dex2oat.String(),
471 Aapt: config.Aapt.String(),
472 SoongZip: config.SoongZip.String(),
473 Zip2zip: config.Zip2zip.String(),
474 ManifestCheck: config.ManifestCheck.String(),
475 ConstructContext: config.ConstructContext.String(),
476 }
477
478 data, err := json.Marshal(jc)
479 if err != nil {
480 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
481 return
482 }
483
484 ctx.Build(pctx, android.BuildParams{
485 Rule: android.WriteFile,
486 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
487 Args: map[string]string{
488 "content": string(data),
489 },
490 })
491}
492
493func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholm335d5962020-01-11 00:37:30 +0000494 if GetGlobalConfig(ctx).DisablePreopt {
495 return
496 }
497
Martin Stjernholmbe9d0d22020-01-10 20:32:59 +0000498 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000499
500 ctx.Strict("DEX2OAT", config.Dex2oat.String())
501 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
502 config.Profman.String(),
503 config.Dex2oat.String(),
504 config.Aapt.String(),
505 config.SoongZip.String(),
506 config.Zip2zip.String(),
507 config.ManifestCheck.String(),
508 config.ConstructContext.String(),
509 }, " "))
510}
511
Colin Cross69f59a32019-02-15 10:39:37 -0800512func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
513 return GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800514 DisablePreopt: false,
515 DisablePreoptModules: nil,
516 OnlyPreoptBootImageAndSystemServer: false,
517 HasSystemOther: false,
518 PatternsOnSystemOther: nil,
519 DisableGenerateProfile: false,
520 ProfileDir: "",
521 BootJars: nil,
Roshan Piusccc26ef2019-11-27 09:37:46 -0800522 UpdatableBootJars: nil,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100523 ArtApexJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800524 SystemServerJars: nil,
525 SystemServerApps: nil,
Roshan Pius9b51a402019-11-21 12:36:53 -0800526 UpdatableSystemServerJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800527 SpeedApps: nil,
528 PreoptFlags: nil,
529 DefaultCompilerFilter: "",
530 SystemServerCompilerFilter: "",
531 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800532 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700533 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800534 AlwaysSystemServerDebugInfo: false,
535 NeverSystemServerDebugInfo: false,
536 AlwaysOtherDebugInfo: false,
537 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800538 IsEng: false,
539 SanitizeLite: false,
540 DefaultAppImages: false,
541 Dex2oatXmx: "",
542 Dex2oatXms: "",
543 EmptyDirectory: "empty_dir",
544 CpuVariant: nil,
545 InstructionSetFeatures: nil,
546 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800547 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800548 BootFlags: "",
549 Dex2oatImageXmx: "",
550 Dex2oatImageXms: "",
Martin Stjernholmbe9d0d22020-01-10 20:32:59 +0000551 }
552}
553
554func GlobalSoongConfigForTests(config android.Config) GlobalSoongConfig {
555 // Install the test GlobalSoongConfig in the Once cache so that later calls to
556 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
557 return config.Once(globalSoongConfigOnceKey, func() interface{} {
558 return GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700559 Profman: android.PathForTesting("profman"),
560 Dex2oat: android.PathForTesting("dex2oat"),
561 Aapt: android.PathForTesting("aapt"),
562 SoongZip: android.PathForTesting("soong_zip"),
563 Zip2zip: android.PathForTesting("zip2zip"),
564 ManifestCheck: android.PathForTesting("manifest_check"),
565 ConstructContext: android.PathForTesting("construct_context.sh"),
Martin Stjernholmbe9d0d22020-01-10 20:32:59 +0000566 }
567 }).(GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800568}