xref: /aosp_15_r20/build/soong/java/dexpreopt_check.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2021 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage java
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"strings"
19*333d2b36SAndroid Build Coastguard Worker
20*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
21*333d2b36SAndroid Build Coastguard Worker
22*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
23*333d2b36SAndroid Build Coastguard Worker	"android/soong/dexpreopt"
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/pathtools"
26*333d2b36SAndroid Build Coastguard Worker)
27*333d2b36SAndroid Build Coastguard Worker
28*333d2b36SAndroid Build Coastguard Workerfunc init() {
29*333d2b36SAndroid Build Coastguard Worker	RegisterDexpreoptCheckBuildComponents(android.InitRegistrationContext)
30*333d2b36SAndroid Build Coastguard Worker}
31*333d2b36SAndroid Build Coastguard Worker
32*333d2b36SAndroid Build Coastguard Workerfunc RegisterDexpreoptCheckBuildComponents(ctx android.RegistrationContext) {
33*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterParallelSingletonModuleType("dexpreopt_systemserver_check", dexpreoptSystemserverCheckFactory)
34*333d2b36SAndroid Build Coastguard Worker}
35*333d2b36SAndroid Build Coastguard Worker
36*333d2b36SAndroid Build Coastguard Worker// A build-time check to verify if all compilation artifacts of system server jars are installed
37*333d2b36SAndroid Build Coastguard Worker// into the system image. When the check fails, it means that dexpreopting is not working for some
38*333d2b36SAndroid Build Coastguard Worker// system server jars and needs to be fixed.
39*333d2b36SAndroid Build Coastguard Worker// This singleton module generates a list of the paths to the artifacts based on
40*333d2b36SAndroid Build Coastguard Worker// PRODUCT_SYSTEM_SERVER_JARS and PRODUCT_APEX_SYSTEM_SERVER_JARS, and passes it to Make via a
41*333d2b36SAndroid Build Coastguard Worker// variable. Make will then do the actual check.
42*333d2b36SAndroid Build Coastguard Worker// Currently, it only checks artifacts of modules defined in Soong. Artifacts of modules defined in
43*333d2b36SAndroid Build Coastguard Worker// Makefile are generated by a script generated by dexpreopt_gen, and their existence is unknown to
44*333d2b36SAndroid Build Coastguard Worker// Make and Ninja.
45*333d2b36SAndroid Build Coastguard Workertype dexpreoptSystemserverCheck struct {
46*333d2b36SAndroid Build Coastguard Worker	android.SingletonModuleBase
47*333d2b36SAndroid Build Coastguard Worker
48*333d2b36SAndroid Build Coastguard Worker	// The install paths to the compilation artifacts.
49*333d2b36SAndroid Build Coastguard Worker	artifacts []string
50*333d2b36SAndroid Build Coastguard Worker}
51*333d2b36SAndroid Build Coastguard Worker
52*333d2b36SAndroid Build Coastguard Workerfunc dexpreoptSystemserverCheckFactory() android.SingletonModule {
53*333d2b36SAndroid Build Coastguard Worker	m := &dexpreoptSystemserverCheck{}
54*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
55*333d2b36SAndroid Build Coastguard Worker	return m
56*333d2b36SAndroid Build Coastguard Worker}
57*333d2b36SAndroid Build Coastguard Worker
58*333d2b36SAndroid Build Coastguard Workerfunc getInstallPath(ctx android.ModuleContext, location string) android.InstallPath {
59*333d2b36SAndroid Build Coastguard Worker	return android.PathForModuleInPartitionInstall(
60*333d2b36SAndroid Build Coastguard Worker		ctx, "", strings.TrimPrefix(location, "/"))
61*333d2b36SAndroid Build Coastguard Worker}
62*333d2b36SAndroid Build Coastguard Worker
63*333d2b36SAndroid Build Coastguard Workertype systemServerDependencyTag struct {
64*333d2b36SAndroid Build Coastguard Worker	blueprint.BaseDependencyTag
65*333d2b36SAndroid Build Coastguard Worker}
66*333d2b36SAndroid Build Coastguard Worker
67*333d2b36SAndroid Build Coastguard Worker// systemServerJarDepTag willl be used for validation. Skip visiblility.
68*333d2b36SAndroid Build Coastguard Workerfunc (b systemServerDependencyTag) ExcludeFromVisibilityEnforcement() {
69*333d2b36SAndroid Build Coastguard Worker}
70*333d2b36SAndroid Build Coastguard Worker
71*333d2b36SAndroid Build Coastguard Workervar (
72*333d2b36SAndroid Build Coastguard Worker	// dep tag for platform and apex system server jars
73*333d2b36SAndroid Build Coastguard Worker	systemServerJarDepTag = systemServerDependencyTag{}
74*333d2b36SAndroid Build Coastguard Worker)
75*333d2b36SAndroid Build Coastguard Worker
76*333d2b36SAndroid Build Coastguard Workervar _ android.ExcludeFromVisibilityEnforcementTag = systemServerJarDepTag
77*333d2b36SAndroid Build Coastguard Worker
78*333d2b36SAndroid Build Coastguard Worker// Add a depenendency on the system server jars. The dexpreopt files of those will be emitted to make.
79*333d2b36SAndroid Build Coastguard Worker// The kati packaging system will verify that those files appear in installed files.
80*333d2b36SAndroid Build Coastguard Worker// Adding the dependency allows the singleton module to determine whether an apex system server jar is system_ext specific.
81*333d2b36SAndroid Build Coastguard Workerfunc (m *dexpreoptSystemserverCheck) DepsMutator(ctx android.BottomUpMutatorContext) {
82*333d2b36SAndroid Build Coastguard Worker	global := dexpreopt.GetGlobalConfig(ctx)
83*333d2b36SAndroid Build Coastguard Worker	targets := ctx.Config().Targets[android.Android]
84*333d2b36SAndroid Build Coastguard Worker
85*333d2b36SAndroid Build Coastguard Worker	// The check should be skipped on unbundled builds because system server jars are not preopted on
86*333d2b36SAndroid Build Coastguard Worker	// unbundled builds since the artifacts are installed into the system image, not the APEXes.
87*333d2b36SAndroid Build Coastguard Worker	if global.DisablePreopt || global.OnlyPreoptArtBootImage || len(targets) == 0 || ctx.Config().UnbundledBuild() {
88*333d2b36SAndroid Build Coastguard Worker		return
89*333d2b36SAndroid Build Coastguard Worker	}
90*333d2b36SAndroid Build Coastguard Worker
91*333d2b36SAndroid Build Coastguard Worker	ctx.AddDependency(ctx.Module(), systemServerJarDepTag, global.AllSystemServerJars(ctx).CopyOfJars()...)
92*333d2b36SAndroid Build Coastguard Worker}
93*333d2b36SAndroid Build Coastguard Worker
94*333d2b36SAndroid Build Coastguard Workerfunc (m *dexpreoptSystemserverCheck) GenerateAndroidBuildActions(ctx android.ModuleContext) {
95*333d2b36SAndroid Build Coastguard Worker	global := dexpreopt.GetGlobalConfig(ctx)
96*333d2b36SAndroid Build Coastguard Worker	targets := ctx.Config().Targets[android.Android]
97*333d2b36SAndroid Build Coastguard Worker
98*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDepsWithTag(systemServerJarDepTag, func(systemServerJar android.Module) {
99*333d2b36SAndroid Build Coastguard Worker		partition := "system"
100*333d2b36SAndroid Build Coastguard Worker		if systemServerJar.InstallInSystemExt() && ctx.Config().InstallApexSystemServerDexpreoptSamePartition() {
101*333d2b36SAndroid Build Coastguard Worker			partition = ctx.DeviceConfig().SystemExtPath() // system_ext
102*333d2b36SAndroid Build Coastguard Worker		}
103*333d2b36SAndroid Build Coastguard Worker		dexLocation := dexpreopt.GetSystemServerDexLocation(ctx, global, systemServerJar.Name())
104*333d2b36SAndroid Build Coastguard Worker		odexLocation := dexpreopt.ToOdexPath(dexLocation, targets[0].Arch.ArchType, partition)
105*333d2b36SAndroid Build Coastguard Worker		odexPath := getInstallPath(ctx, odexLocation)
106*333d2b36SAndroid Build Coastguard Worker		vdexPath := getInstallPath(ctx, pathtools.ReplaceExtension(odexLocation, "vdex"))
107*333d2b36SAndroid Build Coastguard Worker		m.artifacts = append(m.artifacts, odexPath.String(), vdexPath.String())
108*333d2b36SAndroid Build Coastguard Worker	})
109*333d2b36SAndroid Build Coastguard Worker}
110*333d2b36SAndroid Build Coastguard Worker
111*333d2b36SAndroid Build Coastguard Workerfunc (m *dexpreoptSystemserverCheck) GenerateSingletonBuildActions(ctx android.SingletonContext) {
112*333d2b36SAndroid Build Coastguard Worker}
113*333d2b36SAndroid Build Coastguard Worker
114*333d2b36SAndroid Build Coastguard Workerfunc (m *dexpreoptSystemserverCheck) MakeVars(ctx android.MakeVarsContext) {
115*333d2b36SAndroid Build Coastguard Worker	ctx.Strict("DEXPREOPT_SYSTEMSERVER_ARTIFACTS", strings.Join(m.artifacts, " "))
116*333d2b36SAndroid Build Coastguard Worker}
117