blob: f38d89297cb8b473b19e0495f79bb059a1cb75ae [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"
19 "io/ioutil"
Colin Cross69f59a32019-02-15 10:39:37 -080020 "strings"
Colin Cross74ba9622019-02-11 15:11:14 -080021
22 "android/soong/android"
Colin Cross43f08db2018-11-12 10:13:39 -080023)
24
25// GlobalConfig stores the configuration for dex preopting set by the product
26type GlobalConfig struct {
Colin Cross69f59a32019-02-15 10:39:37 -080027 DisablePreopt bool // disable preopt for all modules
Colin Cross43f08db2018-11-12 10:13:39 -080028 DisablePreoptModules []string // modules with preopt disabled by product-specific config
29
30 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
31
Nicolas Geoffray72892f12019-02-22 15:34:40 +000032 GenerateApexImage bool // generate an extra boot image only containing jars from the runtime apex
Nicolas Geoffray25c0e032019-04-04 18:45:20 +010033 UseApexImage bool // use the apex image by default
Nicolas Geoffray72892f12019-02-22 15:34:40 +000034
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
Colin Cross800fe132019-02-11 14:21:24 -080041 BootJars []string // modules for jars that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000042
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +010043 ArtApexJars []string // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080044 ProductUpdatableBootModules []string
45 ProductUpdatableBootLocations []string
46
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
Colin Cross43f08db2018-11-12 10:13:39 -080086 Tools Tools // paths to tools possibly used by the generated commands
87}
88
89// Tools contains paths to tools possibly used by the generated commands. If you add a new tool here you MUST add it
90// to the order-only dependency list in DEXPREOPT_GEN_DEPS.
91type Tools struct {
Colin Cross38b96852019-05-22 10:21:09 -070092 Profman android.Path
93 Dex2oat android.Path
94 Aapt android.Path
95 SoongZip android.Path
96 Zip2zip android.Path
97 ManifestCheck android.Path
Colin Cross43f08db2018-11-12 10:13:39 -080098
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
Dan Willemsen0f416782019-06-13 21:44:53 +0000121 Archs []android.ArchType
122 DexPreoptImages []android.Path
123 DexPreoptImagesDeps []android.Paths
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
Colin Cross69f59a32019-02-15 10:39:37 -0800136func constructPath(ctx android.PathContext, path string) android.Path {
137 buildDirPrefix := ctx.Config().BuildDir() + "/"
138 if path == "" {
139 return nil
140 } else if strings.HasPrefix(path, buildDirPrefix) {
141 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
142 } else {
143 return android.PathForSource(ctx, path)
144 }
Colin Cross43f08db2018-11-12 10:13:39 -0800145}
146
Colin Cross69f59a32019-02-15 10:39:37 -0800147func constructPaths(ctx android.PathContext, paths []string) android.Paths {
148 var ret android.Paths
149 for _, path := range paths {
150 ret = append(ret, constructPath(ctx, path))
151 }
152 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800153}
154
Colin Cross69f59a32019-02-15 10:39:37 -0800155func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
156 ret := map[string]android.Path{}
157 for key, path := range paths {
158 ret[key] = constructPath(ctx, path)
159 }
160 return ret
161}
162
163func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
164 if path == "" {
165 return nil
166 }
167 return constructPath(ctx, path).(android.WritablePath)
168}
169
170// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct. It is used directly in Soong
171// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
Colin Cross2d00f0d2019-05-09 21:50:00 -0700172func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, []byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800173 type GlobalJSONConfig struct {
174 GlobalConfig
175
176 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
177 // used to construct the real value manually below.
178 DirtyImageObjects string
Colin Cross69f59a32019-02-15 10:39:37 -0800179 BootImageProfiles []string
180
181 Tools struct {
Colin Cross38b96852019-05-22 10:21:09 -0700182 Profman string
183 Dex2oat string
184 Aapt string
185 SoongZip string
186 Zip2zip string
187 ManifestCheck string
Colin Cross69f59a32019-02-15 10:39:37 -0800188
Colin Cross38b96852019-05-22 10:21:09 -0700189 ConstructContext string
Colin Cross69f59a32019-02-15 10:39:37 -0800190 }
191 }
192
193 config := GlobalJSONConfig{}
Colin Cross2d00f0d2019-05-09 21:50:00 -0700194 data, err := loadConfig(ctx, path, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800195 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700196 return config.GlobalConfig, nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800197 }
198
199 // Construct paths that require a PathContext.
200 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800201 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
202
203 config.GlobalConfig.Tools.Profman = constructPath(ctx, config.Tools.Profman)
204 config.GlobalConfig.Tools.Dex2oat = constructPath(ctx, config.Tools.Dex2oat)
205 config.GlobalConfig.Tools.Aapt = constructPath(ctx, config.Tools.Aapt)
206 config.GlobalConfig.Tools.SoongZip = constructPath(ctx, config.Tools.SoongZip)
207 config.GlobalConfig.Tools.Zip2zip = constructPath(ctx, config.Tools.Zip2zip)
Colin Cross38b96852019-05-22 10:21:09 -0700208 config.GlobalConfig.Tools.ManifestCheck = constructPath(ctx, config.Tools.ManifestCheck)
Colin Cross69f59a32019-02-15 10:39:37 -0800209 config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
210
Colin Cross2d00f0d2019-05-09 21:50:00 -0700211 return config.GlobalConfig, data, 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.
217func LoadModuleConfig(ctx android.PathContext, path string) (ModuleConfig, error) {
218 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
229 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800230 }
231
232 config := ModuleJSONConfig{}
233
Colin Cross2d00f0d2019-05-09 21:50:00 -0700234 _, err := loadConfig(ctx, path, &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)
246 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800247
Dan Willemsen0f416782019-06-13 21:44:53 +0000248 // This needs to exist, but dependencies are already handled in Make, so we don't need to pass them through JSON.
249 config.ModuleConfig.DexPreoptImagesDeps = make([]android.Paths, len(config.ModuleConfig.DexPreoptImages))
250
Colin Cross69f59a32019-02-15 10:39:37 -0800251 return config.ModuleConfig, nil
252}
253
Colin Cross2d00f0d2019-05-09 21:50:00 -0700254func loadConfig(ctx android.PathContext, path string, config interface{}) ([]byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800255 r, err := ctx.Fs().Open(path)
256 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700257 return nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800258 }
259 defer r.Close()
260
261 data, err := ioutil.ReadAll(r)
Colin Cross43f08db2018-11-12 10:13:39 -0800262 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700263 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800264 }
265
266 err = json.Unmarshal(data, config)
267 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700268 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800269 }
270
Colin Cross2d00f0d2019-05-09 21:50:00 -0700271 return data, nil
Colin Cross43f08db2018-11-12 10:13:39 -0800272}
Colin Cross69f59a32019-02-15 10:39:37 -0800273
274func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
275 return GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800276 DisablePreopt: false,
277 DisablePreoptModules: nil,
278 OnlyPreoptBootImageAndSystemServer: false,
279 HasSystemOther: false,
280 PatternsOnSystemOther: nil,
281 DisableGenerateProfile: false,
282 ProfileDir: "",
283 BootJars: nil,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100284 ArtApexJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800285 ProductUpdatableBootModules: nil,
286 ProductUpdatableBootLocations: nil,
287 SystemServerJars: nil,
288 SystemServerApps: nil,
Roshan Pius9b51a402019-11-21 12:36:53 -0800289 UpdatableSystemServerJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800290 SpeedApps: nil,
291 PreoptFlags: nil,
292 DefaultCompilerFilter: "",
293 SystemServerCompilerFilter: "",
294 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800295 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700296 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800297 AlwaysSystemServerDebugInfo: false,
298 NeverSystemServerDebugInfo: false,
299 AlwaysOtherDebugInfo: false,
300 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800301 IsEng: false,
302 SanitizeLite: false,
303 DefaultAppImages: false,
304 Dex2oatXmx: "",
305 Dex2oatXms: "",
306 EmptyDirectory: "empty_dir",
307 CpuVariant: nil,
308 InstructionSetFeatures: nil,
309 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800310 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800311 BootFlags: "",
312 Dex2oatImageXmx: "",
313 Dex2oatImageXms: "",
314 Tools: Tools{
Colin Cross38b96852019-05-22 10:21:09 -0700315 Profman: android.PathForTesting("profman"),
316 Dex2oat: android.PathForTesting("dex2oat"),
317 Aapt: android.PathForTesting("aapt"),
318 SoongZip: android.PathForTesting("soong_zip"),
319 Zip2zip: android.PathForTesting("zip2zip"),
320 ManifestCheck: android.PathForTesting("manifest_check"),
321 ConstructContext: android.PathForTesting("construct_context.sh"),
Colin Cross69f59a32019-02-15 10:39:37 -0800322 },
323 }
324}