blob: b9220ca699360108af404354217e14e248510d88 [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 android
16
17import (
18 "strings"
19
Paul Duffin13879572019-11-28 14:31:38 +000020 "github.com/google/blueprint"
Jiyong Parkd1063c12019-07-17 20:08:41 +090021 "github.com/google/blueprint/proptools"
22)
23
24// SdkAware is the interface that must be supported by any module to become a member of SDK or to be
25// built with SDK
26type SdkAware interface {
27 Module
28 sdkBase() *SdkBase
29 MakeMemberOf(sdk SdkRef)
30 IsInAnySdk() bool
31 ContainingSdk() SdkRef
32 MemberName() string
33 BuildWithSdks(sdks SdkRefs)
34 RequiredSdks() SdkRefs
35}
36
37// SdkRef refers to a version of an SDK
38type SdkRef struct {
39 Name string
40 Version string
41}
42
Jiyong Park9b409bc2019-10-11 14:59:13 +090043// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
44func (s SdkRef) Unversioned() bool {
45 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +090046}
47
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090048// String returns string representation of this SdkRef for debugging purpose
49func (s SdkRef) String() string {
50 if s.Name == "" {
51 return "(No Sdk)"
52 }
53 if s.Unversioned() {
54 return s.Name
55 }
56 return s.Name + string(SdkVersionSeparator) + s.Version
57}
58
Jiyong Park9b409bc2019-10-11 14:59:13 +090059// SdkVersionSeparator is a character used to separate an sdk name and its version
60const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +090061
Jiyong Park9b409bc2019-10-11 14:59:13 +090062// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +090063func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +090064 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +090065 if len(tokens) < 1 || len(tokens) > 2 {
66 ctx.PropertyErrorf(property, "%q does not follow name#version syntax", str)
67 return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"}
68 }
69
70 name := tokens[0]
71
Jiyong Park9b409bc2019-10-11 14:59:13 +090072 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +090073 if len(tokens) == 2 {
74 version = tokens[1]
75 }
76
77 return SdkRef{Name: name, Version: version}
78}
79
80type SdkRefs []SdkRef
81
Jiyong Park9b409bc2019-10-11 14:59:13 +090082// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +090083func (refs SdkRefs) Contains(s SdkRef) bool {
84 for _, r := range refs {
85 if r == s {
86 return true
87 }
88 }
89 return false
90}
91
92type sdkProperties struct {
93 // The SDK that this module is a member of. nil if it is not a member of any SDK
94 ContainingSdk *SdkRef `blueprint:"mutated"`
95
96 // The list of SDK names and versions that are used to build this module
97 RequiredSdks SdkRefs `blueprint:"mutated"`
98
99 // Name of the module that this sdk member is representing
100 Sdk_member_name *string
101}
102
103// SdkBase is a struct that is expected to be included in module types to implement the SdkAware
104// interface. InitSdkAwareModule should be called to initialize this struct.
105type SdkBase struct {
106 properties sdkProperties
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000107 module SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900108}
109
110func (s *SdkBase) sdkBase() *SdkBase {
111 return s
112}
113
Jiyong Park9b409bc2019-10-11 14:59:13 +0900114// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900115func (s *SdkBase) MakeMemberOf(sdk SdkRef) {
116 s.properties.ContainingSdk = &sdk
117}
118
119// IsInAnySdk returns true if this module is a member of any SDK
120func (s *SdkBase) IsInAnySdk() bool {
121 return s.properties.ContainingSdk != nil
122}
123
124// ContainingSdk returns the SDK that this module is a member of
125func (s *SdkBase) ContainingSdk() SdkRef {
126 if s.properties.ContainingSdk != nil {
127 return *s.properties.ContainingSdk
128 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900129 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900130}
131
Jiyong Park9b409bc2019-10-11 14:59:13 +0900132// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900133func (s *SdkBase) MemberName() string {
134 return proptools.String(s.properties.Sdk_member_name)
135}
136
137// BuildWithSdks is used to mark that this module has to be built with the given SDK(s).
138func (s *SdkBase) BuildWithSdks(sdks SdkRefs) {
139 s.properties.RequiredSdks = sdks
140}
141
142// RequiredSdks returns the SDK(s) that this module has to be built with
143func (s *SdkBase) RequiredSdks() SdkRefs {
144 return s.properties.RequiredSdks
145}
146
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000147func (s *SdkBase) BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder) {
148 sdkModuleContext.ModuleErrorf("module type " + sdkModuleContext.OtherModuleType(s.module) + " cannot be used in an sdk")
149}
150
Jiyong Parkd1063c12019-07-17 20:08:41 +0900151// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
152// SdkBase.
153func InitSdkAwareModule(m SdkAware) {
154 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000155 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900156 m.AddProperties(&base.properties)
157}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000158
159// Provide support for generating the build rules which will build the snapshot.
160type SnapshotBuilder interface {
161 // Copy src to the dest (which is a snapshot relative path) and add the dest
162 // to the zip
163 CopyToSnapshot(src Path, dest string)
164
Paul Duffin91547182019-11-12 19:39:36 +0000165 // Unzip the supplied zip into the snapshot relative directory destDir.
166 UnzipToSnapshot(zipPath Path, destDir string)
167
Paul Duffinb645ec82019-11-27 17:43:54 +0000168 // Add a new prebuilt module to the snapshot. The returned module
169 // must be populated with the module type specific properties. The following
170 // properties will be automatically populated.
171 //
172 // * name
173 // * sdk_member_name
174 // * prefer
175 //
176 // This will result in two Soong modules being generated in the Android. One
177 // that is versioned, coupled to the snapshot version and marked as
178 // prefer=true. And one that is not versioned, not marked as prefer=true and
179 // will only be used if the equivalently named non-prebuilt module is not
180 // present.
181 AddPrebuiltModule(name string, moduleType string) BpModule
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000182}
183
Paul Duffinb645ec82019-11-27 17:43:54 +0000184// A set of properties for use in a .bp file.
185type BpPropertySet interface {
186 // Add a property, the value can be one of the following types:
187 // * string
188 // * array of the above
189 // * bool
190 // * BpPropertySet
191 //
192 // It is an error is multiples properties with the same name are added.
193 AddProperty(name string, value interface{})
194
195 // Add a property set with the specified name and return so that additional
196 // properties can be added.
197 AddPropertySet(name string) BpPropertySet
198}
199
200// A .bp module definition.
201type BpModule interface {
202 BpPropertySet
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000203}
Paul Duffin13879572019-11-28 14:31:38 +0000204
205// An individual member of the SDK, includes all of the variants that the SDK
206// requires.
207type SdkMember interface {
208 // The name of the member.
209 Name() string
210
211 // All the variants required by the SDK.
212 Variants() []SdkAware
213}
214
215// Interface that must be implemented for every type that can be a member of an
216// sdk.
217//
218// The basic implementation should look something like this, where ModuleType is
219// the name of the module type being supported.
220//
221// var ModuleTypeSdkMemberType = newModuleTypeSdkMemberType()
222//
223// func newModuleTypeSdkMemberType() android.SdkMemberType {
224// return &moduleTypeSdkMemberType{}
225// }
226//
227// type moduleTypeSdkMemberType struct {
228// }
229//
230// ...methods...
231//
232type SdkMemberType interface {
233 // Add dependencies from the SDK module to all the variants the member
234 // contributes to the SDK. The exact set of variants required is determined
235 // by the SDK and its properties. The dependencies must be added with the
236 // supplied tag.
237 //
238 // The BottomUpMutatorContext provided is for the SDK module.
239 AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string)
240
241 // Return true if the supplied module is an instance of this member type.
242 //
243 // This is used to check the type of each variant before added to the
244 // SdkMember. Returning false will cause an error to be logged expaining that
245 // the module is not allowed in whichever sdk property it was added.
246 IsInstance(module Module) bool
247
248 // Build the snapshot for the SDK member
249 //
250 // The ModuleContext provided is for the SDK module, so information for
251 // variants in the supplied member can be accessed using the Other... methods.
252 //
253 // The SdkMember is guaranteed to contain variants for which the
254 // IsInstance(Module) method returned true.
255 BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder, member SdkMember)
256}