blob: d66816dc86e07c1fb06ec034440e5c02f391e259 [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
Paul Duffin0e0cf1d2019-11-12 19:39:25 +000034
35 // Build a snapshot of the module.
36 BuildSnapshot(sdkModuleContext ModuleContext, builder SnapshotBuilder)
Jiyong Parkd1063c12019-07-17 20:08:41 +090037}
38
39// SdkRef refers to a version of an SDK
40type SdkRef struct {
41 Name string
42 Version string
43}
44
Jiyong Park9b409bc2019-10-11 14:59:13 +090045// Unversioned determines if the SdkRef is referencing to the unversioned SDK module
46func (s SdkRef) Unversioned() bool {
47 return s.Version == ""
Jiyong Parkd1063c12019-07-17 20:08:41 +090048}
49
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090050// String returns string representation of this SdkRef for debugging purpose
51func (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 Park9b409bc2019-10-11 14:59:13 +090061// SdkVersionSeparator is a character used to separate an sdk name and its version
62const SdkVersionSeparator = '@'
Jiyong Parkd1063c12019-07-17 20:08:41 +090063
Jiyong Park9b409bc2019-10-11 14:59:13 +090064// ParseSdkRef parses a `name@version` style string into a corresponding SdkRef struct
Jiyong Parkd1063c12019-07-17 20:08:41 +090065func ParseSdkRef(ctx BaseModuleContext, str string, property string) SdkRef {
Jiyong Park9b409bc2019-10-11 14:59:13 +090066 tokens := strings.Split(str, string(SdkVersionSeparator))
Jiyong Parkd1063c12019-07-17 20:08:41 +090067 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 Park9b409bc2019-10-11 14:59:13 +090074 var version string
Jiyong Parkd1063c12019-07-17 20:08:41 +090075 if len(tokens) == 2 {
76 version = tokens[1]
77 }
78
79 return SdkRef{Name: name, Version: version}
80}
81
82type SdkRefs []SdkRef
83
Jiyong Park9b409bc2019-10-11 14:59:13 +090084// Contains tells if the given SdkRef is in this list of SdkRef's
Jiyong Parkd1063c12019-07-17 20:08:41 +090085func (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
94type 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.
107type SdkBase struct {
108 properties sdkProperties
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000109 module SdkAware
Jiyong Parkd1063c12019-07-17 20:08:41 +0900110}
111
112func (s *SdkBase) sdkBase() *SdkBase {
113 return s
114}
115
Jiyong Park9b409bc2019-10-11 14:59:13 +0900116// MakeMemberOf sets this module to be a member of a specific SDK
Jiyong Parkd1063c12019-07-17 20:08:41 +0900117func (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
122func (s *SdkBase) IsInAnySdk() bool {
123 return s.properties.ContainingSdk != nil
124}
125
126// ContainingSdk returns the SDK that this module is a member of
127func (s *SdkBase) ContainingSdk() SdkRef {
128 if s.properties.ContainingSdk != nil {
129 return *s.properties.ContainingSdk
130 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900131 return SdkRef{Name: "", Version: ""}
Jiyong Parkd1063c12019-07-17 20:08:41 +0900132}
133
Jiyong Park9b409bc2019-10-11 14:59:13 +0900134// MemberName returns the name of the module that this SDK member is overriding
Jiyong Parkd1063c12019-07-17 20:08:41 +0900135func (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).
140func (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
145func (s *SdkBase) RequiredSdks() SdkRefs {
146 return s.properties.RequiredSdks
147}
148
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000149func (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 Parkd1063c12019-07-17 20:08:41 +0900153// InitSdkAwareModule initializes the SdkBase struct. This must be called by all modules including
154// SdkBase.
155func InitSdkAwareModule(m SdkAware) {
156 base := m.sdkBase()
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000157 base.module = m
Jiyong Parkd1063c12019-07-17 20:08:41 +0900158 m.AddProperties(&base.properties)
159}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000160
161// Provide support for generating the build rules which will build the snapshot.
162type 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
167 // Get the AndroidBpFile for the snapshot.
168 AndroidBpFile() GeneratedSnapshotFile
169
170 // Get a versioned name appropriate for the SDK snapshot version being taken.
171 VersionedSdkMemberName(unversionedName string) interface{}
172}
173
174// Provides support for generating a file, e.g. the Android.bp file.
175type GeneratedSnapshotFile interface {
176 Printfln(format string, args ...interface{})
177 Indent()
178 Dedent()
179}