blob: 6f8ea3a1fbc753674b7f2608580c020e2974b737 [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 Crossacdd6942019-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
29 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
33 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
34 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
35
36 DisableGenerateProfile bool // don't generate profiles
37
Colin Cross800fe132019-02-11 14:21:24 -080038 BootJars []string // modules for jars that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000039
Nicolas Geoffray39fe5742019-02-20 10:00:47 +000040 RuntimeApexJars []string // modules for jars that are in the runtime apex
Colin Cross800fe132019-02-11 14:21:24 -080041 ProductUpdatableBootModules []string
42 ProductUpdatableBootLocations []string
43
Colin Cross43f08db2018-11-12 10:13:39 -080044 SystemServerJars []string // jars that form the system server
45 SystemServerApps []string // apps that are loaded into system server
46 SpeedApps []string // apps that should be speed optimized
47
48 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
49
50 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
51 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
52
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000053 GenerateDMFiles bool // generate Dex Metadata files
54 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 -080055
56 NoDebugInfo bool // don't generate debug info by default
57 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
58 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
59 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
60 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
61
62 MissingUsesLibraries []string // libraries that may be listed in OptionalUsesLibraries but will not be installed by the product
63
64 IsEng bool // build is a eng variant
65 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
66
67 DefaultAppImages bool // build app images (TODO: .art files?) by default
68
Colin Cross800fe132019-02-11 14:21:24 -080069 Dex2oatXmx string // max heap size for dex2oat
70 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080071
72 EmptyDirectory string // path to an empty directory
73
Colin Cross74ba9622019-02-11 15:11:14 -080074 CpuVariant map[android.ArchType]string // cpu variant for each architecture
75 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080076
Colin Cross800fe132019-02-11 14:21:24 -080077 // Only used for boot image
Colin Crossacdd6942019-02-15 10:39:37 -080078 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
79 PreloadedClasses android.OptionalPath // path to a preloaded-classes file
80 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
81 UseProfileForBootImage bool // whether a profile should be used to compile the boot image
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 Crossacdd6942019-02-15 10:39:37 -080092 Profman android.Path
93 Dex2oat android.Path
94 Aapt android.Path
95 SoongZip android.Path
96 Zip2zip android.Path
Colin Cross43f08db2018-11-12 10:13:39 -080097
Colin Crossacdd6942019-02-15 10:39:37 -080098 VerifyUsesLibraries android.Path
99 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 Crossacdd6942019-02-15 10:39:37 -0800105 BuildPath android.OutputPath
106 DexPath 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 Crossacdd6942019-02-15 10:39:37 -0800111 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800112 ProfileIsTextListing bool
113
114 EnforceUsesLibraries bool
115 OptionalUsesLibraries []string
116 UsesLibraries []string
Colin Crossacdd6942019-02-15 10:39:37 -0800117 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800118
Colin Cross74ba9622019-02-11 15:11:14 -0800119 Archs []android.ArchType
Colin Crossacdd6942019-02-15 10:39:37 -0800120 DexPreoptImages []android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800121
Colin Crossacdd6942019-02-15 10:39:37 -0800122 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
123 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800124
Colin Cross43f08db2018-11-12 10:13:39 -0800125 PreoptExtractedApk bool // Overrides OnlyPreoptModules
126
127 NoCreateAppImage bool
128 ForceCreateAppImage bool
129
130 PresignedPrebuilt bool
131
Colin Cross8c6d2502019-01-09 21:09:14 -0800132 NoStripping bool
Colin Crossacdd6942019-02-15 10:39:37 -0800133 StripInputPath android.Path
134 StripOutputPath android.WritablePath
Colin Cross43f08db2018-11-12 10:13:39 -0800135}
136
Colin Crossacdd6942019-02-15 10:39:37 -0800137func constructPath(ctx android.PathContext, path string) android.Path {
138 buildDirPrefix := ctx.Config().BuildDir() + "/"
139 if path == "" {
140 return nil
141 } else if strings.HasPrefix(path, buildDirPrefix) {
142 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
143 } else {
144 return android.PathForSource(ctx, path)
145 }
Colin Cross43f08db2018-11-12 10:13:39 -0800146}
147
Colin Crossacdd6942019-02-15 10:39:37 -0800148func constructPaths(ctx android.PathContext, paths []string) android.Paths {
149 var ret android.Paths
150 for _, path := range paths {
151 ret = append(ret, constructPath(ctx, path))
152 }
153 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800154}
155
Colin Crossacdd6942019-02-15 10:39:37 -0800156func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
157 ret := map[string]android.Path{}
158 for key, path := range paths {
159 ret[key] = constructPath(ctx, path)
160 }
161 return ret
162}
163
164func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
165 if path == "" {
166 return nil
167 }
168 return constructPath(ctx, path).(android.WritablePath)
169}
170
171// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct. It is used directly in Soong
172// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
173func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, error) {
174 type GlobalJSONConfig struct {
175 GlobalConfig
176
177 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
178 // used to construct the real value manually below.
179 DirtyImageObjects string
180 PreloadedClasses string
181 BootImageProfiles []string
182
183 Tools struct {
184 Profman string
185 Dex2oat string
186 Aapt string
187 SoongZip string
188 Zip2zip string
189
190 VerifyUsesLibraries string
191 ConstructContext string
192 }
193 }
194
195 config := GlobalJSONConfig{}
196 err := loadConfig(ctx, path, &config)
197 if err != nil {
198 return config.GlobalConfig, err
199 }
200
201 // Construct paths that require a PathContext.
202 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
203 config.GlobalConfig.PreloadedClasses = android.OptionalPathForPath(constructPath(ctx, config.PreloadedClasses))
204 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
205
206 config.GlobalConfig.Tools.Profman = constructPath(ctx, config.Tools.Profman)
207 config.GlobalConfig.Tools.Dex2oat = constructPath(ctx, config.Tools.Dex2oat)
208 config.GlobalConfig.Tools.Aapt = constructPath(ctx, config.Tools.Aapt)
209 config.GlobalConfig.Tools.SoongZip = constructPath(ctx, config.Tools.SoongZip)
210 config.GlobalConfig.Tools.Zip2zip = constructPath(ctx, config.Tools.Zip2zip)
211 config.GlobalConfig.Tools.VerifyUsesLibraries = constructPath(ctx, config.Tools.VerifyUsesLibraries)
212 config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
213
214 return config.GlobalConfig, nil
215}
216
217// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct. It is not used in Soong, which
218// receives a ModuleConfig struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called from oMake to
219// read the module dexpreopt.config written by Make.
220func LoadModuleConfig(ctx android.PathContext, path string) (ModuleConfig, error) {
221 type ModuleJSONConfig struct {
222 ModuleConfig
223
224 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
225 // used to construct the real value manually below.
226 BuildPath string
227 DexPath string
228 ProfileClassListing string
229 LibraryPaths map[string]string
230 DexPreoptImages []string
231 PreoptBootClassPathDexFiles []string
232 StripInputPath string
233 StripOutputPath string
234 }
235
236 config := ModuleJSONConfig{}
237
238 err := loadConfig(ctx, path, &config)
239 if err != nil {
240 return config.ModuleConfig, err
241 }
242
243 // Construct paths that require a PathContext.
244 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
245 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
246 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
247 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
248 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
249 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
250 config.ModuleConfig.StripInputPath = constructPath(ctx, config.StripInputPath)
251 config.ModuleConfig.StripOutputPath = constructWritablePath(ctx, config.StripOutputPath)
252
253 return config.ModuleConfig, nil
254}
255
256func loadConfig(ctx android.PathContext, path string, config interface{}) error {
257 r, err := ctx.Fs().Open(path)
258 if err != nil {
259 return err
260 }
261 defer r.Close()
262
263 data, err := ioutil.ReadAll(r)
Colin Cross43f08db2018-11-12 10:13:39 -0800264 if err != nil {
265 return err
266 }
267
268 err = json.Unmarshal(data, config)
269 if err != nil {
270 return err
271 }
272
273 return nil
274}
Colin Crossacdd6942019-02-15 10:39:37 -0800275
276func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
277 return GlobalConfig{
278 DefaultNoStripping: false,
279 DisablePreoptModules: nil,
280 OnlyPreoptBootImageAndSystemServer: false,
281 HasSystemOther: false,
282 PatternsOnSystemOther: nil,
283 DisableGenerateProfile: false,
284 BootJars: nil,
285 RuntimeApexJars: nil,
286 ProductUpdatableBootModules: nil,
287 ProductUpdatableBootLocations: nil,
288 SystemServerJars: nil,
289 SystemServerApps: nil,
290 SpeedApps: nil,
291 PreoptFlags: nil,
292 DefaultCompilerFilter: "",
293 SystemServerCompilerFilter: "",
294 GenerateDMFiles: false,
295 NeverAllowStripping: false,
296 NoDebugInfo: false,
297 AlwaysSystemServerDebugInfo: false,
298 NeverSystemServerDebugInfo: false,
299 AlwaysOtherDebugInfo: false,
300 NeverOtherDebugInfo: false,
301 MissingUsesLibraries: nil,
302 IsEng: false,
303 SanitizeLite: false,
304 DefaultAppImages: false,
305 Dex2oatXmx: "",
306 Dex2oatXms: "",
307 EmptyDirectory: "empty_dir",
308 CpuVariant: nil,
309 InstructionSetFeatures: nil,
310 DirtyImageObjects: android.OptionalPath{},
311 PreloadedClasses: android.OptionalPath{},
312 BootImageProfiles: nil,
313 UseProfileForBootImage: false,
314 BootFlags: "",
315 Dex2oatImageXmx: "",
316 Dex2oatImageXms: "",
317 Tools: Tools{
318 Profman: android.PathForTesting("profman"),
319 Dex2oat: android.PathForTesting("dex2oat"),
320 Aapt: android.PathForTesting("aapt"),
321 SoongZip: android.PathForTesting("soong_zip"),
322 Zip2zip: android.PathForTesting("zip2zip"),
323 VerifyUsesLibraries: android.PathForTesting("verify_uses_libraries.sh"),
324 ConstructContext: android.PathForTesting("construct_context.sh"),
325 },
326 }
327}