blob: c25186a823b09af6e1991745d17493c6d7f5f692 [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"
Paul Duffin13879572019-11-28 14:31:38 +000030 "android/soong/java"
Jiyong Parkd1063c12019-07-17 20:08:41 +090031)
32
33func init() {
Jiyong Park232e7852019-11-04 12:23:40 +090034 pctx.Import("android/soong/android")
Paul Duffin375058f2019-11-29 20:17:53 +000035 pctx.Import("android/soong/java/config")
36
Jiyong Parkd1063c12019-07-17 20:08:41 +090037 android.RegisterModuleType("sdk", ModuleFactory)
Jiyong Park9b409bc2019-10-11 14:59:13 +090038 android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
Jiyong Parkd1063c12019-07-17 20:08:41 +090039 android.PreDepsMutators(RegisterPreDepsMutators)
40 android.PostDepsMutators(RegisterPostDepsMutators)
Paul Duffin13879572019-11-28 14:31:38 +000041
42 // Populate the dependency tags for each member list property. This needs to
43 // be done here to break an initialization cycle.
44 for _, memberListProperty := range sdkMemberListProperties {
45 memberListProperty.dependencyTag = &sdkMemberDependencyTag{
46 memberListProperty: memberListProperty,
47 }
48 }
Jiyong Parkd1063c12019-07-17 20:08:41 +090049}
50
51type sdk struct {
52 android.ModuleBase
53 android.DefaultableModuleBase
54
55 properties sdkProperties
Jiyong Park9b409bc2019-10-11 14:59:13 +090056
Jiyong Park232e7852019-11-04 12:23:40 +090057 snapshotFile android.OptionalPath
Paul Duffinac37c502019-11-26 18:02:20 +000058
59 // The builder, preserved for testing.
60 builderForTests *snapshotBuilder
Jiyong Parkd1063c12019-07-17 20:08:41 +090061}
62
63type sdkProperties struct {
Paul Duffina0dbf432019-12-05 11:25:53 +000064 // The list of java header libraries in this SDK
65 //
66 // This should be used for java libraries that are provided separately at runtime,
67 // e.g. through an APEX.
68 Java_header_libs []string
69 // The list of java implementation libraries in this SDK
Jiyong Park9b409bc2019-10-11 14:59:13 +090070 Java_libs []string
71 // The list of native libraries in this SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +090072 Native_shared_libs []string
Paul Duffin91547182019-11-12 19:39:36 +000073 // The list of stub sources in this SDK
74 Stubs_sources []string
Jiyong Park9b409bc2019-10-11 14:59:13 +090075
76 Snapshot bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090077}
78
Paul Duffin13879572019-11-28 14:31:38 +000079type sdkMemberDependencyTag struct {
80 blueprint.BaseDependencyTag
81 memberListProperty *sdkMemberListProperty
82}
83
84// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000085// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +000086type sdkMemberListProperty struct {
87 // the name of the property as used in a .bp file
88 name string
89
90 // getter for the list of member names
91 getter func(properties *sdkProperties) []string
92
93 // the type of member referenced in the list
94 memberType android.SdkMemberType
95
96 // the dependency tag used for items in this list.
97 dependencyTag *sdkMemberDependencyTag
98}
99
Paul Duffina6e737b2019-11-29 11:55:51 +0000100// Information about how to handle each member list property.
101//
102// It is organized first by package and then by name within the package.
103// Packages are in alphabetical order and properties are in alphabetical order
104// within each package.
Paul Duffin13879572019-11-28 14:31:38 +0000105var sdkMemberListProperties = []*sdkMemberListProperty{
Paul Duffina6e737b2019-11-29 11:55:51 +0000106 // Members from cc package.
107 {
108 name: "native_shared_libs",
109 getter: func(properties *sdkProperties) []string { return properties.Native_shared_libs },
110 memberType: cc.LibrarySdkMemberType,
111 },
112 // Members from java package.
Paul Duffin13879572019-11-28 14:31:38 +0000113 {
Paul Duffina0dbf432019-12-05 11:25:53 +0000114 name: "java_header_libs",
115 getter: func(properties *sdkProperties) []string { return properties.Java_header_libs },
116 memberType: java.HeaderLibrarySdkMemberType,
117 },
118 {
Paul Duffin13879572019-11-28 14:31:38 +0000119 name: "java_libs",
120 getter: func(properties *sdkProperties) []string { return properties.Java_libs },
Paul Duffina0dbf432019-12-05 11:25:53 +0000121 memberType: java.ImplLibrarySdkMemberType,
Paul Duffin13879572019-11-28 14:31:38 +0000122 },
123 {
124 name: "stubs_sources",
125 getter: func(properties *sdkProperties) []string { return properties.Stubs_sources },
126 memberType: java.DroidStubsSdkMemberType,
127 },
Paul Duffin13879572019-11-28 14:31:38 +0000128}
129
Jiyong Parkd1063c12019-07-17 20:08:41 +0900130// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
131// which Mainline modules like APEX can choose to build with.
132func ModuleFactory() android.Module {
133 s := &sdk{}
134 s.AddProperties(&s.properties)
135 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
136 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900137 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
138 type props struct {
139 Compile_multilib *string
140 }
141 p := &props{Compile_multilib: proptools.StringPtr("both")}
142 ctx.AppendProperties(p)
143 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900144 return s
145}
146
Jiyong Park9b409bc2019-10-11 14:59:13 +0900147// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
148func SnapshotModuleFactory() android.Module {
149 s := ModuleFactory()
150 s.(*sdk).properties.Snapshot = true
151 return s
152}
153
154func (s *sdk) snapshot() bool {
155 return s.properties.Snapshot
156}
157
Jiyong Parkd1063c12019-07-17 20:08:41 +0900158func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900159 if !s.snapshot() {
160 // We don't need to create a snapshot out of sdk_snapshot.
161 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
162 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
163 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900164}
165
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900166func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900167 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900168 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900169 }
170
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900171 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900172 Class: "FAKE",
173 OutputFile: s.snapshotFile,
174 DistFile: s.snapshotFile,
175 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000176 ExtraFooters: []android.AndroidMkExtraFootersFunc{
177 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
178 // Allow the sdk to be built by simply passing its name on the command line.
179 fmt.Fprintln(w, ".PHONY:", s.Name())
180 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
181 },
182 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900183 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900184}
185
186// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
187// interface and the sdk module type. This function has been made public to be called by tests
188// outside of the sdk package
189func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
190 ctx.BottomUp("SdkMember", memberMutator).Parallel()
191 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
192 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
193}
194
195// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
196// interface and the sdk module type. This function has been made public to be called by tests
197// outside of the sdk package
198func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
199 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
200 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
201 // APEX to its dependents. Since different versions of the same SDK can be used by different
202 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
203 // should have been mutated for the apex before the SDK requirements are set.
204 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
205 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900206 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900207}
208
209type dependencyTag struct {
210 blueprint.BaseDependencyTag
211}
212
Jiyong Parkd1063c12019-07-17 20:08:41 +0900213// For dependencies from an in-development version of an SDK member to frozen versions of the same member
214// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
215type sdkMemberVesionedDepTag struct {
216 dependencyTag
217 member string
218 version string
219}
220
221// Step 1: create dependencies from an SDK module to its members.
222func memberMutator(mctx android.BottomUpMutatorContext) {
223 if m, ok := mctx.Module().(*sdk); ok {
Paul Duffin13879572019-11-28 14:31:38 +0000224 for _, memberListProperty := range sdkMemberListProperties {
225 names := memberListProperty.getter(&m.properties)
226 tag := memberListProperty.dependencyTag
227 memberListProperty.memberType.AddDependencies(mctx, tag, names)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900228 }
229 }
230}
231
232// Step 2: record that dependencies of SDK modules are members of the SDK modules
233func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900234 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900235 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900236 if s.snapshot() && mySdkRef.Unversioned() {
237 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
238 "Did you manually modify Android.bp?")
239 }
240 if !s.snapshot() && !mySdkRef.Unversioned() {
241 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
242 }
243 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
244 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
245 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
246 }
247 }
248
Jiyong Parkd1063c12019-07-17 20:08:41 +0900249 mctx.VisitDirectDeps(func(child android.Module) {
250 if member, ok := child.(android.SdkAware); ok {
251 member.MakeMemberOf(mySdkRef)
252 }
253 })
254 }
255}
256
Jiyong Park9b409bc2019-10-11 14:59:13 +0900257// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900258// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
259// (apex and apk), each of which might want different sdks to be built with. For example, if both
260// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
261// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
262// using.
263func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
264 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900265 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900266 memberName := m.MemberName()
267 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
268 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
269 }
270 }
271}
272
273// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
274// descendants
275func sdkDepsMutator(mctx android.TopDownMutatorContext) {
276 if m, ok := mctx.Module().(android.SdkAware); ok {
277 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
278 // by reading its own properties like `uses_sdks`.
279 requiredSdks := m.RequiredSdks()
280 if len(requiredSdks) > 0 {
281 mctx.VisitDirectDeps(func(m android.Module) {
282 if dep, ok := m.(android.SdkAware); ok {
283 dep.BuildWithSdks(requiredSdks)
284 }
285 })
286 }
287 }
288}
289
290// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
291// versioned module is used instead of the un-versioned (in-development) module libfoo
292func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
293 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900294 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900295 if m.RequiredSdks().Contains(sdk) {
296 // Note that this replacement is done only for the modules that have the same
297 // variations as the current module. Since current module is already mutated for
298 // apex references in other APEXes are not affected by this replacement.
299 memberName := m.MemberName()
300 mctx.ReplaceDependencies(memberName)
301 }
302 }
303 }
304}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900305
306// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
307func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
308 if m, ok := mctx.Module().(interface {
309 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
310 RequiredSdks() android.SdkRefs
311 }); ok {
312 requiredSdks := m.RequiredSdks()
313 if len(requiredSdks) == 0 {
314 return
315 }
316 mctx.VisitDirectDeps(func(dep android.Module) {
317 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
318 // dependency to defaults is always okay
319 return
320 }
321
322 // If the dep is from outside of the APEX, but is not in any of the
323 // required SDKs, we know that the dep is a violation.
324 if sa, ok := dep.(android.SdkAware); ok {
325 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
326 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
327 sa.Name(), sa.ContainingSdk(), requiredSdks)
328 }
329 }
330 })
331 }
332}