blob: 3736103ffbd7bea809d8e7a9a4eda846f10b7f99 [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 (
18 "github.com/google/blueprint"
19
20 "android/soong/android"
21 // This package doesn't depend on the apex package, but import it to make its mutators to be
22 // registered before mutators in this package. See RegisterPostDepsMutators for more details.
23 _ "android/soong/apex"
24)
25
26func init() {
27 android.RegisterModuleType("sdk", ModuleFactory)
28 android.PreDepsMutators(RegisterPreDepsMutators)
29 android.PostDepsMutators(RegisterPostDepsMutators)
30}
31
32type sdk struct {
33 android.ModuleBase
34 android.DefaultableModuleBase
35
36 properties sdkProperties
37}
38
39type sdkProperties struct {
40 // The list of java_import modules that provide Java stubs for this SDK
41 Java_libs []string
42 Native_shared_libs []string
43}
44
45// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
46// which Mainline modules like APEX can choose to build with.
47func ModuleFactory() android.Module {
48 s := &sdk{}
49 s.AddProperties(&s.properties)
50 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
51 android.InitDefaultableModule(s)
52 return s
53}
54
55func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
56 // TODO(jiyong): add build rules for creating stubs from members of this SDK
57}
58
59// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
60// interface and the sdk module type. This function has been made public to be called by tests
61// outside of the sdk package
62func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
63 ctx.BottomUp("SdkMember", memberMutator).Parallel()
64 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
65 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
66}
67
68// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
69// interface and the sdk module type. This function has been made public to be called by tests
70// outside of the sdk package
71func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
72 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
73 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
74 // APEX to its dependents. Since different versions of the same SDK can be used by different
75 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
76 // should have been mutated for the apex before the SDK requirements are set.
77 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
78 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
79}
80
81type dependencyTag struct {
82 blueprint.BaseDependencyTag
83}
84
85// For dependencies from an SDK module to its members
86// e.g. mysdk -> libfoo and libbar
87var sdkMemberDepTag dependencyTag
88
89// For dependencies from an in-development version of an SDK member to frozen versions of the same member
90// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
91type sdkMemberVesionedDepTag struct {
92 dependencyTag
93 member string
94 version string
95}
96
97// Step 1: create dependencies from an SDK module to its members.
98func memberMutator(mctx android.BottomUpMutatorContext) {
99 if m, ok := mctx.Module().(*sdk); ok {
100 mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...)
101
102 targets := mctx.MultiTargets()
103 for _, target := range targets {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700104 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
Jiyong Parkd1063c12019-07-17 20:08:41 +0900105 {Mutator: "image", Variation: "core"},
106 {Mutator: "link", Variation: "shared"},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700107 }...), sdkMemberDepTag, m.properties.Native_shared_libs...)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900108 }
109 }
110}
111
112// Step 2: record that dependencies of SDK modules are members of the SDK modules
113func memberDepsMutator(mctx android.TopDownMutatorContext) {
114 if _, ok := mctx.Module().(*sdk); ok {
115 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
116 mctx.VisitDirectDeps(func(child android.Module) {
117 if member, ok := child.(android.SdkAware); ok {
118 member.MakeMemberOf(mySdkRef)
119 }
120 })
121 }
122}
123
124// Step 3: create dependencies from the in-development version of an SDK member to frozen versions
125// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
126// (apex and apk), each of which might want different sdks to be built with. For example, if both
127// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
128// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
129// using.
130func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
131 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
132 if !m.ContainingSdk().IsCurrentVersion() {
133 memberName := m.MemberName()
134 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
135 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
136 }
137 }
138}
139
140// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
141// descendants
142func sdkDepsMutator(mctx android.TopDownMutatorContext) {
143 if m, ok := mctx.Module().(android.SdkAware); ok {
144 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
145 // by reading its own properties like `uses_sdks`.
146 requiredSdks := m.RequiredSdks()
147 if len(requiredSdks) > 0 {
148 mctx.VisitDirectDeps(func(m android.Module) {
149 if dep, ok := m.(android.SdkAware); ok {
150 dep.BuildWithSdks(requiredSdks)
151 }
152 })
153 }
154 }
155}
156
157// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
158// versioned module is used instead of the un-versioned (in-development) module libfoo
159func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
160 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
161 if sdk := m.ContainingSdk(); !sdk.IsCurrentVersion() {
162 if m.RequiredSdks().Contains(sdk) {
163 // Note that this replacement is done only for the modules that have the same
164 // variations as the current module. Since current module is already mutated for
165 // apex references in other APEXes are not affected by this replacement.
166 memberName := m.MemberName()
167 mctx.ReplaceDependencies(memberName)
168 }
169 }
170 }
171}