blob: 8e1e106a1622fa349e61d630a472295a4e610e70 [file] [log] [blame]
Jiyong Parkd1063c12019-07-17 20:08:41 +09001// 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
15package android
16
17import (
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
25type 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
37type SdkRef struct {
38 Name string
39 Version string
40}
41
Jiyong Park9b409bc2019-10-11 14:59:13 +090042// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
43func (s SdkRef) Unversioned() bool {
44 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +090045}
46
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090047// String returns string representation of this SdkRef for debugging purpose
48func (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 Park9b409bc2019-10-11 14:59:13 +090058// SdkVersionSeparator is a character used to separate an sdk name and its version
59const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +090060
Jiyong Park9b409bc2019-10-11 14:59:13 +090061// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +090062func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +090063 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +090064 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 Park9b409bc2019-10-11 14:59:13 +090071 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +090072 if len(tokens) == 2 {
73 version = tokens[1]
74 }
75
76 return SdkRef{Name: name, Version: version}
77}
78
79type SdkRefs []SdkRef
80
Jiyong Park9b409bc2019-10-11 14:59:13 +090081// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +090082func (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
91type 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.
104type SdkBase struct {
105 properties sdkProperties
106}
107
108func (s *SdkBase) sdkBase() *SdkBase {
109 return s
110}
111
Jiyong Park9b409bc2019-10-11 14:59:13 +0900112// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900113func (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
118func (s *SdkBase) IsInAnySdk() bool {
119 return s.properties.ContainingSdk != nil
120}
121
122// ContainingSdk returns the SDK that this module is a member of
123func (s *SdkBase) ContainingSdk() SdkRef {
124 if s.properties.ContainingSdk != nil {
125 return *s.properties.ContainingSdk
126 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900127 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900128}
129
Jiyong Park9b409bc2019-10-11 14:59:13 +0900130// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900131func (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).
136func (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
141func (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.
147func InitSdkAwareModule(m SdkAware) {
148 base := m.sdkBase()
149 m.AddProperties(&base.properties)
150}