blob: 2d16f65d73a5f76be24169796925bc48e821da2f [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
Colin Cross74d73e22017-08-02 11:05:49 -070042type PrebuiltProperties struct {
43 // When prefer is set to true the prebuilt will be used instead of any source module with
44 // a matching name.
Nan Zhang0007d812017-11-07 10:57:05 -080045 Prefer *bool `android:"arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -070046
Colin Cross74d73e22017-08-02 11:05:49 -070047 SourceExists bool `blueprint:"mutated"`
48 UsePrebuilt bool `blueprint:"mutated"`
49}
50
51type Prebuilt struct {
52 properties PrebuiltProperties
53 module Module
54 srcs *[]string
Jaewoong Jung3e18b192019-06-11 12:25:34 -070055
56 // Metadata for single source Prebuilt modules.
57 srcProps reflect.Value
58 srcField reflect.StructField
Colin Crossce75d2c2016-10-06 16:12:58 -070059}
60
61func (p *Prebuilt) Name(name string) string {
62 return "prebuilt_" + name
63}
64
Paul Duffin50061512020-01-21 16:31:05 +000065func (p *Prebuilt) ForcePrefer() {
66 p.properties.Prefer = proptools.BoolPtr(true)
67}
68
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070069// The below source-related functions and the srcs, src fields are based on an assumption that
70// prebuilt modules have a static source property at the moment. Currently there is only one
71// exception, android_app_import, which chooses a source file depending on the product's DPI
72// preference configs. We'll want to add native support for dynamic source cases if we end up having
73// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -070074func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Jaewoong Jung939ebd52019-03-26 15:07:36 -070075 if p.srcs != nil {
76 if len(*p.srcs) == 0 {
77 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
78 return nil
79 }
Colin Crossce75d2c2016-10-06 16:12:58 -070080
Jaewoong Jung939ebd52019-03-26 15:07:36 -070081 if len(*p.srcs) > 1 {
82 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
83 return nil
84 }
Colin Crossce75d2c2016-10-06 16:12:58 -070085
Jaewoong Jung939ebd52019-03-26 15:07:36 -070086 // Return the singleton source after expanding any filegroup in the
87 // sources.
88 return PathForModuleSrc(ctx, (*p.srcs)[0])
89 } else {
Jaewoong Jung3e18b192019-06-11 12:25:34 -070090 if !p.srcProps.IsValid() {
91 ctx.ModuleErrorf("prebuilt source was not set")
92 }
93 src := p.getSingleSourceFieldValue()
94 if src == "" {
95 ctx.PropertyErrorf(proptools.FieldNameForProperty(p.srcField.Name),
96 "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -070097 return nil
98 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -070099 return PathForModuleSrc(ctx, src)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700100 }
Colin Cross74d73e22017-08-02 11:05:49 -0700101}
102
Jiyong Park4d277042019-04-23 18:00:10 +0900103func (p *Prebuilt) UsePrebuilt() bool {
104 return p.properties.UsePrebuilt
105}
106
Colin Cross74d73e22017-08-02 11:05:49 -0700107func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
108 p := module.Prebuilt()
109 module.AddProperties(&p.properties)
110 p.srcs = srcs
Colin Crossce75d2c2016-10-06 16:12:58 -0700111}
112
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700113func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700114 p := module.Prebuilt()
115 module.AddProperties(&p.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700116 p.srcProps = reflect.ValueOf(srcProps).Elem()
117 p.srcField, _ = p.srcProps.Type().FieldByName(srcField)
118 p.checkSingleSourceProperties()
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700119}
120
Colin Crossce75d2c2016-10-06 16:12:58 -0700121type PrebuiltInterface interface {
122 Module
123 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700124}
125
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700126func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700127 ctx.BottomUp("prebuilts", PrebuiltMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700128}
129
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700130func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700131 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700132 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700133}
134
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700135// PrebuiltMutator ensures that there is always a module with an undecorated name, and marks
Colin Crossce75d2c2016-10-06 16:12:58 -0700136// prebuilt modules that have both a prebuilt and a source module.
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700137func PrebuiltMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700138 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
139 p := m.Prebuilt()
140 name := m.base().BaseModuleName()
141 if ctx.OtherModuleExists(name) {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900142 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
Colin Cross74d73e22017-08-02 11:05:49 -0700143 p.properties.SourceExists = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700144 } else {
145 ctx.Rename(name)
146 }
147 }
148}
149
Colin Crossc3e7fa62017-03-17 13:14:32 -0700150// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
151// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800152func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700153 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
154 p := m.Prebuilt()
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700155 if p.srcs == nil && !p.srcProps.IsValid() {
Colin Cross74d73e22017-08-02 11:05:49 -0700156 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
157 }
158 if !p.properties.SourceExists {
159 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700160 }
161 } else if s, ok := ctx.Module().(Module); ok {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900162 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800163 p := m.(PrebuiltInterface).Prebuilt()
164 if p.usePrebuilt(ctx, s) {
165 p.properties.UsePrebuilt = true
166 s.SkipInstall()
Colin Crossa2f296f2016-11-29 15:16:18 -0800167 }
168 })
169 }
170}
171
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700172// PrebuiltPostDepsMutator does two operations. It replace dependencies on the
173// source module with dependencies on the prebuilt when both modules exist and
174// the prebuilt should be used. When the prebuilt should not be used, disable
175// installing it. Secondly, it also adds a sourcegroup to any filegroups found
176// in the prebuilt's 'Srcs' property.
177func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700178 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
179 p := m.Prebuilt()
180 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700181 if p.properties.UsePrebuilt {
182 if p.properties.SourceExists {
Colin Cross0f3c72f2016-11-23 15:44:07 -0800183 ctx.ReplaceDependencies(name)
184 }
185 } else {
186 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700187 }
188 }
189}
190
Colin Crossa2f296f2016-11-29 15:16:18 -0800191// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
192// will be used if it is marked "prefer" or if the source module is disabled.
193func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700194 if p.srcs != nil && len(*p.srcs) == 0 {
195 return false
196 }
197
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700198 if p.srcProps.IsValid() && p.getSingleSourceFieldValue() == "" {
Colin Crossa2f296f2016-11-29 15:16:18 -0800199 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700200 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700201
Colin Crossa2f296f2016-11-29 15:16:18 -0800202 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800203 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800204 return true
205 }
206
Colin Crossc3e7fa62017-03-17 13:14:32 -0700207 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700208}
Jiyong Park0a573d72019-07-07 12:39:16 +0900209
210func (p *Prebuilt) SourceExists() bool {
211 return p.properties.SourceExists
212}
Jaewoong Jung22dad112019-07-10 10:18:16 -0700213
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700214func (p *Prebuilt) checkSingleSourceProperties() {
215 if !p.srcProps.IsValid() || p.srcField.Name == "" {
216 panic(fmt.Errorf("invalid single source prebuilt %+v", p))
217 }
218
219 if p.srcProps.Kind() != reflect.Struct && p.srcProps.Kind() != reflect.Interface {
220 panic(fmt.Errorf("invalid single source prebuilt %+v", p.srcProps))
221 }
222}
223
224func (p *Prebuilt) getSingleSourceFieldValue() string {
225 value := p.srcProps.FieldByIndex(p.srcField.Index)
226 if value.Kind() == reflect.Ptr {
227 value = value.Elem()
228 }
229 if value.Kind() != reflect.String {
230 panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one", p.srcField.Name))
231 }
232 return value.String()
233}