blob: 4e4fa42ee7238db99fda94cc9d5f48020fa27113 [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)
31 ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators)
32}
33
Paul Duffin80342d72020-06-26 22:08:43 +010034// Marks a dependency tag as possibly preventing a reference to a source from being
35// replaced with the prebuilt.
36type 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 Zhang2502e122017-03-09 18:43:01 -080044type prebuiltDependencyTag struct {
45 blueprint.BaseDependencyTag
46}
47
Jiyong Park03b68dd2019-07-26 23:20:40 +090048var PrebuiltDepTag prebuiltDependencyTag
Colin Crossce75d2c2016-10-06 16:12:58 -070049
Paul Duffin78ac5b92020-01-14 12:42:08 +000050// Mark this tag so dependencies that use it are excluded from visibility enforcement.
51func (t prebuiltDependencyTag) ExcludeFromVisibilityEnforcement() {}
52
Paul Duffindddd5462020-04-07 15:25:44 +010053// Mark this tag so dependencies that use it are excluded from APEX contents.
54func (t prebuiltDependencyTag) ExcludeFromApexContents() {}
55
56var _ ExcludeFromVisibilityEnforcementTag = PrebuiltDepTag
57var _ ExcludeFromApexContentsTag = PrebuiltDepTag
58
Colin Cross74d73e22017-08-02 11:05:49 -070059type PrebuiltProperties struct {
60 // When prefer is set to true the prebuilt will be used instead of any source module with
61 // a matching name.
Nan Zhang0007d812017-11-07 10:57:05 -080062 Prefer *bool `android:"arch_variant"`
Colin Crossce75d2c2016-10-06 16:12:58 -070063
Paul Duffin0c52c7b2021-07-06 17:15:25 +010064 // When specified this names a Soong config variable that controls the prefer property.
65 //
66 // If the value of the named Soong config variable is true then prefer is set to false and vice
67 // versa. If the Soong config variable is not set then it defaults to false, so prefer defaults
68 // to true.
69 //
70 // If specified then the prefer property is ignored in favor of the value of the Soong config
71 // variable.
72 Use_source_config_var *ConfigVarProperties
73
Colin Cross74d73e22017-08-02 11:05:49 -070074 SourceExists bool `blueprint:"mutated"`
75 UsePrebuilt bool `blueprint:"mutated"`
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000076
77 // Set if the module has been renamed to remove the "prebuilt_" prefix.
78 PrebuiltRenamedToSource bool `blueprint:"mutated"`
Colin Cross74d73e22017-08-02 11:05:49 -070079}
80
Paul Duffin0c52c7b2021-07-06 17:15:25 +010081// Properties that can be used to select a Soong config variable.
82type ConfigVarProperties struct {
83 // Allow instances of this struct to be used as a property value in a BpPropertySet.
84 BpPrintableBase
85
86 // The name of the configuration namespace.
87 //
88 // As passed to add_soong_config_namespace in Make.
89 Config_namespace *string
90
91 // The name of the configuration variable.
92 //
93 // As passed to add_soong_config_var_value in Make.
94 Var_name *string
95}
96
Colin Cross74d73e22017-08-02 11:05:49 -070097type Prebuilt struct {
98 properties PrebuiltProperties
Jaewoong Jung3e18b192019-06-11 12:25:34 -070099
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000100 // nil if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs.
101 srcsSupplier PrebuiltSrcsSupplier
102
103 // "-" if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000104 srcsPropertyName string
Colin Crossce75d2c2016-10-06 16:12:58 -0700105}
106
Paul Duffind23c7262020-12-11 18:13:08 +0000107// RemoveOptionalPrebuiltPrefix returns the result of removing the "prebuilt_" prefix from the
108// supplied name if it has one, or returns the name unmodified if it does not.
109func RemoveOptionalPrebuiltPrefix(name string) string {
110 return strings.TrimPrefix(name, "prebuilt_")
111}
112
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000113// RemoveOptionalPrebuiltPrefixFromBazelLabel removes the "prebuilt_" prefix from the *target name* of a Bazel label.
114// This differs from RemoveOptionalPrebuiltPrefix in that it does not remove it from the start of the string, but
115// instead removes it from the target name itself.
116func RemoveOptionalPrebuiltPrefixFromBazelLabel(label string) string {
117 splitLabel := strings.Split(label, ":")
118 bazelModuleNameNoPrebuilt := RemoveOptionalPrebuiltPrefix(splitLabel[1])
119 return strings.Join([]string{
120 splitLabel[0],
121 bazelModuleNameNoPrebuilt,
122 }, ":")
123}
124
Colin Crossce75d2c2016-10-06 16:12:58 -0700125func (p *Prebuilt) Name(name string) string {
Paul Duffin864116c2021-04-02 10:24:13 +0100126 return PrebuiltNameFromSource(name)
127}
128
129// PrebuiltNameFromSource returns the result of prepending the "prebuilt_" prefix to the supplied
130// name.
131func PrebuiltNameFromSource(name string) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700132 return "prebuilt_" + name
133}
134
Paul Duffin50061512020-01-21 16:31:05 +0000135func (p *Prebuilt) ForcePrefer() {
136 p.properties.Prefer = proptools.BoolPtr(true)
137}
138
Paul Duffin38b57852020-05-13 16:08:09 +0100139func (p *Prebuilt) Prefer() bool {
140 return proptools.Bool(p.properties.Prefer)
141}
142
Paul Duffin56dc66e2021-03-01 13:18:44 +0000143// SingleSourcePathFromSupplier invokes the supplied supplier for the current module in the
144// supplied context to retrieve a list of file paths, ensures that the returned list of file paths
145// contains a single value and then assumes that is a module relative file path and converts it to
146// a Path accordingly.
147//
148// Any issues, such as nil supplier or not exactly one file path will be reported as errors on the
149// supplied context and this will return nil.
150func SingleSourcePathFromSupplier(ctx ModuleContext, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) Path {
151 if srcsSupplier != nil {
152 srcs := srcsSupplier(ctx, ctx.Module())
Paul Duffindcb4bd62020-03-12 23:43:52 +0000153
154 if len(srcs) == 0 {
Paul Duffin56dc66e2021-03-01 13:18:44 +0000155 ctx.PropertyErrorf(srcsPropertyName, "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700156 return nil
157 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700158
Paul Duffindcb4bd62020-03-12 23:43:52 +0000159 if len(srcs) > 1 {
Paul Duffin56dc66e2021-03-01 13:18:44 +0000160 ctx.PropertyErrorf(srcsPropertyName, "multiple prebuilt source files")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700161 return nil
162 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700163
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700164 // Return the singleton source after expanding any filegroup in the
165 // sources.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000166 src := srcs[0]
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700167 return PathForModuleSrc(ctx, src)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000168 } else {
169 ctx.ModuleErrorf("prebuilt source was not set")
170 return nil
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700171 }
Colin Cross74d73e22017-08-02 11:05:49 -0700172}
173
Paul Duffin56dc66e2021-03-01 13:18:44 +0000174// The below source-related functions and the srcs, src fields are based on an assumption that
175// prebuilt modules have a static source property at the moment. Currently there is only one
176// exception, android_app_import, which chooses a source file depending on the product's DPI
177// preference configs. We'll want to add native support for dynamic source cases if we end up having
178// more modules like this.
179func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
180 return SingleSourcePathFromSupplier(ctx, p.srcsSupplier, p.srcsPropertyName)
181}
182
Jiyong Park4d277042019-04-23 18:00:10 +0900183func (p *Prebuilt) UsePrebuilt() bool {
184 return p.properties.UsePrebuilt
185}
186
Paul Duffindcb4bd62020-03-12 23:43:52 +0000187// Called to provide the srcs value for the prebuilt module.
188//
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000189// This can be called with a context for any module not just the prebuilt one itself. It can also be
190// called concurrently.
191//
Paul Duffindcb4bd62020-03-12 23:43:52 +0000192// Return the src value or nil if it is not available.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000193type PrebuiltSrcsSupplier func(ctx BaseModuleContext, prebuilt Module) []string
Paul Duffindcb4bd62020-03-12 23:43:52 +0000194
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000195func initPrebuiltModuleCommon(module PrebuiltInterface) *Prebuilt {
196 p := module.Prebuilt()
197 module.AddProperties(&p.properties)
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000198 return p
199}
200
201// Initialize the module as a prebuilt module that has no dedicated property that lists its
202// sources. SingleSourcePathFromSupplier should not be called for this module.
203//
204// This is the case e.g. for header modules, which provides the headers in source form
205// regardless whether they are prebuilt or not.
206func InitPrebuiltModuleWithoutSrcs(module PrebuiltInterface) {
207 p := initPrebuiltModuleCommon(module)
208 p.srcsPropertyName = "-"
209}
210
Paul Duffindcb4bd62020-03-12 23:43:52 +0000211// Initialize the module as a prebuilt module that uses the provided supplier to access the
212// prebuilt sources of the module.
213//
214// The supplier will be called multiple times and must return the same values each time it
215// is called. If it returns an empty array (or nil) then the prebuilt module will not be used
216// as a replacement for a source module with the same name even if prefer = true.
217//
218// If the Prebuilt.SingleSourcePath() is called on the module then this must return an array
219// containing exactly one source file.
220//
221// The provided property name is used to provide helpful error messages in the event that
222// a problem arises, e.g. calling SingleSourcePath() when more than one source is provided.
223func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000224 if srcsSupplier == nil {
225 panic(fmt.Errorf("srcsSupplier must not be nil"))
226 }
227 if srcsPropertyName == "" {
228 panic(fmt.Errorf("srcsPropertyName must not be empty"))
229 }
230
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000231 p := initPrebuiltModuleCommon(module)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000232 p.srcsSupplier = srcsSupplier
233 p.srcsPropertyName = srcsPropertyName
234}
235
236func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
237 if srcs == nil {
238 panic(fmt.Errorf("srcs must not be nil"))
239 }
240
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000241 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000242 return *srcs
243 }
244
245 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Colin Crossce75d2c2016-10-06 16:12:58 -0700246}
247
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700248func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000249 srcPropsValue := reflect.ValueOf(srcProps).Elem()
250 srcStructField, _ := srcPropsValue.Type().FieldByName(srcField)
251 if !srcPropsValue.IsValid() || srcStructField.Name == "" {
252 panic(fmt.Errorf("invalid single source prebuilt %+v", module))
253 }
254
255 if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface {
256 panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps))
257 }
258
259 srcFieldIndex := srcStructField.Index
260 srcPropertyName := proptools.PropertyNameForField(srcField)
261
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000262 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
Jaewoong Jung729c0bd2020-12-08 19:11:54 -0800263 if !module.Enabled() {
Jaewoong Jung84f1b802020-12-04 11:51:29 -0800264 return nil
265 }
Paul Duffindcb4bd62020-03-12 23:43:52 +0000266 value := srcPropsValue.FieldByIndex(srcFieldIndex)
267 if value.Kind() == reflect.Ptr {
268 value = value.Elem()
269 }
270 if value.Kind() != reflect.String {
Martin Stjernholm25a69de2021-09-09 02:48:30 +0100271 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 +0000272 }
273 src := value.String()
274 if src == "" {
275 return nil
276 }
277 return []string{src}
278 }
279
280 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700281}
282
Colin Crossce75d2c2016-10-06 16:12:58 -0700283type PrebuiltInterface interface {
284 Module
285 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700286}
287
Paul Duffine1d38372021-04-02 10:35:24 +0100288// IsModulePreferred returns true if the given module is preferred.
289//
290// A source module is preferred if there is no corresponding prebuilt module or the prebuilt module
291// does not have "prefer: true".
292//
293// A prebuilt module is preferred if there is no corresponding source module or the prebuilt module
294// has "prefer: true".
295func IsModulePreferred(module Module) bool {
296 if module.IsReplacedByPrebuilt() {
297 // A source module that has been replaced by a prebuilt counterpart.
298 return false
299 }
Paul Duffinf7c99f52021-04-28 10:41:21 +0100300 if p := GetEmbeddedPrebuilt(module); p != nil {
301 return p.UsePrebuilt()
Paul Duffine1d38372021-04-02 10:35:24 +0100302 }
303 return true
304}
305
Paul Duffinf7c99f52021-04-28 10:41:21 +0100306// IsModulePrebuilt returns true if the module implements PrebuiltInterface and
307// has been initialized as a prebuilt and so returns a non-nil value from the
308// PrebuiltInterface.Prebuilt() method.
309func IsModulePrebuilt(module Module) bool {
310 return GetEmbeddedPrebuilt(module) != nil
311}
312
313// GetEmbeddedPrebuilt returns a pointer to the embedded Prebuilt structure or
314// nil if the module does not implement PrebuiltInterface or has not been
315// initialized as a prebuilt module.
316func GetEmbeddedPrebuilt(module Module) *Prebuilt {
317 if p, ok := module.(PrebuiltInterface); ok {
318 return p.Prebuilt()
319 }
320
321 return nil
322}
323
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000324// PrebuiltGetPreferred returns the module that is preferred for the given
325// module. That is either the module itself or the prebuilt counterpart that has
326// taken its place. The given module must be a direct dependency of the current
327// context module, and it must be the source module if both source and prebuilt
328// exist.
329//
330// This function is for use on dependencies after PrebuiltPostDepsMutator has
331// run - any dependency that is registered before that will already reference
332// the right module. This function is only safe to call after all mutators that
333// may call CreateVariations, e.g. in GenerateAndroidBuildActions.
334func PrebuiltGetPreferred(ctx BaseModuleContext, module Module) Module {
335 if !module.IsReplacedByPrebuilt() {
336 return module
337 }
338 if IsModulePrebuilt(module) {
339 // If we're given a prebuilt then assume there's no source module around.
340 return module
341 }
342
343 sourceModDepFound := false
344 var prebuiltMod Module
345
346 ctx.WalkDeps(func(child, parent Module) bool {
347 if prebuiltMod != nil {
348 return false
349 }
350 if parent == ctx.Module() {
351 // First level: Only recurse if the module is found as a direct dependency.
352 sourceModDepFound = child == module
353 return sourceModDepFound
354 }
355 // Second level: Follow PrebuiltDepTag to the prebuilt.
356 if t := ctx.OtherModuleDependencyTag(child); t == PrebuiltDepTag {
357 prebuiltMod = child
358 }
359 return false
360 })
361
362 if prebuiltMod == nil {
363 if !sourceModDepFound {
364 panic(fmt.Errorf("Failed to find source module as a direct dependency: %s", module))
365 } else {
366 panic(fmt.Errorf("Failed to find prebuilt for source module: %s", module))
367 }
368 }
369 return prebuiltMod
370}
371
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700372func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000373 ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700374}
375
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700376func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000377 ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700378 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700379 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700380}
381
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000382// PrebuiltRenameMutator ensures that there always is a module with an
383// undecorated name.
384func PrebuiltRenameMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100385 m := ctx.Module()
386 if p := GetEmbeddedPrebuilt(m); p != nil {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000387 name := m.base().BaseModuleName()
388 if !ctx.OtherModuleExists(name) {
389 ctx.Rename(name)
Paul Duffinf7c99f52021-04-28 10:41:21 +0100390 p.properties.PrebuiltRenamedToSource = true
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000391 }
392 }
393}
394
395// PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the
396// corresponding source module, if one exists for the same variant.
397func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100398 m := ctx.Module()
399 // If this module is a prebuilt, is enabled and has not been renamed to source then add a
400 // dependency onto the source if it is present.
401 if p := GetEmbeddedPrebuilt(m); p != nil && m.Enabled() && !p.properties.PrebuiltRenamedToSource {
402 name := m.base().BaseModuleName()
403 if ctx.OtherModuleReverseDependencyVariantExists(name) {
404 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
405 p.properties.SourceExists = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700406 }
407 }
408}
409
Colin Crossc3e7fa62017-03-17 13:14:32 -0700410// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
411// because the source module doesn't exist. It also disables installing overridden source modules.
Colin Crossa2f296f2016-11-29 15:16:18 -0800412func PrebuiltSelectModuleMutator(ctx TopDownMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100413 m := ctx.Module()
414 if p := GetEmbeddedPrebuilt(m); p != nil {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000415 if p.srcsSupplier == nil && p.srcsPropertyName == "" {
Colin Cross74d73e22017-08-02 11:05:49 -0700416 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
417 }
418 if !p.properties.SourceExists {
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000419 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil, m)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700420 }
421 } else if s, ok := ctx.Module().(Module); ok {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100422 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(prebuiltModule Module) {
423 p := GetEmbeddedPrebuilt(prebuiltModule)
424 if p.usePrebuilt(ctx, s, prebuiltModule) {
Colin Crossee6143c2017-12-30 17:54:27 -0800425 p.properties.UsePrebuilt = true
Liz Kammer5ca3a622020-08-05 15:40:41 -0700426 s.ReplacedByPrebuilt()
Colin Crossa2f296f2016-11-29 15:16:18 -0800427 }
428 })
429 }
430}
431
Jaewoong Jung6158dfe2021-03-23 14:08:29 -0700432// PrebuiltPostDepsMutator replaces dependencies on the source module with dependencies on the
433// prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not
434// be used, disable installing it.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700435func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100436 m := ctx.Module()
437 if p := GetEmbeddedPrebuilt(m); p != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700438 name := m.base().BaseModuleName()
Colin Cross74d73e22017-08-02 11:05:49 -0700439 if p.properties.UsePrebuilt {
440 if p.properties.SourceExists {
Paul Duffin80342d72020-06-26 22:08:43 +0100441 ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
442 if t, ok := tag.(ReplaceSourceWithPrebuilt); ok {
443 return t.ReplaceSourceWithPrebuilt()
444 }
445
446 return true
447 })
Colin Cross0f3c72f2016-11-23 15:44:07 -0800448 }
449 } else {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800450 m.HideFromMake()
Colin Crossce75d2c2016-10-06 16:12:58 -0700451 }
452 }
453}
454
Colin Crossa2f296f2016-11-29 15:16:18 -0800455// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
456// will be used if it is marked "prefer" or if the source module is disabled.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000457func (p *Prebuilt) usePrebuilt(ctx TopDownMutatorContext, source Module, prebuilt Module) bool {
458 if p.srcsSupplier != nil && len(p.srcsSupplier(ctx, prebuilt)) == 0 {
Colin Crossa2f296f2016-11-29 15:16:18 -0800459 return false
Colin Crossce75d2c2016-10-06 16:12:58 -0700460 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700461
LuK1337fb545bf2021-01-10 17:47:46 +0100462 // Skip prebuilt modules under unexported namespaces so that we won't
463 // end up shadowing non-prebuilt module when prebuilt module under same
464 // name happens to have a `Prefer` property set to true.
465 if ctx.Config().KatiEnabled() && !prebuilt.ExportedToMake() {
466 return false
467 }
468
Paul Duffin0c52c7b2021-07-06 17:15:25 +0100469 // If source is not available or is disabled then always use the prebuilt.
470 if source == nil || !source.Enabled() {
Colin Crossa2f296f2016-11-29 15:16:18 -0800471 return true
472 }
473
Paul Duffin0c52c7b2021-07-06 17:15:25 +0100474 // If the use_source_config_var property is set then it overrides the prefer property setting.
475 if configVar := p.properties.Use_source_config_var; configVar != nil {
476 return !ctx.Config().VendorConfig(proptools.String(configVar.Config_namespace)).Bool(proptools.String(configVar.Var_name))
477 }
478
479 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
480 return Bool(p.properties.Prefer)
Colin Crossce75d2c2016-10-06 16:12:58 -0700481}
Jiyong Park0a573d72019-07-07 12:39:16 +0900482
483func (p *Prebuilt) SourceExists() bool {
484 return p.properties.SourceExists
485}