Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame^] | 20 | "github.com/google/blueprint" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 21 | "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 |
| 26 | type 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 |
| 38 | type SdkRef struct { |
| 39 | Name string |
| 40 | Version string |
| 41 | } |
| 42 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 43 | // Unversioned determines if the SdkRef is referencing to the unversioned SDK module |
| 44 | func (s SdkRef) Unversioned() bool { |
| 45 | return s.Version == "" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 46 | } |
| 47 | |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 48 | // String returns string representation of this SdkRef for debugging purpose |
| 49 | func (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 Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 59 | // SdkVersionSeparator is a character used to separate an sdk name and its version |
| 60 | const SdkVersionSeparator = '@' |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 61 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 62 | // ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 63 | func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 64 | tokens := strings.Split(str, string(SdkVersionSeparator)) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 65 | 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 Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 72 | var version string |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 73 | if len(tokens) == 2 { |
| 74 | version = tokens[1] |
| 75 | } |
| 76 | |
| 77 | return SdkRef{Name: name, Version: version} |
| 78 | } |
| 79 | |
| 80 | type SdkRefs []SdkRef |
| 81 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 82 | // Contains tells if the given SdkRef is in this list of SdkRef's |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 83 | func (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 | |
| 92 | type 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. |
| 105 | type SdkBase struct { |
| 106 | properties sdkProperties |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 107 | module SdkAware |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | func (s *SdkBase) sdkBase() *SdkBase { |
| 111 | return s |
| 112 | } |
| 113 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 114 | // MakeMemberOf sets this module to be a member of a specific SDK |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 115 | func (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 |
| 120 | func (s *SdkBase) IsInAnySdk() bool { |
| 121 | return s.properties.ContainingSdk != nil |
| 122 | } |
| 123 | |
| 124 | // ContainingSdk returns the SDK that this module is a member of |
| 125 | func (s *SdkBase) ContainingSdk() SdkRef { |
| 126 | if s.properties.ContainingSdk != nil { |
| 127 | return *s.properties.ContainingSdk |
| 128 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 129 | return SdkRef{Name: "", Version: ""} |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 130 | } |
| 131 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 132 | // MemberName returns the name of the module that this SDK member is overriding |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 133 | func (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). |
| 138 | func (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 |
| 143 | func (s *SdkBase) RequiredSdks() SdkRefs { |
| 144 | return s.properties.RequiredSdks |
| 145 | } |
| 146 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 147 | func (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 Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 151 | // InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including |
| 152 | // SdkBase. |
| 153 | func InitSdkAwareModule(m SdkAware) { |
| 154 | base := m.sdkBase() |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 155 | base.module = m |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 156 | m.AddProperties(&base.properties) |
| 157 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 158 | |
| 159 | // Provide support for generating the build rules which will build the snapshot. |
| 160 | type 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 Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 165 | // Unzip the supplied zip into the snapshot relative directory destDir. |
| 166 | UnzipToSnapshot(zipPath Path, destDir string) |
| 167 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 168 | // 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 Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Paul Duffin | b645ec8 | 2019-11-27 17:43:54 +0000 | [diff] [blame] | 184 | // A set of properties for use in a .bp file. |
| 185 | type 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. |
| 201 | type BpModule interface { |
| 202 | BpPropertySet |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 203 | } |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame^] | 204 | |
| 205 | // An individual member of the SDK, includes all of the variants that the SDK |
| 206 | // requires. |
| 207 | type 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 | // |
| 232 | type 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 | } |