blob: 75c85852c5ddb3493fc531796bd958b43fc04e7c [file] [log] [blame]
Jiyong Parkd1063c12019-07-17 20:08:41 +09001// 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 (
Jiyong Park9b409bc2019-10-11 14:59:13 +090018 "fmt"
Paul Duffin504b4612019-11-22 14:52:29 +000019 "io"
Jiyong Park9b409bc2019-10-11 14:59:13 +090020 "strconv"
21
Jiyong Parkd1063c12019-07-17 20:08:41 +090022 "github.com/google/blueprint"
Jiyong Park100f3fd2019-11-06 16:03:32 +090023 "github.com/google/blueprint/proptools"
Jiyong Parkd1063c12019-07-17 20:08:41 +090024
25 "android/soong/android"
26 // This package doesn't depend on the apex package, but import it to make its mutators to be
27 // registered before mutators in this package. See RegisterPostDepsMutators for more details.
28 _ "android/soong/apex"
Jiyong Park73c54ee2019-10-22 20:31:18 +090029 "android/soong/cc"
Paul Duffin13879572019-11-28 14:31:38 +000030 "android/soong/java"
Jiyong Parkd1063c12019-07-17 20:08:41 +090031)
32
33func init() {
Jiyong Park232e7852019-11-04 12:23:40 +090034 pctx.Import("android/soong/android")
Paul Duffin375058f2019-11-29 20:17:53 +000035 pctx.Import("android/soong/java/config")
36
Jiyong Parkd1063c12019-07-17 20:08:41 +090037 android.RegisterModuleType("sdk", ModuleFactory)
Jiyong Park9b409bc2019-10-11 14:59:13 +090038 android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
Jiyong Parkd1063c12019-07-17 20:08:41 +090039 android.PreDepsMutators(RegisterPreDepsMutators)
40 android.PostDepsMutators(RegisterPostDepsMutators)
Paul Duffin13879572019-11-28 14:31:38 +000041
42 // Populate the dependency tags for each member list property. This needs to
43 // be done here to break an initialization cycle.
44 for _, memberListProperty := range sdkMemberListProperties {
45 memberListProperty.dependencyTag = &sdkMemberDependencyTag{
46 memberListProperty: memberListProperty,
47 }
48 }
Jiyong Parkd1063c12019-07-17 20:08:41 +090049}
50
51type sdk struct {
52 android.ModuleBase
53 android.DefaultableModuleBase
54
55 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090056
Jiyong Park232e7852019-11-04 12:23:40 +090057 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000058
59 // The builder, preserved for testing.
60 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090061}
62
63type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090064 // The list of java libraries in this SDK
65 Java_libs []string
66 // The list of native libraries in this SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +090067 Native_shared_libs []string
Paul Duffin91547182019-11-12 19:39:36 +000068 // The list of stub sources in this SDK
69 Stubs_sources []string
Jiyong Park9b409bc2019-10-11 14:59:13 +090070
71 Snapshot bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090072}
73
Paul Duffin13879572019-11-28 14:31:38 +000074type sdkMemberDependencyTag struct {
75 blueprint.BaseDependencyTag
76 memberListProperty *sdkMemberListProperty
77}
78
79// Contains information about the sdk properties that list sdk members, e.g.
80// Java_libs.
81type sdkMemberListProperty struct {
82 // the name of the property as used in a .bp file
83 name string
84
85 // getter for the list of member names
86 getter func(properties *sdkProperties) []string
87
88 // the type of member referenced in the list
89 memberType android.SdkMemberType
90
91 // the dependency tag used for items in this list.
92 dependencyTag *sdkMemberDependencyTag
93}
94
Paul Duffina6e737b2019-11-29 11:55:51 +000095// Information about how to handle each member list property.
96//
97// It is organized first by package and then by name within the package.
98// Packages are in alphabetical order and properties are in alphabetical order
99// within each package.
Paul Duffin13879572019-11-28 14:31:38 +0000100var sdkMemberListProperties = []*sdkMemberListProperty{
Paul Duffina6e737b2019-11-29 11:55:51 +0000101 // Members from cc package.
102 {
103 name: "native_shared_libs",
104 getter: func(properties *sdkProperties) []string { return properties.Native_shared_libs },
105 memberType: cc.LibrarySdkMemberType,
106 },
107 // Members from java package.
Paul Duffin13879572019-11-28 14:31:38 +0000108 {
109 name: "java_libs",
110 getter: func(properties *sdkProperties) []string { return properties.Java_libs },
111 memberType: java.LibrarySdkMemberType,
112 },
113 {
114 name: "stubs_sources",
115 getter: func(properties *sdkProperties) []string { return properties.Stubs_sources },
116 memberType: java.DroidStubsSdkMemberType,
117 },
Paul Duffin13879572019-11-28 14:31:38 +0000118}
119
Jiyong Parkd1063c12019-07-17 20:08:41 +0900120// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
121// which Mainline modules like APEX can choose to build with.
122func ModuleFactory() android.Module {
123 s := &sdk{}
124 s.AddProperties(&s.properties)
125 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
126 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900127 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
128 type props struct {
129 Compile_multilib *string
130 }
131 p := &props{Compile_multilib: proptools.StringPtr("both")}
132 ctx.AppendProperties(p)
133 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900134 return s
135}
136
Jiyong Park9b409bc2019-10-11 14:59:13 +0900137// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
138func SnapshotModuleFactory() android.Module {
139 s := ModuleFactory()
140 s.(*sdk).properties.Snapshot = true
141 return s
142}
143
144func (s *sdk) snapshot() bool {
145 return s.properties.Snapshot
146}
147
Jiyong Parkd1063c12019-07-17 20:08:41 +0900148func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900149 if !s.snapshot() {
150 // We don't need to create a snapshot out of sdk_snapshot.
151 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
152 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
153 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900154}
155
156func (s *sdk) AndroidMkEntries() android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900157 if !s.snapshotFile.Valid() {
158 return android.AndroidMkEntries{}
159 }
160
161 return android.AndroidMkEntries{
162 Class: "FAKE",
163 OutputFile: s.snapshotFile,
164 DistFile: s.snapshotFile,
165 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000166 ExtraFooters: []android.AndroidMkExtraFootersFunc{
167 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
168 // Allow the sdk to be built by simply passing its name on the command line.
169 fmt.Fprintln(w, ".PHONY:", s.Name())
170 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
171 },
172 },
Jiyong Park232e7852019-11-04 12:23:40 +0900173 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900174}
175
176// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
177// interface and the sdk module type. This function has been made public to be called by tests
178// outside of the sdk package
179func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
180 ctx.BottomUp("SdkMember", memberMutator).Parallel()
181 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
182 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
183}
184
185// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
186// interface and the sdk module type. This function has been made public to be called by tests
187// outside of the sdk package
188func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
189 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
190 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
191 // APEX to its dependents. Since different versions of the same SDK can be used by different
192 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
193 // should have been mutated for the apex before the SDK requirements are set.
194 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
195 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900196 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900197}
198
199type dependencyTag struct {
200 blueprint.BaseDependencyTag
201}
202
Jiyong Parkd1063c12019-07-17 20:08:41 +0900203// For dependencies from an in-development version of an SDK member to frozen versions of the same member
204// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
205type sdkMemberVesionedDepTag struct {
206 dependencyTag
207 member string
208 version string
209}
210
211// Step 1: create dependencies from an SDK module to its members.
212func memberMutator(mctx android.BottomUpMutatorContext) {
213 if m, ok := mctx.Module().(*sdk); ok {
Paul Duffin13879572019-11-28 14:31:38 +0000214 for _, memberListProperty := range sdkMemberListProperties {
215 names := memberListProperty.getter(&m.properties)
216 tag := memberListProperty.dependencyTag
217 memberListProperty.memberType.AddDependencies(mctx, tag, names)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900218 }
219 }
220}
221
222// Step 2: record that dependencies of SDK modules are members of the SDK modules
223func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900224 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900225 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900226 if s.snapshot() && mySdkRef.Unversioned() {
227 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
228 "Did you manually modify Android.bp?")
229 }
230 if !s.snapshot() && !mySdkRef.Unversioned() {
231 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
232 }
233 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
234 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
235 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
236 }
237 }
238
Jiyong Parkd1063c12019-07-17 20:08:41 +0900239 mctx.VisitDirectDeps(func(child android.Module) {
240 if member, ok := child.(android.SdkAware); ok {
241 member.MakeMemberOf(mySdkRef)
242 }
243 })
244 }
245}
246
Jiyong Park9b409bc2019-10-11 14:59:13 +0900247// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900248// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
249// (apex and apk), each of which might want different sdks to be built with. For example, if both
250// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
251// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
252// using.
253func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
254 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900255 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900256 memberName := m.MemberName()
257 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
258 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
259 }
260 }
261}
262
263// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
264// descendants
265func sdkDepsMutator(mctx android.TopDownMutatorContext) {
266 if m, ok := mctx.Module().(android.SdkAware); ok {
267 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
268 // by reading its own properties like `uses_sdks`.
269 requiredSdks := m.RequiredSdks()
270 if len(requiredSdks) > 0 {
271 mctx.VisitDirectDeps(func(m android.Module) {
272 if dep, ok := m.(android.SdkAware); ok {
273 dep.BuildWithSdks(requiredSdks)
274 }
275 })
276 }
277 }
278}
279
280// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
281// versioned module is used instead of the un-versioned (in-development) module libfoo
282func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
283 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900284 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900285 if m.RequiredSdks().Contains(sdk) {
286 // Note that this replacement is done only for the modules that have the same
287 // variations as the current module. Since current module is already mutated for
288 // apex references in other APEXes are not affected by this replacement.
289 memberName := m.MemberName()
290 mctx.ReplaceDependencies(memberName)
291 }
292 }
293 }
294}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900295
296// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
297func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
298 if m, ok := mctx.Module().(interface {
299 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
300 RequiredSdks() android.SdkRefs
301 }); ok {
302 requiredSdks := m.RequiredSdks()
303 if len(requiredSdks) == 0 {
304 return
305 }
306 mctx.VisitDirectDeps(func(dep android.Module) {
307 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
308 // dependency to defaults is always okay
309 return
310 }
311
312 // If the dep is from outside of the APEX, but is not in any of the
313 // required SDKs, we know that the dep is a violation.
314 if sa, ok := dep.(android.SdkAware); ok {
315 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
316 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
317 sa.Name(), sa.ContainingSdk(), requiredSdks)
318 }
319 }
320 })
321 }
322}