1// Copyright (C) 2019 The Android Open Source Project 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 sdk 16 17import ( 18 "fmt" 19 "io" 20 21 "github.com/google/blueprint" 22 "github.com/google/blueprint/proptools" 23 24 "android/soong/android" 25 // This package doesn't depend on the apex package, but import it to make its mutators to be 26 // registered before mutators in this package. See RegisterPostDepsMutators for more details. 27 _ "android/soong/apex" 28) 29 30func init() { 31 pctx.Import("android/soong/android") 32 pctx.Import("android/soong/java/config") 33 34 registerSdkBuildComponents(android.InitRegistrationContext) 35} 36 37func registerSdkBuildComponents(ctx android.RegistrationContext) { 38 ctx.RegisterModuleType("sdk", SdkModuleFactory) 39 ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) 40} 41 42type sdk struct { 43 android.ModuleBase 44 android.DefaultableModuleBase 45 46 // The dynamically generated information about the registered SdkMemberType 47 dynamicSdkMemberTypes *dynamicSdkMemberTypes 48 49 // The dynamically created instance of the properties struct containing the sdk member type 50 // list properties, e.g. java_libs. 51 dynamicMemberTypeListProperties interface{} 52 53 // The dynamically generated information about the registered SdkMemberTrait 54 dynamicSdkMemberTraits *dynamicSdkMemberTraits 55 56 // The dynamically created instance of the properties struct containing the sdk member trait 57 // list properties. 58 dynamicMemberTraitListProperties interface{} 59 60 // Information about the OsType specific member variants depended upon by this variant. 61 // 62 // Set by OsType specific variants in the collectMembers() method and used by the 63 // CommonOS variant when building the snapshot. That work is all done on separate 64 // calls to the sdk.GenerateAndroidBuildActions method which is guaranteed to be 65 // called for the OsType specific variants before the CommonOS variant (because 66 // the latter depends on the former). 67 memberVariantDeps []sdkMemberVariantDep 68 69 // The multilib variants that are used by this sdk variant. 70 multilibUsages multilibUsage 71 72 properties sdkProperties 73 74 snapshotFile android.OptionalPath 75 76 infoFile android.OptionalPath 77 78 // The builder, preserved for testing. 79 builderForTests *snapshotBuilder 80} 81 82type sdkProperties struct { 83 Snapshot bool `blueprint:"mutated"` 84 85 // True if this is a module_exports (or module_exports_snapshot) module type. 86 Module_exports bool `blueprint:"mutated"` 87 88 // The additional visibility to add to the prebuilt modules to allow them to 89 // reference each other. 90 // 91 // This can only be used to widen the visibility of the members: 92 // 93 // * Specifying //visibility:public here will make all members visible and 94 // essentially ignore their own visibility. 95 // * Specifying //visibility:private here is an error. 96 // * Specifying any other rule here will add it to the members visibility and 97 // be output to the member prebuilt in the snapshot. Duplicates will be 98 // dropped. Adding a rule to members that have //visibility:private will 99 // cause the //visibility:private to be discarded. 100 Prebuilt_visibility []string 101} 102 103// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) 104// which Mainline modules like APEX can choose to build with. 105func SdkModuleFactory() android.Module { 106 return newSdkModule(false) 107} 108 109func newSdkModule(moduleExports bool) *sdk { 110 s := &sdk{} 111 s.properties.Module_exports = moduleExports 112 // Get the dynamic sdk member type data for the currently registered sdk member types. 113 sdkMemberTypeKey, sdkMemberTypes := android.RegisteredSdkMemberTypes(moduleExports) 114 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(sdkMemberTypeKey, sdkMemberTypes) 115 // Create an instance of the dynamically created struct that contains all the 116 // properties for the member type specific list properties. 117 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberTypeListProperties() 118 119 sdkMemberTraitsKey, sdkMemberTraits := android.RegisteredSdkMemberTraits() 120 s.dynamicSdkMemberTraits = getDynamicSdkMemberTraits(sdkMemberTraitsKey, sdkMemberTraits) 121 // Create an instance of the dynamically created struct that contains all the properties for the 122 // member trait specific list properties. 123 s.dynamicMemberTraitListProperties = s.dynamicSdkMemberTraits.createMemberTraitListProperties() 124 125 // Create a wrapper around the dynamic trait specific properties so that they have to be 126 // specified within a traits:{} section in the .bp file. 127 traitsWrapper := struct { 128 Traits interface{} 129 }{s.dynamicMemberTraitListProperties} 130 131 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties, &traitsWrapper) 132 133 // Make sure that the prebuilt visibility property is verified for errors. 134 android.AddVisibilityProperty(s, "prebuilt_visibility", &s.properties.Prebuilt_visibility) 135 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon) 136 android.InitDefaultableModule(s) 137 android.AddLoadHook(s, func(ctx android.LoadHookContext) { 138 type props struct { 139 Compile_multilib *string 140 } 141 p := &props{Compile_multilib: proptools.StringPtr("both")} 142 ctx.PrependProperties(p) 143 }) 144 return s 145} 146 147// sdk_snapshot is a snapshot of an SDK. This is an auto-generated module. 148func SnapshotModuleFactory() android.Module { 149 s := newSdkModule(false) 150 s.properties.Snapshot = true 151 return s 152} 153 154func (s *sdk) memberTypeListProperties() []*sdkMemberTypeListProperty { 155 return s.dynamicSdkMemberTypes.memberTypeListProperties 156} 157 158func (s *sdk) memberTypeListProperty(memberType android.SdkMemberType) *sdkMemberTypeListProperty { 159 return s.dynamicSdkMemberTypes.memberTypeToProperty[memberType] 160} 161 162// memberTraitListProperties returns the list of *sdkMemberTraitListProperty instances for this sdk. 163func (s *sdk) memberTraitListProperties() []*sdkMemberTraitListProperty { 164 return s.dynamicSdkMemberTraits.memberTraitListProperties 165} 166 167func (s *sdk) snapshot() bool { 168 return s.properties.Snapshot 169} 170 171func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) { 172 if s.snapshot() { 173 // We don't need to create a snapshot out of sdk_snapshot. 174 // That doesn't make sense. We need a snapshot to create sdk_snapshot. 175 return 176 } 177 178 // This method is guaranteed to be called on OsType specific variants before it is called 179 // on their corresponding CommonOS variant. 180 if !s.IsCommonOSVariant() { 181 // Update the OsType specific sdk variant with information about its members. 182 s.collectMembers(ctx) 183 } else { 184 // Get the OsType specific variants on which the CommonOS depends. 185 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx) 186 var sdkVariants []*sdk 187 for _, m := range osSpecificVariants { 188 if sdkVariant, ok := m.(*sdk); ok { 189 sdkVariants = append(sdkVariants, sdkVariant) 190 } 191 } 192 193 // Generate the snapshot from the member info. 194 s.buildSnapshot(ctx, sdkVariants) 195 } 196} 197 198func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries { 199 if !s.snapshotFile.Valid() != !s.infoFile.Valid() { 200 panic("Snapshot (%q) and info file (%q) should both be set or neither should be set.") 201 } else if !s.snapshotFile.Valid() { 202 return []android.AndroidMkEntries{} 203 } 204 205 return []android.AndroidMkEntries{android.AndroidMkEntries{ 206 Class: "FAKE", 207 OutputFile: s.snapshotFile, 208 DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path(), s.infoFile.Path()), 209 Include: "$(BUILD_PHONY_PACKAGE)", 210 ExtraFooters: []android.AndroidMkExtraFootersFunc{ 211 func(w io.Writer, name, prefix, moduleDir string) { 212 // Allow the sdk to be built by simply passing its name on the command line. 213 fmt.Fprintln(w, ".PHONY:", s.Name()) 214 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String()) 215 216 // Allow the sdk info to be built by simply passing its name on the command line. 217 infoTarget := s.Name() + ".info" 218 fmt.Fprintln(w, ".PHONY:", infoTarget) 219 fmt.Fprintln(w, infoTarget+":", s.infoFile.String()) 220 }, 221 }, 222 }} 223} 224 225func (s *sdk) OutputFiles(tag string) (android.Paths, error) { 226 switch tag { 227 case "": 228 if s.snapshotFile.Valid() { 229 return []android.Path{s.snapshotFile.Path()}, nil 230 } 231 return nil, fmt.Errorf("snapshot file not defined. This is most likely because this isn't the common_os variant of this module") 232 default: 233 return nil, fmt.Errorf("unknown tag %q", tag) 234 } 235} 236 237// gatherTraits gathers the traits from the dynamically generated trait specific properties. 238// 239// Returns a map from member name to the set of required traits. 240func (s *sdk) gatherTraits() map[string]android.SdkMemberTraitSet { 241 traitListByMember := map[string][]android.SdkMemberTrait{} 242 for _, memberListProperty := range s.memberTraitListProperties() { 243 names := memberListProperty.getter(s.dynamicMemberTraitListProperties) 244 for _, name := range names { 245 traitListByMember[name] = append(traitListByMember[name], memberListProperty.memberTrait) 246 } 247 } 248 249 traitSetByMember := map[string]android.SdkMemberTraitSet{} 250 for name, list := range traitListByMember { 251 traitSetByMember[name] = android.NewSdkMemberTraitSet(list) 252 } 253 254 return traitSetByMember 255} 256 257// newDependencyContext creates a new SdkDependencyContext for this sdk. 258func (s *sdk) newDependencyContext(mctx android.BottomUpMutatorContext) android.SdkDependencyContext { 259 traits := s.gatherTraits() 260 261 return &dependencyContext{ 262 BottomUpMutatorContext: mctx, 263 requiredTraits: traits, 264 } 265} 266 267type dependencyContext struct { 268 android.BottomUpMutatorContext 269 270 // Map from member name to the set of traits that the sdk requires the member provides. 271 requiredTraits map[string]android.SdkMemberTraitSet 272} 273 274func (d *dependencyContext) RequiredTraits(name string) android.SdkMemberTraitSet { 275 if s, ok := d.requiredTraits[name]; ok { 276 return s 277 } else { 278 return android.EmptySdkMemberTraitSet() 279 } 280} 281 282func (d *dependencyContext) RequiresTrait(name string, trait android.SdkMemberTrait) bool { 283 return d.RequiredTraits(name).Contains(trait) 284} 285 286var _ android.SdkDependencyContext = (*dependencyContext)(nil) 287 288type dependencyTag struct { 289 blueprint.BaseDependencyTag 290} 291 292// Mark this tag so dependencies that use it are excluded from APEX contents. 293func (t dependencyTag) ExcludeFromApexContents() {} 294 295var _ android.ExcludeFromApexContentsTag = dependencyTag{} 296 297func (s *sdk) DepsMutator(mctx android.BottomUpMutatorContext) { 298 // Add dependencies from non CommonOS variants to the sdk member variants. 299 if s.IsCommonOSVariant() { 300 return 301 } 302 303 ctx := s.newDependencyContext(mctx) 304 for _, memberListProperty := range s.memberTypeListProperties() { 305 if memberListProperty.getter == nil { 306 continue 307 } 308 names := memberListProperty.getter(s.dynamicMemberTypeListProperties) 309 if len(names) > 0 { 310 memberType := memberListProperty.memberType 311 312 // Verify that the member type supports the specified traits. 313 supportedTraits := memberType.SupportedTraits() 314 for _, name := range names { 315 requiredTraits := ctx.RequiredTraits(name) 316 unsupportedTraits := requiredTraits.Subtract(supportedTraits) 317 if !unsupportedTraits.Empty() { 318 ctx.ModuleErrorf("sdk member %q has traits %s that are unsupported by its member type %q", 319 name, unsupportedTraits, memberType.SdkPropertyName()) 320 } 321 } 322 323 // Add dependencies using the appropriate tag. 324 tag := memberListProperty.dependencyTag 325 memberType.AddDependencies(ctx, tag, names) 326 } 327 } 328} 329