| 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. | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 64 | 	Prefer *bool `android:"arch_variant"` | 
| 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. | 
 | 74 | 	Use_source_config_var *ConfigVarProperties | 
| Paul Duffin | bf4de04 | 2022-09-27 12:41:52 +0100 | [diff] [blame] | 75 | } | 
 | 76 |  | 
 | 77 | // CopyUserSuppliedPropertiesFromPrebuilt copies the user supplied prebuilt properties from the | 
 | 78 | // prebuilt properties. | 
 | 79 | func (u *UserSuppliedPrebuiltProperties) CopyUserSuppliedPropertiesFromPrebuilt(p *Prebuilt) { | 
 | 80 | 	*u = p.properties.UserSuppliedPrebuiltProperties | 
 | 81 | } | 
 | 82 |  | 
 | 83 | type PrebuiltProperties struct { | 
 | 84 | 	UserSuppliedPrebuiltProperties | 
| Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 85 |  | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 86 | 	SourceExists bool `blueprint:"mutated"` | 
 | 87 | 	UsePrebuilt  bool `blueprint:"mutated"` | 
| Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 88 |  | 
 | 89 | 	// Set if the module has been renamed to remove the "prebuilt_" prefix. | 
 | 90 | 	PrebuiltRenamedToSource bool `blueprint:"mutated"` | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 91 | } | 
 | 92 |  | 
| Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 93 | // Properties that can be used to select a Soong config variable. | 
 | 94 | type ConfigVarProperties struct { | 
 | 95 | 	// Allow instances of this struct to be used as a property value in a BpPropertySet. | 
 | 96 | 	BpPrintableBase | 
 | 97 |  | 
 | 98 | 	// The name of the configuration namespace. | 
 | 99 | 	// | 
 | 100 | 	// As passed to add_soong_config_namespace in Make. | 
 | 101 | 	Config_namespace *string | 
 | 102 |  | 
 | 103 | 	// The name of the configuration variable. | 
 | 104 | 	// | 
 | 105 | 	// As passed to add_soong_config_var_value in Make. | 
 | 106 | 	Var_name *string | 
 | 107 | } | 
 | 108 |  | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 109 | type Prebuilt struct { | 
 | 110 | 	properties PrebuiltProperties | 
| Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 111 |  | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 112 | 	// nil if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs. | 
 | 113 | 	srcsSupplier PrebuiltSrcsSupplier | 
 | 114 |  | 
 | 115 | 	// "-" if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs. | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 116 | 	srcsPropertyName string | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 117 | } | 
 | 118 |  | 
| Paul Duffin | d23c726 | 2020-12-11 18:13:08 +0000 | [diff] [blame] | 119 | // RemoveOptionalPrebuiltPrefix returns the result of removing the "prebuilt_" prefix from the | 
 | 120 | // supplied name if it has one, or returns the name unmodified if it does not. | 
 | 121 | func RemoveOptionalPrebuiltPrefix(name string) string { | 
 | 122 | 	return strings.TrimPrefix(name, "prebuilt_") | 
 | 123 | } | 
 | 124 |  | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 125 | // RemoveOptionalPrebuiltPrefixFromBazelLabel removes the "prebuilt_" prefix from the *target name* of a Bazel label. | 
 | 126 | // This differs from RemoveOptionalPrebuiltPrefix in that it does not remove it from the start of the string, but | 
 | 127 | // instead removes it from the target name itself. | 
 | 128 | func RemoveOptionalPrebuiltPrefixFromBazelLabel(label string) string { | 
 | 129 | 	splitLabel := strings.Split(label, ":") | 
 | 130 | 	bazelModuleNameNoPrebuilt := RemoveOptionalPrebuiltPrefix(splitLabel[1]) | 
 | 131 | 	return strings.Join([]string{ | 
 | 132 | 		splitLabel[0], | 
 | 133 | 		bazelModuleNameNoPrebuilt, | 
 | 134 | 	}, ":") | 
 | 135 | } | 
 | 136 |  | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 137 | func (p *Prebuilt) Name(name string) string { | 
| Paul Duffin | 864116c | 2021-04-02 10:24:13 +0100 | [diff] [blame] | 138 | 	return PrebuiltNameFromSource(name) | 
 | 139 | } | 
 | 140 |  | 
 | 141 | // PrebuiltNameFromSource returns the result of prepending the "prebuilt_" prefix to the supplied | 
 | 142 | // name. | 
 | 143 | func PrebuiltNameFromSource(name string) string { | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 144 | 	return "prebuilt_" + name | 
 | 145 | } | 
 | 146 |  | 
| Paul Duffin | 5006151 | 2020-01-21 16:31:05 +0000 | [diff] [blame] | 147 | func (p *Prebuilt) ForcePrefer() { | 
 | 148 | 	p.properties.Prefer = proptools.BoolPtr(true) | 
 | 149 | } | 
 | 150 |  | 
| Paul Duffin | 38b5785 | 2020-05-13 16:08:09 +0100 | [diff] [blame] | 151 | func (p *Prebuilt) Prefer() bool { | 
 | 152 | 	return proptools.Bool(p.properties.Prefer) | 
 | 153 | } | 
 | 154 |  | 
| Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 155 | // SingleSourcePathFromSupplier invokes the supplied supplier for the current module in the | 
 | 156 | // supplied context to retrieve a list of file paths, ensures that the returned list of file paths | 
 | 157 | // contains a single value and then assumes that is a module relative file path and converts it to | 
 | 158 | // a Path accordingly. | 
 | 159 | // | 
 | 160 | // Any issues, such as nil supplier or not exactly one file path will be reported as errors on the | 
 | 161 | // supplied context and this will return nil. | 
 | 162 | func SingleSourcePathFromSupplier(ctx ModuleContext, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) Path { | 
 | 163 | 	if srcsSupplier != nil { | 
 | 164 | 		srcs := srcsSupplier(ctx, ctx.Module()) | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 165 |  | 
 | 166 | 		if len(srcs) == 0 { | 
| Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 167 | 			ctx.PropertyErrorf(srcsPropertyName, "missing prebuilt source file") | 
| Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 168 | 			return nil | 
 | 169 | 		} | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 170 |  | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 171 | 		if len(srcs) > 1 { | 
| Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 172 | 			ctx.PropertyErrorf(srcsPropertyName, "multiple prebuilt source files") | 
| Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 173 | 			return nil | 
 | 174 | 		} | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 175 |  | 
| Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 176 | 		// Return the singleton source after expanding any filegroup in the | 
 | 177 | 		// sources. | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 178 | 		src := srcs[0] | 
| Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 179 | 		return PathForModuleSrc(ctx, src) | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 180 | 	} else { | 
 | 181 | 		ctx.ModuleErrorf("prebuilt source was not set") | 
 | 182 | 		return nil | 
| Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 183 | 	} | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 184 | } | 
 | 185 |  | 
| Paul Duffin | 56dc66e | 2021-03-01 13:18:44 +0000 | [diff] [blame] | 186 | // The below source-related functions and the srcs, src fields are based on an assumption that | 
 | 187 | // prebuilt modules have a static source property at the moment. Currently there is only one | 
 | 188 | // exception, android_app_import, which chooses a source file depending on the product's DPI | 
 | 189 | // preference configs. We'll want to add native support for dynamic source cases if we end up having | 
 | 190 | // more modules like this. | 
 | 191 | func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path { | 
 | 192 | 	return SingleSourcePathFromSupplier(ctx, p.srcsSupplier, p.srcsPropertyName) | 
 | 193 | } | 
 | 194 |  | 
| Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 195 | func (p *Prebuilt) UsePrebuilt() bool { | 
 | 196 | 	return p.properties.UsePrebuilt | 
 | 197 | } | 
 | 198 |  | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 199 | // Called to provide the srcs value for the prebuilt module. | 
 | 200 | // | 
| Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 201 | // This can be called with a context for any module not just the prebuilt one itself. It can also be | 
 | 202 | // called concurrently. | 
 | 203 | // | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 204 | // Return the src value or nil if it is not available. | 
| Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 205 | type PrebuiltSrcsSupplier func(ctx BaseModuleContext, prebuilt Module) []string | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 206 |  | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 207 | func initPrebuiltModuleCommon(module PrebuiltInterface) *Prebuilt { | 
 | 208 | 	p := module.Prebuilt() | 
 | 209 | 	module.AddProperties(&p.properties) | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 210 | 	return p | 
 | 211 | } | 
 | 212 |  | 
 | 213 | // Initialize the module as a prebuilt module that has no dedicated property that lists its | 
 | 214 | // sources. SingleSourcePathFromSupplier should not be called for this module. | 
 | 215 | // | 
 | 216 | // This is the case e.g. for header modules, which provides the headers in source form | 
 | 217 | // regardless whether they are prebuilt or not. | 
 | 218 | func InitPrebuiltModuleWithoutSrcs(module PrebuiltInterface) { | 
 | 219 | 	p := initPrebuiltModuleCommon(module) | 
 | 220 | 	p.srcsPropertyName = "-" | 
 | 221 | } | 
 | 222 |  | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 223 | // Initialize the module as a prebuilt module that uses the provided supplier to access the | 
 | 224 | // prebuilt sources of the module. | 
 | 225 | // | 
 | 226 | // The supplier will be called multiple times and must return the same values each time it | 
 | 227 | // is called. If it returns an empty array (or nil) then the prebuilt module will not be used | 
 | 228 | // as a replacement for a source module with the same name even if prefer = true. | 
 | 229 | // | 
 | 230 | // If the Prebuilt.SingleSourcePath() is called on the module then this must return an array | 
 | 231 | // containing exactly one source file. | 
 | 232 | // | 
 | 233 | // The provided property name is used to provide helpful error messages in the event that | 
 | 234 | // a problem arises, e.g. calling SingleSourcePath() when more than one source is provided. | 
 | 235 | func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) { | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 236 | 	if srcsSupplier == nil { | 
 | 237 | 		panic(fmt.Errorf("srcsSupplier must not be nil")) | 
 | 238 | 	} | 
 | 239 | 	if srcsPropertyName == "" { | 
 | 240 | 		panic(fmt.Errorf("srcsPropertyName must not be empty")) | 
 | 241 | 	} | 
 | 242 |  | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 243 | 	p := initPrebuiltModuleCommon(module) | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 244 | 	p.srcsSupplier = srcsSupplier | 
 | 245 | 	p.srcsPropertyName = srcsPropertyName | 
 | 246 | } | 
 | 247 |  | 
 | 248 | func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) { | 
 | 249 | 	if srcs == nil { | 
 | 250 | 		panic(fmt.Errorf("srcs must not be nil")) | 
 | 251 | 	} | 
 | 252 |  | 
| Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 253 | 	srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 254 | 		return *srcs | 
 | 255 | 	} | 
 | 256 |  | 
 | 257 | 	InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 258 | } | 
 | 259 |  | 
| Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 260 | func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) { | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 261 | 	srcPropsValue := reflect.ValueOf(srcProps).Elem() | 
 | 262 | 	srcStructField, _ := srcPropsValue.Type().FieldByName(srcField) | 
 | 263 | 	if !srcPropsValue.IsValid() || srcStructField.Name == "" { | 
 | 264 | 		panic(fmt.Errorf("invalid single source prebuilt %+v", module)) | 
 | 265 | 	} | 
 | 266 |  | 
 | 267 | 	if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface { | 
 | 268 | 		panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps)) | 
 | 269 | 	} | 
 | 270 |  | 
 | 271 | 	srcFieldIndex := srcStructField.Index | 
 | 272 | 	srcPropertyName := proptools.PropertyNameForField(srcField) | 
 | 273 |  | 
| Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 274 | 	srcsSupplier := func(ctx BaseModuleContext, _ Module) []string { | 
| Jaewoong Jung | 729c0bd | 2020-12-08 19:11:54 -0800 | [diff] [blame] | 275 | 		if !module.Enabled() { | 
| Jaewoong Jung | 84f1b80 | 2020-12-04 11:51:29 -0800 | [diff] [blame] | 276 | 			return nil | 
 | 277 | 		} | 
| Paul Duffin | dcb4bd6 | 2020-03-12 23:43:52 +0000 | [diff] [blame] | 278 | 		value := srcPropsValue.FieldByIndex(srcFieldIndex) | 
 | 279 | 		if value.Kind() == reflect.Ptr { | 
 | 280 | 			value = value.Elem() | 
 | 281 | 		} | 
 | 282 | 		if value.Kind() != reflect.String { | 
| Martin Stjernholm | 25a69de | 2021-09-09 02:48:30 +0100 | [diff] [blame] | 283 | 			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] | 284 | 		} | 
 | 285 | 		src := value.String() | 
 | 286 | 		if src == "" { | 
 | 287 | 			return nil | 
 | 288 | 		} | 
 | 289 | 		return []string{src} | 
 | 290 | 	} | 
 | 291 |  | 
 | 292 | 	InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName) | 
| Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 293 | } | 
 | 294 |  | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 295 | type PrebuiltInterface interface { | 
 | 296 | 	Module | 
 | 297 | 	Prebuilt() *Prebuilt | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 298 | } | 
 | 299 |  | 
| Paul Duffin | e1d3837 | 2021-04-02 10:35:24 +0100 | [diff] [blame] | 300 | // IsModulePreferred returns true if the given module is preferred. | 
 | 301 | // | 
 | 302 | // A source module is preferred if there is no corresponding prebuilt module or the prebuilt module | 
 | 303 | // does not have "prefer: true". | 
 | 304 | // | 
 | 305 | // A prebuilt module is preferred if there is no corresponding source module or the prebuilt module | 
 | 306 | // has "prefer: true". | 
 | 307 | func IsModulePreferred(module Module) bool { | 
 | 308 | 	if module.IsReplacedByPrebuilt() { | 
 | 309 | 		// A source module that has been replaced by a prebuilt counterpart. | 
 | 310 | 		return false | 
 | 311 | 	} | 
| Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 312 | 	if p := GetEmbeddedPrebuilt(module); p != nil { | 
 | 313 | 		return p.UsePrebuilt() | 
| Paul Duffin | e1d3837 | 2021-04-02 10:35:24 +0100 | [diff] [blame] | 314 | 	} | 
 | 315 | 	return true | 
 | 316 | } | 
 | 317 |  | 
| Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 318 | // IsModulePrebuilt returns true if the module implements PrebuiltInterface and | 
 | 319 | // has been initialized as a prebuilt and so returns a non-nil value from the | 
 | 320 | // PrebuiltInterface.Prebuilt() method. | 
 | 321 | func IsModulePrebuilt(module Module) bool { | 
 | 322 | 	return GetEmbeddedPrebuilt(module) != nil | 
 | 323 | } | 
 | 324 |  | 
 | 325 | // GetEmbeddedPrebuilt returns a pointer to the embedded Prebuilt structure or | 
 | 326 | // nil if the module does not implement PrebuiltInterface or has not been | 
 | 327 | // initialized as a prebuilt module. | 
 | 328 | func GetEmbeddedPrebuilt(module Module) *Prebuilt { | 
 | 329 | 	if p, ok := module.(PrebuiltInterface); ok { | 
 | 330 | 		return p.Prebuilt() | 
 | 331 | 	} | 
 | 332 |  | 
 | 333 | 	return nil | 
 | 334 | } | 
 | 335 |  | 
| Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 336 | // PrebuiltGetPreferred returns the module that is preferred for the given | 
 | 337 | // module. That is either the module itself or the prebuilt counterpart that has | 
 | 338 | // taken its place. The given module must be a direct dependency of the current | 
 | 339 | // context module, and it must be the source module if both source and prebuilt | 
 | 340 | // exist. | 
 | 341 | // | 
 | 342 | // This function is for use on dependencies after PrebuiltPostDepsMutator has | 
 | 343 | // run - any dependency that is registered before that will already reference | 
 | 344 | // the right module. This function is only safe to call after all mutators that | 
 | 345 | // may call CreateVariations, e.g. in GenerateAndroidBuildActions. | 
 | 346 | func PrebuiltGetPreferred(ctx BaseModuleContext, module Module) Module { | 
 | 347 | 	if !module.IsReplacedByPrebuilt() { | 
 | 348 | 		return module | 
 | 349 | 	} | 
 | 350 | 	if IsModulePrebuilt(module) { | 
 | 351 | 		// If we're given a prebuilt then assume there's no source module around. | 
 | 352 | 		return module | 
 | 353 | 	} | 
 | 354 |  | 
 | 355 | 	sourceModDepFound := false | 
 | 356 | 	var prebuiltMod Module | 
 | 357 |  | 
 | 358 | 	ctx.WalkDeps(func(child, parent Module) bool { | 
 | 359 | 		if prebuiltMod != nil { | 
 | 360 | 			return false | 
 | 361 | 		} | 
 | 362 | 		if parent == ctx.Module() { | 
 | 363 | 			// First level: Only recurse if the module is found as a direct dependency. | 
 | 364 | 			sourceModDepFound = child == module | 
 | 365 | 			return sourceModDepFound | 
 | 366 | 		} | 
 | 367 | 		// Second level: Follow PrebuiltDepTag to the prebuilt. | 
 | 368 | 		if t := ctx.OtherModuleDependencyTag(child); t == PrebuiltDepTag { | 
 | 369 | 			prebuiltMod = child | 
 | 370 | 		} | 
 | 371 | 		return false | 
 | 372 | 	}) | 
 | 373 |  | 
 | 374 | 	if prebuiltMod == nil { | 
 | 375 | 		if !sourceModDepFound { | 
 | 376 | 			panic(fmt.Errorf("Failed to find source module as a direct dependency: %s", module)) | 
 | 377 | 		} else { | 
 | 378 | 			panic(fmt.Errorf("Failed to find prebuilt for source module: %s", module)) | 
 | 379 | 		} | 
 | 380 | 	} | 
 | 381 | 	return prebuiltMod | 
 | 382 | } | 
 | 383 |  | 
| Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 384 | func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) { | 
| Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 385 | 	ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel() | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 386 | } | 
 | 387 |  | 
| Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 388 | func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) { | 
| Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 389 | 	ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).Parallel() | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 390 | 	ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel() | 
| Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 391 | 	ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel() | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 392 | } | 
 | 393 |  | 
| Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 394 | // PrebuiltRenameMutator ensures that there always is a module with an | 
 | 395 | // undecorated name. | 
 | 396 | func PrebuiltRenameMutator(ctx BottomUpMutatorContext) { | 
| Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 397 | 	m := ctx.Module() | 
 | 398 | 	if p := GetEmbeddedPrebuilt(m); p != nil { | 
| Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 399 | 		name := m.base().BaseModuleName() | 
 | 400 | 		if !ctx.OtherModuleExists(name) { | 
 | 401 | 			ctx.Rename(name) | 
| Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 402 | 			p.properties.PrebuiltRenamedToSource = true | 
| Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 403 | 		} | 
 | 404 | 	} | 
 | 405 | } | 
 | 406 |  | 
 | 407 | // PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the | 
 | 408 | // corresponding source module, if one exists for the same variant. | 
 | 409 | func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) { | 
| Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 410 | 	m := ctx.Module() | 
 | 411 | 	// If this module is a prebuilt, is enabled and has not been renamed to source then add a | 
 | 412 | 	// dependency onto the source if it is present. | 
 | 413 | 	if p := GetEmbeddedPrebuilt(m); p != nil && m.Enabled() && !p.properties.PrebuiltRenamedToSource { | 
 | 414 | 		name := m.base().BaseModuleName() | 
 | 415 | 		if ctx.OtherModuleReverseDependencyVariantExists(name) { | 
 | 416 | 			ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name) | 
 | 417 | 			p.properties.SourceExists = true | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 418 | 		} | 
 | 419 | 	} | 
 | 420 | } | 
 | 421 |  | 
| Jooyung Han | ebaa573 | 2023-05-02 11:43:14 +0900 | [diff] [blame] | 422 | // checkInvariantsForSourceAndPrebuilt checks if invariants are kept when replacing | 
 | 423 | // source with prebuilt. Note that the current module for the context is the source module. | 
 | 424 | func checkInvariantsForSourceAndPrebuilt(ctx BaseModuleContext, s, p Module) { | 
 | 425 | 	if _, ok := s.(OverrideModule); ok { | 
 | 426 | 		// skip the check when the source module is `override_X` because it's only a placeholder | 
 | 427 | 		// for the actual source module. The check will be invoked for the actual module. | 
 | 428 | 		return | 
 | 429 | 	} | 
 | 430 | 	if sourcePartition, prebuiltPartition := s.PartitionTag(ctx.DeviceConfig()), p.PartitionTag(ctx.DeviceConfig()); sourcePartition != prebuiltPartition { | 
 | 431 | 		ctx.OtherModuleErrorf(p, "partition is different: %s(%s) != %s(%s)", | 
 | 432 | 			sourcePartition, ctx.ModuleName(), prebuiltPartition, ctx.OtherModuleName(p)) | 
 | 433 | 	} | 
 | 434 | } | 
 | 435 |  | 
| Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 436 | // PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or | 
 | 437 | // 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] | 438 | func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) { | 
| Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 439 | 	m := ctx.Module() | 
 | 440 | 	if p := GetEmbeddedPrebuilt(m); p != nil { | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 441 | 		if p.srcsSupplier == nil && p.srcsPropertyName == "" { | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 442 | 			panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it")) | 
 | 443 | 		} | 
 | 444 | 		if !p.properties.SourceExists { | 
| Paul Duffin | c04fb9e | 2021-03-01 12:25:10 +0000 | [diff] [blame] | 445 | 			p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil, m) | 
| Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 446 | 		} | 
 | 447 | 	} else if s, ok := ctx.Module().(Module); ok { | 
| Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 448 | 		ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(prebuiltModule Module) { | 
 | 449 | 			p := GetEmbeddedPrebuilt(prebuiltModule) | 
 | 450 | 			if p.usePrebuilt(ctx, s, prebuiltModule) { | 
| Jooyung Han | ebaa573 | 2023-05-02 11:43:14 +0900 | [diff] [blame] | 451 | 				checkInvariantsForSourceAndPrebuilt(ctx, s, prebuiltModule) | 
 | 452 |  | 
| Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 453 | 				p.properties.UsePrebuilt = true | 
| Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 454 | 				s.ReplacedByPrebuilt() | 
| Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 455 | 			} | 
 | 456 | 		}) | 
 | 457 | 	} | 
 | 458 | } | 
 | 459 |  | 
| Jaewoong Jung | 6158dfe | 2021-03-23 14:08:29 -0700 | [diff] [blame] | 460 | // PrebuiltPostDepsMutator replaces dependencies on the source module with dependencies on the | 
 | 461 | // prebuilt when both modules exist and the prebuilt should be used.  When the prebuilt should not | 
 | 462 | // be used, disable installing it. | 
| Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 463 | func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) { | 
| Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 464 | 	m := ctx.Module() | 
 | 465 | 	if p := GetEmbeddedPrebuilt(m); p != nil { | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 466 | 		name := m.base().BaseModuleName() | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 467 | 		if p.properties.UsePrebuilt { | 
 | 468 | 			if p.properties.SourceExists { | 
| Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 469 | 				ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool { | 
 | 470 | 					if t, ok := tag.(ReplaceSourceWithPrebuilt); ok { | 
 | 471 | 						return t.ReplaceSourceWithPrebuilt() | 
 | 472 | 					} | 
 | 473 |  | 
 | 474 | 					return true | 
 | 475 | 				}) | 
| Colin Cross | 0f3c72f | 2016-11-23 15:44:07 -0800 | [diff] [blame] | 476 | 			} | 
 | 477 | 		} else { | 
| Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 478 | 			m.HideFromMake() | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 479 | 		} | 
 | 480 | 	} | 
 | 481 | } | 
 | 482 |  | 
| Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 483 | // usePrebuilt returns true if a prebuilt should be used instead of the source module.  The prebuilt | 
 | 484 | // 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] | 485 | func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module, prebuilt Module) bool { | 
| Liz Kammer | e1b39a5 | 2023-09-18 14:32:18 -0400 | [diff] [blame] | 486 | 	if !ctx.Config().Bp2buildMode() { | 
 | 487 | 		if p.srcsSupplier != nil && len(p.srcsSupplier(ctx, prebuilt)) == 0 { | 
 | 488 | 			return false | 
 | 489 | 		} | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 490 |  | 
| Liz Kammer | e1b39a5 | 2023-09-18 14:32:18 -0400 | [diff] [blame] | 491 | 		// Skip prebuilt modules under unexported namespaces so that we won't | 
 | 492 | 		// end up shadowing non-prebuilt module when prebuilt module under same | 
 | 493 | 		// name happens to have a `Prefer` property set to true. | 
 | 494 | 		if ctx.Config().KatiEnabled() && !prebuilt.ExportedToMake() { | 
 | 495 | 			return false | 
 | 496 | 		} | 
| LuK1337 | fb545bf | 2021-01-10 17:47:46 +0100 | [diff] [blame] | 497 | 	} | 
 | 498 |  | 
| Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 499 | 	// If source is not available or is disabled then always use the prebuilt. | 
 | 500 | 	if source == nil || !source.Enabled() { | 
| Liz Kammer | e1b39a5 | 2023-09-18 14:32:18 -0400 | [diff] [blame] | 501 | 		// If in bp2build mode, we need to check product variables & Soong config variables, which may | 
 | 502 | 		// have overridden the "enabled" property but have not been merged into the property value as | 
 | 503 | 		// they would in a non-bp2build mode invocation | 
 | 504 | 		if ctx.Config().Bp2buildMode() && source != nil { | 
 | 505 | 			productVariableProps, errs := ProductVariableProperties(ctx, source) | 
 | 506 | 			if productConfigProps, exists := productVariableProps["Enabled"]; len(errs) == 0 && exists && len(productConfigProps) == 1 { | 
 | 507 | 				var prop ProductConfigOrSoongConfigProperty | 
 | 508 | 				var value bool | 
 | 509 | 				for p, v := range productConfigProps { | 
 | 510 | 					prop = p | 
 | 511 | 					actual, ok := v.(*bool) | 
 | 512 | 					if ok { | 
 | 513 | 						value = proptools.Bool(actual) | 
 | 514 | 					} | 
 | 515 | 				} | 
 | 516 | 				if scv, ok := prop.(SoongConfigProperty); ok { | 
 | 517 | 					// If the product config var is enabled but the value of enabled is false still, the | 
 | 518 | 					// prebuilt is preferred. Otherwise, check if the prebulit is explicitly preferred | 
 | 519 | 					if ctx.Config().VendorConfig(scv.namespace).Bool(strings.ToLower(scv.name)) && !value { | 
 | 520 | 						return true | 
 | 521 | 					} | 
 | 522 | 				} else { | 
 | 523 | 					// TODO: b/300998219 - handle product vars | 
 | 524 | 					// We don't handle product variables yet, so return based on the non-product specific | 
 | 525 | 					// value of enabled | 
 | 526 | 					return true | 
 | 527 | 				} | 
 | 528 | 			} else { | 
 | 529 | 				// No "enabled" property override, return true since this module isn't enabled | 
 | 530 | 				return true | 
 | 531 | 			} | 
 | 532 | 		} else { | 
 | 533 | 			return true | 
 | 534 | 		} | 
| Colin Cross | a2f296f | 2016-11-29 15:16:18 -0800 | [diff] [blame] | 535 | 	} | 
 | 536 |  | 
| Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 537 | 	// If the use_source_config_var property is set then it overrides the prefer property setting. | 
 | 538 | 	if configVar := p.properties.Use_source_config_var; configVar != nil { | 
 | 539 | 		return !ctx.Config().VendorConfig(proptools.String(configVar.Config_namespace)).Bool(proptools.String(configVar.Var_name)) | 
 | 540 | 	} | 
 | 541 |  | 
 | 542 | 	// TODO: use p.Properties.Name and ctx.ModuleDir to override preference | 
 | 543 | 	return Bool(p.properties.Prefer) | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 544 | } | 
| Jiyong Park | 0a573d7 | 2019-07-07 12:39:16 +0900 | [diff] [blame] | 545 |  | 
 | 546 | func (p *Prebuilt) SourceExists() bool { | 
 | 547 | 	return p.properties.SourceExists | 
 | 548 | } |