blob: 83e36737136ed3b712c869c021535819fe6cdd9f [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
Roshan Piusccc26ef2019-11-27 09:37:46 -080041 BootJars []string // modules for jars that form the boot class path
42 UpdatableBootJars []string // jars within apex that form the boot class path
Vladimir Markod2ee5322018-12-19 17:57:57 +000043
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000044 ArtApexJars []string // modules for jars that are in the ART APEX
Colin Cross800fe132019-02-11 14:21:24 -080045
Roshan Pius9b51a402019-11-21 12:36:53 -080046 SystemServerJars []string // jars that form the system server
47 SystemServerApps []string // apps that are loaded into system server
48 UpdatableSystemServerJars []string // jars within apex that are loaded into system server
49 SpeedApps []string // apps that should be speed optimized
Colin Cross43f08db2018-11-12 10:13:39 -080050
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
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000120 Archs []android.ArchType
121 DexPreoptImages []android.Path
122 DexPreoptImagesDeps []android.OutputPaths
123 DexPreoptImageLocations []string
Colin Cross43f08db2018-11-12 10:13:39 -0800124
Colin Cross69f59a32019-02-15 10:39:37 -0800125 PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
126 PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
Colin Cross800fe132019-02-11 14:21:24 -0800127
Colin Cross43f08db2018-11-12 10:13:39 -0800128 PreoptExtractedApk bool // Overrides OnlyPreoptModules
129
130 NoCreateAppImage bool
131 ForceCreateAppImage bool
132
133 PresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -0800134}
135
Colin Cross69f59a32019-02-15 10:39:37 -0800136func constructPath(ctx android.PathContext, path string) android.Path {
137 buildDirPrefix := ctx.Config().BuildDir() + "/"
138 if path == "" {
139 return nil
140 } else if strings.HasPrefix(path, buildDirPrefix) {
141 return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
142 } else {
143 return android.PathForSource(ctx, path)
144 }
Colin Cross43f08db2018-11-12 10:13:39 -0800145}
146
Colin Cross69f59a32019-02-15 10:39:37 -0800147func constructPaths(ctx android.PathContext, paths []string) android.Paths {
148 var ret android.Paths
149 for _, path := range paths {
150 ret = append(ret, constructPath(ctx, path))
151 }
152 return ret
Colin Cross43f08db2018-11-12 10:13:39 -0800153}
154
Colin Cross69f59a32019-02-15 10:39:37 -0800155func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path {
156 ret := map[string]android.Path{}
157 for key, path := range paths {
158 ret[key] = constructPath(ctx, path)
159 }
160 return ret
161}
162
163func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
164 if path == "" {
165 return nil
166 }
167 return constructPath(ctx, path).(android.WritablePath)
168}
169
170// LoadGlobalConfig reads the global dexpreopt.config file into a GlobalConfig struct. It is used directly in Soong
171// and in dexpreopt_gen called from Make to read the $OUT/dexpreopt.config written by Make.
Colin Cross2d00f0d2019-05-09 21:50:00 -0700172func LoadGlobalConfig(ctx android.PathContext, path string) (GlobalConfig, []byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800173 type GlobalJSONConfig struct {
174 GlobalConfig
175
176 // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
177 // used to construct the real value manually below.
178 DirtyImageObjects string
Colin Cross69f59a32019-02-15 10:39:37 -0800179 BootImageProfiles []string
180
181 Tools struct {
Colin Cross38b96852019-05-22 10:21:09 -0700182 Profman string
183 Dex2oat string
184 Aapt string
185 SoongZip string
186 Zip2zip string
187 ManifestCheck string
Colin Cross69f59a32019-02-15 10:39:37 -0800188
Colin Cross38b96852019-05-22 10:21:09 -0700189 ConstructContext string
Colin Cross69f59a32019-02-15 10:39:37 -0800190 }
191 }
192
193 config := GlobalJSONConfig{}
Colin Cross2d00f0d2019-05-09 21:50:00 -0700194 data, err := loadConfig(ctx, path, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800195 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700196 return config.GlobalConfig, nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800197 }
198
199 // Construct paths that require a PathContext.
200 config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects))
Colin Cross69f59a32019-02-15 10:39:37 -0800201 config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
202
203 config.GlobalConfig.Tools.Profman = constructPath(ctx, config.Tools.Profman)
204 config.GlobalConfig.Tools.Dex2oat = constructPath(ctx, config.Tools.Dex2oat)
205 config.GlobalConfig.Tools.Aapt = constructPath(ctx, config.Tools.Aapt)
206 config.GlobalConfig.Tools.SoongZip = constructPath(ctx, config.Tools.SoongZip)
207 config.GlobalConfig.Tools.Zip2zip = constructPath(ctx, config.Tools.Zip2zip)
Colin Cross38b96852019-05-22 10:21:09 -0700208 config.GlobalConfig.Tools.ManifestCheck = constructPath(ctx, config.Tools.ManifestCheck)
Colin Cross69f59a32019-02-15 10:39:37 -0800209 config.GlobalConfig.Tools.ConstructContext = constructPath(ctx, config.Tools.ConstructContext)
210
Colin Cross2d00f0d2019-05-09 21:50:00 -0700211 return config.GlobalConfig, data, nil
Colin Cross69f59a32019-02-15 10:39:37 -0800212}
213
214// LoadModuleConfig reads a per-module dexpreopt.config file into a ModuleConfig struct. It is not used in Soong, which
215// receives a ModuleConfig struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called from oMake to
216// read the module dexpreopt.config written by Make.
217func LoadModuleConfig(ctx android.PathContext, path string) (ModuleConfig, error) {
218 type ModuleJSONConfig struct {
219 ModuleConfig
220
221 // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be
222 // used to construct the real value manually below.
223 BuildPath string
224 DexPath string
Colin Cross38b96852019-05-22 10:21:09 -0700225 ManifestPath string
Colin Cross69f59a32019-02-15 10:39:37 -0800226 ProfileClassListing string
227 LibraryPaths map[string]string
228 DexPreoptImages []string
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000229 DexPreoptImageLocations []string
Colin Cross69f59a32019-02-15 10:39:37 -0800230 PreoptBootClassPathDexFiles []string
Colin Cross69f59a32019-02-15 10:39:37 -0800231 }
232
233 config := ModuleJSONConfig{}
234
Colin Cross2d00f0d2019-05-09 21:50:00 -0700235 _, err := loadConfig(ctx, path, &config)
Colin Cross69f59a32019-02-15 10:39:37 -0800236 if err != nil {
237 return config.ModuleConfig, err
238 }
239
240 // Construct paths that require a PathContext.
241 config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
242 config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
Colin Cross38b96852019-05-22 10:21:09 -0700243 config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath)
Colin Cross69f59a32019-02-15 10:39:37 -0800244 config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
245 config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths)
246 config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000247 config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations
Colin Cross69f59a32019-02-15 10:39:37 -0800248 config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
Colin Cross69f59a32019-02-15 10:39:37 -0800249
Dan Willemsen0f416782019-06-13 21:44:53 +0000250 // This needs to exist, but dependencies are already handled in Make, so we don't need to pass them through JSON.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000251 config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages))
Dan Willemsen0f416782019-06-13 21:44:53 +0000252
Colin Cross69f59a32019-02-15 10:39:37 -0800253 return config.ModuleConfig, nil
254}
255
Colin Cross2d00f0d2019-05-09 21:50:00 -0700256func loadConfig(ctx android.PathContext, path string, config interface{}) ([]byte, error) {
Colin Cross69f59a32019-02-15 10:39:37 -0800257 r, err := ctx.Fs().Open(path)
258 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700259 return nil, err
Colin Cross69f59a32019-02-15 10:39:37 -0800260 }
261 defer r.Close()
262
263 data, err := ioutil.ReadAll(r)
Colin Cross43f08db2018-11-12 10:13:39 -0800264 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700265 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800266 }
267
268 err = json.Unmarshal(data, config)
269 if err != nil {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700270 return nil, err
Colin Cross43f08db2018-11-12 10:13:39 -0800271 }
272
Colin Cross2d00f0d2019-05-09 21:50:00 -0700273 return data, nil
Colin Cross43f08db2018-11-12 10:13:39 -0800274}
Colin Cross69f59a32019-02-15 10:39:37 -0800275
276func GlobalConfigForTests(ctx android.PathContext) GlobalConfig {
277 return GlobalConfig{
Colin Cross69f59a32019-02-15 10:39:37 -0800278 DisablePreopt: false,
279 DisablePreoptModules: nil,
280 OnlyPreoptBootImageAndSystemServer: false,
281 HasSystemOther: false,
282 PatternsOnSystemOther: nil,
283 DisableGenerateProfile: false,
284 ProfileDir: "",
285 BootJars: nil,
Roshan Piusccc26ef2019-11-27 09:37:46 -0800286 UpdatableBootJars: nil,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100287 ArtApexJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800288 SystemServerJars: nil,
289 SystemServerApps: nil,
Roshan Pius9b51a402019-11-21 12:36:53 -0800290 UpdatableSystemServerJars: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800291 SpeedApps: nil,
292 PreoptFlags: nil,
293 DefaultCompilerFilter: "",
294 SystemServerCompilerFilter: "",
295 GenerateDMFiles: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800296 NoDebugInfo: false,
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700297 DontResolveStartupStrings: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800298 AlwaysSystemServerDebugInfo: false,
299 NeverSystemServerDebugInfo: false,
300 AlwaysOtherDebugInfo: false,
301 NeverOtherDebugInfo: false,
Colin Cross69f59a32019-02-15 10:39:37 -0800302 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{},
Colin Cross69f59a32019-02-15 10:39:37 -0800311 BootImageProfiles: nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800312 BootFlags: "",
313 Dex2oatImageXmx: "",
314 Dex2oatImageXms: "",
315 Tools: Tools{
Colin Cross38b96852019-05-22 10:21:09 -0700316 Profman: android.PathForTesting("profman"),
317 Dex2oat: android.PathForTesting("dex2oat"),
318 Aapt: android.PathForTesting("aapt"),
319 SoongZip: android.PathForTesting("soong_zip"),
320 Zip2zip: android.PathForTesting("zip2zip"),
321 ManifestCheck: android.PathForTesting("manifest_check"),
322 ConstructContext: android.PathForTesting("construct_context.sh"),
Colin Cross69f59a32019-02-15 10:39:37 -0800323 },
324 }
325}