blob: 80dd088c87fb973f49c15f4b67b71f541984bbfe [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
95var sdkMemberListProperties = []*sdkMemberListProperty{
96 {
97 name: "java_libs",
98 getter: func(properties *sdkProperties) []string { return properties.Java_libs },
99 memberType: java.LibrarySdkMemberType,
100 },
101 {
102 name: "stubs_sources",
103 getter: func(properties *sdkProperties) []string { return properties.Stubs_sources },
104 memberType: java.DroidStubsSdkMemberType,
105 },
106 {
107 name: "native_shared_libs",
108 getter: func(properties *sdkProperties) []string { return properties.Native_shared_libs },
109 memberType: cc.LibrarySdkMemberType,
110 },
111}
112
Jiyong Parkd1063c12019-07-17 20:08:41 +0900113// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
114// which Mainline modules like APEX can choose to build with.
115func ModuleFactory() android.Module {
116 s := &sdk{}
117 s.AddProperties(&s.properties)
118 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
119 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900120 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
121 type props struct {
122 Compile_multilib *string
123 }
124 p := &props{Compile_multilib: proptools.StringPtr("both")}
125 ctx.AppendProperties(p)
126 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900127 return s
128}
129
Jiyong Park9b409bc2019-10-11 14:59:13 +0900130// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
131func SnapshotModuleFactory() android.Module {
132 s := ModuleFactory()
133 s.(*sdk).properties.Snapshot = true
134 return s
135}
136
137func (s *sdk) snapshot() bool {
138 return s.properties.Snapshot
139}
140
Jiyong Parkd1063c12019-07-17 20:08:41 +0900141func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900142 if !s.snapshot() {
143 // We don't need to create a snapshot out of sdk_snapshot.
144 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
145 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
146 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900147}
148
149func (s *sdk) AndroidMkEntries() android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900150 if !s.snapshotFile.Valid() {
151 return android.AndroidMkEntries{}
152 }
153
154 return android.AndroidMkEntries{
155 Class: "FAKE",
156 OutputFile: s.snapshotFile,
157 DistFile: s.snapshotFile,
158 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000159 ExtraFooters: []android.AndroidMkExtraFootersFunc{
160 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
161 // Allow the sdk to be built by simply passing its name on the command line.
162 fmt.Fprintln(w, ".PHONY:", s.Name())
163 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
164 },
165 },
Jiyong Park232e7852019-11-04 12:23:40 +0900166 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900167}
168
169// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
170// interface and the sdk module type. This function has been made public to be called by tests
171// outside of the sdk package
172func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
173 ctx.BottomUp("SdkMember", memberMutator).Parallel()
174 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
175 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
176}
177
178// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
179// interface and the sdk module type. This function has been made public to be called by tests
180// outside of the sdk package
181func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
182 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
183 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
184 // APEX to its dependents. Since different versions of the same SDK can be used by different
185 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
186 // should have been mutated for the apex before the SDK requirements are set.
187 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
188 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900189 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900190}
191
192type dependencyTag struct {
193 blueprint.BaseDependencyTag
194}
195
Jiyong Parkd1063c12019-07-17 20:08:41 +0900196// For dependencies from an in-development version of an SDK member to frozen versions of the same member
197// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
198type sdkMemberVesionedDepTag struct {
199 dependencyTag
200 member string
201 version string
202}
203
204// Step 1: create dependencies from an SDK module to its members.
205func memberMutator(mctx android.BottomUpMutatorContext) {
206 if m, ok := mctx.Module().(*sdk); ok {
Paul Duffin13879572019-11-28 14:31:38 +0000207 for _, memberListProperty := range sdkMemberListProperties {
208 names := memberListProperty.getter(&m.properties)
209 tag := memberListProperty.dependencyTag
210 memberListProperty.memberType.AddDependencies(mctx, tag, names)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900211 }
212 }
213}
214
215// Step 2: record that dependencies of SDK modules are members of the SDK modules
216func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900217 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900218 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900219 if s.snapshot() && mySdkRef.Unversioned() {
220 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
221 "Did you manually modify Android.bp?")
222 }
223 if !s.snapshot() && !mySdkRef.Unversioned() {
224 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
225 }
226 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
227 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
228 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
229 }
230 }
231
Jiyong Parkd1063c12019-07-17 20:08:41 +0900232 mctx.VisitDirectDeps(func(child android.Module) {
233 if member, ok := child.(android.SdkAware); ok {
234 member.MakeMemberOf(mySdkRef)
235 }
236 })
237 }
238}
239
Jiyong Park9b409bc2019-10-11 14:59:13 +0900240// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900241// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
242// (apex and apk), each of which might want different sdks to be built with. For example, if both
243// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
244// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
245// using.
246func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
247 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900248 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900249 memberName := m.MemberName()
250 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
251 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
252 }
253 }
254}
255
256// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
257// descendants
258func sdkDepsMutator(mctx android.TopDownMutatorContext) {
259 if m, ok := mctx.Module().(android.SdkAware); ok {
260 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
261 // by reading its own properties like `uses_sdks`.
262 requiredSdks := m.RequiredSdks()
263 if len(requiredSdks) > 0 {
264 mctx.VisitDirectDeps(func(m android.Module) {
265 if dep, ok := m.(android.SdkAware); ok {
266 dep.BuildWithSdks(requiredSdks)
267 }
268 })
269 }
270 }
271}
272
273// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
274// versioned module is used instead of the un-versioned (in-development) module libfoo
275func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
276 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900277 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900278 if m.RequiredSdks().Contains(sdk) {
279 // Note that this replacement is done only for the modules that have the same
280 // variations as the current module. Since current module is already mutated for
281 // apex references in other APEXes are not affected by this replacement.
282 memberName := m.MemberName()
283 mctx.ReplaceDependencies(memberName)
284 }
285 }
286 }
287}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900288
289// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
290func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
291 if m, ok := mctx.Module().(interface {
292 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
293 RequiredSdks() android.SdkRefs
294 }); ok {
295 requiredSdks := m.RequiredSdks()
296 if len(requiredSdks) == 0 {
297 return
298 }
299 mctx.VisitDirectDeps(func(dep android.Module) {
300 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
301 // dependency to defaults is always okay
302 return
303 }
304
305 // If the dep is from outside of the APEX, but is not in any of the
306 // required SDKs, we know that the dep is a violation.
307 if sa, ok := dep.(android.SdkAware); ok {
308 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
309 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
310 sa.Name(), sa.ContainingSdk(), requiredSdks)
311 }
312 }
313 })
314 }
315}