blob: 9215eff3fdcf068628cca9a298377632b00f3e26 [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 {
Colin Cross69f59a32019-02-15 10:39:37 -080027 DisablePreopt bool // disable preopt for all modules
Colin Cross43f08db2018-11-12 10:13:39 -080028 DisablePreoptModules []string // modules with preopt disabled by product-specific config
29
30 OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
31
Nicolas Geoffray72892f12019-02-22 15:34:40 +000032 GenerateApexImage bool // generate an extra boot image only containing jars from the runtime apex
Nicolas Geoffray25c0e032019-04-04 18:45:20 +010033 UseApexImage bool // use the apex image by default
Nicolas Geoffray72892f12019-02-22 15:34:40 +000034
Colin Cross43f08db2018-11-12 10:13:39 -080035 HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
36 PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
37
Colin Cross69f59a32019-02-15 10:39:37 -080038 DisableGenerateProfile bool // don't generate profiles
39 ProfileDir string // directory to find profiles in
Colin Cross43f08db2018-11-12 10:13:39 -080040
Colin Cross800fe132019-02-11 14:21:24 -080041 BootJars []string // modules for jars that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000042
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +010043 ArtApexJars []string // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080044 ProductUpdatableBootModules []string
45 ProductUpdatableBootLocations []string
46
Colin Cross43f08db2018-11-12 10:13:39 -080047 SystemServerJars []string // jars that form the system server
48 SystemServerApps []string // apps that are loaded into system server
49 SpeedApps []string // apps that should be speed optimized
50
51 PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
52
53 DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
54 SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
55
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010056 GenerateDMFiles bool // generate Dex Metadata files
Colin Cross43f08db2018-11-12 10:13:39 -080057
58 NoDebugInfo bool // don't generate debug info by default
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -070059 DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
Colin Cross43f08db2018-11-12 10:13:39 -080060 AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
61 NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
62 AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
63 NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
64
Colin Cross43f08db2018-11-12 10:13:39 -080065 IsEng bool // build is a eng variant
66 SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
67
68 DefaultAppImages bool // build app images (TODO: .art files?) by default
69
Colin Cross800fe132019-02-11 14:21:24 -080070 Dex2oatXmx string // max heap size for dex2oat
71 Dex2oatXms string // initial heap size for dex2oat
Colin Cross43f08db2018-11-12 10:13:39 -080072
73 EmptyDirectory string // path to an empty directory
74
Colin Cross74ba9622019-02-11 15:11:14 -080075 CpuVariant map[android.ArchType]string // cpu variant for each architecture
76 InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
Colin Cross43f08db2018-11-12 10:13:39 -080077
Colin Cross800fe132019-02-11 14:21:24 -080078 // Only used for boot image
Mathieu Chartier6adeee12019-06-26 10:01:36 -070079 DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file
80 BootImageProfiles android.Paths // path to a boot-image-profile.txt file
81 BootFlags string // extra flags to pass to dex2oat for the boot image
82 Dex2oatImageXmx string // max heap size for dex2oat for the boot image
83 Dex2oatImageXms string // initial heap size for dex2oat for the boot image
Colin Cross800fe132019-02-11 14:21:24 -080084
Colin Cross43f08db2018-11-12 10:13:39 -080085 Tools Tools // paths to tools possibly used by the generated commands
86}
87
88// Tools contains paths to tools possibly used by the generated commands. If you add a new tool here you MUST add it
89// to the order-only dependency list in DEXPREOPT_GEN_DEPS.
90type Tools struct {
Colin Cross38b96852019-05-22 10:21:09 -070091 Profman android.Path
92 Dex2oat android.Path
93 Aapt android.Path
94 SoongZip android.Path
95 Zip2zip android.Path
96 ManifestCheck android.Path
Colin Cross43f08db2018-11-12 10:13:39 -080097
Colin Cross38b96852019-05-22 10:21:09 -070098 ConstructContext android.Path
Colin Cross43f08db2018-11-12 10:13:39 -080099}
100
101type ModuleConfig struct {
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800102 Name string
103 DexLocation string // dex location on device
Colin Cross69f59a32019-02-15 10:39:37 -0800104 BuildPath android.OutputPath
105 DexPath android.Path
Colin Cross38b96852019-05-22 10:21:09 -0700106 ManifestPath 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 Cross69f59a32019-02-15 10:39:37 -0800111 ProfileClassListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800112 ProfileIsTextListing bool
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100113 ProfileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800114
Colin Cross50ddcc42019-05-16 12:28:22 -0700115 EnforceUsesLibraries bool
116 PresentOptionalUsesLibraries []string
117 UsesLibraries []string
118 LibraryPaths map[string]android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800119
Dan Willemsen0f416782019-06-13 21:44:53 +0000120 Archs []android.ArchType
121 DexPreoptImages []android.Path
122 DexPreoptImagesDeps []android.Paths
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
Colin Cross43f08db2018-11-12 10:13:39 -0800133}
134
Colin Cross69f59a32019-02-15 10:39:37 -0800135func constructPath(ctx android.PathContext, path string) android.Path {
136 buildDirPrefix := ctx.Config().BuildDir() + "/"
137 if path == "" {
138 return nil
139 } else if strings.HasPrefix(path, buildDirPrefix) {
140 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
141 } else {
142 return android.PathForSource(ctx, path)
143 }
Colin Cross43f08db2018-11-12 10:13:39 -0800144}
145
Colin Cross69f59a32019-02-15 10:39:37 -0800146func constructPaths(ctx android.PathContext, paths []string) android.Paths {
147 var ret android.Paths
148 for _, path := range paths {
149 ret = append(ret, constructPath(ctx, path))
150 }
151 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800152}
153
Colin Cross69f59a32019-02-15 10:39:37 -0800154func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
155 ret := map[string]android.Path{}
156 for key, path := range paths {
157 ret[key] = constructPath(ctx, path)
158 }
159 return ret
160}
161
162func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
163 if path == "" {
164 return nil
165 }
166 return constructPath(ctx, path).(android.WritablePath)
167}
168
169// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct. It is used directly in Soong
170// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
Colin Cross2d00f0d2019-05-09 21:50:00 -0700171func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, []byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800172 type GlobalJSONConfig struct {
173 GlobalConfig
174
175 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
176 // used to construct the real value manually below.
177 DirtyImageObjects string
Colin Cross69f59a32019-02-15 10:39:37 -0800178 BootImageProfiles []string
179
180 Tools struct {
Colin Cross38b96852019-05-22 10:21:09 -0700181 Profman string
182 Dex2oat string
183 Aapt string
184 SoongZip string
185 Zip2zip string
186 ManifestCheck string
Colin Cross69f59a32019-02-15 10:39:37 -0800187
Colin Cross38b96852019-05-22 10:21:09 -0700188 ConstructContext string
Colin Cross69f59a32019-02-15 10:39:37 -0800189 }
190 }
191
192 config := GlobalJSONConfig{}
Colin Cross2d00f0d2019-05-09 21:50:00 -0700193 data, err := loadConfig(ctx, path, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800194 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700195 return config.GlobalConfig, nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800196 }
197
198 // Construct paths that require a PathContext.
199 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800200 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
201
202 config.GlobalConfig.Tools.Profman = constructPath(ctx, config.Tools.Profman)
203 config.GlobalConfig.Tools.Dex2oat = constructPath(ctx, config.Tools.Dex2oat)
204 config.GlobalConfig.Tools.Aapt = constructPath(ctx, config.Tools.Aapt)
205 config.GlobalConfig.Tools.SoongZip = constructPath(ctx, config.Tools.SoongZip)
206 config.GlobalConfig.Tools.Zip2zip = constructPath(ctx, config.Tools.Zip2zip)
Colin Cross38b96852019-05-22 10:21:09 -0700207 config.GlobalConfig.Tools.ManifestCheck = constructPath(ctx, config.Tools.ManifestCheck)
Colin Cross69f59a32019-02-15 10:39:37 -0800208 config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
209
Colin Cross2d00f0d2019-05-09 21:50:00 -0700210 return config.GlobalConfig, data, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800211}
212
213// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct. It is not used in Soong, which
214// receives a ModuleConfig struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called from oMake to
215// read the module dexpreopt.config written by Make.
216func LoadModuleConfig(ctx android.PathContext, path string) (ModuleConfig, error) {
217 type ModuleJSONConfig struct {
218 ModuleConfig
219
220 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
221 // used to construct the real value manually below.
222 BuildPath string
223 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700224 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800225 ProfileClassListing string
226 LibraryPaths map[string]string
227 DexPreoptImages []string
228 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800229 }
230
231 config := ModuleJSONConfig{}
232
Colin Cross2d00f0d2019-05-09 21:50:00 -0700233 _, err := loadConfig(ctx, path, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800234 if err != nil {
235 return config.ModuleConfig, err
236 }
237
238 // Construct paths that require a PathContext.
239 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
240 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700241 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800242 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
243 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
244 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
245 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800246
Dan Willemsen0f416782019-06-13 21:44:53 +0000247 // This needs to exist, but dependencies are already handled in Make, so we don't need to pass them through JSON.
248 config.ModuleConfig.DexPreoptImagesDeps = make([]android.Paths, len(config.ModuleConfig.DexPreoptImages))
249
Colin Cross69f59a32019-02-15 10:39:37 -0800250 return config.ModuleConfig, nil
251}
252
Colin Cross2d00f0d2019-05-09 21:50:00 -0700253func loadConfig(ctx android.PathContext, path string, config interface{}) ([]byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800254 r, err := ctx.Fs().Open(path)
255 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700256 return nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800257 }
258 defer r.Close()
259
260 data, err := ioutil.ReadAll(r)
Colin Cross43f08db2018-11-12 10:13:39 -0800261 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700262 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800263 }
264
265 err = json.Unmarshal(data, config)
266 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700267 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800268 }
269
Colin Cross2d00f0d2019-05-09 21:50:00 -0700270 return data, nil
Colin Cross43f08db2018-11-12 10:13:39 -0800271}
Colin Cross69f59a32019-02-15 10:39:37 -0800272
273func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
274 return GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800275 DisablePreopt: false,
276 DisablePreoptModules: nil,
277 OnlyPreoptBootImageAndSystemServer: false,
278 HasSystemOther: false,
279 PatternsOnSystemOther: nil,
280 DisableGenerateProfile: false,
281 ProfileDir: "",
282 BootJars: nil,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100283 ArtApexJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800284 ProductUpdatableBootModules: nil,
285 ProductUpdatableBootLocations: nil,
286 SystemServerJars: nil,
287 SystemServerApps: nil,
288 SpeedApps: nil,
289 PreoptFlags: nil,
290 DefaultCompilerFilter: "",
291 SystemServerCompilerFilter: "",
292 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800293 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700294 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800295 AlwaysSystemServerDebugInfo: false,
296 NeverSystemServerDebugInfo: false,
297 AlwaysOtherDebugInfo: false,
298 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800299 IsEng: false,
300 SanitizeLite: false,
301 DefaultAppImages: false,
302 Dex2oatXmx: "",
303 Dex2oatXms: "",
304 EmptyDirectory: "empty_dir",
305 CpuVariant: nil,
306 InstructionSetFeatures: nil,
307 DirtyImageObjects: android.OptionalPath{},
Colin Cross69f59a32019-02-15 10:39:37 -0800308 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800309 BootFlags: "",
310 Dex2oatImageXmx: "",
311 Dex2oatImageXms: "",
312 Tools: Tools{
Colin Cross38b96852019-05-22 10:21:09 -0700313 Profman: android.PathForTesting("profman"),
314 Dex2oat: android.PathForTesting("dex2oat"),
315 Aapt: android.PathForTesting("aapt"),
316 SoongZip: android.PathForTesting("soong_zip"),
317 Zip2zip: android.PathForTesting("zip2zip"),
318 ManifestCheck: android.PathForTesting("manifest_check"),
319 ConstructContext: android.PathForTesting("construct_context.sh"),
Colin Cross69f59a32019-02-15 10:39:37 -0800320 },
321 }
322}