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