Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 17 | import ( |
| 18 | "fmt" |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 19 | "reflect" |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint" |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 23 | ) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 24 | |
| 25 | // This file implements common functionality for handling modules that may exist as prebuilts, |
| 26 | // source, or both. |
| 27 | |
Paul Duffin | 0c4979b | 2019-12-19 15:11:53 +0000 | [diff] [blame^] | 28 | func RegisterPrebuiltMutators(ctx RegistrationContext) { |
| 29 | ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators) |
| 30 | ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators) |
| 31 | } |
| 32 | |
Nan Zhang | 2502e12 | 2017-03-09 18:43:01 -0800 | [diff] [blame] | 33 | type prebuiltDependencyTag struct { |
| 34 | blueprint.BaseDependencyTag |
| 35 | } |
| 36 | |
Jiyong Park | 03b68dd | 2019-07-26 23:20:40 +0900 | [diff] [blame] | 37 | var PrebuiltDepTag prebuiltDependencyTag |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 38 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 39 | type 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 Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 42 | Prefer *bool `android:"arch_variant"` |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 43 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 44 | SourceExists bool `blueprint:"mutated"` |
| 45 | UsePrebuilt bool `blueprint:"mutated"` |
| 46 | } |
| 47 | |
| 48 | type Prebuilt struct { |
| 49 | properties PrebuiltProperties |
| 50 | module Module |
| 51 | srcs *[]string |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 52 | |
| 53 | // Metadata for single source Prebuilt modules. |
| 54 | srcProps reflect.Value |
| 55 | srcField reflect.StructField |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func (p *Prebuilt) Name(name string) string { |
| 59 | return "prebuilt_" + name |
| 60 | } |
| 61 | |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 62 | // 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 Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 67 | func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path { |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 68 | if p.srcs != nil { |
| 69 | if len(*p.srcs) == 0 { |
| 70 | ctx.PropertyErrorf("srcs", "missing prebuilt source file") |
| 71 | return nil |
| 72 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 73 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 74 | if len(*p.srcs) > 1 { |
| 75 | ctx.PropertyErrorf("srcs", "multiple prebuilt source files") |
| 76 | return nil |
| 77 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 78 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 79 | // Return the singleton source after expanding any filegroup in the |
| 80 | // sources. |
| 81 | return PathForModuleSrc(ctx, (*p.srcs)[0]) |
| 82 | } else { |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 83 | 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 Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 90 | return nil |
| 91 | } |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 92 | return PathForModuleSrc(ctx, src) |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 93 | } |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 96 | func (p *Prebuilt) UsePrebuilt() bool { |
| 97 | return p.properties.UsePrebuilt |
| 98 | } |
| 99 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 100 | func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) { |
| 101 | p := module.Prebuilt() |
| 102 | module.AddProperties(&p.properties) |
| 103 | p.srcs = srcs |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 106 | func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) { |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 107 | p := module.Prebuilt() |
| 108 | module.AddProperties(&p.properties) |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 109 | p.srcProps = reflect.ValueOf(srcProps).Elem() |
| 110 | p.srcField, _ = p.srcProps.Type().FieldByName(srcField) |
| 111 | p.checkSingleSourceProperties() |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 114 | type PrebuiltInterface interface { |
| 115 | Module |
| 116 | Prebuilt() *Prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 119 | func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) { |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 120 | ctx.BottomUp("prebuilts", PrebuiltMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 123 | func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 124 | ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel() |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 125 | ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 128 | // PrebuiltMutator ensures that there is always a module with an undecorated name, and marks |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 129 | // prebuilt modules that have both a prebuilt and a source module. |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 130 | func PrebuiltMutator(ctx BottomUpMutatorContext) { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 131 | 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 Park | 03b68dd | 2019-07-26 23:20:40 +0900 | [diff] [blame] | 135 | ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 136 | p.properties.SourceExists = true |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 137 | } else { |
| 138 | ctx.Rename(name) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 143 | // 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 Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 145 | func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 146 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 147 | p := m.Prebuilt() |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 148 | if p.srcs == nil && !p.srcProps.IsValid() { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 149 | 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 Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 153 | } |
| 154 | } else if s, ok := ctx.Module().(Module); ok { |
Jiyong Park | 03b68dd | 2019-07-26 23:20:40 +0900 | [diff] [blame] | 155 | ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) { |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 156 | p := m.(PrebuiltInterface).Prebuilt() |
| 157 | if p.usePrebuilt(ctx, s) { |
| 158 | p.properties.UsePrebuilt = true |
| 159 | s.SkipInstall() |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 160 | } |
| 161 | }) |
| 162 | } |
| 163 | } |
| 164 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 165 | // 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. |
| 170 | func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 171 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 172 | p := m.Prebuilt() |
| 173 | name := m.base().BaseModuleName() |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 174 | if p.properties.UsePrebuilt { |
| 175 | if p.properties.SourceExists { |
Colin Cross | 0f3c72f | 2016-11-23 15:44:07 -0800 | [diff] [blame] | 176 | ctx.ReplaceDependencies(name) |
| 177 | } |
| 178 | } else { |
| 179 | m.SkipInstall() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 184 | // 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. |
| 186 | func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool { |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 187 | if p.srcs != nil && len(*p.srcs) == 0 { |
| 188 | return false |
| 189 | } |
| 190 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 191 | if p.srcProps.IsValid() && p.getSingleSourceFieldValue() == "" { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 192 | return false |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 193 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 194 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 195 | // TODO: use p.Properties.Name and ctx.ModuleDir to override preference |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 196 | if Bool(p.properties.Prefer) { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 197 | return true |
| 198 | } |
| 199 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 200 | return source == nil || !source.Enabled() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 201 | } |
Jiyong Park | 0a573d7 | 2019-07-07 12:39:16 +0900 | [diff] [blame] | 202 | |
| 203 | func (p *Prebuilt) SourceExists() bool { |
| 204 | return p.properties.SourceExists |
| 205 | } |
Jaewoong Jung | 22dad11 | 2019-07-10 10:18:16 -0700 | [diff] [blame] | 206 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 207 | func (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 | |
| 217 | func (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 | } |