blob: cd9aafa39a73862103477ec3a0bb0c21fbb66ec5 [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
Paul Duffin9ab556f2019-12-11 18:42:17 +000066 // The list of shared native libraries in this SDK
Paul Duffinfa028722019-12-12 11:31:59 +000067 Native_shared_libs []string
68
Paul Duffin9ab556f2019-12-11 18:42:17 +000069 // The list of static native libraries in this SDK
70 Native_static_libs []string
71
Paul Duffinfa028722019-12-12 11:31:59 +000072 // For module types from the java package
73
Paul Duffina0dbf432019-12-05 11:25:53 +000074 // The list of java header libraries in this SDK
75 //
76 // This should be used for java libraries that are provided separately at runtime,
77 // e.g. through an APEX.
78 Java_header_libs []string
Paul Duffinfa028722019-12-12 11:31:59 +000079
Paul Duffina0dbf432019-12-05 11:25:53 +000080 // The list of java implementation libraries in this SDK
Jiyong Park9b409bc2019-10-11 14:59:13 +090081 Java_libs []string
Paul Duffinfa028722019-12-12 11:31:59 +000082
Paul Duffin91547182019-11-12 19:39:36 +000083 // The list of stub sources in this SDK
84 Stubs_sources []string
Jiyong Park9b409bc2019-10-11 14:59:13 +090085
86 Snapshot bool `blueprint:"mutated"`
Jiyong Parkd1063c12019-07-17 20:08:41 +090087}
88
Paul Duffin13879572019-11-28 14:31:38 +000089type sdkMemberDependencyTag struct {
90 blueprint.BaseDependencyTag
91 memberListProperty *sdkMemberListProperty
92}
93
94// Contains information about the sdk properties that list sdk members, e.g.
Paul Duffina0dbf432019-12-05 11:25:53 +000095// Java_header_libs.
Paul Duffin13879572019-11-28 14:31:38 +000096type sdkMemberListProperty struct {
97 // the name of the property as used in a .bp file
98 name string
99
100 // getter for the list of member names
101 getter func(properties *sdkProperties) []string
102
103 // the type of member referenced in the list
104 memberType android.SdkMemberType
105
106 // the dependency tag used for items in this list.
107 dependencyTag *sdkMemberDependencyTag
108}
109
Paul Duffina6e737b2019-11-29 11:55:51 +0000110// Information about how to handle each member list property.
111//
112// It is organized first by package and then by name within the package.
113// Packages are in alphabetical order and properties are in alphabetical order
114// within each package.
Paul Duffin13879572019-11-28 14:31:38 +0000115var sdkMemberListProperties = []*sdkMemberListProperty{
Paul Duffina6e737b2019-11-29 11:55:51 +0000116 // Members from cc package.
117 {
118 name: "native_shared_libs",
119 getter: func(properties *sdkProperties) []string { return properties.Native_shared_libs },
Paul Duffin9ab556f2019-12-11 18:42:17 +0000120 memberType: cc.SharedLibrarySdkMemberType,
121 },
122 {
123 name: "native_static_libs",
124 getter: func(properties *sdkProperties) []string { return properties.Native_static_libs },
125 memberType: cc.StaticLibrarySdkMemberType,
Paul Duffina6e737b2019-11-29 11:55:51 +0000126 },
127 // Members from java package.
Paul Duffin13879572019-11-28 14:31:38 +0000128 {
Paul Duffina0dbf432019-12-05 11:25:53 +0000129 name: "java_header_libs",
130 getter: func(properties *sdkProperties) []string { return properties.Java_header_libs },
131 memberType: java.HeaderLibrarySdkMemberType,
132 },
133 {
Paul Duffin13879572019-11-28 14:31:38 +0000134 name: "java_libs",
135 getter: func(properties *sdkProperties) []string { return properties.Java_libs },
Paul Duffina0dbf432019-12-05 11:25:53 +0000136 memberType: java.ImplLibrarySdkMemberType,
Paul Duffin13879572019-11-28 14:31:38 +0000137 },
138 {
139 name: "stubs_sources",
140 getter: func(properties *sdkProperties) []string { return properties.Stubs_sources },
141 memberType: java.DroidStubsSdkMemberType,
142 },
Paul Duffin13879572019-11-28 14:31:38 +0000143}
144
Jiyong Parkd1063c12019-07-17 20:08:41 +0900145// sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
146// which Mainline modules like APEX can choose to build with.
147func ModuleFactory() android.Module {
148 s := &sdk{}
149 s.AddProperties(&s.properties)
150 android.InitAndroidMultiTargetsArchModule(s, android.HostAndDeviceSupported, android.MultilibCommon)
151 android.InitDefaultableModule(s)
Jiyong Park100f3fd2019-11-06 16:03:32 +0900152 android.AddLoadHook(s, func(ctx android.LoadHookContext) {
153 type props struct {
154 Compile_multilib *string
155 }
156 p := &props{Compile_multilib: proptools.StringPtr("both")}
157 ctx.AppendProperties(p)
158 })
Jiyong Parkd1063c12019-07-17 20:08:41 +0900159 return s
160}
161
Jiyong Park9b409bc2019-10-11 14:59:13 +0900162// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
163func SnapshotModuleFactory() android.Module {
164 s := ModuleFactory()
165 s.(*sdk).properties.Snapshot = true
166 return s
167}
168
169func (s *sdk) snapshot() bool {
170 return s.properties.Snapshot
171}
172
Jiyong Parkd1063c12019-07-17 20:08:41 +0900173func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park232e7852019-11-04 12:23:40 +0900174 if !s.snapshot() {
175 // We don't need to create a snapshot out of sdk_snapshot.
176 // That doesn't make sense. We need a snapshot to create sdk_snapshot.
177 s.snapshotFile = android.OptionalPathForPath(s.buildSnapshot(ctx))
178 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900179}
180
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900181func (s *sdk) AndroidMkEntries() []android.AndroidMkEntries {
Jiyong Park232e7852019-11-04 12:23:40 +0900182 if !s.snapshotFile.Valid() {
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900183 return []android.AndroidMkEntries{}
Jiyong Park232e7852019-11-04 12:23:40 +0900184 }
185
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900186 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park232e7852019-11-04 12:23:40 +0900187 Class: "FAKE",
188 OutputFile: s.snapshotFile,
189 DistFile: s.snapshotFile,
190 Include: "$(BUILD_PHONY_PACKAGE)",
Paul Duffin504b4612019-11-22 14:52:29 +0000191 ExtraFooters: []android.AndroidMkExtraFootersFunc{
192 func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
193 // Allow the sdk to be built by simply passing its name on the command line.
194 fmt.Fprintln(w, ".PHONY:", s.Name())
195 fmt.Fprintln(w, s.Name()+":", s.snapshotFile.String())
196 },
197 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900198 }}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900199}
200
201// RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
202// interface and the sdk module type. This function has been made public to be called by tests
203// outside of the sdk package
204func RegisterPreDepsMutators(ctx android.RegisterMutatorsContext) {
205 ctx.BottomUp("SdkMember", memberMutator).Parallel()
206 ctx.TopDown("SdkMember_deps", memberDepsMutator).Parallel()
207 ctx.BottomUp("SdkMemberInterVersion", memberInterVersionMutator).Parallel()
208}
209
210// RegisterPostDepshMutators registers post-deps mutators to support modules implementing SdkAware
211// interface and the sdk module type. This function has been made public to be called by tests
212// outside of the sdk package
213func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
214 // These must run AFTER apexMutator. Note that the apex package is imported even though there is
215 // no direct dependency to the package here. sdkDepsMutator sets the SDK requirements from an
216 // APEX to its dependents. Since different versions of the same SDK can be used by different
217 // APEXes, the apex and its dependents (which includes the dependencies to the sdk members)
218 // should have been mutated for the apex before the SDK requirements are set.
219 ctx.TopDown("SdkDepsMutator", sdkDepsMutator).Parallel()
220 ctx.BottomUp("SdkDepsReplaceMutator", sdkDepsReplaceMutator).Parallel()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900221 ctx.TopDown("SdkRequirementCheck", sdkRequirementsMutator).Parallel()
Jiyong Parkd1063c12019-07-17 20:08:41 +0900222}
223
224type dependencyTag struct {
225 blueprint.BaseDependencyTag
226}
227
Jiyong Parkd1063c12019-07-17 20:08:41 +0900228// For dependencies from an in-development version of an SDK member to frozen versions of the same member
229// e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
230type sdkMemberVesionedDepTag struct {
231 dependencyTag
232 member string
233 version string
234}
235
236// Step 1: create dependencies from an SDK module to its members.
237func memberMutator(mctx android.BottomUpMutatorContext) {
238 if m, ok := mctx.Module().(*sdk); ok {
Paul Duffin13879572019-11-28 14:31:38 +0000239 for _, memberListProperty := range sdkMemberListProperties {
240 names := memberListProperty.getter(&m.properties)
241 tag := memberListProperty.dependencyTag
242 memberListProperty.memberType.AddDependencies(mctx, tag, names)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900243 }
244 }
245}
246
247// Step 2: record that dependencies of SDK modules are members of the SDK modules
248func memberDepsMutator(mctx android.TopDownMutatorContext) {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900249 if s, ok := mctx.Module().(*sdk); ok {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900250 mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900251 if s.snapshot() && mySdkRef.Unversioned() {
252 mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
253 "Did you manually modify Android.bp?")
254 }
255 if !s.snapshot() && !mySdkRef.Unversioned() {
256 mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
257 }
258 if mySdkRef.Version != "" && mySdkRef.Version != "current" {
259 if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
260 mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
261 }
262 }
263
Jiyong Parkd1063c12019-07-17 20:08:41 +0900264 mctx.VisitDirectDeps(func(child android.Module) {
265 if member, ok := child.(android.SdkAware); ok {
266 member.MakeMemberOf(mySdkRef)
267 }
268 })
269 }
270}
271
Jiyong Park9b409bc2019-10-11 14:59:13 +0900272// Step 3: create dependencies from the unversioned SDK member to snapshot versions
Jiyong Parkd1063c12019-07-17 20:08:41 +0900273// of the same member. By having these dependencies, they are mutated for multiple Mainline modules
274// (apex and apk), each of which might want different sdks to be built with. For example, if both
275// apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
276// built with libfoo.mysdk.11 and libfoo.mysdk.12, respectively depending on which sdk they are
277// using.
278func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
279 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900280 if !m.ContainingSdk().Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900281 memberName := m.MemberName()
282 tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
283 mctx.AddReverseDependency(mctx.Module(), tag, memberName)
284 }
285 }
286}
287
288// Step 4: transitively ripple down the SDK requirements from the root modules like APEX to its
289// descendants
290func sdkDepsMutator(mctx android.TopDownMutatorContext) {
291 if m, ok := mctx.Module().(android.SdkAware); ok {
292 // Module types for Mainline modules (e.g. APEX) are expected to implement RequiredSdks()
293 // by reading its own properties like `uses_sdks`.
294 requiredSdks := m.RequiredSdks()
295 if len(requiredSdks) > 0 {
296 mctx.VisitDirectDeps(func(m android.Module) {
297 if dep, ok := m.(android.SdkAware); ok {
298 dep.BuildWithSdks(requiredSdks)
299 }
300 })
301 }
302 }
303}
304
305// Step 5: if libfoo.mysdk.11 is in the context where version 11 of mysdk is requested, the
306// versioned module is used instead of the un-versioned (in-development) module libfoo
307func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
308 if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
Jiyong Park9b409bc2019-10-11 14:59:13 +0900309 if sdk := m.ContainingSdk(); !sdk.Unversioned() {
Jiyong Parkd1063c12019-07-17 20:08:41 +0900310 if m.RequiredSdks().Contains(sdk) {
311 // Note that this replacement is done only for the modules that have the same
312 // variations as the current module. Since current module is already mutated for
313 // apex references in other APEXes are not affected by this replacement.
314 memberName := m.MemberName()
315 mctx.ReplaceDependencies(memberName)
316 }
317 }
318 }
319}
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900320
321// Step 6: ensure that the dependencies from outside of the APEX are all from the required SDKs
322func sdkRequirementsMutator(mctx android.TopDownMutatorContext) {
323 if m, ok := mctx.Module().(interface {
324 DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool
325 RequiredSdks() android.SdkRefs
326 }); ok {
327 requiredSdks := m.RequiredSdks()
328 if len(requiredSdks) == 0 {
329 return
330 }
331 mctx.VisitDirectDeps(func(dep android.Module) {
332 if mctx.OtherModuleDependencyTag(dep) == android.DefaultsDepTag {
333 // dependency to defaults is always okay
334 return
335 }
336
337 // If the dep is from outside of the APEX, but is not in any of the
338 // required SDKs, we know that the dep is a violation.
339 if sa, ok := dep.(android.SdkAware); ok {
340 if !m.DepIsInSameApex(mctx, dep) && !requiredSdks.Contains(sa.ContainingSdk()) {
341 mctx.ModuleErrorf("depends on %q (in SDK %q) that isn't part of the required SDKs: %v",
342 sa.Name(), sa.ContainingSdk(), requiredSdks)
343 }
344 }
345 })
346 }
347}