blob: c780cb24f3f9533261a6c777e52a4ab71675dcaf [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
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070065// The below source-related functions and the srcs, src fields are based on an assumption that
66// prebuilt modules have a static source property at the moment. Currently there is only one
67// exception, android_app_import, which chooses a source file depending on the product's DPI
68// preference configs. We'll want to add native support for dynamic source cases if we end up having
69// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -070070func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Jaewoong Jung939ebd52019-03-26 15:07:36 -070071 if p.srcs != nil {
72 if len(*p.srcs) == 0 {
73 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
74 return nil
75 }
Colin Crossce75d2c2016-10-06 16:12:58 -070076
Jaewoong Jung939ebd52019-03-26 15:07:36 -070077 if len(*p.srcs) > 1 {
78 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
79 return nil
80 }
Colin Crossce75d2c2016-10-06 16:12:58 -070081
Jaewoong Jung939ebd52019-03-26 15:07:36 -070082 // Return the singleton source after expanding any filegroup in the
83 // sources.
84 return PathForModuleSrc(ctx, (*p.srcs)[0])
85 } else {
Jaewoong Jung3e18b192019-06-11 12:25:34 -070086 if !p.srcProps.IsValid() {
87 ctx.ModuleErrorf("prebuilt source was not set")
88 }
89 src := p.getSingleSourceFieldValue()
90 if src == "" {
91 ctx.PropertyErrorf(proptools.FieldNameForProperty(p.srcField.Name),
92 "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -070093 return nil
94 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -070095 return PathForModuleSrc(ctx, src)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070096 }
Colin Cross74d73e22017-08-02 11:05:49 -070097}
98
Jiyong Park4d277042019-04-23 18:00:10 +090099func (p *Prebuilt) UsePrebuilt() bool {
100 return p.properties.UsePrebuilt
101}
102
Colin Cross74d73e22017-08-02 11:05:49 -0700103func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
104 p := module.Prebuilt()
105 module.AddProperties(&p.properties)
106 p.srcs = srcs
Colin Crossce75d2c2016-10-06 16:12:58 -0700107}
108
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700109func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700110 p := module.Prebuilt()
111 module.AddProperties(&p.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700112 p.srcProps = reflect.ValueOf(srcProps).Elem()
113 p.srcField, _ = p.srcProps.Type().FieldByName(srcField)
114 p.checkSingleSourceProperties()
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700115}
116
Colin Crossce75d2c2016-10-06 16:12:58 -0700117type PrebuiltInterface interface {
118 Module
119 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700120}
121
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700122func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700123 ctx.BottomUp("prebuilts", PrebuiltMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700124}
125
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700126func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700127 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700128 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700129}
130
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700131// PrebuiltMutator ensures that there is always a module with an undecorated name, and marks
Colin Crossce75d2c2016-10-06 16:12:58 -0700132// prebuilt modules that have both a prebuilt and a source module.
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700133func PrebuiltMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700134 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
135 p := m.Prebuilt()
136 name := m.base().BaseModuleName()
137 if ctx.OtherModuleExists(name) {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900138 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
Colin Cross74d73e22017-08-02 11:05:49 -0700139 p.properties.SourceExists = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700140 } else {
141 ctx.Rename(name)
142 }
143 }
144}
145
Colin Crossc3e7fa62017-03-17 13:14:32 -0700146// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
147// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800148func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700149 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
150 p := m.Prebuilt()
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700151 if p.srcs == nil && !p.srcProps.IsValid() {
Colin Cross74d73e22017-08-02 11:05:49 -0700152 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
153 }
154 if !p.properties.SourceExists {
155 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700156 }
157 } else if s, ok := ctx.Module().(Module); ok {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900158 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800159 p := m.(PrebuiltInterface).Prebuilt()
160 if p.usePrebuilt(ctx, s) {
161 p.properties.UsePrebuilt = true
162 s.SkipInstall()
Colin Crossa2f296f2016-11-29 15:16:18 -0800163 }
164 })
165 }
166}
167
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700168// PrebuiltPostDepsMutator does two operations. It replace dependencies on the
169// source module with dependencies on the prebuilt when both modules exist and
170// the prebuilt should be used. When the prebuilt should not be used, disable
171// installing it. Secondly, it also adds a sourcegroup to any filegroups found
172// in the prebuilt's 'Srcs' property.
173func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700174 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
175 p := m.Prebuilt()
176 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700177 if p.properties.UsePrebuilt {
178 if p.properties.SourceExists {
Colin Cross0f3c72f2016-11-23 15:44:07 -0800179 ctx.ReplaceDependencies(name)
180 }
181 } else {
182 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700183 }
184 }
185}
186
Colin Crossa2f296f2016-11-29 15:16:18 -0800187// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
188// will be used if it is marked "prefer" or if the source module is disabled.
189func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700190 if p.srcs != nil && len(*p.srcs) == 0 {
191 return false
192 }
193
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700194 if p.srcProps.IsValid() && p.getSingleSourceFieldValue() == "" {
Colin Crossa2f296f2016-11-29 15:16:18 -0800195 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700196 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700197
Colin Crossa2f296f2016-11-29 15:16:18 -0800198 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800199 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800200 return true
201 }
202
Colin Crossc3e7fa62017-03-17 13:14:32 -0700203 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700204}
Jiyong Park0a573d72019-07-07 12:39:16 +0900205
206func (p *Prebuilt) SourceExists() bool {
207 return p.properties.SourceExists
208}
Jaewoong Jung22dad112019-07-10 10:18:16 -0700209
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700210func (p *Prebuilt) checkSingleSourceProperties() {
211 if !p.srcProps.IsValid() || p.srcField.Name == "" {
212 panic(fmt.Errorf("invalid single source prebuilt %+v", p))
213 }
214
215 if p.srcProps.Kind() != reflect.Struct && p.srcProps.Kind() != reflect.Interface {
216 panic(fmt.Errorf("invalid single source prebuilt %+v", p.srcProps))
217 }
218}
219
220func (p *Prebuilt) getSingleSourceFieldValue() string {
221 value := p.srcProps.FieldByIndex(p.srcField.Index)
222 if value.Kind() == reflect.Ptr {
223 value = value.Elem()
224 }
225 if value.Kind() != reflect.String {
226 panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one", p.srcField.Name))
227 }
228 return value.String()
229}