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