blob: 151f032fb3b9868634285dde1ada7fd585b47b13 [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 {
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +000030 DisablePreopt bool // disable preopt for all modules (excluding boot images)
31 DisablePreoptBootImages bool // disable prepot for boot images
32 DisablePreoptModules []string // modules with preopt disabled by product-specific config
Colin Cross43f08db2018-11-12 10:13:39 -080033
34 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
35
Ulya Trafimovich9023b022021-03-22 16:02:28 +000036 PreoptWithUpdatableBcp bool // If updatable boot jars are included in dexpreopt or not.
37
Vladimir Marko40139d62020-02-06 15:14:29 +000038 UseArtImage bool // use the art image (use other boot class path dex files without image)
39
Colin Cross43f08db2018-11-12 10:13:39 -080040 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
41 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
42
Colin Cross69f59a32019-02-15 10:39:37 -080043 DisableGenerateProfile bool // don't generate profiles
44 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080045
Ulya Trafimovich249386a2020-07-01 14:31:13 +010046 BootJars android.ConfiguredJarList // modules for jars that form the boot class path
47 UpdatableBootJars android.ConfiguredJarList // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000048
Ulya Trafimovich249386a2020-07-01 14:31:13 +010049 ArtApexJars android.ConfiguredJarList // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080050
Ulya Trafimovich249386a2020-07-01 14:31:13 +010051 SystemServerJars []string // jars that form the system server
52 SystemServerApps []string // apps that are loaded into system server
53 UpdatableSystemServerJars android.ConfiguredJarList // jars within apex that are loaded into system server
54 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080055
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +000056 BrokenSuboptimalOrderOfSystemServerJars bool // if true, sub-optimal order does not cause a build error
57
Colin Cross43f08db2018-11-12 10:13:39 -080058 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
59
60 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
61 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
62
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010063 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080064
65 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070066 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080067 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
68 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
69 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
70 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
71
Colin Cross43f08db2018-11-12 10:13:39 -080072 IsEng bool // build is a eng variant
73 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
74
75 DefaultAppImages bool // build app images (TODO: .art files?) by default
76
Colin Cross800fe132019-02-11 14:21:24 -080077 Dex2oatXmx string // max heap size for dex2oat
78 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080079
80 EmptyDirectory string // path to an empty directory
81
Colin Cross74ba9622019-02-11 15:11:14 -080082 CpuVariant map[android.ArchType]string // cpu variant for each architecture
83 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080084
Nicolas Geoffray1086e602021-01-20 14:30:40 +000085 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
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000089
Ulya Trafimovich4a13acb2021-03-02 12:25:02 +000090 // If true, downgrade the compiler filter of dexpreopt to "verify" when verify_uses_libraries
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000091 // check fails, instead of failing the build. This will disable any AOT-compilation.
92 //
93 // The intended use case for this flag is to have a smoother migration path for the Java
94 // modules that need to add <uses-library> information in their build files. The flag allows to
95 // quickly silence build errors. This flag should be used with caution and only as a temporary
96 // measure, as it masks real errors and affects performance.
97 RelaxUsesLibraryCheck bool
Colin Cross43f08db2018-11-12 10:13:39 -080098}
99
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000100// GlobalSoongConfig contains the global config that is generated from Soong,
101// stored in dexpreopt_soong.config.
102type GlobalSoongConfig struct {
103 // Paths to tools possibly used by the generated commands.
104 Profman android.Path
105 Dex2oat android.Path
106 Aapt android.Path
107 SoongZip android.Path
108 Zip2zip android.Path
109 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700110 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800111}
112
113type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800114 Name string
115 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800116 BuildPath android.OutputPath
117 DexPath android.Path
Jeongik Cha33a3a812021-04-15 09:12:49 +0900118 ManifestPath android.OptionalPath
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800119 UncompressedDex bool
120 HasApkLibraries bool
121 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800122
Colin Cross69f59a32019-02-15 10:39:37 -0800123 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800124 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100125 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800126
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000127 EnforceUsesLibraries bool // turn on build-time verify_uses_libraries check
128 EnforceUsesLibrariesStatusFile android.Path // a file with verify_uses_libraries errors (if any)
129 ProvidesUsesLibrary string // library name (usually the same as module name)
130 ClassLoaderContexts ClassLoaderContextMap
Colin Cross43f08db2018-11-12 10:13:39 -0800131
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000132 Archs []android.ArchType
Jeongik Chac6246672021-04-08 00:00:19 +0900133 DexPreoptImages android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000134 DexPreoptImagesDeps []android.OutputPaths
135 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800136
Colin Cross69f59a32019-02-15 10:39:37 -0800137 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
138 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800139
Colin Cross43f08db2018-11-12 10:13:39 -0800140 PreoptExtractedApk bool // Overrides OnlyPreoptModules
141
142 NoCreateAppImage bool
143 ForceCreateAppImage bool
144
145 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800146}
147
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000148type globalSoongConfigSingleton struct{}
149
150var pctx = android.NewPackageContext("android/soong/dexpreopt")
151
152func init() {
153 pctx.Import("android/soong/android")
154 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
155 return &globalSoongConfigSingleton{}
156 })
157}
158
Colin Cross69f59a32019-02-15 10:39:37 -0800159func constructPath(ctx android.PathContext, path string) android.Path {
160 buildDirPrefix := ctx.Config().BuildDir() + "/"
161 if path == "" {
162 return nil
163 } else if strings.HasPrefix(path, buildDirPrefix) {
164 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
165 } else {
166 return android.PathForSource(ctx, path)
167 }
Colin Cross43f08db2018-11-12 10:13:39 -0800168}
169
Colin Cross69f59a32019-02-15 10:39:37 -0800170func constructPaths(ctx android.PathContext, paths []string) android.Paths {
171 var ret android.Paths
172 for _, path := range paths {
173 ret = append(ret, constructPath(ctx, path))
174 }
175 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800176}
177
Colin Cross69f59a32019-02-15 10:39:37 -0800178func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
179 if path == "" {
180 return nil
181 }
182 return constructPath(ctx, path).(android.WritablePath)
183}
184
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000185// ParseGlobalConfig parses the given data assumed to be read from the global
186// dexpreopt.config file into a GlobalConfig struct.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000187func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800188 type GlobalJSONConfig struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000189 *GlobalConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800190
191 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
192 // used to construct the real value manually below.
Paul Duffin7ccacae2020-10-23 21:14:20 +0100193 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800194 }
195
196 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000197 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800198 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000199 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800200 }
201
202 // Construct paths that require a PathContext.
Colin Cross69f59a32019-02-15 10:39:37 -0800203 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
204
Colin Cross988414c2020-01-11 01:11:46 +0000205 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800206}
207
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000208type globalConfigAndRaw struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000209 global *GlobalConfig
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000210 data []byte
211}
212
213// GetGlobalConfig returns the global dexpreopt.config that's created in the
214// make config phase. It is loaded once the first time it is called for any
215// ctx.Config(), and returns the same data for all future calls with the same
216// ctx.Config(). A value can be inserted for tests using
217// setDexpreoptTestGlobalConfig.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000218func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000219 return getGlobalConfigRaw(ctx).global
220}
221
222// GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
223// the literal content of dexpreopt.config.
224func GetGlobalConfigRawData(ctx android.PathContext) []byte {
225 return getGlobalConfigRaw(ctx).data
226}
227
228var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
229var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
230
231func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
232 return ctx.Config().Once(globalConfigOnceKey, func() interface{} {
233 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
234 panic(err)
235 } else if data != nil {
236 globalConfig, err := ParseGlobalConfig(ctx, data)
237 if err != nil {
238 panic(err)
239 }
240 return globalConfigAndRaw{globalConfig, data}
241 }
242
243 // No global config filename set, see if there is a test config set
244 return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} {
245 // Nope, return a config with preopting disabled
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000246 return globalConfigAndRaw{&GlobalConfig{
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000247 DisablePreopt: true,
248 DisablePreoptBootImages: true,
249 DisableGenerateProfile: true,
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000250 }, nil}
251 })
252 }).(globalConfigAndRaw)
253}
254
255// SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
256// will return. It must be called before the first call to GetGlobalConfig for
257// the config.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000258func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000259 config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
260}
261
Jeongik Chac6246672021-04-08 00:00:19 +0900262// This struct is required to convert ModuleConfig from/to JSON.
263// The types of fields in ModuleConfig are not convertible,
264// so moduleJSONConfig has those fields as a convertible type.
265type moduleJSONConfig struct {
266 *ModuleConfig
267
268 BuildPath string
269 DexPath string
270 ManifestPath string
271
272 ProfileClassListing string
273 ProfileBootListing string
274
275 EnforceUsesLibrariesStatusFile string
276 ClassLoaderContexts jsonClassLoaderContextMap
277
278 DexPreoptImages []string
279 DexPreoptImagesDeps [][]string
280
281 PreoptBootClassPathDexFiles []string
282}
283
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000284// ParseModuleConfig parses a per-module dexpreopt.config file into a
285// ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
286// struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
287// from Make to read the module dexpreopt.config written in the Make config
288// stage.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000289func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
Jeongik Chac6246672021-04-08 00:00:19 +0900290 config := moduleJSONConfig{}
Colin Cross69f59a32019-02-15 10:39:37 -0800291
Colin Cross988414c2020-01-11 01:11:46 +0000292 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800293 if err != nil {
294 return config.ModuleConfig, err
295 }
296
297 // Construct paths that require a PathContext.
298 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
299 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Jeongik Cha33a3a812021-04-15 09:12:49 +0900300 config.ModuleConfig.ManifestPath = android.OptionalPathForPath(constructPath(ctx, config.ManifestPath))
Colin Cross69f59a32019-02-15 10:39:37 -0800301 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000302 config.ModuleConfig.EnforceUsesLibrariesStatusFile = constructPath(ctx, config.EnforceUsesLibrariesStatusFile)
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000303 config.ModuleConfig.ClassLoaderContexts = fromJsonClassLoaderContext(ctx, config.ClassLoaderContexts)
Colin Cross69f59a32019-02-15 10:39:37 -0800304 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
305 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800306
Dan Willemsen0f416782019-06-13 21:44:53 +0000307 // 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 +0000308 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000309
Colin Cross69f59a32019-02-15 10:39:37 -0800310 return config.ModuleConfig, nil
311}
312
Jeongik Chac6246672021-04-08 00:00:19 +0900313func pathsListToStringLists(pathsList []android.OutputPaths) [][]string {
314 ret := make([][]string, 0, len(pathsList))
315 for _, paths := range pathsList {
316 ret = append(ret, paths.Strings())
317 }
318 return ret
319}
320
321func moduleConfigToJSON(config *ModuleConfig) ([]byte, error) {
322 return json.MarshalIndent(&moduleJSONConfig{
323 BuildPath: config.BuildPath.String(),
324 DexPath: config.DexPath.String(),
325 ManifestPath: config.ManifestPath.String(),
326 ProfileClassListing: config.ProfileClassListing.String(),
327 ProfileBootListing: config.ProfileBootListing.String(),
328 EnforceUsesLibrariesStatusFile: config.EnforceUsesLibrariesStatusFile.String(),
329 ClassLoaderContexts: toJsonClassLoaderContext(config.ClassLoaderContexts),
330 DexPreoptImages: config.DexPreoptImages.Strings(),
331 DexPreoptImagesDeps: pathsListToStringLists(config.DexPreoptImagesDeps),
332 PreoptBootClassPathDexFiles: config.PreoptBootClassPathDexFiles.Strings(),
333 ModuleConfig: config,
334 }, "", " ")
335}
336
337// WriteModuleConfig serializes a ModuleConfig into a per-module dexpreopt.config JSON file.
338// These config files are used for post-processing.
339func WriteModuleConfig(ctx android.ModuleContext, config *ModuleConfig, path android.WritablePath) {
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000340 if path == nil {
341 return
342 }
343
Jeongik Chac6246672021-04-08 00:00:19 +0900344 data, err := moduleConfigToJSON(config)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000345 if err != nil {
346 ctx.ModuleErrorf("failed to JSON marshal module dexpreopt.config: %v", err)
347 return
348 }
349
350 android.WriteFileRule(ctx, path, string(data))
351}
352
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000353// dex2oatModuleName returns the name of the module to use for the dex2oat host
354// tool. It should be a binary module with public visibility that is compiled
355// and installed for host.
356func dex2oatModuleName(config android.Config) string {
357 // Default to the debug variant of dex2oat to help find bugs.
358 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
359 if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
360 return "dex2oat"
361 } else {
362 return "dex2oatd"
363 }
364}
365
Paul Duffinb506c9d2021-03-24 14:34:40 +0000366type dex2oatDependencyTag struct {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000367 blueprint.BaseDependencyTag
Paul Duffinb506c9d2021-03-24 14:34:40 +0000368}
369
370func (d dex2oatDependencyTag) ExcludeFromVisibilityEnforcement() {
371}
372
373func (d dex2oatDependencyTag) ExcludeFromApexContents() {
374}
375
376// Dex2oatDepTag represents the dependency onto the dex2oatd module. It is added to any module that
377// needs dexpreopting and so it makes no sense for it to be checked for visibility or included in
378// the apex.
379var Dex2oatDepTag = dex2oatDependencyTag{}
380
381var _ android.ExcludeFromVisibilityEnforcementTag = Dex2oatDepTag
382var _ android.ExcludeFromApexContentsTag = Dex2oatDepTag
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000383
Martin Stjernholm6d415272020-01-31 17:10:36 +0000384// RegisterToolDeps adds the necessary dependencies to binary modules for tools
385// that are required later when Get(Cached)GlobalSoongConfig is called. It
386// should be called from a mutator that's registered with
387// android.RegistrationContext.FinalDepsMutators.
388func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000389 dex2oatBin := dex2oatModuleName(ctx.Config())
390 v := ctx.Config().BuildOSTarget.Variations()
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000391 ctx.AddFarVariationDependencies(v, Dex2oatDepTag, dex2oatBin)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000392}
393
394func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
395 dex2oatBin := dex2oatModuleName(ctx.Config())
396
Martin Stjernholmc0048622020-08-18 17:37:41 +0100397 // Find the right dex2oat module, trying to follow PrebuiltDepTag from source
398 // to prebuilt if there is one. We wouldn't have to do this if the
399 // prebuilt_postdeps mutator that replaces source deps with prebuilt deps was
400 // run after RegisterToolDeps above, but changing that leads to ordering
401 // problems between mutators (RegisterToolDeps needs to run late to act on
402 // final variants, while prebuilt_postdeps needs to run before many of the
403 // PostDeps mutators, like the APEX mutators). Hence we need to dig out the
404 // prebuilt explicitly here instead.
405 var dex2oatModule android.Module
406 ctx.WalkDeps(func(child, parent android.Module) bool {
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000407 if parent == ctx.Module() && ctx.OtherModuleDependencyTag(child) == Dex2oatDepTag {
Martin Stjernholmc0048622020-08-18 17:37:41 +0100408 // Found the source module, or prebuilt module that has replaced the source.
409 dex2oatModule = child
410 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil {
411 return false // If it's the prebuilt we're done.
412 } else {
413 return true // Recurse to check if the source has a prebuilt dependency.
414 }
415 }
416 if parent == dex2oatModule && ctx.OtherModuleDependencyTag(child) == android.PrebuiltDepTag {
417 if p, ok := child.(android.PrebuiltInterface); ok && p.Prebuilt() != nil && p.Prebuilt().UsePrebuilt() {
418 dex2oatModule = child // Found a prebuilt that should be used.
419 }
420 }
421 return false
422 })
423
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000424 if dex2oatModule == nil {
425 // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
426 panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
427 }
428
429 dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
430 if !dex2oatPath.Valid() {
431 panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
432 }
433
434 return dex2oatPath.Path()
435}
436
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000437// createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000438// Should not be used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000439func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000440 return &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000441 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000442 Dex2oat: dex2oatPathFromDep(ctx),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000443 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
444 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
445 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
446 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100447 ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000448 }
449}
450
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000451// The main reason for this Once cache for GlobalSoongConfig is to make the
452// dex2oat path available to singletons. In ordinary modules we get it through a
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000453// Dex2oatDepTag dependency, but in singletons there's no simple way to do the
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000454// same thing and ensure the right variant is selected, hence this cache to make
455// the resolved path available to singletons. This means we depend on there
Ulya Trafimovicha4a1c4e2021-01-15 18:40:04 +0000456// being at least one ordinary module with a Dex2oatDepTag dependency.
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000457//
458// TODO(b/147613152): Implement a way to deal with dependencies from singletons,
Paul Duffin9f045242021-01-21 15:05:11 +0000459// and then possibly remove this cache altogether.
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000460var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
461
462// GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
463// and later returns the same cached instance.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000464func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000465 globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
466 return createGlobalSoongConfig(ctx)
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000467 }).(*GlobalSoongConfig)
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000468
469 // Always resolve the tool path from the dependency, to ensure that every
470 // module has the dependency added properly.
471 myDex2oat := dex2oatPathFromDep(ctx)
472 if myDex2oat != globalSoong.Dex2oat {
473 panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
474 }
475
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000476 return globalSoong
477}
478
479// GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
480// earlier GetGlobalSoongConfig call. This function works with any context
481// compatible with a basic PathContext, since it doesn't try to create a
Martin Stjernholm6d415272020-01-31 17:10:36 +0000482// GlobalSoongConfig with the proper paths (which requires a full
483// ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
484// is returned.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000485func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000486 return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
487 return (*GlobalSoongConfig)(nil)
488 }).(*GlobalSoongConfig)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000489}
490
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000491type globalJsonSoongConfig struct {
492 Profman string
493 Dex2oat string
494 Aapt string
495 SoongZip string
496 Zip2zip string
497 ManifestCheck string
498 ConstructContext string
499}
500
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000501// ParseGlobalSoongConfig parses the given data assumed to be read from the
502// global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
503// only used in dexpreopt_gen.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000504func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000505 var jc globalJsonSoongConfig
506
Colin Cross988414c2020-01-11 01:11:46 +0000507 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000508 if err != nil {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000509 return &GlobalSoongConfig{}, err
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000510 }
511
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000512 config := &GlobalSoongConfig{
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000513 Profman: constructPath(ctx, jc.Profman),
514 Dex2oat: constructPath(ctx, jc.Dex2oat),
515 Aapt: constructPath(ctx, jc.Aapt),
516 SoongZip: constructPath(ctx, jc.SoongZip),
517 Zip2zip: constructPath(ctx, jc.Zip2zip),
518 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
519 ConstructContext: constructPath(ctx, jc.ConstructContext),
520 }
521
522 return config, nil
523}
524
525func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000526 if GetGlobalConfig(ctx).DisablePreopt {
527 return
528 }
529
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000530 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000531 if config == nil {
532 // No module has enabled dexpreopting, so we assume there will be no calls
533 // to dexpreopt_gen.
534 return
535 }
536
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000537 jc := globalJsonSoongConfig{
538 Profman: config.Profman.String(),
539 Dex2oat: config.Dex2oat.String(),
540 Aapt: config.Aapt.String(),
541 SoongZip: config.SoongZip.String(),
542 Zip2zip: config.Zip2zip.String(),
543 ManifestCheck: config.ManifestCheck.String(),
544 ConstructContext: config.ConstructContext.String(),
545 }
546
547 data, err := json.Marshal(jc)
548 if err != nil {
549 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
550 return
551 }
552
Colin Crosscf371cc2020-11-13 11:48:42 -0800553 android.WriteFileRule(ctx, android.PathForOutput(ctx, "dexpreopt_soong.config"), string(data))
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000554}
555
556func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000557 if GetGlobalConfig(ctx).DisablePreopt {
558 return
559 }
560
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000561 config := GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm6d415272020-01-31 17:10:36 +0000562 if config == nil {
563 return
564 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000565
566 ctx.Strict("DEX2OAT", config.Dex2oat.String())
567 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
568 config.Profman.String(),
569 config.Dex2oat.String(),
570 config.Aapt.String(),
571 config.SoongZip.String(),
572 config.Zip2zip.String(),
573 config.ManifestCheck.String(),
574 config.ConstructContext.String(),
575 }, " "))
576}
577
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000578func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
579 return &GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800580 DisablePreopt: false,
581 DisablePreoptModules: nil,
582 OnlyPreoptBootImageAndSystemServer: false,
583 HasSystemOther: false,
584 PatternsOnSystemOther: nil,
585 DisableGenerateProfile: false,
586 ProfileDir: "",
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100587 BootJars: android.EmptyConfiguredJarList(),
588 UpdatableBootJars: android.EmptyConfiguredJarList(),
589 ArtApexJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800590 SystemServerJars: nil,
591 SystemServerApps: nil,
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100592 UpdatableSystemServerJars: android.EmptyConfiguredJarList(),
Colin Cross69f59a32019-02-15 10:39:37 -0800593 SpeedApps: nil,
594 PreoptFlags: nil,
595 DefaultCompilerFilter: "",
596 SystemServerCompilerFilter: "",
597 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800598 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700599 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800600 AlwaysSystemServerDebugInfo: false,
601 NeverSystemServerDebugInfo: false,
602 AlwaysOtherDebugInfo: false,
603 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800604 IsEng: false,
605 SanitizeLite: false,
606 DefaultAppImages: false,
607 Dex2oatXmx: "",
608 Dex2oatXms: "",
609 EmptyDirectory: "empty_dir",
610 CpuVariant: nil,
611 InstructionSetFeatures: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800612 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800613 BootFlags: "",
614 Dex2oatImageXmx: "",
615 Dex2oatImageXms: "",
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000616 }
617}
618
Paul Duffin9f045242021-01-21 15:05:11 +0000619func globalSoongConfigForTests() *GlobalSoongConfig {
620 return &GlobalSoongConfig{
621 Profman: android.PathForTesting("profman"),
622 Dex2oat: android.PathForTesting("dex2oat"),
623 Aapt: android.PathForTesting("aapt"),
624 SoongZip: android.PathForTesting("soong_zip"),
625 Zip2zip: android.PathForTesting("zip2zip"),
626 ManifestCheck: android.PathForTesting("manifest_check"),
627 ConstructContext: android.PathForTesting("construct_context"),
628 }
Colin Cross69f59a32019-02-15 10:39:37 -0800629}