blob: 9d0f539ba628f7fa70aab16dce87daed38eb3a05 [file] [log] [blame]
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +00001// 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
15package java
16
17import (
18 "strings"
19
Spandan Das950deca2024-10-01 18:35:23 +000020 "github.com/google/blueprint"
21
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +000022 "android/soong/android"
23 "android/soong/dexpreopt"
24
25 "github.com/google/blueprint/pathtools"
26)
27
28func init() {
29 RegisterDexpreoptCheckBuildComponents(android.InitRegistrationContext)
30}
31
32func RegisterDexpreoptCheckBuildComponents(ctx android.RegistrationContext) {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000033 ctx.RegisterParallelSingletonModuleType("dexpreopt_systemserver_check", dexpreoptSystemserverCheckFactory)
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +000034}
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.
45type dexpreoptSystemserverCheck struct {
46 android.SingletonModuleBase
47
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +000048 // The install paths to the compilation artifacts.
49 artifacts []string
50}
51
52func dexpreoptSystemserverCheckFactory() android.SingletonModule {
53 m := &dexpreoptSystemserverCheck{}
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +000054 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
55 return m
56}
57
58func getInstallPath(ctx android.ModuleContext, location string) android.InstallPath {
59 return android.PathForModuleInPartitionInstall(
Colin Crossc68db4b2021-11-11 18:59:15 -080060 ctx, "", strings.TrimPrefix(location, "/"))
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +000061}
62
Spandan Das950deca2024-10-01 18:35:23 +000063type systemServerDependencyTag struct {
64 blueprint.BaseDependencyTag
65}
66
67// systemServerJarDepTag willl be used for validation. Skip visiblility.
68func (b systemServerDependencyTag) ExcludeFromVisibilityEnforcement() {
69}
70
71var (
72 // dep tag for platform and apex system server jars
73 systemServerJarDepTag = systemServerDependencyTag{}
74)
75
76var _ 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.
81func (m *dexpreoptSystemserverCheck) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +000082 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 Zhang23984422023-11-09 16:47:04 +000087 if global.DisablePreopt || global.OnlyPreoptArtBootImage || len(targets) == 0 || ctx.Config().UnbundledBuild() {
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +000088 return
89 }
90
Spandan Das950deca2024-10-01 18:35:23 +000091 ctx.AddDependency(ctx.Module(), systemServerJarDepTag, global.AllSystemServerJars(ctx).CopyOfJars()...)
92}
93
94func (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 Stefanifaaee922025-03-07 17:41:33 +0100103 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 Das950deca2024-10-01 18:35:23 +0000109 odexLocation := dexpreopt.ToOdexPath(dexLocation, targets[0].Arch.ArchType, partition)
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +0000110 odexPath := getInstallPath(ctx, odexLocation)
111 vdexPath := getInstallPath(ctx, pathtools.ReplaceExtension(odexLocation, "vdex"))
Spandan Das950deca2024-10-01 18:35:23 +0000112 m.artifacts = append(m.artifacts, odexPath.String(), vdexPath.String())
113 })
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +0000114}
115
116func (m *dexpreoptSystemserverCheck) GenerateSingletonBuildActions(ctx android.SingletonContext) {
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +0000117}
118
119func (m *dexpreoptSystemserverCheck) MakeVars(ctx android.MakeVarsContext) {
120 ctx.Strict("DEXPREOPT_SYSTEMSERVER_ARTIFACTS", strings.Join(m.artifacts, " "))
121}