blob: 8176299481c058ca1b905600ce692ee2ff1d55d9 [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"
Paul Duffind23c7262020-12-11 18:13:08 +000020 "strings"
Colin Cross74d73e22017-08-02 11:05:49 -070021
22 "github.com/google/blueprint"
Jaewoong Jung939ebd52019-03-26 15:07:36 -070023 "github.com/google/blueprint/proptools"
Colin Cross74d73e22017-08-02 11:05:49 -070024)
Colin Crossce75d2c2016-10-06 16:12:58 -070025
26// This file implements common functionality for handling modules that may exist as prebuilts,
27// source, or both.
28
Paul Duffin0c4979b2019-12-19 15:11:53 +000029func RegisterPrebuiltMutators(ctx RegistrationContext) {
30 ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators)
31 ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators)
32}
33
Paul Duffin80342d72020-06-26 22:08:43 +010034// Marks a dependency tag as possibly preventing a reference to a source from being
35// replaced with the prebuilt.
36type ReplaceSourceWithPrebuilt interface {
37 blueprint.DependencyTag
38
39 // Return true if the dependency defined by this tag should be replaced with the
40 // prebuilt.
41 ReplaceSourceWithPrebuilt() bool
42}
43
Nan Zhang2502e122017-03-09 18:43:01 -080044type prebuiltDependencyTag struct {
45 blueprint.BaseDependencyTag
46}
47
Jiyong Park03b68dd2019-07-26 23:20:40 +090048var PrebuiltDepTag prebuiltDependencyTag
Colin Crossce75d2c2016-10-06 16:12:58 -070049
Paul Duffin78ac5b92020-01-14 12:42:08 +000050// Mark this tag so dependencies that use it are excluded from visibility enforcement.
51func (t prebuiltDependencyTag) ExcludeFromVisibilityEnforcement() {}
52
Paul Duffindddd5462020-04-07 15:25:44 +010053// Mark this tag so dependencies that use it are excluded from APEX contents.
54func (t prebuiltDependencyTag) ExcludeFromApexContents() {}
55
56var _ ExcludeFromVisibilityEnforcementTag = PrebuiltDepTag
57var _ ExcludeFromApexContentsTag = PrebuiltDepTag
58
Colin Cross74d73e22017-08-02 11:05:49 -070059type PrebuiltProperties struct {
60 // When prefer is set to true the prebuilt will be used instead of any source module with
61 // a matching name.
Nan Zhang0007d812017-11-07 10:57:05 -080062 Prefer *bool `android:"arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -070063
Colin Cross74d73e22017-08-02 11:05:49 -070064 SourceExists bool `blueprint:"mutated"`
65 UsePrebuilt bool `blueprint:"mutated"`
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000066
67 // Set if the module has been renamed to remove the "prebuilt_" prefix.
68 PrebuiltRenamedToSource bool `blueprint:"mutated"`
Colin Cross74d73e22017-08-02 11:05:49 -070069}
70
71type Prebuilt struct {
72 properties PrebuiltProperties
Jaewoong Jung3e18b192019-06-11 12:25:34 -070073
Paul Duffindcb4bd62020-03-12 23:43:52 +000074 srcsSupplier PrebuiltSrcsSupplier
75 srcsPropertyName string
Colin Crossce75d2c2016-10-06 16:12:58 -070076}
77
Paul Duffind23c7262020-12-11 18:13:08 +000078// RemoveOptionalPrebuiltPrefix returns the result of removing the "prebuilt_" prefix from the
79// supplied name if it has one, or returns the name unmodified if it does not.
80func RemoveOptionalPrebuiltPrefix(name string) string {
81 return strings.TrimPrefix(name, "prebuilt_")
82}
83
Colin Crossce75d2c2016-10-06 16:12:58 -070084func (p *Prebuilt) Name(name string) string {
Paul Duffin864116c2021-04-02 10:24:13 +010085 return PrebuiltNameFromSource(name)
86}
87
88// PrebuiltNameFromSource returns the result of prepending the "prebuilt_" prefix to the supplied
89// name.
90func PrebuiltNameFromSource(name string) string {
Colin Crossce75d2c2016-10-06 16:12:58 -070091 return "prebuilt_" + name
92}
93
Paul Duffin50061512020-01-21 16:31:05 +000094func (p *Prebuilt) ForcePrefer() {
95 p.properties.Prefer = proptools.BoolPtr(true)
96}
97
Paul Duffin38b57852020-05-13 16:08:09 +010098func (p *Prebuilt) Prefer() bool {
99 return proptools.Bool(p.properties.Prefer)
100}
101
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700102// The below source-related functions and the srcs, src fields are based on an assumption that
103// prebuilt modules have a static source property at the moment. Currently there is only one
104// exception, android_app_import, which chooses a source file depending on the product's DPI
105// preference configs. We'll want to add native support for dynamic source cases if we end up having
106// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -0700107func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000108 if p.srcsSupplier != nil {
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000109 srcs := p.srcsSupplier(ctx, ctx.Module())
Paul Duffindcb4bd62020-03-12 23:43:52 +0000110
111 if len(srcs) == 0 {
112 ctx.PropertyErrorf(p.srcsPropertyName, "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700113 return nil
114 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700115
Paul Duffindcb4bd62020-03-12 23:43:52 +0000116 if len(srcs) > 1 {
117 ctx.PropertyErrorf(p.srcsPropertyName, "multiple prebuilt source files")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700118 return nil
119 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700120
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700121 // Return the singleton source after expanding any filegroup in the
122 // sources.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000123 src := srcs[0]
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700124 return PathForModuleSrc(ctx, src)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000125 } else {
126 ctx.ModuleErrorf("prebuilt source was not set")
127 return nil
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700128 }
Colin Cross74d73e22017-08-02 11:05:49 -0700129}
130
Jiyong Park4d277042019-04-23 18:00:10 +0900131func (p *Prebuilt) UsePrebuilt() bool {
132 return p.properties.UsePrebuilt
133}
134
Paul Duffindcb4bd62020-03-12 23:43:52 +0000135// Called to provide the srcs value for the prebuilt module.
136//
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000137// This can be called with a context for any module not just the prebuilt one itself. It can also be
138// called concurrently.
139//
Paul Duffindcb4bd62020-03-12 23:43:52 +0000140// Return the src value or nil if it is not available.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000141type PrebuiltSrcsSupplier func(ctx BaseModuleContext, prebuilt Module) []string
Paul Duffindcb4bd62020-03-12 23:43:52 +0000142
143// Initialize the module as a prebuilt module that uses the provided supplier to access the
144// prebuilt sources of the module.
145//
146// The supplier will be called multiple times and must return the same values each time it
147// is called. If it returns an empty array (or nil) then the prebuilt module will not be used
148// as a replacement for a source module with the same name even if prefer = true.
149//
150// If the Prebuilt.SingleSourcePath() is called on the module then this must return an array
151// containing exactly one source file.
152//
153// The provided property name is used to provide helpful error messages in the event that
154// a problem arises, e.g. calling SingleSourcePath() when more than one source is provided.
155func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) {
Colin Cross74d73e22017-08-02 11:05:49 -0700156 p := module.Prebuilt()
157 module.AddProperties(&p.properties)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000158
159 if srcsSupplier == nil {
160 panic(fmt.Errorf("srcsSupplier must not be nil"))
161 }
162 if srcsPropertyName == "" {
163 panic(fmt.Errorf("srcsPropertyName must not be empty"))
164 }
165
166 p.srcsSupplier = srcsSupplier
167 p.srcsPropertyName = srcsPropertyName
168}
169
170func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
171 if srcs == nil {
172 panic(fmt.Errorf("srcs must not be nil"))
173 }
174
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000175 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000176 return *srcs
177 }
178
179 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Colin Crossce75d2c2016-10-06 16:12:58 -0700180}
181
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700182func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000183 srcPropsValue := reflect.ValueOf(srcProps).Elem()
184 srcStructField, _ := srcPropsValue.Type().FieldByName(srcField)
185 if !srcPropsValue.IsValid() || srcStructField.Name == "" {
186 panic(fmt.Errorf("invalid single source prebuilt %+v", module))
187 }
188
189 if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface {
190 panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps))
191 }
192
193 srcFieldIndex := srcStructField.Index
194 srcPropertyName := proptools.PropertyNameForField(srcField)
195
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000196 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
Jaewoong Jung729c0bd2020-12-08 19:11:54 -0800197 if !module.Enabled() {
Jaewoong Jung84f1b802020-12-04 11:51:29 -0800198 return nil
199 }
Paul Duffindcb4bd62020-03-12 23:43:52 +0000200 value := srcPropsValue.FieldByIndex(srcFieldIndex)
201 if value.Kind() == reflect.Ptr {
202 value = value.Elem()
203 }
204 if value.Kind() != reflect.String {
205 panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one but was %d %q", srcPropertyName, value.Kind(), value))
206 }
207 src := value.String()
208 if src == "" {
209 return nil
210 }
211 return []string{src}
212 }
213
214 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700215}
216
Colin Crossce75d2c2016-10-06 16:12:58 -0700217type PrebuiltInterface interface {
218 Module
219 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700220}
221
Paul Duffine1d38372021-04-02 10:35:24 +0100222// IsModulePreferred returns true if the given module is preferred.
223//
224// A source module is preferred if there is no corresponding prebuilt module or the prebuilt module
225// does not have "prefer: true".
226//
227// A prebuilt module is preferred if there is no corresponding source module or the prebuilt module
228// has "prefer: true".
229func IsModulePreferred(module Module) bool {
230 if module.IsReplacedByPrebuilt() {
231 // A source module that has been replaced by a prebuilt counterpart.
232 return false
233 }
234 if prebuilt, ok := module.(PrebuiltInterface); ok {
235 if p := prebuilt.Prebuilt(); p != nil {
236 return p.UsePrebuilt()
237 }
238 }
239 return true
240}
241
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700242func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000243 ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700244}
245
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700246func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000247 ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700248 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700249 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700250}
251
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000252// PrebuiltRenameMutator ensures that there always is a module with an
253// undecorated name.
254func PrebuiltRenameMutator(ctx BottomUpMutatorContext) {
255 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
256 name := m.base().BaseModuleName()
257 if !ctx.OtherModuleExists(name) {
258 ctx.Rename(name)
259 m.Prebuilt().properties.PrebuiltRenamedToSource = true
260 }
261 }
262}
263
264// PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the
265// corresponding source module, if one exists for the same variant.
266func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) {
Martin Stjernholmf4677322020-07-07 02:20:40 +0100267 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Enabled() && m.Prebuilt() != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700268 p := m.Prebuilt()
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000269 if !p.properties.PrebuiltRenamedToSource {
270 name := m.base().BaseModuleName()
271 if ctx.OtherModuleReverseDependencyVariantExists(name) {
272 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
273 p.properties.SourceExists = true
274 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700275 }
276 }
277}
278
Colin Crossc3e7fa62017-03-17 13:14:32 -0700279// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
280// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800281func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700282 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
283 p := m.Prebuilt()
Paul Duffindcb4bd62020-03-12 23:43:52 +0000284 if p.srcsSupplier == nil {
Colin Cross74d73e22017-08-02 11:05:49 -0700285 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
286 }
287 if !p.properties.SourceExists {
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000288 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil, m)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700289 }
290 } else if s, ok := ctx.Module().(Module); ok {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900291 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800292 p := m.(PrebuiltInterface).Prebuilt()
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000293 if p.usePrebuilt(ctx, s, m) {
Colin Crossee6143c2017-12-30 17:54:27 -0800294 p.properties.UsePrebuilt = true
Liz Kammer5ca3a622020-08-05 15:40:41 -0700295 s.ReplacedByPrebuilt()
Colin Crossa2f296f2016-11-29 15:16:18 -0800296 }
297 })
298 }
299}
300
Jaewoong Jung6158dfe2021-03-23 14:08:29 -0700301// PrebuiltPostDepsMutator replaces dependencies on the source module with dependencies on the
302// prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not
303// be used, disable installing it.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700304func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700305 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
306 p := m.Prebuilt()
307 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700308 if p.properties.UsePrebuilt {
309 if p.properties.SourceExists {
Paul Duffin80342d72020-06-26 22:08:43 +0100310 ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
311 if t, ok := tag.(ReplaceSourceWithPrebuilt); ok {
312 return t.ReplaceSourceWithPrebuilt()
313 }
314
315 return true
316 })
Colin Cross0f3c72f2016-11-23 15:44:07 -0800317 }
318 } else {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800319 m.HideFromMake()
Colin Crossce75d2c2016-10-06 16:12:58 -0700320 }
321 }
322}
323
Colin Crossa2f296f2016-11-29 15:16:18 -0800324// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
325// will be used if it is marked "prefer" or if the source module is disabled.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000326func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module, prebuilt Module) bool {
327 if p.srcsSupplier != nil && len(p.srcsSupplier(ctx, prebuilt)) == 0 {
Colin Crossa2f296f2016-11-29 15:16:18 -0800328 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700329 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700330
Colin Crossa2f296f2016-11-29 15:16:18 -0800331 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800332 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800333 return true
334 }
335
Colin Crossc3e7fa62017-03-17 13:14:32 -0700336 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700337}
Jiyong Park0a573d72019-07-07 12:39:16 +0900338
339func (p *Prebuilt) SourceExists() bool {
340 return p.properties.SourceExists
341}