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