blob: 99814019ca66ce116154355e0b5a7b8dfcc8763d [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
34 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
35 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
36
Colin Cross69f59a32019-02-15 10:39:37 -080037 DisableGenerateProfile bool // don't generate profiles
38 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080039
Colin Cross800fe132019-02-11 14:21:24 -080040 BootJars []string // modules for jars that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000041
Nicolas Geoffray39fe5742019-02-20 10:00:47 +000042 RuntimeApexJars []string // modules for jars that are in the runtime apex
Colin Cross800fe132019-02-11 14:21:24 -080043 ProductUpdatableBootModules []string
44 ProductUpdatableBootLocations []string
45
Colin Cross43f08db2018-11-12 10:13:39 -080046 SystemServerJars []string // jars that form the system server
47 SystemServerApps []string // apps that are loaded into system server
48 SpeedApps []string // apps that should be speed optimized
49
50 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
51
52 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
53 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
54
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000055 GenerateDMFiles bool // generate Dex Metadata files
56 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 -080057
58 NoDebugInfo bool // don't generate debug info by default
59 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
60 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
61 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
62 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
63
64 MissingUsesLibraries []string // libraries that may be listed in OptionalUsesLibraries but will not be installed by the product
65
66 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
Colin Cross69f59a32019-02-15 10:39:37 -080080 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
81 PreloadedClasses android.OptionalPath // path to a preloaded-classes file
82 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
83 UseProfileForBootImage bool // whether a profile should be used to compile the boot image
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 Cross69f59a32019-02-15 10:39:37 -080094 Profman android.Path
95 Dex2oat android.Path
96 Aapt android.Path
97 SoongZip android.Path
98 Zip2zip android.Path
Colin Cross43f08db2018-11-12 10:13:39 -080099
Colin Cross69f59a32019-02-15 10:39:37 -0800100 VerifyUsesLibraries android.Path
101 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
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800109 UncompressedDex bool
110 HasApkLibraries bool
111 PreoptFlags []string
Colin Cross43f08db2018-11-12 10:13:39 -0800112
Colin Cross69f59a32019-02-15 10:39:37 -0800113 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800114 ProfileIsTextListing bool
115
116 EnforceUsesLibraries bool
117 OptionalUsesLibraries []string
118 UsesLibraries []string
Colin Cross69f59a32019-02-15 10:39:37 -0800119 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800120
Colin Cross74ba9622019-02-11 15:11:14 -0800121 Archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800122 DexPreoptImages []android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800123
Colin Cross69f59a32019-02-15 10:39:37 -0800124 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
125 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800126
Colin Cross43f08db2018-11-12 10:13:39 -0800127 PreoptExtractedApk bool // Overrides OnlyPreoptModules
128
129 NoCreateAppImage bool
130 ForceCreateAppImage bool
131
132 PresignedPrebuilt bool
133
Colin Cross8c6d2502019-01-09 21:09:14 -0800134 NoStripping bool
Colin Cross69f59a32019-02-15 10:39:37 -0800135 StripInputPath android.Path
136 StripOutputPath android.WritablePath
Colin Cross43f08db2018-11-12 10:13:39 -0800137}
138
Colin Cross69f59a32019-02-15 10:39:37 -0800139func constructPath(ctx android.PathContext, path string) android.Path {
140 buildDirPrefix := ctx.Config().BuildDir() + "/"
141 if path == "" {
142 return nil
143 } else if strings.HasPrefix(path, buildDirPrefix) {
144 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
145 } else {
146 return android.PathForSource(ctx, path)
147 }
Colin Cross43f08db2018-11-12 10:13:39 -0800148}
149
Colin Cross69f59a32019-02-15 10:39:37 -0800150func constructPaths(ctx android.PathContext, paths []string) android.Paths {
151 var ret android.Paths
152 for _, path := range paths {
153 ret = append(ret, constructPath(ctx, path))
154 }
155 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800156}
157
Colin Cross69f59a32019-02-15 10:39:37 -0800158func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
159 ret := map[string]android.Path{}
160 for key, path := range paths {
161 ret[key] = constructPath(ctx, path)
162 }
163 return ret
164}
165
166func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
167 if path == "" {
168 return nil
169 }
170 return constructPath(ctx, path).(android.WritablePath)
171}
172
173// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct. It is used directly in Soong
174// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
175func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, error) {
176 type GlobalJSONConfig struct {
177 GlobalConfig
178
179 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
180 // used to construct the real value manually below.
181 DirtyImageObjects string
182 PreloadedClasses string
183 BootImageProfiles []string
184
185 Tools struct {
186 Profman string
187 Dex2oat string
188 Aapt string
189 SoongZip string
190 Zip2zip string
191
192 VerifyUsesLibraries string
193 ConstructContext string
194 }
195 }
196
197 config := GlobalJSONConfig{}
198 err := loadConfig(ctx, path, &config)
199 if err != nil {
200 return config.GlobalConfig, err
201 }
202
203 // Construct paths that require a PathContext.
204 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
205 config.GlobalConfig.PreloadedClasses = android.OptionalPathForPath(constructPath(ctx, config.PreloadedClasses))
206 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)
213 config.GlobalConfig.Tools.VerifyUsesLibraries = constructPath(ctx, config.Tools.VerifyUsesLibraries)
214 config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
215
216 return config.GlobalConfig, nil
217}
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
230 ProfileClassListing string
231 LibraryPaths map[string]string
232 DexPreoptImages []string
233 PreoptBootClassPathDexFiles []string
234 StripInputPath string
235 StripOutputPath string
236 }
237
238 config := ModuleJSONConfig{}
239
240 err := loadConfig(ctx, path, &config)
241 if err != nil {
242 return config.ModuleConfig, err
243 }
244
245 // Construct paths that require a PathContext.
246 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
247 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
248 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
249 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
250 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
251 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
252 config.ModuleConfig.StripInputPath = constructPath(ctx, config.StripInputPath)
253 config.ModuleConfig.StripOutputPath = constructWritablePath(ctx, config.StripOutputPath)
254
255 return config.ModuleConfig, nil
256}
257
258func loadConfig(ctx android.PathContext, path string, config interface{}) error {
259 r, err := ctx.Fs().Open(path)
260 if err != nil {
261 return err
262 }
263 defer r.Close()
264
265 data, err := ioutil.ReadAll(r)
Colin Cross43f08db2018-11-12 10:13:39 -0800266 if err != nil {
267 return err
268 }
269
270 err = json.Unmarshal(data, config)
271 if err != nil {
272 return err
273 }
274
275 return nil
276}
Colin Cross69f59a32019-02-15 10:39:37 -0800277
278func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
279 return GlobalConfig{
280 DefaultNoStripping: false,
281 DisablePreopt: false,
282 DisablePreoptModules: nil,
283 OnlyPreoptBootImageAndSystemServer: false,
284 HasSystemOther: false,
285 PatternsOnSystemOther: nil,
286 DisableGenerateProfile: false,
287 ProfileDir: "",
288 BootJars: nil,
289 RuntimeApexJars: nil,
290 ProductUpdatableBootModules: nil,
291 ProductUpdatableBootLocations: nil,
292 SystemServerJars: nil,
293 SystemServerApps: nil,
294 SpeedApps: nil,
295 PreoptFlags: nil,
296 DefaultCompilerFilter: "",
297 SystemServerCompilerFilter: "",
298 GenerateDMFiles: false,
299 NeverAllowStripping: false,
300 NoDebugInfo: false,
301 AlwaysSystemServerDebugInfo: false,
302 NeverSystemServerDebugInfo: false,
303 AlwaysOtherDebugInfo: false,
304 NeverOtherDebugInfo: false,
305 MissingUsesLibraries: nil,
306 IsEng: false,
307 SanitizeLite: false,
308 DefaultAppImages: false,
309 Dex2oatXmx: "",
310 Dex2oatXms: "",
311 EmptyDirectory: "empty_dir",
312 CpuVariant: nil,
313 InstructionSetFeatures: nil,
314 DirtyImageObjects: android.OptionalPath{},
315 PreloadedClasses: android.OptionalPath{},
316 BootImageProfiles: nil,
317 UseProfileForBootImage: false,
318 BootFlags: "",
319 Dex2oatImageXmx: "",
320 Dex2oatImageXms: "",
321 Tools: Tools{
322 Profman: android.PathForTesting("profman"),
323 Dex2oat: android.PathForTesting("dex2oat"),
324 Aapt: android.PathForTesting("aapt"),
325 SoongZip: android.PathForTesting("soong_zip"),
326 Zip2zip: android.PathForTesting("zip2zip"),
327 VerifyUsesLibraries: android.PathForTesting("verify_uses_libraries.sh"),
328 ConstructContext: android.PathForTesting("construct_context.sh"),
329 },
330 }
331}