blob: 3e3295849124ff9f6014cd82eb0d565213a8db14 [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
Mathieu Chartier6adeee12019-06-26 10:01:36 -070082 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
83 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
84 BootFlags string // extra flags to pass to dex2oat for the boot image
85 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
86 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross800fe132019-02-11 14:21:24 -080087
Colin Cross43f08db2018-11-12 10:13:39 -080088 Tools Tools // paths to tools possibly used by the generated commands
89}
90
91// Tools contains paths to tools possibly used by the generated commands. If you add a new tool here you MUST add it
92// to the order-only dependency list in DEXPREOPT_GEN_DEPS.
93type Tools struct {
Colin Cross38b96852019-05-22 10:21:09 -070094 Profman android.Path
95 Dex2oat android.Path
96 Aapt android.Path
97 SoongZip android.Path
98 Zip2zip android.Path
99 ManifestCheck android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800100
Colin Cross38b96852019-05-22 10:21:09 -0700101 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800102}
103
104type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800105 Name string
106 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800107 BuildPath android.OutputPath
108 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700109 ManifestPath android.Path
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800110 UncompressedDex bool
111 HasApkLibraries bool
112 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800113
Colin Cross69f59a32019-02-15 10:39:37 -0800114 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800115 ProfileIsTextListing bool
116
Colin Cross50ddcc42019-05-16 12:28:22 -0700117 EnforceUsesLibraries bool
118 PresentOptionalUsesLibraries []string
119 UsesLibraries []string
120 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800121
Dan Willemsen0f416782019-06-13 21:44:53 +0000122 Archs []android.ArchType
123 DexPreoptImages []android.Path
124 DexPreoptImagesDeps []android.Paths
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
135
Colin Cross8c6d2502019-01-09 21:09:14 -0800136 NoStripping bool
Colin Cross69f59a32019-02-15 10:39:37 -0800137 StripInputPath android.Path
138 StripOutputPath android.WritablePath
Colin Cross43f08db2018-11-12 10:13:39 -0800139}
140
Colin Cross69f59a32019-02-15 10:39:37 -0800141func constructPath(ctx android.PathContext, path string) android.Path {
142 buildDirPrefix := ctx.Config().BuildDir() + "/"
143 if path == "" {
144 return nil
145 } else if strings.HasPrefix(path, buildDirPrefix) {
146 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
147 } else {
148 return android.PathForSource(ctx, path)
149 }
Colin Cross43f08db2018-11-12 10:13:39 -0800150}
151
Colin Cross69f59a32019-02-15 10:39:37 -0800152func constructPaths(ctx android.PathContext, paths []string) android.Paths {
153 var ret android.Paths
154 for _, path := range paths {
155 ret = append(ret, constructPath(ctx, path))
156 }
157 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800158}
159
Colin Cross69f59a32019-02-15 10:39:37 -0800160func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
161 ret := map[string]android.Path{}
162 for key, path := range paths {
163 ret[key] = constructPath(ctx, path)
164 }
165 return ret
166}
167
168func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
169 if path == "" {
170 return nil
171 }
172 return constructPath(ctx, path).(android.WritablePath)
173}
174
175// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct. It is used directly in Soong
176// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
Colin Cross2d00f0d2019-05-09 21:50:00 -0700177func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, []byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800178 type GlobalJSONConfig struct {
179 GlobalConfig
180
181 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
182 // used to construct the real value manually below.
183 DirtyImageObjects string
Colin Cross69f59a32019-02-15 10:39:37 -0800184 BootImageProfiles []string
185
186 Tools struct {
Colin Cross38b96852019-05-22 10:21:09 -0700187 Profman string
188 Dex2oat string
189 Aapt string
190 SoongZip string
191 Zip2zip string
192 ManifestCheck string
Colin Cross69f59a32019-02-15 10:39:37 -0800193
Colin Cross38b96852019-05-22 10:21:09 -0700194 ConstructContext string
Colin Cross69f59a32019-02-15 10:39:37 -0800195 }
196 }
197
198 config := GlobalJSONConfig{}
Colin Cross2d00f0d2019-05-09 21:50:00 -0700199 data, err := loadConfig(ctx, path, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800200 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700201 return config.GlobalConfig, nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800202 }
203
204 // Construct paths that require a PathContext.
205 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800206 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
207
208 config.GlobalConfig.Tools.Profman = constructPath(ctx, config.Tools.Profman)
209 config.GlobalConfig.Tools.Dex2oat = constructPath(ctx, config.Tools.Dex2oat)
210 config.GlobalConfig.Tools.Aapt = constructPath(ctx, config.Tools.Aapt)
211 config.GlobalConfig.Tools.SoongZip = constructPath(ctx, config.Tools.SoongZip)
212 config.GlobalConfig.Tools.Zip2zip = constructPath(ctx, config.Tools.Zip2zip)
Colin Cross38b96852019-05-22 10:21:09 -0700213 config.GlobalConfig.Tools.ManifestCheck = constructPath(ctx, config.Tools.ManifestCheck)
Colin Cross69f59a32019-02-15 10:39:37 -0800214 config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
215
Colin Cross2d00f0d2019-05-09 21:50:00 -0700216 return config.GlobalConfig, data, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800217}
218
219// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct. It is not used in Soong, which
220// receives a ModuleConfig struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called from oMake to
221// read the module dexpreopt.config written by Make.
222func LoadModuleConfig(ctx android.PathContext, path string) (ModuleConfig, error) {
223 type ModuleJSONConfig struct {
224 ModuleConfig
225
226 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
227 // used to construct the real value manually below.
228 BuildPath string
229 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700230 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800231 ProfileClassListing string
232 LibraryPaths map[string]string
233 DexPreoptImages []string
234 PreoptBootClassPathDexFiles []string
235 StripInputPath string
236 StripOutputPath string
237 }
238
239 config := ModuleJSONConfig{}
240
Colin Cross2d00f0d2019-05-09 21:50:00 -0700241 _, err := loadConfig(ctx, path, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800242 if err != nil {
243 return config.ModuleConfig, err
244 }
245
246 // Construct paths that require a PathContext.
247 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
248 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700249 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800250 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
251 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
252 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
253 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
254 config.ModuleConfig.StripInputPath = constructPath(ctx, config.StripInputPath)
255 config.ModuleConfig.StripOutputPath = constructWritablePath(ctx, config.StripOutputPath)
256
Dan Willemsen0f416782019-06-13 21:44:53 +0000257 // This needs to exist, but dependencies are already handled in Make, so we don't need to pass them through JSON.
258 config.ModuleConfig.DexPreoptImagesDeps = make([]android.Paths, len(config.ModuleConfig.DexPreoptImages))
259
Colin Cross69f59a32019-02-15 10:39:37 -0800260 return config.ModuleConfig, nil
261}
262
Colin Cross2d00f0d2019-05-09 21:50:00 -0700263func loadConfig(ctx android.PathContext, path string, config interface{}) ([]byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800264 r, err := ctx.Fs().Open(path)
265 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700266 return nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800267 }
268 defer r.Close()
269
270 data, err := ioutil.ReadAll(r)
Colin Cross43f08db2018-11-12 10:13:39 -0800271 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700272 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800273 }
274
275 err = json.Unmarshal(data, config)
276 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700277 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800278 }
279
Colin Cross2d00f0d2019-05-09 21:50:00 -0700280 return data, nil
Colin Cross43f08db2018-11-12 10:13:39 -0800281}
Colin Cross69f59a32019-02-15 10:39:37 -0800282
283func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
284 return GlobalConfig{
285 DefaultNoStripping: false,
286 DisablePreopt: false,
287 DisablePreoptModules: nil,
288 OnlyPreoptBootImageAndSystemServer: false,
289 HasSystemOther: false,
290 PatternsOnSystemOther: nil,
291 DisableGenerateProfile: false,
292 ProfileDir: "",
293 BootJars: nil,
294 RuntimeApexJars: nil,
295 ProductUpdatableBootModules: nil,
296 ProductUpdatableBootLocations: nil,
297 SystemServerJars: nil,
298 SystemServerApps: nil,
299 SpeedApps: nil,
300 PreoptFlags: nil,
301 DefaultCompilerFilter: "",
302 SystemServerCompilerFilter: "",
303 GenerateDMFiles: false,
304 NeverAllowStripping: false,
305 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700306 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800307 AlwaysSystemServerDebugInfo: false,
308 NeverSystemServerDebugInfo: false,
309 AlwaysOtherDebugInfo: false,
310 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800311 IsEng: false,
312 SanitizeLite: false,
313 DefaultAppImages: false,
314 Dex2oatXmx: "",
315 Dex2oatXms: "",
316 EmptyDirectory: "empty_dir",
317 CpuVariant: nil,
318 InstructionSetFeatures: nil,
319 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800320 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800321 BootFlags: "",
322 Dex2oatImageXmx: "",
323 Dex2oatImageXms: "",
324 Tools: Tools{
Colin Cross38b96852019-05-22 10:21:09 -0700325 Profman: android.PathForTesting("profman"),
326 Dex2oat: android.PathForTesting("dex2oat"),
327 Aapt: android.PathForTesting("aapt"),
328 SoongZip: android.PathForTesting("soong_zip"),
329 Zip2zip: android.PathForTesting("zip2zip"),
330 ManifestCheck: android.PathForTesting("manifest_check"),
331 ConstructContext: android.PathForTesting("construct_context.sh"),
Colin Cross69f59a32019-02-15 10:39:37 -0800332 },
333 }
334}