dexpreopt_check.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2021 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 java
  15. import (
  16. "strings"
  17. "android/soong/android"
  18. "android/soong/dexpreopt"
  19. "github.com/google/blueprint/pathtools"
  20. )
  21. func init() {
  22. RegisterDexpreoptCheckBuildComponents(android.InitRegistrationContext)
  23. }
  24. func RegisterDexpreoptCheckBuildComponents(ctx android.RegistrationContext) {
  25. ctx.RegisterParallelSingletonModuleType("dexpreopt_systemserver_check", dexpreoptSystemserverCheckFactory)
  26. }
  27. // A build-time check to verify if all compilation artifacts of system server jars are installed
  28. // into the system image. When the check fails, it means that dexpreopting is not working for some
  29. // system server jars and needs to be fixed.
  30. // This singleton module generates a list of the paths to the artifacts based on
  31. // PRODUCT_SYSTEM_SERVER_JARS and PRODUCT_APEX_SYSTEM_SERVER_JARS, and passes it to Make via a
  32. // variable. Make will then do the actual check.
  33. // Currently, it only checks artifacts of modules defined in Soong. Artifacts of modules defined in
  34. // Makefile are generated by a script generated by dexpreopt_gen, and their existence is unknown to
  35. // Make and Ninja.
  36. type dexpreoptSystemserverCheck struct {
  37. android.SingletonModuleBase
  38. // Mapping from the module name to the install paths to the compilation artifacts.
  39. artifactsByModuleName map[string][]string
  40. // The install paths to the compilation artifacts.
  41. artifacts []string
  42. }
  43. func dexpreoptSystemserverCheckFactory() android.SingletonModule {
  44. m := &dexpreoptSystemserverCheck{}
  45. m.artifactsByModuleName = make(map[string][]string)
  46. android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
  47. return m
  48. }
  49. func getInstallPath(ctx android.ModuleContext, location string) android.InstallPath {
  50. return android.PathForModuleInPartitionInstall(
  51. ctx, "", strings.TrimPrefix(location, "/"))
  52. }
  53. func (m *dexpreoptSystemserverCheck) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  54. global := dexpreopt.GetGlobalConfig(ctx)
  55. targets := ctx.Config().Targets[android.Android]
  56. // The check should be skipped on unbundled builds because system server jars are not preopted on
  57. // unbundled builds since the artifacts are installed into the system image, not the APEXes.
  58. if global.DisablePreopt || len(targets) == 0 || ctx.Config().UnbundledBuild() {
  59. return
  60. }
  61. systemServerJars := global.AllSystemServerJars(ctx)
  62. for _, jar := range systemServerJars.CopyOfJars() {
  63. dexLocation := dexpreopt.GetSystemServerDexLocation(ctx, global, jar)
  64. odexLocation := dexpreopt.ToOdexPath(dexLocation, targets[0].Arch.ArchType)
  65. odexPath := getInstallPath(ctx, odexLocation)
  66. vdexPath := getInstallPath(ctx, pathtools.ReplaceExtension(odexLocation, "vdex"))
  67. m.artifactsByModuleName[jar] = []string{odexPath.String(), vdexPath.String()}
  68. }
  69. }
  70. func (m *dexpreoptSystemserverCheck) GenerateSingletonBuildActions(ctx android.SingletonContext) {
  71. // Only keep modules defined in Soong.
  72. ctx.VisitAllModules(func(module android.Module) {
  73. if artifacts, ok := m.artifactsByModuleName[module.Name()]; ok {
  74. m.artifacts = append(m.artifacts, artifacts...)
  75. }
  76. })
  77. }
  78. func (m *dexpreoptSystemserverCheck) MakeVars(ctx android.MakeVarsContext) {
  79. ctx.Strict("DEXPREOPT_SYSTEMSERVER_ARTIFACTS", strings.Join(m.artifacts, " "))
  80. }