blob: dfab13b9234d710d547d01b69773379f037a2c40 [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 Duffin2c5454f2020-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 Duffin3766cb72020-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"`
65}
66
67type Prebuilt struct {
68 properties PrebuiltProperties
Jaewoong Jung3e18b192019-06-11 12:25:34 -070069
Paul Duffin406ea1c2020-03-12 23:43:52 +000070 srcsSupplier PrebuiltSrcsSupplier
71 srcsPropertyName string
Colin Crossce75d2c2016-10-06 16:12:58 -070072}
73
74func (p *Prebuilt) Name(name string) string {
75 return "prebuilt_" + name
76}
77
Paul Duffin50061512020-01-21 16:31:05 +000078func (p *Prebuilt) ForcePrefer() {
79 p.properties.Prefer = proptools.BoolPtr(true)
80}
81
Paul Duffindd89a282020-05-13 16:08:09 +010082func (p *Prebuilt) Prefer() bool {
83 return proptools.Bool(p.properties.Prefer)
84}
85
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070086// The below source-related functions and the srcs, src fields are based on an assumption that
87// prebuilt modules have a static source property at the moment. Currently there is only one
88// exception, android_app_import, which chooses a source file depending on the product's DPI
89// preference configs. We'll want to add native support for dynamic source cases if we end up having
90// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -070091func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Paul Duffin406ea1c2020-03-12 23:43:52 +000092 if p.srcsSupplier != nil {
93 srcs := p.srcsSupplier()
94
95 if len(srcs) == 0 {
96 ctx.PropertyErrorf(p.srcsPropertyName, "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -070097 return nil
98 }
Colin Crossce75d2c2016-10-06 16:12:58 -070099
Paul Duffin406ea1c2020-03-12 23:43:52 +0000100 if len(srcs) > 1 {
101 ctx.PropertyErrorf(p.srcsPropertyName, "multiple prebuilt source files")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700102 return nil
103 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700104
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700105 // Return the singleton source after expanding any filegroup in the
106 // sources.
Paul Duffin406ea1c2020-03-12 23:43:52 +0000107 src := srcs[0]
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700108 return PathForModuleSrc(ctx, src)
Paul Duffin406ea1c2020-03-12 23:43:52 +0000109 } else {
110 ctx.ModuleErrorf("prebuilt source was not set")
111 return nil
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700112 }
Colin Cross74d73e22017-08-02 11:05:49 -0700113}
114
Jiyong Park4d277042019-04-23 18:00:10 +0900115func (p *Prebuilt) UsePrebuilt() bool {
116 return p.properties.UsePrebuilt
117}
118
Paul Duffin406ea1c2020-03-12 23:43:52 +0000119// Called to provide the srcs value for the prebuilt module.
120//
121// Return the src value or nil if it is not available.
122type PrebuiltSrcsSupplier func() []string
123
124// Initialize the module as a prebuilt module that uses the provided supplier to access the
125// prebuilt sources of the module.
126//
127// The supplier will be called multiple times and must return the same values each time it
128// is called. If it returns an empty array (or nil) then the prebuilt module will not be used
129// as a replacement for a source module with the same name even if prefer = true.
130//
131// If the Prebuilt.SingleSourcePath() is called on the module then this must return an array
132// containing exactly one source file.
133//
134// The provided property name is used to provide helpful error messages in the event that
135// a problem arises, e.g. calling SingleSourcePath() when more than one source is provided.
136func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) {
Colin Cross74d73e22017-08-02 11:05:49 -0700137 p := module.Prebuilt()
138 module.AddProperties(&p.properties)
Paul Duffin406ea1c2020-03-12 23:43:52 +0000139
140 if srcsSupplier == nil {
141 panic(fmt.Errorf("srcsSupplier must not be nil"))
142 }
143 if srcsPropertyName == "" {
144 panic(fmt.Errorf("srcsPropertyName must not be empty"))
145 }
146
147 p.srcsSupplier = srcsSupplier
148 p.srcsPropertyName = srcsPropertyName
149}
150
151func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
152 if srcs == nil {
153 panic(fmt.Errorf("srcs must not be nil"))
154 }
155
156 srcsSupplier := func() []string {
157 return *srcs
158 }
159
160 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Colin Crossce75d2c2016-10-06 16:12:58 -0700161}
162
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700163func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Paul Duffin406ea1c2020-03-12 23:43:52 +0000164 srcPropsValue := reflect.ValueOf(srcProps).Elem()
165 srcStructField, _ := srcPropsValue.Type().FieldByName(srcField)
166 if !srcPropsValue.IsValid() || srcStructField.Name == "" {
167 panic(fmt.Errorf("invalid single source prebuilt %+v", module))
168 }
169
170 if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface {
171 panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps))
172 }
173
174 srcFieldIndex := srcStructField.Index
175 srcPropertyName := proptools.PropertyNameForField(srcField)
176
177 srcsSupplier := func() []string {
178 value := srcPropsValue.FieldByIndex(srcFieldIndex)
179 if value.Kind() == reflect.Ptr {
180 value = value.Elem()
181 }
182 if value.Kind() != reflect.String {
183 panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one but was %d %q", srcPropertyName, value.Kind(), value))
184 }
185 src := value.String()
186 if src == "" {
187 return nil
188 }
189 return []string{src}
190 }
191
192 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700193}
194
Colin Crossce75d2c2016-10-06 16:12:58 -0700195type PrebuiltInterface interface {
196 Module
197 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700198}
199
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700200func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700201 ctx.BottomUp("prebuilts", PrebuiltMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700202}
203
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700204func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700205 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700206 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700207}
208
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700209// PrebuiltMutator ensures that there is always a module with an undecorated name, and marks
Colin Crossce75d2c2016-10-06 16:12:58 -0700210// prebuilt modules that have both a prebuilt and a source module.
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700211func PrebuiltMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700212 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
213 p := m.Prebuilt()
214 name := m.base().BaseModuleName()
215 if ctx.OtherModuleExists(name) {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900216 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
Colin Cross74d73e22017-08-02 11:05:49 -0700217 p.properties.SourceExists = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700218 } else {
219 ctx.Rename(name)
220 }
221 }
222}
223
Colin Crossc3e7fa62017-03-17 13:14:32 -0700224// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
225// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800226func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700227 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
228 p := m.Prebuilt()
Paul Duffin406ea1c2020-03-12 23:43:52 +0000229 if p.srcsSupplier == nil {
Colin Cross74d73e22017-08-02 11:05:49 -0700230 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
231 }
232 if !p.properties.SourceExists {
233 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700234 }
235 } else if s, ok := ctx.Module().(Module); ok {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900236 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800237 p := m.(PrebuiltInterface).Prebuilt()
238 if p.usePrebuilt(ctx, s) {
239 p.properties.UsePrebuilt = true
240 s.SkipInstall()
Colin Crossa2f296f2016-11-29 15:16:18 -0800241 }
242 })
243 }
244}
245
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700246// PrebuiltPostDepsMutator does two operations. It replace dependencies on the
247// source module with dependencies on the prebuilt when both modules exist and
248// the prebuilt should be used. When the prebuilt should not be used, disable
249// installing it. Secondly, it also adds a sourcegroup to any filegroups found
250// in the prebuilt's 'Srcs' property.
251func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700252 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
253 p := m.Prebuilt()
254 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700255 if p.properties.UsePrebuilt {
256 if p.properties.SourceExists {
Paul Duffin2c5454f2020-06-26 22:08:43 +0100257 ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
258 if t, ok := tag.(ReplaceSourceWithPrebuilt); ok {
259 return t.ReplaceSourceWithPrebuilt()
260 }
261
262 return true
263 })
Colin Cross0f3c72f2016-11-23 15:44:07 -0800264 }
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 Duffin406ea1c2020-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}