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 | |
Paul Duffin | bf4de04 | 2022-09-27 12:41:52 +0100 | [diff] [blame] | 59 | // UserSuppliedPrebuiltProperties contains the prebuilt properties that can be specified in an |
| 60 | // Android.bp file. |
| 61 | type UserSuppliedPrebuiltProperties struct { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 62 | // When prefer is set to true the prebuilt will be used instead of any source module with |
| 63 | // a matching name. |
Cole Faust | 12ff57d | 2024-07-30 13:17:28 -0700 | [diff] [blame] | 64 | Prefer proptools.Configurable[bool] `android:"arch_variant,replace_instead_of_append"` |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 65 | |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 66 | // When specified this names a Soong config variable that controls the prefer property. |
| 67 | // |
| 68 | // If the value of the named Soong config variable is true then prefer is set to false and vice |
| 69 | // versa. If the Soong config variable is not set then it defaults to false, so prefer defaults |
| 70 | // to true. |
| 71 | // |
| 72 | // If specified then the prefer property is ignored in favor of the value of the Soong config |
| 73 | // variable. |
Spandan Das | a9cf0c8 | 2024-04-01 22:28:59 +0000 | [diff] [blame] | 74 | // |
| 75 | // DEPRECATED: This property is being deprecated b/308188211. |
| 76 | // Use RELEASE_APEX_CONTRIBUTIONS build flags to select prebuilts of mainline modules. |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 77 | Use_source_config_var *ConfigVarProperties |
Paul Duffin | bf4de04 | 2022-09-27 12:41:52 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // CopyUserSuppliedPropertiesFromPrebuilt copies the user supplied prebuilt properties from the |
| 81 | // prebuilt properties. |
| 82 | func (u *UserSuppliedPrebuiltProperties) CopyUserSuppliedPropertiesFromPrebuilt(p *Prebuilt) { |
| 83 | *u = p.properties.UserSuppliedPrebuiltProperties |
| 84 | } |
| 85 | |
| 86 | type PrebuiltProperties struct { |
| 87 | UserSuppliedPrebuiltProperties |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 88 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 89 | SourceExists bool `blueprint:"mutated"` |
| 90 | UsePrebuilt bool `blueprint:"mutated"` |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 91 | |
| 92 | // Set if the module has been renamed to remove the "prebuilt_" prefix. |
| 93 | PrebuiltRenamedToSource bool `blueprint:"mutated"` |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 96 | // Properties that can be used to select a Soong config variable. |
| 97 | type ConfigVarProperties struct { |
| 98 | // Allow instances of this struct to be used as a property value in a BpPropertySet. |
| 99 | BpPrintableBase |
| 100 | |
| 101 | // The name of the configuration namespace. |
| 102 | // |
| 103 | // As passed to add_soong_config_namespace in Make. |
| 104 | Config_namespace *string |
| 105 | |
| 106 | // The name of the configuration variable. |
| 107 | // |
| 108 | // As passed to add_soong_config_var_value in Make. |
| 109 | Var_name *string |
| 110 | } |
| 111 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 112 | type Prebuilt struct { |
| 113 | properties PrebuiltProperties |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 114 | |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 115 | // nil if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs. |
| 116 | srcsSupplier PrebuiltSrcsSupplier |
| 117 | |
| 118 | // "-" if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs. |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 119 | srcsPropertyName string |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Paul Duffin | d23c726 | 2020-12-11 18:13:08 +0000 | [diff] [blame] | 122 | // RemoveOptionalPrebuiltPrefix returns the result of removing the "prebuilt_" prefix from the |
| 123 | // supplied name if it has one, or returns the name unmodified if it does not. |
| 124 | func RemoveOptionalPrebuiltPrefix(name string) string { |
| 125 | return strings.TrimPrefix(name, "prebuilt_") |
| 126 | } |
| 127 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 128 | // RemoveOptionalPrebuiltPrefixFromBazelLabel removes the "prebuilt_" prefix from the *target name* of a Bazel label. |
| 129 | // This differs from RemoveOptionalPrebuiltPrefix in that it does not remove it from the start of the string, but |
| 130 | // instead removes it from the target name itself. |
| 131 | func RemoveOptionalPrebuiltPrefixFromBazelLabel(label string) string { |
| 132 | splitLabel := strings.Split(label, ":") |
| 133 | bazelModuleNameNoPrebuilt := RemoveOptionalPrebuiltPrefix(splitLabel[1]) |
| 134 | return strings.Join([]string{ |
| 135 | splitLabel[0], |
| 136 | bazelModuleNameNoPrebuilt, |
| 137 | }, ":") |
| 138 | } |
| 139 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 140 | func (p *Prebuilt) Name(name string) string { |
Paul Duffin | 864116c | 2021-04-02 10:24:13 +0100 | [diff] [blame] | 141 | return PrebuiltNameFromSource(name) |
| 142 | } |
| 143 | |
| 144 | // PrebuiltNameFromSource returns the result of prepending the "prebuilt_" prefix to the supplied |
| 145 | // name. |
| 146 | func PrebuiltNameFromSource(name string) string { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 147 | return "prebuilt_" + name |
| 148 | } |
| 149 | |
Paul Duffin | 5006151 | 2020-01-21 16:31:05 +0000 | [diff] [blame] | 150 | func (p *Prebuilt) ForcePrefer() { |
Cole Faust | 12ff57d | 2024-07-30 13:17:28 -0700 | [diff] [blame] | 151 | p.properties.Prefer = NewSimpleConfigurable(true) |
Paul Duffin | 38b5785 | 2020-05-13 16:08:09 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 154 | // SingleSourcePathFromSupplier invokes the supplied supplier for the current module in the |
| 155 | // supplied context to retrieve a list of file paths, ensures that the returned list of file paths |
| 156 | // contains a single value and then assumes that is a module relative file path and converts it to |
| 157 | // a Path accordingly. |
| 158 | // |
| 159 | // Any issues, such as nil supplier or not exactly one file path will be reported as errors on the |
| 160 | // supplied context and this will return nil. |
| 161 | func SingleSourcePathFromSupplier(ctx ModuleContext, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) Path { |
| 162 | if srcsSupplier != nil { |
| 163 | srcs := srcsSupplier(ctx, ctx.Module()) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 164 | |
| 165 | if len(srcs) == 0 { |
Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 166 | ctx.PropertyErrorf(srcsPropertyName, "missing prebuilt source file") |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 167 | return nil |
| 168 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 169 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 170 | if len(srcs) > 1 { |
Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 171 | ctx.PropertyErrorf(srcsPropertyName, "multiple prebuilt source files") |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 172 | return nil |
| 173 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 174 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 175 | // Return the singleton source after expanding any filegroup in the |
| 176 | // sources. |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 177 | src := srcs[0] |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 178 | return PathForModuleSrc(ctx, src) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 179 | } else { |
| 180 | ctx.ModuleErrorf("prebuilt source was not set") |
| 181 | return nil |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 182 | } |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 185 | // The below source-related functions and the srcs, src fields are based on an assumption that |
| 186 | // prebuilt modules have a static source property at the moment. Currently there is only one |
| 187 | // exception, android_app_import, which chooses a source file depending on the product's DPI |
| 188 | // preference configs. We'll want to add native support for dynamic source cases if we end up having |
| 189 | // more modules like this. |
| 190 | func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path { |
| 191 | return SingleSourcePathFromSupplier(ctx, p.srcsSupplier, p.srcsPropertyName) |
| 192 | } |
| 193 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 194 | func (p *Prebuilt) UsePrebuilt() bool { |
| 195 | return p.properties.UsePrebuilt |
| 196 | } |
| 197 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 198 | // Called to provide the srcs value for the prebuilt module. |
| 199 | // |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 200 | // This can be called with a context for any module not just the prebuilt one itself. It can also be |
| 201 | // called concurrently. |
| 202 | // |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 203 | // Return the src value or nil if it is not available. |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 204 | type PrebuiltSrcsSupplier func(ctx BaseModuleContext, prebuilt Module) []string |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 205 | |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 206 | func initPrebuiltModuleCommon(module PrebuiltInterface) *Prebuilt { |
| 207 | p := module.Prebuilt() |
| 208 | module.AddProperties(&p.properties) |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 209 | return p |
| 210 | } |
| 211 | |
| 212 | // Initialize the module as a prebuilt module that has no dedicated property that lists its |
| 213 | // sources. SingleSourcePathFromSupplier should not be called for this module. |
| 214 | // |
| 215 | // This is the case e.g. for header modules, which provides the headers in source form |
| 216 | // regardless whether they are prebuilt or not. |
| 217 | func InitPrebuiltModuleWithoutSrcs(module PrebuiltInterface) { |
| 218 | p := initPrebuiltModuleCommon(module) |
| 219 | p.srcsPropertyName = "-" |
| 220 | } |
| 221 | |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 222 | // Initialize the module as a prebuilt module that uses the provided supplier to access the |
| 223 | // prebuilt sources of the module. |
| 224 | // |
| 225 | // The supplier will be called multiple times and must return the same values each time it |
| 226 | // is called. If it returns an empty array (or nil) then the prebuilt module will not be used |
| 227 | // as a replacement for a source module with the same name even if prefer = true. |
| 228 | // |
| 229 | // If the Prebuilt.SingleSourcePath() is called on the module then this must return an array |
| 230 | // containing exactly one source file. |
| 231 | // |
| 232 | // The provided property name is used to provide helpful error messages in the event that |
| 233 | // a problem arises, e.g. calling SingleSourcePath() when more than one source is provided. |
| 234 | func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 235 | if srcsSupplier == nil { |
| 236 | panic(fmt.Errorf("srcsSupplier must not be nil")) |
| 237 | } |
| 238 | if srcsPropertyName == "" { |
| 239 | panic(fmt.Errorf("srcsPropertyName must not be empty")) |
| 240 | } |
| 241 | |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 242 | p := initPrebuiltModuleCommon(module) |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 243 | p.srcsSupplier = srcsSupplier |
| 244 | p.srcsPropertyName = srcsPropertyName |
| 245 | } |
| 246 | |
Cole Faust | 96a692b | 2024-08-08 14:47:51 -0700 | [diff] [blame] | 247 | // InitPrebuiltModule is the same as InitPrebuiltModuleWithSrcSupplier, but uses the |
| 248 | // provided list of strings property as the source provider. |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 249 | func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) { |
| 250 | if srcs == nil { |
| 251 | panic(fmt.Errorf("srcs must not be nil")) |
| 252 | } |
| 253 | |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 254 | srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 255 | return *srcs |
| 256 | } |
| 257 | |
| 258 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Cole Faust | 96a692b | 2024-08-08 14:47:51 -0700 | [diff] [blame] | 261 | // InitConfigurablePrebuiltModule is the same as InitPrebuiltModule, but uses a |
| 262 | // Configurable list of strings property instead of a regular list of strings. |
| 263 | func InitConfigurablePrebuiltModule(module PrebuiltInterface, srcs *proptools.Configurable[[]string]) { |
| 264 | if srcs == nil { |
| 265 | panic(fmt.Errorf("srcs must not be nil")) |
| 266 | } |
| 267 | |
| 268 | srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { |
| 269 | return srcs.GetOrDefault(ctx, nil) |
| 270 | } |
| 271 | |
| 272 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") |
| 273 | } |
| 274 | |
Cole Faust | c71b175 | 2024-10-14 12:14:54 -0700 | [diff] [blame] | 275 | // InitConfigurablePrebuiltModuleString is the same as InitPrebuiltModule, but uses a |
| 276 | // Configurable string property instead of a regular list of strings. It only produces a single |
| 277 | // source file. |
| 278 | func InitConfigurablePrebuiltModuleString(module PrebuiltInterface, srcs *proptools.Configurable[string], propertyName string) { |
| 279 | if srcs == nil { |
| 280 | panic(fmt.Errorf("%s must not be nil", propertyName)) |
| 281 | } |
| 282 | |
| 283 | srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { |
| 284 | src := srcs.GetOrDefault(ctx, "") |
| 285 | if src == "" { |
| 286 | return nil |
| 287 | } |
| 288 | return []string{src} |
| 289 | } |
| 290 | |
| 291 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, propertyName) |
| 292 | } |
| 293 | |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 294 | func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) { |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 295 | srcPropsValue := reflect.ValueOf(srcProps).Elem() |
| 296 | srcStructField, _ := srcPropsValue.Type().FieldByName(srcField) |
| 297 | if !srcPropsValue.IsValid() || srcStructField.Name == "" { |
| 298 | panic(fmt.Errorf("invalid single source prebuilt %+v", module)) |
| 299 | } |
| 300 | |
| 301 | if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface { |
| 302 | panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps)) |
| 303 | } |
| 304 | |
| 305 | srcFieldIndex := srcStructField.Index |
| 306 | srcPropertyName := proptools.PropertyNameForField(srcField) |
| 307 | |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 308 | srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 309 | if !module.Enabled(ctx) { |
Jaewoong Jung | 84f1b80 | 2020-12-04 11:51:29 -0800 | [diff] [blame] | 310 | return nil |
| 311 | } |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 312 | value := srcPropsValue.FieldByIndex(srcFieldIndex) |
| 313 | if value.Kind() == reflect.Ptr { |
Cole Faust | 97494b1 | 2024-01-12 14:02:47 -0800 | [diff] [blame] | 314 | if value.IsNil() { |
| 315 | return nil |
| 316 | } |
Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 317 | value = value.Elem() |
| 318 | } |
| 319 | if value.Kind() != reflect.String { |
Martin Stjernholm | 25a69de | 2021-09-09 02:48:30 +0100 | [diff] [blame] | 320 | 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] | 321 | } |
| 322 | src := value.String() |
| 323 | if src == "" { |
| 324 | return nil |
| 325 | } |
| 326 | return []string{src} |
| 327 | } |
| 328 | |
| 329 | InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName) |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 332 | type PrebuiltInterface interface { |
| 333 | Module |
| 334 | Prebuilt() *Prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Paul Duffin | e1d3837 | 2021-04-02 10:35:24 +0100 | [diff] [blame] | 337 | // IsModulePreferred returns true if the given module is preferred. |
| 338 | // |
| 339 | // A source module is preferred if there is no corresponding prebuilt module or the prebuilt module |
| 340 | // does not have "prefer: true". |
| 341 | // |
| 342 | // A prebuilt module is preferred if there is no corresponding source module or the prebuilt module |
| 343 | // has "prefer: true". |
| 344 | func IsModulePreferred(module Module) bool { |
| 345 | if module.IsReplacedByPrebuilt() { |
| 346 | // A source module that has been replaced by a prebuilt counterpart. |
| 347 | return false |
| 348 | } |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 349 | if p := GetEmbeddedPrebuilt(module); p != nil { |
| 350 | return p.UsePrebuilt() |
Paul Duffin | e1d3837 | 2021-04-02 10:35:24 +0100 | [diff] [blame] | 351 | } |
| 352 | return true |
| 353 | } |
| 354 | |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 355 | // IsModulePrebuilt returns true if the module implements PrebuiltInterface and |
| 356 | // has been initialized as a prebuilt and so returns a non-nil value from the |
| 357 | // PrebuiltInterface.Prebuilt() method. |
| 358 | func IsModulePrebuilt(module Module) bool { |
| 359 | return GetEmbeddedPrebuilt(module) != nil |
| 360 | } |
| 361 | |
| 362 | // GetEmbeddedPrebuilt returns a pointer to the embedded Prebuilt structure or |
| 363 | // nil if the module does not implement PrebuiltInterface or has not been |
| 364 | // initialized as a prebuilt module. |
| 365 | func GetEmbeddedPrebuilt(module Module) *Prebuilt { |
| 366 | if p, ok := module.(PrebuiltInterface); ok { |
| 367 | return p.Prebuilt() |
| 368 | } |
| 369 | |
| 370 | return nil |
| 371 | } |
| 372 | |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 373 | // PrebuiltGetPreferred returns the module that is preferred for the given |
| 374 | // module. That is either the module itself or the prebuilt counterpart that has |
| 375 | // taken its place. The given module must be a direct dependency of the current |
| 376 | // context module, and it must be the source module if both source and prebuilt |
| 377 | // exist. |
| 378 | // |
| 379 | // This function is for use on dependencies after PrebuiltPostDepsMutator has |
| 380 | // run - any dependency that is registered before that will already reference |
Colin Cross | 1e954b6 | 2024-09-13 13:50:00 -0700 | [diff] [blame] | 381 | // the right module. This function is only safe to call after all TransitionMutators |
| 382 | // have run, e.g. in GenerateAndroidBuildActions. |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 383 | func PrebuiltGetPreferred(ctx BaseModuleContext, module Module) Module { |
Yu Liu | b527532 | 2024-11-13 18:40:43 +0000 | [diff] [blame] | 384 | if !OtherModuleProviderOrDefault(ctx, module, CommonModuleInfoKey).ReplacedByPrebuilt { |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 385 | return module |
| 386 | } |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 387 | if _, ok := OtherModuleProvider(ctx, module, PrebuiltModuleProviderKey); ok { |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 388 | // If we're given a prebuilt then assume there's no source module around. |
| 389 | return module |
| 390 | } |
| 391 | |
| 392 | sourceModDepFound := false |
| 393 | var prebuiltMod Module |
| 394 | |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 395 | ctx.WalkDepsProxy(func(child, parent ModuleProxy) bool { |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 396 | if prebuiltMod != nil { |
| 397 | return false |
| 398 | } |
Yu Liu | d2a9595 | 2024-10-10 00:15:26 +0000 | [diff] [blame] | 399 | if ctx.EqualModules(parent, ctx.Module()) { |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 400 | // First level: Only recurse if the module is found as a direct dependency. |
| 401 | sourceModDepFound = child == module |
| 402 | return sourceModDepFound |
| 403 | } |
| 404 | // Second level: Follow PrebuiltDepTag to the prebuilt. |
| 405 | if t := ctx.OtherModuleDependencyTag(child); t == PrebuiltDepTag { |
| 406 | prebuiltMod = child |
| 407 | } |
| 408 | return false |
| 409 | }) |
| 410 | |
| 411 | if prebuiltMod == nil { |
| 412 | if !sourceModDepFound { |
| 413 | panic(fmt.Errorf("Failed to find source module as a direct dependency: %s", module)) |
| 414 | } else { |
| 415 | panic(fmt.Errorf("Failed to find prebuilt for source module: %s", module)) |
| 416 | } |
| 417 | } |
| 418 | return prebuiltMod |
| 419 | } |
| 420 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 421 | func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) { |
Colin Cross | 8a96280 | 2024-10-09 15:29:27 -0700 | [diff] [blame] | 422 | ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).UsesRename() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 425 | func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) { |
Colin Cross | 8a96280 | 2024-10-09 15:29:27 -0700 | [diff] [blame] | 426 | ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).UsesReverseDependencies() |
| 427 | ctx.BottomUp("prebuilt_select", PrebuiltSelectModuleMutator) |
| 428 | ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).UsesReplaceDependencies() |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 431 | // Returns the name of the source module corresponding to a prebuilt module |
| 432 | // For source modules, it returns its own name |
| 433 | type baseModuleName interface { |
| 434 | BaseModuleName() string |
| 435 | } |
| 436 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 437 | // PrebuiltRenameMutator ensures that there always is a module with an |
| 438 | // undecorated name. |
| 439 | func PrebuiltRenameMutator(ctx BottomUpMutatorContext) { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 440 | m := ctx.Module() |
| 441 | if p := GetEmbeddedPrebuilt(m); p != nil { |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 442 | bmn, _ := m.(baseModuleName) |
| 443 | name := bmn.BaseModuleName() |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 444 | if !ctx.OtherModuleExists(name) { |
| 445 | ctx.Rename(name) |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 446 | p.properties.PrebuiltRenamedToSource = true |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | // PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the |
| 452 | // corresponding source module, if one exists for the same variant. |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 453 | // Add a dependency from the prebuilt to `all_apex_contributions` |
| 454 | // The metadata will be used for source vs prebuilts selection |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 455 | func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 456 | m := ctx.Module() |
Spandan Das | 85bd462 | 2024-08-01 00:51:20 +0000 | [diff] [blame] | 457 | if p := GetEmbeddedPrebuilt(m); p != nil { |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 458 | // Add a dependency from the prebuilt to the `all_apex_contributions` |
| 459 | // metadata module |
| 460 | // TODO: When all branches contain this singleton module, make this strict |
| 461 | // TODO: Add this dependency only for mainline prebuilts and not every prebuilt module |
| 462 | if ctx.OtherModuleExists("all_apex_contributions") { |
Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 463 | ctx.AddDependency(m, AcDepTag, "all_apex_contributions") |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 464 | } |
Spandan Das | 85bd462 | 2024-08-01 00:51:20 +0000 | [diff] [blame] | 465 | if m.Enabled(ctx) && !p.properties.PrebuiltRenamedToSource { |
| 466 | // If this module is a prebuilt, is enabled and has not been renamed to source then add a |
| 467 | // dependency onto the source if it is present. |
| 468 | bmn, _ := m.(baseModuleName) |
| 469 | name := bmn.BaseModuleName() |
| 470 | if ctx.OtherModuleReverseDependencyVariantExists(name) { |
| 471 | ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name) |
| 472 | p.properties.SourceExists = true |
| 473 | } |
| 474 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Jooyung Han | ebaa573 | 2023-05-02 11:43:14 +0900 | [diff] [blame] | 478 | // checkInvariantsForSourceAndPrebuilt checks if invariants are kept when replacing |
| 479 | // source with prebuilt. Note that the current module for the context is the source module. |
| 480 | func checkInvariantsForSourceAndPrebuilt(ctx BaseModuleContext, s, p Module) { |
| 481 | if _, ok := s.(OverrideModule); ok { |
| 482 | // skip the check when the source module is `override_X` because it's only a placeholder |
| 483 | // for the actual source module. The check will be invoked for the actual module. |
| 484 | return |
| 485 | } |
| 486 | if sourcePartition, prebuiltPartition := s.PartitionTag(ctx.DeviceConfig()), p.PartitionTag(ctx.DeviceConfig()); sourcePartition != prebuiltPartition { |
| 487 | ctx.OtherModuleErrorf(p, "partition is different: %s(%s) != %s(%s)", |
| 488 | sourcePartition, ctx.ModuleName(), prebuiltPartition, ctx.OtherModuleName(p)) |
| 489 | } |
| 490 | } |
| 491 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 492 | // PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or |
| 493 | // because the source module doesn't exist. It also disables installing overridden source modules. |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 494 | // |
| 495 | // If the visited module is the metadata module `all_apex_contributions`, it sets a |
| 496 | // provider containing metadata about whether source or prebuilt of mainline modules should be used. |
| 497 | // This logic was added here to prevent the overhead of creating a new mutator. |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 498 | func PrebuiltSelectModuleMutator(ctx BottomUpMutatorContext) { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 499 | m := ctx.Module() |
| 500 | if p := GetEmbeddedPrebuilt(m); p != nil { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 501 | if p.srcsSupplier == nil && p.srcsPropertyName == "" { |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 502 | panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it")) |
| 503 | } |
| 504 | if !p.properties.SourceExists { |
Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 505 | p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil, m) |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 506 | } |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 507 | // Propagate the provider received from `all_apex_contributions` |
| 508 | // to the source module |
Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 509 | ctx.VisitDirectDepsWithTag(AcDepTag, func(am Module) { |
Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 510 | psi, _ := OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider) |
| 511 | SetProvider(ctx, PrebuiltSelectionInfoProvider, psi) |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 512 | }) |
| 513 | |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 514 | } else if s, ok := ctx.Module().(Module); ok { |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 515 | // Use `all_apex_contributions` for source vs prebuilt selection. |
| 516 | psi := PrebuiltSelectionInfoMap{} |
| 517 | ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(am Module) { |
| 518 | // The value of psi gets overwritten with the provider from the last visited prebuilt. |
| 519 | // But all prebuilts have the same value of the provider, so this should be idempontent. |
| 520 | psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider) |
| 521 | }) |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 522 | ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(prebuiltModule Module) { |
| 523 | p := GetEmbeddedPrebuilt(prebuiltModule) |
| 524 | if p.usePrebuilt(ctx, s, prebuiltModule) { |
Jooyung Han | ebaa573 | 2023-05-02 11:43:14 +0900 | [diff] [blame] | 525 | checkInvariantsForSourceAndPrebuilt(ctx, s, prebuiltModule) |
| 526 | |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 527 | p.properties.UsePrebuilt = true |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 528 | s.ReplacedByPrebuilt() |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 529 | } |
| 530 | }) |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 531 | |
| 532 | // If any module in this mainline module family has been flagged using apex_contributions, disable every other module in that family |
| 533 | // Add source |
| 534 | allModules := []Module{s} |
| 535 | // Add each prebuilt |
| 536 | ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(prebuiltModule Module) { |
| 537 | allModules = append(allModules, prebuiltModule) |
| 538 | }) |
| 539 | hideUnflaggedModules(ctx, psi, allModules) |
| 540 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 541 | } |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 542 | |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 543 | // If this is `all_apex_contributions`, set a provider containing |
| 544 | // metadata about source vs prebuilts selection |
| 545 | if am, ok := m.(*allApexContributions); ok { |
| 546 | am.SetPrebuiltSelectionInfoProvider(ctx) |
| 547 | } |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 550 | // If any module in this mainline module family has been flagged using apex_contributions, disable every other module in that family |
| 551 | func hideUnflaggedModules(ctx BottomUpMutatorContext, psi PrebuiltSelectionInfoMap, allModulesInFamily []Module) { |
| 552 | var selectedModuleInFamily Module |
| 553 | // query all_apex_contributions to see if any module in this family has been selected |
| 554 | for _, moduleInFamily := range allModulesInFamily { |
| 555 | // validate that are no duplicates |
Spandan Das | 23956d1 | 2024-01-19 00:22:22 +0000 | [diff] [blame] | 556 | if isSelected(psi, moduleInFamily) { |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 557 | if selectedModuleInFamily == nil { |
| 558 | // Store this so we can validate that there are no duplicates |
| 559 | selectedModuleInFamily = moduleInFamily |
| 560 | } else { |
| 561 | // There are duplicate modules from the same mainline module family |
| 562 | ctx.ModuleErrorf("Found duplicate variations of the same module in apex_contributions: %s and %s. Please remove one of these.\n", selectedModuleInFamily.Name(), moduleInFamily.Name()) |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | // If a module has been selected, hide all other modules |
| 568 | if selectedModuleInFamily != nil { |
| 569 | for _, moduleInFamily := range allModulesInFamily { |
| 570 | if moduleInFamily.Name() != selectedModuleInFamily.Name() { |
| 571 | moduleInFamily.HideFromMake() |
Spandan Das | 034af2c | 2024-10-30 21:45:09 +0000 | [diff] [blame] | 572 | moduleInFamily.SkipInstall() |
Spandan Das | f2c1057 | 2024-02-27 04:49:52 +0000 | [diff] [blame] | 573 | // If this is a prebuilt module, unset properties.UsePrebuilt |
| 574 | // properties.UsePrebuilt might evaluate to true via soong config var fallback mechanism |
| 575 | // Set it to false explicitly so that the following mutator does not replace rdeps to this unselected prebuilt |
| 576 | if p := GetEmbeddedPrebuilt(moduleInFamily); p != nil { |
| 577 | p.properties.UsePrebuilt = false |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | // Do a validation pass to make sure that multiple prebuilts of a specific module are not selected. |
| 583 | // This might happen if the prebuilts share the same soong config var namespace. |
| 584 | // This should be an error, unless one of the prebuilts has been explicitly declared in apex_contributions |
| 585 | var selectedPrebuilt Module |
| 586 | for _, moduleInFamily := range allModulesInFamily { |
| 587 | // Skip if this module is in a different namespace |
| 588 | if !moduleInFamily.ExportedToMake() { |
| 589 | continue |
| 590 | } |
| 591 | // Skip for the top-level java_sdk_library_(_import). This has some special cases that need to be addressed first. |
| 592 | // This does not run into non-determinism because PrebuiltPostDepsMutator also has the special case |
| 593 | if sdkLibrary, ok := moduleInFamily.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil { |
| 594 | continue |
| 595 | } |
| 596 | if p := GetEmbeddedPrebuilt(moduleInFamily); p != nil && p.properties.UsePrebuilt { |
| 597 | if selectedPrebuilt == nil { |
| 598 | selectedPrebuilt = moduleInFamily |
| 599 | } else { |
| 600 | ctx.ModuleErrorf("Multiple prebuilt modules %v and %v have been marked as preferred for this source module. "+ |
| 601 | "Please add the appropriate prebuilt module to apex_contributions for this release config.", selectedPrebuilt.Name(), moduleInFamily.Name()) |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
Jaewoong Jung | 6158dfe | 2021-03-23 14:08:29 -0700 | [diff] [blame] | 607 | // PrebuiltPostDepsMutator replaces dependencies on the source module with dependencies on the |
| 608 | // prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not |
| 609 | // be used, disable installing it. |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 610 | func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 611 | m := ctx.Module() |
| 612 | if p := GetEmbeddedPrebuilt(m); p != nil { |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 613 | bmn, _ := m.(baseModuleName) |
| 614 | name := bmn.BaseModuleName() |
Spandan Das | 81d95c5 | 2024-02-01 23:41:11 +0000 | [diff] [blame] | 615 | psi := PrebuiltSelectionInfoMap{} |
Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 616 | ctx.VisitDirectDepsWithTag(AcDepTag, func(am Module) { |
Spandan Das | 81d95c5 | 2024-02-01 23:41:11 +0000 | [diff] [blame] | 617 | psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider) |
| 618 | }) |
| 619 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 620 | if p.properties.UsePrebuilt { |
| 621 | if p.properties.SourceExists { |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 622 | ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool { |
Spandan Das | 81d95c5 | 2024-02-01 23:41:11 +0000 | [diff] [blame] | 623 | if sdkLibrary, ok := m.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil { |
| 624 | // Do not replace deps to the top-level prebuilt java_sdk_library hook. |
| 625 | // This hook has been special-cased in #isSelected to be _always_ active, even in next builds |
| 626 | // for dexpreopt and hiddenapi processing. |
| 627 | // If we do not special-case this here, rdeps referring to a java_sdk_library in next builds via libs |
| 628 | // will get prebuilt stubs |
| 629 | // TODO (b/308187268): Remove this after the apexes have been added to apex_contributions |
Spandan Das | f2c1057 | 2024-02-27 04:49:52 +0000 | [diff] [blame] | 630 | if psi.IsSelected(name) { |
Spandan Das | 81d95c5 | 2024-02-01 23:41:11 +0000 | [diff] [blame] | 631 | return false |
| 632 | } |
| 633 | } |
| 634 | |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 635 | if t, ok := tag.(ReplaceSourceWithPrebuilt); ok { |
| 636 | return t.ReplaceSourceWithPrebuilt() |
| 637 | } |
Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 638 | return true |
| 639 | }) |
Colin Cross | 0f3c72f | 2016-11-23 15:44:07 -0800 | [diff] [blame] | 640 | } |
| 641 | } else { |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 642 | m.HideFromMake() |
Spandan Das | 034af2c | 2024-10-30 21:45:09 +0000 | [diff] [blame] | 643 | m.SkipInstall() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
Spandan Das | fc12d2f | 2023-10-27 20:06:32 +0000 | [diff] [blame] | 648 | // A wrapper around PrebuiltSelectionInfoMap.IsSelected with special handling for java_sdk_library |
| 649 | // java_sdk_library is a macro that creates |
| 650 | // 1. top-level impl library |
| 651 | // 2. stub libraries (suffixed with .stubs...) |
| 652 | // |
| 653 | // java_sdk_library_import is a macro that creates |
| 654 | // 1. top-level "impl" library |
| 655 | // 2. stub libraries (suffixed with .stubs...) |
| 656 | // |
| 657 | // the impl of java_sdk_library_import is a "hook" for hiddenapi and dexpreopt processing. It does not have an impl jar, but acts as a shim |
| 658 | // to provide the jar deapxed from the prebuilt apex |
| 659 | // |
| 660 | // isSelected uses `all_apex_contributions` to supersede source vs prebuilts selection of the stub libraries. It does not supersede the |
| 661 | // selection of the top-level "impl" library so that this hook can work |
| 662 | // |
| 663 | // TODO (b/308174306) - Fix this when we need to support multiple prebuilts in main |
| 664 | func isSelected(psi PrebuiltSelectionInfoMap, m Module) bool { |
| 665 | if sdkLibrary, ok := m.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil { |
| 666 | sln := proptools.String(sdkLibrary.SdkLibraryName()) |
Spandan Das | 23956d1 | 2024-01-19 00:22:22 +0000 | [diff] [blame] | 667 | |
Spandan Das | fc12d2f | 2023-10-27 20:06:32 +0000 | [diff] [blame] | 668 | // This is the top-level library |
| 669 | // Do not supersede the existing prebuilts vs source selection mechanisms |
Spandan Das | 81d95c5 | 2024-02-01 23:41:11 +0000 | [diff] [blame] | 670 | // TODO (b/308187268): Remove this after the apexes have been added to apex_contributions |
Spandan Das | 23956d1 | 2024-01-19 00:22:22 +0000 | [diff] [blame] | 671 | if bmn, ok := m.(baseModuleName); ok && sln == bmn.BaseModuleName() { |
Spandan Das | fc12d2f | 2023-10-27 20:06:32 +0000 | [diff] [blame] | 672 | return false |
| 673 | } |
| 674 | |
| 675 | // Stub library created by java_sdk_library_import |
Spandan Das | 23956d1 | 2024-01-19 00:22:22 +0000 | [diff] [blame] | 676 | // java_sdk_library creates several child modules (java_import + prebuilt_stubs_sources) dynamically. |
| 677 | // This code block ensures that these child modules are selected if the top-level java_sdk_library_import is listed |
| 678 | // in the selected apex_contributions. |
| 679 | if javaImport, ok := m.(createdByJavaSdkLibraryName); ok && javaImport.CreatedByJavaSdkLibraryName() != nil { |
| 680 | return psi.IsSelected(PrebuiltNameFromSource(proptools.String(javaImport.CreatedByJavaSdkLibraryName()))) |
Spandan Das | fc12d2f | 2023-10-27 20:06:32 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | // Stub library created by java_sdk_library |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 684 | return psi.IsSelected(sln) |
Spandan Das | fc12d2f | 2023-10-27 20:06:32 +0000 | [diff] [blame] | 685 | } |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 686 | return psi.IsSelected(m.Name()) |
Spandan Das | fc12d2f | 2023-10-27 20:06:32 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Spandan Das | 23956d1 | 2024-01-19 00:22:22 +0000 | [diff] [blame] | 689 | // implemented by child modules of java_sdk_library_import |
| 690 | type createdByJavaSdkLibraryName interface { |
| 691 | CreatedByJavaSdkLibraryName() *string |
| 692 | } |
| 693 | |
Spandan Das | 972917d | 2024-02-27 09:31:51 +0000 | [diff] [blame] | 694 | // Returns true if the prebuilt variant is disabled |
| 695 | // e.g. for a cc_prebuilt_library_shared, this will return |
| 696 | // - true for the static variant of the module |
| 697 | // - false for the shared variant of the module |
| 698 | // |
| 699 | // Even though this is a cc_prebuilt_library_shared, we create both the variants today |
| 700 | // https://source.corp.google.com/h/googleplex-android/platform/build/soong/+/e08e32b45a18a77bc3c3e751f730539b1b374f1b:cc/library.go;l=2113-2116;drc=2c4a9779cd1921d0397a12b3d3521f4c9b30d747;bpv=1;bpt=0 |
Colin Cross | b2388e3 | 2024-10-07 15:05:23 -0700 | [diff] [blame] | 701 | func (p *Prebuilt) variantIsDisabled(ctx BaseModuleContext, prebuilt Module) bool { |
Spandan Das | 972917d | 2024-02-27 09:31:51 +0000 | [diff] [blame] | 702 | return p.srcsSupplier != nil && len(p.srcsSupplier(ctx, prebuilt)) == 0 |
| 703 | } |
| 704 | |
Spandan Das | 85bd462 | 2024-08-01 00:51:20 +0000 | [diff] [blame] | 705 | type apexVariationName interface { |
| 706 | ApexVariationName() string |
| 707 | } |
| 708 | |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 709 | // usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt |
| 710 | // will be used if it is marked "prefer" or if the source module is disabled. |
Colin Cross | b2388e3 | 2024-10-07 15:05:23 -0700 | [diff] [blame] | 711 | func (p *Prebuilt) usePrebuilt(ctx BaseModuleContext, source Module, prebuilt Module) bool { |
Spandan Das | 85bd462 | 2024-08-01 00:51:20 +0000 | [diff] [blame] | 712 | isMainlinePrebuilt := func(prebuilt Module) bool { |
| 713 | apex, ok := prebuilt.(apexVariationName) |
| 714 | if !ok { |
| 715 | return false |
| 716 | } |
| 717 | // Prebuilts of aosp apexes in prebuilts/runtime |
| 718 | // Used in minimal art branches |
| 719 | if prebuilt.base().BaseModuleName() == apex.ApexVariationName() { |
| 720 | return false |
| 721 | } |
| 722 | return InList(apex.ApexVariationName(), ctx.Config().AllMainlineApexNames()) |
| 723 | } |
| 724 | |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 725 | // Use `all_apex_contributions` for source vs prebuilt selection. |
| 726 | psi := PrebuiltSelectionInfoMap{} |
Spandan Das | 85bd462 | 2024-08-01 00:51:20 +0000 | [diff] [blame] | 727 | var psiDepTag blueprint.DependencyTag |
| 728 | if p := GetEmbeddedPrebuilt(ctx.Module()); p != nil { |
| 729 | // This is a prebuilt module, visit all_apex_contributions to get the info |
| 730 | psiDepTag = AcDepTag |
| 731 | } else { |
| 732 | // This is a source module, visit any of its prebuilts to get the info |
| 733 | psiDepTag = PrebuiltDepTag |
| 734 | } |
| 735 | ctx.VisitDirectDepsWithTag(psiDepTag, func(am Module) { |
Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 736 | psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider) |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 737 | }) |
Spandan Das | fc12d2f | 2023-10-27 20:06:32 +0000 | [diff] [blame] | 738 | |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 739 | // If the source module is explicitly listed in the metadata module, use that |
Spandan Das | fc12d2f | 2023-10-27 20:06:32 +0000 | [diff] [blame] | 740 | if source != nil && isSelected(psi, source) { |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 741 | return false |
| 742 | } |
| 743 | // If the prebuilt module is explicitly listed in the metadata module, use that |
Spandan Das | 972917d | 2024-02-27 09:31:51 +0000 | [diff] [blame] | 744 | if isSelected(psi, prebuilt) && !p.variantIsDisabled(ctx, prebuilt) { |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 745 | return true |
| 746 | } |
Spandan Das | fc12d2f | 2023-10-27 20:06:32 +0000 | [diff] [blame] | 747 | |
Spandan Das | 85bd462 | 2024-08-01 00:51:20 +0000 | [diff] [blame] | 748 | // If this is a mainline prebuilt, but has not been flagged, hide it. |
| 749 | if isMainlinePrebuilt(prebuilt) { |
| 750 | return false |
| 751 | } |
| 752 | |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 753 | // If the baseModuleName could not be found in the metadata module, |
| 754 | // fall back to the existing source vs prebuilt selection. |
| 755 | // TODO: Drop the fallback mechanisms |
| 756 | |
Spandan Das | 972917d | 2024-02-27 09:31:51 +0000 | [diff] [blame] | 757 | if p.variantIsDisabled(ctx, prebuilt) { |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 758 | return false |
| 759 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 760 | |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 761 | // Skip prebuilt modules under unexported namespaces so that we won't |
| 762 | // end up shadowing non-prebuilt module when prebuilt module under same |
| 763 | // name happens to have a `Prefer` property set to true. |
| 764 | if ctx.Config().KatiEnabled() && !prebuilt.ExportedToMake() { |
| 765 | return false |
LuK1337 | fb545bf | 2021-01-10 17:47:46 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 768 | // If source is not available or is disabled then always use the prebuilt. |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 769 | if source == nil || !source.Enabled(ctx) { |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 770 | return true |
Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 773 | // TODO: use p.Properties.Name and ctx.ModuleDir to override preference |
Cole Faust | 12ff57d | 2024-07-30 13:17:28 -0700 | [diff] [blame] | 774 | return p.properties.Prefer.GetOrDefault(ctx, false) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 775 | } |
Jiyong Park | 0a573d7 | 2019-07-07 12:39:16 +0900 | [diff] [blame] | 776 | |
| 777 | func (p *Prebuilt) SourceExists() bool { |
| 778 | return p.properties.SourceExists |
| 779 | } |