blob: 8c5c8d8f67422d687c2d0518f9405b48b982edfd [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 Duffinfa028722019-12-12 11:31:59 +000064 // For module types from the cc package
65
66 // The list of native libraries in this SDK
67 Native_shared_libs []string
68
69 // For module types from the java package
70
Paul Duffina0dbf432019-12-05 11:25:53 +000071 // The list of java header libraries in this SDK
72 //
73 // This should be used for java libraries that are provided separately at runtime,
74 // e.g. through an APEX.
75 Java_header_libs []string
Paul Duffinfa028722019-12-12 11:31:59 +000076
Paul Duffina0dbf432019-12-05 11:25:53 +000077 // The list of java implementation libraries in this SDK
Jiyong Park9b409bc2019-10-11 14:59:13 +090078 Java_libs []string
Paul Duffinfa028722019-12-12 11:31:59 +000079
Paul Duffin91547182019-11-12 19:39:36 +000080 // The list of stub sources in this SDK
81 Stubs_sources []string
Jiyong Park9b409bc2019-10-11 14:59:13 +090082
83 Snapshot bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090084}
85
Paul Duffin13879572019-11-28 14:31:38 +000086type sdkMemberDependencyTag struct {
87 blueprint.BaseDependencyTag
88 memberListProperty *sdkMemberListProperty
89}
90
91// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000092// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +000093type sdkMemberListProperty struct {
94 // the name of the property as used in a .bp file
95 name string
96
97 // getter for the list of member names
98 getter func(properties *sdkProperties) []string
99
100 // the type of member referenced in the list
101 memberType android.SdkMemberType
102
103 // the dependency tag used for items in this list.
104 dependencyTag *sdkMemberDependencyTag
105}
106
Paul Duffina6e737b2019-11-29 11:55:51 +0000107// Information about how to handle each member list property.
108//
109// It is organized first by package and then by name within the package.
110// Packages are in alphabetical order and properties are in alphabetical order
111// within each package.
Paul Duffin13879572019-11-28 14:31:38 +0000112var sdkMemberListProperties = []*sdkMemberListProperty{
Paul Duffina6e737b2019-11-29 11:55:51 +0000113 // Members from cc package.
114 {
115 name: "native_shared_libs",
116 getter: func(properties *sdkProperties) []string { return properties.Native_shared_libs },
117 memberType: cc.LibrarySdkMemberType,
118 },
119 // Members from java package.
Paul Duffin13879572019-11-28 14:31:38 +0000120 {
Paul Duffina0dbf432019-12-05 11:25:53 +0000121 name: "java_header_libs",
122 getter: func(properties *sdkProperties) []string { return properties.Java_header_libs },
123 memberType: java.HeaderLibrarySdkMemberType,
124 },
125 {
Paul Duffin13879572019-11-28 14:31:38 +0000126 name: "java_libs",
127 getter: func(properties *sdkProperties) []string { return properties.Java_libs },
Paul Duffina0dbf432019-12-05 11:25:53 +0000128 memberType: java.ImplLibrarySdkMemberType,
Paul Duffin13879572019-11-28 14:31:38 +0000129 },
130 {
131 name: "stubs_sources",
132 getter: func(properties *sdkProperties) []string { return properties.Stubs_sources },
133 memberType: java.DroidStubsSdkMemberType,
134 },
Paul Duffin13879572019-11-28 14:31:38 +0000135}
136
Jiyong Parkd1063c12019-07-17 20:08:41 +0900137// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
138// which Mainline modules like APEX can choose to build with.
139func ModuleFactory() android.Module {
140 s := &sdk{}
141 s.AddProperties(&s.properties)
142 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
143 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900144 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
145 type props struct {
146 Compile_multilib *string
147 }
148 p := &props{Compile_multilib: proptools.StringPtr("both")}
149 ctx.AppendProperties(p)
150 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900151 return s
152}
153
Jiyong Park9b409bc2019-10-11 14:59:13 +0900154// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
155func SnapshotModuleFactory() android.Module {
156 s := ModuleFactory()
157 s.(*sdk).properties.Snapshot = true
158 return s
159}
160
161func (s *sdk) snapshot() bool {
162 return s.properties.Snapshot
163}
164
Jiyong Parkd1063c12019-07-17 20:08:41 +0900165func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900166 if !s.snapshot() {
167 // We don't need to create a snapshot out of sdk_snapshot.
168 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
169 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
170 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900171}
172
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900173func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900174 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900175 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900176 }
177
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900178 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900179 Class: "FAKE",
180 OutputFile: s.snapshotFile,
181 DistFile: s.snapshotFile,
182 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000183 ExtraFooters: []android.AndroidMkExtraFootersFunc{
184 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
185 // Allow the sdk to be built by simply passing its name on the command line.
186 fmt.Fprintln(w, ".PHONY:", s.Name())
187 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
188 },
189 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900190 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900191}
192
193// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
194// interface and the sdk module type. This function has been made public to be called by tests
195// outside of the sdk package
196func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
197 ctx.BottomUp("SdkMember", memberMutator).Parallel()
198 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
199 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
200}
201
202// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
203// interface and the sdk module type. This function has been made public to be called by tests
204// outside of the sdk package
205func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
206 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
207 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
208 // APEX to its dependents. Since different versions of the same SDK can be used by different
209 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
210 // should have been mutated for the apex before the SDK requirements are set.
211 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
212 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900213 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900214}
215
216type dependencyTag struct {
217 blueprint.BaseDependencyTag
218}
219
Jiyong Parkd1063c12019-07-17 20:08:41 +0900220// For dependencies from an in-development version of an SDK member to frozen versions of the same member
221// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
222type sdkMemberVesionedDepTag struct {
223 dependencyTag
224 member string
225 version string
226}
227
228// Step 1: create dependencies from an SDK module to its members.
229func memberMutator(mctx android.BottomUpMutatorContext) {
230 if m, ok := mctx.Module().(*sdk); ok {
Paul Duffin13879572019-11-28 14:31:38 +0000231 for _, memberListProperty := range sdkMemberListProperties {
232 names := memberListProperty.getter(&m.properties)
233 tag := memberListProperty.dependencyTag
234 memberListProperty.memberType.AddDependencies(mctx, tag, names)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900235 }
236 }
237}
238
239// Step 2: record that dependencies of SDK modules are members of the SDK modules
240func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900241 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900242 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900243 if s.snapshot() && mySdkRef.Unversioned() {
244 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
245 "Did you manually modify Android.bp?")
246 }
247 if !s.snapshot() && !mySdkRef.Unversioned() {
248 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
249 }
250 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
251 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
252 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
253 }
254 }
255
Jiyong Parkd1063c12019-07-17 20:08:41 +0900256 mctx.VisitDirectDeps(func(child android.Module) {
257 if member, ok := child.(android.SdkAware); ok {
258 member.MakeMemberOf(mySdkRef)
259 }
260 })
261 }
262}
263
Jiyong Park9b409bc2019-10-11 14:59:13 +0900264// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900265// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
266// (apex and apk), each of which might want different sdks to be built with. For example, if both
267// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
268// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
269// using.
270func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
271 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900272 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900273 memberName := m.MemberName()
274 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
275 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
276 }
277 }
278}
279
280// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
281// descendants
282func sdkDepsMutator(mctx android.TopDownMutatorContext) {
283 if m, ok := mctx.Module().(android.SdkAware); ok {
284 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
285 // by reading its own properties like `uses_sdks`.
286 requiredSdks := m.RequiredSdks()
287 if len(requiredSdks) > 0 {
288 mctx.VisitDirectDeps(func(m android.Module) {
289 if dep, ok := m.(android.SdkAware); ok {
290 dep.BuildWithSdks(requiredSdks)
291 }
292 })
293 }
294 }
295}
296
297// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
298// versioned module is used instead of the un-versioned (in-development) module libfoo
299func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
300 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900301 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900302 if m.RequiredSdks().Contains(sdk) {
303 // Note that this replacement is done only for the modules that have the same
304 // variations as the current module. Since current module is already mutated for
305 // apex references in other APEXes are not affected by this replacement.
306 memberName := m.MemberName()
307 mctx.ReplaceDependencies(memberName)
308 }
309 }
310 }
311}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900312
313// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
314func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
315 if m, ok := mctx.Module().(interface {
316 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
317 RequiredSdks() android.SdkRefs
318 }); ok {
319 requiredSdks := m.RequiredSdks()
320 if len(requiredSdks) == 0 {
321 return
322 }
323 mctx.VisitDirectDeps(func(dep android.Module) {
324 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
325 // dependency to defaults is always okay
326 return
327 }
328
329 // If the dep is from outside of the APEX, but is not in any of the
330 // required SDKs, we know that the dep is a violation.
331 if sa, ok := dep.(android.SdkAware); ok {
332 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
333 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
334 sa.Name(), sa.ContainingSdk(), requiredSdks)
335 }
336 }
337 })
338 }
339}