blob: e35387841911764bfb03270fe9ae55f04720c8c5 [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
Hans Boehme4b53422020-01-25 01:44:30 +000025// 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
Vladimir Marko40139d62020-02-06 15:14:29 +000033 UseArtImage bool // use the art image (use other boot class path dex files without image)
34
Colin Cross43f08db2018-11-12 10:13:39 -080035 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
36 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
37
Colin Cross69f59a32019-02-15 10:39:37 -080038 DisableGenerateProfile bool // don't generate profiles
39 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080040
Roshan Piusccc26ef2019-11-27 09:37:46 -080041 BootJars []string // modules for jars that form the boot class path
42 UpdatableBootJars []string // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000043
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000044 ArtApexJars []string // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080045
Roshan Pius9b51a402019-11-21 12:36:53 -080046 SystemServerJars []string // jars that form the system server
47 SystemServerApps []string // apps that are loaded into system server
48 UpdatableSystemServerJars []string // jars within apex that are loaded into system server
49 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080050
51 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
52
53 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
54 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
55
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010056 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080057
58 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070059 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080060 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
61 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
62 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
63 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
64
Colin Cross43f08db2018-11-12 10:13:39 -080065 IsEng bool // build is a eng variant
66 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
67
68 DefaultAppImages bool // build app images (TODO: .art files?) by default
69
Colin Cross800fe132019-02-11 14:21:24 -080070 Dex2oatXmx string // max heap size for dex2oat
71 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080072
73 EmptyDirectory string // path to an empty directory
74
Colin Cross74ba9622019-02-11 15:11:14 -080075 CpuVariant map[android.ArchType]string // cpu variant for each architecture
76 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080077
Colin Cross800fe132019-02-11 14:21:24 -080078 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070079 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
80 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
81 BootFlags string // extra flags to pass to dex2oat for the boot image
82 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
83 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Hans Boehme4b53422020-01-25 01:44:30 +000084
85 SoongConfig GlobalSoongConfig // settings read from dexpreopt_soong.config
Colin Cross43f08db2018-11-12 10:13:39 -080086}
87
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000088// GlobalSoongConfig contains the global config that is generated from Soong,
89// stored in dexpreopt_soong.config.
90type GlobalSoongConfig struct {
91 // Paths to tools possibly used by the generated commands.
92 Profman android.Path
93 Dex2oat android.Path
94 Aapt android.Path
95 SoongZip android.Path
96 Zip2zip android.Path
97 ManifestCheck android.Path
Colin Cross38b96852019-05-22 10:21:09 -070098 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -080099}
100
101type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800102 Name string
103 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800104 BuildPath android.OutputPath
105 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700106 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800107 UncompressedDex bool
108 HasApkLibraries bool
109 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800110
Colin Cross69f59a32019-02-15 10:39:37 -0800111 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800112 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100113 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800114
Colin Cross50ddcc42019-05-16 12:28:22 -0700115 EnforceUsesLibraries bool
116 PresentOptionalUsesLibraries []string
117 UsesLibraries []string
118 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800119
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000120 Archs []android.ArchType
121 DexPreoptImages []android.Path
122 DexPreoptImagesDeps []android.OutputPaths
123 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800124
Colin Cross69f59a32019-02-15 10:39:37 -0800125 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
126 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800127
Colin Cross43f08db2018-11-12 10:13:39 -0800128 PreoptExtractedApk bool // Overrides OnlyPreoptModules
129
130 NoCreateAppImage bool
131 ForceCreateAppImage bool
132
133 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800134}
135
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000136type globalSoongConfigSingleton struct{}
137
138var pctx = android.NewPackageContext("android/soong/dexpreopt")
139
140func init() {
141 pctx.Import("android/soong/android")
142 android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton {
143 return &globalSoongConfigSingleton{}
144 })
145}
146
Colin Cross69f59a32019-02-15 10:39:37 -0800147func constructPath(ctx android.PathContext, path string) android.Path {
148 buildDirPrefix := ctx.Config().BuildDir() + "/"
149 if path == "" {
150 return nil
151 } else if strings.HasPrefix(path, buildDirPrefix) {
152 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
153 } else {
154 return android.PathForSource(ctx, path)
155 }
Colin Cross43f08db2018-11-12 10:13:39 -0800156}
157
Colin Cross69f59a32019-02-15 10:39:37 -0800158func constructPaths(ctx android.PathContext, paths []string) android.Paths {
159 var ret android.Paths
160 for _, path := range paths {
161 ret = append(ret, constructPath(ctx, path))
162 }
163 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800164}
165
Colin Cross69f59a32019-02-15 10:39:37 -0800166func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
167 ret := map[string]android.Path{}
168 for key, path := range paths {
169 ret[key] = constructPath(ctx, path)
170 }
171 return ret
172}
173
174func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
175 if path == "" {
176 return nil
177 }
178 return constructPath(ctx, path).(android.WritablePath)
179}
180
Hans Boehm453bf092020-01-25 01:44:30 +0000181// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig
Hans Boehme4b53422020-01-25 01:44:30 +0000182// struct, except the SoongConfig field which is set from the provided
183// soongConfig argument. LoadGlobalConfig is used directly in Soong and in
184// dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by
185// Make.
186func LoadGlobalConfig(ctx android.PathContext, data []byte, soongConfig GlobalSoongConfig) (GlobalConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800187 type GlobalJSONConfig struct {
188 GlobalConfig
189
190 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
191 // used to construct the real value manually below.
192 DirtyImageObjects string
Colin Cross69f59a32019-02-15 10:39:37 -0800193 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.
203 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800204 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
205
Hans Boehme4b53422020-01-25 01:44:30 +0000206 // Set this here to force the caller to provide a value for this struct (from
207 // either CreateGlobalSoongConfig or LoadGlobalSoongConfig).
208 config.GlobalConfig.SoongConfig = soongConfig
209
Colin Cross988414c2020-01-11 01:11:46 +0000210 return config.GlobalConfig, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800211}
212
Hans Boehm453bf092020-01-25 01:44:30 +0000213// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct. It is not used in Soong, which
214// receives a ModuleConfig struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called from oMake to
215// read the module dexpreopt.config written by Make.
216func LoadModuleConfig(ctx android.PathContext, data []byte) (ModuleConfig, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800217 type ModuleJSONConfig struct {
218 ModuleConfig
219
220 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
221 // used to construct the real value manually below.
222 BuildPath string
223 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700224 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800225 ProfileClassListing string
226 LibraryPaths map[string]string
227 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000228 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800229 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800230 }
231
232 config := ModuleJSONConfig{}
233
Colin Cross988414c2020-01-11 01:11:46 +0000234 err := json.Unmarshal(data, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800235 if err != nil {
236 return config.ModuleConfig, err
237 }
238
239 // Construct paths that require a PathContext.
240 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
241 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700242 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800243 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
244 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
245 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000246 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800247 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800248
Dan Willemsen0f416782019-06-13 21:44:53 +0000249 // 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 +0000250 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000251
Colin Cross69f59a32019-02-15 10:39:37 -0800252 return config.ModuleConfig, nil
253}
254
Hans Boehme4b53422020-01-25 01:44:30 +0000255// CreateGlobalSoongConfig creates a GlobalSoongConfig from the current context.
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000256// Should not be used in dexpreopt_gen.
Hans Boehme4b53422020-01-25 01:44:30 +0000257func CreateGlobalSoongConfig(ctx android.PathContext) GlobalSoongConfig {
Hans Boehm7b2e6f32020-01-25 01:44:30 +0000258 // Default to debug version to help find bugs.
259 // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
260 var dex2oatBinary string
261 if ctx.Config().Getenv("USE_DEX2OAT_DEBUG") == "false" {
262 dex2oatBinary = "dex2oat"
263 } else {
264 dex2oatBinary = "dex2oatd"
265 }
266
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000267 return GlobalSoongConfig{
268 Profman: ctx.Config().HostToolPath(ctx, "profman"),
Hans Boehm7b2e6f32020-01-25 01:44:30 +0000269 Dex2oat: ctx.Config().HostToolPath(ctx, dex2oatBinary),
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000270 Aapt: ctx.Config().HostToolPath(ctx, "aapt"),
271 SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
272 Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
273 ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
274 ConstructContext: android.PathForSource(ctx, "build/make/core/construct_context.sh"),
275 }
276}
277
278type globalJsonSoongConfig struct {
279 Profman string
280 Dex2oat string
281 Aapt string
282 SoongZip string
283 Zip2zip string
284 ManifestCheck string
285 ConstructContext string
286}
287
Hans Boehm453bf092020-01-25 01:44:30 +0000288// LoadGlobalSoongConfig reads the dexpreopt_soong.config file into a
289// GlobalSoongConfig struct. It is only used in dexpreopt_gen.
290func LoadGlobalSoongConfig(ctx android.PathContext, data []byte) (GlobalSoongConfig, error) {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000291 var jc globalJsonSoongConfig
292
Colin Cross988414c2020-01-11 01:11:46 +0000293 err := json.Unmarshal(data, &jc)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000294 if err != nil {
295 return GlobalSoongConfig{}, err
296 }
297
298 config := GlobalSoongConfig{
299 Profman: constructPath(ctx, jc.Profman),
300 Dex2oat: constructPath(ctx, jc.Dex2oat),
301 Aapt: constructPath(ctx, jc.Aapt),
302 SoongZip: constructPath(ctx, jc.SoongZip),
303 Zip2zip: constructPath(ctx, jc.Zip2zip),
304 ManifestCheck: constructPath(ctx, jc.ManifestCheck),
305 ConstructContext: constructPath(ctx, jc.ConstructContext),
306 }
307
308 return config, nil
309}
310
311func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Hans Boehme4b53422020-01-25 01:44:30 +0000312 config := CreateGlobalSoongConfig(ctx)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000313 jc := globalJsonSoongConfig{
314 Profman: config.Profman.String(),
315 Dex2oat: config.Dex2oat.String(),
316 Aapt: config.Aapt.String(),
317 SoongZip: config.SoongZip.String(),
318 Zip2zip: config.Zip2zip.String(),
319 ManifestCheck: config.ManifestCheck.String(),
320 ConstructContext: config.ConstructContext.String(),
321 }
322
323 data, err := json.Marshal(jc)
324 if err != nil {
325 ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
326 return
327 }
328
329 ctx.Build(pctx, android.BuildParams{
330 Rule: android.WriteFile,
331 Output: android.PathForOutput(ctx, "dexpreopt_soong.config"),
332 Args: map[string]string{
333 "content": string(data),
334 },
335 })
336}
337
338func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
Hans Boehme4b53422020-01-25 01:44:30 +0000339 config := CreateGlobalSoongConfig(ctx)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000340
341 ctx.Strict("DEX2OAT", config.Dex2oat.String())
342 ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
343 config.Profman.String(),
344 config.Dex2oat.String(),
345 config.Aapt.String(),
346 config.SoongZip.String(),
347 config.Zip2zip.String(),
348 config.ManifestCheck.String(),
349 config.ConstructContext.String(),
350 }, " "))
351}
352
Colin Cross69f59a32019-02-15 10:39:37 -0800353func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
354 return GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800355 DisablePreopt: false,
356 DisablePreoptModules: nil,
357 OnlyPreoptBootImageAndSystemServer: false,
358 HasSystemOther: false,
359 PatternsOnSystemOther: nil,
360 DisableGenerateProfile: false,
361 ProfileDir: "",
362 BootJars: nil,
Roshan Piusccc26ef2019-11-27 09:37:46 -0800363 UpdatableBootJars: nil,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100364 ArtApexJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800365 SystemServerJars: nil,
366 SystemServerApps: nil,
Roshan Pius9b51a402019-11-21 12:36:53 -0800367 UpdatableSystemServerJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800368 SpeedApps: nil,
369 PreoptFlags: nil,
370 DefaultCompilerFilter: "",
371 SystemServerCompilerFilter: "",
372 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800373 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700374 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800375 AlwaysSystemServerDebugInfo: false,
376 NeverSystemServerDebugInfo: false,
377 AlwaysOtherDebugInfo: false,
378 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800379 IsEng: false,
380 SanitizeLite: false,
381 DefaultAppImages: false,
382 Dex2oatXmx: "",
383 Dex2oatXms: "",
384 EmptyDirectory: "empty_dir",
385 CpuVariant: nil,
386 InstructionSetFeatures: nil,
387 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800388 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800389 BootFlags: "",
390 Dex2oatImageXmx: "",
391 Dex2oatImageXms: "",
Hans Boehme4b53422020-01-25 01:44:30 +0000392 SoongConfig: GlobalSoongConfig{
Colin Cross38b96852019-05-22 10:21:09 -0700393 Profman: android.PathForTesting("profman"),
394 Dex2oat: android.PathForTesting("dex2oat"),
395 Aapt: android.PathForTesting("aapt"),
396 SoongZip: android.PathForTesting("soong_zip"),
397 Zip2zip: android.PathForTesting("zip2zip"),
398 ManifestCheck: android.PathForTesting("manifest_check"),
399 ConstructContext: android.PathForTesting("construct_context.sh"),
Hans Boehme4b53422020-01-25 01:44:30 +0000400 },
401 }
Colin Cross69f59a32019-02-15 10:39:37 -0800402}