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 | |
Spandan Das | 950deca | 2024-10-01 18:35:23 +0000 | [diff] [blame] | 20 | "github.com/google/blueprint" |
| 21 | |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 22 | "android/soong/android" |
| 23 | "android/soong/dexpreopt" |
| 24 | |
| 25 | "github.com/google/blueprint/pathtools" |
| 26 | ) |
| 27 | |
| 28 | func init() { |
| 29 | RegisterDexpreoptCheckBuildComponents(android.InitRegistrationContext) |
| 30 | } |
| 31 | |
| 32 | func RegisterDexpreoptCheckBuildComponents(ctx android.RegistrationContext) { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 33 | ctx.RegisterParallelSingletonModuleType("dexpreopt_systemserver_check", dexpreoptSystemserverCheckFactory) |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // A build-time check to verify if all compilation artifacts of system server jars are installed |
| 37 | // into the system image. When the check fails, it means that dexpreopting is not working for some |
| 38 | // system server jars and needs to be fixed. |
| 39 | // This singleton module generates a list of the paths to the artifacts based on |
| 40 | // PRODUCT_SYSTEM_SERVER_JARS and PRODUCT_APEX_SYSTEM_SERVER_JARS, and passes it to Make via a |
| 41 | // variable. Make will then do the actual check. |
| 42 | // Currently, it only checks artifacts of modules defined in Soong. Artifacts of modules defined in |
| 43 | // Makefile are generated by a script generated by dexpreopt_gen, and their existence is unknown to |
| 44 | // Make and Ninja. |
| 45 | type dexpreoptSystemserverCheck struct { |
| 46 | android.SingletonModuleBase |
| 47 | |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 48 | // The install paths to the compilation artifacts. |
| 49 | artifacts []string |
| 50 | } |
| 51 | |
| 52 | func dexpreoptSystemserverCheckFactory() android.SingletonModule { |
| 53 | m := &dexpreoptSystemserverCheck{} |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 54 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 55 | return m |
| 56 | } |
| 57 | |
| 58 | func getInstallPath(ctx android.ModuleContext, location string) android.InstallPath { |
| 59 | return android.PathForModuleInPartitionInstall( |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 60 | ctx, "", strings.TrimPrefix(location, "/")) |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Spandan Das | 950deca | 2024-10-01 18:35:23 +0000 | [diff] [blame] | 63 | type systemServerDependencyTag struct { |
| 64 | blueprint.BaseDependencyTag |
| 65 | } |
| 66 | |
| 67 | // systemServerJarDepTag willl be used for validation. Skip visiblility. |
| 68 | func (b systemServerDependencyTag) ExcludeFromVisibilityEnforcement() { |
| 69 | } |
| 70 | |
| 71 | var ( |
| 72 | // dep tag for platform and apex system server jars |
| 73 | systemServerJarDepTag = systemServerDependencyTag{} |
| 74 | ) |
| 75 | |
| 76 | var _ android.ExcludeFromVisibilityEnforcementTag = systemServerJarDepTag |
| 77 | |
| 78 | // Add a depenendency on the system server jars. The dexpreopt files of those will be emitted to make. |
| 79 | // The kati packaging system will verify that those files appear in installed files. |
| 80 | // Adding the dependency allows the singleton module to determine whether an apex system server jar is system_ext specific. |
| 81 | func (m *dexpreoptSystemserverCheck) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 82 | global := dexpreopt.GetGlobalConfig(ctx) |
| 83 | targets := ctx.Config().Targets[android.Android] |
| 84 | |
| 85 | // The check should be skipped on unbundled builds because system server jars are not preopted on |
| 86 | // unbundled builds since the artifacts are installed into the system image, not the APEXes. |
Jiakai Zhang | 2398442 | 2023-11-09 16:47:04 +0000 | [diff] [blame] | 87 | if global.DisablePreopt || global.OnlyPreoptArtBootImage || len(targets) == 0 || ctx.Config().UnbundledBuild() { |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 88 | return |
| 89 | } |
| 90 | |
Spandan Das | 950deca | 2024-10-01 18:35:23 +0000 | [diff] [blame] | 91 | ctx.AddDependency(ctx.Module(), systemServerJarDepTag, global.AllSystemServerJars(ctx).CopyOfJars()...) |
| 92 | } |
| 93 | |
| 94 | func (m *dexpreoptSystemserverCheck) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 95 | global := dexpreopt.GetGlobalConfig(ctx) |
| 96 | targets := ctx.Config().Targets[android.Android] |
| 97 | |
| 98 | ctx.VisitDirectDepsWithTag(systemServerJarDepTag, func(systemServerJar android.Module) { |
| 99 | partition := "system" |
| 100 | if systemServerJar.InstallInSystemExt() && ctx.Config().InstallApexSystemServerDexpreoptSamePartition() { |
| 101 | partition = ctx.DeviceConfig().SystemExtPath() // system_ext |
| 102 | } |
Luca Stefani | faaee92 | 2025-03-07 17:41:33 +0100 | [diff] [blame^] | 103 | var dexLocation string |
| 104 | if m, ok := systemServerJar.(ModuleWithStem); ok { |
| 105 | dexLocation = dexpreopt.GetSystemServerDexLocation(ctx, global, m.Stem()) |
| 106 | } else { |
| 107 | ctx.PropertyErrorf("dexpreopt_systemserver_check", "%v is not a ModuleWithStem", systemServerJar.Name()) |
| 108 | } |
Spandan Das | 950deca | 2024-10-01 18:35:23 +0000 | [diff] [blame] | 109 | odexLocation := dexpreopt.ToOdexPath(dexLocation, targets[0].Arch.ArchType, partition) |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 110 | odexPath := getInstallPath(ctx, odexLocation) |
| 111 | vdexPath := getInstallPath(ctx, pathtools.ReplaceExtension(odexLocation, "vdex")) |
Spandan Das | 950deca | 2024-10-01 18:35:23 +0000 | [diff] [blame] | 112 | m.artifacts = append(m.artifacts, odexPath.String(), vdexPath.String()) |
| 113 | }) |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | func (m *dexpreoptSystemserverCheck) GenerateSingletonBuildActions(ctx android.SingletonContext) { |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | func (m *dexpreoptSystemserverCheck) MakeVars(ctx android.MakeVarsContext) { |
| 120 | ctx.Strict("DEXPREOPT_SYSTEMSERVER_ARTIFACTS", strings.Join(m.artifacts, " ")) |
| 121 | } |