blob: 2a929c536351e52b91a7f23bd788b7bdf32adddc [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"
Colin Cross69f59a32019-02-15 10:39:37 -080019 "strings"
Colin Cross74ba9622019-02-11 15:11:14 -080020
21 "android/soong/android"
Colin Cross43f08db2018-11-12 10:13:39 -080022)
23
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000024// GlobalConfig stores the configuration for dex preopting. The fields are set
25// from product variables via dex_preopt_config.mk, except for SoongConfig
26// which come from CreateGlobalSoongConfig.
Colin Cross43f08db2018-11-12 10:13:39 -080027type GlobalConfig struct {
Colin Cross69f59a32019-02-15 10:39:37 -080028 DisablePreopt bool // disable preopt for all modules
Colin Cross43f08db2018-11-12 10:13:39 -080029 DisablePreoptModules []string // modules with preopt disabled by product-specific config
30
31 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
32
Nicolas Geoffray72892f12019-02-22 15:34:40 +000033 GenerateApexImage bool // generate an extra boot image only containing jars from the runtime apex
Nicolas Geoffray25c0e032019-04-04 18:45:20 +010034 UseApexImage bool // use the apex image by default
Nicolas Geoffray72892f12019-02-22 15:34:40 +000035
Colin Cross43f08db2018-11-12 10:13:39 -080036 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
37 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
38
Colin Cross69f59a32019-02-15 10:39:37 -080039 DisableGenerateProfile bool // don't generate profiles
40 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080041
Roshan Piusccc26ef2019-11-27 09:37:46 -080042 BootJars []string // modules for jars that form the boot class path
43 UpdatableBootJars []string // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000044
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000045 ArtApexJars []string // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080046
Roshan Pius9b51a402019-11-21 12:36:53 -080047 SystemServerJars []string // jars that form the system server
48 SystemServerApps []string // apps that are loaded into system server
49 UpdatableSystemServerJars []string // jars within apex that are loaded into system server
50 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080051
52 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
53
54 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
55 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
56
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010057 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080058
59 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070060 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080061 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
62 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
63 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
64 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
65
Colin Cross43f08db2018-11-12 10:13:39 -080066 IsEng bool // build is a eng variant
67 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
68
69 DefaultAppImages bool // build app images (TODO: .art files?) by default
70
Colin Cross800fe132019-02-11 14:21:24 -080071 Dex2oatXmx string // max heap size for dex2oat
72 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080073
74 EmptyDirectory string // path to an empty directory
75
Colin Cross74ba9622019-02-11 15:11:14 -080076 CpuVariant map[android.ArchType]string // cpu variant for each architecture
77 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080078
Colin Cross800fe132019-02-11 14:21:24 -080079 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070080 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
81 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
82 BootFlags string // extra flags to pass to dex2oat for the boot image
83 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
84 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross800fe132019-02-11 14:21:24 -080085
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000086 SoongConfig GlobalSoongConfig // settings read from dexpreopt_soong.config
Colin Cross43f08db2018-11-12 10:13:39 -080087}
88
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000089// GlobalSoongConfig contains the global config that is generated from Soong,
90// stored in dexpreopt_soong.config.
91type GlobalSoongConfig struct {
92 // Paths to tools possibly used by the generated commands.
93 Profman android.Path
94 Dex2oat android.Path
95 Aapt android.Path
96 SoongZip android.Path
97 Zip2zip android.Path
98 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -070099 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800100}
101
102type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800103 Name string
104 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800105 BuildPath android.OutputPath
106 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700107 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800108 UncompressedDex bool
109 HasApkLibraries bool
110 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800111
Colin Cross69f59a32019-02-15 10:39:37 -0800112 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800113 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100114 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800115
Colin Cross50ddcc42019-05-16 12:28:22 -0700116 EnforceUsesLibraries bool
117 PresentOptionalUsesLibraries []string
118 UsesLibraries []string
119 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800120
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000121 Archs []android.ArchType
122 DexPreoptImages []android.Path
123 DexPreoptImagesDeps []android.OutputPaths
124 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800125
Colin Cross69f59a32019-02-15 10:39:37 -0800126 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
127 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800128
Colin Cross43f08db2018-11-12 10:13:39 -0800129 PreoptExtractedApk bool // Overrides OnlyPreoptModules
130
131 NoCreateAppImage bool
132 ForceCreateAppImage bool
133
134 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800135}
136
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000137type globalSoongConfigSingleton struct{}
138
139var pctx = android.NewPackageContext("android/soong/dexpreopt")
140
141func init() {
142 pctx.Import("android/soong/android")
143 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
144 return &globalSoongConfigSingleton{}
145 })
146}
147
Colin Cross69f59a32019-02-15 10:39:37 -0800148func constructPath(ctx android.PathContext, path string) android.Path {
149 buildDirPrefix := ctx.Config().BuildDir() + "/"
150 if path == "" {
151 return nil
152 } else if strings.HasPrefix(path, buildDirPrefix) {
153 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
154 } else {
155 return android.PathForSource(ctx, path)
156 }
Colin Cross43f08db2018-11-12 10:13:39 -0800157}
158
Colin Cross69f59a32019-02-15 10:39:37 -0800159func constructPaths(ctx android.PathContext, paths []string) android.Paths {
160 var ret android.Paths
161 for _, path := range paths {
162 ret = append(ret, constructPath(ctx, path))
163 }
164 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800165}
166
Colin Cross69f59a32019-02-15 10:39:37 -0800167func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
168 ret := map[string]android.Path{}
169 for key, path := range paths {
170 ret[key] = constructPath(ctx, path)
171 }
172 return ret
173}
174
175func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
176 if path == "" {
177 return nil
178 }
179 return constructPath(ctx, path).(android.WritablePath)
180}
181
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000182// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig
183// struct, except the SoongConfig field which is set from the provided
184// soongConfig argument. LoadGlobalConfig is used directly in Soong and in
185// dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by
186// Make.
Colin Cross988414c2020-01-11 01:11:46 +0000187func LoadGlobalConfig(ctx android.PathContext, data []byte, soongConfig GlobalSoongConfig) (GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800188 type GlobalJSONConfig struct {
189 GlobalConfig
190
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.
193 DirtyImageObjects string
Colin Cross69f59a32019-02-15 10:39:37 -0800194 BootImageProfiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800195 }
196
197 config := GlobalJSONConfig{}
Colin Cross988414c2020-01-11 01:11:46 +0000198 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800199 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000200 return config.GlobalConfig, err
Colin Cross69f59a32019-02-15 10:39:37 -0800201 }
202
203 // Construct paths that require a PathContext.
204 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800205 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
206
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000207 // Set this here to force the caller to provide a value for this struct (from
208 // either CreateGlobalSoongConfig or LoadGlobalSoongConfig).
209 config.GlobalConfig.SoongConfig = soongConfig
Colin Cross69f59a32019-02-15 10:39:37 -0800210
Colin Cross988414c2020-01-11 01:11:46 +0000211 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800212}
213
214// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct. It is not used in Soong, which
215// receives a ModuleConfig struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called from oMake to
216// read the module dexpreopt.config written by Make.
Colin Cross988414c2020-01-11 01:11:46 +0000217func LoadModuleConfig(ctx android.PathContext, data []byte) (ModuleConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800218 type ModuleJSONConfig struct {
219 ModuleConfig
220
221 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
222 // used to construct the real value manually below.
223 BuildPath string
224 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700225 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800226 ProfileClassListing string
227 LibraryPaths map[string]string
228 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000229 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800230 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800231 }
232
233 config := ModuleJSONConfig{}
234
Colin Cross988414c2020-01-11 01:11:46 +0000235 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800236 if err != nil {
237 return config.ModuleConfig, err
238 }
239
240 // Construct paths that require a PathContext.
241 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
242 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700243 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800244 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
245 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
246 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000247 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800248 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800249
Dan Willemsen0f416782019-06-13 21:44:53 +0000250 // 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 +0000251 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000252
Colin Cross69f59a32019-02-15 10:39:37 -0800253 return config.ModuleConfig, nil
254}
255
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000256// CreateGlobalSoongConfig creates a GlobalSoongConfig from the current context.
257// Should not be used in dexpreopt_gen.
258func CreateGlobalSoongConfig(ctx android.PathContext) GlobalSoongConfig {
259 // Default to debug version to help find bugs.
260 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
261 var dex2oatBinary string
262 if ctx.Config().Getenv("USE_DEX2OAT_DEBUG") == "false" {
263 dex2oatBinary = "dex2oat"
264 } else {
265 dex2oatBinary = "dex2oatd"
266 }
267
268 return GlobalSoongConfig{
269 Profman: ctx.Config().HostToolPath(ctx, "profman"),
270 Dex2oat: ctx.Config().HostToolPath(ctx, dex2oatBinary),
271 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
272 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
273 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
274 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
275 ConstructContext: android.PathForSource(ctx, "build/make/core/construct_context.sh"),
276 }
277}
278
279type globalJsonSoongConfig struct {
280 Profman string
281 Dex2oat string
282 Aapt string
283 SoongZip string
284 Zip2zip string
285 ManifestCheck string
286 ConstructContext string
287}
288
289// LoadGlobalSoongConfig reads the dexpreopt_soong.config file into a
290// GlobalSoongConfig struct. It is only used in dexpreopt_gen.
Colin Cross988414c2020-01-11 01:11:46 +0000291func LoadGlobalSoongConfig(ctx android.PathContext, data []byte) (GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000292 var jc globalJsonSoongConfig
293
Colin Cross988414c2020-01-11 01:11:46 +0000294 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000295 if err != nil {
296 return GlobalSoongConfig{}, err
297 }
298
299 config := GlobalSoongConfig{
300 Profman: constructPath(ctx, jc.Profman),
301 Dex2oat: constructPath(ctx, jc.Dex2oat),
302 Aapt: constructPath(ctx, jc.Aapt),
303 SoongZip: constructPath(ctx, jc.SoongZip),
304 Zip2zip: constructPath(ctx, jc.Zip2zip),
305 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
306 ConstructContext: constructPath(ctx, jc.ConstructContext),
307 }
308
309 return config, nil
310}
311
312func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
313 config := CreateGlobalSoongConfig(ctx)
314 jc := globalJsonSoongConfig{
315 Profman: config.Profman.String(),
316 Dex2oat: config.Dex2oat.String(),
317 Aapt: config.Aapt.String(),
318 SoongZip: config.SoongZip.String(),
319 Zip2zip: config.Zip2zip.String(),
320 ManifestCheck: config.ManifestCheck.String(),
321 ConstructContext: config.ConstructContext.String(),
322 }
323
324 data, err := json.Marshal(jc)
325 if err != nil {
326 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
327 return
328 }
329
330 ctx.Build(pctx, android.BuildParams{
331 Rule: android.WriteFile,
332 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
333 Args: map[string]string{
334 "content": string(data),
335 },
336 })
337}
338
339func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
340 config := CreateGlobalSoongConfig(ctx)
341
342 ctx.Strict("DEX2OAT", config.Dex2oat.String())
343 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
344 config.Profman.String(),
345 config.Dex2oat.String(),
346 config.Aapt.String(),
347 config.SoongZip.String(),
348 config.Zip2zip.String(),
349 config.ManifestCheck.String(),
350 config.ConstructContext.String(),
351 }, " "))
352}
353
Colin Cross69f59a32019-02-15 10:39:37 -0800354func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
355 return GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800356 DisablePreopt: false,
357 DisablePreoptModules: nil,
358 OnlyPreoptBootImageAndSystemServer: false,
359 HasSystemOther: false,
360 PatternsOnSystemOther: nil,
361 DisableGenerateProfile: false,
362 ProfileDir: "",
363 BootJars: nil,
Roshan Piusccc26ef2019-11-27 09:37:46 -0800364 UpdatableBootJars: nil,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100365 ArtApexJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800366 SystemServerJars: nil,
367 SystemServerApps: nil,
Roshan Pius9b51a402019-11-21 12:36:53 -0800368 UpdatableSystemServerJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800369 SpeedApps: nil,
370 PreoptFlags: nil,
371 DefaultCompilerFilter: "",
372 SystemServerCompilerFilter: "",
373 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800374 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700375 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800376 AlwaysSystemServerDebugInfo: false,
377 NeverSystemServerDebugInfo: false,
378 AlwaysOtherDebugInfo: false,
379 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800380 IsEng: false,
381 SanitizeLite: false,
382 DefaultAppImages: false,
383 Dex2oatXmx: "",
384 Dex2oatXms: "",
385 EmptyDirectory: "empty_dir",
386 CpuVariant: nil,
387 InstructionSetFeatures: nil,
388 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800389 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800390 BootFlags: "",
391 Dex2oatImageXmx: "",
392 Dex2oatImageXms: "",
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000393 SoongConfig: GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700394 Profman: android.PathForTesting("profman"),
395 Dex2oat: android.PathForTesting("dex2oat"),
396 Aapt: android.PathForTesting("aapt"),
397 SoongZip: android.PathForTesting("soong_zip"),
398 Zip2zip: android.PathForTesting("zip2zip"),
399 ManifestCheck: android.PathForTesting("manifest_check"),
400 ConstructContext: android.PathForTesting("construct_context.sh"),
Colin Cross69f59a32019-02-15 10:39:37 -0800401 },
402 }
403}