blob: 431ace9a52e3830d2db459c462742dbe85138985 [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
Paul Duffinac37c502019-11-26 18:02:20 +000047
48 // The builder, preserved for testing.
49 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090050}
51
52type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090053 // The list of java libraries in this SDK
54 Java_libs []string
55 // The list of native libraries in this SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +090056 Native_shared_libs []string
Paul Duffin91547182019-11-12 19:39:36 +000057 // The list of stub sources in this SDK
58 Stubs_sources []string
Jiyong Park9b409bc2019-10-11 14:59:13 +090059
60 Snapshot bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090061}
62
63// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
64// which Mainline modules like APEX can choose to build with.
65func ModuleFactory() android.Module {
66 s := &sdk{}
67 s.AddProperties(&s.properties)
68 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
69 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +090070 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
71 type props struct {
72 Compile_multilib *string
73 }
74 p := &props{Compile_multilib: proptools.StringPtr("both")}
75 ctx.AppendProperties(p)
76 })
Jiyong Parkd1063c12019-07-17 20:08:41 +090077 return s
78}
79
Jiyong Park9b409bc2019-10-11 14:59:13 +090080// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
81func SnapshotModuleFactory() android.Module {
82 s := ModuleFactory()
83 s.(*sdk).properties.Snapshot = true
84 return s
85}
86
87func (s *sdk) snapshot() bool {
88 return s.properties.Snapshot
89}
90
Jiyong Parkd1063c12019-07-17 20:08:41 +090091func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +090092 if !s.snapshot() {
93 // We don't need to create a snapshot out of sdk_snapshot.
94 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
95 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
96 }
Jiyong Park9b409bc2019-10-11 14:59:13 +090097}
98
99func (s *sdk) AndroidMkEntries() android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900100 if !s.snapshotFile.Valid() {
101 return android.AndroidMkEntries{}
102 }
103
104 return android.AndroidMkEntries{
105 Class: "FAKE",
106 OutputFile: s.snapshotFile,
107 DistFile: s.snapshotFile,
108 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000109 ExtraFooters: []android.AndroidMkExtraFootersFunc{
110 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
111 // Allow the sdk to be built by simply passing its name on the command line.
112 fmt.Fprintln(w, ".PHONY:", s.Name())
113 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
114 },
115 },
Jiyong Park232e7852019-11-04 12:23:40 +0900116 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900117}
118
119// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
120// interface and the sdk module type. This function has been made public to be called by tests
121// outside of the sdk package
122func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
123 ctx.BottomUp("SdkMember", memberMutator).Parallel()
124 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
125 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
126}
127
128// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
129// interface and the sdk module type. This function has been made public to be called by tests
130// outside of the sdk package
131func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
132 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
133 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
134 // APEX to its dependents. Since different versions of the same SDK can be used by different
135 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
136 // should have been mutated for the apex before the SDK requirements are set.
137 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
138 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900139 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900140}
141
142type dependencyTag struct {
143 blueprint.BaseDependencyTag
144}
145
146// For dependencies from an SDK module to its members
147// e.g. mysdk -> libfoo and libbar
148var sdkMemberDepTag dependencyTag
149
150// For dependencies from an in-development version of an SDK member to frozen versions of the same member
151// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
152type sdkMemberVesionedDepTag struct {
153 dependencyTag
154 member string
155 version string
156}
157
158// Step 1: create dependencies from an SDK module to its members.
159func memberMutator(mctx android.BottomUpMutatorContext) {
160 if m, ok := mctx.Module().(*sdk); ok {
161 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...)
Paul Duffin91547182019-11-12 19:39:36 +0000162 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Stubs_sources...)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900163
164 targets := mctx.MultiTargets()
165 for _, target := range targets {
Jiyong Park73c54ee2019-10-22 20:31:18 +0900166 for _, lib := range m.properties.Native_shared_libs {
167 name, version := cc.StubsLibNameAndVersion(lib)
168 if version == "" {
169 version = cc.LatestStubsVersionFor(mctx.Config(), name)
170 }
171 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Colin Cross7228ecd2019-11-18 16:00:16 -0800172 {Mutator: "image", Variation: android.CoreVariation},
Jiyong Park73c54ee2019-10-22 20:31:18 +0900173 {Mutator: "link", Variation: "shared"},
174 {Mutator: "version", Variation: version},
175 }...), sdkMemberDepTag, name)
176 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900177 }
178 }
179}
180
181// Step 2: record that dependencies of SDK modules are members of the SDK modules
182func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900183 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900184 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900185 if s.snapshot() && mySdkRef.Unversioned() {
186 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
187 "Did you manually modify Android.bp?")
188 }
189 if !s.snapshot() && !mySdkRef.Unversioned() {
190 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
191 }
192 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
193 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
194 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
195 }
196 }
197
Jiyong Parkd1063c12019-07-17 20:08:41 +0900198 mctx.VisitDirectDeps(func(child android.Module) {
199 if member, ok := child.(android.SdkAware); ok {
200 member.MakeMemberOf(mySdkRef)
201 }
202 })
203 }
204}
205
Jiyong Park9b409bc2019-10-11 14:59:13 +0900206// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900207// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
208// (apex and apk), each of which might want different sdks to be built with. For example, if both
209// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
210// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
211// using.
212func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
213 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900214 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900215 memberName := m.MemberName()
216 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
217 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
218 }
219 }
220}
221
222// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
223// descendants
224func sdkDepsMutator(mctx android.TopDownMutatorContext) {
225 if m, ok := mctx.Module().(android.SdkAware); ok {
226 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
227 // by reading its own properties like `uses_sdks`.
228 requiredSdks := m.RequiredSdks()
229 if len(requiredSdks) > 0 {
230 mctx.VisitDirectDeps(func(m android.Module) {
231 if dep, ok := m.(android.SdkAware); ok {
232 dep.BuildWithSdks(requiredSdks)
233 }
234 })
235 }
236 }
237}
238
239// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
240// versioned module is used instead of the un-versioned (in-development) module libfoo
241func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
242 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900243 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900244 if m.RequiredSdks().Contains(sdk) {
245 // Note that this replacement is done only for the modules that have the same
246 // variations as the current module. Since current module is already mutated for
247 // apex references in other APEXes are not affected by this replacement.
248 memberName := m.MemberName()
249 mctx.ReplaceDependencies(memberName)
250 }
251 }
252 }
253}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900254
255// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
256func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
257 if m, ok := mctx.Module().(interface {
258 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
259 RequiredSdks() android.SdkRefs
260 }); ok {
261 requiredSdks := m.RequiredSdks()
262 if len(requiredSdks) == 0 {
263 return
264 }
265 mctx.VisitDirectDeps(func(dep android.Module) {
266 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
267 // dependency to defaults is always okay
268 return
269 }
270
271 // If the dep is from outside of the APEX, but is not in any of the
272 // required SDKs, we know that the dep is a violation.
273 if sa, ok := dep.(android.SdkAware); ok {
274 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
275 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
276 sa.Name(), sa.ContainingSdk(), requiredSdks)
277 }
278 }
279 })
280 }
281}