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 89// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.) 90// which Mainline modules like APEX can choose to build with. 91func SdkModuleFactory() android.Module { 92 return newSdkModule(false) 93} 94 95func newSdkModule(moduleExports bool) *sdk { 96 s := &sdk{} 97 s.properties.Module_exports = moduleExports 98 // Get the dynamic sdk member type data for the currently registered sdk member types. 99 sdkMemberTypeKey, sdkMemberTypes := android.RegisteredSdkMemberTypes(moduleExports) 100 s.dynamicSdkMemberTypes = getDynamicSdkMemberTypes(sdkMemberTypeKey, sdkMemberTypes) 101 // Create an instance of the dynamically created struct that contains all the 102 // properties for the member type specific list properties. 103 s.dynamicMemberTypeListProperties = s.dynamicSdkMemberTypes.createMemberTypeListProperties() 104 105 sdkMemberTraitsKey, sdkMemberTraits := android.RegisteredSdkMemberTraits() 106 s.dynamicSdkMemberTraits = getDynamicSdkMemberTraits(sdkMemberTraitsKey, sdkMemberTraits) 107 // Create an instance of the dynamically created struct that contains all the properties for the 108 // member trait specific list properties. 109 s.dynamicMemberTraitListProperties = s.dynamicSdkMemberTraits.createMemberTraitListProperties() 110 111 // Create a wrapper around the dynamic trait specific properties so that they have to be 112 // specified within a traits:{} section in the .bp file. 113 traitsWrapper := struct { 114 Traits interface{} 115 }{s.dynamicMemberTraitListProperties} 116 117 s.AddProperties(&s.properties, s.dynamicMemberTypeListProperties, &traitsWrapper) 118 119 android.InitCommonOSAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon) 120 android.InitDefaultableModule(s) 121 android.AddLoadHook(s, func(ctx android.LoadHookContext) { 122 type props struct { 123 Compile_multilib *string 124 } 125 p := &props{Compile_multilib: proptools.StringPtr("both")} 126 ctx.PrependProperties(p) 127 }) 128 return s 129} 130 131// sdk_snapshot is a snapshot of an SDK. This is an auto-generated module. 132func SnapshotModuleFactory() android.Module { 133 s := newSdkModule(false) 134 s.properties.Snapshot = true 135 return s 136} 137 138func (s *sdk) memberTypeListProperties() []*sdkMemberTypeListProperty { 139 return s.dynamicSdkMemberTypes.memberTypeListProperties 140} 141 142func (s *sdk) memberTypeListProperty(memberType android.SdkMemberType) *sdkMemberTypeListProperty { 143 return s.dynamicSdkMemberTypes.memberTypeToProperty[memberType] 144} 145 146// memberTraitListProperties returns the list of *sdkMemberTraitListProperty instances for this sdk. 147func (s *sdk) memberTraitListProperties() []*sdkMemberTraitListProperty { 148 return s.dynamicSdkMemberTraits.memberTraitListProperties 149} 150 151func (s *sdk) snapshot() bool { 152 return s.properties.Snapshot 153} 154 155func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) { 156 if s.snapshot() { 157 // We don't need to create a snapshot out of sdk_snapshot. 158 // That doesn't make sense. We need a snapshot to create sdk_snapshot. 159 return 160 } 161 162 // This method is guaranteed to be called on OsType specific variants before it is called 163 // on their corresponding CommonOS variant. 164 if !s.IsCommonOSVariant() { 165 // Update the OsType specific sdk variant with information about its members. 166 s.collectMembers(ctx) 167 } else { 168 // Get the OsType specific variants on which the CommonOS depends. 169 osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx) 170 var sdkVariants []*sdk 171 for _, m := range osSpecificVariants { 172 if sdkVariant, ok := m.(*sdk); ok { 173 sdkVariants = append(sdkVariants, sdkVariant) 174 } 175 } 176 177 // Generate the snapshot from the member info. 178 s.buildSnapshot(ctx, sdkVariants) 179 } 180 181 if s.snapshotFile.Valid() { 182 ctx.SetOutputFiles([]android.Path{s.snapshotFile.Path()}, "") 183 } 184} 185 186func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries { 187 if !s.snapshotFile.Valid() != !s.infoFile.Valid() { 188 panic("Snapshot (%q) and info file (%q) should both be set or neither should be set.") 189 } else if !s.snapshotFile.Valid() { 190 return []android.AndroidMkEntries{} 191 } 192 193 return []android.AndroidMkEntries{android.AndroidMkEntries{ 194 Class: "FAKE", 195 OutputFile: s.snapshotFile, 196 DistFiles: android.MakeDefaultDistFiles(s.snapshotFile.Path(), s.infoFile.Path()), 197 Include: "$(BUILD_PHONY_PACKAGE)", 198 ExtraEntries: []android.AndroidMkExtraEntriesFunc{ 199 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 200 entries.SetBool("LOCAL_DONT_CHECK_MODULE", true) 201 }, 202 }, 203 ExtraFooters: []android.AndroidMkExtraFootersFunc{ 204 func(w io.Writer, name, prefix, moduleDir string) { 205 // Allow the sdk to be built by simply passing its name on the command line. 206 fmt.Fprintln(w, ".PHONY:", s.Name()) 207 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String()) 208 209 // Allow the sdk info to be built by simply passing its name on the command line. 210 infoTarget := s.Name() + ".info" 211 fmt.Fprintln(w, ".PHONY:", infoTarget) 212 fmt.Fprintln(w, infoTarget+":", s.infoFile.String()) 213 }, 214 }, 215 }} 216} 217 218// gatherTraits gathers the traits from the dynamically generated trait specific properties. 219// 220// Returns a map from member name to the set of required traits. 221func (s *sdk) gatherTraits() map[string]android.SdkMemberTraitSet { 222 traitListByMember := map[string][]android.SdkMemberTrait{} 223 for _, memberListProperty := range s.memberTraitListProperties() { 224 names := memberListProperty.getter(s.dynamicMemberTraitListProperties) 225 for _, name := range names { 226 traitListByMember[name] = append(traitListByMember[name], memberListProperty.memberTrait) 227 } 228 } 229 230 traitSetByMember := map[string]android.SdkMemberTraitSet{} 231 for name, list := range traitListByMember { 232 traitSetByMember[name] = android.NewSdkMemberTraitSet(list) 233 } 234 235 return traitSetByMember 236} 237 238// newDependencyContext creates a new SdkDependencyContext for this sdk. 239func (s *sdk) newDependencyContext(mctx android.BottomUpMutatorContext) android.SdkDependencyContext { 240 traits := s.gatherTraits() 241 242 return &dependencyContext{ 243 BottomUpMutatorContext: mctx, 244 requiredTraits: traits, 245 } 246} 247 248type dependencyContext struct { 249 android.BottomUpMutatorContext 250 251 // Map from member name to the set of traits that the sdk requires the member provides. 252 requiredTraits map[string]android.SdkMemberTraitSet 253} 254 255func (d *dependencyContext) RequiredTraits(name string) android.SdkMemberTraitSet { 256 if s, ok := d.requiredTraits[name]; ok { 257 return s 258 } else { 259 return android.EmptySdkMemberTraitSet() 260 } 261} 262 263func (d *dependencyContext) RequiresTrait(name string, trait android.SdkMemberTrait) bool { 264 return d.RequiredTraits(name).Contains(trait) 265} 266 267var _ android.SdkDependencyContext = (*dependencyContext)(nil) 268 269type dependencyTag struct { 270 blueprint.BaseDependencyTag 271} 272 273// Mark this tag so dependencies that use it are excluded from APEX contents. 274func (t dependencyTag) ExcludeFromApexContents() {} 275 276var _ android.ExcludeFromApexContentsTag = dependencyTag{} 277 278func (s *sdk) DepsMutator(mctx android.BottomUpMutatorContext) { 279 // Add dependencies from non CommonOS variants to the sdk member variants. 280 if s.IsCommonOSVariant() { 281 return 282 } 283 284 ctx := s.newDependencyContext(mctx) 285 for _, memberListProperty := range s.memberTypeListProperties() { 286 if memberListProperty.getter == nil { 287 continue 288 } 289 names := memberListProperty.getter(s.dynamicMemberTypeListProperties) 290 if len(names) > 0 { 291 memberType := memberListProperty.memberType 292 293 // Verify that the member type supports the specified traits. 294 supportedTraits := memberType.SupportedTraits() 295 for _, name := range names { 296 requiredTraits := ctx.RequiredTraits(name) 297 unsupportedTraits := requiredTraits.Subtract(supportedTraits) 298 if !unsupportedTraits.Empty() { 299 ctx.ModuleErrorf("sdk member %q has traits %s that are unsupported by its member type %q", 300 name, unsupportedTraits, memberType.SdkPropertyName()) 301 } 302 } 303 304 // Add dependencies using the appropriate tag. 305 tag := memberListProperty.dependencyTag 306 memberType.AddDependencies(ctx, tag, names) 307 } 308 } 309} 310