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" |
Paul Duffin | d23c726 | 2020-12-11 18:13:08 +0000 | [diff] [blame] | 20 | "strings" |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 24 | ) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 25 | |
| 26 | // This file implements common functionality for handling modules that may exist as prebuilts, |
| 27 | // source, or both. |
| 28 | |
Paul Duffin | 0c4979b | 2019-12-19 15:11:53 +0000 | [diff] [blame] | 29 | func RegisterPrebuiltMutators(ctx RegistrationContext) { |
| 30 | ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators) |
| 31 | ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators) |
| 32 | } |
| 33 | |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 34 | // Marks a dependency tag as possibly preventing a reference to a source from being |
| 35 | // replaced with the prebuilt. |
| 36 | type ReplaceSourceWithPrebuilt interface { |
| 37 | blueprint.DependencyTag |
| 38 | |
| 39 | // Return true if the dependency defined by this tag should be replaced with the |
| 40 | // prebuilt. |
| 41 | ReplaceSourceWithPrebuilt() bool |
| 42 | } |
| 43 | |
Nan Zhang | 2502e12 | 2017-03-09 18:43:01 -0800 | [diff] [blame] | 44 | type prebuiltDependencyTag struct { |
| 45 | blueprint.BaseDependencyTag |
| 46 | } |
| 47 | |
Jiyong Park | 03b68dd | 2019-07-26 23:20:40 +0900 | [diff] [blame] | 48 | var PrebuiltDepTag prebuiltDependencyTag |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 49 | |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 50 | // Mark this tag so dependencies that use it are excluded from visibility enforcement. |
| 51 | func (t prebuiltDependencyTag) ExcludeFromVisibilityEnforcement() {} |
| 52 | |
Paul Duffin | dddd546 | 2020-04-07 15:25:44 +0100 | [diff] [blame] | 53 | // Mark this tag so dependencies that use it are excluded from APEX contents. |
| 54 | func (t prebuiltDependencyTag) ExcludeFromApexContents() {} |
| 55 | |
| 56 | var _ ExcludeFromVisibilityEnforcementTag = PrebuiltDepTag |
| 57 | var _ ExcludeFromApexContentsTag = PrebuiltDepTag |
| 58 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 59 | type PrebuiltProperties struct { |
| 60 | // When prefer is set to true the prebuilt will be used instead of any source module with |
| 61 | // a matching name. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 62 | Prefer *bool `android:"arch_variant"` |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 63 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 64 | SourceExists bool `blueprint:"mutated"` |
| 65 | UsePrebuilt bool `blueprint:"mutated"` |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 66 | |
| 67 | // Set if the module has been renamed to remove the "prebuilt_" prefix. |
| 68 | PrebuiltRenamedToSource bool `blueprint:"mutated"` |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | type Prebuilt struct { |
| 72 | properties PrebuiltProperties |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 73 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 74 | srcsSupplier PrebuiltSrcsSupplier |
| 75 | srcsPropertyName string |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Paul Duffin | d23c726 | 2020-12-11 18:13:08 +0000 | [diff] [blame] | 78 | // RemoveOptionalPrebuiltPrefix returns the result of removing the "prebuilt_" prefix from the |
| 79 | // supplied name if it has one, or returns the name unmodified if it does not. |
| 80 | func RemoveOptionalPrebuiltPrefix(name string) string { |
| 81 | return strings.TrimPrefix(name, "prebuilt_") |
| 82 | } |
| 83 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 84 | func (p *Prebuilt) Name(name string) string { |
| 85 | return "prebuilt_" + name |
| 86 | } |
| 87 | |
Paul Duffin | 5006151 | 2020-01-21 16:31:05 +0000 | [diff] [blame] | 88 | func (p *Prebuilt) ForcePrefer() { |
| 89 | p.properties.Prefer = proptools.BoolPtr(true) |
| 90 | } |
| 91 | |
Paul Duffin | 38b5785 | 2020-05-13 16:08:09 +0100 | [diff] [blame] | 92 | func (p *Prebuilt) Prefer() bool { |
| 93 | return proptools.Bool(p.properties.Prefer) |
| 94 | } |
| 95 | |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 96 | // The below source-related functions and the srcs, src fields are based on an assumption that |
| 97 | // prebuilt modules have a static source property at the moment. Currently there is only one |
| 98 | // exception, android_app_import, which chooses a source file depending on the product's DPI |
| 99 | // preference configs. We'll want to add native support for dynamic source cases if we end up having |
| 100 | // more modules like this. |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 101 | func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 102 | if p.srcsSupplier != nil { |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 103 | srcs := p.srcsSupplier(ctx, ctx.Module()) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 104 | |
| 105 | if len(srcs) == 0 { |
| 106 | ctx.PropertyErrorf(p.srcsPropertyName, "missing prebuilt source file") |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 107 | return nil |
| 108 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 109 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 110 | if len(srcs) > 1 { |
| 111 | ctx.PropertyErrorf(p.srcsPropertyName, "multiple prebuilt source files") |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 112 | return nil |
| 113 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 114 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 115 | // Return the singleton source after expanding any filegroup in the |
| 116 | // sources. |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 117 | src := srcs[0] |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 118 | return PathForModuleSrc(ctx, src) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 119 | } else { |
| 120 | ctx.ModuleErrorf("prebuilt source was not set") |
| 121 | return nil |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 122 | } |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 125 | func (p *Prebuilt) UsePrebuilt() bool { |
| 126 | return p.properties.UsePrebuilt |
| 127 | } |
| 128 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 129 | // Called to provide the srcs value for the prebuilt module. |
| 130 | // |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 131 | // This can be called with a context for any module not just the prebuilt one itself. It can also be |
| 132 | // called concurrently. |
| 133 | // |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 134 | // Return the src value or nil if it is not available. |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 135 | type PrebuiltSrcsSupplier func(ctx BaseModuleContext, prebuilt Module) []string |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 136 | |
| 137 | // Initialize the module as a prebuilt module that uses the provided supplier to access the |
| 138 | // prebuilt sources of the module. |
| 139 | // |
| 140 | // The supplier will be called multiple times and must return the same values each time it |
| 141 | // is called. If it returns an empty array (or nil) then the prebuilt module will not be used |
| 142 | // as a replacement for a source module with the same name even if prefer = true. |
| 143 | // |
| 144 | // If the Prebuilt.SingleSourcePath() is called on the module then this must return an array |
| 145 | // containing exactly one source file. |
| 146 | // |
| 147 | // The provided property name is used to provide helpful error messages in the event that |
| 148 | // a problem arises, e.g. calling SingleSourcePath() when more than one source is provided. |
| 149 | func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 150 | p := module.Prebuilt() |
| 151 | module.AddProperties(&p.properties) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 152 | |
| 153 | if srcsSupplier == nil { |
| 154 | panic(fmt.Errorf("srcsSupplier must not be nil")) |
| 155 | } |
| 156 | if srcsPropertyName == "" { |
| 157 | panic(fmt.Errorf("srcsPropertyName must not be empty")) |
| 158 | } |
| 159 | |
| 160 | p.srcsSupplier = srcsSupplier |
| 161 | p.srcsPropertyName = srcsPropertyName |
| 162 | } |
| 163 | |
| 164 | func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) { |
| 165 | if srcs == nil { |
| 166 | panic(fmt.Errorf("srcs must not be nil")) |
| 167 | } |
| 168 | |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 169 | srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 170 | return *srcs |
| 171 | } |
| 172 | |
| 173 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 176 | func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 177 | srcPropsValue := reflect.ValueOf(srcProps).Elem() |
| 178 | srcStructField, _ := srcPropsValue.Type().FieldByName(srcField) |
| 179 | if !srcPropsValue.IsValid() || srcStructField.Name == "" { |
| 180 | panic(fmt.Errorf("invalid single source prebuilt %+v", module)) |
| 181 | } |
| 182 | |
| 183 | if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface { |
| 184 | panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps)) |
| 185 | } |
| 186 | |
| 187 | srcFieldIndex := srcStructField.Index |
| 188 | srcPropertyName := proptools.PropertyNameForField(srcField) |
| 189 | |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 190 | srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { |
Jaewoong Jung | 729c0bd | 2020-12-08 19:11:54 -0800 | [diff] [blame] | 191 | if !module.Enabled() { |
Jaewoong Jung | 84f1b80 | 2020-12-04 11:51:29 -0800 | [diff] [blame] | 192 | return nil |
| 193 | } |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 194 | value := srcPropsValue.FieldByIndex(srcFieldIndex) |
| 195 | if value.Kind() == reflect.Ptr { |
| 196 | value = value.Elem() |
| 197 | } |
| 198 | if value.Kind() != reflect.String { |
| 199 | panic(fmt.Errorf("prebuilt src field %q should be a string or a pointer to one but was %d %q", srcPropertyName, value.Kind(), value)) |
| 200 | } |
| 201 | src := value.String() |
| 202 | if src == "" { |
| 203 | return nil |
| 204 | } |
| 205 | return []string{src} |
| 206 | } |
| 207 | |
| 208 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName) |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 211 | type PrebuiltInterface interface { |
| 212 | Module |
| 213 | Prebuilt() *Prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 216 | func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 217 | ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 220 | func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 221 | ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 222 | ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel() |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 223 | ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 226 | // PrebuiltRenameMutator ensures that there always is a module with an |
| 227 | // undecorated name. |
| 228 | func PrebuiltRenameMutator(ctx BottomUpMutatorContext) { |
| 229 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 230 | name := m.base().BaseModuleName() |
| 231 | if !ctx.OtherModuleExists(name) { |
| 232 | ctx.Rename(name) |
| 233 | m.Prebuilt().properties.PrebuiltRenamedToSource = true |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the |
| 239 | // corresponding source module, if one exists for the same variant. |
| 240 | func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) { |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 241 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Enabled() && m.Prebuilt() != nil { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 242 | p := m.Prebuilt() |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 243 | if !p.properties.PrebuiltRenamedToSource { |
| 244 | name := m.base().BaseModuleName() |
| 245 | if ctx.OtherModuleReverseDependencyVariantExists(name) { |
| 246 | ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name) |
| 247 | p.properties.SourceExists = true |
| 248 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 253 | // PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or |
| 254 | // 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] | 255 | func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) { |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 256 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 257 | p := m.Prebuilt() |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 258 | if p.srcsSupplier == nil { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 259 | panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it")) |
| 260 | } |
| 261 | if !p.properties.SourceExists { |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 262 | p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil, m) |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 263 | } |
| 264 | } else if s, ok := ctx.Module().(Module); ok { |
Jiyong Park | 03b68dd | 2019-07-26 23:20:40 +0900 | [diff] [blame] | 265 | ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(m Module) { |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 266 | p := m.(PrebuiltInterface).Prebuilt() |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 267 | if p.usePrebuilt(ctx, s, m) { |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 268 | p.properties.UsePrebuilt = true |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 269 | s.ReplacedByPrebuilt() |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 270 | } |
| 271 | }) |
| 272 | } |
| 273 | } |
| 274 | |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 275 | // PrebuiltPostDepsMutator does two operations. It replace dependencies on the |
| 276 | // source module with dependencies on the prebuilt when both modules exist and |
| 277 | // the prebuilt should be used. When the prebuilt should not be used, disable |
| 278 | // installing it. Secondly, it also adds a sourcegroup to any filegroups found |
| 279 | // in the prebuilt's 'Srcs' property. |
| 280 | func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 281 | if m, ok := ctx.Module().(PrebuiltInterface); ok && m.Prebuilt() != nil { |
| 282 | p := m.Prebuilt() |
| 283 | name := m.base().BaseModuleName() |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 284 | if p.properties.UsePrebuilt { |
| 285 | if p.properties.SourceExists { |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 286 | ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool { |
| 287 | if t, ok := tag.(ReplaceSourceWithPrebuilt); ok { |
| 288 | return t.ReplaceSourceWithPrebuilt() |
| 289 | } |
| 290 | |
| 291 | return true |
| 292 | }) |
Colin Cross | 0f3c72f | 2016-11-23 15:44:07 -0800 | [diff] [blame] | 293 | } |
| 294 | } else { |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 295 | m.HideFromMake() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 300 | // usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt |
| 301 | // will be used if it is marked "prefer" or if the source module is disabled. |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 302 | func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module, prebuilt Module) bool { |
| 303 | if p.srcsSupplier != nil && len(p.srcsSupplier(ctx, prebuilt)) == 0 { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 304 | return false |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 305 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 306 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 307 | // TODO: use p.Properties.Name and ctx.ModuleDir to override preference |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 308 | if Bool(p.properties.Prefer) { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 309 | return true |
| 310 | } |
| 311 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 312 | return source == nil || !source.Enabled() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 313 | } |
Jiyong Park | 0a573d7 | 2019-07-07 12:39:16 +0900 | [diff] [blame] | 314 | |
| 315 | func (p *Prebuilt) SourceExists() bool { |
| 316 | return p.properties.SourceExists |
| 317 | } |