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 |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 34 | |
| 35 | // Build a snapshot of the module. |
| 36 | BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // SdkRef refers to a version of an SDK |
| 40 | type SdkRef struct { |
| 41 | Name string |
| 42 | Version string |
| 43 | } |
| 44 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 45 | // Unversioned determines if the SdkRef is referencing to the unversioned SDK module |
| 46 | func (s SdkRef) Unversioned() bool { |
| 47 | return s.Version == "" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 48 | } |
| 49 | |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 50 | // String returns string representation of this SdkRef for debugging purpose |
| 51 | func (s SdkRef) String() string { |
| 52 | if s.Name == "" { |
| 53 | return "(No Sdk)" |
| 54 | } |
| 55 | if s.Unversioned() { |
| 56 | return s.Name |
| 57 | } |
| 58 | return s.Name + string(SdkVersionSeparator) + s.Version |
| 59 | } |
| 60 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 61 | // SdkVersionSeparator is a character used to separate an sdk name and its version |
| 62 | const SdkVersionSeparator = '@' |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 63 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 64 | // ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 65 | func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 66 | tokens := strings.Split(str, string(SdkVersionSeparator)) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 67 | if len(tokens) < 1 || len(tokens) > 2 { |
| 68 | ctx.PropertyErrorf(property, "%q does not follow name#version syntax", str) |
| 69 | return SdkRef{Name: "invalid sdk name", Version: "invalid sdk version"} |
| 70 | } |
| 71 | |
| 72 | name := tokens[0] |
| 73 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 74 | var version string |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 75 | if len(tokens) == 2 { |
| 76 | version = tokens[1] |
| 77 | } |
| 78 | |
| 79 | return SdkRef{Name: name, Version: version} |
| 80 | } |
| 81 | |
| 82 | type SdkRefs []SdkRef |
| 83 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 84 | // 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] | 85 | func (refs SdkRefs) Contains(s SdkRef) bool { |
| 86 | for _, r := range refs { |
| 87 | if r == s { |
| 88 | return true |
| 89 | } |
| 90 | } |
| 91 | return false |
| 92 | } |
| 93 | |
| 94 | type sdkProperties struct { |
| 95 | // The SDK that this module is a member of. nil if it is not a member of any SDK |
| 96 | ContainingSdk *SdkRef `blueprint:"mutated"` |
| 97 | |
| 98 | // The list of SDK names and versions that are used to build this module |
| 99 | RequiredSdks SdkRefs `blueprint:"mutated"` |
| 100 | |
| 101 | // Name of the module that this sdk member is representing |
| 102 | Sdk_member_name *string |
| 103 | } |
| 104 | |
| 105 | // SdkBase is a struct that is expected to be included in module types to implement the SdkAware |
| 106 | // interface. InitSdkAwareModule should be called to initialize this struct. |
| 107 | type SdkBase struct { |
| 108 | properties sdkProperties |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 109 | module SdkAware |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | func (s *SdkBase) sdkBase() *SdkBase { |
| 113 | return s |
| 114 | } |
| 115 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 116 | // MakeMemberOf sets this module to be a member of a specific SDK |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 117 | func (s *SdkBase) MakeMemberOf(sdk SdkRef) { |
| 118 | s.properties.ContainingSdk = &sdk |
| 119 | } |
| 120 | |
| 121 | // IsInAnySdk returns true if this module is a member of any SDK |
| 122 | func (s *SdkBase) IsInAnySdk() bool { |
| 123 | return s.properties.ContainingSdk != nil |
| 124 | } |
| 125 | |
| 126 | // ContainingSdk returns the SDK that this module is a member of |
| 127 | func (s *SdkBase) ContainingSdk() SdkRef { |
| 128 | if s.properties.ContainingSdk != nil { |
| 129 | return *s.properties.ContainingSdk |
| 130 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 131 | return SdkRef{Name: "", Version: ""} |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 132 | } |
| 133 | |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 134 | // 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] | 135 | func (s *SdkBase) MemberName() string { |
| 136 | return proptools.String(s.properties.Sdk_member_name) |
| 137 | } |
| 138 | |
| 139 | // BuildWithSdks is used to mark that this module has to be built with the given SDK(s). |
| 140 | func (s *SdkBase) BuildWithSdks(sdks SdkRefs) { |
| 141 | s.properties.RequiredSdks = sdks |
| 142 | } |
| 143 | |
| 144 | // RequiredSdks returns the SDK(s) that this module has to be built with |
| 145 | func (s *SdkBase) RequiredSdks() SdkRefs { |
| 146 | return s.properties.RequiredSdks |
| 147 | } |
| 148 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 149 | func (s *SdkBase) BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder) { |
| 150 | sdkModuleContext.ModuleErrorf("module type " + sdkModuleContext.OtherModuleType(s.module) + " cannot be used in an sdk") |
| 151 | } |
| 152 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 153 | // InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including |
| 154 | // SdkBase. |
| 155 | func InitSdkAwareModule(m SdkAware) { |
| 156 | base := m.sdkBase() |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 157 | base.module = m |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 158 | m.AddProperties(&base.properties) |
| 159 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 160 | |
| 161 | // Provide support for generating the build rules which will build the snapshot. |
| 162 | type SnapshotBuilder interface { |
| 163 | // Copy src to the dest (which is a snapshot relative path) and add the dest |
| 164 | // to the zip |
| 165 | CopyToSnapshot(src Path, dest string) |
| 166 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame^] | 167 | // Unzip the supplied zip into the snapshot relative directory destDir. |
| 168 | UnzipToSnapshot(zipPath Path, destDir string) |
| 169 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 170 | // Get the AndroidBpFile for the snapshot. |
| 171 | AndroidBpFile() GeneratedSnapshotFile |
| 172 | |
| 173 | // Get a versioned name appropriate for the SDK snapshot version being taken. |
| 174 | VersionedSdkMemberName(unversionedName string) interface{} |
| 175 | } |
| 176 | |
| 177 | // Provides support for generating a file, e.g. the Android.bp file. |
| 178 | type GeneratedSnapshotFile interface { |
| 179 | Printfln(format string, args ...interface{}) |
| 180 | Indent() |
| 181 | Dedent() |
| 182 | } |