blob: 1ff2a6eb6fbb6380d2336fc46a41f4e25add35d0 [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 Trafimovich31e444e2020-08-14 17:32:16 +0100103const UnknownInstallLibraryPath = "error"
104
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100105// LibraryPath contains paths to the library DEX jar on host and on device.
106type LibraryPath struct {
107 Host android.Path
108 Device string
109}
110
111// LibraryPaths is a map from library name to on-host and on-device paths to its DEX jar.
112type LibraryPaths map[string]*LibraryPath
113
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100114// Add a new path to the map of library paths, unless a path for this library already exists.
115func (libPaths LibraryPaths) AddLibraryPath(ctx android.PathContext, lib *string, hostPath, installPath android.Path) {
116 if lib == nil {
117 return
118 }
119 if _, present := libPaths[*lib]; !present {
120 var devicePath string
121 if installPath != nil {
122 devicePath = android.InstallPathToOnDevicePath(ctx, installPath.(android.InstallPath))
123 } else {
124 // For some stub libraries the only known thing is the name of their implementation
125 // library, but the library itself is unavailable (missing or part of a prebuilt). In
126 // such cases we still need to add the library to <uses-library> tags in the manifest,
127 // but we cannot use if for dexpreopt.
128 devicePath = UnknownInstallLibraryPath
129 }
130 libPaths[*lib] = &LibraryPath{hostPath, devicePath}
131 }
132 return
133}
134
135// Add library paths from the second map to the first map (do not override existing entries).
136func (libPaths LibraryPaths) AddLibraryPaths(otherPaths LibraryPaths) {
137 for lib, path := range otherPaths {
138 if _, present := libPaths[lib]; !present {
139 libPaths[lib] = path
140 }
141 }
142}
143
Colin Cross43f08db2018-11-12 10:13:39 -0800144type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800145 Name string
146 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800147 BuildPath android.OutputPath
148 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700149 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800150 UncompressedDex bool
151 HasApkLibraries bool
152 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800153
Colin Cross69f59a32019-02-15 10:39:37 -0800154 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800155 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100156 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800157
Ulya Trafimovich6e827482020-06-12 14:32:24 +0100158 EnforceUsesLibraries bool
159 OptionalUsesLibraries []string
160 UsesLibraries []string
161 LibraryPaths LibraryPaths
Colin Cross43f08db2018-11-12 10:13:39 -0800162
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000163 Archs []android.ArchType
164 DexPreoptImages []android.Path
165 DexPreoptImagesDeps []android.OutputPaths
166 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800167
Colin Cross69f59a32019-02-15 10:39:37 -0800168 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
169 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800170
Colin Cross43f08db2018-11-12 10:13:39 -0800171 PreoptExtractedApk bool // Overrides OnlyPreoptModules
172
173 NoCreateAppImage bool
174 ForceCreateAppImage bool
175
176 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800177}
178
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000179type globalSoongConfigSingleton struct{}
180
181var pctx = android.NewPackageContext("android/soong/dexpreopt")
182
183func init() {
184 pctx.Import("android/soong/android")
185 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
186 return &globalSoongConfigSingleton{}
187 })
188}
189
Colin Cross69f59a32019-02-15 10:39:37 -0800190func constructPath(ctx android.PathContext, path string) android.Path {
191 buildDirPrefix := ctx.Config().BuildDir() + "/"
192 if path == "" {
193 return nil
194 } else if strings.HasPrefix(path, buildDirPrefix) {
195 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
196 } else {
197 return android.PathForSource(ctx, path)
198 }
Colin Cross43f08db2018-11-12 10:13:39 -0800199}
200
Colin Cross69f59a32019-02-15 10:39:37 -0800201func constructPaths(ctx android.PathContext, paths []string) android.Paths {
202 var ret android.Paths
203 for _, path := range paths {
204 ret = append(ret, constructPath(ctx, path))
205 }
206 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800207}
208
Colin Cross69f59a32019-02-15 10:39:37 -0800209func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
210 if path == "" {
211 return nil
212 }
213 return constructPath(ctx, path).(android.WritablePath)
214}
215
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000216// ParseGlobalConfig parses the given data assumed to be read from the global
217// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000218func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800219 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000220 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800221
222 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
223 // used to construct the real value manually below.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100224 BootJars []string
225 UpdatableBootJars []string
226 ArtApexJars []string
227 UpdatableSystemServerJars []string
228 DirtyImageObjects string
229 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800230 }
231
232 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000233 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800234 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000235 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800236 }
237
238 // Construct paths that require a PathContext.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100239 config.GlobalConfig.BootJars = android.CreateConfiguredJarList(ctx, config.BootJars)
240 config.GlobalConfig.UpdatableBootJars = android.CreateConfiguredJarList(ctx, config.UpdatableBootJars)
241 config.GlobalConfig.ArtApexJars = android.CreateConfiguredJarList(ctx, config.ArtApexJars)
242 config.GlobalConfig.UpdatableSystemServerJars = android.CreateConfiguredJarList(ctx, config.UpdatableSystemServerJars)
Colin Cross69f59a32019-02-15 10:39:37 -0800243 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800244 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
245
Colin Cross988414c2020-01-11 01:11:46 +0000246 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800247}
248
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000249type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000250 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000251 data []byte
252}
253
254// GetGlobalConfig returns the global dexpreopt.config that's created in the
255// make config phase. It is loaded once the first time it is called for any
256// ctx.Config(), and returns the same data for all future calls with the same
257// ctx.Config(). A value can be inserted for tests using
258// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000259func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000260 return getGlobalConfigRaw(ctx).global
261}
262
263// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
264// the literal content of dexpreopt.config.
265func GetGlobalConfigRawData(ctx android.PathContext) []byte {
266 return getGlobalConfigRaw(ctx).data
267}
268
269var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
270var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
271
272func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
273 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
274 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
275 panic(err)
276 } else if data != nil {
277 globalConfig, err := ParseGlobalConfig(ctx, data)
278 if err != nil {
279 panic(err)
280 }
281 return globalConfigAndRaw{globalConfig, data}
282 }
283
284 // No global config filename set, see if there is a test config set
285 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
286 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000287 return globalConfigAndRaw{&GlobalConfig{
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000288 DisablePreopt: true,
289 DisableGenerateProfile: true,
290 }, nil}
291 })
292 }).(globalConfigAndRaw)
293}
294
295// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
296// will return. It must be called before the first call to GetGlobalConfig for
297// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000298func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000299 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
300}
301
302// ParseModuleConfig parses a per-module dexpreopt.config file into a
303// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
304// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
305// from Make to read the module dexpreopt.config written in the Make config
306// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000307func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100308 type jsonLibraryPath struct {
309 Host string
310 Device string
311 }
312
313 type jsonLibraryPaths map[string]jsonLibraryPath
314
Colin Cross69f59a32019-02-15 10:39:37 -0800315 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000316 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800317
318 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
319 // used to construct the real value manually below.
320 BuildPath string
321 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700322 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800323 ProfileClassListing string
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100324 LibraryPaths jsonLibraryPaths
Colin Cross69f59a32019-02-15 10:39:37 -0800325 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000326 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800327 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800328 }
329
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100330 // convert JSON map of library paths to LibraryPaths
331 constructLibraryPaths := func(ctx android.PathContext, paths jsonLibraryPaths) LibraryPaths {
332 m := LibraryPaths{}
333 for lib, path := range paths {
334 m[lib] = &LibraryPath{
335 constructPath(ctx, path.Host),
336 path.Device,
337 }
338 }
339 return m
340 }
341
Colin Cross69f59a32019-02-15 10:39:37 -0800342 config := ModuleJSONConfig{}
343
Colin Cross988414c2020-01-11 01:11:46 +0000344 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800345 if err != nil {
346 return config.ModuleConfig, err
347 }
348
349 // Construct paths that require a PathContext.
350 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
351 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700352 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800353 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100354 config.ModuleConfig.LibraryPaths = constructLibraryPaths(ctx, config.LibraryPaths)
Colin Cross69f59a32019-02-15 10:39:37 -0800355 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000356 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800357 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800358
Dan Willemsen0f416782019-06-13 21:44:53 +0000359 // 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 +0000360 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000361
Colin Cross69f59a32019-02-15 10:39:37 -0800362 return config.ModuleConfig, nil
363}
364
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000365// dex2oatModuleName returns the name of the module to use for the dex2oat host
366// tool. It should be a binary module with public visibility that is compiled
367// and installed for host.
368func dex2oatModuleName(config android.Config) string {
369 // Default to the debug variant of dex2oat to help find bugs.
370 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
371 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
372 return "dex2oat"
373 } else {
374 return "dex2oatd"
375 }
376}
377
378var dex2oatDepTag = struct {
379 blueprint.BaseDependencyTag
380}{}
381
Martin Stjernholm6d415272020-01-31 17:10:36 +0000382// RegisterToolDeps adds the necessary dependencies to binary modules for tools
383// that are required later when Get(Cached)GlobalSoongConfig is called. It
384// should be called from a mutator that's registered with
385// android.RegistrationContext.FinalDepsMutators.
386func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000387 dex2oatBin := dex2oatModuleName(ctx.Config())
388 v := ctx.Config().BuildOSTarget.Variations()
389 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
390}
391
392func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
393 dex2oatBin := dex2oatModuleName(ctx.Config())
394
395 dex2oatModule := ctx.GetDirectDepWithTag(dex2oatBin, dex2oatDepTag)
396 if dex2oatModule == nil {
397 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
398 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
399 }
400
401 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
402 if !dex2oatPath.Valid() {
403 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
404 }
405
406 return dex2oatPath.Path()
407}
408
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000409// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000410// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000411func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000412 if ctx.Config().TestProductVariables != nil {
413 // If we're called in a test there'll be a confusing error from the path
414 // functions below that gets reported without a stack trace, so let's panic
415 // properly with a more helpful message.
416 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
417 }
418
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000419 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000420 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000421 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000422 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
423 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
424 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
425 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100426 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000427 }
428}
429
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000430// The main reason for this Once cache for GlobalSoongConfig is to make the
431// dex2oat path available to singletons. In ordinary modules we get it through a
432// dex2oatDepTag dependency, but in singletons there's no simple way to do the
433// same thing and ensure the right variant is selected, hence this cache to make
434// the resolved path available to singletons. This means we depend on there
435// being at least one ordinary module with a dex2oatDepTag dependency.
436//
437// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
438// and then possibly remove this cache altogether (but the use in
439// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000440var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
441
442// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
443// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000444func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000445 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
446 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000447 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000448
449 // Always resolve the tool path from the dependency, to ensure that every
450 // module has the dependency added properly.
451 myDex2oat := dex2oatPathFromDep(ctx)
452 if myDex2oat != globalSoong.Dex2oat {
453 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
454 }
455
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000456 return globalSoong
457}
458
459// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
460// earlier GetGlobalSoongConfig call. This function works with any context
461// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000462// GlobalSoongConfig with the proper paths (which requires a full
463// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
464// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000465func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000466 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
467 return (*GlobalSoongConfig)(nil)
468 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000469}
470
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000471type globalJsonSoongConfig struct {
472 Profman string
473 Dex2oat string
474 Aapt string
475 SoongZip string
476 Zip2zip string
477 ManifestCheck string
478 ConstructContext string
479}
480
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000481// ParseGlobalSoongConfig parses the given data assumed to be read from the
482// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
483// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000484func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000485 var jc globalJsonSoongConfig
486
Colin Cross988414c2020-01-11 01:11:46 +0000487 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000488 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000489 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000490 }
491
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000492 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000493 Profman: constructPath(ctx, jc.Profman),
494 Dex2oat: constructPath(ctx, jc.Dex2oat),
495 Aapt: constructPath(ctx, jc.Aapt),
496 SoongZip: constructPath(ctx, jc.SoongZip),
497 Zip2zip: constructPath(ctx, jc.Zip2zip),
498 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
499 ConstructContext: constructPath(ctx, jc.ConstructContext),
500 }
501
502 return config, nil
503}
504
505func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000506 if GetGlobalConfig(ctx).DisablePreopt {
507 return
508 }
509
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000510 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000511 if config == nil {
512 // No module has enabled dexpreopting, so we assume there will be no calls
513 // to dexpreopt_gen.
514 return
515 }
516
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000517 jc := globalJsonSoongConfig{
518 Profman: config.Profman.String(),
519 Dex2oat: config.Dex2oat.String(),
520 Aapt: config.Aapt.String(),
521 SoongZip: config.SoongZip.String(),
522 Zip2zip: config.Zip2zip.String(),
523 ManifestCheck: config.ManifestCheck.String(),
524 ConstructContext: config.ConstructContext.String(),
525 }
526
527 data, err := json.Marshal(jc)
528 if err != nil {
529 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
530 return
531 }
532
533 ctx.Build(pctx, android.BuildParams{
534 Rule: android.WriteFile,
535 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
536 Args: map[string]string{
537 "content": string(data),
538 },
539 })
540}
541
542func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000543 if GetGlobalConfig(ctx).DisablePreopt {
544 return
545 }
546
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000547 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000548 if config == nil {
549 return
550 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000551
552 ctx.Strict("DEX2OAT", config.Dex2oat.String())
553 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
554 config.Profman.String(),
555 config.Dex2oat.String(),
556 config.Aapt.String(),
557 config.SoongZip.String(),
558 config.Zip2zip.String(),
559 config.ManifestCheck.String(),
560 config.ConstructContext.String(),
561 }, " "))
562}
563
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000564func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
565 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800566 DisablePreopt: false,
567 DisablePreoptModules: nil,
568 OnlyPreoptBootImageAndSystemServer: false,
569 HasSystemOther: false,
570 PatternsOnSystemOther: nil,
571 DisableGenerateProfile: false,
572 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100573 BootJars: android.EmptyConfiguredJarList(),
574 UpdatableBootJars: android.EmptyConfiguredJarList(),
575 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800576 SystemServerJars: nil,
577 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100578 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800579 SpeedApps: nil,
580 PreoptFlags: nil,
581 DefaultCompilerFilter: "",
582 SystemServerCompilerFilter: "",
583 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800584 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700585 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800586 AlwaysSystemServerDebugInfo: false,
587 NeverSystemServerDebugInfo: false,
588 AlwaysOtherDebugInfo: false,
589 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800590 IsEng: false,
591 SanitizeLite: false,
592 DefaultAppImages: false,
593 Dex2oatXmx: "",
594 Dex2oatXms: "",
595 EmptyDirectory: "empty_dir",
596 CpuVariant: nil,
597 InstructionSetFeatures: nil,
598 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800599 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800600 BootFlags: "",
601 Dex2oatImageXmx: "",
602 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000603 }
604}
605
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000606func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000607 // Install the test GlobalSoongConfig in the Once cache so that later calls to
608 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
609 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000610 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700611 Profman: android.PathForTesting("profman"),
612 Dex2oat: android.PathForTesting("dex2oat"),
613 Aapt: android.PathForTesting("aapt"),
614 SoongZip: android.PathForTesting("soong_zip"),
615 Zip2zip: android.PathForTesting("zip2zip"),
616 ManifestCheck: android.PathForTesting("manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100617 ConstructContext: android.PathForTesting("construct_context"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000618 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000619 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800620}