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 | |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 64 | // When specified this names a Soong config variable that controls the prefer property. |
| 65 | // |
| 66 | // If the value of the named Soong config variable is true then prefer is set to false and vice |
| 67 | // versa. If the Soong config variable is not set then it defaults to false, so prefer defaults |
| 68 | // to true. |
| 69 | // |
| 70 | // If specified then the prefer property is ignored in favor of the value of the Soong config |
| 71 | // variable. |
| 72 | Use_source_config_var *ConfigVarProperties |
| 73 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 74 | SourceExists bool `blueprint:"mutated"` |
| 75 | UsePrebuilt bool `blueprint:"mutated"` |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 76 | |
| 77 | // Set if the module has been renamed to remove the "prebuilt_" prefix. |
| 78 | PrebuiltRenamedToSource bool `blueprint:"mutated"` |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 81 | // Properties that can be used to select a Soong config variable. |
| 82 | type ConfigVarProperties struct { |
| 83 | // Allow instances of this struct to be used as a property value in a BpPropertySet. |
| 84 | BpPrintableBase |
| 85 | |
| 86 | // The name of the configuration namespace. |
| 87 | // |
| 88 | // As passed to add_soong_config_namespace in Make. |
| 89 | Config_namespace *string |
| 90 | |
| 91 | // The name of the configuration variable. |
| 92 | // |
| 93 | // As passed to add_soong_config_var_value in Make. |
| 94 | Var_name *string |
| 95 | } |
| 96 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 97 | type Prebuilt struct { |
| 98 | properties PrebuiltProperties |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 99 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 100 | srcsSupplier PrebuiltSrcsSupplier |
| 101 | srcsPropertyName string |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Paul Duffin | d23c726 | 2020-12-11 18:13:08 +0000 | [diff] [blame] | 104 | // RemoveOptionalPrebuiltPrefix returns the result of removing the "prebuilt_" prefix from the |
| 105 | // supplied name if it has one, or returns the name unmodified if it does not. |
| 106 | func RemoveOptionalPrebuiltPrefix(name string) string { |
| 107 | return strings.TrimPrefix(name, "prebuilt_") |
| 108 | } |
| 109 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 110 | func (p *Prebuilt) Name(name string) string { |
Paul Duffin | 864116c | 2021-04-02 10:24:13 +0100 | [diff] [blame] | 111 | return PrebuiltNameFromSource(name) |
| 112 | } |
| 113 | |
| 114 | // PrebuiltNameFromSource returns the result of prepending the "prebuilt_" prefix to the supplied |
| 115 | // name. |
| 116 | func PrebuiltNameFromSource(name string) string { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 117 | return "prebuilt_" + name |
| 118 | } |
| 119 | |
Paul Duffin | 5006151 | 2020-01-21 16:31:05 +0000 | [diff] [blame] | 120 | func (p *Prebuilt) ForcePrefer() { |
| 121 | p.properties.Prefer = proptools.BoolPtr(true) |
| 122 | } |
| 123 | |
Paul Duffin | 38b5785 | 2020-05-13 16:08:09 +0100 | [diff] [blame] | 124 | func (p *Prebuilt) Prefer() bool { |
| 125 | return proptools.Bool(p.properties.Prefer) |
| 126 | } |
| 127 | |
Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 128 | // SingleSourcePathFromSupplier invokes the supplied supplier for the current module in the |
| 129 | // supplied context to retrieve a list of file paths, ensures that the returned list of file paths |
| 130 | // contains a single value and then assumes that is a module relative file path and converts it to |
| 131 | // a Path accordingly. |
| 132 | // |
| 133 | // Any issues, such as nil supplier or not exactly one file path will be reported as errors on the |
| 134 | // supplied context and this will return nil. |
| 135 | func SingleSourcePathFromSupplier(ctx ModuleContext, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) Path { |
| 136 | if srcsSupplier != nil { |
| 137 | srcs := srcsSupplier(ctx, ctx.Module()) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 138 | |
| 139 | if len(srcs) == 0 { |
Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 140 | ctx.PropertyErrorf(srcsPropertyName, "missing prebuilt source file") |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 141 | return nil |
| 142 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 143 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 144 | if len(srcs) > 1 { |
Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 145 | ctx.PropertyErrorf(srcsPropertyName, "multiple prebuilt source files") |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 146 | return nil |
| 147 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 148 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 149 | // Return the singleton source after expanding any filegroup in the |
| 150 | // sources. |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 151 | src := srcs[0] |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 152 | return PathForModuleSrc(ctx, src) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 153 | } else { |
| 154 | ctx.ModuleErrorf("prebuilt source was not set") |
| 155 | return nil |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 156 | } |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 159 | // The below source-related functions and the srcs, src fields are based on an assumption that |
| 160 | // prebuilt modules have a static source property at the moment. Currently there is only one |
| 161 | // exception, android_app_import, which chooses a source file depending on the product's DPI |
| 162 | // preference configs. We'll want to add native support for dynamic source cases if we end up having |
| 163 | // more modules like this. |
| 164 | func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path { |
| 165 | return SingleSourcePathFromSupplier(ctx, p.srcsSupplier, p.srcsPropertyName) |
| 166 | } |
| 167 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 168 | func (p *Prebuilt) UsePrebuilt() bool { |
| 169 | return p.properties.UsePrebuilt |
| 170 | } |
| 171 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 172 | // Called to provide the srcs value for the prebuilt module. |
| 173 | // |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 174 | // This can be called with a context for any module not just the prebuilt one itself. It can also be |
| 175 | // called concurrently. |
| 176 | // |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 177 | // Return the src value or nil if it is not available. |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 178 | type PrebuiltSrcsSupplier func(ctx BaseModuleContext, prebuilt Module) []string |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 179 | |
| 180 | // Initialize the module as a prebuilt module that uses the provided supplier to access the |
| 181 | // prebuilt sources of the module. |
| 182 | // |
| 183 | // The supplier will be called multiple times and must return the same values each time it |
| 184 | // is called. If it returns an empty array (or nil) then the prebuilt module will not be used |
| 185 | // as a replacement for a source module with the same name even if prefer = true. |
| 186 | // |
| 187 | // If the Prebuilt.SingleSourcePath() is called on the module then this must return an array |
| 188 | // containing exactly one source file. |
| 189 | // |
| 190 | // The provided property name is used to provide helpful error messages in the event that |
| 191 | // a problem arises, e.g. calling SingleSourcePath() when more than one source is provided. |
| 192 | func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 193 | p := module.Prebuilt() |
| 194 | module.AddProperties(&p.properties) |
Liz Kammer | 69d6413 | 2021-06-24 13:03:06 -0400 | [diff] [blame] | 195 | module.base().customizableProperties = module.GetProperties() |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 196 | |
| 197 | if srcsSupplier == nil { |
| 198 | panic(fmt.Errorf("srcsSupplier must not be nil")) |
| 199 | } |
| 200 | if srcsPropertyName == "" { |
| 201 | panic(fmt.Errorf("srcsPropertyName must not be empty")) |
| 202 | } |
| 203 | |
| 204 | p.srcsSupplier = srcsSupplier |
| 205 | p.srcsPropertyName = srcsPropertyName |
| 206 | } |
| 207 | |
| 208 | func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) { |
| 209 | if srcs == nil { |
| 210 | panic(fmt.Errorf("srcs must not be nil")) |
| 211 | } |
| 212 | |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 213 | srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 214 | return *srcs |
| 215 | } |
| 216 | |
| 217 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 220 | func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 221 | srcPropsValue := reflect.ValueOf(srcProps).Elem() |
| 222 | srcStructField, _ := srcPropsValue.Type().FieldByName(srcField) |
| 223 | if !srcPropsValue.IsValid() || srcStructField.Name == "" { |
| 224 | panic(fmt.Errorf("invalid single source prebuilt %+v", module)) |
| 225 | } |
| 226 | |
| 227 | if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface { |
| 228 | panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps)) |
| 229 | } |
| 230 | |
| 231 | srcFieldIndex := srcStructField.Index |
| 232 | srcPropertyName := proptools.PropertyNameForField(srcField) |
| 233 | |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 234 | srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { |
Jaewoong Jung | 729c0bd | 2020-12-08 19:11:54 -0800 | [diff] [blame] | 235 | if !module.Enabled() { |
Jaewoong Jung | 84f1b80 | 2020-12-04 11:51:29 -0800 | [diff] [blame] | 236 | return nil |
| 237 | } |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 238 | value := srcPropsValue.FieldByIndex(srcFieldIndex) |
| 239 | if value.Kind() == reflect.Ptr { |
| 240 | value = value.Elem() |
| 241 | } |
| 242 | if value.Kind() != reflect.String { |
Martin Stjernholm | 25a69de | 2021-09-09 02:48:30 +0100 | [diff] [blame^] | 243 | panic(fmt.Errorf("prebuilt src field %q in %T in module %s should be a string or a pointer to one but was %v", srcField, srcProps, module, value)) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 244 | } |
| 245 | src := value.String() |
| 246 | if src == "" { |
| 247 | return nil |
| 248 | } |
| 249 | return []string{src} |
| 250 | } |
| 251 | |
| 252 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName) |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 255 | type PrebuiltInterface interface { |
| 256 | Module |
| 257 | Prebuilt() *Prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Paul Duffin | e1d3837 | 2021-04-02 10:35:24 +0100 | [diff] [blame] | 260 | // IsModulePreferred returns true if the given module is preferred. |
| 261 | // |
| 262 | // A source module is preferred if there is no corresponding prebuilt module or the prebuilt module |
| 263 | // does not have "prefer: true". |
| 264 | // |
| 265 | // A prebuilt module is preferred if there is no corresponding source module or the prebuilt module |
| 266 | // has "prefer: true". |
| 267 | func IsModulePreferred(module Module) bool { |
| 268 | if module.IsReplacedByPrebuilt() { |
| 269 | // A source module that has been replaced by a prebuilt counterpart. |
| 270 | return false |
| 271 | } |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 272 | if p := GetEmbeddedPrebuilt(module); p != nil { |
| 273 | return p.UsePrebuilt() |
Paul Duffin | e1d3837 | 2021-04-02 10:35:24 +0100 | [diff] [blame] | 274 | } |
| 275 | return true |
| 276 | } |
| 277 | |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 278 | // IsModulePrebuilt returns true if the module implements PrebuiltInterface and |
| 279 | // has been initialized as a prebuilt and so returns a non-nil value from the |
| 280 | // PrebuiltInterface.Prebuilt() method. |
| 281 | func IsModulePrebuilt(module Module) bool { |
| 282 | return GetEmbeddedPrebuilt(module) != nil |
| 283 | } |
| 284 | |
| 285 | // GetEmbeddedPrebuilt returns a pointer to the embedded Prebuilt structure or |
| 286 | // nil if the module does not implement PrebuiltInterface or has not been |
| 287 | // initialized as a prebuilt module. |
| 288 | func GetEmbeddedPrebuilt(module Module) *Prebuilt { |
| 289 | if p, ok := module.(PrebuiltInterface); ok { |
| 290 | return p.Prebuilt() |
| 291 | } |
| 292 | |
| 293 | return nil |
| 294 | } |
| 295 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 296 | func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 297 | ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 300 | func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 301 | ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 302 | ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel() |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 303 | ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 306 | // PrebuiltRenameMutator ensures that there always is a module with an |
| 307 | // undecorated name. |
| 308 | func PrebuiltRenameMutator(ctx BottomUpMutatorContext) { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 309 | m := ctx.Module() |
| 310 | if p := GetEmbeddedPrebuilt(m); p != nil { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 311 | name := m.base().BaseModuleName() |
| 312 | if !ctx.OtherModuleExists(name) { |
| 313 | ctx.Rename(name) |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 314 | p.properties.PrebuiltRenamedToSource = true |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the |
| 320 | // corresponding source module, if one exists for the same variant. |
| 321 | func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 322 | m := ctx.Module() |
| 323 | // If this module is a prebuilt, is enabled and has not been renamed to source then add a |
| 324 | // dependency onto the source if it is present. |
| 325 | if p := GetEmbeddedPrebuilt(m); p != nil && m.Enabled() && !p.properties.PrebuiltRenamedToSource { |
| 326 | name := m.base().BaseModuleName() |
| 327 | if ctx.OtherModuleReverseDependencyVariantExists(name) { |
| 328 | ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name) |
| 329 | p.properties.SourceExists = true |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 334 | // PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or |
| 335 | // 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] | 336 | func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 337 | m := ctx.Module() |
| 338 | if p := GetEmbeddedPrebuilt(m); p != nil { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 339 | if p.srcsSupplier == nil { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 340 | panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it")) |
| 341 | } |
| 342 | if !p.properties.SourceExists { |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 343 | p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil, m) |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 344 | } |
| 345 | } else if s, ok := ctx.Module().(Module); ok { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 346 | ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(prebuiltModule Module) { |
| 347 | p := GetEmbeddedPrebuilt(prebuiltModule) |
| 348 | if p.usePrebuilt(ctx, s, prebuiltModule) { |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 349 | p.properties.UsePrebuilt = true |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 350 | s.ReplacedByPrebuilt() |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 351 | } |
| 352 | }) |
| 353 | } |
| 354 | } |
| 355 | |
Jaewoong Jung | 6158dfe | 2021-03-23 14:08:29 -0700 | [diff] [blame] | 356 | // PrebuiltPostDepsMutator replaces dependencies on the source module with dependencies on the |
| 357 | // prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not |
| 358 | // be used, disable installing it. |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 359 | func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 360 | m := ctx.Module() |
| 361 | if p := GetEmbeddedPrebuilt(m); p != nil { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 362 | name := m.base().BaseModuleName() |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 363 | if p.properties.UsePrebuilt { |
| 364 | if p.properties.SourceExists { |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 365 | ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool { |
| 366 | if t, ok := tag.(ReplaceSourceWithPrebuilt); ok { |
| 367 | return t.ReplaceSourceWithPrebuilt() |
| 368 | } |
| 369 | |
| 370 | return true |
| 371 | }) |
Colin Cross | 0f3c72f | 2016-11-23 15:44:07 -0800 | [diff] [blame] | 372 | } |
| 373 | } else { |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 374 | m.HideFromMake() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 379 | // usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt |
| 380 | // 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] | 381 | func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module, prebuilt Module) bool { |
| 382 | if p.srcsSupplier != nil && len(p.srcsSupplier(ctx, prebuilt)) == 0 { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 383 | return false |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 384 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 385 | |
LuK1337 | fb545bf | 2021-01-10 17:47:46 +0100 | [diff] [blame] | 386 | // Skip prebuilt modules under unexported namespaces so that we won't |
| 387 | // end up shadowing non-prebuilt module when prebuilt module under same |
| 388 | // name happens to have a `Prefer` property set to true. |
| 389 | if ctx.Config().KatiEnabled() && !prebuilt.ExportedToMake() { |
| 390 | return false |
| 391 | } |
| 392 | |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 393 | // If source is not available or is disabled then always use the prebuilt. |
| 394 | if source == nil || !source.Enabled() { |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 395 | return true |
| 396 | } |
| 397 | |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 398 | // If the use_source_config_var property is set then it overrides the prefer property setting. |
| 399 | if configVar := p.properties.Use_source_config_var; configVar != nil { |
| 400 | return !ctx.Config().VendorConfig(proptools.String(configVar.Config_namespace)).Bool(proptools.String(configVar.Var_name)) |
| 401 | } |
| 402 | |
| 403 | // TODO: use p.Properties.Name and ctx.ModuleDir to override preference |
| 404 | return Bool(p.properties.Prefer) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 405 | } |
Jiyong Park | 0a573d7 | 2019-07-07 12:39:16 +0900 | [diff] [blame] | 406 | |
| 407 | func (p *Prebuilt) SourceExists() bool { |
| 408 | return p.properties.SourceExists |
| 409 | } |