blob: 7668d9188c167122a66d5170cc4cc25593e72a4b [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"
19 "strconv"
20
Jiyong Parkd1063c12019-07-17 20:08:41 +090021 "github.com/google/blueprint"
22
23 "android/soong/android"
24 // This package doesn't depend on the apex package, but import it to make its mutators to be
25 // registered before mutators in this package. See RegisterPostDepsMutators for more details.
26 _ "android/soong/apex"
27)
28
29func init() {
30 android.RegisterModuleType("sdk", ModuleFactory)
Jiyong Park9b409bc2019-10-11 14:59:13 +090031 android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
Jiyong Parkd1063c12019-07-17 20:08:41 +090032 android.PreDepsMutators(RegisterPreDepsMutators)
33 android.PostDepsMutators(RegisterPostDepsMutators)
34}
35
36type sdk struct {
37 android.ModuleBase
38 android.DefaultableModuleBase
39
40 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090041
42 updateScript android.OutputPath
43 freezeScript android.OutputPath
Jiyong Parkd1063c12019-07-17 20:08:41 +090044}
45
46type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090047 // The list of java libraries in this SDK
48 Java_libs []string
49 // The list of native libraries in this SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +090050 Native_shared_libs []string
Jiyong Park9b409bc2019-10-11 14:59:13 +090051
52 Snapshot bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090053}
54
55// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
56// which Mainline modules like APEX can choose to build with.
57func ModuleFactory() android.Module {
58 s := &sdk{}
59 s.AddProperties(&s.properties)
60 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
61 android.InitDefaultableModule(s)
62 return s
63}
64
Jiyong Park9b409bc2019-10-11 14:59:13 +090065// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
66func SnapshotModuleFactory() android.Module {
67 s := ModuleFactory()
68 s.(*sdk).properties.Snapshot = true
69 return s
70}
71
72func (s *sdk) snapshot() bool {
73 return s.properties.Snapshot
74}
75
76func (s *sdk) frozenVersions(ctx android.BaseModuleContext) []string {
77 if s.snapshot() {
78 panic(fmt.Errorf("frozenVersions() called for sdk_snapshot %q", ctx.ModuleName()))
79 }
80 versions := []string{}
81 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
82 depTag := ctx.OtherModuleDependencyTag(child)
83 if depTag == sdkMemberDepTag {
84 return true
85 }
86 if versionedDepTag, ok := depTag.(sdkMemberVesionedDepTag); ok {
87 v := versionedDepTag.version
88 if v != "current" && !android.InList(v, versions) {
89 versions = append(versions, versionedDepTag.version)
90 }
91 }
92 return false
93 })
94 return android.SortedUniqueStrings(versions)
95}
96
Jiyong Parkd1063c12019-07-17 20:08:41 +090097func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +090098 s.buildSnapshotGenerationScripts(ctx)
99}
100
101func (s *sdk) AndroidMkEntries() android.AndroidMkEntries {
102 return s.androidMkEntriesForScript()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900103}
104
105// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
106// interface and the sdk module type. This function has been made public to be called by tests
107// outside of the sdk package
108func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
109 ctx.BottomUp("SdkMember", memberMutator).Parallel()
110 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
111 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
112}
113
114// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
115// interface and the sdk module type. This function has been made public to be called by tests
116// outside of the sdk package
117func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
118 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
119 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
120 // APEX to its dependents. Since different versions of the same SDK can be used by different
121 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
122 // should have been mutated for the apex before the SDK requirements are set.
123 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
124 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900125 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900126}
127
128type dependencyTag struct {
129 blueprint.BaseDependencyTag
130}
131
132// For dependencies from an SDK module to its members
133// e.g. mysdk -> libfoo and libbar
134var sdkMemberDepTag dependencyTag
135
136// For dependencies from an in-development version of an SDK member to frozen versions of the same member
137// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
138type sdkMemberVesionedDepTag struct {
139 dependencyTag
140 member string
141 version string
142}
143
144// Step 1: create dependencies from an SDK module to its members.
145func memberMutator(mctx android.BottomUpMutatorContext) {
146 if m, ok := mctx.Module().(*sdk); ok {
147 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...)
148
149 targets := mctx.MultiTargets()
150 for _, target := range targets {
151 mctx.AddFarVariationDependencies([]blueprint.Variation{
152 {Mutator: "arch", Variation: target.String()},
153 {Mutator: "image", Variation: "core"},
154 {Mutator: "link", Variation: "shared"},
155 }, sdkMemberDepTag, m.properties.Native_shared_libs...)
156 }
157 }
158}
159
160// Step 2: record that dependencies of SDK modules are members of the SDK modules
161func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900162 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900163 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900164 if s.snapshot() && mySdkRef.Unversioned() {
165 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
166 "Did you manually modify Android.bp?")
167 }
168 if !s.snapshot() && !mySdkRef.Unversioned() {
169 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
170 }
171 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
172 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
173 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
174 }
175 }
176
Jiyong Parkd1063c12019-07-17 20:08:41 +0900177 mctx.VisitDirectDeps(func(child android.Module) {
178 if member, ok := child.(android.SdkAware); ok {
179 member.MakeMemberOf(mySdkRef)
180 }
181 })
182 }
183}
184
Jiyong Park9b409bc2019-10-11 14:59:13 +0900185// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900186// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
187// (apex and apk), each of which might want different sdks to be built with. For example, if both
188// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
189// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
190// using.
191func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
192 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900193 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900194 memberName := m.MemberName()
195 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
196 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
197 }
198 }
199}
200
201// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
202// descendants
203func sdkDepsMutator(mctx android.TopDownMutatorContext) {
204 if m, ok := mctx.Module().(android.SdkAware); ok {
205 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
206 // by reading its own properties like `uses_sdks`.
207 requiredSdks := m.RequiredSdks()
208 if len(requiredSdks) > 0 {
209 mctx.VisitDirectDeps(func(m android.Module) {
210 if dep, ok := m.(android.SdkAware); ok {
211 dep.BuildWithSdks(requiredSdks)
212 }
213 })
214 }
215 }
216}
217
218// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
219// versioned module is used instead of the un-versioned (in-development) module libfoo
220func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
221 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900222 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900223 if m.RequiredSdks().Contains(sdk) {
224 // Note that this replacement is done only for the modules that have the same
225 // variations as the current module. Since current module is already mutated for
226 // apex references in other APEXes are not affected by this replacement.
227 memberName := m.MemberName()
228 mctx.ReplaceDependencies(memberName)
229 }
230 }
231 }
232}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900233
234// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
235func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
236 if m, ok := mctx.Module().(interface {
237 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
238 RequiredSdks() android.SdkRefs
239 }); ok {
240 requiredSdks := m.RequiredSdks()
241 if len(requiredSdks) == 0 {
242 return
243 }
244 mctx.VisitDirectDeps(func(dep android.Module) {
245 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
246 // dependency to defaults is always okay
247 return
248 }
249
250 // If the dep is from outside of the APEX, but is not in any of the
251 // required SDKs, we know that the dep is a violation.
252 if sa, ok := dep.(android.SdkAware); ok {
253 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
254 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
255 sa.Name(), sa.ContainingSdk(), requiredSdks)
256 }
257 }
258 })
259 }
260}