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