Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package dexpreopt |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 19 | "fmt" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 20 | "strings" |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 21 | |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint" |
| 23 | |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 24 | "android/soong/android" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 27 | // GlobalConfig stores the configuration for dex preopting. The fields are set |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 28 | // from product variables via dex_preopt_config.mk. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 29 | type GlobalConfig struct { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 30 | DisablePreopt bool // disable preopt for all modules |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 31 | DisablePreoptModules []string // modules with preopt disabled by product-specific config |
| 32 | |
| 33 | OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server |
| 34 | |
Vladimir Marko | 40139d6 | 2020-02-06 15:14:29 +0000 | [diff] [blame] | 35 | UseArtImage bool // use the art image (use other boot class path dex files without image) |
| 36 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 37 | 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 Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 40 | DisableGenerateProfile bool // don't generate profiles |
| 41 | ProfileDir string // directory to find profiles in |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 42 | |
Roshan Pius | ccc26ef | 2019-11-27 09:37:46 -0800 | [diff] [blame] | 43 | BootJars []string // modules for jars that form the boot class path |
| 44 | UpdatableBootJars []string // jars within apex that form the boot class path |
Vladimir Marko | d2ee532 | 2018-12-19 17:57:57 +0000 | [diff] [blame] | 45 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 46 | ArtApexJars []string // modules for jars that are in the ART APEX |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 47 | |
Roshan Pius | 9b51a40 | 2019-11-21 12:36:53 -0800 | [diff] [blame] | 48 | SystemServerJars []string // jars that form the system server |
| 49 | SystemServerApps []string // apps that are loaded into system server |
| 50 | UpdatableSystemServerJars []string // jars within apex that are loaded into system server |
| 51 | SpeedApps []string // apps that should be speed optimized |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 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 Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 58 | GenerateDMFiles bool // generate Dex Metadata files |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 59 | |
| 60 | NoDebugInfo bool // don't generate debug info by default |
Mathieu Chartier | 3f7ddbb | 2019-04-29 09:33:50 -0700 | [diff] [blame] | 61 | DontResolveStartupStrings bool // don't resolve string literals loaded during application startup. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 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 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 67 | IsEng bool // build is a eng variant |
| 68 | SanitizeLite bool // build is the second phase of a SANITIZE_LITE build |
| 69 | |
| 70 | DefaultAppImages bool // build app images (TODO: .art files?) by default |
| 71 | |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 72 | Dex2oatXmx string // max heap size for dex2oat |
| 73 | Dex2oatXms string // initial heap size for dex2oat |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 74 | |
| 75 | EmptyDirectory string // path to an empty directory |
| 76 | |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 77 | CpuVariant map[android.ArchType]string // cpu variant for each architecture |
| 78 | InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 79 | |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 80 | // Only used for boot image |
Mathieu Chartier | 6adeee1 | 2019-06-26 10:01:36 -0700 | [diff] [blame] | 81 | DirtyImageObjects android.OptionalPath // path to a dirty-image-objects file |
| 82 | BootImageProfiles android.Paths // path to a boot-image-profile.txt file |
| 83 | BootFlags string // extra flags to pass to dex2oat for the boot image |
| 84 | Dex2oatImageXmx string // max heap size for dex2oat for the boot image |
| 85 | Dex2oatImageXms string // initial heap size for dex2oat for the boot image |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 88 | // GlobalSoongConfig contains the global config that is generated from Soong, |
| 89 | // stored in dexpreopt_soong.config. |
| 90 | type GlobalSoongConfig struct { |
| 91 | // Paths to tools possibly used by the generated commands. |
| 92 | Profman android.Path |
| 93 | Dex2oat android.Path |
| 94 | Aapt android.Path |
| 95 | SoongZip android.Path |
| 96 | Zip2zip android.Path |
| 97 | ManifestCheck android.Path |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 98 | ConstructContext android.Path |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | type ModuleConfig struct { |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 102 | Name string |
| 103 | DexLocation string // dex location on device |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 104 | BuildPath android.OutputPath |
| 105 | DexPath android.Path |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 106 | ManifestPath android.Path |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 107 | UncompressedDex bool |
| 108 | HasApkLibraries bool |
| 109 | PreoptFlags []string |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 110 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 111 | ProfileClassListing android.OptionalPath |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 112 | ProfileIsTextListing bool |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 113 | ProfileBootListing android.OptionalPath |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 114 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 115 | EnforceUsesLibraries bool |
| 116 | PresentOptionalUsesLibraries []string |
| 117 | UsesLibraries []string |
| 118 | LibraryPaths map[string]android.Path |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 119 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 120 | Archs []android.ArchType |
| 121 | DexPreoptImages []android.Path |
| 122 | DexPreoptImagesDeps []android.OutputPaths |
| 123 | DexPreoptImageLocations []string |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 124 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 125 | PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files |
| 126 | PreoptBootClassPathDexLocations []string // virtual locations of boot class path files |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 127 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 128 | PreoptExtractedApk bool // Overrides OnlyPreoptModules |
| 129 | |
| 130 | NoCreateAppImage bool |
| 131 | ForceCreateAppImage bool |
| 132 | |
| 133 | PresignedPrebuilt bool |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 136 | type globalSoongConfigSingleton struct{} |
| 137 | |
| 138 | var pctx = android.NewPackageContext("android/soong/dexpreopt") |
| 139 | |
| 140 | func init() { |
| 141 | pctx.Import("android/soong/android") |
| 142 | android.RegisterSingletonType("dexpreopt-soong-config", func() android.Singleton { |
| 143 | return &globalSoongConfigSingleton{} |
| 144 | }) |
| 145 | } |
| 146 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 147 | func constructPath(ctx android.PathContext, path string) android.Path { |
| 148 | buildDirPrefix := ctx.Config().BuildDir() + "/" |
| 149 | if path == "" { |
| 150 | return nil |
| 151 | } else if strings.HasPrefix(path, buildDirPrefix) { |
| 152 | return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix)) |
| 153 | } else { |
| 154 | return android.PathForSource(ctx, path) |
| 155 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 158 | func constructPaths(ctx android.PathContext, paths []string) android.Paths { |
| 159 | var ret android.Paths |
| 160 | for _, path := range paths { |
| 161 | ret = append(ret, constructPath(ctx, path)) |
| 162 | } |
| 163 | return ret |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 166 | func constructPathMap(ctx android.PathContext, paths map[string]string) map[string]android.Path { |
| 167 | ret := map[string]android.Path{} |
| 168 | for key, path := range paths { |
| 169 | ret[key] = constructPath(ctx, path) |
| 170 | } |
| 171 | return ret |
| 172 | } |
| 173 | |
| 174 | func constructWritablePath(ctx android.PathContext, path string) android.WritablePath { |
| 175 | if path == "" { |
| 176 | return nil |
| 177 | } |
| 178 | return constructPath(ctx, path).(android.WritablePath) |
| 179 | } |
| 180 | |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 181 | // ParseGlobalConfig parses the given data assumed to be read from the global |
| 182 | // dexpreopt.config file into a GlobalConfig struct. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 183 | func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 184 | type GlobalJSONConfig struct { |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 185 | *GlobalConfig |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 186 | |
| 187 | // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be |
| 188 | // used to construct the real value manually below. |
| 189 | DirtyImageObjects string |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 190 | BootImageProfiles []string |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | config := GlobalJSONConfig{} |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 194 | err := json.Unmarshal(data, &config) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 195 | if err != nil { |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 196 | return config.GlobalConfig, err |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Construct paths that require a PathContext. |
| 200 | config.GlobalConfig.DirtyImageObjects = android.OptionalPathForPath(constructPath(ctx, config.DirtyImageObjects)) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 201 | config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles) |
| 202 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 203 | return config.GlobalConfig, nil |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 206 | type globalConfigAndRaw struct { |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 207 | global *GlobalConfig |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 208 | data []byte |
| 209 | } |
| 210 | |
| 211 | // GetGlobalConfig returns the global dexpreopt.config that's created in the |
| 212 | // make config phase. It is loaded once the first time it is called for any |
| 213 | // ctx.Config(), and returns the same data for all future calls with the same |
| 214 | // ctx.Config(). A value can be inserted for tests using |
| 215 | // setDexpreoptTestGlobalConfig. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 216 | func GetGlobalConfig(ctx android.PathContext) *GlobalConfig { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 217 | return getGlobalConfigRaw(ctx).global |
| 218 | } |
| 219 | |
| 220 | // GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns |
| 221 | // the literal content of dexpreopt.config. |
| 222 | func GetGlobalConfigRawData(ctx android.PathContext) []byte { |
| 223 | return getGlobalConfigRaw(ctx).data |
| 224 | } |
| 225 | |
| 226 | var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig") |
| 227 | var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig") |
| 228 | |
| 229 | func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw { |
| 230 | return ctx.Config().Once(globalConfigOnceKey, func() interface{} { |
| 231 | if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil { |
| 232 | panic(err) |
| 233 | } else if data != nil { |
| 234 | globalConfig, err := ParseGlobalConfig(ctx, data) |
| 235 | if err != nil { |
| 236 | panic(err) |
| 237 | } |
| 238 | return globalConfigAndRaw{globalConfig, data} |
| 239 | } |
| 240 | |
| 241 | // No global config filename set, see if there is a test config set |
| 242 | return ctx.Config().Once(testGlobalConfigOnceKey, func() interface{} { |
| 243 | // Nope, return a config with preopting disabled |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 244 | return globalConfigAndRaw{&GlobalConfig{ |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 245 | DisablePreopt: true, |
| 246 | DisableGenerateProfile: true, |
| 247 | }, nil} |
| 248 | }) |
| 249 | }).(globalConfigAndRaw) |
| 250 | } |
| 251 | |
| 252 | // SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig |
| 253 | // will return. It must be called before the first call to GetGlobalConfig for |
| 254 | // the config. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 255 | func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 256 | config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} }) |
| 257 | } |
| 258 | |
| 259 | // ParseModuleConfig parses a per-module dexpreopt.config file into a |
| 260 | // ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig |
| 261 | // struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called |
| 262 | // from Make to read the module dexpreopt.config written in the Make config |
| 263 | // stage. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 264 | func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 265 | type ModuleJSONConfig struct { |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 266 | *ModuleConfig |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 267 | |
| 268 | // Copies of entries in ModuleConfig that are not constructable without extra parameters. They will be |
| 269 | // used to construct the real value manually below. |
| 270 | BuildPath string |
| 271 | DexPath string |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 272 | ManifestPath string |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 273 | ProfileClassListing string |
| 274 | LibraryPaths map[string]string |
| 275 | DexPreoptImages []string |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 276 | DexPreoptImageLocations []string |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 277 | PreoptBootClassPathDexFiles []string |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | config := ModuleJSONConfig{} |
| 281 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 282 | err := json.Unmarshal(data, &config) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 283 | if err != nil { |
| 284 | return config.ModuleConfig, err |
| 285 | } |
| 286 | |
| 287 | // Construct paths that require a PathContext. |
| 288 | config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath) |
| 289 | config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath) |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 290 | config.ModuleConfig.ManifestPath = constructPath(ctx, config.ManifestPath) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 291 | config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing)) |
| 292 | config.ModuleConfig.LibraryPaths = constructPathMap(ctx, config.LibraryPaths) |
| 293 | config.ModuleConfig.DexPreoptImages = constructPaths(ctx, config.DexPreoptImages) |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 294 | config.ModuleConfig.DexPreoptImageLocations = config.DexPreoptImageLocations |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 295 | config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 296 | |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 297 | // This needs to exist, but dependencies are already handled in Make, so we don't need to pass them through JSON. |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 298 | config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.DexPreoptImages)) |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 299 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 300 | return config.ModuleConfig, nil |
| 301 | } |
| 302 | |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 303 | // dex2oatModuleName returns the name of the module to use for the dex2oat host |
| 304 | // tool. It should be a binary module with public visibility that is compiled |
| 305 | // and installed for host. |
| 306 | func dex2oatModuleName(config android.Config) string { |
| 307 | // Default to the debug variant of dex2oat to help find bugs. |
| 308 | // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions. |
| 309 | if config.Getenv("USE_DEX2OAT_DEBUG") == "false" { |
| 310 | return "dex2oat" |
| 311 | } else { |
| 312 | return "dex2oatd" |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | var dex2oatDepTag = struct { |
| 317 | blueprint.BaseDependencyTag |
| 318 | }{} |
| 319 | |
| 320 | type DexPreoptModule interface { |
| 321 | dexPreoptModuleSignature() // Not called - only for type detection. |
| 322 | } |
| 323 | |
| 324 | // RegisterToolDepsMutator registers a mutator that adds the necessary |
| 325 | // dependencies to binary modules for tools that are required later when |
| 326 | // Get(Cached)GlobalSoongConfig is called. It should be passed to |
| 327 | // android.RegistrationContext.FinalDepsMutators, and module types that need |
| 328 | // dependencies also need to embed DexPreoptModule. |
| 329 | func RegisterToolDepsMutator(ctx android.RegisterMutatorsContext) { |
| 330 | ctx.BottomUp("dexpreopt_tool_deps", toolDepsMutator).Parallel() |
| 331 | } |
| 332 | |
| 333 | func toolDepsMutator(ctx android.BottomUpMutatorContext) { |
| 334 | if GetGlobalConfig(ctx).DisablePreopt { |
| 335 | // Only register dependencies if dexpreopting is enabled. Necessary to avoid |
| 336 | // them in non-platform builds where dex2oat etc isn't available. |
| 337 | // |
| 338 | // It would be nice to not register this mutator at all then, but |
| 339 | // RegisterMutatorsContext available at registration doesn't have the state |
| 340 | // necessary to pass as PathContext to constructPath etc. |
| 341 | return |
| 342 | } |
| 343 | if _, ok := ctx.Module().(DexPreoptModule); !ok { |
| 344 | return |
| 345 | } |
| 346 | dex2oatBin := dex2oatModuleName(ctx.Config()) |
| 347 | v := ctx.Config().BuildOSTarget.Variations() |
| 348 | ctx.AddFarVariationDependencies(v, dex2oatDepTag, dex2oatBin) |
| 349 | } |
| 350 | |
| 351 | func dex2oatPathFromDep(ctx android.ModuleContext) android.Path { |
| 352 | dex2oatBin := dex2oatModuleName(ctx.Config()) |
| 353 | |
| 354 | dex2oatModule := ctx.GetDirectDepWithTag(dex2oatBin, dex2oatDepTag) |
| 355 | if dex2oatModule == nil { |
| 356 | // If this happens there's probably a missing call to AddToolDeps in DepsMutator. |
| 357 | panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin)) |
| 358 | } |
| 359 | |
| 360 | dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath() |
| 361 | if !dex2oatPath.Valid() { |
| 362 | panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule)) |
| 363 | } |
| 364 | |
| 365 | return dex2oatPath.Path() |
| 366 | } |
| 367 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 368 | // createGlobalSoongConfig creates a GlobalSoongConfig from the current context. |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 369 | // Should not be used in dexpreopt_gen. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 370 | func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 371 | if ctx.Config().TestProductVariables != nil { |
| 372 | // If we're called in a test there'll be a confusing error from the path |
| 373 | // functions below that gets reported without a stack trace, so let's panic |
| 374 | // properly with a more helpful message. |
| 375 | panic("This should not be called from tests. Please call GlobalSoongConfigForTests somewhere in the test setup.") |
| 376 | } |
| 377 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 378 | return &GlobalSoongConfig{ |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 379 | Profman: ctx.Config().HostToolPath(ctx, "profman"), |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 380 | Dex2oat: dex2oatPathFromDep(ctx), |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 381 | Aapt: ctx.Config().HostToolPath(ctx, "aapt"), |
| 382 | SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"), |
| 383 | Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"), |
| 384 | ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"), |
| 385 | ConstructContext: android.PathForSource(ctx, "build/make/core/construct_context.sh"), |
| 386 | } |
| 387 | } |
| 388 | |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 389 | // The main reason for this Once cache for GlobalSoongConfig is to make the |
| 390 | // dex2oat path available to singletons. In ordinary modules we get it through a |
| 391 | // dex2oatDepTag dependency, but in singletons there's no simple way to do the |
| 392 | // same thing and ensure the right variant is selected, hence this cache to make |
| 393 | // the resolved path available to singletons. This means we depend on there |
| 394 | // being at least one ordinary module with a dex2oatDepTag dependency. |
| 395 | // |
| 396 | // TODO(b/147613152): Implement a way to deal with dependencies from singletons, |
| 397 | // and then possibly remove this cache altogether (but the use in |
| 398 | // GlobalSoongConfigForTests also needs to be rethought). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 399 | var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig") |
| 400 | |
| 401 | // GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called, |
| 402 | // and later returns the same cached instance. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 403 | func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 404 | globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} { |
| 405 | return createGlobalSoongConfig(ctx) |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 406 | }).(*GlobalSoongConfig) |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 407 | |
| 408 | // Always resolve the tool path from the dependency, to ensure that every |
| 409 | // module has the dependency added properly. |
| 410 | myDex2oat := dex2oatPathFromDep(ctx) |
| 411 | if myDex2oat != globalSoong.Dex2oat { |
| 412 | panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat)) |
| 413 | } |
| 414 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 415 | return globalSoong |
| 416 | } |
| 417 | |
| 418 | // GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an |
| 419 | // earlier GetGlobalSoongConfig call. This function works with any context |
| 420 | // compatible with a basic PathContext, since it doesn't try to create a |
| 421 | // GlobalSoongConfig (which requires a full ModuleContext). It will panic if |
| 422 | // called before the first GetGlobalSoongConfig call. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 423 | func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig { |
| 424 | return ctx.Config().Get(globalSoongConfigOnceKey).(*GlobalSoongConfig) |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 427 | type globalJsonSoongConfig struct { |
| 428 | Profman string |
| 429 | Dex2oat string |
| 430 | Aapt string |
| 431 | SoongZip string |
| 432 | Zip2zip string |
| 433 | ManifestCheck string |
| 434 | ConstructContext string |
| 435 | } |
| 436 | |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 437 | // ParseGlobalSoongConfig parses the given data assumed to be read from the |
| 438 | // global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is |
| 439 | // only used in dexpreopt_gen. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 440 | func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) { |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 441 | var jc globalJsonSoongConfig |
| 442 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 443 | err := json.Unmarshal(data, &jc) |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 444 | if err != nil { |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 445 | return &GlobalSoongConfig{}, err |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 448 | config := &GlobalSoongConfig{ |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 449 | Profman: constructPath(ctx, jc.Profman), |
| 450 | Dex2oat: constructPath(ctx, jc.Dex2oat), |
| 451 | Aapt: constructPath(ctx, jc.Aapt), |
| 452 | SoongZip: constructPath(ctx, jc.SoongZip), |
| 453 | Zip2zip: constructPath(ctx, jc.Zip2zip), |
| 454 | ManifestCheck: constructPath(ctx, jc.ManifestCheck), |
| 455 | ConstructContext: constructPath(ctx, jc.ConstructContext), |
| 456 | } |
| 457 | |
| 458 | return config, nil |
| 459 | } |
| 460 | |
| 461 | func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 462 | if GetGlobalConfig(ctx).DisablePreopt { |
| 463 | return |
| 464 | } |
| 465 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 466 | config := GetCachedGlobalSoongConfig(ctx) |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 467 | jc := globalJsonSoongConfig{ |
| 468 | Profman: config.Profman.String(), |
| 469 | Dex2oat: config.Dex2oat.String(), |
| 470 | Aapt: config.Aapt.String(), |
| 471 | SoongZip: config.SoongZip.String(), |
| 472 | Zip2zip: config.Zip2zip.String(), |
| 473 | ManifestCheck: config.ManifestCheck.String(), |
| 474 | ConstructContext: config.ConstructContext.String(), |
| 475 | } |
| 476 | |
| 477 | data, err := json.Marshal(jc) |
| 478 | if err != nil { |
| 479 | ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err) |
| 480 | return |
| 481 | } |
| 482 | |
| 483 | ctx.Build(pctx, android.BuildParams{ |
| 484 | Rule: android.WriteFile, |
| 485 | Output: android.PathForOutput(ctx, "dexpreopt_soong.config"), |
| 486 | Args: map[string]string{ |
| 487 | "content": string(data), |
| 488 | }, |
| 489 | }) |
| 490 | } |
| 491 | |
| 492 | func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) { |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 493 | if GetGlobalConfig(ctx).DisablePreopt { |
| 494 | return |
| 495 | } |
| 496 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 497 | config := GetCachedGlobalSoongConfig(ctx) |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 498 | |
| 499 | ctx.Strict("DEX2OAT", config.Dex2oat.String()) |
| 500 | ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{ |
| 501 | config.Profman.String(), |
| 502 | config.Dex2oat.String(), |
| 503 | config.Aapt.String(), |
| 504 | config.SoongZip.String(), |
| 505 | config.Zip2zip.String(), |
| 506 | config.ManifestCheck.String(), |
| 507 | config.ConstructContext.String(), |
| 508 | }, " ")) |
| 509 | } |
| 510 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 511 | func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig { |
| 512 | return &GlobalConfig{ |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 513 | DisablePreopt: false, |
| 514 | DisablePreoptModules: nil, |
| 515 | OnlyPreoptBootImageAndSystemServer: false, |
| 516 | HasSystemOther: false, |
| 517 | PatternsOnSystemOther: nil, |
| 518 | DisableGenerateProfile: false, |
| 519 | ProfileDir: "", |
| 520 | BootJars: nil, |
Roshan Pius | ccc26ef | 2019-11-27 09:37:46 -0800 | [diff] [blame] | 521 | UpdatableBootJars: nil, |
Martin Stjernholm | cc4b0ad | 2019-07-05 22:38:25 +0100 | [diff] [blame] | 522 | ArtApexJars: nil, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 523 | SystemServerJars: nil, |
| 524 | SystemServerApps: nil, |
Roshan Pius | 9b51a40 | 2019-11-21 12:36:53 -0800 | [diff] [blame] | 525 | UpdatableSystemServerJars: nil, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 526 | SpeedApps: nil, |
| 527 | PreoptFlags: nil, |
| 528 | DefaultCompilerFilter: "", |
| 529 | SystemServerCompilerFilter: "", |
| 530 | GenerateDMFiles: false, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 531 | NoDebugInfo: false, |
Mathieu Chartier | 3f7ddbb | 2019-04-29 09:33:50 -0700 | [diff] [blame] | 532 | DontResolveStartupStrings: false, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 533 | AlwaysSystemServerDebugInfo: false, |
| 534 | NeverSystemServerDebugInfo: false, |
| 535 | AlwaysOtherDebugInfo: false, |
| 536 | NeverOtherDebugInfo: false, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 537 | IsEng: false, |
| 538 | SanitizeLite: false, |
| 539 | DefaultAppImages: false, |
| 540 | Dex2oatXmx: "", |
| 541 | Dex2oatXms: "", |
| 542 | EmptyDirectory: "empty_dir", |
| 543 | CpuVariant: nil, |
| 544 | InstructionSetFeatures: nil, |
| 545 | DirtyImageObjects: android.OptionalPath{}, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 546 | BootImageProfiles: nil, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 547 | BootFlags: "", |
| 548 | Dex2oatImageXmx: "", |
| 549 | Dex2oatImageXms: "", |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 553 | func GlobalSoongConfigForTests(config android.Config) *GlobalSoongConfig { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 554 | // Install the test GlobalSoongConfig in the Once cache so that later calls to |
| 555 | // Get(Cached)GlobalSoongConfig returns it without trying to create a real one. |
| 556 | return config.Once(globalSoongConfigOnceKey, func() interface{} { |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 557 | return &GlobalSoongConfig{ |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 558 | Profman: android.PathForTesting("profman"), |
| 559 | Dex2oat: android.PathForTesting("dex2oat"), |
| 560 | Aapt: android.PathForTesting("aapt"), |
| 561 | SoongZip: android.PathForTesting("soong_zip"), |
| 562 | Zip2zip: android.PathForTesting("zip2zip"), |
| 563 | ManifestCheck: android.PathForTesting("manifest_check"), |
| 564 | ConstructContext: android.PathForTesting("construct_context.sh"), |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 565 | } |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame^] | 566 | }).(*GlobalSoongConfig) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 567 | } |