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 | |
| 20 | "github.com/google/blueprint/proptools" |
| 21 | ) |
| 22 | |
| 23 | // SdkAware is the interface that must be supported by any module to become a member of SDK or to be |
| 24 | // built with SDK |
| 25 | type SdkAware interface { |
| 26 | Module |
| 27 | sdkBase() *SdkBase |
| 28 | MakeMemberOf(sdk SdkRef) |
| 29 | IsInAnySdk() bool |
| 30 | ContainingSdk() SdkRef |
| 31 | MemberName() string |
| 32 | BuildWithSdks(sdks SdkRefs) |
| 33 | RequiredSdks() SdkRefs |
| 34 | } |
| 35 | |
| 36 | // SdkRef refers to a version of an SDK |
| 37 | type SdkRef struct { |
| 38 | Name string |
| 39 | Version string |
| 40 | } |
| 41 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame^] | 42 | // Unversioned determines if the SdkRef is referencing to the unversioned SDK module |
| 43 | func (s SdkRef) Unversioned() bool { |
| 44 | return s.Version == "" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 45 | } |
| 46 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame^] | 47 | // SdkVersionSeparator is a character used to separate an sdk name and its version |
| 48 | const SdkVersionSeparator = '@' |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 49 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame^] | 50 | // ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 51 | func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame^] | 52 | tokens := strings.Split(str, string(SdkVersionSeparator)) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 53 | if len(tokens) < 1 || len(tokens) > 2 { |
| 54 | ctx.PropertyErrorf(property, "%q does not follow name#version syntax", str) |
| 55 | return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"} |
| 56 | } |
| 57 | |
| 58 | name := tokens[0] |
| 59 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame^] | 60 | var version string |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 61 | if len(tokens) == 2 { |
| 62 | version = tokens[1] |
| 63 | } |
| 64 | |
| 65 | return SdkRef{Name: name, Version: version} |
| 66 | } |
| 67 | |
| 68 | type SdkRefs []SdkRef |
| 69 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame^] | 70 | // 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] | 71 | func (refs SdkRefs) Contains(s SdkRef) bool { |
| 72 | for _, r := range refs { |
| 73 | if r == s { |
| 74 | return true |
| 75 | } |
| 76 | } |
| 77 | return false |
| 78 | } |
| 79 | |
| 80 | type sdkProperties struct { |
| 81 | // The SDK that this module is a member of. nil if it is not a member of any SDK |
| 82 | ContainingSdk *SdkRef `blueprint:"mutated"` |
| 83 | |
| 84 | // The list of SDK names and versions that are used to build this module |
| 85 | RequiredSdks SdkRefs `blueprint:"mutated"` |
| 86 | |
| 87 | // Name of the module that this sdk member is representing |
| 88 | Sdk_member_name *string |
| 89 | } |
| 90 | |
| 91 | // SdkBase is a struct that is expected to be included in module types to implement the SdkAware |
| 92 | // interface. InitSdkAwareModule should be called to initialize this struct. |
| 93 | type SdkBase struct { |
| 94 | properties sdkProperties |
| 95 | } |
| 96 | |
| 97 | func (s *SdkBase) sdkBase() *SdkBase { |
| 98 | return s |
| 99 | } |
| 100 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame^] | 101 | // MakeMemberOf sets this module to be a member of a specific SDK |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 102 | func (s *SdkBase) MakeMemberOf(sdk SdkRef) { |
| 103 | s.properties.ContainingSdk = &sdk |
| 104 | } |
| 105 | |
| 106 | // IsInAnySdk returns true if this module is a member of any SDK |
| 107 | func (s *SdkBase) IsInAnySdk() bool { |
| 108 | return s.properties.ContainingSdk != nil |
| 109 | } |
| 110 | |
| 111 | // ContainingSdk returns the SDK that this module is a member of |
| 112 | func (s *SdkBase) ContainingSdk() SdkRef { |
| 113 | if s.properties.ContainingSdk != nil { |
| 114 | return *s.properties.ContainingSdk |
| 115 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame^] | 116 | return SdkRef{Name: "", Version: ""} |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 117 | } |
| 118 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame^] | 119 | // 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] | 120 | func (s *SdkBase) MemberName() string { |
| 121 | return proptools.String(s.properties.Sdk_member_name) |
| 122 | } |
| 123 | |
| 124 | // BuildWithSdks is used to mark that this module has to be built with the given SDK(s). |
| 125 | func (s *SdkBase) BuildWithSdks(sdks SdkRefs) { |
| 126 | s.properties.RequiredSdks = sdks |
| 127 | } |
| 128 | |
| 129 | // RequiredSdks returns the SDK(s) that this module has to be built with |
| 130 | func (s *SdkBase) RequiredSdks() SdkRefs { |
| 131 | return s.properties.RequiredSdks |
| 132 | } |
| 133 | |
| 134 | // InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including |
| 135 | // SdkBase. |
| 136 | func InitSdkAwareModule(m SdkAware) { |
| 137 | base := m.sdkBase() |
| 138 | m.AddProperties(&base.properties) |
| 139 | } |