blob: ed2f26c91defc9e7d0199bbf7f12aad614879c16 [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"
Jiyong Parkd1063c12019-07-17 20:08:41 +090030)
31
32func init() {
Jiyong Park232e7852019-11-04 12:23:40 +090033 pctx.Import("android/soong/android")
Jiyong Parkd1063c12019-07-17 20:08:41 +090034 android.RegisterModuleType("sdk", ModuleFactory)
Jiyong Park9b409bc2019-10-11 14:59:13 +090035 android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
Jiyong Parkd1063c12019-07-17 20:08:41 +090036 android.PreDepsMutators(RegisterPreDepsMutators)
37 android.PostDepsMutators(RegisterPostDepsMutators)
38}
39
40type sdk struct {
41 android.ModuleBase
42 android.DefaultableModuleBase
43
44 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090045
Jiyong Park232e7852019-11-04 12:23:40 +090046 snapshotFile android.OptionalPath
Jiyong Parkd1063c12019-07-17 20:08:41 +090047}
48
49type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090050 // The list of java libraries in this SDK
51 Java_libs []string
52 // The list of native libraries in this SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +090053 Native_shared_libs []string
Paul Duffin91547182019-11-12 19:39:36 +000054 // The list of stub sources in this SDK
55 Stubs_sources []string
Jiyong Park9b409bc2019-10-11 14:59:13 +090056
57 Snapshot bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090058}
59
60// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
61// which Mainline modules like APEX can choose to build with.
62func ModuleFactory() android.Module {
63 s := &sdk{}
64 s.AddProperties(&s.properties)
65 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
66 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +090067 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
68 type props struct {
69 Compile_multilib *string
70 }
71 p := &props{Compile_multilib: proptools.StringPtr("both")}
72 ctx.AppendProperties(p)
73 })
Jiyong Parkd1063c12019-07-17 20:08:41 +090074 return s
75}
76
Jiyong Park9b409bc2019-10-11 14:59:13 +090077// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
78func SnapshotModuleFactory() android.Module {
79 s := ModuleFactory()
80 s.(*sdk).properties.Snapshot = true
81 return s
82}
83
84func (s *sdk) snapshot() bool {
85 return s.properties.Snapshot
86}
87
88func (s *sdk) frozenVersions(ctx android.BaseModuleContext) []string {
89 if s.snapshot() {
90 panic(fmt.Errorf("frozenVersions() called for sdk_snapshot %q", ctx.ModuleName()))
91 }
92 versions := []string{}
93 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
94 depTag := ctx.OtherModuleDependencyTag(child)
95 if depTag == sdkMemberDepTag {
96 return true
97 }
98 if versionedDepTag, ok := depTag.(sdkMemberVesionedDepTag); ok {
99 v := versionedDepTag.version
100 if v != "current" && !android.InList(v, versions) {
101 versions = append(versions, versionedDepTag.version)
102 }
103 }
104 return false
105 })
106 return android.SortedUniqueStrings(versions)
107}
108
Jiyong Parkd1063c12019-07-17 20:08:41 +0900109func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900110 if !s.snapshot() {
111 // We don't need to create a snapshot out of sdk_snapshot.
112 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
113 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
114 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900115}
116
117func (s *sdk) AndroidMkEntries() android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900118 if !s.snapshotFile.Valid() {
119 return android.AndroidMkEntries{}
120 }
121
122 return android.AndroidMkEntries{
123 Class: "FAKE",
124 OutputFile: s.snapshotFile,
125 DistFile: s.snapshotFile,
126 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000127 ExtraFooters: []android.AndroidMkExtraFootersFunc{
128 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
129 // Allow the sdk to be built by simply passing its name on the command line.
130 fmt.Fprintln(w, ".PHONY:", s.Name())
131 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
132 },
133 },
Jiyong Park232e7852019-11-04 12:23:40 +0900134 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900135}
136
137// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
138// interface and the sdk module type. This function has been made public to be called by tests
139// outside of the sdk package
140func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
141 ctx.BottomUp("SdkMember", memberMutator).Parallel()
142 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
143 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
144}
145
146// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
147// interface and the sdk module type. This function has been made public to be called by tests
148// outside of the sdk package
149func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
150 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
151 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
152 // APEX to its dependents. Since different versions of the same SDK can be used by different
153 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
154 // should have been mutated for the apex before the SDK requirements are set.
155 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
156 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900157 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900158}
159
160type dependencyTag struct {
161 blueprint.BaseDependencyTag
162}
163
164// For dependencies from an SDK module to its members
165// e.g. mysdk -> libfoo and libbar
166var sdkMemberDepTag dependencyTag
167
168// For dependencies from an in-development version of an SDK member to frozen versions of the same member
169// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
170type sdkMemberVesionedDepTag struct {
171 dependencyTag
172 member string
173 version string
174}
175
176// Step 1: create dependencies from an SDK module to its members.
177func memberMutator(mctx android.BottomUpMutatorContext) {
178 if m, ok := mctx.Module().(*sdk); ok {
179 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...)
Paul Duffin91547182019-11-12 19:39:36 +0000180 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Stubs_sources...)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900181
182 targets := mctx.MultiTargets()
183 for _, target := range targets {
Jiyong Park73c54ee2019-10-22 20:31:18 +0900184 for _, lib := range m.properties.Native_shared_libs {
185 name, version := cc.StubsLibNameAndVersion(lib)
186 if version == "" {
187 version = cc.LatestStubsVersionFor(mctx.Config(), name)
188 }
189 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
190 {Mutator: "image", Variation: "core"},
191 {Mutator: "link", Variation: "shared"},
192 {Mutator: "version", Variation: version},
193 }...), sdkMemberDepTag, name)
194 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900195 }
196 }
197}
198
199// Step 2: record that dependencies of SDK modules are members of the SDK modules
200func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900201 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900202 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900203 if s.snapshot() && mySdkRef.Unversioned() {
204 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
205 "Did you manually modify Android.bp?")
206 }
207 if !s.snapshot() && !mySdkRef.Unversioned() {
208 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
209 }
210 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
211 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
212 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
213 }
214 }
215
Jiyong Parkd1063c12019-07-17 20:08:41 +0900216 mctx.VisitDirectDeps(func(child android.Module) {
217 if member, ok := child.(android.SdkAware); ok {
218 member.MakeMemberOf(mySdkRef)
219 }
220 })
221 }
222}
223
Jiyong Park9b409bc2019-10-11 14:59:13 +0900224// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900225// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
226// (apex and apk), each of which might want different sdks to be built with. For example, if both
227// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
228// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
229// using.
230func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
231 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900232 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900233 memberName := m.MemberName()
234 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
235 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
236 }
237 }
238}
239
240// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
241// descendants
242func sdkDepsMutator(mctx android.TopDownMutatorContext) {
243 if m, ok := mctx.Module().(android.SdkAware); ok {
244 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
245 // by reading its own properties like `uses_sdks`.
246 requiredSdks := m.RequiredSdks()
247 if len(requiredSdks) > 0 {
248 mctx.VisitDirectDeps(func(m android.Module) {
249 if dep, ok := m.(android.SdkAware); ok {
250 dep.BuildWithSdks(requiredSdks)
251 }
252 })
253 }
254 }
255}
256
257// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
258// versioned module is used instead of the un-versioned (in-development) module libfoo
259func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
260 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900261 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900262 if m.RequiredSdks().Contains(sdk) {
263 // Note that this replacement is done only for the modules that have the same
264 // variations as the current module. Since current module is already mutated for
265 // apex references in other APEXes are not affected by this replacement.
266 memberName := m.MemberName()
267 mctx.ReplaceDependencies(memberName)
268 }
269 }
270 }
271}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900272
273// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
274func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
275 if m, ok := mctx.Module().(interface {
276 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
277 RequiredSdks() android.SdkRefs
278 }); ok {
279 requiredSdks := m.RequiredSdks()
280 if len(requiredSdks) == 0 {
281 return
282 }
283 mctx.VisitDirectDeps(func(dep android.Module) {
284 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
285 // dependency to defaults is always okay
286 return
287 }
288
289 // If the dep is from outside of the APEX, but is not in any of the
290 // required SDKs, we know that the dep is a violation.
291 if sa, ok := dep.(android.SdkAware); ok {
292 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
293 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
294 sa.Name(), sa.ContainingSdk(), requiredSdks)
295 }
296 }
297 })
298 }
299}