blob: 616fbe19d624ba1a5523376517d3e9edf8c40191 [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 Park9b409bc2019-10-11 14:59:13 +090047// SdkVersionSeparator is a character used to separate an sdk name and its version
48const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +090049
Jiyong Park9b409bc2019-10-11 14:59:13 +090050// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +090051func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +090052 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +090053 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 Park9b409bc2019-10-11 14:59:13 +090060 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +090061 if len(tokens) == 2 {
62 version = tokens[1]
63 }
64
65 return SdkRef{Name: name, Version: version}
66}
67
68type SdkRefs []SdkRef
69
Jiyong Park9b409bc2019-10-11 14:59:13 +090070// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +090071func (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
80type 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.
93type SdkBase struct {
94 properties sdkProperties
95}
96
97func (s *SdkBase) sdkBase() *SdkBase {
98 return s
99}
100
Jiyong Park9b409bc2019-10-11 14:59:13 +0900101// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900102func (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
107func (s *SdkBase) IsInAnySdk() bool {
108 return s.properties.ContainingSdk != nil
109}
110
111// ContainingSdk returns the SDK that this module is a member of
112func (s *SdkBase) ContainingSdk() SdkRef {
113 if s.properties.ContainingSdk != nil {
114 return *s.properties.ContainingSdk
115 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900116 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900117}
118
Jiyong Park9b409bc2019-10-11 14:59:13 +0900119// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900120func (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).
125func (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
130func (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.
136func InitSdkAwareModule(m SdkAware) {
137 base := m.sdkBase()
138 m.AddProperties(&base.properties)
139}