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
15package java
16
17import (
18	"android/soong/android"
19	"android/soong/dexpreopt"
20
21	"github.com/google/blueprint"
22)
23
24func init() {
25	registerSystemserverClasspathBuildComponents(android.InitRegistrationContext)
26}
27
28func registerSystemserverClasspathBuildComponents(ctx android.RegistrationContext) {
29	ctx.RegisterModuleType("platform_systemserverclasspath", platformSystemServerClasspathFactory)
30	ctx.RegisterModuleType("systemserverclasspath_fragment", systemServerClasspathFactory)
31}
32
33type platformSystemServerClasspathModule struct {
34	android.ModuleBase
35
36	ClasspathFragmentBase
37}
38
39func platformSystemServerClasspathFactory() android.Module {
40	m := &platformSystemServerClasspathModule{}
41	initClasspathFragment(m, SYSTEMSERVERCLASSPATH)
42	android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
43	return m
44}
45
46func (p *platformSystemServerClasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) {
47	return p.classpathFragmentBase().androidMkEntries()
48}
49
50func (p *platformSystemServerClasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
51	classpathJars := configuredJarListToClasspathJars(ctx, p.ClasspathFragmentToConfiguredJarList(ctx), p.classpathType)
52	p.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, classpathJars)
53}
54
55func (p *platformSystemServerClasspathModule) ClasspathFragmentToConfiguredJarList(ctx android.ModuleContext) android.ConfiguredJarList {
56	global := dexpreopt.GetGlobalConfig(ctx)
57	return global.SystemServerJars
58}
59
60type SystemServerClasspathModule struct {
61	android.ModuleBase
62	android.ApexModuleBase
63
64	ClasspathFragmentBase
65
66	properties systemServerClasspathFragmentProperties
67}
68
69func (s *SystemServerClasspathModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
70	return nil
71}
72
73type systemServerClasspathFragmentProperties struct {
74	// The contents of this systemserverclasspath_fragment, could be either java_library, or java_sdk_library.
75	//
76	// The order of this list matters as it is the order that is used in the SYSTEMSERVERCLASSPATH.
77	Contents []string
78}
79
80func systemServerClasspathFactory() android.Module {
81	m := &SystemServerClasspathModule{}
82	m.AddProperties(&m.properties)
83	android.InitApexModule(m)
84	initClasspathFragment(m, SYSTEMSERVERCLASSPATH)
85	android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
86	return m
87}
88
89func (s *SystemServerClasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
90	if len(s.properties.Contents) == 0 {
91		ctx.PropertyErrorf("contents", "empty contents are not allowed")
92	}
93
94	classpathJars := configuredJarListToClasspathJars(ctx, s.ClasspathFragmentToConfiguredJarList(ctx), s.classpathType)
95	s.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, classpathJars)
96}
97
98func (s *SystemServerClasspathModule) ClasspathFragmentToConfiguredJarList(ctx android.ModuleContext) android.ConfiguredJarList {
99	global := dexpreopt.GetGlobalConfig(ctx)
100
101	possibleUpdatableModules := gatherPossibleUpdatableModuleNamesAndStems(ctx, s.properties.Contents, systemServerClasspathFragmentContentDepTag)
102
103	// Only create configs for updatable boot jars. Non-updatable system server jars must be part of the
104	// platform_systemserverclasspath's classpath proto config to guarantee that they come before any
105	// updatable jars at runtime.
106	return global.UpdatableSystemServerJars.Filter(possibleUpdatableModules)
107}
108
109type systemServerClasspathFragmentContentDependencyTag struct {
110	blueprint.BaseDependencyTag
111}
112
113// The systemserverclasspath_fragment contents must never depend on prebuilts.
114func (systemServerClasspathFragmentContentDependencyTag) ReplaceSourceWithPrebuilt() bool {
115	return false
116}
117
118// Contents of system server fragments in an apex are considered to be directly in the apex, as if
119// they were listed in java_libs.
120func (systemServerClasspathFragmentContentDependencyTag) CopyDirectlyInAnyApex() {}
121
122var _ android.CopyDirectlyInAnyApexTag = systemServerClasspathFragmentContentDepTag
123
124// The tag used for the dependency between the systemserverclasspath_fragment module and its contents.
125var systemServerClasspathFragmentContentDepTag = systemServerClasspathFragmentContentDependencyTag{}
126
127func IsSystemServerClasspathFragmentContentDepTag(tag blueprint.DependencyTag) bool {
128	return tag == systemServerClasspathFragmentContentDepTag
129}
130
131func (s *SystemServerClasspathModule) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
132	module := ctx.Module()
133
134	for _, name := range s.properties.Contents {
135		ctx.AddDependency(module, systemServerClasspathFragmentContentDepTag, name)
136	}
137}
138