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 | |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 39 | // Mark this tag so dependencies that use it are excluded from visibility enforcement. |
| 40 | func (t prebuiltDependencyTag) ExcludeFromVisibilityEnforcement() {} |
| 41 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 42 | type 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 Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 45 | Prefer *bool `android:"arch_variant"` |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 46 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 47 | SourceExists bool `blueprint:"mutated"` |
| 48 | UsePrebuilt bool `blueprint:"mutated"` |
| 49 | } |
| 50 | |
| 51 | type Prebuilt struct { |
| 52 | properties PrebuiltProperties |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 53 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 54 | srcsSupplier PrebuiltSrcsSupplier |
| 55 | srcsPropertyName string |
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 | |
Paul Duffin | 5006151 | 2020-01-21 16:31:05 +0000 | [diff] [blame] | 62 | func (p *Prebuilt) ForcePrefer() { |
| 63 | p.properties.Prefer = proptools.BoolPtr(true) |
| 64 | } |
| 65 | |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 66 | // The below source-related functions and the srcs, src fields are based on an assumption that |
| 67 | // prebuilt modules have a static source property at the moment. Currently there is only one |
| 68 | // exception, android_app_import, which chooses a source file depending on the product's DPI |
| 69 | // preference configs. We'll want to add native support for dynamic source cases if we end up having |
| 70 | // more modules like this. |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 71 | func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 72 | if p.srcsSupplier != nil { |
| 73 | srcs := p.srcsSupplier() |
| 74 | |
| 75 | if len(srcs) == 0 { |
| 76 | ctx.PropertyErrorf(p.srcsPropertyName, "missing prebuilt source file") |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 77 | return nil |
| 78 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 79 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 80 | if len(srcs) > 1 { |
| 81 | ctx.PropertyErrorf(p.srcsPropertyName, "multiple prebuilt source files") |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 82 | return nil |
| 83 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 84 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 85 | // Return the singleton source after expanding any filegroup in the |
| 86 | // sources. |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 87 | src := srcs[0] |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 88 | return PathForModuleSrc(ctx, src) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 89 | } else { |
| 90 | ctx.ModuleErrorf("prebuilt source was not set") |
| 91 | return nil |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 92 | } |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 95 | func (p *Prebuilt) UsePrebuilt() bool { |
| 96 | return p.properties.UsePrebuilt |
| 97 | } |
| 98 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 99 | // Called to provide the srcs value for the prebuilt module. |
| 100 | // |
| 101 | // Return the src value or nil if it is not available. |
| 102 | type PrebuiltSrcsSupplier func() []string |
| 103 | |
| 104 | // Initialize the module as a prebuilt module that uses the provided supplier to access the |
| 105 | // prebuilt sources of the module. |
| 106 | // |
| 107 | // The supplier will be called multiple times and must return the same values each time it |
| 108 | // is called. If it returns an empty array (or nil) then the prebuilt module will not be used |
| 109 | // as a replacement for a source module with the same name even if prefer = true. |
| 110 | // |
| 111 | // If the Prebuilt.SingleSourcePath() is called on the module then this must return an array |
| 112 | // containing exactly one source file. |
| 113 | // |
| 114 | // The provided property name is used to provide helpful error messages in the event that |
| 115 | // a problem arises, e.g. calling SingleSourcePath() when more than one source is provided. |
| 116 | func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 117 | p := module.Prebuilt() |
| 118 | module.AddProperties(&p.properties) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 119 | |
| 120 | if srcsSupplier == nil { |
| 121 | panic(fmt.Errorf("srcsSupplier must not be nil")) |
| 122 | } |
| 123 | if srcsPropertyName == "" { |
| 124 | panic(fmt.Errorf("srcsPropertyName must not be empty")) |
| 125 | } |
| 126 | |
| 127 | p.srcsSupplier = srcsSupplier |
| 128 | p.srcsPropertyName = srcsPropertyName |
| 129 | } |
| 130 | |
| 131 | func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) { |
| 132 | if srcs == nil { |
| 133 | panic(fmt.Errorf("srcs must not be nil")) |
| 134 | } |
| 135 | |
| 136 | srcsSupplier := func() []string { |
| 137 | return *srcs |
| 138 | } |
| 139 | |
| 140 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 143 | func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 144 | srcPropsValue := reflect.ValueOf(srcProps).Elem() |
| 145 | srcStructField, _ := srcPropsValue.Type().FieldByName(srcField) |
| 146 | if !srcPropsValue.IsValid() || srcStructField.Name == "" { |
| 147 | panic(fmt.Errorf("invalid single source prebuilt %+v", module)) |
| 148 | } |
| 149 | |
| 150 | if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface { |
| 151 | panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps)) |
| 152 | } |
| 153 | |
| 154 | srcFieldIndex := srcStructField.Index |
| 155 | srcPropertyName := proptools.PropertyNameForField(srcField) |
| 156 | |
| 157 | srcsSupplier := func() []string { |
| 158 | value := srcPropsValue.FieldByIndex(srcFieldIndex) |
| 159 | if value.Kind() == reflect.Ptr { |
| 160 | value = value.Elem() |
| 161 | } |
| 162 | if value.Kind() != reflect.String { |
| 163 | panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one but was %d %q", srcPropertyName, value.Kind(), value)) |
| 164 | } |
| 165 | src := value.String() |
| 166 | if src == "" { |
| 167 | return nil |
| 168 | } |
| 169 | return []string{src} |
| 170 | } |
| 171 | |
| 172 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName) |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 175 | type PrebuiltInterface interface { |
| 176 | Module |
| 177 | Prebuilt() *Prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 180 | func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) { |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 181 | ctx.BottomUp("prebuilts", PrebuiltMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 184 | func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 185 | ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel() |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 186 | ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 189 | // 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] | 190 | // prebuilt modules that have both a prebuilt and a source module. |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 191 | func PrebuiltMutator(ctx BottomUpMutatorContext) { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 192 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 193 | p := m.Prebuilt() |
| 194 | name := m.base().BaseModuleName() |
| 195 | if ctx.OtherModuleExists(name) { |
Jiyong Park | 03b68dd | 2019-07-26 23:20:40 +0900 | [diff] [blame] | 196 | ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 197 | p.properties.SourceExists = true |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 198 | } else { |
| 199 | ctx.Rename(name) |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 204 | // PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or |
| 205 | // 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] | 206 | func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 207 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 208 | p := m.Prebuilt() |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 209 | if p.srcsSupplier == nil { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 210 | panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it")) |
| 211 | } |
| 212 | if !p.properties.SourceExists { |
| 213 | p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil) |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 214 | } |
| 215 | } else if s, ok := ctx.Module().(Module); ok { |
Jiyong Park | 03b68dd | 2019-07-26 23:20:40 +0900 | [diff] [blame] | 216 | ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) { |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 217 | p := m.(PrebuiltInterface).Prebuilt() |
| 218 | if p.usePrebuilt(ctx, s) { |
| 219 | p.properties.UsePrebuilt = true |
| 220 | s.SkipInstall() |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 221 | } |
| 222 | }) |
| 223 | } |
| 224 | } |
| 225 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 226 | // PrebuiltPostDepsMutator does two operations. It replace dependencies on the |
| 227 | // source module with dependencies on the prebuilt when both modules exist and |
| 228 | // the prebuilt should be used. When the prebuilt should not be used, disable |
| 229 | // installing it. Secondly, it also adds a sourcegroup to any filegroups found |
| 230 | // in the prebuilt's 'Srcs' property. |
| 231 | func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 232 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 233 | p := m.Prebuilt() |
| 234 | name := m.base().BaseModuleName() |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 235 | if p.properties.UsePrebuilt { |
| 236 | if p.properties.SourceExists { |
Colin Cross | 0f3c72f | 2016-11-23 15:44:07 -0800 | [diff] [blame] | 237 | ctx.ReplaceDependencies(name) |
| 238 | } |
| 239 | } else { |
| 240 | m.SkipInstall() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 245 | // usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt |
| 246 | // will be used if it is marked "prefer" or if the source module is disabled. |
| 247 | func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module) bool { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame^] | 248 | if p.srcsSupplier != nil && len(p.srcsSupplier()) == 0 { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 249 | return false |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 250 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 251 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 252 | // TODO: use p.Properties.Name and ctx.ModuleDir to override preference |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 253 | if Bool(p.properties.Prefer) { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 254 | return true |
| 255 | } |
| 256 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 257 | return source == nil || !source.Enabled() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 258 | } |
Jiyong Park | 0a573d7 | 2019-07-07 12:39:16 +0900 | [diff] [blame] | 259 | |
| 260 | func (p *Prebuilt) SourceExists() bool { |
| 261 | return p.properties.SourceExists |
| 262 | } |