blob: db5e97ae9062cadc892cf5003a5231531e26c68c [file] [log] [blame]
Colin Cross43f08db2018-11-12 10:13:39 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package dexpreopt
16
17import (
18 "encoding/json"
Martin Stjernholmd90676f2020-01-11 00:37:30 +000019 "fmt"
Colin Cross69f59a32019-02-15 10:39:37 -080020 "strings"
Colin Cross74ba9622019-02-11 15:11:14 -080021
Martin Stjernholmd90676f2020-01-11 00:37:30 +000022 "github.com/google/blueprint"
23
Colin Cross74ba9622019-02-11 15:11:14 -080024 "android/soong/android"
Colin Cross43f08db2018-11-12 10:13:39 -080025)
26
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000027// GlobalConfig stores the configuration for dex preopting. The fields are set
Martin Stjernholm75a48d82020-01-10 20:32:59 +000028// from product variables via dex_preopt_config.mk.
Colin Cross43f08db2018-11-12 10:13:39 -080029type GlobalConfig struct {
Colin Cross69f59a32019-02-15 10:39:37 -080030 DisablePreopt bool // disable preopt for all modules
Colin Cross43f08db2018-11-12 10:13:39 -080031 DisablePreoptModules []string // modules with preopt disabled by product-specific config
32
33 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
34
Vladimir Marko40139d62020-02-06 15:14:29 +000035 UseArtImage bool // use the art image (use other boot class path dex files without image)
36
Colin Cross43f08db2018-11-12 10:13:39 -080037 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
38 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
39
Colin Cross69f59a32019-02-15 10:39:37 -080040 DisableGenerateProfile bool // don't generate profiles
41 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080042
Ulya Trafimovich249386a2020-07-01 14:31:13 +010043 BootJars android.ConfiguredJarList // modules for jars that form the boot class path
44 UpdatableBootJars android.ConfiguredJarList // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000045
Ulya Trafimovich249386a2020-07-01 14:31:13 +010046 ArtApexJars android.ConfiguredJarList // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080047
Ulya Trafimovich249386a2020-07-01 14:31:13 +010048 SystemServerJars []string // jars that form the system server
49 SystemServerApps []string // apps that are loaded into system server
50 UpdatableSystemServerJars android.ConfiguredJarList // jars within apex that are loaded into system server
51 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080052
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +000053 BrokenSuboptimalOrderOfSystemServerJars bool // if true, sub-optimal order does not cause a build error
54
Colin Cross43f08db2018-11-12 10:13:39 -080055 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
56
57 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
58 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
59
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010060 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080061
62 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070063 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080064 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
65 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
66 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
67 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
68
Colin Cross43f08db2018-11-12 10:13:39 -080069 IsEng bool // build is a eng variant
70 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
71
72 DefaultAppImages bool // build app images (TODO: .art files?) by default
73
Colin Cross800fe132019-02-11 14:21:24 -080074 Dex2oatXmx string // max heap size for dex2oat
75 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080076
77 EmptyDirectory string // path to an empty directory
78
Colin Cross74ba9622019-02-11 15:11:14 -080079 CpuVariant map[android.ArchType]string // cpu variant for each architecture
80 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080081
Colin Cross800fe132019-02-11 14:21:24 -080082 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070083 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
84 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
85 BootFlags string // extra flags to pass to dex2oat for the boot image
86 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
87 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross43f08db2018-11-12 10:13:39 -080088}
89
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000090// GlobalSoongConfig contains the global config that is generated from Soong,
91// stored in dexpreopt_soong.config.
92type GlobalSoongConfig struct {
93 // Paths to tools possibly used by the generated commands.
94 Profman android.Path
95 Dex2oat android.Path
96 Aapt android.Path
97 SoongZip android.Path
98 Zip2zip android.Path
99 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700100 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800101}
102
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100103// LibraryPath contains paths to the library DEX jar on host and on device.
104type LibraryPath struct {
105 Host android.Path
106 Device string
107}
108
109// LibraryPaths is a map from library name to on-host and on-device paths to its DEX jar.
110type LibraryPaths map[string]*LibraryPath
111
Colin Cross43f08db2018-11-12 10:13:39 -0800112type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800113 Name string
114 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800115 BuildPath android.OutputPath
116 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700117 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800118 UncompressedDex bool
119 HasApkLibraries bool
120 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800121
Colin Cross69f59a32019-02-15 10:39:37 -0800122 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800123 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100124 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800125
Ulya Trafimovich6e827482020-06-12 14:32:24 +0100126 EnforceUsesLibraries bool
127 OptionalUsesLibraries []string
128 UsesLibraries []string
129 LibraryPaths LibraryPaths
Colin Cross43f08db2018-11-12 10:13:39 -0800130
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000131 Archs []android.ArchType
132 DexPreoptImages []android.Path
133 DexPreoptImagesDeps []android.OutputPaths
134 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800135
Colin Cross69f59a32019-02-15 10:39:37 -0800136 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
137 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800138
Colin Cross43f08db2018-11-12 10:13:39 -0800139 PreoptExtractedApk bool // Overrides OnlyPreoptModules
140
141 NoCreateAppImage bool
142 ForceCreateAppImage bool
143
144 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800145}
146
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000147type globalSoongConfigSingleton struct{}
148
149var pctx = android.NewPackageContext("android/soong/dexpreopt")
150
151func init() {
152 pctx.Import("android/soong/android")
153 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
154 return &globalSoongConfigSingleton{}
155 })
156}
157
Colin Cross69f59a32019-02-15 10:39:37 -0800158func constructPath(ctx android.PathContext, path string) android.Path {
159 buildDirPrefix := ctx.Config().BuildDir() + "/"
160 if path == "" {
161 return nil
162 } else if strings.HasPrefix(path, buildDirPrefix) {
163 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
164 } else {
165 return android.PathForSource(ctx, path)
166 }
Colin Cross43f08db2018-11-12 10:13:39 -0800167}
168
Colin Cross69f59a32019-02-15 10:39:37 -0800169func constructPaths(ctx android.PathContext, paths []string) android.Paths {
170 var ret android.Paths
171 for _, path := range paths {
172 ret = append(ret, constructPath(ctx, path))
173 }
174 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800175}
176
Colin Cross69f59a32019-02-15 10:39:37 -0800177func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
178 if path == "" {
179 return nil
180 }
181 return constructPath(ctx, path).(android.WritablePath)
182}
183
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000184// ParseGlobalConfig parses the given data assumed to be read from the global
185// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000186func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800187 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000188 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800189
190 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
191 // used to construct the real value manually below.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100192 BootJars []string
193 UpdatableBootJars []string
194 ArtApexJars []string
195 UpdatableSystemServerJars []string
196 DirtyImageObjects string
197 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800198 }
199
200 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000201 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800202 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000203 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800204 }
205
206 // Construct paths that require a PathContext.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100207 config.GlobalConfig.BootJars = android.CreateConfiguredJarList(ctx, config.BootJars)
208 config.GlobalConfig.UpdatableBootJars = android.CreateConfiguredJarList(ctx, config.UpdatableBootJars)
209 config.GlobalConfig.ArtApexJars = android.CreateConfiguredJarList(ctx, config.ArtApexJars)
210 config.GlobalConfig.UpdatableSystemServerJars = android.CreateConfiguredJarList(ctx, config.UpdatableSystemServerJars)
Colin Cross69f59a32019-02-15 10:39:37 -0800211 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800212 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
213
Colin Cross988414c2020-01-11 01:11:46 +0000214 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800215}
216
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000217type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000218 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000219 data []byte
220}
221
222// GetGlobalConfig returns the global dexpreopt.config that's created in the
223// make config phase. It is loaded once the first time it is called for any
224// ctx.Config(), and returns the same data for all future calls with the same
225// ctx.Config(). A value can be inserted for tests using
226// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000227func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000228 return getGlobalConfigRaw(ctx).global
229}
230
231// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
232// the literal content of dexpreopt.config.
233func GetGlobalConfigRawData(ctx android.PathContext) []byte {
234 return getGlobalConfigRaw(ctx).data
235}
236
237var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
238var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
239
240func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
241 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
242 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
243 panic(err)
244 } else if data != nil {
245 globalConfig, err := ParseGlobalConfig(ctx, data)
246 if err != nil {
247 panic(err)
248 }
249 return globalConfigAndRaw{globalConfig, data}
250 }
251
252 // No global config filename set, see if there is a test config set
253 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
254 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000255 return globalConfigAndRaw{&GlobalConfig{
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000256 DisablePreopt: true,
257 DisableGenerateProfile: true,
258 }, nil}
259 })
260 }).(globalConfigAndRaw)
261}
262
263// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
264// will return. It must be called before the first call to GetGlobalConfig for
265// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000266func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000267 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
268}
269
270// ParseModuleConfig parses a per-module dexpreopt.config file into a
271// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
272// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
273// from Make to read the module dexpreopt.config written in the Make config
274// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000275func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100276 type jsonLibraryPath struct {
277 Host string
278 Device string
279 }
280
281 type jsonLibraryPaths map[string]jsonLibraryPath
282
Colin Cross69f59a32019-02-15 10:39:37 -0800283 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000284 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800285
286 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
287 // used to construct the real value manually below.
288 BuildPath string
289 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700290 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800291 ProfileClassListing string
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100292 LibraryPaths jsonLibraryPaths
Colin Cross69f59a32019-02-15 10:39:37 -0800293 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000294 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800295 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800296 }
297
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100298 // convert JSON map of library paths to LibraryPaths
299 constructLibraryPaths := func(ctx android.PathContext, paths jsonLibraryPaths) LibraryPaths {
300 m := LibraryPaths{}
301 for lib, path := range paths {
302 m[lib] = &LibraryPath{
303 constructPath(ctx, path.Host),
304 path.Device,
305 }
306 }
307 return m
308 }
309
Colin Cross69f59a32019-02-15 10:39:37 -0800310 config := ModuleJSONConfig{}
311
Colin Cross988414c2020-01-11 01:11:46 +0000312 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800313 if err != nil {
314 return config.ModuleConfig, err
315 }
316
317 // Construct paths that require a PathContext.
318 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
319 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700320 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800321 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100322 config.ModuleConfig.LibraryPaths = constructLibraryPaths(ctx, config.LibraryPaths)
Colin Cross69f59a32019-02-15 10:39:37 -0800323 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000324 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800325 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800326
Dan Willemsen0f416782019-06-13 21:44:53 +0000327 // 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 +0000328 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000329
Colin Cross69f59a32019-02-15 10:39:37 -0800330 return config.ModuleConfig, nil
331}
332
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000333// dex2oatModuleName returns the name of the module to use for the dex2oat host
334// tool. It should be a binary module with public visibility that is compiled
335// and installed for host.
336func dex2oatModuleName(config android.Config) string {
337 // Default to the debug variant of dex2oat to help find bugs.
338 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
339 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
340 return "dex2oat"
341 } else {
342 return "dex2oatd"
343 }
344}
345
346var dex2oatDepTag = struct {
347 blueprint.BaseDependencyTag
348}{}
349
Martin Stjernholm6d415272020-01-31 17:10:36 +0000350// RegisterToolDeps adds the necessary dependencies to binary modules for tools
351// that are required later when Get(Cached)GlobalSoongConfig is called. It
352// should be called from a mutator that's registered with
353// android.RegistrationContext.FinalDepsMutators.
354func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000355 dex2oatBin := dex2oatModuleName(ctx.Config())
356 v := ctx.Config().BuildOSTarget.Variations()
357 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
358}
359
360func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
361 dex2oatBin := dex2oatModuleName(ctx.Config())
362
363 dex2oatModule := ctx.GetDirectDepWithTag(dex2oatBin, dex2oatDepTag)
364 if dex2oatModule == nil {
365 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
366 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
367 }
368
369 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
370 if !dex2oatPath.Valid() {
371 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
372 }
373
374 return dex2oatPath.Path()
375}
376
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000377// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000378// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000379func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000380 if ctx.Config().TestProductVariables != nil {
381 // If we're called in a test there'll be a confusing error from the path
382 // functions below that gets reported without a stack trace, so let's panic
383 // properly with a more helpful message.
384 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
385 }
386
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000387 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000388 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000389 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000390 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
391 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
392 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
393 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100394 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000395 }
396}
397
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000398// The main reason for this Once cache for GlobalSoongConfig is to make the
399// dex2oat path available to singletons. In ordinary modules we get it through a
400// dex2oatDepTag dependency, but in singletons there's no simple way to do the
401// same thing and ensure the right variant is selected, hence this cache to make
402// the resolved path available to singletons. This means we depend on there
403// being at least one ordinary module with a dex2oatDepTag dependency.
404//
405// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
406// and then possibly remove this cache altogether (but the use in
407// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000408var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
409
410// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
411// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000412func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000413 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
414 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000415 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000416
417 // Always resolve the tool path from the dependency, to ensure that every
418 // module has the dependency added properly.
419 myDex2oat := dex2oatPathFromDep(ctx)
420 if myDex2oat != globalSoong.Dex2oat {
421 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
422 }
423
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000424 return globalSoong
425}
426
427// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
428// earlier GetGlobalSoongConfig call. This function works with any context
429// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000430// GlobalSoongConfig with the proper paths (which requires a full
431// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
432// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000433func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000434 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
435 return (*GlobalSoongConfig)(nil)
436 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000437}
438
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000439type globalJsonSoongConfig struct {
440 Profman string
441 Dex2oat string
442 Aapt string
443 SoongZip string
444 Zip2zip string
445 ManifestCheck string
446 ConstructContext string
447}
448
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000449// ParseGlobalSoongConfig parses the given data assumed to be read from the
450// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
451// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000452func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000453 var jc globalJsonSoongConfig
454
Colin Cross988414c2020-01-11 01:11:46 +0000455 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000456 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000457 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000458 }
459
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000460 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000461 Profman: constructPath(ctx, jc.Profman),
462 Dex2oat: constructPath(ctx, jc.Dex2oat),
463 Aapt: constructPath(ctx, jc.Aapt),
464 SoongZip: constructPath(ctx, jc.SoongZip),
465 Zip2zip: constructPath(ctx, jc.Zip2zip),
466 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
467 ConstructContext: constructPath(ctx, jc.ConstructContext),
468 }
469
470 return config, nil
471}
472
473func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000474 if GetGlobalConfig(ctx).DisablePreopt {
475 return
476 }
477
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000478 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000479 if config == nil {
480 // No module has enabled dexpreopting, so we assume there will be no calls
481 // to dexpreopt_gen.
482 return
483 }
484
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000485 jc := globalJsonSoongConfig{
486 Profman: config.Profman.String(),
487 Dex2oat: config.Dex2oat.String(),
488 Aapt: config.Aapt.String(),
489 SoongZip: config.SoongZip.String(),
490 Zip2zip: config.Zip2zip.String(),
491 ManifestCheck: config.ManifestCheck.String(),
492 ConstructContext: config.ConstructContext.String(),
493 }
494
495 data, err := json.Marshal(jc)
496 if err != nil {
497 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
498 return
499 }
500
501 ctx.Build(pctx, android.BuildParams{
502 Rule: android.WriteFile,
503 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
504 Args: map[string]string{
505 "content": string(data),
506 },
507 })
508}
509
510func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000511 if GetGlobalConfig(ctx).DisablePreopt {
512 return
513 }
514
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000515 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000516 if config == nil {
517 return
518 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000519
520 ctx.Strict("DEX2OAT", config.Dex2oat.String())
521 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
522 config.Profman.String(),
523 config.Dex2oat.String(),
524 config.Aapt.String(),
525 config.SoongZip.String(),
526 config.Zip2zip.String(),
527 config.ManifestCheck.String(),
528 config.ConstructContext.String(),
529 }, " "))
530}
531
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000532func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
533 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800534 DisablePreopt: false,
535 DisablePreoptModules: nil,
536 OnlyPreoptBootImageAndSystemServer: false,
537 HasSystemOther: false,
538 PatternsOnSystemOther: nil,
539 DisableGenerateProfile: false,
540 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100541 BootJars: android.EmptyConfiguredJarList(),
542 UpdatableBootJars: android.EmptyConfiguredJarList(),
543 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800544 SystemServerJars: nil,
545 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100546 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800547 SpeedApps: nil,
548 PreoptFlags: nil,
549 DefaultCompilerFilter: "",
550 SystemServerCompilerFilter: "",
551 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800552 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700553 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800554 AlwaysSystemServerDebugInfo: false,
555 NeverSystemServerDebugInfo: false,
556 AlwaysOtherDebugInfo: false,
557 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800558 IsEng: false,
559 SanitizeLite: false,
560 DefaultAppImages: false,
561 Dex2oatXmx: "",
562 Dex2oatXms: "",
563 EmptyDirectory: "empty_dir",
564 CpuVariant: nil,
565 InstructionSetFeatures: nil,
566 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800567 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800568 BootFlags: "",
569 Dex2oatImageXmx: "",
570 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000571 }
572}
573
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000574func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000575 // Install the test GlobalSoongConfig in the Once cache so that later calls to
576 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
577 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000578 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700579 Profman: android.PathForTesting("profman"),
580 Dex2oat: android.PathForTesting("dex2oat"),
581 Aapt: android.PathForTesting("aapt"),
582 SoongZip: android.PathForTesting("soong_zip"),
583 Zip2zip: android.PathForTesting("zip2zip"),
584 ManifestCheck: android.PathForTesting("manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100585 ConstructContext: android.PathForTesting("construct_context"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000586 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000587 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800588}