blob: 98cb5723a6978b1a4df2cede22fc818e4f226aa5 [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
Paul Duffin80342d72020-06-26 22:08:43 +010033// Marks a dependency tag as possibly preventing a reference to a source from being
34// replaced with the prebuilt.
35type ReplaceSourceWithPrebuilt interface {
36 blueprint.DependencyTag
37
38 // Return true if the dependency defined by this tag should be replaced with the
39 // prebuilt.
40 ReplaceSourceWithPrebuilt() bool
41}
42
Nan Zhang2502e122017-03-09 18:43:01 -080043type prebuiltDependencyTag struct {
44 blueprint.BaseDependencyTag
45}
46
Jiyong Park03b68dd2019-07-26 23:20:40 +090047var PrebuiltDepTag prebuiltDependencyTag
Colin Crossce75d2c2016-10-06 16:12:58 -070048
Paul Duffin78ac5b92020-01-14 12:42:08 +000049// Mark this tag so dependencies that use it are excluded from visibility enforcement.
50func (t prebuiltDependencyTag) ExcludeFromVisibilityEnforcement() {}
51
Paul Duffindddd5462020-04-07 15:25:44 +010052// Mark this tag so dependencies that use it are excluded from APEX contents.
53func (t prebuiltDependencyTag) ExcludeFromApexContents() {}
54
55var _ ExcludeFromVisibilityEnforcementTag = PrebuiltDepTag
56var _ ExcludeFromApexContentsTag = PrebuiltDepTag
57
Colin Cross74d73e22017-08-02 11:05:49 -070058type PrebuiltProperties struct {
59 // When prefer is set to true the prebuilt will be used instead of any source module with
60 // a matching name.
Nan Zhang0007d812017-11-07 10:57:05 -080061 Prefer *bool `android:"arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -070062
Colin Cross74d73e22017-08-02 11:05:49 -070063 SourceExists bool `blueprint:"mutated"`
64 UsePrebuilt bool `blueprint:"mutated"`
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000065
66 // Set if the module has been renamed to remove the "prebuilt_" prefix.
67 PrebuiltRenamedToSource bool `blueprint:"mutated"`
Colin Cross74d73e22017-08-02 11:05:49 -070068}
69
70type Prebuilt struct {
71 properties PrebuiltProperties
Jaewoong Jung3e18b192019-06-11 12:25:34 -070072
Paul Duffindcb4bd62020-03-12 23:43:52 +000073 srcsSupplier PrebuiltSrcsSupplier
74 srcsPropertyName string
Colin Crossce75d2c2016-10-06 16:12:58 -070075}
76
77func (p *Prebuilt) Name(name string) string {
78 return "prebuilt_" + name
79}
80
Paul Duffin50061512020-01-21 16:31:05 +000081func (p *Prebuilt) ForcePrefer() {
82 p.properties.Prefer = proptools.BoolPtr(true)
83}
84
Paul Duffin38b57852020-05-13 16:08:09 +010085func (p *Prebuilt) Prefer() bool {
86 return proptools.Bool(p.properties.Prefer)
87}
88
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070089// The below source-related functions and the srcs, src fields are based on an assumption that
90// prebuilt modules have a static source property at the moment. Currently there is only one
91// exception, android_app_import, which chooses a source file depending on the product's DPI
92// preference configs. We'll want to add native support for dynamic source cases if we end up having
93// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -070094func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Paul Duffindcb4bd62020-03-12 23:43:52 +000095 if p.srcsSupplier != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070096 srcs := p.srcsSupplier(ctx)
Paul Duffindcb4bd62020-03-12 23:43:52 +000097
98 if len(srcs) == 0 {
99 ctx.PropertyErrorf(p.srcsPropertyName, "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700100 return nil
101 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700102
Paul Duffindcb4bd62020-03-12 23:43:52 +0000103 if len(srcs) > 1 {
104 ctx.PropertyErrorf(p.srcsPropertyName, "multiple prebuilt source files")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700105 return nil
106 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700107
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700108 // Return the singleton source after expanding any filegroup in the
109 // sources.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000110 src := srcs[0]
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700111 return PathForModuleSrc(ctx, src)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000112 } else {
113 ctx.ModuleErrorf("prebuilt source was not set")
114 return nil
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700115 }
Colin Cross74d73e22017-08-02 11:05:49 -0700116}
117
Jiyong Park4d277042019-04-23 18:00:10 +0900118func (p *Prebuilt) UsePrebuilt() bool {
119 return p.properties.UsePrebuilt
120}
121
Paul Duffindcb4bd62020-03-12 23:43:52 +0000122// Called to provide the srcs value for the prebuilt module.
123//
124// Return the src value or nil if it is not available.
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700125type PrebuiltSrcsSupplier func(ctx BaseModuleContext) []string
Paul Duffindcb4bd62020-03-12 23:43:52 +0000126
127// Initialize the module as a prebuilt module that uses the provided supplier to access the
128// prebuilt sources of the module.
129//
130// The supplier will be called multiple times and must return the same values each time it
131// is called. If it returns an empty array (or nil) then the prebuilt module will not be used
132// as a replacement for a source module with the same name even if prefer = true.
133//
134// If the Prebuilt.SingleSourcePath() is called on the module then this must return an array
135// containing exactly one source file.
136//
137// The provided property name is used to provide helpful error messages in the event that
138// a problem arises, e.g. calling SingleSourcePath() when more than one source is provided.
139func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) {
Colin Cross74d73e22017-08-02 11:05:49 -0700140 p := module.Prebuilt()
141 module.AddProperties(&p.properties)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000142
143 if srcsSupplier == nil {
144 panic(fmt.Errorf("srcsSupplier must not be nil"))
145 }
146 if srcsPropertyName == "" {
147 panic(fmt.Errorf("srcsPropertyName must not be empty"))
148 }
149
150 p.srcsSupplier = srcsSupplier
151 p.srcsPropertyName = srcsPropertyName
152}
153
154func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
155 if srcs == nil {
156 panic(fmt.Errorf("srcs must not be nil"))
157 }
158
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700159 srcsSupplier := func(ctx BaseModuleContext) []string {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000160 return *srcs
161 }
162
163 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Colin Crossce75d2c2016-10-06 16:12:58 -0700164}
165
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700166func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000167 srcPropsValue := reflect.ValueOf(srcProps).Elem()
168 srcStructField, _ := srcPropsValue.Type().FieldByName(srcField)
169 if !srcPropsValue.IsValid() || srcStructField.Name == "" {
170 panic(fmt.Errorf("invalid single source prebuilt %+v", module))
171 }
172
173 if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface {
174 panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps))
175 }
176
177 srcFieldIndex := srcStructField.Index
178 srcPropertyName := proptools.PropertyNameForField(srcField)
179
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700180 srcsSupplier := func(ctx BaseModuleContext) []string {
Jaewoong Jung84f1b802020-12-04 11:51:29 -0800181 if !ctx.Module().Enabled() {
182 return nil
183 }
Paul Duffindcb4bd62020-03-12 23:43:52 +0000184 value := srcPropsValue.FieldByIndex(srcFieldIndex)
185 if value.Kind() == reflect.Ptr {
186 value = value.Elem()
187 }
188 if value.Kind() != reflect.String {
189 panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one but was %d %q", srcPropertyName, value.Kind(), value))
190 }
191 src := value.String()
192 if src == "" {
193 return nil
194 }
195 return []string{src}
196 }
197
198 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700199}
200
Colin Crossce75d2c2016-10-06 16:12:58 -0700201type PrebuiltInterface interface {
202 Module
203 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700204}
205
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700206func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000207 ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700208}
209
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700210func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000211 ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700212 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700213 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700214}
215
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000216// PrebuiltRenameMutator ensures that there always is a module with an
217// undecorated name.
218func PrebuiltRenameMutator(ctx BottomUpMutatorContext) {
219 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
220 name := m.base().BaseModuleName()
221 if !ctx.OtherModuleExists(name) {
222 ctx.Rename(name)
223 m.Prebuilt().properties.PrebuiltRenamedToSource = true
224 }
225 }
226}
227
228// PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the
229// corresponding source module, if one exists for the same variant.
230func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) {
Martin Stjernholmf4677322020-07-07 02:20:40 +0100231 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Enabled() && m.Prebuilt() != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700232 p := m.Prebuilt()
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000233 if !p.properties.PrebuiltRenamedToSource {
234 name := m.base().BaseModuleName()
235 if ctx.OtherModuleReverseDependencyVariantExists(name) {
236 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
237 p.properties.SourceExists = true
238 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700239 }
240 }
241}
242
Colin Crossc3e7fa62017-03-17 13:14:32 -0700243// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
244// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800245func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700246 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
247 p := m.Prebuilt()
Paul Duffindcb4bd62020-03-12 23:43:52 +0000248 if p.srcsSupplier == nil {
Colin Cross74d73e22017-08-02 11:05:49 -0700249 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
250 }
251 if !p.properties.SourceExists {
252 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700253 }
254 } else if s, ok := ctx.Module().(Module); ok {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900255 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800256 p := m.(PrebuiltInterface).Prebuilt()
257 if p.usePrebuilt(ctx, s) {
258 p.properties.UsePrebuilt = true
Liz Kammer5ca3a622020-08-05 15:40:41 -0700259 s.ReplacedByPrebuilt()
Colin Crossa2f296f2016-11-29 15:16:18 -0800260 }
261 })
262 }
263}
264
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700265// PrebuiltPostDepsMutator does two operations. It replace dependencies on the
266// source module with dependencies on the prebuilt when both modules exist and
267// the prebuilt should be used. When the prebuilt should not be used, disable
268// installing it. Secondly, it also adds a sourcegroup to any filegroups found
269// in the prebuilt's 'Srcs' property.
270func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700271 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
272 p := m.Prebuilt()
273 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700274 if p.properties.UsePrebuilt {
275 if p.properties.SourceExists {
Paul Duffin80342d72020-06-26 22:08:43 +0100276 ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
277 if t, ok := tag.(ReplaceSourceWithPrebuilt); ok {
278 return t.ReplaceSourceWithPrebuilt()
279 }
280
281 return true
282 })
Colin Cross0f3c72f2016-11-23 15:44:07 -0800283 }
284 } else {
285 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700286 }
287 }
288}
289
Colin Crossa2f296f2016-11-29 15:16:18 -0800290// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
291// will be used if it is marked "prefer" or if the source module is disabled.
292func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700293 if p.srcsSupplier != nil && len(p.srcsSupplier(ctx)) == 0 {
Colin Crossa2f296f2016-11-29 15:16:18 -0800294 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700295 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700296
Colin Crossa2f296f2016-11-29 15:16:18 -0800297 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800298 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800299 return true
300 }
301
Colin Crossc3e7fa62017-03-17 13:14:32 -0700302 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700303}
Jiyong Park0a573d72019-07-07 12:39:16 +0900304
305func (p *Prebuilt) SourceExists() bool {
306 return p.properties.SourceExists
307}