blob: 8114a65a56b2d9c805b43af2a99687db1ea675cd [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 {
85 return "prebuilt_" + name
86}
87
Paul Duffin50061512020-01-21 16:31:05 +000088func (p *Prebuilt) ForcePrefer() {
89 p.properties.Prefer = proptools.BoolPtr(true)
90}
91
Paul Duffin38b57852020-05-13 16:08:09 +010092func (p *Prebuilt) Prefer() bool {
93 return proptools.Bool(p.properties.Prefer)
94}
95
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070096// The below source-related functions and the srcs, src fields are based on an assumption that
97// prebuilt modules have a static source property at the moment. Currently there is only one
98// exception, android_app_import, which chooses a source file depending on the product's DPI
99// preference configs. We'll want to add native support for dynamic source cases if we end up having
100// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -0700101func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000102 if p.srcsSupplier != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700103 srcs := p.srcsSupplier(ctx)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000104
105 if len(srcs) == 0 {
106 ctx.PropertyErrorf(p.srcsPropertyName, "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700107 return nil
108 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700109
Paul Duffindcb4bd62020-03-12 23:43:52 +0000110 if len(srcs) > 1 {
111 ctx.PropertyErrorf(p.srcsPropertyName, "multiple prebuilt source files")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700112 return nil
113 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700114
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700115 // Return the singleton source after expanding any filegroup in the
116 // sources.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000117 src := srcs[0]
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700118 return PathForModuleSrc(ctx, src)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000119 } else {
120 ctx.ModuleErrorf("prebuilt source was not set")
121 return nil
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700122 }
Colin Cross74d73e22017-08-02 11:05:49 -0700123}
124
Jiyong Park4d277042019-04-23 18:00:10 +0900125func (p *Prebuilt) UsePrebuilt() bool {
126 return p.properties.UsePrebuilt
127}
128
Paul Duffindcb4bd62020-03-12 23:43:52 +0000129// Called to provide the srcs value for the prebuilt module.
130//
131// Return the src value or nil if it is not available.
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700132type PrebuiltSrcsSupplier func(ctx BaseModuleContext) []string
Paul Duffindcb4bd62020-03-12 23:43:52 +0000133
134// Initialize the module as a prebuilt module that uses the provided supplier to access the
135// prebuilt sources of the module.
136//
137// The supplier will be called multiple times and must return the same values each time it
138// is called. If it returns an empty array (or nil) then the prebuilt module will not be used
139// as a replacement for a source module with the same name even if prefer = true.
140//
141// If the Prebuilt.SingleSourcePath() is called on the module then this must return an array
142// containing exactly one source file.
143//
144// The provided property name is used to provide helpful error messages in the event that
145// a problem arises, e.g. calling SingleSourcePath() when more than one source is provided.
146func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) {
Colin Cross74d73e22017-08-02 11:05:49 -0700147 p := module.Prebuilt()
148 module.AddProperties(&p.properties)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000149
150 if srcsSupplier == nil {
151 panic(fmt.Errorf("srcsSupplier must not be nil"))
152 }
153 if srcsPropertyName == "" {
154 panic(fmt.Errorf("srcsPropertyName must not be empty"))
155 }
156
157 p.srcsSupplier = srcsSupplier
158 p.srcsPropertyName = srcsPropertyName
159}
160
161func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
162 if srcs == nil {
163 panic(fmt.Errorf("srcs must not be nil"))
164 }
165
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700166 srcsSupplier := func(ctx BaseModuleContext) []string {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000167 return *srcs
168 }
169
170 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Colin Crossce75d2c2016-10-06 16:12:58 -0700171}
172
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700173func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000174 srcPropsValue := reflect.ValueOf(srcProps).Elem()
175 srcStructField, _ := srcPropsValue.Type().FieldByName(srcField)
176 if !srcPropsValue.IsValid() || srcStructField.Name == "" {
177 panic(fmt.Errorf("invalid single source prebuilt %+v", module))
178 }
179
180 if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface {
181 panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps))
182 }
183
184 srcFieldIndex := srcStructField.Index
185 srcPropertyName := proptools.PropertyNameForField(srcField)
186
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700187 srcsSupplier := func(ctx BaseModuleContext) []string {
Jaewoong Jung729c0bd2020-12-08 19:11:54 -0800188 if !module.Enabled() {
Jaewoong Jung84f1b802020-12-04 11:51:29 -0800189 return nil
190 }
Paul Duffindcb4bd62020-03-12 23:43:52 +0000191 value := srcPropsValue.FieldByIndex(srcFieldIndex)
192 if value.Kind() == reflect.Ptr {
193 value = value.Elem()
194 }
195 if value.Kind() != reflect.String {
196 panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one but was %d %q", srcPropertyName, value.Kind(), value))
197 }
198 src := value.String()
199 if src == "" {
200 return nil
201 }
202 return []string{src}
203 }
204
205 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700206}
207
Colin Crossce75d2c2016-10-06 16:12:58 -0700208type PrebuiltInterface interface {
209 Module
210 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700211}
212
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700213func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000214 ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700215}
216
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700217func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000218 ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700219 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700220 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700221}
222
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000223// PrebuiltRenameMutator ensures that there always is a module with an
224// undecorated name.
225func PrebuiltRenameMutator(ctx BottomUpMutatorContext) {
226 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
227 name := m.base().BaseModuleName()
228 if !ctx.OtherModuleExists(name) {
229 ctx.Rename(name)
230 m.Prebuilt().properties.PrebuiltRenamedToSource = true
231 }
232 }
233}
234
235// PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the
236// corresponding source module, if one exists for the same variant.
237func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) {
Martin Stjernholmf4677322020-07-07 02:20:40 +0100238 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Enabled() && m.Prebuilt() != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700239 p := m.Prebuilt()
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000240 if !p.properties.PrebuiltRenamedToSource {
241 name := m.base().BaseModuleName()
242 if ctx.OtherModuleReverseDependencyVariantExists(name) {
243 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
244 p.properties.SourceExists = true
245 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700246 }
247 }
248}
249
Colin Crossc3e7fa62017-03-17 13:14:32 -0700250// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
251// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800252func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700253 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
254 p := m.Prebuilt()
Paul Duffindcb4bd62020-03-12 23:43:52 +0000255 if p.srcsSupplier == nil {
Colin Cross74d73e22017-08-02 11:05:49 -0700256 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
257 }
258 if !p.properties.SourceExists {
259 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700260 }
261 } else if s, ok := ctx.Module().(Module); ok {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900262 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800263 p := m.(PrebuiltInterface).Prebuilt()
264 if p.usePrebuilt(ctx, s) {
265 p.properties.UsePrebuilt = true
Liz Kammer5ca3a622020-08-05 15:40:41 -0700266 s.ReplacedByPrebuilt()
Colin Crossa2f296f2016-11-29 15:16:18 -0800267 }
268 })
269 }
270}
271
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700272// PrebuiltPostDepsMutator does two operations. It replace dependencies on the
273// source module with dependencies on the prebuilt when both modules exist and
274// the prebuilt should be used. When the prebuilt should not be used, disable
275// installing it. Secondly, it also adds a sourcegroup to any filegroups found
276// in the prebuilt's 'Srcs' property.
277func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700278 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
279 p := m.Prebuilt()
280 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700281 if p.properties.UsePrebuilt {
282 if p.properties.SourceExists {
Paul Duffin80342d72020-06-26 22:08:43 +0100283 ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
284 if t, ok := tag.(ReplaceSourceWithPrebuilt); ok {
285 return t.ReplaceSourceWithPrebuilt()
286 }
287
288 return true
289 })
Colin Cross0f3c72f2016-11-23 15:44:07 -0800290 }
291 } else {
292 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700293 }
294 }
295}
296
Colin Crossa2f296f2016-11-29 15:16:18 -0800297// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
298// will be used if it is marked "prefer" or if the source module is disabled.
299func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700300 if p.srcsSupplier != nil && len(p.srcsSupplier(ctx)) == 0 {
Colin Crossa2f296f2016-11-29 15:16:18 -0800301 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700302 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700303
Colin Crossa2f296f2016-11-29 15:16:18 -0800304 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800305 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800306 return true
307 }
308
Colin Crossc3e7fa62017-03-17 13:14:32 -0700309 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700310}
Jiyong Park0a573d72019-07-07 12:39:16 +0900311
312func (p *Prebuilt) SourceExists() bool {
313 return p.properties.SourceExists
314}