blob: 321d5ab2c2600d68612fc6f8e8b10a7c0fa0501c [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
91func (s *sdk) frozenVersions(ctx android.BaseModuleContext) []string {
92 if s.snapshot() {
93 panic(fmt.Errorf("frozenVersions() called for sdk_snapshot %q", ctx.ModuleName()))
94 }
95 versions := []string{}
96 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
97 depTag := ctx.OtherModuleDependencyTag(child)
98 if depTag == sdkMemberDepTag {
99 return true
100 }
101 if versionedDepTag, ok := depTag.(sdkMemberVesionedDepTag); ok {
102 v := versionedDepTag.version
103 if v != "current" && !android.InList(v, versions) {
104 versions = append(versions, versionedDepTag.version)
105 }
106 }
107 return false
108 })
109 return android.SortedUniqueStrings(versions)
110}
111
Jiyong Parkd1063c12019-07-17 20:08:41 +0900112func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900113 if !s.snapshot() {
114 // We don't need to create a snapshot out of sdk_snapshot.
115 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
116 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
117 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900118}
119
120func (s *sdk) AndroidMkEntries() android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900121 if !s.snapshotFile.Valid() {
122 return android.AndroidMkEntries{}
123 }
124
125 return android.AndroidMkEntries{
126 Class: "FAKE",
127 OutputFile: s.snapshotFile,
128 DistFile: s.snapshotFile,
129 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000130 ExtraFooters: []android.AndroidMkExtraFootersFunc{
131 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
132 // Allow the sdk to be built by simply passing its name on the command line.
133 fmt.Fprintln(w, ".PHONY:", s.Name())
134 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
135 },
136 },
Jiyong Park232e7852019-11-04 12:23:40 +0900137 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900138}
139
140// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
141// interface and the sdk module type. This function has been made public to be called by tests
142// outside of the sdk package
143func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
144 ctx.BottomUp("SdkMember", memberMutator).Parallel()
145 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
146 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
147}
148
149// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
150// interface and the sdk module type. This function has been made public to be called by tests
151// outside of the sdk package
152func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
153 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
154 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
155 // APEX to its dependents. Since different versions of the same SDK can be used by different
156 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
157 // should have been mutated for the apex before the SDK requirements are set.
158 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
159 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900160 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900161}
162
163type dependencyTag struct {
164 blueprint.BaseDependencyTag
165}
166
167// For dependencies from an SDK module to its members
168// e.g. mysdk -> libfoo and libbar
169var sdkMemberDepTag dependencyTag
170
171// For dependencies from an in-development version of an SDK member to frozen versions of the same member
172// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
173type sdkMemberVesionedDepTag struct {
174 dependencyTag
175 member string
176 version string
177}
178
179// Step 1: create dependencies from an SDK module to its members.
180func memberMutator(mctx android.BottomUpMutatorContext) {
181 if m, ok := mctx.Module().(*sdk); ok {
182 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...)
Paul Duffin91547182019-11-12 19:39:36 +0000183 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Stubs_sources...)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900184
185 targets := mctx.MultiTargets()
186 for _, target := range targets {
Jiyong Park73c54ee2019-10-22 20:31:18 +0900187 for _, lib := range m.properties.Native_shared_libs {
188 name, version := cc.StubsLibNameAndVersion(lib)
189 if version == "" {
190 version = cc.LatestStubsVersionFor(mctx.Config(), name)
191 }
192 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Colin Cross7228ecd2019-11-18 16:00:16 -0800193 {Mutator: "image", Variation: android.CoreVariation},
Jiyong Park73c54ee2019-10-22 20:31:18 +0900194 {Mutator: "link", Variation: "shared"},
195 {Mutator: "version", Variation: version},
196 }...), sdkMemberDepTag, name)
197 }
Jiyong Parkd1063c12019-07-17 20:08:41 +0900198 }
199 }
200}
201
202// Step 2: record that dependencies of SDK modules are members of the SDK modules
203func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900204 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900205 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900206 if s.snapshot() && mySdkRef.Unversioned() {
207 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
208 "Did you manually modify Android.bp?")
209 }
210 if !s.snapshot() && !mySdkRef.Unversioned() {
211 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
212 }
213 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
214 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
215 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
216 }
217 }
218
Jiyong Parkd1063c12019-07-17 20:08:41 +0900219 mctx.VisitDirectDeps(func(child android.Module) {
220 if member, ok := child.(android.SdkAware); ok {
221 member.MakeMemberOf(mySdkRef)
222 }
223 })
224 }
225}
226
Jiyong Park9b409bc2019-10-11 14:59:13 +0900227// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900228// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
229// (apex and apk), each of which might want different sdks to be built with. For example, if both
230// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
231// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
232// using.
233func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
234 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900235 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900236 memberName := m.MemberName()
237 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
238 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
239 }
240 }
241}
242
243// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
244// descendants
245func sdkDepsMutator(mctx android.TopDownMutatorContext) {
246 if m, ok := mctx.Module().(android.SdkAware); ok {
247 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
248 // by reading its own properties like `uses_sdks`.
249 requiredSdks := m.RequiredSdks()
250 if len(requiredSdks) > 0 {
251 mctx.VisitDirectDeps(func(m android.Module) {
252 if dep, ok := m.(android.SdkAware); ok {
253 dep.BuildWithSdks(requiredSdks)
254 }
255 })
256 }
257 }
258}
259
260// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
261// versioned module is used instead of the un-versioned (in-development) module libfoo
262func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
263 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900264 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900265 if m.RequiredSdks().Contains(sdk) {
266 // Note that this replacement is done only for the modules that have the same
267 // variations as the current module. Since current module is already mutated for
268 // apex references in other APEXes are not affected by this replacement.
269 memberName := m.MemberName()
270 mctx.ReplaceDependencies(memberName)
271 }
272 }
273 }
274}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900275
276// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
277func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
278 if m, ok := mctx.Module().(interface {
279 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
280 RequiredSdks() android.SdkRefs
281 }); ok {
282 requiredSdks := m.RequiredSdks()
283 if len(requiredSdks) == 0 {
284 return
285 }
286 mctx.VisitDirectDeps(func(dep android.Module) {
287 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
288 // dependency to defaults is always okay
289 return
290 }
291
292 // If the dep is from outside of the APEX, but is not in any of the
293 // required SDKs, we know that the dep is a violation.
294 if sa, ok := dep.(android.SdkAware); ok {
295 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
296 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
297 sa.Name(), sa.ContainingSdk(), requiredSdks)
298 }
299 }
300 })
301 }
302}