blob: 0ee1201a732861dc96d17d2d7e8f480f37373e19 [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
Nan Zhang2502e122017-03-09 18:43:01 -080028type prebuiltDependencyTag struct {
29 blueprint.BaseDependencyTag
30}
31
Jiyong Park03b68dd2019-07-26 23:20:40 +090032var PrebuiltDepTag prebuiltDependencyTag
Colin Crossce75d2c2016-10-06 16:12:58 -070033
Colin Cross74d73e22017-08-02 11:05:49 -070034type PrebuiltProperties struct {
35 // When prefer is set to true the prebuilt will be used instead of any source module with
36 // a matching name.
Nan Zhang0007d812017-11-07 10:57:05 -080037 Prefer *bool `android:"arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -070038
Colin Cross74d73e22017-08-02 11:05:49 -070039 SourceExists bool `blueprint:"mutated"`
40 UsePrebuilt bool `blueprint:"mutated"`
41}
42
43type Prebuilt struct {
44 properties PrebuiltProperties
45 module Module
46 srcs *[]string
Jaewoong Jung3e18b192019-06-11 12:25:34 -070047
48 // Metadata for single source Prebuilt modules.
49 srcProps reflect.Value
50 srcField reflect.StructField
Colin Crossce75d2c2016-10-06 16:12:58 -070051}
52
53func (p *Prebuilt) Name(name string) string {
54 return "prebuilt_" + name
55}
56
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070057// The below source-related functions and the srcs, src fields are based on an assumption that
58// prebuilt modules have a static source property at the moment. Currently there is only one
59// exception, android_app_import, which chooses a source file depending on the product's DPI
60// preference configs. We'll want to add native support for dynamic source cases if we end up having
61// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -070062func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Jaewoong Jung939ebd52019-03-26 15:07:36 -070063 if p.srcs != nil {
64 if len(*p.srcs) == 0 {
65 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
66 return nil
67 }
Colin Crossce75d2c2016-10-06 16:12:58 -070068
Jaewoong Jung939ebd52019-03-26 15:07:36 -070069 if len(*p.srcs) > 1 {
70 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
71 return nil
72 }
Colin Crossce75d2c2016-10-06 16:12:58 -070073
Jaewoong Jung939ebd52019-03-26 15:07:36 -070074 // Return the singleton source after expanding any filegroup in the
75 // sources.
76 return PathForModuleSrc(ctx, (*p.srcs)[0])
77 } else {
Jaewoong Jung3e18b192019-06-11 12:25:34 -070078 if !p.srcProps.IsValid() {
79 ctx.ModuleErrorf("prebuilt source was not set")
80 }
81 src := p.getSingleSourceFieldValue()
82 if src == "" {
83 ctx.PropertyErrorf(proptools.FieldNameForProperty(p.srcField.Name),
84 "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -070085 return nil
86 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -070087 return PathForModuleSrc(ctx, src)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070088 }
Colin Cross74d73e22017-08-02 11:05:49 -070089}
90
Jiyong Park4d277042019-04-23 18:00:10 +090091func (p *Prebuilt) UsePrebuilt() bool {
92 return p.properties.UsePrebuilt
93}
94
Colin Cross74d73e22017-08-02 11:05:49 -070095func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
96 p := module.Prebuilt()
97 module.AddProperties(&p.properties)
98 p.srcs = srcs
Colin Crossce75d2c2016-10-06 16:12:58 -070099}
100
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700101func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700102 p := module.Prebuilt()
103 module.AddProperties(&p.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700104 p.srcProps = reflect.ValueOf(srcProps).Elem()
105 p.srcField, _ = p.srcProps.Type().FieldByName(srcField)
106 p.checkSingleSourceProperties()
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700107}
108
Colin Crossce75d2c2016-10-06 16:12:58 -0700109type PrebuiltInterface interface {
110 Module
111 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700112}
113
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700114func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700115 ctx.BottomUp("prebuilts", PrebuiltMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700116}
117
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700118func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700119 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700120 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700121}
122
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700123// PrebuiltMutator ensures that there is always a module with an undecorated name, and marks
Colin Crossce75d2c2016-10-06 16:12:58 -0700124// prebuilt modules that have both a prebuilt and a source module.
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700125func PrebuiltMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700126 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
127 p := m.Prebuilt()
128 name := m.base().BaseModuleName()
129 if ctx.OtherModuleExists(name) {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900130 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
Colin Cross74d73e22017-08-02 11:05:49 -0700131 p.properties.SourceExists = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700132 } else {
133 ctx.Rename(name)
134 }
135 }
136}
137
Colin Crossc3e7fa62017-03-17 13:14:32 -0700138// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
139// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800140func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700141 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
142 p := m.Prebuilt()
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700143 if p.srcs == nil && !p.srcProps.IsValid() {
Colin Cross74d73e22017-08-02 11:05:49 -0700144 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
145 }
146 if !p.properties.SourceExists {
147 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700148 }
149 } else if s, ok := ctx.Module().(Module); ok {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900150 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800151 p := m.(PrebuiltInterface).Prebuilt()
152 if p.usePrebuilt(ctx, s) {
153 p.properties.UsePrebuilt = true
154 s.SkipInstall()
Colin Crossa2f296f2016-11-29 15:16:18 -0800155 }
156 })
157 }
158}
159
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700160// PrebuiltPostDepsMutator does two operations. It replace dependencies on the
161// source module with dependencies on the prebuilt when both modules exist and
162// the prebuilt should be used. When the prebuilt should not be used, disable
163// installing it. Secondly, it also adds a sourcegroup to any filegroups found
164// in the prebuilt's 'Srcs' property.
165func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700166 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
167 p := m.Prebuilt()
168 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700169 if p.properties.UsePrebuilt {
170 if p.properties.SourceExists {
Colin Cross0f3c72f2016-11-23 15:44:07 -0800171 ctx.ReplaceDependencies(name)
172 }
173 } else {
174 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700175 }
176 }
177}
178
Colin Crossa2f296f2016-11-29 15:16:18 -0800179// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
180// will be used if it is marked "prefer" or if the source module is disabled.
181func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700182 if p.srcs != nil && len(*p.srcs) == 0 {
183 return false
184 }
185
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700186 if p.srcProps.IsValid() && p.getSingleSourceFieldValue() == "" {
Colin Crossa2f296f2016-11-29 15:16:18 -0800187 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700188 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700189
Colin Crossa2f296f2016-11-29 15:16:18 -0800190 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800191 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800192 return true
193 }
194
Colin Crossc3e7fa62017-03-17 13:14:32 -0700195 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700196}
Jiyong Park0a573d72019-07-07 12:39:16 +0900197
198func (p *Prebuilt) SourceExists() bool {
199 return p.properties.SourceExists
200}
Jaewoong Jung22dad112019-07-10 10:18:16 -0700201
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700202func (p *Prebuilt) checkSingleSourceProperties() {
203 if !p.srcProps.IsValid() || p.srcField.Name == "" {
204 panic(fmt.Errorf("invalid single source prebuilt %+v", p))
205 }
206
207 if p.srcProps.Kind() != reflect.Struct && p.srcProps.Kind() != reflect.Interface {
208 panic(fmt.Errorf("invalid single source prebuilt %+v", p.srcProps))
209 }
210}
211
212func (p *Prebuilt) getSingleSourceFieldValue() string {
213 value := p.srcProps.FieldByIndex(p.srcField.Index)
214 if value.Kind() == reflect.Ptr {
215 value = value.Elem()
216 }
217 if value.Kind() != reflect.String {
218 panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one", p.srcField.Name))
219 }
220 return value.String()
221}