blob: a29ec911d7405d165c80c701f434fd24126e9bd5 [file] [log] [blame]
Colin Crossce75d2c2016-10-06 16:12:58 -07001// Copyright 2016 Google Inc. All rights reserved.
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
Colin Cross74d73e22017-08-02 11:05:49 -070017import (
18 "fmt"
Jaewoong Jung3e18b192019-06-11 12:25:34 -070019 "reflect"
Colin Cross74d73e22017-08-02 11:05:49 -070020
21 "github.com/google/blueprint"
Jaewoong Jung939ebd52019-03-26 15:07:36 -070022 "github.com/google/blueprint/proptools"
Colin Cross74d73e22017-08-02 11:05:49 -070023)
Colin Crossce75d2c2016-10-06 16:12:58 -070024
25// This file implements common functionality for handling modules that may exist as prebuilts,
26// source, or both.
27
Paul Duffin0c4979b2019-12-19 15:11:53 +000028func RegisterPrebuiltMutators(ctx RegistrationContext) {
29 ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators)
30 ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators)
31}
32
Nan Zhang2502e122017-03-09 18:43:01 -080033type prebuiltDependencyTag struct {
34 blueprint.BaseDependencyTag
35}
36
Jiyong Park03b68dd2019-07-26 23:20:40 +090037var PrebuiltDepTag prebuiltDependencyTag
Colin Crossce75d2c2016-10-06 16:12:58 -070038
Paul Duffin78ac5b92020-01-14 12:42:08 +000039// Mark this tag so dependencies that use it are excluded from visibility enforcement.
40func (t prebuiltDependencyTag) ExcludeFromVisibilityEnforcement() {}
41
Paul Duffindddd5462020-04-07 15:25:44 +010042// Mark this tag so dependencies that use it are excluded from APEX contents.
43func (t prebuiltDependencyTag) ExcludeFromApexContents() {}
44
45var _ ExcludeFromVisibilityEnforcementTag = PrebuiltDepTag
46var _ ExcludeFromApexContentsTag = PrebuiltDepTag
47
Colin Cross74d73e22017-08-02 11:05:49 -070048type PrebuiltProperties struct {
49 // When prefer is set to true the prebuilt will be used instead of any source module with
50 // a matching name.
Nan Zhang0007d812017-11-07 10:57:05 -080051 Prefer *bool `android:"arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -070052
Colin Cross74d73e22017-08-02 11:05:49 -070053 SourceExists bool `blueprint:"mutated"`
54 UsePrebuilt bool `blueprint:"mutated"`
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000055
56 // Set if the module has been renamed to remove the "prebuilt_" prefix.
57 PrebuiltRenamedToSource bool `blueprint:"mutated"`
Colin Cross74d73e22017-08-02 11:05:49 -070058}
59
60type Prebuilt struct {
61 properties PrebuiltProperties
Jaewoong Jung3e18b192019-06-11 12:25:34 -070062
Paul Duffindcb4bd62020-03-12 23:43:52 +000063 srcsSupplier PrebuiltSrcsSupplier
64 srcsPropertyName string
Colin Crossce75d2c2016-10-06 16:12:58 -070065}
66
67func (p *Prebuilt) Name(name string) string {
68 return "prebuilt_" + name
69}
70
Paul Duffin50061512020-01-21 16:31:05 +000071func (p *Prebuilt) ForcePrefer() {
72 p.properties.Prefer = proptools.BoolPtr(true)
73}
74
Paul Duffin38b57852020-05-13 16:08:09 +010075func (p *Prebuilt) Prefer() bool {
76 return proptools.Bool(p.properties.Prefer)
77}
78
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070079// The below source-related functions and the srcs, src fields are based on an assumption that
80// prebuilt modules have a static source property at the moment. Currently there is only one
81// exception, android_app_import, which chooses a source file depending on the product's DPI
82// preference configs. We'll want to add native support for dynamic source cases if we end up having
83// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -070084func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Paul Duffindcb4bd62020-03-12 23:43:52 +000085 if p.srcsSupplier != nil {
86 srcs := p.srcsSupplier()
87
88 if len(srcs) == 0 {
89 ctx.PropertyErrorf(p.srcsPropertyName, "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -070090 return nil
91 }
Colin Crossce75d2c2016-10-06 16:12:58 -070092
Paul Duffindcb4bd62020-03-12 23:43:52 +000093 if len(srcs) > 1 {
94 ctx.PropertyErrorf(p.srcsPropertyName, "multiple prebuilt source files")
Jaewoong Jung939ebd52019-03-26 15:07:36 -070095 return nil
96 }
Colin Crossce75d2c2016-10-06 16:12:58 -070097
Jaewoong Jung939ebd52019-03-26 15:07:36 -070098 // Return the singleton source after expanding any filegroup in the
99 // sources.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000100 src := srcs[0]
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700101 return PathForModuleSrc(ctx, src)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000102 } else {
103 ctx.ModuleErrorf("prebuilt source was not set")
104 return nil
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700105 }
Colin Cross74d73e22017-08-02 11:05:49 -0700106}
107
Jiyong Park4d277042019-04-23 18:00:10 +0900108func (p *Prebuilt) UsePrebuilt() bool {
109 return p.properties.UsePrebuilt
110}
111
Paul Duffindcb4bd62020-03-12 23:43:52 +0000112// Called to provide the srcs value for the prebuilt module.
113//
114// Return the src value or nil if it is not available.
115type PrebuiltSrcsSupplier func() []string
116
117// Initialize the module as a prebuilt module that uses the provided supplier to access the
118// prebuilt sources of the module.
119//
120// The supplier will be called multiple times and must return the same values each time it
121// is called. If it returns an empty array (or nil) then the prebuilt module will not be used
122// as a replacement for a source module with the same name even if prefer = true.
123//
124// If the Prebuilt.SingleSourcePath() is called on the module then this must return an array
125// containing exactly one source file.
126//
127// The provided property name is used to provide helpful error messages in the event that
128// a problem arises, e.g. calling SingleSourcePath() when more than one source is provided.
129func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) {
Colin Cross74d73e22017-08-02 11:05:49 -0700130 p := module.Prebuilt()
131 module.AddProperties(&p.properties)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000132
133 if srcsSupplier == nil {
134 panic(fmt.Errorf("srcsSupplier must not be nil"))
135 }
136 if srcsPropertyName == "" {
137 panic(fmt.Errorf("srcsPropertyName must not be empty"))
138 }
139
140 p.srcsSupplier = srcsSupplier
141 p.srcsPropertyName = srcsPropertyName
142}
143
144func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
145 if srcs == nil {
146 panic(fmt.Errorf("srcs must not be nil"))
147 }
148
149 srcsSupplier := func() []string {
150 return *srcs
151 }
152
153 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Colin Crossce75d2c2016-10-06 16:12:58 -0700154}
155
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700156func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000157 srcPropsValue := reflect.ValueOf(srcProps).Elem()
158 srcStructField, _ := srcPropsValue.Type().FieldByName(srcField)
159 if !srcPropsValue.IsValid() || srcStructField.Name == "" {
160 panic(fmt.Errorf("invalid single source prebuilt %+v", module))
161 }
162
163 if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface {
164 panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps))
165 }
166
167 srcFieldIndex := srcStructField.Index
168 srcPropertyName := proptools.PropertyNameForField(srcField)
169
170 srcsSupplier := func() []string {
171 value := srcPropsValue.FieldByIndex(srcFieldIndex)
172 if value.Kind() == reflect.Ptr {
173 value = value.Elem()
174 }
175 if value.Kind() != reflect.String {
176 panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one but was %d %q", srcPropertyName, value.Kind(), value))
177 }
178 src := value.String()
179 if src == "" {
180 return nil
181 }
182 return []string{src}
183 }
184
185 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700186}
187
Colin Crossce75d2c2016-10-06 16:12:58 -0700188type PrebuiltInterface interface {
189 Module
190 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700191}
192
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700193func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000194 ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700195}
196
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700197func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000198 ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700199 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700200 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700201}
202
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000203// PrebuiltRenameMutator ensures that there always is a module with an
204// undecorated name.
205func PrebuiltRenameMutator(ctx BottomUpMutatorContext) {
206 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
207 name := m.base().BaseModuleName()
208 if !ctx.OtherModuleExists(name) {
209 ctx.Rename(name)
210 m.Prebuilt().properties.PrebuiltRenamedToSource = true
211 }
212 }
213}
214
215// PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the
216// corresponding source module, if one exists for the same variant.
217func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700218 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
219 p := m.Prebuilt()
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000220 if !p.properties.PrebuiltRenamedToSource {
221 name := m.base().BaseModuleName()
222 if ctx.OtherModuleReverseDependencyVariantExists(name) {
223 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
224 p.properties.SourceExists = true
225 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700226 }
227 }
228}
229
Colin Crossc3e7fa62017-03-17 13:14:32 -0700230// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
231// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800232func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700233 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
234 p := m.Prebuilt()
Paul Duffindcb4bd62020-03-12 23:43:52 +0000235 if p.srcsSupplier == nil {
Colin Cross74d73e22017-08-02 11:05:49 -0700236 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
237 }
238 if !p.properties.SourceExists {
239 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700240 }
241 } else if s, ok := ctx.Module().(Module); ok {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900242 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800243 p := m.(PrebuiltInterface).Prebuilt()
244 if p.usePrebuilt(ctx, s) {
245 p.properties.UsePrebuilt = true
246 s.SkipInstall()
Colin Crossa2f296f2016-11-29 15:16:18 -0800247 }
248 })
249 }
250}
251
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700252// PrebuiltPostDepsMutator does two operations. It replace dependencies on the
253// source module with dependencies on the prebuilt when both modules exist and
254// the prebuilt should be used. When the prebuilt should not be used, disable
255// installing it. Secondly, it also adds a sourcegroup to any filegroups found
256// in the prebuilt's 'Srcs' property.
257func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700258 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
259 p := m.Prebuilt()
260 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700261 if p.properties.UsePrebuilt {
262 if p.properties.SourceExists {
Colin Cross0f3c72f2016-11-23 15:44:07 -0800263 ctx.ReplaceDependencies(name)
264 }
265 } else {
266 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700267 }
268 }
269}
270
Colin Crossa2f296f2016-11-29 15:16:18 -0800271// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
272// will be used if it is marked "prefer" or if the source module is disabled.
273func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000274 if p.srcsSupplier != nil && len(p.srcsSupplier()) == 0 {
Colin Crossa2f296f2016-11-29 15:16:18 -0800275 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700276 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700277
Colin Crossa2f296f2016-11-29 15:16:18 -0800278 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800279 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800280 return true
281 }
282
Colin Crossc3e7fa62017-03-17 13:14:32 -0700283 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700284}
Jiyong Park0a573d72019-07-07 12:39:16 +0900285
286func (p *Prebuilt) SourceExists() bool {
287 return p.properties.SourceExists
288}