blob: 064d9d9c58aa6e8ba52958067bb7bbe9b911dcd8 [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"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010020 "sort"
Colin Cross69f59a32019-02-15 10:39:37 -080021 "strings"
Colin Cross74ba9622019-02-11 15:11:14 -080022
Martin Stjernholmd90676f2020-01-11 00:37:30 +000023 "github.com/google/blueprint"
24
Colin Cross74ba9622019-02-11 15:11:14 -080025 "android/soong/android"
Colin Cross43f08db2018-11-12 10:13:39 -080026)
27
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000028// GlobalConfig stores the configuration for dex preopting. The fields are set
Martin Stjernholm75a48d82020-01-10 20:32:59 +000029// from product variables via dex_preopt_config.mk.
Colin Cross43f08db2018-11-12 10:13:39 -080030type GlobalConfig struct {
Colin Cross69f59a32019-02-15 10:39:37 -080031 DisablePreopt bool // disable preopt for all modules
Colin Cross43f08db2018-11-12 10:13:39 -080032 DisablePreoptModules []string // modules with preopt disabled by product-specific config
33
34 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
35
Vladimir Marko40139d62020-02-06 15:14:29 +000036 UseArtImage bool // use the art image (use other boot class path dex files without image)
37
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
Ulya Trafimovich249386a2020-07-01 14:31:13 +010044 BootJars android.ConfiguredJarList // modules for jars that form the boot class path
45 UpdatableBootJars android.ConfiguredJarList // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000046
Ulya Trafimovich249386a2020-07-01 14:31:13 +010047 ArtApexJars android.ConfiguredJarList // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080048
Ulya Trafimovich249386a2020-07-01 14:31:13 +010049 SystemServerJars []string // jars that form the system server
50 SystemServerApps []string // apps that are loaded into system server
51 UpdatableSystemServerJars android.ConfiguredJarList // 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
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +000054 BrokenSuboptimalOrderOfSystemServerJars bool // if true, sub-optimal order does not cause a build error
55
Colin Cross43f08db2018-11-12 10:13:39 -080056 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
57
58 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
59 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
60
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010061 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080062
63 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070064 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080065 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
66 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
67 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
68 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
69
Colin Cross43f08db2018-11-12 10:13:39 -080070 IsEng bool // build is a eng variant
71 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
72
73 DefaultAppImages bool // build app images (TODO: .art files?) by default
74
Colin Cross800fe132019-02-11 14:21:24 -080075 Dex2oatXmx string // max heap size for dex2oat
76 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080077
78 EmptyDirectory string // path to an empty directory
79
Colin Cross74ba9622019-02-11 15:11:14 -080080 CpuVariant map[android.ArchType]string // cpu variant for each architecture
81 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080082
Colin Cross800fe132019-02-11 14:21:24 -080083 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070084 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
85 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
86 BootFlags string // extra flags to pass to dex2oat for the boot image
87 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
88 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross43f08db2018-11-12 10:13:39 -080089}
90
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000091// GlobalSoongConfig contains the global config that is generated from Soong,
92// stored in dexpreopt_soong.config.
93type GlobalSoongConfig struct {
94 // Paths to tools possibly used by the generated commands.
95 Profman android.Path
96 Dex2oat android.Path
97 Aapt android.Path
98 SoongZip android.Path
99 Zip2zip android.Path
100 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700101 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800102}
103
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100104const UnknownInstallLibraryPath = "error"
105
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100106// LibraryPath contains paths to the library DEX jar on host and on device.
107type LibraryPath struct {
108 Host android.Path
109 Device string
110}
111
112// LibraryPaths is a map from library name to on-host and on-device paths to its DEX jar.
113type LibraryPaths map[string]*LibraryPath
114
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100115// Add a new path to the map of library paths, unless a path for this library already exists.
116func (libPaths LibraryPaths) AddLibraryPath(ctx android.PathContext, lib *string, hostPath, installPath android.Path) {
117 if lib == nil {
118 return
119 }
120 if _, present := libPaths[*lib]; !present {
121 var devicePath string
122 if installPath != nil {
123 devicePath = android.InstallPathToOnDevicePath(ctx, installPath.(android.InstallPath))
124 } else {
125 // For some stub libraries the only known thing is the name of their implementation
126 // library, but the library itself is unavailable (missing or part of a prebuilt). In
127 // such cases we still need to add the library to <uses-library> tags in the manifest,
128 // but we cannot use if for dexpreopt.
129 devicePath = UnknownInstallLibraryPath
130 }
131 libPaths[*lib] = &LibraryPath{hostPath, devicePath}
132 }
133 return
134}
135
136// Add library paths from the second map to the first map (do not override existing entries).
137func (libPaths LibraryPaths) AddLibraryPaths(otherPaths LibraryPaths) {
138 for lib, path := range otherPaths {
139 if _, present := libPaths[lib]; !present {
140 libPaths[lib] = path
141 }
142 }
143}
144
145// Return sorted names of the libraries in the map.
146func (libPaths LibraryPaths) Names() []string {
147 keys := make([]string, 0, len(libPaths))
148 for k := range libPaths {
149 keys = append(keys, k)
150 }
151 sort.Strings(keys)
152 return keys
153}
154
Colin Cross43f08db2018-11-12 10:13:39 -0800155type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800156 Name string
157 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800158 BuildPath android.OutputPath
159 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700160 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800161 UncompressedDex bool
162 HasApkLibraries bool
163 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800164
Colin Cross69f59a32019-02-15 10:39:37 -0800165 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800166 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100167 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800168
Ulya Trafimovich6e827482020-06-12 14:32:24 +0100169 EnforceUsesLibraries bool
170 OptionalUsesLibraries []string
171 UsesLibraries []string
172 LibraryPaths LibraryPaths
Colin Cross43f08db2018-11-12 10:13:39 -0800173
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000174 Archs []android.ArchType
175 DexPreoptImages []android.Path
176 DexPreoptImagesDeps []android.OutputPaths
177 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800178
Colin Cross69f59a32019-02-15 10:39:37 -0800179 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
180 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800181
Colin Cross43f08db2018-11-12 10:13:39 -0800182 PreoptExtractedApk bool // Overrides OnlyPreoptModules
183
184 NoCreateAppImage bool
185 ForceCreateAppImage bool
186
187 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800188}
189
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000190type globalSoongConfigSingleton struct{}
191
192var pctx = android.NewPackageContext("android/soong/dexpreopt")
193
194func init() {
195 pctx.Import("android/soong/android")
196 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
197 return &globalSoongConfigSingleton{}
198 })
199}
200
Colin Cross69f59a32019-02-15 10:39:37 -0800201func constructPath(ctx android.PathContext, path string) android.Path {
202 buildDirPrefix := ctx.Config().BuildDir() + "/"
203 if path == "" {
204 return nil
205 } else if strings.HasPrefix(path, buildDirPrefix) {
206 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
207 } else {
208 return android.PathForSource(ctx, path)
209 }
Colin Cross43f08db2018-11-12 10:13:39 -0800210}
211
Colin Cross69f59a32019-02-15 10:39:37 -0800212func constructPaths(ctx android.PathContext, paths []string) android.Paths {
213 var ret android.Paths
214 for _, path := range paths {
215 ret = append(ret, constructPath(ctx, path))
216 }
217 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800218}
219
Colin Cross69f59a32019-02-15 10:39:37 -0800220func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
221 if path == "" {
222 return nil
223 }
224 return constructPath(ctx, path).(android.WritablePath)
225}
226
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000227// ParseGlobalConfig parses the given data assumed to be read from the global
228// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000229func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800230 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000231 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800232
233 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
234 // used to construct the real value manually below.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100235 BootJars []string
236 UpdatableBootJars []string
237 ArtApexJars []string
238 UpdatableSystemServerJars []string
239 DirtyImageObjects string
240 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800241 }
242
243 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000244 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800245 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000246 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800247 }
248
249 // Construct paths that require a PathContext.
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100250 config.GlobalConfig.BootJars = android.CreateConfiguredJarList(ctx, config.BootJars)
251 config.GlobalConfig.UpdatableBootJars = android.CreateConfiguredJarList(ctx, config.UpdatableBootJars)
252 config.GlobalConfig.ArtApexJars = android.CreateConfiguredJarList(ctx, config.ArtApexJars)
253 config.GlobalConfig.UpdatableSystemServerJars = android.CreateConfiguredJarList(ctx, config.UpdatableSystemServerJars)
Colin Cross69f59a32019-02-15 10:39:37 -0800254 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800255 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
256
Colin Cross988414c2020-01-11 01:11:46 +0000257 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800258}
259
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000260type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000261 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000262 data []byte
263}
264
265// GetGlobalConfig returns the global dexpreopt.config that's created in the
266// make config phase. It is loaded once the first time it is called for any
267// ctx.Config(), and returns the same data for all future calls with the same
268// ctx.Config(). A value can be inserted for tests using
269// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000270func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000271 return getGlobalConfigRaw(ctx).global
272}
273
274// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
275// the literal content of dexpreopt.config.
276func GetGlobalConfigRawData(ctx android.PathContext) []byte {
277 return getGlobalConfigRaw(ctx).data
278}
279
280var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
281var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
282
283func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
284 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
285 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
286 panic(err)
287 } else if data != nil {
288 globalConfig, err := ParseGlobalConfig(ctx, data)
289 if err != nil {
290 panic(err)
291 }
292 return globalConfigAndRaw{globalConfig, data}
293 }
294
295 // No global config filename set, see if there is a test config set
296 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
297 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000298 return globalConfigAndRaw{&GlobalConfig{
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000299 DisablePreopt: true,
300 DisableGenerateProfile: true,
301 }, nil}
302 })
303 }).(globalConfigAndRaw)
304}
305
306// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
307// will return. It must be called before the first call to GetGlobalConfig for
308// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000309func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000310 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
311}
312
313// ParseModuleConfig parses a per-module dexpreopt.config file into a
314// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
315// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
316// from Make to read the module dexpreopt.config written in the Make config
317// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000318func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100319 type jsonLibraryPath struct {
320 Host string
321 Device string
322 }
323
324 type jsonLibraryPaths map[string]jsonLibraryPath
325
Colin Cross69f59a32019-02-15 10:39:37 -0800326 type ModuleJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000327 *ModuleConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800328
329 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
330 // used to construct the real value manually below.
331 BuildPath string
332 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700333 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800334 ProfileClassListing string
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100335 LibraryPaths jsonLibraryPaths
Colin Cross69f59a32019-02-15 10:39:37 -0800336 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000337 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800338 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800339 }
340
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100341 // convert JSON map of library paths to LibraryPaths
342 constructLibraryPaths := func(ctx android.PathContext, paths jsonLibraryPaths) LibraryPaths {
343 m := LibraryPaths{}
344 for lib, path := range paths {
345 m[lib] = &LibraryPath{
346 constructPath(ctx, path.Host),
347 path.Device,
348 }
349 }
350 return m
351 }
352
Colin Cross69f59a32019-02-15 10:39:37 -0800353 config := ModuleJSONConfig{}
354
Colin Cross988414c2020-01-11 01:11:46 +0000355 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800356 if err != nil {
357 return config.ModuleConfig, err
358 }
359
360 // Construct paths that require a PathContext.
361 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
362 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700363 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800364 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovichd4bcea42020-06-03 14:57:22 +0100365 config.ModuleConfig.LibraryPaths = constructLibraryPaths(ctx, config.LibraryPaths)
Colin Cross69f59a32019-02-15 10:39:37 -0800366 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000367 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800368 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800369
Dan Willemsen0f416782019-06-13 21:44:53 +0000370 // 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 +0000371 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000372
Colin Cross69f59a32019-02-15 10:39:37 -0800373 return config.ModuleConfig, nil
374}
375
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000376// dex2oatModuleName returns the name of the module to use for the dex2oat host
377// tool. It should be a binary module with public visibility that is compiled
378// and installed for host.
379func dex2oatModuleName(config android.Config) string {
380 // Default to the debug variant of dex2oat to help find bugs.
381 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
382 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
383 return "dex2oat"
384 } else {
385 return "dex2oatd"
386 }
387}
388
389var dex2oatDepTag = struct {
390 blueprint.BaseDependencyTag
391}{}
392
Martin Stjernholm6d415272020-01-31 17:10:36 +0000393// RegisterToolDeps adds the necessary dependencies to binary modules for tools
394// that are required later when Get(Cached)GlobalSoongConfig is called. It
395// should be called from a mutator that's registered with
396// android.RegistrationContext.FinalDepsMutators.
397func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000398 dex2oatBin := dex2oatModuleName(ctx.Config())
399 v := ctx.Config().BuildOSTarget.Variations()
400 ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin)
401}
402
403func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
404 dex2oatBin := dex2oatModuleName(ctx.Config())
405
406 dex2oatModule := ctx.GetDirectDepWithTag(dex2oatBin, dex2oatDepTag)
407 if dex2oatModule == nil {
408 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
409 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
410 }
411
412 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
413 if !dex2oatPath.Valid() {
414 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
415 }
416
417 return dex2oatPath.Path()
418}
419
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000420// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000421// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000422func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000423 if ctx.Config().TestProductVariables != nil {
424 // If we're called in a test there'll be a confusing error from the path
425 // functions below that gets reported without a stack trace, so let's panic
426 // properly with a more helpful message.
427 panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.")
428 }
429
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000430 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000431 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000432 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000433 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
434 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
435 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
436 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100437 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000438 }
439}
440
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000441// The main reason for this Once cache for GlobalSoongConfig is to make the
442// dex2oat path available to singletons. In ordinary modules we get it through a
443// dex2oatDepTag dependency, but in singletons there's no simple way to do the
444// same thing and ensure the right variant is selected, hence this cache to make
445// the resolved path available to singletons. This means we depend on there
446// being at least one ordinary module with a dex2oatDepTag dependency.
447//
448// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
449// and then possibly remove this cache altogether (but the use in
450// GlobalSoongConfigForTests also needs to be rethought).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000451var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
452
453// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
454// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000455func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000456 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
457 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000458 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000459
460 // Always resolve the tool path from the dependency, to ensure that every
461 // module has the dependency added properly.
462 myDex2oat := dex2oatPathFromDep(ctx)
463 if myDex2oat != globalSoong.Dex2oat {
464 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
465 }
466
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000467 return globalSoong
468}
469
470// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
471// earlier GetGlobalSoongConfig call. This function works with any context
472// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000473// GlobalSoongConfig with the proper paths (which requires a full
474// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
475// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000476func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000477 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
478 return (*GlobalSoongConfig)(nil)
479 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000480}
481
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000482type globalJsonSoongConfig struct {
483 Profman string
484 Dex2oat string
485 Aapt string
486 SoongZip string
487 Zip2zip string
488 ManifestCheck string
489 ConstructContext string
490}
491
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000492// ParseGlobalSoongConfig parses the given data assumed to be read from the
493// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
494// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000495func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000496 var jc globalJsonSoongConfig
497
Colin Cross988414c2020-01-11 01:11:46 +0000498 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000499 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000500 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000501 }
502
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000503 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000504 Profman: constructPath(ctx, jc.Profman),
505 Dex2oat: constructPath(ctx, jc.Dex2oat),
506 Aapt: constructPath(ctx, jc.Aapt),
507 SoongZip: constructPath(ctx, jc.SoongZip),
508 Zip2zip: constructPath(ctx, jc.Zip2zip),
509 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
510 ConstructContext: constructPath(ctx, jc.ConstructContext),
511 }
512
513 return config, nil
514}
515
516func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000517 if GetGlobalConfig(ctx).DisablePreopt {
518 return
519 }
520
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000521 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000522 if config == nil {
523 // No module has enabled dexpreopting, so we assume there will be no calls
524 // to dexpreopt_gen.
525 return
526 }
527
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000528 jc := globalJsonSoongConfig{
529 Profman: config.Profman.String(),
530 Dex2oat: config.Dex2oat.String(),
531 Aapt: config.Aapt.String(),
532 SoongZip: config.SoongZip.String(),
533 Zip2zip: config.Zip2zip.String(),
534 ManifestCheck: config.ManifestCheck.String(),
535 ConstructContext: config.ConstructContext.String(),
536 }
537
538 data, err := json.Marshal(jc)
539 if err != nil {
540 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
541 return
542 }
543
544 ctx.Build(pctx, android.BuildParams{
545 Rule: android.WriteFile,
546 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
547 Args: map[string]string{
548 "content": string(data),
549 },
550 })
551}
552
553func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000554 if GetGlobalConfig(ctx).DisablePreopt {
555 return
556 }
557
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000558 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000559 if config == nil {
560 return
561 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000562
563 ctx.Strict("DEX2OAT", config.Dex2oat.String())
564 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
565 config.Profman.String(),
566 config.Dex2oat.String(),
567 config.Aapt.String(),
568 config.SoongZip.String(),
569 config.Zip2zip.String(),
570 config.ManifestCheck.String(),
571 config.ConstructContext.String(),
572 }, " "))
573}
574
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000575func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
576 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800577 DisablePreopt: false,
578 DisablePreoptModules: nil,
579 OnlyPreoptBootImageAndSystemServer: false,
580 HasSystemOther: false,
581 PatternsOnSystemOther: nil,
582 DisableGenerateProfile: false,
583 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100584 BootJars: android.EmptyConfiguredJarList(),
585 UpdatableBootJars: android.EmptyConfiguredJarList(),
586 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800587 SystemServerJars: nil,
588 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100589 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800590 SpeedApps: nil,
591 PreoptFlags: nil,
592 DefaultCompilerFilter: "",
593 SystemServerCompilerFilter: "",
594 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800595 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700596 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800597 AlwaysSystemServerDebugInfo: false,
598 NeverSystemServerDebugInfo: false,
599 AlwaysOtherDebugInfo: false,
600 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800601 IsEng: false,
602 SanitizeLite: false,
603 DefaultAppImages: false,
604 Dex2oatXmx: "",
605 Dex2oatXms: "",
606 EmptyDirectory: "empty_dir",
607 CpuVariant: nil,
608 InstructionSetFeatures: nil,
609 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800610 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800611 BootFlags: "",
612 Dex2oatImageXmx: "",
613 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000614 }
615}
616
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000617func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000618 // Install the test GlobalSoongConfig in the Once cache so that later calls to
619 // Get(Cached)GlobalSoongConfig returns it without trying to create a real one.
620 return config.Once(globalSoongConfigOnceKey, func() interface{} {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000621 return &GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700622 Profman: android.PathForTesting("profman"),
623 Dex2oat: android.PathForTesting("dex2oat"),
624 Aapt: android.PathForTesting("aapt"),
625 SoongZip: android.PathForTesting("soong_zip"),
626 Zip2zip: android.PathForTesting("zip2zip"),
627 ManifestCheck: android.PathForTesting("manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100628 ConstructContext: android.PathForTesting("construct_context"),
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000629 }
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000630 }).(*GlobalSoongConfig)
Colin Cross69f59a32019-02-15 10:39:37 -0800631}