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 android.RegisterSdkMemberType(&systemServerClasspathFragmentMemberType{ 28 SdkMemberTypeBase: android.SdkMemberTypeBase{ 29 PropertyName: "systemserverclasspath_fragments", 30 SupportsSdk: true, 31 32 // This was only added in Tiramisu. 33 SupportedBuildReleaseSpecification: "Tiramisu+", 34 }, 35 }) 36} 37 38func registerSystemserverClasspathBuildComponents(ctx android.RegistrationContext) { 39 ctx.RegisterModuleType("platform_systemserverclasspath", platformSystemServerClasspathFactory) 40 ctx.RegisterModuleType("systemserverclasspath_fragment", systemServerClasspathFactory) 41 ctx.RegisterModuleType("prebuilt_systemserverclasspath_fragment", prebuiltSystemServerClasspathModuleFactory) 42} 43 44type platformSystemServerClasspathModule struct { 45 android.ModuleBase 46 47 ClasspathFragmentBase 48} 49 50func platformSystemServerClasspathFactory() android.Module { 51 m := &platformSystemServerClasspathModule{} 52 initClasspathFragment(m, SYSTEMSERVERCLASSPATH) 53 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) 54 return m 55} 56 57func (p *platformSystemServerClasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) { 58 return p.classpathFragmentBase().androidMkEntries() 59} 60 61func (p *platformSystemServerClasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { 62 configuredJars := p.configuredJars(ctx) 63 classpathJars := configuredJarListToClasspathJars(ctx, configuredJars, p.classpathType) 64 standaloneConfiguredJars := p.standaloneConfiguredJars(ctx) 65 standaloneClasspathJars := configuredJarListToClasspathJars(ctx, standaloneConfiguredJars, STANDALONE_SYSTEMSERVER_JARS) 66 configuredJars = configuredJars.AppendList(&standaloneConfiguredJars) 67 classpathJars = append(classpathJars, standaloneClasspathJars...) 68 p.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars) 69} 70 71func (p *platformSystemServerClasspathModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList { 72 // TODO(satayev): include any apex jars that don't populate their classpath proto config. 73 return dexpreopt.GetGlobalConfig(ctx).SystemServerJars 74} 75 76func (p *platformSystemServerClasspathModule) standaloneConfiguredJars(ctx android.ModuleContext) android.ConfiguredJarList { 77 return dexpreopt.GetGlobalConfig(ctx).StandaloneSystemServerJars 78} 79 80type SystemServerClasspathModule struct { 81 android.ModuleBase 82 android.ApexModuleBase 83 android.SdkBase 84 85 ClasspathFragmentBase 86 87 properties systemServerClasspathFragmentProperties 88 89 // Collect the module directory for IDE info in java/jdeps.go. 90 modulePaths []string 91} 92 93func (s *SystemServerClasspathModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error { 94 return nil 95} 96 97type systemServerClasspathFragmentProperties struct { 98 // List of system_server classpath jars, could be either java_library, or java_sdk_library. 99 // 100 // The order of this list matters as it is the order that is used in the SYSTEMSERVERCLASSPATH. 101 Contents []string 102 103 // List of jars that system_server loads dynamically using separate classloaders. 104 // 105 // The order does not matter. 106 Standalone_contents []string 107} 108 109func systemServerClasspathFactory() android.Module { 110 m := &SystemServerClasspathModule{} 111 m.AddProperties(&m.properties) 112 android.InitApexModule(m) 113 android.InitSdkAwareModule(m) 114 initClasspathFragment(m, SYSTEMSERVERCLASSPATH) 115 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) 116 return m 117} 118 119func (s *SystemServerClasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { 120 if len(s.properties.Contents) == 0 && len(s.properties.Standalone_contents) == 0 { 121 ctx.PropertyErrorf("contents", "Either contents or standalone_contents needs to be non-empty") 122 } 123 124 configuredJars := s.configuredJars(ctx) 125 classpathJars := configuredJarListToClasspathJars(ctx, configuredJars, s.classpathType) 126 standaloneConfiguredJars := s.standaloneConfiguredJars(ctx) 127 standaloneClasspathJars := configuredJarListToClasspathJars(ctx, standaloneConfiguredJars, STANDALONE_SYSTEMSERVER_JARS) 128 configuredJars = configuredJars.AppendList(&standaloneConfiguredJars) 129 classpathJars = append(classpathJars, standaloneClasspathJars...) 130 s.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars) 131 132 // Collect the module directory for IDE info in java/jdeps.go. 133 s.modulePaths = append(s.modulePaths, ctx.ModuleDir()) 134} 135 136func (s *SystemServerClasspathModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList { 137 global := dexpreopt.GetGlobalConfig(ctx) 138 139 possibleUpdatableModules := gatherPossibleApexModuleNamesAndStems(ctx, s.properties.Contents, systemServerClasspathFragmentContentDepTag) 140 jars, unknown := global.ApexSystemServerJars.Filter(possibleUpdatableModules) 141 // TODO(satayev): remove geotz ssc_fragment, since geotz is not part of SSCP anymore. 142 _, unknown = android.RemoveFromList("geotz", unknown) 143 // This module only exists in car products. 144 // So ignore it even if it is not in PRODUCT_APEX_SYSTEM_SERVER_JARS. 145 // TODO(b/203233647): Add better mechanism to make it optional. 146 _, unknown = android.RemoveFromList("car-frameworks-service-module", unknown) 147 148 // This module is optional, so it is not present in all products. 149 // (See PRODUCT_ISOLATED_COMPILATION_ENABLED.) 150 // So ignore it even if it is not in PRODUCT_APEX_SYSTEM_SERVER_JARS. 151 // TODO(b/203233647): Add better mechanism to make it optional. 152 _, unknown = android.RemoveFromList("service-compos", unknown) 153 154 // TODO(satayev): for apex_test we want to include all contents unconditionally to classpaths 155 // config. However, any test specific jars would not be present in ApexSystemServerJars. Instead, 156 // we should check if we are creating a config for apex_test via ApexInfo and amend the values. 157 // This is an exception to support end-to-end test for ApexdUnitTests, until such support exists. 158 if android.InList("test_service-apexd", possibleUpdatableModules) { 159 jars = jars.Append("com.android.apex.test_package", "test_service-apexd") 160 } else if global.ApexSystemServerJars.Len() > 0 && len(unknown) > 0 && !android.IsModuleInVersionedSdk(ctx.Module()) { 161 // For non test apexes, make sure that all contents are actually declared in make. 162 ctx.ModuleErrorf("%s in contents must also be declared in PRODUCT_APEX_SYSTEM_SERVER_JARS", unknown) 163 } 164 165 return jars 166} 167 168func (s *SystemServerClasspathModule) standaloneConfiguredJars(ctx android.ModuleContext) android.ConfiguredJarList { 169 global := dexpreopt.GetGlobalConfig(ctx) 170 171 possibleUpdatableModules := gatherPossibleApexModuleNamesAndStems(ctx, s.properties.Standalone_contents, systemServerClasspathFragmentContentDepTag) 172 jars, _ := global.ApexStandaloneSystemServerJars.Filter(possibleUpdatableModules) 173 174 // TODO(jiakaiz): add a check to ensure that the contents are declared in make. 175 176 return jars 177} 178 179type systemServerClasspathFragmentContentDependencyTag struct { 180 blueprint.BaseDependencyTag 181} 182 183// The systemserverclasspath_fragment contents must never depend on prebuilts. 184func (systemServerClasspathFragmentContentDependencyTag) ReplaceSourceWithPrebuilt() bool { 185 return false 186} 187 188// SdkMemberType causes dependencies added with this tag to be automatically added to the sdk as if 189// they were specified using java_systemserver_libs or java_sdk_libs. 190func (b systemServerClasspathFragmentContentDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType { 191 // If the module is a java_sdk_library then treat it as if it was specified in the java_sdk_libs 192 // property, otherwise treat if it was specified in the java_systemserver_libs property. 193 if javaSdkLibrarySdkMemberType.IsInstance(child) { 194 return javaSdkLibrarySdkMemberType 195 } 196 197 return javaSystemserverLibsSdkMemberType 198} 199 200func (b systemServerClasspathFragmentContentDependencyTag) ExportMember() bool { 201 return true 202} 203 204// Contents of system server fragments in an apex are considered to be directly in the apex, as if 205// they were listed in java_libs. 206func (systemServerClasspathFragmentContentDependencyTag) CopyDirectlyInAnyApex() {} 207 208// Contents of system server fragments require files from prebuilt apex files. 209func (systemServerClasspathFragmentContentDependencyTag) RequiresFilesFromPrebuiltApex() {} 210 211var _ android.ReplaceSourceWithPrebuilt = systemServerClasspathFragmentContentDepTag 212var _ android.SdkMemberDependencyTag = systemServerClasspathFragmentContentDepTag 213var _ android.CopyDirectlyInAnyApexTag = systemServerClasspathFragmentContentDepTag 214var _ android.RequiresFilesFromPrebuiltApexTag = systemServerClasspathFragmentContentDepTag 215 216// The tag used for the dependency between the systemserverclasspath_fragment module and its contents. 217var systemServerClasspathFragmentContentDepTag = systemServerClasspathFragmentContentDependencyTag{} 218 219func IsSystemServerClasspathFragmentContentDepTag(tag blueprint.DependencyTag) bool { 220 return tag == systemServerClasspathFragmentContentDepTag 221} 222 223func (s *SystemServerClasspathModule) ComponentDepsMutator(ctx android.BottomUpMutatorContext) { 224 module := ctx.Module() 225 _, isSourceModule := module.(*SystemServerClasspathModule) 226 var deps []string 227 deps = append(deps, s.properties.Contents...) 228 deps = append(deps, s.properties.Standalone_contents...) 229 230 for _, name := range deps { 231 // A systemserverclasspath_fragment must depend only on other source modules, while the 232 // prebuilt_systemserverclasspath_fragment_fragment must only depend on other prebuilt modules. 233 if !isSourceModule { 234 name = android.PrebuiltNameFromSource(name) 235 } 236 ctx.AddDependency(module, systemServerClasspathFragmentContentDepTag, name) 237 } 238} 239 240// Collect information for opening IDE project files in java/jdeps.go. 241func (s *SystemServerClasspathModule) IDEInfo(dpInfo *android.IdeInfo) { 242 dpInfo.Deps = append(dpInfo.Deps, s.properties.Contents...) 243 dpInfo.Deps = append(dpInfo.Deps, s.properties.Standalone_contents...) 244 dpInfo.Paths = append(dpInfo.Paths, s.modulePaths...) 245} 246 247type systemServerClasspathFragmentMemberType struct { 248 android.SdkMemberTypeBase 249} 250 251func (s *systemServerClasspathFragmentMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) { 252 ctx.AddVariationDependencies(nil, dependencyTag, names...) 253} 254 255func (s *systemServerClasspathFragmentMemberType) IsInstance(module android.Module) bool { 256 _, ok := module.(*SystemServerClasspathModule) 257 return ok 258} 259 260func (s *systemServerClasspathFragmentMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { 261 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_systemserverclasspath_fragment") 262} 263 264func (s *systemServerClasspathFragmentMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { 265 return &systemServerClasspathFragmentSdkMemberProperties{} 266} 267 268type systemServerClasspathFragmentSdkMemberProperties struct { 269 android.SdkMemberPropertiesBase 270 271 // List of system_server classpath jars, could be either java_library, or java_sdk_library. 272 // 273 // The order of this list matters as it is the order that is used in the SYSTEMSERVERCLASSPATH. 274 Contents []string 275 276 // List of jars that system_server loads dynamically using separate classloaders. 277 // 278 // The order does not matter. 279 Standalone_contents []string 280} 281 282func (s *systemServerClasspathFragmentSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { 283 module := variant.(*SystemServerClasspathModule) 284 285 s.Contents = module.properties.Contents 286 s.Standalone_contents = module.properties.Standalone_contents 287} 288 289func (s *systemServerClasspathFragmentSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { 290 builder := ctx.SnapshotBuilder() 291 requiredMemberDependency := builder.SdkMemberReferencePropertyTag(true) 292 293 if len(s.Contents) > 0 { 294 propertySet.AddPropertyWithTag("contents", s.Contents, requiredMemberDependency) 295 } 296 297 if len(s.Standalone_contents) > 0 { 298 propertySet.AddPropertyWithTag("standalone_contents", s.Standalone_contents, requiredMemberDependency) 299 } 300} 301 302var _ android.SdkMemberType = (*systemServerClasspathFragmentMemberType)(nil) 303 304// A prebuilt version of the systemserverclasspath_fragment module. 305type prebuiltSystemServerClasspathModule struct { 306 SystemServerClasspathModule 307 prebuilt android.Prebuilt 308} 309 310func (module *prebuiltSystemServerClasspathModule) Prebuilt() *android.Prebuilt { 311 return &module.prebuilt 312} 313 314func (module *prebuiltSystemServerClasspathModule) Name() string { 315 return module.prebuilt.Name(module.ModuleBase.Name()) 316} 317 318func (module *prebuiltSystemServerClasspathModule) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string { 319 return nil 320} 321 322var _ android.RequiredFilesFromPrebuiltApex = (*prebuiltSystemServerClasspathModule)(nil) 323 324func prebuiltSystemServerClasspathModuleFactory() android.Module { 325 m := &prebuiltSystemServerClasspathModule{} 326 m.AddProperties(&m.properties) 327 // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs 328 // array. 329 android.InitPrebuiltModule(m, &[]string{"placeholder"}) 330 android.InitApexModule(m) 331 android.InitSdkAwareModule(m) 332 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) 333 return m 334} 335