blob: 2c99f1f7a549bd3eba299e9c2db1e4c4cf196740 [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
Colin Cross74d73e22017-08-02 11:05:49 -070039type PrebuiltProperties struct {
40 // When prefer is set to true the prebuilt will be used instead of any source module with
41 // a matching name.
Nan Zhang0007d812017-11-07 10:57:05 -080042 Prefer *bool `android:"arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -070043
Colin Cross74d73e22017-08-02 11:05:49 -070044 SourceExists bool `blueprint:"mutated"`
45 UsePrebuilt bool `blueprint:"mutated"`
46}
47
48type Prebuilt struct {
49 properties PrebuiltProperties
50 module Module
51 srcs *[]string
Jaewoong Jung3e18b192019-06-11 12:25:34 -070052
53 // Metadata for single source Prebuilt modules.
54 srcProps reflect.Value
55 srcField reflect.StructField
Colin Crossce75d2c2016-10-06 16:12:58 -070056}
57
58func (p *Prebuilt) Name(name string) string {
59 return "prebuilt_" + name
60}
61
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070062// The below source-related functions and the srcs, src fields are based on an assumption that
63// prebuilt modules have a static source property at the moment. Currently there is only one
64// exception, android_app_import, which chooses a source file depending on the product's DPI
65// preference configs. We'll want to add native support for dynamic source cases if we end up having
66// more modules like this.
Colin Cross74d73e22017-08-02 11:05:49 -070067func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
Jaewoong Jung939ebd52019-03-26 15:07:36 -070068 if p.srcs != nil {
69 if len(*p.srcs) == 0 {
70 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
71 return nil
72 }
Colin Crossce75d2c2016-10-06 16:12:58 -070073
Jaewoong Jung939ebd52019-03-26 15:07:36 -070074 if len(*p.srcs) > 1 {
75 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
76 return nil
77 }
Colin Crossce75d2c2016-10-06 16:12:58 -070078
Jaewoong Jung939ebd52019-03-26 15:07:36 -070079 // Return the singleton source after expanding any filegroup in the
80 // sources.
81 return PathForModuleSrc(ctx, (*p.srcs)[0])
82 } else {
Jaewoong Jung3e18b192019-06-11 12:25:34 -070083 if !p.srcProps.IsValid() {
84 ctx.ModuleErrorf("prebuilt source was not set")
85 }
86 src := p.getSingleSourceFieldValue()
87 if src == "" {
88 ctx.PropertyErrorf(proptools.FieldNameForProperty(p.srcField.Name),
89 "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -070090 return nil
91 }
Jaewoong Jung3e18b192019-06-11 12:25:34 -070092 return PathForModuleSrc(ctx, src)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070093 }
Colin Cross74d73e22017-08-02 11:05:49 -070094}
95
Jiyong Park4d277042019-04-23 18:00:10 +090096func (p *Prebuilt) UsePrebuilt() bool {
97 return p.properties.UsePrebuilt
98}
99
Colin Cross74d73e22017-08-02 11:05:49 -0700100func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
101 p := module.Prebuilt()
102 module.AddProperties(&p.properties)
103 p.srcs = srcs
Colin Crossce75d2c2016-10-06 16:12:58 -0700104}
105
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700106func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700107 p := module.Prebuilt()
108 module.AddProperties(&p.properties)
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700109 p.srcProps = reflect.ValueOf(srcProps).Elem()
110 p.srcField, _ = p.srcProps.Type().FieldByName(srcField)
111 p.checkSingleSourceProperties()
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700112}
113
Colin Crossce75d2c2016-10-06 16:12:58 -0700114type PrebuiltInterface interface {
115 Module
116 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700117}
118
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700119func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700120 ctx.BottomUp("prebuilts", PrebuiltMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700121}
122
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700123func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Colin Crosscec81712017-07-13 14:43:27 -0700124 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700125 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700126}
127
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700128// PrebuiltMutator ensures that there is always a module with an undecorated name, and marks
Colin Crossce75d2c2016-10-06 16:12:58 -0700129// prebuilt modules that have both a prebuilt and a source module.
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700130func PrebuiltMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700131 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
132 p := m.Prebuilt()
133 name := m.base().BaseModuleName()
134 if ctx.OtherModuleExists(name) {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900135 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
Colin Cross74d73e22017-08-02 11:05:49 -0700136 p.properties.SourceExists = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700137 } else {
138 ctx.Rename(name)
139 }
140 }
141}
142
Colin Crossc3e7fa62017-03-17 13:14:32 -0700143// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
144// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800145func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Colin Crossc3e7fa62017-03-17 13:14:32 -0700146 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
147 p := m.Prebuilt()
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700148 if p.srcs == nil && !p.srcProps.IsValid() {
Colin Cross74d73e22017-08-02 11:05:49 -0700149 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
150 }
151 if !p.properties.SourceExists {
152 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700153 }
154 } else if s, ok := ctx.Module().(Module); ok {
Jiyong Park03b68dd2019-07-26 23:20:40 +0900155 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) {
Colin Crossee6143c2017-12-30 17:54:27 -0800156 p := m.(PrebuiltInterface).Prebuilt()
157 if p.usePrebuilt(ctx, s) {
158 p.properties.UsePrebuilt = true
159 s.SkipInstall()
Colin Crossa2f296f2016-11-29 15:16:18 -0800160 }
161 })
162 }
163}
164
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700165// PrebuiltPostDepsMutator does two operations. It replace dependencies on the
166// source module with dependencies on the prebuilt when both modules exist and
167// the prebuilt should be used. When the prebuilt should not be used, disable
168// installing it. Secondly, it also adds a sourcegroup to any filegroups found
169// in the prebuilt's 'Srcs' property.
170func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700171 if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil {
172 p := m.Prebuilt()
173 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700174 if p.properties.UsePrebuilt {
175 if p.properties.SourceExists {
Colin Cross0f3c72f2016-11-23 15:44:07 -0800176 ctx.ReplaceDependencies(name)
177 }
178 } else {
179 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700180 }
181 }
182}
183
Colin Crossa2f296f2016-11-29 15:16:18 -0800184// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
185// will be used if it is marked "prefer" or if the source module is disabled.
186func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool {
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700187 if p.srcs != nil && len(*p.srcs) == 0 {
188 return false
189 }
190
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700191 if p.srcProps.IsValid() && p.getSingleSourceFieldValue() == "" {
Colin Crossa2f296f2016-11-29 15:16:18 -0800192 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700193 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700194
Colin Crossa2f296f2016-11-29 15:16:18 -0800195 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Nan Zhang0007d812017-11-07 10:57:05 -0800196 if Bool(p.properties.Prefer) {
Colin Crossa2f296f2016-11-29 15:16:18 -0800197 return true
198 }
199
Colin Crossc3e7fa62017-03-17 13:14:32 -0700200 return source == nil || !source.Enabled()
Colin Crossce75d2c2016-10-06 16:12:58 -0700201}
Jiyong Park0a573d72019-07-07 12:39:16 +0900202
203func (p *Prebuilt) SourceExists() bool {
204 return p.properties.SourceExists
205}
Jaewoong Jung22dad112019-07-10 10:18:16 -0700206
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700207func (p *Prebuilt) checkSingleSourceProperties() {
208 if !p.srcProps.IsValid() || p.srcField.Name == "" {
209 panic(fmt.Errorf("invalid single source prebuilt %+v", p))
210 }
211
212 if p.srcProps.Kind() != reflect.Struct && p.srcProps.Kind() != reflect.Interface {
213 panic(fmt.Errorf("invalid single source prebuilt %+v", p.srcProps))
214 }
215}
216
217func (p *Prebuilt) getSingleSourceFieldValue() string {
218 value := p.srcProps.FieldByIndex(p.srcField.Index)
219 if value.Kind() == reflect.Ptr {
220 value = value.Elem()
221 }
222 if value.Kind() != reflect.String {
223 panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one", p.srcField.Name))
224 }
225 return value.String()
226}