• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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() != s.infoFile.Valid() {
182		panic(fmt.Sprintf("Snapshot (%q) and info file (%q) should both be set or neither should be set.", s.snapshotFile, s.infoFile))
183	}
184
185	if s.snapshotFile.Valid() {
186		ctx.SetOutputFiles([]android.Path{s.snapshotFile.Path()}, "")
187		ctx.SetOutputFiles([]android.Path{s.snapshotFile.Path(), s.infoFile.Path()}, android.DefaultDistTag)
188	}
189}
190
191func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
192	if !s.snapshotFile.Valid() {
193		return []android.AndroidMkEntries{}
194	}
195
196	return []android.AndroidMkEntries{android.AndroidMkEntries{
197		Class:      "FAKE",
198		OutputFile: s.snapshotFile,
199		Include:    "$(BUILD_PHONY_PACKAGE)",
200		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
201			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
202				entries.SetBool("LOCAL_DONT_CHECK_MODULE", true)
203			},
204		},
205		ExtraFooters: []android.AndroidMkExtraFootersFunc{
206			func(w io.Writer, name, prefix, moduleDir string) {
207				// Allow the sdk to be built by simply passing its name on the command line.
208				fmt.Fprintln(w, ".PHONY:", s.Name())
209				fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
210
211				// Allow the sdk info to be built by simply passing its name on the command line.
212				infoTarget := s.Name() + ".info"
213				fmt.Fprintln(w, ".PHONY:", infoTarget)
214				fmt.Fprintln(w, infoTarget+":", s.infoFile.String())
215			},
216		},
217	}}
218}
219
220// gatherTraits gathers the traits from the dynamically generated trait specific properties.
221//
222// Returns a map from member name to the set of required traits.
223func (s *sdk) gatherTraits() map[string]android.SdkMemberTraitSet {
224	traitListByMember := map[string][]android.SdkMemberTrait{}
225	for _, memberListProperty := range s.memberTraitListProperties() {
226		names := memberListProperty.getter(s.dynamicMemberTraitListProperties)
227		for _, name := range names {
228			traitListByMember[name] = append(traitListByMember[name], memberListProperty.memberTrait)
229		}
230	}
231
232	traitSetByMember := map[string]android.SdkMemberTraitSet{}
233	for name, list := range traitListByMember {
234		traitSetByMember[name] = android.NewSdkMemberTraitSet(list)
235	}
236
237	return traitSetByMember
238}
239
240// newDependencyContext creates a new SdkDependencyContext for this sdk.
241func (s *sdk) newDependencyContext(mctx android.BottomUpMutatorContext) android.SdkDependencyContext {
242	traits := s.gatherTraits()
243
244	return &dependencyContext{
245		BottomUpMutatorContext: mctx,
246		requiredTraits:         traits,
247	}
248}
249
250type dependencyContext struct {
251	android.BottomUpMutatorContext
252
253	// Map from member name to the set of traits that the sdk requires the member provides.
254	requiredTraits map[string]android.SdkMemberTraitSet
255}
256
257func (d *dependencyContext) RequiredTraits(name string) android.SdkMemberTraitSet {
258	if s, ok := d.requiredTraits[name]; ok {
259		return s
260	} else {
261		return android.EmptySdkMemberTraitSet()
262	}
263}
264
265func (d *dependencyContext) RequiresTrait(name string, trait android.SdkMemberTrait) bool {
266	return d.RequiredTraits(name).Contains(trait)
267}
268
269var _ android.SdkDependencyContext = (*dependencyContext)(nil)
270
271type dependencyTag struct {
272	blueprint.BaseDependencyTag
273}
274
275// Mark this tag so dependencies that use it are excluded from APEX contents.
276func (t dependencyTag) ExcludeFromApexContents() {}
277
278var _ android.ExcludeFromApexContentsTag = dependencyTag{}
279
280func (s *sdk) DepsMutator(mctx android.BottomUpMutatorContext) {
281	// Add dependencies from non CommonOS variants to the sdk member variants.
282	if s.IsCommonOSVariant() {
283		return
284	}
285
286	ctx := s.newDependencyContext(mctx)
287	for _, memberListProperty := range s.memberTypeListProperties() {
288		if memberListProperty.getter == nil {
289			continue
290		}
291		names := memberListProperty.getter(s.dynamicMemberTypeListProperties)
292		if len(names) > 0 {
293			memberType := memberListProperty.memberType
294
295			// Verify that the member type supports the specified traits.
296			supportedTraits := memberType.SupportedTraits()
297			for _, name := range names {
298				requiredTraits := ctx.RequiredTraits(name)
299				unsupportedTraits := requiredTraits.Subtract(supportedTraits)
300				if !unsupportedTraits.Empty() {
301					ctx.ModuleErrorf("sdk member %q has traits %s that are unsupported by its member type %q",
302						name, unsupportedTraits, memberType.SdkPropertyName())
303				}
304			}
305
306			// Add dependencies using the appropriate tag.
307			tag := memberListProperty.dependencyTag
308			memberType.AddDependencies(ctx, tag, names)
309		}
310	}
311}
312