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