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