config.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. package dexpreopt
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "strings"
  20. "github.com/google/blueprint"
  21. "android/soong/android"
  22. )
  23. // GlobalConfig stores the configuration for dex preopting. The fields are set
  24. // from product variables via dex_preopt_config.mk.
  25. type GlobalConfig struct {
  26. DisablePreopt bool // disable preopt for all modules (excluding boot images)
  27. DisablePreoptBootImages bool // disable prepot for boot images
  28. DisablePreoptModules []string // modules with preopt disabled by product-specific config
  29. OnlyPreoptBootImageAndSystemServer bool // only preopt jars in the boot image or system server
  30. PreoptWithUpdatableBcp bool // If updatable boot jars are included in dexpreopt or not.
  31. HasSystemOther bool // store odex files that match PatternsOnSystemOther on the system_other partition
  32. PatternsOnSystemOther []string // patterns (using '%' to denote a prefix match) to put odex on the system_other partition
  33. DisableGenerateProfile bool // don't generate profiles
  34. ProfileDir string // directory to find profiles in
  35. BootJars android.ConfiguredJarList // modules for jars that form the boot class path
  36. ApexBootJars android.ConfiguredJarList // jars within apex that form the boot class path
  37. ArtApexJars android.ConfiguredJarList // modules for jars that are in the ART APEX
  38. SystemServerJars android.ConfiguredJarList // system_server classpath jars on the platform
  39. SystemServerApps []string // apps that are loaded into system server
  40. ApexSystemServerJars android.ConfiguredJarList // system_server classpath jars delivered via apex
  41. StandaloneSystemServerJars android.ConfiguredJarList // jars on the platform that system_server loads dynamically using separate classloaders
  42. ApexStandaloneSystemServerJars android.ConfiguredJarList // jars delivered via apex that system_server loads dynamically using separate classloaders
  43. SpeedApps []string // apps that should be speed optimized
  44. BrokenSuboptimalOrderOfSystemServerJars bool // if true, sub-optimal order does not cause a build error
  45. PreoptFlags []string // global dex2oat flags that should be used if no module-specific dex2oat flags are specified
  46. DefaultCompilerFilter string // default compiler filter to pass to dex2oat, overridden by --compiler-filter= in module-specific dex2oat flags
  47. SystemServerCompilerFilter string // default compiler filter to pass to dex2oat for system server jars
  48. GenerateDMFiles bool // generate Dex Metadata files
  49. NoDebugInfo bool // don't generate debug info by default
  50. DontResolveStartupStrings bool // don't resolve string literals loaded during application startup.
  51. AlwaysSystemServerDebugInfo bool // always generate mini debug info for system server modules (overrides NoDebugInfo=true)
  52. NeverSystemServerDebugInfo bool // never generate mini debug info for system server modules (overrides NoDebugInfo=false)
  53. AlwaysOtherDebugInfo bool // always generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
  54. NeverOtherDebugInfo bool // never generate mini debug info for non-system server modules (overrides NoDebugInfo=true)
  55. IsEng bool // build is a eng variant
  56. SanitizeLite bool // build is the second phase of a SANITIZE_LITE build
  57. DefaultAppImages bool // build app images (TODO: .art files?) by default
  58. Dex2oatXmx string // max heap size for dex2oat
  59. Dex2oatXms string // initial heap size for dex2oat
  60. EmptyDirectory string // path to an empty directory
  61. CpuVariant map[android.ArchType]string // cpu variant for each architecture
  62. InstructionSetFeatures map[android.ArchType]string // instruction set for each architecture
  63. BootImageProfiles android.Paths // path to a boot-image-profile.txt file
  64. BootFlags string // extra flags to pass to dex2oat for the boot image
  65. Dex2oatImageXmx string // max heap size for dex2oat for the boot image
  66. Dex2oatImageXms string // initial heap size for dex2oat for the boot image
  67. // If true, downgrade the compiler filter of dexpreopt to "verify" when verify_uses_libraries
  68. // check fails, instead of failing the build. This will disable any AOT-compilation.
  69. //
  70. // The intended use case for this flag is to have a smoother migration path for the Java
  71. // modules that need to add <uses-library> information in their build files. The flag allows to
  72. // quickly silence build errors. This flag should be used with caution and only as a temporary
  73. // measure, as it masks real errors and affects performance.
  74. RelaxUsesLibraryCheck bool
  75. EnableUffdGc bool // preopt with the assumption that userfaultfd GC will be used on device.
  76. }
  77. var allPlatformSystemServerJarsKey = android.NewOnceKey("allPlatformSystemServerJars")
  78. // Returns all jars on the platform that system_server loads, including those on classpath and those
  79. // loaded dynamically.
  80. func (g *GlobalConfig) AllPlatformSystemServerJars(ctx android.PathContext) *android.ConfiguredJarList {
  81. return ctx.Config().Once(allPlatformSystemServerJarsKey, func() interface{} {
  82. res := g.SystemServerJars.AppendList(&g.StandaloneSystemServerJars)
  83. return &res
  84. }).(*android.ConfiguredJarList)
  85. }
  86. var allApexSystemServerJarsKey = android.NewOnceKey("allApexSystemServerJars")
  87. // Returns all jars delivered via apex that system_server loads, including those on classpath and
  88. // those loaded dynamically.
  89. func (g *GlobalConfig) AllApexSystemServerJars(ctx android.PathContext) *android.ConfiguredJarList {
  90. return ctx.Config().Once(allApexSystemServerJarsKey, func() interface{} {
  91. res := g.ApexSystemServerJars.AppendList(&g.ApexStandaloneSystemServerJars)
  92. return &res
  93. }).(*android.ConfiguredJarList)
  94. }
  95. var allSystemServerClasspathJarsKey = android.NewOnceKey("allSystemServerClasspathJars")
  96. // Returns all system_server classpath jars.
  97. func (g *GlobalConfig) AllSystemServerClasspathJars(ctx android.PathContext) *android.ConfiguredJarList {
  98. return ctx.Config().Once(allSystemServerClasspathJarsKey, func() interface{} {
  99. res := g.SystemServerJars.AppendList(&g.ApexSystemServerJars)
  100. return &res
  101. }).(*android.ConfiguredJarList)
  102. }
  103. var allSystemServerJarsKey = android.NewOnceKey("allSystemServerJars")
  104. // Returns all jars that system_server loads.
  105. func (g *GlobalConfig) AllSystemServerJars(ctx android.PathContext) *android.ConfiguredJarList {
  106. return ctx.Config().Once(allSystemServerJarsKey, func() interface{} {
  107. res := g.AllPlatformSystemServerJars(ctx).AppendList(g.AllApexSystemServerJars(ctx))
  108. return &res
  109. }).(*android.ConfiguredJarList)
  110. }
  111. // GlobalSoongConfig contains the global config that is generated from Soong,
  112. // stored in dexpreopt_soong.config.
  113. type GlobalSoongConfig struct {
  114. // Paths to tools possibly used by the generated commands.
  115. Profman android.Path
  116. Dex2oat android.Path
  117. Aapt android.Path
  118. SoongZip android.Path
  119. Zip2zip android.Path
  120. ManifestCheck android.Path
  121. ConstructContext android.Path
  122. }
  123. type ModuleConfig struct {
  124. Name string
  125. DexLocation string // dex location on device
  126. BuildPath android.OutputPath
  127. DexPath android.Path
  128. ManifestPath android.OptionalPath
  129. UncompressedDex bool
  130. HasApkLibraries bool
  131. PreoptFlags []string
  132. ProfileClassListing android.OptionalPath
  133. ProfileIsTextListing bool
  134. ProfileBootListing android.OptionalPath
  135. EnforceUsesLibraries bool // turn on build-time verify_uses_libraries check
  136. EnforceUsesLibrariesStatusFile android.Path // a file with verify_uses_libraries errors (if any)
  137. ProvidesUsesLibrary string // library name (usually the same as module name)
  138. ClassLoaderContexts ClassLoaderContextMap
  139. Archs []android.ArchType
  140. DexPreoptImagesDeps []android.OutputPaths
  141. DexPreoptImageLocationsOnHost []string // boot image location on host (file path without the arch subdirectory)
  142. DexPreoptImageLocationsOnDevice []string // boot image location on device (file path without the arch subdirectory)
  143. PreoptBootClassPathDexFiles android.Paths // file paths of boot class path files
  144. PreoptBootClassPathDexLocations []string // virtual locations of boot class path files
  145. PreoptExtractedApk bool // Overrides OnlyPreoptModules
  146. NoCreateAppImage bool
  147. ForceCreateAppImage bool
  148. PresignedPrebuilt bool
  149. }
  150. type globalSoongConfigSingleton struct{}
  151. var pctx = android.NewPackageContext("android/soong/dexpreopt")
  152. func init() {
  153. pctx.Import("android/soong/android")
  154. android.RegisterParallelSingletonType("dexpreopt-soong-config", func() android.Singleton {
  155. return &globalSoongConfigSingleton{}
  156. })
  157. }
  158. func constructPath(ctx android.PathContext, path string) android.Path {
  159. buildDirPrefix := ctx.Config().SoongOutDir() + "/"
  160. if path == "" {
  161. return nil
  162. } else if strings.HasPrefix(path, buildDirPrefix) {
  163. return android.PathForOutput(ctx, strings.TrimPrefix(path, buildDirPrefix))
  164. } else {
  165. return android.PathForSource(ctx, path)
  166. }
  167. }
  168. func constructPaths(ctx android.PathContext, paths []string) android.Paths {
  169. var ret android.Paths
  170. for _, path := range paths {
  171. ret = append(ret, constructPath(ctx, path))
  172. }
  173. return ret
  174. }
  175. func constructWritablePath(ctx android.PathContext, path string) android.WritablePath {
  176. if path == "" {
  177. return nil
  178. }
  179. return constructPath(ctx, path).(android.WritablePath)
  180. }
  181. // ParseGlobalConfig parses the given data assumed to be read from the global
  182. // dexpreopt.config file into a GlobalConfig struct.
  183. func ParseGlobalConfig(ctx android.PathContext, data []byte) (*GlobalConfig, error) {
  184. type GlobalJSONConfig struct {
  185. *GlobalConfig
  186. // Copies of entries in GlobalConfig that are not constructable without extra parameters. They will be
  187. // used to construct the real value manually below.
  188. BootImageProfiles []string
  189. }
  190. config := GlobalJSONConfig{}
  191. err := json.Unmarshal(data, &config)
  192. if err != nil {
  193. return config.GlobalConfig, err
  194. }
  195. // Construct paths that require a PathContext.
  196. config.GlobalConfig.BootImageProfiles = constructPaths(ctx, config.BootImageProfiles)
  197. return config.GlobalConfig, nil
  198. }
  199. type globalConfigAndRaw struct {
  200. global *GlobalConfig
  201. data []byte
  202. pathErrors []error
  203. }
  204. // GetGlobalConfig returns the global dexpreopt.config that's created in the
  205. // make config phase. It is loaded once the first time it is called for any
  206. // ctx.Config(), and returns the same data for all future calls with the same
  207. // ctx.Config(). A value can be inserted for tests using
  208. // setDexpreoptTestGlobalConfig.
  209. func GetGlobalConfig(ctx android.PathContext) *GlobalConfig {
  210. return getGlobalConfigRaw(ctx).global
  211. }
  212. // GetGlobalConfigRawData is the same as GetGlobalConfig, except that it returns
  213. // the literal content of dexpreopt.config.
  214. func GetGlobalConfigRawData(ctx android.PathContext) []byte {
  215. return getGlobalConfigRaw(ctx).data
  216. }
  217. var globalConfigOnceKey = android.NewOnceKey("DexpreoptGlobalConfig")
  218. var testGlobalConfigOnceKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
  219. type pathContextErrorCollector struct {
  220. android.PathContext
  221. errors []error
  222. }
  223. func (p *pathContextErrorCollector) Errorf(format string, args ...interface{}) {
  224. p.errors = append(p.errors, fmt.Errorf(format, args...))
  225. }
  226. func getGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
  227. config := 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. pathErrorCollectorCtx := &pathContextErrorCollector{PathContext: ctx}
  232. globalConfig, err := ParseGlobalConfig(pathErrorCollectorCtx, data)
  233. if err != nil {
  234. panic(err)
  235. }
  236. return globalConfigAndRaw{globalConfig, data, pathErrorCollectorCtx.errors}
  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. DisablePreoptBootImages: true,
  244. DisableGenerateProfile: true,
  245. }, nil, nil}
  246. })
  247. }).(globalConfigAndRaw)
  248. // Avoid non-deterministic errors by reporting cached path errors on all callers.
  249. for _, err := range config.pathErrors {
  250. if ctx.Config().AllowMissingDependencies() {
  251. // When AllowMissingDependencies it set, report errors through AddMissingDependencies.
  252. // If AddMissingDependencies doesn't exist on the current context (for example when
  253. // called with a SingletonContext), just swallow the errors since there is no way to
  254. // report them.
  255. if missingDepsCtx, ok := ctx.(interface {
  256. AddMissingDependencies(missingDeps []string)
  257. }); ok {
  258. missingDepsCtx.AddMissingDependencies([]string{err.Error()})
  259. }
  260. } else {
  261. android.ReportPathErrorf(ctx, "%s", err)
  262. }
  263. }
  264. return config
  265. }
  266. // SetTestGlobalConfig sets a GlobalConfig that future calls to GetGlobalConfig
  267. // will return. It must be called before the first call to GetGlobalConfig for
  268. // the config.
  269. func SetTestGlobalConfig(config android.Config, globalConfig *GlobalConfig) {
  270. config.Once(testGlobalConfigOnceKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil, nil} })
  271. }
  272. // This struct is required to convert ModuleConfig from/to JSON.
  273. // The types of fields in ModuleConfig are not convertible,
  274. // so moduleJSONConfig has those fields as a convertible type.
  275. type moduleJSONConfig struct {
  276. *ModuleConfig
  277. BuildPath string
  278. DexPath string
  279. ManifestPath string
  280. ProfileClassListing string
  281. ProfileBootListing string
  282. EnforceUsesLibrariesStatusFile string
  283. ClassLoaderContexts jsonClassLoaderContextMap
  284. DexPreoptImagesDeps [][]string
  285. PreoptBootClassPathDexFiles []string
  286. }
  287. // ParseModuleConfig parses a per-module dexpreopt.config file into a
  288. // ModuleConfig struct. It is not used in Soong, which receives a ModuleConfig
  289. // struct directly from java/dexpreopt.go. It is used in dexpreopt_gen called
  290. // from Make to read the module dexpreopt.config written in the Make config
  291. // stage.
  292. func ParseModuleConfig(ctx android.PathContext, data []byte) (*ModuleConfig, error) {
  293. config := moduleJSONConfig{}
  294. err := json.Unmarshal(data, &config)
  295. if err != nil {
  296. return config.ModuleConfig, err
  297. }
  298. // Construct paths that require a PathContext.
  299. config.ModuleConfig.BuildPath = constructPath(ctx, config.BuildPath).(android.OutputPath)
  300. config.ModuleConfig.DexPath = constructPath(ctx, config.DexPath)
  301. config.ModuleConfig.ManifestPath = android.OptionalPathForPath(constructPath(ctx, config.ManifestPath))
  302. config.ModuleConfig.ProfileClassListing = android.OptionalPathForPath(constructPath(ctx, config.ProfileClassListing))
  303. config.ModuleConfig.EnforceUsesLibrariesStatusFile = constructPath(ctx, config.EnforceUsesLibrariesStatusFile)
  304. config.ModuleConfig.ClassLoaderContexts = fromJsonClassLoaderContext(ctx, config.ClassLoaderContexts)
  305. config.ModuleConfig.PreoptBootClassPathDexFiles = constructPaths(ctx, config.PreoptBootClassPathDexFiles)
  306. // This needs to exist, but dependencies are already handled in Make, so we don't need to pass them through JSON.
  307. config.ModuleConfig.DexPreoptImagesDeps = make([]android.OutputPaths, len(config.ModuleConfig.Archs))
  308. return config.ModuleConfig, nil
  309. }
  310. func pathsListToStringLists(pathsList []android.OutputPaths) [][]string {
  311. ret := make([][]string, 0, len(pathsList))
  312. for _, paths := range pathsList {
  313. ret = append(ret, paths.Strings())
  314. }
  315. return ret
  316. }
  317. func moduleConfigToJSON(config *ModuleConfig) ([]byte, error) {
  318. return json.MarshalIndent(&moduleJSONConfig{
  319. BuildPath: config.BuildPath.String(),
  320. DexPath: config.DexPath.String(),
  321. ManifestPath: config.ManifestPath.String(),
  322. ProfileClassListing: config.ProfileClassListing.String(),
  323. ProfileBootListing: config.ProfileBootListing.String(),
  324. EnforceUsesLibrariesStatusFile: config.EnforceUsesLibrariesStatusFile.String(),
  325. ClassLoaderContexts: toJsonClassLoaderContext(config.ClassLoaderContexts),
  326. DexPreoptImagesDeps: pathsListToStringLists(config.DexPreoptImagesDeps),
  327. PreoptBootClassPathDexFiles: config.PreoptBootClassPathDexFiles.Strings(),
  328. ModuleConfig: config,
  329. }, "", " ")
  330. }
  331. // WriteModuleConfig serializes a ModuleConfig into a per-module dexpreopt.config JSON file.
  332. // These config files are used for post-processing.
  333. func WriteModuleConfig(ctx android.ModuleContext, config *ModuleConfig, path android.WritablePath) {
  334. if path == nil {
  335. return
  336. }
  337. data, err := moduleConfigToJSON(config)
  338. if err != nil {
  339. ctx.ModuleErrorf("failed to JSON marshal module dexpreopt.config: %v", err)
  340. return
  341. }
  342. android.WriteFileRule(ctx, path, string(data))
  343. }
  344. // dex2oatModuleName returns the name of the module to use for the dex2oat host
  345. // tool. It should be a binary module with public visibility that is compiled
  346. // and installed for host.
  347. func dex2oatModuleName(config android.Config) string {
  348. // Default to the debug variant of dex2oat to help find bugs.
  349. // Set USE_DEX2OAT_DEBUG to false for only building non-debug versions.
  350. if config.Getenv("USE_DEX2OAT_DEBUG") == "false" {
  351. return "dex2oat"
  352. } else {
  353. return "dex2oatd"
  354. }
  355. }
  356. type dex2oatDependencyTag struct {
  357. blueprint.BaseDependencyTag
  358. android.LicenseAnnotationToolchainDependencyTag
  359. }
  360. func (d dex2oatDependencyTag) ExcludeFromVisibilityEnforcement() {
  361. }
  362. func (d dex2oatDependencyTag) ExcludeFromApexContents() {
  363. }
  364. func (d dex2oatDependencyTag) AllowDisabledModuleDependency(target android.Module) bool {
  365. // RegisterToolDeps may run after the prebuilt mutators and hence register a
  366. // dependency on the source module even when the prebuilt is to be used.
  367. // dex2oatPathFromDep takes that into account when it retrieves the path to
  368. // the binary, but we also need to disable the check for dependencies on
  369. // disabled modules.
  370. return target.IsReplacedByPrebuilt()
  371. }
  372. // Dex2oatDepTag represents the dependency onto the dex2oatd module. It is added to any module that
  373. // needs dexpreopting and so it makes no sense for it to be checked for visibility or included in
  374. // the apex.
  375. var Dex2oatDepTag = dex2oatDependencyTag{}
  376. var _ android.ExcludeFromVisibilityEnforcementTag = Dex2oatDepTag
  377. var _ android.ExcludeFromApexContentsTag = Dex2oatDepTag
  378. var _ android.AllowDisabledModuleDependency = Dex2oatDepTag
  379. // RegisterToolDeps adds the necessary dependencies to binary modules for tools
  380. // that are required later when Get(Cached)GlobalSoongConfig is called. It
  381. // should be called from a mutator that's registered with
  382. // android.RegistrationContext.FinalDepsMutators.
  383. func RegisterToolDeps(ctx android.BottomUpMutatorContext) {
  384. dex2oatBin := dex2oatModuleName(ctx.Config())
  385. v := ctx.Config().BuildOSTarget.Variations()
  386. ctx.AddFarVariationDependencies(v, Dex2oatDepTag, dex2oatBin)
  387. }
  388. func IsDex2oatNeeded(ctx android.PathContext) bool {
  389. global := GetGlobalConfig(ctx)
  390. return !global.DisablePreopt || !global.DisablePreoptBootImages
  391. }
  392. func dex2oatPathFromDep(ctx android.ModuleContext) android.Path {
  393. if !IsDex2oatNeeded(ctx) {
  394. return nil
  395. }
  396. dex2oatBin := dex2oatModuleName(ctx.Config())
  397. // Find the right dex2oat module, trying to follow PrebuiltDepTag from source
  398. // to prebuilt if there is one. We wouldn't have to do this if the
  399. // prebuilt_postdeps mutator that replaces source deps with prebuilt deps was
  400. // run after RegisterToolDeps above, but changing that leads to ordering
  401. // problems between mutators (RegisterToolDeps needs to run late to act on
  402. // final variants, while prebuilt_postdeps needs to run before many of the
  403. // PostDeps mutators, like the APEX mutators). Hence we need to dig out the
  404. // prebuilt explicitly here instead.
  405. var dex2oatModule android.Module
  406. ctx.WalkDeps(func(child, parent android.Module) bool {
  407. if parent == ctx.Module() && ctx.OtherModuleDependencyTag(child) == Dex2oatDepTag {
  408. // Found the source module, or prebuilt module that has replaced the source.
  409. dex2oatModule = child
  410. if android.IsModulePrebuilt(child) {
  411. return false // If it's the prebuilt we're done.
  412. } else {
  413. return true // Recurse to check if the source has a prebuilt dependency.
  414. }
  415. }
  416. if parent == dex2oatModule && ctx.OtherModuleDependencyTag(child) == android.PrebuiltDepTag {
  417. if p := android.GetEmbeddedPrebuilt(child); p != nil && p.UsePrebuilt() {
  418. dex2oatModule = child // Found a prebuilt that should be used.
  419. }
  420. }
  421. return false
  422. })
  423. if dex2oatModule == nil {
  424. // If this happens there's probably a missing call to AddToolDeps in DepsMutator.
  425. panic(fmt.Sprintf("Failed to lookup %s dependency", dex2oatBin))
  426. }
  427. dex2oatPath := dex2oatModule.(android.HostToolProvider).HostToolPath()
  428. if !dex2oatPath.Valid() {
  429. panic(fmt.Sprintf("Failed to find host tool path in %s", dex2oatModule))
  430. }
  431. return dex2oatPath.Path()
  432. }
  433. // createGlobalSoongConfig creates a GlobalSoongConfig from the current context.
  434. // Should not be used in dexpreopt_gen.
  435. func createGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
  436. return &GlobalSoongConfig{
  437. Profman: ctx.Config().HostToolPath(ctx, "profman"),
  438. Dex2oat: dex2oatPathFromDep(ctx),
  439. Aapt: ctx.Config().HostToolPath(ctx, "aapt2"),
  440. SoongZip: ctx.Config().HostToolPath(ctx, "soong_zip"),
  441. Zip2zip: ctx.Config().HostToolPath(ctx, "zip2zip"),
  442. ManifestCheck: ctx.Config().HostToolPath(ctx, "manifest_check"),
  443. ConstructContext: ctx.Config().HostToolPath(ctx, "construct_context"),
  444. }
  445. }
  446. // The main reason for this Once cache for GlobalSoongConfig is to make the
  447. // dex2oat path available to singletons. In ordinary modules we get it through a
  448. // Dex2oatDepTag dependency, but in singletons there's no simple way to do the
  449. // same thing and ensure the right variant is selected, hence this cache to make
  450. // the resolved path available to singletons. This means we depend on there
  451. // being at least one ordinary module with a Dex2oatDepTag dependency.
  452. //
  453. // TODO(b/147613152): Implement a way to deal with dependencies from singletons,
  454. // and then possibly remove this cache altogether.
  455. var globalSoongConfigOnceKey = android.NewOnceKey("DexpreoptGlobalSoongConfig")
  456. // GetGlobalSoongConfig creates a GlobalSoongConfig the first time it's called,
  457. // and later returns the same cached instance.
  458. func GetGlobalSoongConfig(ctx android.ModuleContext) *GlobalSoongConfig {
  459. globalSoong := ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
  460. return createGlobalSoongConfig(ctx)
  461. }).(*GlobalSoongConfig)
  462. // Always resolve the tool path from the dependency, to ensure that every
  463. // module has the dependency added properly.
  464. myDex2oat := dex2oatPathFromDep(ctx)
  465. if myDex2oat != globalSoong.Dex2oat {
  466. panic(fmt.Sprintf("Inconsistent dex2oat path in cached config: expected %s, got %s", globalSoong.Dex2oat, myDex2oat))
  467. }
  468. return globalSoong
  469. }
  470. // GetCachedGlobalSoongConfig returns a cached GlobalSoongConfig created by an
  471. // earlier GetGlobalSoongConfig call. This function works with any context
  472. // compatible with a basic PathContext, since it doesn't try to create a
  473. // GlobalSoongConfig with the proper paths (which requires a full
  474. // ModuleContext). If there has been no prior call to GetGlobalSoongConfig, nil
  475. // is returned.
  476. func GetCachedGlobalSoongConfig(ctx android.PathContext) *GlobalSoongConfig {
  477. return ctx.Config().Once(globalSoongConfigOnceKey, func() interface{} {
  478. return (*GlobalSoongConfig)(nil)
  479. }).(*GlobalSoongConfig)
  480. }
  481. type globalJsonSoongConfig struct {
  482. Profman string
  483. Dex2oat string
  484. Aapt string
  485. SoongZip string
  486. Zip2zip string
  487. ManifestCheck string
  488. ConstructContext string
  489. }
  490. // ParseGlobalSoongConfig parses the given data assumed to be read from the
  491. // global dexpreopt_soong.config file into a GlobalSoongConfig struct. It is
  492. // only used in dexpreopt_gen.
  493. func ParseGlobalSoongConfig(ctx android.PathContext, data []byte) (*GlobalSoongConfig, error) {
  494. var jc globalJsonSoongConfig
  495. err := json.Unmarshal(data, &jc)
  496. if err != nil {
  497. return &GlobalSoongConfig{}, err
  498. }
  499. config := &GlobalSoongConfig{
  500. Profman: constructPath(ctx, jc.Profman),
  501. Dex2oat: constructPath(ctx, jc.Dex2oat),
  502. Aapt: constructPath(ctx, jc.Aapt),
  503. SoongZip: constructPath(ctx, jc.SoongZip),
  504. Zip2zip: constructPath(ctx, jc.Zip2zip),
  505. ManifestCheck: constructPath(ctx, jc.ManifestCheck),
  506. ConstructContext: constructPath(ctx, jc.ConstructContext),
  507. }
  508. return config, nil
  509. }
  510. // checkBootJarsConfigConsistency checks the consistency of BootJars and ApexBootJars fields in
  511. // DexpreoptGlobalConfig and Config.productVariables.
  512. func checkBootJarsConfigConsistency(ctx android.SingletonContext, dexpreoptConfig *GlobalConfig, config android.Config) {
  513. compareBootJars := func(property string, dexpreoptJars, variableJars android.ConfiguredJarList) {
  514. dexpreoptPairs := dexpreoptJars.CopyOfApexJarPairs()
  515. variablePairs := variableJars.CopyOfApexJarPairs()
  516. if !reflect.DeepEqual(dexpreoptPairs, variablePairs) {
  517. ctx.Errorf("Inconsistent configuration of %[1]s\n"+
  518. " dexpreopt.GlobalConfig.%[1]s = %[2]s\n"+
  519. " productVariables.%[1]s = %[3]s",
  520. property, dexpreoptPairs, variablePairs)
  521. }
  522. }
  523. compareBootJars("BootJars", dexpreoptConfig.BootJars, config.NonApexBootJars())
  524. compareBootJars("ApexBootJars", dexpreoptConfig.ApexBootJars, config.ApexBootJars())
  525. }
  526. func (s *globalSoongConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
  527. checkBootJarsConfigConsistency(ctx, GetGlobalConfig(ctx), ctx.Config())
  528. if GetGlobalConfig(ctx).DisablePreopt {
  529. return
  530. }
  531. config := GetCachedGlobalSoongConfig(ctx)
  532. if config == nil {
  533. // No module has enabled dexpreopting, so we assume there will be no calls
  534. // to dexpreopt_gen.
  535. return
  536. }
  537. jc := globalJsonSoongConfig{
  538. Profman: config.Profman.String(),
  539. Dex2oat: config.Dex2oat.String(),
  540. Aapt: config.Aapt.String(),
  541. SoongZip: config.SoongZip.String(),
  542. Zip2zip: config.Zip2zip.String(),
  543. ManifestCheck: config.ManifestCheck.String(),
  544. ConstructContext: config.ConstructContext.String(),
  545. }
  546. data, err := json.Marshal(jc)
  547. if err != nil {
  548. ctx.Errorf("failed to JSON marshal GlobalSoongConfig: %v", err)
  549. return
  550. }
  551. android.WriteFileRule(ctx, android.PathForOutput(ctx, "dexpreopt_soong.config"), string(data))
  552. }
  553. func (s *globalSoongConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
  554. if GetGlobalConfig(ctx).DisablePreopt {
  555. return
  556. }
  557. config := GetCachedGlobalSoongConfig(ctx)
  558. if config == nil {
  559. return
  560. }
  561. ctx.Strict("DEX2OAT", config.Dex2oat.String())
  562. ctx.Strict("DEXPREOPT_GEN_DEPS", strings.Join([]string{
  563. config.Profman.String(),
  564. config.Dex2oat.String(),
  565. config.Aapt.String(),
  566. config.SoongZip.String(),
  567. config.Zip2zip.String(),
  568. config.ManifestCheck.String(),
  569. config.ConstructContext.String(),
  570. }, " "))
  571. }
  572. func GlobalConfigForTests(ctx android.PathContext) *GlobalConfig {
  573. return &GlobalConfig{
  574. DisablePreopt: false,
  575. DisablePreoptModules: nil,
  576. OnlyPreoptBootImageAndSystemServer: false,
  577. HasSystemOther: false,
  578. PatternsOnSystemOther: nil,
  579. DisableGenerateProfile: false,
  580. ProfileDir: "",
  581. BootJars: android.EmptyConfiguredJarList(),
  582. ApexBootJars: android.EmptyConfiguredJarList(),
  583. ArtApexJars: android.EmptyConfiguredJarList(),
  584. SystemServerJars: android.EmptyConfiguredJarList(),
  585. SystemServerApps: nil,
  586. ApexSystemServerJars: android.EmptyConfiguredJarList(),
  587. StandaloneSystemServerJars: android.EmptyConfiguredJarList(),
  588. ApexStandaloneSystemServerJars: android.EmptyConfiguredJarList(),
  589. SpeedApps: nil,
  590. PreoptFlags: nil,
  591. DefaultCompilerFilter: "",
  592. SystemServerCompilerFilter: "",
  593. GenerateDMFiles: false,
  594. NoDebugInfo: false,
  595. DontResolveStartupStrings: false,
  596. AlwaysSystemServerDebugInfo: false,
  597. NeverSystemServerDebugInfo: false,
  598. AlwaysOtherDebugInfo: false,
  599. NeverOtherDebugInfo: false,
  600. IsEng: false,
  601. SanitizeLite: false,
  602. DefaultAppImages: false,
  603. Dex2oatXmx: "",
  604. Dex2oatXms: "",
  605. EmptyDirectory: "empty_dir",
  606. CpuVariant: nil,
  607. InstructionSetFeatures: nil,
  608. BootImageProfiles: nil,
  609. BootFlags: "",
  610. Dex2oatImageXmx: "",
  611. Dex2oatImageXms: "",
  612. }
  613. }
  614. func globalSoongConfigForTests() *GlobalSoongConfig {
  615. return &GlobalSoongConfig{
  616. Profman: android.PathForTesting("profman"),
  617. Dex2oat: android.PathForTesting("dex2oat"),
  618. Aapt: android.PathForTesting("aapt2"),
  619. SoongZip: android.PathForTesting("soong_zip"),
  620. Zip2zip: android.PathForTesting("zip2zip"),
  621. ManifestCheck: android.PathForTesting("manifest_check"),
  622. ConstructContext: android.PathForTesting("construct_context"),
  623. }
  624. }