blob: 18b0040b916ea39197290b49387e0be7fb01d0cf [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")
Paul Duffin375058f2019-11-29 20:17:53 +000034 pctx.Import("android/soong/java/config")
35
Jiyong Parkd1063c12019-07-17 20:08:41 +090036 android.RegisterModuleType("sdk", ModuleFactory)
Jiyong Park9b409bc2019-10-11 14:59:13 +090037 android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
Jiyong Parkd1063c12019-07-17 20:08:41 +090038 android.PreDepsMutators(RegisterPreDepsMutators)
39 android.PostDepsMutators(RegisterPostDepsMutators)
40}
41
42type sdk struct {
43 android.ModuleBase
44 android.DefaultableModuleBase
45
46 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090047
Jiyong Park232e7852019-11-04 12:23:40 +090048 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000049
50 // The builder, preserved for testing.
51 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090052}
53
54type sdkProperties struct {
Jiyong Park9b409bc2019-10-11 14:59:13 +090055 // The list of java libraries in this SDK
56 Java_libs []string
57 // The list of native libraries in this SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +090058 Native_shared_libs []string
Paul Duffin91547182019-11-12 19:39:36 +000059 // The list of stub sources in this SDK
60 Stubs_sources []string
Jiyong Park9b409bc2019-10-11 14:59:13 +090061
62 Snapshot bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090063}
64
65// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
66// which Mainline modules like APEX can choose to build with.
67func ModuleFactory() android.Module {
68 s := &sdk{}
69 s.AddProperties(&s.properties)
70 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
71 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +090072 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
73 type props struct {
74 Compile_multilib *string
75 }
76 p := &props{Compile_multilib: proptools.StringPtr("both")}
77 ctx.AppendProperties(p)
78 })
Jiyong Parkd1063c12019-07-17 20:08:41 +090079 return s
80}
81
Jiyong Park9b409bc2019-10-11 14:59:13 +090082// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
83func SnapshotModuleFactory() android.Module {
84 s := ModuleFactory()
85 s.(*sdk).properties.Snapshot = true
86 return s
87}
88
89func (s *sdk) snapshot() bool {
90 return s.properties.Snapshot
91}
92
Jiyong Parkd1063c12019-07-17 20:08:41 +090093func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +090094 if !s.snapshot() {
95 // We don't need to create a snapshot out of sdk_snapshot.
96 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
97 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
98 }
Jiyong Park9b409bc2019-10-11 14:59:13 +090099}
100
101func (s *sdk) AndroidMkEntries() android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900102 if !s.snapshotFile.Valid() {
103 return android.AndroidMkEntries{}
104 }
105
106 return android.AndroidMkEntries{
107 Class: "FAKE",
108 OutputFile: s.snapshotFile,
109 DistFile: s.snapshotFile,
110 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000111 ExtraFooters: []android.AndroidMkExtraFootersFunc{
112 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
113 // Allow the sdk to be built by simply passing its name on the command line.
114 fmt.Fprintln(w, ".PHONY:", s.Name())
115 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
116 },
117 },
Jiyong Park232e7852019-11-04 12:23:40 +0900118 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900119}
120
121// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
122// interface and the sdk module type. This function has been made public to be called by tests
123// outside of the sdk package
124func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
125 ctx.BottomUp("SdkMember", memberMutator).Parallel()
126 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
127 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
128}
129
130// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
131// interface and the sdk module type. This function has been made public to be called by tests
132// outside of the sdk package
133func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
134 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
135 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
136 // APEX to its dependents. Since different versions of the same SDK can be used by different
137 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
138 // should have been mutated for the apex before the SDK requirements are set.
139 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
140 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900141 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900142}
143
144type dependencyTag struct {
145 blueprint.BaseDependencyTag
146}
147
148// For dependencies from an SDK module to its members
149// e.g. mysdk -> libfoo and libbar
150var sdkMemberDepTag dependencyTag
151
152// For dependencies from an in-development version of an SDK member to frozen versions of the same member
153// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
154type sdkMemberVesionedDepTag struct {
155 dependencyTag
156 member string
157 version string
158}
159
160// Step 1: create dependencies from an SDK module to its members.
161func memberMutator(mctx android.BottomUpMutatorContext) {
162 if m, ok := mctx.Module().(*sdk); ok {
163 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...)
Paul Duffin91547182019-11-12 19:39:36 +0000164 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Stubs_sources...)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900165
166 targets := mctx.MultiTargets()
167 for _, target := range targets {
Jiyong Park73c54ee2019-10-22 20:31:18 +0900168 for _, lib := range m.properties.Native_shared_libs {
169 name, version := cc.StubsLibNameAndVersion(lib)
170 if version == "" {
171 version = cc.LatestStubsVersionFor(mctx.Config(), name)
172 }
173 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Colin Cross7228ecd2019-11-18 16:00:16 -0800174 {Mutator: "image", Variation: android.CoreVariation},
Jiyong Park73c54ee2019-10-22 20:31:18 +0900175 {Mutator: "link", Variation: "shared"},
176 {Mutator: "version", Variation: version},
177 }...), sdkMemberDepTag, name)
178 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900179 }
180 }
181}
182
183// Step 2: record that dependencies of SDK modules are members of the SDK modules
184func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900185 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900186 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900187 if s.snapshot() && mySdkRef.Unversioned() {
188 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
189 "Did you manually modify Android.bp?")
190 }
191 if !s.snapshot() && !mySdkRef.Unversioned() {
192 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
193 }
194 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
195 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
196 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
197 }
198 }
199
Jiyong Parkd1063c12019-07-17 20:08:41 +0900200 mctx.VisitDirectDeps(func(child android.Module) {
201 if member, ok := child.(android.SdkAware); ok {
202 member.MakeMemberOf(mySdkRef)
203 }
204 })
205 }
206}
207
Jiyong Park9b409bc2019-10-11 14:59:13 +0900208// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900209// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
210// (apex and apk), each of which might want different sdks to be built with. For example, if both
211// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
212// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
213// using.
214func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
215 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900216 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900217 memberName := m.MemberName()
218 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
219 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
220 }
221 }
222}
223
224// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
225// descendants
226func sdkDepsMutator(mctx android.TopDownMutatorContext) {
227 if m, ok := mctx.Module().(android.SdkAware); ok {
228 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
229 // by reading its own properties like `uses_sdks`.
230 requiredSdks := m.RequiredSdks()
231 if len(requiredSdks) > 0 {
232 mctx.VisitDirectDeps(func(m android.Module) {
233 if dep, ok := m.(android.SdkAware); ok {
234 dep.BuildWithSdks(requiredSdks)
235 }
236 })
237 }
238 }
239}
240
241// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
242// versioned module is used instead of the un-versioned (in-development) module libfoo
243func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
244 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900245 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900246 if m.RequiredSdks().Contains(sdk) {
247 // Note that this replacement is done only for the modules that have the same
248 // variations as the current module. Since current module is already mutated for
249 // apex references in other APEXes are not affected by this replacement.
250 memberName := m.MemberName()
251 mctx.ReplaceDependencies(memberName)
252 }
253 }
254 }
255}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900256
257// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
258func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
259 if m, ok := mctx.Module().(interface {
260 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
261 RequiredSdks() android.SdkRefs
262 }); ok {
263 requiredSdks := m.RequiredSdks()
264 if len(requiredSdks) == 0 {
265 return
266 }
267 mctx.VisitDirectDeps(func(dep android.Module) {
268 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
269 // dependency to defaults is always okay
270 return
271 }
272
273 // If the dep is from outside of the APEX, but is not in any of the
274 // required SDKs, we know that the dep is a violation.
275 if sa, ok := dep.(android.SdkAware); ok {
276 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
277 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
278 sa.Name(), sa.ContainingSdk(), requiredSdks)
279 }
280 }
281 })
282 }
283}