blob: defec11863f7ab290ae3f7e1c68691b657ff0e75 [file] [log] [blame]
Colin Crossce75d2c2016-10-06 16:12:58 -07001// 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
15package android
16
Colin Cross74d73e22017-08-02 11:05:49 -070017import (
18 "fmt"
Jaewoong Jung3e18b192019-06-11 12:25:34 -070019 "reflect"
Paul Duffind23c7262020-12-11 18:13:08 +000020 "strings"
Colin Cross74d73e22017-08-02 11:05:49 -070021
22 "github.com/google/blueprint"
Jaewoong Jung939ebd52019-03-26 15:07:36 -070023 "github.com/google/blueprint/proptools"
Colin Cross74d73e22017-08-02 11:05:49 -070024)
Colin Crossce75d2c2016-10-06 16:12:58 -070025
26// This file implements common functionality for handling modules that may exist as prebuilts,
27// source, or both.
28
Paul Duffin0c4979b2019-12-19 15:11:53 +000029func RegisterPrebuiltMutators(ctx RegistrationContext) {
30 ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators)
Colin Crossd8d8b852024-12-20 16:32:37 -080031 ctx.PreDepsMutators(RegisterPrebuiltsPreDepsMutators)
Paul Duffin0c4979b2019-12-19 15:11:53 +000032 ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators)
33}
34
Paul Duffin80342d72020-06-26 22:08:43 +010035// Marks a dependency tag as possibly preventing a reference to a source from being
36// replaced with the prebuilt.
37type ReplaceSourceWithPrebuilt interface {
38 blueprint.DependencyTag
39
40 // Return true if the dependency defined by this tag should be replaced with the
41 // prebuilt.
42 ReplaceSourceWithPrebuilt() bool
43}
44
Nan Zhang2502e122017-03-09 18:43:01 -080045type prebuiltDependencyTag struct {
46 blueprint.BaseDependencyTag
47}
48
Jiyong Park03b68dd2019-07-26 23:20:40 +090049var PrebuiltDepTag prebuiltDependencyTag
Colin Crossce75d2c2016-10-06 16:12:58 -070050
Paul Duffin78ac5b92020-01-14 12:42:08 +000051// Mark this tag so dependencies that use it are excluded from visibility enforcement.
52func (t prebuiltDependencyTag) ExcludeFromVisibilityEnforcement() {}
53
Paul Duffindddd5462020-04-07 15:25:44 +010054// Mark this tag so dependencies that use it are excluded from APEX contents.
55func (t prebuiltDependencyTag) ExcludeFromApexContents() {}
56
57var _ ExcludeFromVisibilityEnforcementTag = PrebuiltDepTag
58var _ ExcludeFromApexContentsTag = PrebuiltDepTag
59
Paul Duffinbf4de042022-09-27 12:41:52 +010060// UserSuppliedPrebuiltProperties contains the prebuilt properties that can be specified in an
61// Android.bp file.
62type UserSuppliedPrebuiltProperties struct {
Colin Cross74d73e22017-08-02 11:05:49 -070063 // When prefer is set to true the prebuilt will be used instead of any source module with
64 // a matching name.
Cole Faust12ff57d2024-07-30 13:17:28 -070065 Prefer proptools.Configurable[bool] `android:"arch_variant,replace_instead_of_append"`
Colin Crossce75d2c2016-10-06 16:12:58 -070066
Paul Duffin0c52c7b2021-07-06 17:15:25 +010067 // When specified this names a Soong config variable that controls the prefer property.
68 //
69 // If the value of the named Soong config variable is true then prefer is set to false and vice
70 // versa. If the Soong config variable is not set then it defaults to false, so prefer defaults
71 // to true.
72 //
73 // If specified then the prefer property is ignored in favor of the value of the Soong config
74 // variable.
Spandan Dasa9cf0c82024-04-01 22:28:59 +000075 //
76 // DEPRECATED: This property is being deprecated b/308188211.
77 // Use RELEASE_APEX_CONTRIBUTIONS build flags to select prebuilts of mainline modules.
Paul Duffin0c52c7b2021-07-06 17:15:25 +010078 Use_source_config_var *ConfigVarProperties
Paul Duffinbf4de042022-09-27 12:41:52 +010079}
80
81// CopyUserSuppliedPropertiesFromPrebuilt copies the user supplied prebuilt properties from the
82// prebuilt properties.
83func (u *UserSuppliedPrebuiltProperties) CopyUserSuppliedPropertiesFromPrebuilt(p *Prebuilt) {
84 *u = p.properties.UserSuppliedPrebuiltProperties
85}
86
87type PrebuiltProperties struct {
88 UserSuppliedPrebuiltProperties
Paul Duffin0c52c7b2021-07-06 17:15:25 +010089
Colin Cross74d73e22017-08-02 11:05:49 -070090 SourceExists bool `blueprint:"mutated"`
91 UsePrebuilt bool `blueprint:"mutated"`
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000092
93 // Set if the module has been renamed to remove the "prebuilt_" prefix.
94 PrebuiltRenamedToSource bool `blueprint:"mutated"`
Colin Cross74d73e22017-08-02 11:05:49 -070095}
96
Paul Duffin0c52c7b2021-07-06 17:15:25 +010097// Properties that can be used to select a Soong config variable.
98type ConfigVarProperties struct {
99 // Allow instances of this struct to be used as a property value in a BpPropertySet.
100 BpPrintableBase
101
102 // The name of the configuration namespace.
103 //
104 // As passed to add_soong_config_namespace in Make.
105 Config_namespace *string
106
107 // The name of the configuration variable.
108 //
109 // As passed to add_soong_config_var_value in Make.
110 Var_name *string
111}
112
Colin Cross74d73e22017-08-02 11:05:49 -0700113type Prebuilt struct {
114 properties PrebuiltProperties
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700115
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000116 // nil if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs.
117 srcsSupplier PrebuiltSrcsSupplier
118
119 // "-" if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000120 srcsPropertyName string
Colin Crossce75d2c2016-10-06 16:12:58 -0700121}
122
Paul Duffind23c7262020-12-11 18:13:08 +0000123// RemoveOptionalPrebuiltPrefix returns the result of removing the "prebuilt_" prefix from the
124// supplied name if it has one, or returns the name unmodified if it does not.
125func RemoveOptionalPrebuiltPrefix(name string) string {
126 return strings.TrimPrefix(name, "prebuilt_")
127}
128
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000129// RemoveOptionalPrebuiltPrefixFromBazelLabel removes the "prebuilt_" prefix from the *target name* of a Bazel label.
130// This differs from RemoveOptionalPrebuiltPrefix in that it does not remove it from the start of the string, but
131// instead removes it from the target name itself.
132func RemoveOptionalPrebuiltPrefixFromBazelLabel(label string) string {
133 splitLabel := strings.Split(label, ":")
134 bazelModuleNameNoPrebuilt := RemoveOptionalPrebuiltPrefix(splitLabel[1])
135 return strings.Join([]string{
136 splitLabel[0],
137 bazelModuleNameNoPrebuilt,
138 }, ":")
139}
140
Colin Crossce75d2c2016-10-06 16:12:58 -0700141func (p *Prebuilt) Name(name string) string {
Paul Duffin864116c2021-04-02 10:24:13 +0100142 return PrebuiltNameFromSource(name)
143}
144
145// PrebuiltNameFromSource returns the result of prepending the "prebuilt_" prefix to the supplied
146// name.
147func PrebuiltNameFromSource(name string) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700148 return "prebuilt_" + name
149}
150
Paul Duffin50061512020-01-21 16:31:05 +0000151func (p *Prebuilt) ForcePrefer() {
Cole Faust12ff57d2024-07-30 13:17:28 -0700152 p.properties.Prefer = NewSimpleConfigurable(true)
Paul Duffin38b57852020-05-13 16:08:09 +0100153}
154
Paul Duffin56dc66e2021-03-01 13:18:44 +0000155// 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.
162func SingleSourcePathFromSupplier(ctx ModuleContext, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) Path {
163 if srcsSupplier != nil {
164 srcs := srcsSupplier(ctx, ctx.Module())
Paul Duffindcb4bd62020-03-12 23:43:52 +0000165
166 if len(srcs) == 0 {
Paul Duffin56dc66e2021-03-01 13:18:44 +0000167 ctx.PropertyErrorf(srcsPropertyName, "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700168 return nil
169 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700170
Paul Duffindcb4bd62020-03-12 23:43:52 +0000171 if len(srcs) > 1 {
Paul Duffin56dc66e2021-03-01 13:18:44 +0000172 ctx.PropertyErrorf(srcsPropertyName, "multiple prebuilt source files")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700173 return nil
174 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700175
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700176 // Return the singleton source after expanding any filegroup in the
177 // sources.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000178 src := srcs[0]
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700179 return PathForModuleSrc(ctx, src)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000180 } else {
181 ctx.ModuleErrorf("prebuilt source was not set")
182 return nil
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700183 }
Colin Cross74d73e22017-08-02 11:05:49 -0700184}
185
Paul Duffin56dc66e2021-03-01 13:18:44 +0000186// 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.
191func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
192 return SingleSourcePathFromSupplier(ctx, p.srcsSupplier, p.srcsPropertyName)
193}
194
Jiyong Park4d277042019-04-23 18:00:10 +0900195func (p *Prebuilt) UsePrebuilt() bool {
196 return p.properties.UsePrebuilt
197}
198
Colin Crossd8d8b852024-12-20 16:32:37 -0800199func (p *Prebuilt) SetUsePrebuilt(use bool) {
200 p.properties.UsePrebuilt = use
201}
202
Paul Duffindcb4bd62020-03-12 23:43:52 +0000203// Called to provide the srcs value for the prebuilt module.
204//
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000205// This can be called with a context for any module not just the prebuilt one itself. It can also be
206// called concurrently.
207//
Paul Duffindcb4bd62020-03-12 23:43:52 +0000208// Return the src value or nil if it is not available.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000209type PrebuiltSrcsSupplier func(ctx BaseModuleContext, prebuilt Module) []string
Paul Duffindcb4bd62020-03-12 23:43:52 +0000210
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000211func initPrebuiltModuleCommon(module PrebuiltInterface) *Prebuilt {
212 p := module.Prebuilt()
213 module.AddProperties(&p.properties)
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000214 return p
215}
216
217// Initialize the module as a prebuilt module that has no dedicated property that lists its
218// sources. SingleSourcePathFromSupplier should not be called for this module.
219//
220// This is the case e.g. for header modules, which provides the headers in source form
221// regardless whether they are prebuilt or not.
222func InitPrebuiltModuleWithoutSrcs(module PrebuiltInterface) {
223 p := initPrebuiltModuleCommon(module)
224 p.srcsPropertyName = "-"
225}
226
Paul Duffindcb4bd62020-03-12 23:43:52 +0000227// Initialize the module as a prebuilt module that uses the provided supplier to access the
228// prebuilt sources of the module.
229//
230// The supplier will be called multiple times and must return the same values each time it
231// is called. If it returns an empty array (or nil) then the prebuilt module will not be used
232// as a replacement for a source module with the same name even if prefer = true.
233//
234// If the Prebuilt.SingleSourcePath() is called on the module then this must return an array
235// containing exactly one source file.
236//
237// The provided property name is used to provide helpful error messages in the event that
238// a problem arises, e.g. calling SingleSourcePath() when more than one source is provided.
239func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000240 if srcsSupplier == nil {
241 panic(fmt.Errorf("srcsSupplier must not be nil"))
242 }
243 if srcsPropertyName == "" {
244 panic(fmt.Errorf("srcsPropertyName must not be empty"))
245 }
246
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000247 p := initPrebuiltModuleCommon(module)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000248 p.srcsSupplier = srcsSupplier
249 p.srcsPropertyName = srcsPropertyName
250}
251
Cole Faust96a692b2024-08-08 14:47:51 -0700252// InitPrebuiltModule is the same as InitPrebuiltModuleWithSrcSupplier, but uses the
253// provided list of strings property as the source provider.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000254func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
255 if srcs == nil {
256 panic(fmt.Errorf("srcs must not be nil"))
257 }
258
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000259 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000260 return *srcs
261 }
262
263 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Colin Crossce75d2c2016-10-06 16:12:58 -0700264}
265
Cole Faust96a692b2024-08-08 14:47:51 -0700266// InitConfigurablePrebuiltModule is the same as InitPrebuiltModule, but uses a
267// Configurable list of strings property instead of a regular list of strings.
268func InitConfigurablePrebuiltModule(module PrebuiltInterface, srcs *proptools.Configurable[[]string]) {
269 if srcs == nil {
270 panic(fmt.Errorf("srcs must not be nil"))
271 }
272
273 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
274 return srcs.GetOrDefault(ctx, nil)
275 }
276
277 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
278}
279
Cole Faustc71b1752024-10-14 12:14:54 -0700280// InitConfigurablePrebuiltModuleString is the same as InitPrebuiltModule, but uses a
281// Configurable string property instead of a regular list of strings. It only produces a single
282// source file.
283func InitConfigurablePrebuiltModuleString(module PrebuiltInterface, srcs *proptools.Configurable[string], propertyName string) {
284 if srcs == nil {
285 panic(fmt.Errorf("%s must not be nil", propertyName))
286 }
287
288 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
289 src := srcs.GetOrDefault(ctx, "")
290 if src == "" {
291 return nil
292 }
293 return []string{src}
294 }
295
296 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, propertyName)
297}
298
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700299func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000300 srcPropsValue := reflect.ValueOf(srcProps).Elem()
301 srcStructField, _ := srcPropsValue.Type().FieldByName(srcField)
302 if !srcPropsValue.IsValid() || srcStructField.Name == "" {
303 panic(fmt.Errorf("invalid single source prebuilt %+v", module))
304 }
305
306 if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface {
307 panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps))
308 }
309
310 srcFieldIndex := srcStructField.Index
311 srcPropertyName := proptools.PropertyNameForField(srcField)
312
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000313 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
Cole Fausta963b942024-04-11 17:43:00 -0700314 if !module.Enabled(ctx) {
Jaewoong Jung84f1b802020-12-04 11:51:29 -0800315 return nil
316 }
Paul Duffindcb4bd62020-03-12 23:43:52 +0000317 value := srcPropsValue.FieldByIndex(srcFieldIndex)
318 if value.Kind() == reflect.Ptr {
Cole Faust97494b12024-01-12 14:02:47 -0800319 if value.IsNil() {
320 return nil
321 }
Paul Duffindcb4bd62020-03-12 23:43:52 +0000322 value = value.Elem()
323 }
324 if value.Kind() != reflect.String {
Martin Stjernholm25a69de2021-09-09 02:48:30 +0100325 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 Duffindcb4bd62020-03-12 23:43:52 +0000326 }
327 src := value.String()
328 if src == "" {
329 return nil
330 }
331 return []string{src}
332 }
333
334 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700335}
336
Colin Crossce75d2c2016-10-06 16:12:58 -0700337type PrebuiltInterface interface {
338 Module
339 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700340}
341
Paul Duffine1d38372021-04-02 10:35:24 +0100342// IsModulePreferred returns true if the given module is preferred.
343//
344// A source module is preferred if there is no corresponding prebuilt module or the prebuilt module
345// does not have "prefer: true".
346//
347// A prebuilt module is preferred if there is no corresponding source module or the prebuilt module
348// has "prefer: true".
349func IsModulePreferred(module Module) bool {
350 if module.IsReplacedByPrebuilt() {
351 // A source module that has been replaced by a prebuilt counterpart.
352 return false
353 }
Paul Duffinf7c99f52021-04-28 10:41:21 +0100354 if p := GetEmbeddedPrebuilt(module); p != nil {
355 return p.UsePrebuilt()
Paul Duffine1d38372021-04-02 10:35:24 +0100356 }
357 return true
358}
359
Paul Duffinf7c99f52021-04-28 10:41:21 +0100360// IsModulePrebuilt returns true if the module implements PrebuiltInterface and
361// has been initialized as a prebuilt and so returns a non-nil value from the
362// PrebuiltInterface.Prebuilt() method.
363func IsModulePrebuilt(module Module) bool {
364 return GetEmbeddedPrebuilt(module) != nil
365}
366
367// GetEmbeddedPrebuilt returns a pointer to the embedded Prebuilt structure or
368// nil if the module does not implement PrebuiltInterface or has not been
369// initialized as a prebuilt module.
370func GetEmbeddedPrebuilt(module Module) *Prebuilt {
371 if p, ok := module.(PrebuiltInterface); ok {
372 return p.Prebuilt()
373 }
374
375 return nil
376}
377
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000378// PrebuiltGetPreferred returns the module that is preferred for the given
379// module. That is either the module itself or the prebuilt counterpart that has
380// taken its place. The given module must be a direct dependency of the current
381// context module, and it must be the source module if both source and prebuilt
382// exist.
383//
384// This function is for use on dependencies after PrebuiltPostDepsMutator has
385// run - any dependency that is registered before that will already reference
Colin Cross1e954b62024-09-13 13:50:00 -0700386// the right module. This function is only safe to call after all TransitionMutators
387// have run, e.g. in GenerateAndroidBuildActions.
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000388func PrebuiltGetPreferred(ctx BaseModuleContext, module Module) Module {
Yu Liub5275322024-11-13 18:40:43 +0000389 if !OtherModuleProviderOrDefault(ctx, module, CommonModuleInfoKey).ReplacedByPrebuilt {
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000390 return module
391 }
Yu Liu8a8d5b42025-01-07 00:48:08 +0000392 if _, ok := OtherModuleProvider(ctx, module, PrebuiltModuleInfoProvider); ok {
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000393 // If we're given a prebuilt then assume there's no source module around.
394 return module
395 }
396
397 sourceModDepFound := false
398 var prebuiltMod Module
399
Yu Liud2a95952024-10-10 00:15:26 +0000400 ctx.WalkDepsProxy(func(child, parent ModuleProxy) bool {
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000401 if prebuiltMod != nil {
402 return false
403 }
Yu Liud2a95952024-10-10 00:15:26 +0000404 if ctx.EqualModules(parent, ctx.Module()) {
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000405 // First level: Only recurse if the module is found as a direct dependency.
406 sourceModDepFound = child == module
407 return sourceModDepFound
408 }
409 // Second level: Follow PrebuiltDepTag to the prebuilt.
410 if t := ctx.OtherModuleDependencyTag(child); t == PrebuiltDepTag {
411 prebuiltMod = child
412 }
413 return false
414 })
415
416 if prebuiltMod == nil {
417 if !sourceModDepFound {
418 panic(fmt.Errorf("Failed to find source module as a direct dependency: %s", module))
419 } else {
420 panic(fmt.Errorf("Failed to find prebuilt for source module: %s", module))
421 }
422 }
423 return prebuiltMod
424}
425
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700426func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -0700427 ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).UsesRename()
Colin Crosscec81712017-07-13 14:43:27 -0700428}
429
Colin Crossd8d8b852024-12-20 16:32:37 -0800430func RegisterPrebuiltsPreDepsMutators(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -0700431 ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).UsesReverseDependencies()
432 ctx.BottomUp("prebuilt_select", PrebuiltSelectModuleMutator)
Colin Crossd8d8b852024-12-20 16:32:37 -0800433}
434
435func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -0700436 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).UsesReplaceDependencies()
Colin Crosscec81712017-07-13 14:43:27 -0700437}
438
Spandan Das3576e762024-01-03 18:57:03 +0000439// Returns the name of the source module corresponding to a prebuilt module
440// For source modules, it returns its own name
441type baseModuleName interface {
442 BaseModuleName() string
443}
444
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000445// PrebuiltRenameMutator ensures that there always is a module with an
446// undecorated name.
447func PrebuiltRenameMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100448 m := ctx.Module()
449 if p := GetEmbeddedPrebuilt(m); p != nil {
Spandan Das3576e762024-01-03 18:57:03 +0000450 bmn, _ := m.(baseModuleName)
451 name := bmn.BaseModuleName()
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000452 if !ctx.OtherModuleExists(name) {
453 ctx.Rename(name)
Paul Duffinf7c99f52021-04-28 10:41:21 +0100454 p.properties.PrebuiltRenamedToSource = true
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000455 }
456 }
457}
458
459// PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the
460// corresponding source module, if one exists for the same variant.
Spandan Das1c4d94d2023-10-26 22:38:34 +0000461// Add a dependency from the prebuilt to `all_apex_contributions`
462// The metadata will be used for source vs prebuilts selection
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000463func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100464 m := ctx.Module()
Spandan Das85bd4622024-08-01 00:51:20 +0000465 if p := GetEmbeddedPrebuilt(m); p != nil {
Spandan Das1c4d94d2023-10-26 22:38:34 +0000466 // Add a dependency from the prebuilt to the `all_apex_contributions`
467 // metadata module
468 // TODO: When all branches contain this singleton module, make this strict
469 // TODO: Add this dependency only for mainline prebuilts and not every prebuilt module
470 if ctx.OtherModuleExists("all_apex_contributions") {
Jihoon Kanga3a05462024-04-05 00:36:44 +0000471 ctx.AddDependency(m, AcDepTag, "all_apex_contributions")
Spandan Das1c4d94d2023-10-26 22:38:34 +0000472 }
Spandan Das85bd4622024-08-01 00:51:20 +0000473 if m.Enabled(ctx) && !p.properties.PrebuiltRenamedToSource {
474 // If this module is a prebuilt, is enabled and has not been renamed to source then add a
475 // dependency onto the source if it is present.
476 bmn, _ := m.(baseModuleName)
477 name := bmn.BaseModuleName()
478 if ctx.OtherModuleReverseDependencyVariantExists(name) {
Colin Crossd8d8b852024-12-20 16:32:37 -0800479 ctx.AddReverseVariationDependency(nil, PrebuiltDepTag, name)
Spandan Das85bd4622024-08-01 00:51:20 +0000480 p.properties.SourceExists = true
481 }
482 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700483 }
484}
485
Jooyung Hanebaa5732023-05-02 11:43:14 +0900486// checkInvariantsForSourceAndPrebuilt checks if invariants are kept when replacing
487// source with prebuilt. Note that the current module for the context is the source module.
488func checkInvariantsForSourceAndPrebuilt(ctx BaseModuleContext, s, p Module) {
489 if _, ok := s.(OverrideModule); ok {
490 // skip the check when the source module is `override_X` because it's only a placeholder
491 // for the actual source module. The check will be invoked for the actual module.
492 return
493 }
494 if sourcePartition, prebuiltPartition := s.PartitionTag(ctx.DeviceConfig()), p.PartitionTag(ctx.DeviceConfig()); sourcePartition != prebuiltPartition {
495 ctx.OtherModuleErrorf(p, "partition is different: %s(%s) != %s(%s)",
496 sourcePartition, ctx.ModuleName(), prebuiltPartition, ctx.OtherModuleName(p))
497 }
498}
499
Colin Crossc3e7fa62017-03-17 13:14:32 -0700500// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
501// because the source module doesn't exist. It also disables installing overridden source modules.
Spandan Dase3fcb412023-10-26 20:48:02 +0000502//
503// If the visited module is the metadata module `all_apex_contributions`, it sets a
504// provider containing metadata about whether source or prebuilt of mainline modules should be used.
505// This logic was added here to prevent the overhead of creating a new mutator.
Spandan Das1c4d94d2023-10-26 22:38:34 +0000506func PrebuiltSelectModuleMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100507 m := ctx.Module()
508 if p := GetEmbeddedPrebuilt(m); p != nil {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000509 if p.srcsSupplier == nil && p.srcsPropertyName == "" {
Colin Cross74d73e22017-08-02 11:05:49 -0700510 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
511 }
512 if !p.properties.SourceExists {
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000513 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil, m)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700514 }
Spandan Das1c4d94d2023-10-26 22:38:34 +0000515 // Propagate the provider received from `all_apex_contributions`
516 // to the source module
Jihoon Kanga3a05462024-04-05 00:36:44 +0000517 ctx.VisitDirectDepsWithTag(AcDepTag, func(am Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800518 psi, _ := OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider)
519 SetProvider(ctx, PrebuiltSelectionInfoProvider, psi)
Spandan Das1c4d94d2023-10-26 22:38:34 +0000520 })
521
Colin Crossc3e7fa62017-03-17 13:14:32 -0700522 } else if s, ok := ctx.Module().(Module); ok {
Spandan Das3576e762024-01-03 18:57:03 +0000523 // Use `all_apex_contributions` for source vs prebuilt selection.
524 psi := PrebuiltSelectionInfoMap{}
525 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(am Module) {
526 // The value of psi gets overwritten with the provider from the last visited prebuilt.
527 // But all prebuilts have the same value of the provider, so this should be idempontent.
528 psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider)
529 })
Paul Duffinf7c99f52021-04-28 10:41:21 +0100530 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(prebuiltModule Module) {
531 p := GetEmbeddedPrebuilt(prebuiltModule)
532 if p.usePrebuilt(ctx, s, prebuiltModule) {
Jooyung Hanebaa5732023-05-02 11:43:14 +0900533 checkInvariantsForSourceAndPrebuilt(ctx, s, prebuiltModule)
534
Colin Crossee6143c2017-12-30 17:54:27 -0800535 p.properties.UsePrebuilt = true
Liz Kammer5ca3a622020-08-05 15:40:41 -0700536 s.ReplacedByPrebuilt()
Colin Crossa2f296f2016-11-29 15:16:18 -0800537 }
538 })
Spandan Das3576e762024-01-03 18:57:03 +0000539
540 // If any module in this mainline module family has been flagged using apex_contributions, disable every other module in that family
541 // Add source
542 allModules := []Module{s}
543 // Add each prebuilt
544 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(prebuiltModule Module) {
545 allModules = append(allModules, prebuiltModule)
546 })
547 hideUnflaggedModules(ctx, psi, allModules)
548
Colin Crossa2f296f2016-11-29 15:16:18 -0800549 }
Spandan Das3576e762024-01-03 18:57:03 +0000550
Spandan Dase3fcb412023-10-26 20:48:02 +0000551 // If this is `all_apex_contributions`, set a provider containing
552 // metadata about source vs prebuilts selection
553 if am, ok := m.(*allApexContributions); ok {
554 am.SetPrebuiltSelectionInfoProvider(ctx)
555 }
Colin Crossa2f296f2016-11-29 15:16:18 -0800556}
557
Spandan Das3576e762024-01-03 18:57:03 +0000558// If any module in this mainline module family has been flagged using apex_contributions, disable every other module in that family
559func hideUnflaggedModules(ctx BottomUpMutatorContext, psi PrebuiltSelectionInfoMap, allModulesInFamily []Module) {
560 var selectedModuleInFamily Module
561 // query all_apex_contributions to see if any module in this family has been selected
562 for _, moduleInFamily := range allModulesInFamily {
563 // validate that are no duplicates
Spandan Das23956d12024-01-19 00:22:22 +0000564 if isSelected(psi, moduleInFamily) {
Spandan Das3576e762024-01-03 18:57:03 +0000565 if selectedModuleInFamily == nil {
566 // Store this so we can validate that there are no duplicates
567 selectedModuleInFamily = moduleInFamily
568 } else {
569 // There are duplicate modules from the same mainline module family
570 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())
571 }
572 }
573 }
574
575 // If a module has been selected, hide all other modules
576 if selectedModuleInFamily != nil {
577 for _, moduleInFamily := range allModulesInFamily {
578 if moduleInFamily.Name() != selectedModuleInFamily.Name() {
579 moduleInFamily.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +0000580 moduleInFamily.SkipInstall()
Spandan Dasf2c10572024-02-27 04:49:52 +0000581 // If this is a prebuilt module, unset properties.UsePrebuilt
582 // properties.UsePrebuilt might evaluate to true via soong config var fallback mechanism
583 // Set it to false explicitly so that the following mutator does not replace rdeps to this unselected prebuilt
584 if p := GetEmbeddedPrebuilt(moduleInFamily); p != nil {
585 p.properties.UsePrebuilt = false
586 }
587 }
588 }
589 }
590 // Do a validation pass to make sure that multiple prebuilts of a specific module are not selected.
591 // This might happen if the prebuilts share the same soong config var namespace.
592 // This should be an error, unless one of the prebuilts has been explicitly declared in apex_contributions
593 var selectedPrebuilt Module
594 for _, moduleInFamily := range allModulesInFamily {
595 // Skip if this module is in a different namespace
596 if !moduleInFamily.ExportedToMake() {
597 continue
598 }
599 // Skip for the top-level java_sdk_library_(_import). This has some special cases that need to be addressed first.
600 // This does not run into non-determinism because PrebuiltPostDepsMutator also has the special case
601 if sdkLibrary, ok := moduleInFamily.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil {
602 continue
603 }
604 if p := GetEmbeddedPrebuilt(moduleInFamily); p != nil && p.properties.UsePrebuilt {
605 if selectedPrebuilt == nil {
606 selectedPrebuilt = moduleInFamily
607 } else {
608 ctx.ModuleErrorf("Multiple prebuilt modules %v and %v have been marked as preferred for this source module. "+
609 "Please add the appropriate prebuilt module to apex_contributions for this release config.", selectedPrebuilt.Name(), moduleInFamily.Name())
Spandan Das3576e762024-01-03 18:57:03 +0000610 }
611 }
612 }
613}
614
Jaewoong Jung6158dfe2021-03-23 14:08:29 -0700615// PrebuiltPostDepsMutator replaces dependencies on the source module with dependencies on the
616// prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not
617// be used, disable installing it.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700618func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100619 m := ctx.Module()
620 if p := GetEmbeddedPrebuilt(m); p != nil {
Spandan Das3576e762024-01-03 18:57:03 +0000621 bmn, _ := m.(baseModuleName)
622 name := bmn.BaseModuleName()
Spandan Das81d95c52024-02-01 23:41:11 +0000623 psi := PrebuiltSelectionInfoMap{}
Jihoon Kanga3a05462024-04-05 00:36:44 +0000624 ctx.VisitDirectDepsWithTag(AcDepTag, func(am Module) {
Spandan Das81d95c52024-02-01 23:41:11 +0000625 psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider)
626 })
627
Colin Cross74d73e22017-08-02 11:05:49 -0700628 if p.properties.UsePrebuilt {
629 if p.properties.SourceExists {
Paul Duffin80342d72020-06-26 22:08:43 +0100630 ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
Spandan Das81d95c52024-02-01 23:41:11 +0000631 if sdkLibrary, ok := m.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil {
632 // Do not replace deps to the top-level prebuilt java_sdk_library hook.
633 // This hook has been special-cased in #isSelected to be _always_ active, even in next builds
634 // for dexpreopt and hiddenapi processing.
635 // If we do not special-case this here, rdeps referring to a java_sdk_library in next builds via libs
636 // will get prebuilt stubs
637 // TODO (b/308187268): Remove this after the apexes have been added to apex_contributions
Spandan Dasf2c10572024-02-27 04:49:52 +0000638 if psi.IsSelected(name) {
Spandan Das81d95c52024-02-01 23:41:11 +0000639 return false
640 }
641 }
642
Paul Duffin80342d72020-06-26 22:08:43 +0100643 if t, ok := tag.(ReplaceSourceWithPrebuilt); ok {
644 return t.ReplaceSourceWithPrebuilt()
645 }
Paul Duffin80342d72020-06-26 22:08:43 +0100646 return true
647 })
Colin Cross0f3c72f2016-11-23 15:44:07 -0800648 }
649 } else {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800650 m.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +0000651 m.SkipInstall()
Colin Crossce75d2c2016-10-06 16:12:58 -0700652 }
653 }
654}
655
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000656// A wrapper around PrebuiltSelectionInfoMap.IsSelected with special handling for java_sdk_library
657// java_sdk_library is a macro that creates
658// 1. top-level impl library
659// 2. stub libraries (suffixed with .stubs...)
660//
661// java_sdk_library_import is a macro that creates
662// 1. top-level "impl" library
663// 2. stub libraries (suffixed with .stubs...)
664//
665// 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
666// to provide the jar deapxed from the prebuilt apex
667//
668// isSelected uses `all_apex_contributions` to supersede source vs prebuilts selection of the stub libraries. It does not supersede the
669// selection of the top-level "impl" library so that this hook can work
670//
671// TODO (b/308174306) - Fix this when we need to support multiple prebuilts in main
672func isSelected(psi PrebuiltSelectionInfoMap, m Module) bool {
673 if sdkLibrary, ok := m.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil {
674 sln := proptools.String(sdkLibrary.SdkLibraryName())
Spandan Das23956d12024-01-19 00:22:22 +0000675
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000676 // This is the top-level library
677 // Do not supersede the existing prebuilts vs source selection mechanisms
Spandan Das81d95c52024-02-01 23:41:11 +0000678 // TODO (b/308187268): Remove this after the apexes have been added to apex_contributions
Spandan Das23956d12024-01-19 00:22:22 +0000679 if bmn, ok := m.(baseModuleName); ok && sln == bmn.BaseModuleName() {
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000680 return false
681 }
682
683 // Stub library created by java_sdk_library_import
Spandan Das23956d12024-01-19 00:22:22 +0000684 // java_sdk_library creates several child modules (java_import + prebuilt_stubs_sources) dynamically.
685 // This code block ensures that these child modules are selected if the top-level java_sdk_library_import is listed
686 // in the selected apex_contributions.
687 if javaImport, ok := m.(createdByJavaSdkLibraryName); ok && javaImport.CreatedByJavaSdkLibraryName() != nil {
688 return psi.IsSelected(PrebuiltNameFromSource(proptools.String(javaImport.CreatedByJavaSdkLibraryName())))
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000689 }
690
691 // Stub library created by java_sdk_library
Spandan Das3576e762024-01-03 18:57:03 +0000692 return psi.IsSelected(sln)
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000693 }
Spandan Das3576e762024-01-03 18:57:03 +0000694 return psi.IsSelected(m.Name())
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000695}
696
Spandan Das23956d12024-01-19 00:22:22 +0000697// implemented by child modules of java_sdk_library_import
698type createdByJavaSdkLibraryName interface {
699 CreatedByJavaSdkLibraryName() *string
700}
701
Spandan Das972917d2024-02-27 09:31:51 +0000702// Returns true if the prebuilt variant is disabled
703// e.g. for a cc_prebuilt_library_shared, this will return
704// - true for the static variant of the module
705// - false for the shared variant of the module
706//
707// Even though this is a cc_prebuilt_library_shared, we create both the variants today
708// 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 Crossb2388e32024-10-07 15:05:23 -0700709func (p *Prebuilt) variantIsDisabled(ctx BaseModuleContext, prebuilt Module) bool {
Spandan Das972917d2024-02-27 09:31:51 +0000710 return p.srcsSupplier != nil && len(p.srcsSupplier(ctx, prebuilt)) == 0
711}
712
Spandan Das85bd4622024-08-01 00:51:20 +0000713type apexVariationName interface {
714 ApexVariationName() string
715}
716
Colin Crossa2f296f2016-11-29 15:16:18 -0800717// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
718// will be used if it is marked "prefer" or if the source module is disabled.
Colin Crossb2388e32024-10-07 15:05:23 -0700719func (p *Prebuilt) usePrebuilt(ctx BaseModuleContext, source Module, prebuilt Module) bool {
Spandan Das85bd4622024-08-01 00:51:20 +0000720 isMainlinePrebuilt := func(prebuilt Module) bool {
721 apex, ok := prebuilt.(apexVariationName)
722 if !ok {
723 return false
724 }
725 // Prebuilts of aosp apexes in prebuilts/runtime
726 // Used in minimal art branches
727 if prebuilt.base().BaseModuleName() == apex.ApexVariationName() {
728 return false
729 }
730 return InList(apex.ApexVariationName(), ctx.Config().AllMainlineApexNames())
731 }
732
Spandan Das1c4d94d2023-10-26 22:38:34 +0000733 // Use `all_apex_contributions` for source vs prebuilt selection.
734 psi := PrebuiltSelectionInfoMap{}
Spandan Das85bd4622024-08-01 00:51:20 +0000735 var psiDepTag blueprint.DependencyTag
736 if p := GetEmbeddedPrebuilt(ctx.Module()); p != nil {
737 // This is a prebuilt module, visit all_apex_contributions to get the info
738 psiDepTag = AcDepTag
739 } else {
740 // This is a source module, visit any of its prebuilts to get the info
741 psiDepTag = PrebuiltDepTag
742 }
743 ctx.VisitDirectDepsWithTag(psiDepTag, func(am Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800744 psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider)
Spandan Das1c4d94d2023-10-26 22:38:34 +0000745 })
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000746
Spandan Das1c4d94d2023-10-26 22:38:34 +0000747 // If the source module is explicitly listed in the metadata module, use that
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000748 if source != nil && isSelected(psi, source) {
Spandan Das1c4d94d2023-10-26 22:38:34 +0000749 return false
750 }
751 // If the prebuilt module is explicitly listed in the metadata module, use that
Spandan Das972917d2024-02-27 09:31:51 +0000752 if isSelected(psi, prebuilt) && !p.variantIsDisabled(ctx, prebuilt) {
Spandan Das1c4d94d2023-10-26 22:38:34 +0000753 return true
754 }
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000755
Spandan Das85bd4622024-08-01 00:51:20 +0000756 // If this is a mainline prebuilt, but has not been flagged, hide it.
757 if isMainlinePrebuilt(prebuilt) {
758 return false
759 }
760
Spandan Das1c4d94d2023-10-26 22:38:34 +0000761 // If the baseModuleName could not be found in the metadata module,
762 // fall back to the existing source vs prebuilt selection.
763 // TODO: Drop the fallback mechanisms
764
Spandan Das972917d2024-02-27 09:31:51 +0000765 if p.variantIsDisabled(ctx, prebuilt) {
Colin Crossb63d7b32023-12-07 16:54:51 -0800766 return false
767 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700768
Colin Crossb63d7b32023-12-07 16:54:51 -0800769 // Skip prebuilt modules under unexported namespaces so that we won't
770 // end up shadowing non-prebuilt module when prebuilt module under same
771 // name happens to have a `Prefer` property set to true.
772 if ctx.Config().KatiEnabled() && !prebuilt.ExportedToMake() {
773 return false
LuK1337fb545bf2021-01-10 17:47:46 +0100774 }
775
Paul Duffin0c52c7b2021-07-06 17:15:25 +0100776 // If source is not available or is disabled then always use the prebuilt.
Cole Fausta963b942024-04-11 17:43:00 -0700777 if source == nil || !source.Enabled(ctx) {
Colin Crossb63d7b32023-12-07 16:54:51 -0800778 return true
Colin Crossa2f296f2016-11-29 15:16:18 -0800779 }
780
Paul Duffin0c52c7b2021-07-06 17:15:25 +0100781 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Cole Faust12ff57d2024-07-30 13:17:28 -0700782 return p.properties.Prefer.GetOrDefault(ctx, false)
Colin Crossce75d2c2016-10-06 16:12:58 -0700783}
Jiyong Park0a573d72019-07-07 12:39:16 +0900784
785func (p *Prebuilt) SourceExists() bool {
786 return p.properties.SourceExists
787}