blob: a2f1af4a6f179e79fa4ce78e21918d111343f35b [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 {
27 DefaultNoStripping bool // don't strip dex files by default
28
Colin Cross69f59a32019-02-15 10:39:37 -080029 DisablePreopt bool // disable preopt for all modules
Colin Cross43f08db2018-11-12 10:13:39 -080030 DisablePreoptModules []string // modules with preopt disabled by product-specific config
31
32 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
33
Nicolas Geoffray72892f12019-02-22 15:34:40 +000034 GenerateApexImage bool // generate an extra boot image only containing jars from the runtime apex
Nicolas Geoffray25c0e032019-04-04 18:45:20 +010035 UseApexImage bool // use the apex image by default
Nicolas Geoffray72892f12019-02-22 15:34:40 +000036
Colin Cross43f08db2018-11-12 10:13:39 -080037 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
38 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
39
Colin Cross69f59a32019-02-15 10:39:37 -080040 DisableGenerateProfile bool // don't generate profiles
41 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080042
Colin Cross800fe132019-02-11 14:21:24 -080043 BootJars []string // modules for jars that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000044
Nicolas Geoffray39fe5742019-02-20 10:00:47 +000045 RuntimeApexJars []string // modules for jars that are in the runtime apex
Colin Cross800fe132019-02-11 14:21:24 -080046 ProductUpdatableBootModules []string
47 ProductUpdatableBootLocations []string
48
Colin Cross43f08db2018-11-12 10:13:39 -080049 SystemServerJars []string // jars that form the system server
50 SystemServerApps []string // apps that are loaded into system server
51 SpeedApps []string // apps that should be speed optimized
52
53 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
54
55 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
56 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
57
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000058 GenerateDMFiles bool // generate Dex Metadata files
59 NeverAllowStripping bool // whether stripping should not be done - used as build time check to make sure dex files are always available
Colin Cross43f08db2018-11-12 10:13:39 -080060
61 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070062 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080063 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
64 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
65 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
66 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
67
Colin Cross43f08db2018-11-12 10:13:39 -080068 IsEng bool // build is a eng variant
69 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
70
71 DefaultAppImages bool // build app images (TODO: .art files?) by default
72
Colin Cross800fe132019-02-11 14:21:24 -080073 Dex2oatXmx string // max heap size for dex2oat
74 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080075
76 EmptyDirectory string // path to an empty directory
77
Colin Cross74ba9622019-02-11 15:11:14 -080078 CpuVariant map[android.ArchType]string // cpu variant for each architecture
79 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080080
Colin Cross800fe132019-02-11 14:21:24 -080081 // Only used for boot image
Colin Cross69f59a32019-02-15 10:39:37 -080082 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
83 PreloadedClasses android.OptionalPath // path to a preloaded-classes file
84 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
85 UseProfileForBootImage bool // whether a profile should be used to compile the boot image
86 BootFlags string // extra flags to pass to dex2oat for the boot image
87 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
88 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross800fe132019-02-11 14:21:24 -080089
Colin Cross43f08db2018-11-12 10:13:39 -080090 Tools Tools // paths to tools possibly used by the generated commands
91}
92
93// Tools contains paths to tools possibly used by the generated commands. If you add a new tool here you MUST add it
94// to the order-only dependency list in DEXPREOPT_GEN_DEPS.
95type Tools struct {
Colin Cross38b96852019-05-22 10:21:09 -070096 Profman android.Path
97 Dex2oat android.Path
98 Aapt android.Path
99 SoongZip android.Path
100 Zip2zip android.Path
101 ManifestCheck android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800102
Colin Cross38b96852019-05-22 10:21:09 -0700103 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800104}
105
106type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800107 Name string
108 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800109 BuildPath android.OutputPath
110 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700111 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800112 UncompressedDex bool
113 HasApkLibraries bool
114 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800115
Colin Cross69f59a32019-02-15 10:39:37 -0800116 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800117 ProfileIsTextListing bool
118
Colin Cross50ddcc42019-05-16 12:28:22 -0700119 EnforceUsesLibraries bool
120 PresentOptionalUsesLibraries []string
121 UsesLibraries []string
122 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800123
Dan Willemsen0f416782019-06-13 21:44:53 +0000124 Archs []android.ArchType
125 DexPreoptImages []android.Path
126 DexPreoptImagesDeps []android.Paths
Colin Cross43f08db2018-11-12 10:13:39 -0800127
Colin Cross69f59a32019-02-15 10:39:37 -0800128 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
129 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800130
Colin Cross43f08db2018-11-12 10:13:39 -0800131 PreoptExtractedApk bool // Overrides OnlyPreoptModules
132
133 NoCreateAppImage bool
134 ForceCreateAppImage bool
135
136 PresignedPrebuilt bool
137
Colin Cross8c6d2502019-01-09 21:09:14 -0800138 NoStripping bool
Colin Cross69f59a32019-02-15 10:39:37 -0800139 StripInputPath android.Path
140 StripOutputPath android.WritablePath
Colin Cross43f08db2018-11-12 10:13:39 -0800141}
142
Colin Cross69f59a32019-02-15 10:39:37 -0800143func constructPath(ctx android.PathContext, path string) android.Path {
144 buildDirPrefix := ctx.Config().BuildDir() + "/"
145 if path == "" {
146 return nil
147 } else if strings.HasPrefix(path, buildDirPrefix) {
148 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
149 } else {
150 return android.PathForSource(ctx, path)
151 }
Colin Cross43f08db2018-11-12 10:13:39 -0800152}
153
Colin Cross69f59a32019-02-15 10:39:37 -0800154func constructPaths(ctx android.PathContext, paths []string) android.Paths {
155 var ret android.Paths
156 for _, path := range paths {
157 ret = append(ret, constructPath(ctx, path))
158 }
159 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800160}
161
Colin Cross69f59a32019-02-15 10:39:37 -0800162func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
163 ret := map[string]android.Path{}
164 for key, path := range paths {
165 ret[key] = constructPath(ctx, path)
166 }
167 return ret
168}
169
170func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
171 if path == "" {
172 return nil
173 }
174 return constructPath(ctx, path).(android.WritablePath)
175}
176
177// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct. It is used directly in Soong
178// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
Colin Cross2d00f0d2019-05-09 21:50:00 -0700179func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, []byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800180 type GlobalJSONConfig struct {
181 GlobalConfig
182
183 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
184 // used to construct the real value manually below.
185 DirtyImageObjects string
186 PreloadedClasses string
187 BootImageProfiles []string
188
189 Tools struct {
Colin Cross38b96852019-05-22 10:21:09 -0700190 Profman string
191 Dex2oat string
192 Aapt string
193 SoongZip string
194 Zip2zip string
195 ManifestCheck string
Colin Cross69f59a32019-02-15 10:39:37 -0800196
Colin Cross38b96852019-05-22 10:21:09 -0700197 ConstructContext string
Colin Cross69f59a32019-02-15 10:39:37 -0800198 }
199 }
200
201 config := GlobalJSONConfig{}
Colin Cross2d00f0d2019-05-09 21:50:00 -0700202 data, err := loadConfig(ctx, path, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800203 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700204 return config.GlobalConfig, nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800205 }
206
207 // Construct paths that require a PathContext.
208 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
209 config.GlobalConfig.PreloadedClasses = android.OptionalPathForPath(constructPath(ctx, config.PreloadedClasses))
210 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
211
212 config.GlobalConfig.Tools.Profman = constructPath(ctx, config.Tools.Profman)
213 config.GlobalConfig.Tools.Dex2oat = constructPath(ctx, config.Tools.Dex2oat)
214 config.GlobalConfig.Tools.Aapt = constructPath(ctx, config.Tools.Aapt)
215 config.GlobalConfig.Tools.SoongZip = constructPath(ctx, config.Tools.SoongZip)
216 config.GlobalConfig.Tools.Zip2zip = constructPath(ctx, config.Tools.Zip2zip)
Colin Cross38b96852019-05-22 10:21:09 -0700217 config.GlobalConfig.Tools.ManifestCheck = constructPath(ctx, config.Tools.ManifestCheck)
Colin Cross69f59a32019-02-15 10:39:37 -0800218 config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
219
Colin Cross2d00f0d2019-05-09 21:50:00 -0700220 return config.GlobalConfig, data, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800221}
222
223// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct. It is not used in Soong, which
224// receives a ModuleConfig struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called from oMake to
225// read the module dexpreopt.config written by Make.
226func LoadModuleConfig(ctx android.PathContext, path string) (ModuleConfig, error) {
227 type ModuleJSONConfig struct {
228 ModuleConfig
229
230 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
231 // used to construct the real value manually below.
232 BuildPath string
233 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700234 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800235 ProfileClassListing string
236 LibraryPaths map[string]string
237 DexPreoptImages []string
238 PreoptBootClassPathDexFiles []string
239 StripInputPath string
240 StripOutputPath string
241 }
242
243 config := ModuleJSONConfig{}
244
Colin Cross2d00f0d2019-05-09 21:50:00 -0700245 _, err := loadConfig(ctx, path, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800246 if err != nil {
247 return config.ModuleConfig, err
248 }
249
250 // Construct paths that require a PathContext.
251 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
252 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700253 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800254 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
255 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
256 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
257 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
258 config.ModuleConfig.StripInputPath = constructPath(ctx, config.StripInputPath)
259 config.ModuleConfig.StripOutputPath = constructWritablePath(ctx, config.StripOutputPath)
260
Dan Willemsen0f416782019-06-13 21:44:53 +0000261 // This needs to exist, but dependencies are already handled in Make, so we don't need to pass them through JSON.
262 config.ModuleConfig.DexPreoptImagesDeps = make([]android.Paths, len(config.ModuleConfig.DexPreoptImages))
263
Colin Cross69f59a32019-02-15 10:39:37 -0800264 return config.ModuleConfig, nil
265}
266
Colin Cross2d00f0d2019-05-09 21:50:00 -0700267func loadConfig(ctx android.PathContext, path string, config interface{}) ([]byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800268 r, err := ctx.Fs().Open(path)
269 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700270 return nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800271 }
272 defer r.Close()
273
274 data, err := ioutil.ReadAll(r)
Colin Cross43f08db2018-11-12 10:13:39 -0800275 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700276 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800277 }
278
279 err = json.Unmarshal(data, config)
280 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700281 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800282 }
283
Colin Cross2d00f0d2019-05-09 21:50:00 -0700284 return data, nil
Colin Cross43f08db2018-11-12 10:13:39 -0800285}
Colin Cross69f59a32019-02-15 10:39:37 -0800286
287func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
288 return GlobalConfig{
289 DefaultNoStripping: false,
290 DisablePreopt: false,
291 DisablePreoptModules: nil,
292 OnlyPreoptBootImageAndSystemServer: false,
293 HasSystemOther: false,
294 PatternsOnSystemOther: nil,
295 DisableGenerateProfile: false,
296 ProfileDir: "",
297 BootJars: nil,
298 RuntimeApexJars: nil,
299 ProductUpdatableBootModules: nil,
300 ProductUpdatableBootLocations: nil,
301 SystemServerJars: nil,
302 SystemServerApps: nil,
303 SpeedApps: nil,
304 PreoptFlags: nil,
305 DefaultCompilerFilter: "",
306 SystemServerCompilerFilter: "",
307 GenerateDMFiles: false,
308 NeverAllowStripping: false,
309 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700310 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800311 AlwaysSystemServerDebugInfo: false,
312 NeverSystemServerDebugInfo: false,
313 AlwaysOtherDebugInfo: false,
314 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800315 IsEng: false,
316 SanitizeLite: false,
317 DefaultAppImages: false,
318 Dex2oatXmx: "",
319 Dex2oatXms: "",
320 EmptyDirectory: "empty_dir",
321 CpuVariant: nil,
322 InstructionSetFeatures: nil,
323 DirtyImageObjects: android.OptionalPath{},
324 PreloadedClasses: android.OptionalPath{},
325 BootImageProfiles: nil,
326 UseProfileForBootImage: false,
327 BootFlags: "",
328 Dex2oatImageXmx: "",
329 Dex2oatImageXms: "",
330 Tools: Tools{
Colin Cross38b96852019-05-22 10:21:09 -0700331 Profman: android.PathForTesting("profman"),
332 Dex2oat: android.PathForTesting("dex2oat"),
333 Aapt: android.PathForTesting("aapt"),
334 SoongZip: android.PathForTesting("soong_zip"),
335 Zip2zip: android.PathForTesting("zip2zip"),
336 ManifestCheck: android.PathForTesting("manifest_check"),
337 ConstructContext: android.PathForTesting("construct_context.sh"),
Colin Cross69f59a32019-02-15 10:39:37 -0800338 },
339 }
340}