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