blob: f1fa0ff34a054711686950a240980cb7f52fcd1c [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
62 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
63 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
64 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
65 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
66
67 MissingUsesLibraries []string // libraries that may be listed in OptionalUsesLibraries but will not be installed by the product
68
69 IsEng bool // build is a eng variant
70 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
71
72 DefaultAppImages bool // build app images (TODO: .art files?) by default
73
Colin Cross800fe132019-02-11 14:21:24 -080074 Dex2oatXmx string // max heap size for dex2oat
75 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080076
77 EmptyDirectory string // path to an empty directory
78
Colin Cross74ba9622019-02-11 15:11:14 -080079 CpuVariant map[android.ArchType]string // cpu variant for each architecture
80 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080081
Colin Cross800fe132019-02-11 14:21:24 -080082 // Only used for boot image
Colin Cross69f59a32019-02-15 10:39:37 -080083 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
84 PreloadedClasses android.OptionalPath // path to a preloaded-classes file
85 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
86 UseProfileForBootImage bool // whether a profile should be used to compile the boot image
87 BootFlags string // extra flags to pass to dex2oat for the boot image
88 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
89 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross800fe132019-02-11 14:21:24 -080090
Colin Cross43f08db2018-11-12 10:13:39 -080091 Tools Tools // paths to tools possibly used by the generated commands
92}
93
94// Tools contains paths to tools possibly used by the generated commands. If you add a new tool here you MUST add it
95// to the order-only dependency list in DEXPREOPT_GEN_DEPS.
96type Tools struct {
Colin Cross69f59a32019-02-15 10:39:37 -080097 Profman android.Path
98 Dex2oat android.Path
99 Aapt android.Path
100 SoongZip android.Path
101 Zip2zip android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800102
Colin Cross69f59a32019-02-15 10:39:37 -0800103 VerifyUsesLibraries android.Path
104 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800105}
106
107type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800108 Name string
109 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800110 BuildPath android.OutputPath
111 DexPath 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
119 EnforceUsesLibraries bool
120 OptionalUsesLibraries []string
121 UsesLibraries []string
Colin Cross69f59a32019-02-15 10:39:37 -0800122 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800123
Colin Cross74ba9622019-02-11 15:11:14 -0800124 Archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800125 DexPreoptImages []android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800126
Colin Cross69f59a32019-02-15 10:39:37 -0800127 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
128 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800129
Colin Cross43f08db2018-11-12 10:13:39 -0800130 PreoptExtractedApk bool // Overrides OnlyPreoptModules
131
132 NoCreateAppImage bool
133 ForceCreateAppImage bool
134
135 PresignedPrebuilt bool
136
Colin Cross8c6d2502019-01-09 21:09:14 -0800137 NoStripping bool
Colin Cross69f59a32019-02-15 10:39:37 -0800138 StripInputPath android.Path
139 StripOutputPath android.WritablePath
Colin Cross43f08db2018-11-12 10:13:39 -0800140}
141
Colin Cross69f59a32019-02-15 10:39:37 -0800142func constructPath(ctx android.PathContext, path string) android.Path {
143 buildDirPrefix := ctx.Config().BuildDir() + "/"
144 if path == "" {
145 return nil
146 } else if strings.HasPrefix(path, buildDirPrefix) {
147 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
148 } else {
149 return android.PathForSource(ctx, path)
150 }
Colin Cross43f08db2018-11-12 10:13:39 -0800151}
152
Colin Cross69f59a32019-02-15 10:39:37 -0800153func constructPaths(ctx android.PathContext, paths []string) android.Paths {
154 var ret android.Paths
155 for _, path := range paths {
156 ret = append(ret, constructPath(ctx, path))
157 }
158 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800159}
160
Colin Cross69f59a32019-02-15 10:39:37 -0800161func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
162 ret := map[string]android.Path{}
163 for key, path := range paths {
164 ret[key] = constructPath(ctx, path)
165 }
166 return ret
167}
168
169func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
170 if path == "" {
171 return nil
172 }
173 return constructPath(ctx, path).(android.WritablePath)
174}
175
176// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct. It is used directly in Soong
177// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
178func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, error) {
179 type GlobalJSONConfig struct {
180 GlobalConfig
181
182 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
183 // used to construct the real value manually below.
184 DirtyImageObjects string
185 PreloadedClasses string
186 BootImageProfiles []string
187
188 Tools struct {
189 Profman string
190 Dex2oat string
191 Aapt string
192 SoongZip string
193 Zip2zip string
194
195 VerifyUsesLibraries string
196 ConstructContext string
197 }
198 }
199
200 config := GlobalJSONConfig{}
201 err := loadConfig(ctx, path, &config)
202 if err != nil {
203 return config.GlobalConfig, err
204 }
205
206 // Construct paths that require a PathContext.
207 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
208 config.GlobalConfig.PreloadedClasses = android.OptionalPathForPath(constructPath(ctx, config.PreloadedClasses))
209 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
210
211 config.GlobalConfig.Tools.Profman = constructPath(ctx, config.Tools.Profman)
212 config.GlobalConfig.Tools.Dex2oat = constructPath(ctx, config.Tools.Dex2oat)
213 config.GlobalConfig.Tools.Aapt = constructPath(ctx, config.Tools.Aapt)
214 config.GlobalConfig.Tools.SoongZip = constructPath(ctx, config.Tools.SoongZip)
215 config.GlobalConfig.Tools.Zip2zip = constructPath(ctx, config.Tools.Zip2zip)
216 config.GlobalConfig.Tools.VerifyUsesLibraries = constructPath(ctx, config.Tools.VerifyUsesLibraries)
217 config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
218
219 return config.GlobalConfig, nil
220}
221
222// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct. It is not used in Soong, which
223// receives a ModuleConfig struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called from oMake to
224// read the module dexpreopt.config written by Make.
225func LoadModuleConfig(ctx android.PathContext, path string) (ModuleConfig, error) {
226 type ModuleJSONConfig struct {
227 ModuleConfig
228
229 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
230 // used to construct the real value manually below.
231 BuildPath string
232 DexPath string
233 ProfileClassListing string
234 LibraryPaths map[string]string
235 DexPreoptImages []string
236 PreoptBootClassPathDexFiles []string
237 StripInputPath string
238 StripOutputPath string
239 }
240
241 config := ModuleJSONConfig{}
242
243 err := loadConfig(ctx, path, &config)
244 if err != nil {
245 return config.ModuleConfig, err
246 }
247
248 // Construct paths that require a PathContext.
249 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
250 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
251 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
252 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
253 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
254 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
255 config.ModuleConfig.StripInputPath = constructPath(ctx, config.StripInputPath)
256 config.ModuleConfig.StripOutputPath = constructWritablePath(ctx, config.StripOutputPath)
257
258 return config.ModuleConfig, nil
259}
260
261func loadConfig(ctx android.PathContext, path string, config interface{}) error {
262 r, err := ctx.Fs().Open(path)
263 if err != nil {
264 return err
265 }
266 defer r.Close()
267
268 data, err := ioutil.ReadAll(r)
Colin Cross43f08db2018-11-12 10:13:39 -0800269 if err != nil {
270 return err
271 }
272
273 err = json.Unmarshal(data, config)
274 if err != nil {
275 return err
276 }
277
278 return nil
279}
Colin Cross69f59a32019-02-15 10:39:37 -0800280
281func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
282 return GlobalConfig{
283 DefaultNoStripping: false,
284 DisablePreopt: false,
285 DisablePreoptModules: nil,
286 OnlyPreoptBootImageAndSystemServer: false,
287 HasSystemOther: false,
288 PatternsOnSystemOther: nil,
289 DisableGenerateProfile: false,
290 ProfileDir: "",
291 BootJars: nil,
292 RuntimeApexJars: nil,
293 ProductUpdatableBootModules: nil,
294 ProductUpdatableBootLocations: nil,
295 SystemServerJars: nil,
296 SystemServerApps: nil,
297 SpeedApps: nil,
298 PreoptFlags: nil,
299 DefaultCompilerFilter: "",
300 SystemServerCompilerFilter: "",
301 GenerateDMFiles: false,
302 NeverAllowStripping: false,
303 NoDebugInfo: false,
304 AlwaysSystemServerDebugInfo: false,
305 NeverSystemServerDebugInfo: false,
306 AlwaysOtherDebugInfo: false,
307 NeverOtherDebugInfo: false,
308 MissingUsesLibraries: nil,
309 IsEng: false,
310 SanitizeLite: false,
311 DefaultAppImages: false,
312 Dex2oatXmx: "",
313 Dex2oatXms: "",
314 EmptyDirectory: "empty_dir",
315 CpuVariant: nil,
316 InstructionSetFeatures: nil,
317 DirtyImageObjects: android.OptionalPath{},
318 PreloadedClasses: android.OptionalPath{},
319 BootImageProfiles: nil,
320 UseProfileForBootImage: false,
321 BootFlags: "",
322 Dex2oatImageXmx: "",
323 Dex2oatImageXms: "",
324 Tools: Tools{
325 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 VerifyUsesLibraries: android.PathForTesting("verify_uses_libraries.sh"),
331 ConstructContext: android.PathForTesting("construct_context.sh"),
332 },
333 }
334}