blob: 8856d2619af69a3b333258082dd542f809492dc8 [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
Paul Duffinbf4de042022-09-27 12:41:52 +010059// UserSuppliedPrebuiltProperties contains the prebuilt properties that can be specified in an
60// Android.bp file.
61type UserSuppliedPrebuiltProperties struct {
Colin Cross74d73e22017-08-02 11:05:49 -070062 // When prefer is set to true the prebuilt will be used instead of any source module with
63 // a matching name.
Cole Faust12ff57d2024-07-30 13:17:28 -070064 Prefer proptools.Configurable[bool] `android:"arch_variant,replace_instead_of_append"`
Colin Crossce75d2c2016-10-06 16:12:58 -070065
Paul Duffin0c52c7b2021-07-06 17:15:25 +010066 // When specified this names a Soong config variable that controls the prefer property.
67 //
68 // If the value of the named Soong config variable is true then prefer is set to false and vice
69 // versa. If the Soong config variable is not set then it defaults to false, so prefer defaults
70 // to true.
71 //
72 // If specified then the prefer property is ignored in favor of the value of the Soong config
73 // variable.
Spandan Dasa9cf0c82024-04-01 22:28:59 +000074 //
75 // DEPRECATED: This property is being deprecated b/308188211.
76 // Use RELEASE_APEX_CONTRIBUTIONS build flags to select prebuilts of mainline modules.
Paul Duffin0c52c7b2021-07-06 17:15:25 +010077 Use_source_config_var *ConfigVarProperties
Paul Duffinbf4de042022-09-27 12:41:52 +010078}
79
80// CopyUserSuppliedPropertiesFromPrebuilt copies the user supplied prebuilt properties from the
81// prebuilt properties.
82func (u *UserSuppliedPrebuiltProperties) CopyUserSuppliedPropertiesFromPrebuilt(p *Prebuilt) {
83 *u = p.properties.UserSuppliedPrebuiltProperties
84}
85
86type PrebuiltProperties struct {
87 UserSuppliedPrebuiltProperties
Paul Duffin0c52c7b2021-07-06 17:15:25 +010088
Colin Cross74d73e22017-08-02 11:05:49 -070089 SourceExists bool `blueprint:"mutated"`
90 UsePrebuilt bool `blueprint:"mutated"`
Martin Stjernholm009a9dc2020-03-05 17:34:13 +000091
92 // Set if the module has been renamed to remove the "prebuilt_" prefix.
93 PrebuiltRenamedToSource bool `blueprint:"mutated"`
Colin Cross74d73e22017-08-02 11:05:49 -070094}
95
Paul Duffin0c52c7b2021-07-06 17:15:25 +010096// Properties that can be used to select a Soong config variable.
97type ConfigVarProperties struct {
98 // Allow instances of this struct to be used as a property value in a BpPropertySet.
99 BpPrintableBase
100
101 // The name of the configuration namespace.
102 //
103 // As passed to add_soong_config_namespace in Make.
104 Config_namespace *string
105
106 // The name of the configuration variable.
107 //
108 // As passed to add_soong_config_var_value in Make.
109 Var_name *string
110}
111
Colin Cross74d73e22017-08-02 11:05:49 -0700112type Prebuilt struct {
113 properties PrebuiltProperties
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700114
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000115 // nil if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs.
116 srcsSupplier PrebuiltSrcsSupplier
117
118 // "-" if the prebuilt has no srcs property at all. See InitPrebuiltModuleWithoutSrcs.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000119 srcsPropertyName string
Colin Crossce75d2c2016-10-06 16:12:58 -0700120}
121
Paul Duffind23c7262020-12-11 18:13:08 +0000122// RemoveOptionalPrebuiltPrefix returns the result of removing the "prebuilt_" prefix from the
123// supplied name if it has one, or returns the name unmodified if it does not.
124func RemoveOptionalPrebuiltPrefix(name string) string {
125 return strings.TrimPrefix(name, "prebuilt_")
126}
127
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000128// RemoveOptionalPrebuiltPrefixFromBazelLabel removes the "prebuilt_" prefix from the *target name* of a Bazel label.
129// This differs from RemoveOptionalPrebuiltPrefix in that it does not remove it from the start of the string, but
130// instead removes it from the target name itself.
131func RemoveOptionalPrebuiltPrefixFromBazelLabel(label string) string {
132 splitLabel := strings.Split(label, ":")
133 bazelModuleNameNoPrebuilt := RemoveOptionalPrebuiltPrefix(splitLabel[1])
134 return strings.Join([]string{
135 splitLabel[0],
136 bazelModuleNameNoPrebuilt,
137 }, ":")
138}
139
Colin Crossce75d2c2016-10-06 16:12:58 -0700140func (p *Prebuilt) Name(name string) string {
Paul Duffin864116c2021-04-02 10:24:13 +0100141 return PrebuiltNameFromSource(name)
142}
143
144// PrebuiltNameFromSource returns the result of prepending the "prebuilt_" prefix to the supplied
145// name.
146func PrebuiltNameFromSource(name string) string {
Colin Crossce75d2c2016-10-06 16:12:58 -0700147 return "prebuilt_" + name
148}
149
Paul Duffin50061512020-01-21 16:31:05 +0000150func (p *Prebuilt) ForcePrefer() {
Cole Faust12ff57d2024-07-30 13:17:28 -0700151 p.properties.Prefer = NewSimpleConfigurable(true)
Paul Duffin38b57852020-05-13 16:08:09 +0100152}
153
Paul Duffin56dc66e2021-03-01 13:18:44 +0000154// SingleSourcePathFromSupplier invokes the supplied supplier for the current module in the
155// supplied context to retrieve a list of file paths, ensures that the returned list of file paths
156// contains a single value and then assumes that is a module relative file path and converts it to
157// a Path accordingly.
158//
159// Any issues, such as nil supplier or not exactly one file path will be reported as errors on the
160// supplied context and this will return nil.
161func SingleSourcePathFromSupplier(ctx ModuleContext, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) Path {
162 if srcsSupplier != nil {
163 srcs := srcsSupplier(ctx, ctx.Module())
Paul Duffindcb4bd62020-03-12 23:43:52 +0000164
165 if len(srcs) == 0 {
Paul Duffin56dc66e2021-03-01 13:18:44 +0000166 ctx.PropertyErrorf(srcsPropertyName, "missing prebuilt source file")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700167 return nil
168 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700169
Paul Duffindcb4bd62020-03-12 23:43:52 +0000170 if len(srcs) > 1 {
Paul Duffin56dc66e2021-03-01 13:18:44 +0000171 ctx.PropertyErrorf(srcsPropertyName, "multiple prebuilt source files")
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700172 return nil
173 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700174
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700175 // Return the singleton source after expanding any filegroup in the
176 // sources.
Paul Duffindcb4bd62020-03-12 23:43:52 +0000177 src := srcs[0]
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700178 return PathForModuleSrc(ctx, src)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000179 } else {
180 ctx.ModuleErrorf("prebuilt source was not set")
181 return nil
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700182 }
Colin Cross74d73e22017-08-02 11:05:49 -0700183}
184
Paul Duffin56dc66e2021-03-01 13:18:44 +0000185// The below source-related functions and the srcs, src fields are based on an assumption that
186// prebuilt modules have a static source property at the moment. Currently there is only one
187// exception, android_app_import, which chooses a source file depending on the product's DPI
188// preference configs. We'll want to add native support for dynamic source cases if we end up having
189// more modules like this.
190func (p *Prebuilt) SingleSourcePath(ctx ModuleContext) Path {
191 return SingleSourcePathFromSupplier(ctx, p.srcsSupplier, p.srcsPropertyName)
192}
193
Jiyong Park4d277042019-04-23 18:00:10 +0900194func (p *Prebuilt) UsePrebuilt() bool {
195 return p.properties.UsePrebuilt
196}
197
Paul Duffindcb4bd62020-03-12 23:43:52 +0000198// Called to provide the srcs value for the prebuilt module.
199//
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000200// This can be called with a context for any module not just the prebuilt one itself. It can also be
201// called concurrently.
202//
Paul Duffindcb4bd62020-03-12 23:43:52 +0000203// Return the src value or nil if it is not available.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000204type PrebuiltSrcsSupplier func(ctx BaseModuleContext, prebuilt Module) []string
Paul Duffindcb4bd62020-03-12 23:43:52 +0000205
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000206func initPrebuiltModuleCommon(module PrebuiltInterface) *Prebuilt {
207 p := module.Prebuilt()
208 module.AddProperties(&p.properties)
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000209 return p
210}
211
212// Initialize the module as a prebuilt module that has no dedicated property that lists its
213// sources. SingleSourcePathFromSupplier should not be called for this module.
214//
215// This is the case e.g. for header modules, which provides the headers in source form
216// regardless whether they are prebuilt or not.
217func InitPrebuiltModuleWithoutSrcs(module PrebuiltInterface) {
218 p := initPrebuiltModuleCommon(module)
219 p.srcsPropertyName = "-"
220}
221
Paul Duffindcb4bd62020-03-12 23:43:52 +0000222// Initialize the module as a prebuilt module that uses the provided supplier to access the
223// prebuilt sources of the module.
224//
225// The supplier will be called multiple times and must return the same values each time it
226// is called. If it returns an empty array (or nil) then the prebuilt module will not be used
227// as a replacement for a source module with the same name even if prefer = true.
228//
229// If the Prebuilt.SingleSourcePath() is called on the module then this must return an array
230// containing exactly one source file.
231//
232// The provided property name is used to provide helpful error messages in the event that
233// a problem arises, e.g. calling SingleSourcePath() when more than one source is provided.
234func InitPrebuiltModuleWithSrcSupplier(module PrebuiltInterface, srcsSupplier PrebuiltSrcsSupplier, srcsPropertyName string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000235 if srcsSupplier == nil {
236 panic(fmt.Errorf("srcsSupplier must not be nil"))
237 }
238 if srcsPropertyName == "" {
239 panic(fmt.Errorf("srcsPropertyName must not be empty"))
240 }
241
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000242 p := initPrebuiltModuleCommon(module)
Paul Duffindcb4bd62020-03-12 23:43:52 +0000243 p.srcsSupplier = srcsSupplier
244 p.srcsPropertyName = srcsPropertyName
245}
246
247func InitPrebuiltModule(module PrebuiltInterface, srcs *[]string) {
248 if srcs == nil {
249 panic(fmt.Errorf("srcs must not be nil"))
250 }
251
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000252 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000253 return *srcs
254 }
255
256 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Colin Crossce75d2c2016-10-06 16:12:58 -0700257}
258
Jaewoong Jung3e18b192019-06-11 12:25:34 -0700259func InitSingleSourcePrebuiltModule(module PrebuiltInterface, srcProps interface{}, srcField string) {
Paul Duffindcb4bd62020-03-12 23:43:52 +0000260 srcPropsValue := reflect.ValueOf(srcProps).Elem()
261 srcStructField, _ := srcPropsValue.Type().FieldByName(srcField)
262 if !srcPropsValue.IsValid() || srcStructField.Name == "" {
263 panic(fmt.Errorf("invalid single source prebuilt %+v", module))
264 }
265
266 if srcPropsValue.Kind() != reflect.Struct && srcPropsValue.Kind() != reflect.Interface {
267 panic(fmt.Errorf("invalid single source prebuilt %+v", srcProps))
268 }
269
270 srcFieldIndex := srcStructField.Index
271 srcPropertyName := proptools.PropertyNameForField(srcField)
272
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000273 srcsSupplier := func(ctx BaseModuleContext, _ Module) []string {
Cole Fausta963b942024-04-11 17:43:00 -0700274 if !module.Enabled(ctx) {
Jaewoong Jung84f1b802020-12-04 11:51:29 -0800275 return nil
276 }
Paul Duffindcb4bd62020-03-12 23:43:52 +0000277 value := srcPropsValue.FieldByIndex(srcFieldIndex)
278 if value.Kind() == reflect.Ptr {
Cole Faust97494b12024-01-12 14:02:47 -0800279 if value.IsNil() {
280 return nil
281 }
Paul Duffindcb4bd62020-03-12 23:43:52 +0000282 value = value.Elem()
283 }
284 if value.Kind() != reflect.String {
Martin Stjernholm25a69de2021-09-09 02:48:30 +0100285 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 +0000286 }
287 src := value.String()
288 if src == "" {
289 return nil
290 }
291 return []string{src}
292 }
293
294 InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcPropertyName)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700295}
296
Colin Crossce75d2c2016-10-06 16:12:58 -0700297type PrebuiltInterface interface {
298 Module
299 Prebuilt() *Prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700300}
301
Paul Duffine1d38372021-04-02 10:35:24 +0100302// IsModulePreferred returns true if the given module is preferred.
303//
304// A source module is preferred if there is no corresponding prebuilt module or the prebuilt module
305// does not have "prefer: true".
306//
307// A prebuilt module is preferred if there is no corresponding source module or the prebuilt module
308// has "prefer: true".
309func IsModulePreferred(module Module) bool {
310 if module.IsReplacedByPrebuilt() {
311 // A source module that has been replaced by a prebuilt counterpart.
312 return false
313 }
Paul Duffinf7c99f52021-04-28 10:41:21 +0100314 if p := GetEmbeddedPrebuilt(module); p != nil {
315 return p.UsePrebuilt()
Paul Duffine1d38372021-04-02 10:35:24 +0100316 }
317 return true
318}
319
Paul Duffinf7c99f52021-04-28 10:41:21 +0100320// IsModulePrebuilt returns true if the module implements PrebuiltInterface and
321// has been initialized as a prebuilt and so returns a non-nil value from the
322// PrebuiltInterface.Prebuilt() method.
323func IsModulePrebuilt(module Module) bool {
324 return GetEmbeddedPrebuilt(module) != nil
325}
326
327// GetEmbeddedPrebuilt returns a pointer to the embedded Prebuilt structure or
328// nil if the module does not implement PrebuiltInterface or has not been
329// initialized as a prebuilt module.
330func GetEmbeddedPrebuilt(module Module) *Prebuilt {
331 if p, ok := module.(PrebuiltInterface); ok {
332 return p.Prebuilt()
333 }
334
335 return nil
336}
337
Martin Stjernholmdbd814d2022-01-12 23:18:30 +0000338// PrebuiltGetPreferred returns the module that is preferred for the given
339// module. That is either the module itself or the prebuilt counterpart that has
340// taken its place. The given module must be a direct dependency of the current
341// context module, and it must be the source module if both source and prebuilt
342// exist.
343//
344// This function is for use on dependencies after PrebuiltPostDepsMutator has
345// run - any dependency that is registered before that will already reference
346// the right module. This function is only safe to call after all mutators that
347// may call CreateVariations, e.g. in GenerateAndroidBuildActions.
348func PrebuiltGetPreferred(ctx BaseModuleContext, module Module) Module {
349 if !module.IsReplacedByPrebuilt() {
350 return module
351 }
352 if IsModulePrebuilt(module) {
353 // If we're given a prebuilt then assume there's no source module around.
354 return module
355 }
356
357 sourceModDepFound := false
358 var prebuiltMod Module
359
360 ctx.WalkDeps(func(child, parent Module) bool {
361 if prebuiltMod != nil {
362 return false
363 }
364 if parent == ctx.Module() {
365 // First level: Only recurse if the module is found as a direct dependency.
366 sourceModDepFound = child == module
367 return sourceModDepFound
368 }
369 // Second level: Follow PrebuiltDepTag to the prebuilt.
370 if t := ctx.OtherModuleDependencyTag(child); t == PrebuiltDepTag {
371 prebuiltMod = child
372 }
373 return false
374 })
375
376 if prebuiltMod == nil {
377 if !sourceModDepFound {
378 panic(fmt.Errorf("Failed to find source module as a direct dependency: %s", module))
379 } else {
380 panic(fmt.Errorf("Failed to find prebuilt for source module: %s", module))
381 }
382 }
383 return prebuiltMod
384}
385
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700386func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000387 ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700388}
389
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700390func RegisterPrebuiltsPostDepsMutators(ctx RegisterMutatorsContext) {
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000391 ctx.BottomUp("prebuilt_source", PrebuiltSourceDepsMutator).Parallel()
Spandan Das1c4d94d2023-10-26 22:38:34 +0000392 ctx.BottomUp("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700393 ctx.BottomUp("prebuilt_postdeps", PrebuiltPostDepsMutator).Parallel()
Colin Crosscec81712017-07-13 14:43:27 -0700394}
395
Spandan Das3576e762024-01-03 18:57:03 +0000396// Returns the name of the source module corresponding to a prebuilt module
397// For source modules, it returns its own name
398type baseModuleName interface {
399 BaseModuleName() string
400}
401
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000402// PrebuiltRenameMutator ensures that there always is a module with an
403// undecorated name.
404func PrebuiltRenameMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100405 m := ctx.Module()
406 if p := GetEmbeddedPrebuilt(m); p != nil {
Spandan Das3576e762024-01-03 18:57:03 +0000407 bmn, _ := m.(baseModuleName)
408 name := bmn.BaseModuleName()
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000409 if !ctx.OtherModuleExists(name) {
410 ctx.Rename(name)
Paul Duffinf7c99f52021-04-28 10:41:21 +0100411 p.properties.PrebuiltRenamedToSource = true
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000412 }
413 }
414}
415
416// PrebuiltSourceDepsMutator adds dependencies to the prebuilt module from the
417// corresponding source module, if one exists for the same variant.
Spandan Das1c4d94d2023-10-26 22:38:34 +0000418// Add a dependency from the prebuilt to `all_apex_contributions`
419// The metadata will be used for source vs prebuilts selection
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000420func PrebuiltSourceDepsMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100421 m := ctx.Module()
422 // If this module is a prebuilt, is enabled and has not been renamed to source then add a
423 // dependency onto the source if it is present.
Cole Fausta963b942024-04-11 17:43:00 -0700424 if p := GetEmbeddedPrebuilt(m); p != nil && m.Enabled(ctx) && !p.properties.PrebuiltRenamedToSource {
Spandan Das3576e762024-01-03 18:57:03 +0000425 bmn, _ := m.(baseModuleName)
426 name := bmn.BaseModuleName()
Paul Duffinf7c99f52021-04-28 10:41:21 +0100427 if ctx.OtherModuleReverseDependencyVariantExists(name) {
428 ctx.AddReverseDependency(ctx.Module(), PrebuiltDepTag, name)
429 p.properties.SourceExists = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700430 }
Spandan Das1c4d94d2023-10-26 22:38:34 +0000431 // Add a dependency from the prebuilt to the `all_apex_contributions`
432 // metadata module
433 // TODO: When all branches contain this singleton module, make this strict
434 // TODO: Add this dependency only for mainline prebuilts and not every prebuilt module
435 if ctx.OtherModuleExists("all_apex_contributions") {
Jihoon Kanga3a05462024-04-05 00:36:44 +0000436 ctx.AddDependency(m, AcDepTag, "all_apex_contributions")
Spandan Das1c4d94d2023-10-26 22:38:34 +0000437 }
438
Colin Crossce75d2c2016-10-06 16:12:58 -0700439 }
440}
441
Jooyung Hanebaa5732023-05-02 11:43:14 +0900442// checkInvariantsForSourceAndPrebuilt checks if invariants are kept when replacing
443// source with prebuilt. Note that the current module for the context is the source module.
444func checkInvariantsForSourceAndPrebuilt(ctx BaseModuleContext, s, p Module) {
445 if _, ok := s.(OverrideModule); ok {
446 // skip the check when the source module is `override_X` because it's only a placeholder
447 // for the actual source module. The check will be invoked for the actual module.
448 return
449 }
450 if sourcePartition, prebuiltPartition := s.PartitionTag(ctx.DeviceConfig()), p.PartitionTag(ctx.DeviceConfig()); sourcePartition != prebuiltPartition {
451 ctx.OtherModuleErrorf(p, "partition is different: %s(%s) != %s(%s)",
452 sourcePartition, ctx.ModuleName(), prebuiltPartition, ctx.OtherModuleName(p))
453 }
454}
455
Colin Crossc3e7fa62017-03-17 13:14:32 -0700456// PrebuiltSelectModuleMutator marks prebuilts that are used, either overriding source modules or
457// because the source module doesn't exist. It also disables installing overridden source modules.
Spandan Dase3fcb412023-10-26 20:48:02 +0000458//
459// If the visited module is the metadata module `all_apex_contributions`, it sets a
460// provider containing metadata about whether source or prebuilt of mainline modules should be used.
461// This logic was added here to prevent the overhead of creating a new mutator.
Spandan Das1c4d94d2023-10-26 22:38:34 +0000462func PrebuiltSelectModuleMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100463 m := ctx.Module()
464 if p := GetEmbeddedPrebuilt(m); p != nil {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000465 if p.srcsSupplier == nil && p.srcsPropertyName == "" {
Colin Cross74d73e22017-08-02 11:05:49 -0700466 panic(fmt.Errorf("prebuilt module did not have InitPrebuiltModule called on it"))
467 }
468 if !p.properties.SourceExists {
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000469 p.properties.UsePrebuilt = p.usePrebuilt(ctx, nil, m)
Colin Crossc3e7fa62017-03-17 13:14:32 -0700470 }
Spandan Das1c4d94d2023-10-26 22:38:34 +0000471 // Propagate the provider received from `all_apex_contributions`
472 // to the source module
Jihoon Kanga3a05462024-04-05 00:36:44 +0000473 ctx.VisitDirectDepsWithTag(AcDepTag, func(am Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800474 psi, _ := OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider)
475 SetProvider(ctx, PrebuiltSelectionInfoProvider, psi)
Spandan Das1c4d94d2023-10-26 22:38:34 +0000476 })
477
Colin Crossc3e7fa62017-03-17 13:14:32 -0700478 } else if s, ok := ctx.Module().(Module); ok {
Spandan Das3576e762024-01-03 18:57:03 +0000479 // Use `all_apex_contributions` for source vs prebuilt selection.
480 psi := PrebuiltSelectionInfoMap{}
481 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(am Module) {
482 // The value of psi gets overwritten with the provider from the last visited prebuilt.
483 // But all prebuilts have the same value of the provider, so this should be idempontent.
484 psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider)
485 })
Paul Duffinf7c99f52021-04-28 10:41:21 +0100486 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(prebuiltModule Module) {
487 p := GetEmbeddedPrebuilt(prebuiltModule)
488 if p.usePrebuilt(ctx, s, prebuiltModule) {
Jooyung Hanebaa5732023-05-02 11:43:14 +0900489 checkInvariantsForSourceAndPrebuilt(ctx, s, prebuiltModule)
490
Colin Crossee6143c2017-12-30 17:54:27 -0800491 p.properties.UsePrebuilt = true
Liz Kammer5ca3a622020-08-05 15:40:41 -0700492 s.ReplacedByPrebuilt()
Colin Crossa2f296f2016-11-29 15:16:18 -0800493 }
494 })
Spandan Das3576e762024-01-03 18:57:03 +0000495
496 // If any module in this mainline module family has been flagged using apex_contributions, disable every other module in that family
497 // Add source
498 allModules := []Module{s}
499 // Add each prebuilt
500 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(prebuiltModule Module) {
501 allModules = append(allModules, prebuiltModule)
502 })
503 hideUnflaggedModules(ctx, psi, allModules)
504
Colin Crossa2f296f2016-11-29 15:16:18 -0800505 }
Spandan Das3576e762024-01-03 18:57:03 +0000506
Spandan Dase3fcb412023-10-26 20:48:02 +0000507 // If this is `all_apex_contributions`, set a provider containing
508 // metadata about source vs prebuilts selection
509 if am, ok := m.(*allApexContributions); ok {
510 am.SetPrebuiltSelectionInfoProvider(ctx)
511 }
Colin Crossa2f296f2016-11-29 15:16:18 -0800512}
513
Spandan Das3576e762024-01-03 18:57:03 +0000514// If any module in this mainline module family has been flagged using apex_contributions, disable every other module in that family
515func hideUnflaggedModules(ctx BottomUpMutatorContext, psi PrebuiltSelectionInfoMap, allModulesInFamily []Module) {
516 var selectedModuleInFamily Module
517 // query all_apex_contributions to see if any module in this family has been selected
518 for _, moduleInFamily := range allModulesInFamily {
519 // validate that are no duplicates
Spandan Das23956d12024-01-19 00:22:22 +0000520 if isSelected(psi, moduleInFamily) {
Spandan Das3576e762024-01-03 18:57:03 +0000521 if selectedModuleInFamily == nil {
522 // Store this so we can validate that there are no duplicates
523 selectedModuleInFamily = moduleInFamily
524 } else {
525 // There are duplicate modules from the same mainline module family
526 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())
527 }
528 }
529 }
530
531 // If a module has been selected, hide all other modules
532 if selectedModuleInFamily != nil {
533 for _, moduleInFamily := range allModulesInFamily {
534 if moduleInFamily.Name() != selectedModuleInFamily.Name() {
535 moduleInFamily.HideFromMake()
Spandan Dasf2c10572024-02-27 04:49:52 +0000536 // If this is a prebuilt module, unset properties.UsePrebuilt
537 // properties.UsePrebuilt might evaluate to true via soong config var fallback mechanism
538 // Set it to false explicitly so that the following mutator does not replace rdeps to this unselected prebuilt
539 if p := GetEmbeddedPrebuilt(moduleInFamily); p != nil {
540 p.properties.UsePrebuilt = false
541 }
542 }
543 }
544 }
545 // Do a validation pass to make sure that multiple prebuilts of a specific module are not selected.
546 // This might happen if the prebuilts share the same soong config var namespace.
547 // This should be an error, unless one of the prebuilts has been explicitly declared in apex_contributions
548 var selectedPrebuilt Module
549 for _, moduleInFamily := range allModulesInFamily {
550 // Skip if this module is in a different namespace
551 if !moduleInFamily.ExportedToMake() {
552 continue
553 }
554 // Skip for the top-level java_sdk_library_(_import). This has some special cases that need to be addressed first.
555 // This does not run into non-determinism because PrebuiltPostDepsMutator also has the special case
556 if sdkLibrary, ok := moduleInFamily.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil {
557 continue
558 }
559 if p := GetEmbeddedPrebuilt(moduleInFamily); p != nil && p.properties.UsePrebuilt {
560 if selectedPrebuilt == nil {
561 selectedPrebuilt = moduleInFamily
562 } else {
563 ctx.ModuleErrorf("Multiple prebuilt modules %v and %v have been marked as preferred for this source module. "+
564 "Please add the appropriate prebuilt module to apex_contributions for this release config.", selectedPrebuilt.Name(), moduleInFamily.Name())
Spandan Das3576e762024-01-03 18:57:03 +0000565 }
566 }
567 }
568}
569
Jaewoong Jung6158dfe2021-03-23 14:08:29 -0700570// PrebuiltPostDepsMutator replaces dependencies on the source module with dependencies on the
571// prebuilt when both modules exist and the prebuilt should be used. When the prebuilt should not
572// be used, disable installing it.
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700573func PrebuiltPostDepsMutator(ctx BottomUpMutatorContext) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100574 m := ctx.Module()
575 if p := GetEmbeddedPrebuilt(m); p != nil {
Spandan Das3576e762024-01-03 18:57:03 +0000576 bmn, _ := m.(baseModuleName)
577 name := bmn.BaseModuleName()
Spandan Das81d95c52024-02-01 23:41:11 +0000578 psi := PrebuiltSelectionInfoMap{}
Jihoon Kanga3a05462024-04-05 00:36:44 +0000579 ctx.VisitDirectDepsWithTag(AcDepTag, func(am Module) {
Spandan Das81d95c52024-02-01 23:41:11 +0000580 psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider)
581 })
582
Colin Cross74d73e22017-08-02 11:05:49 -0700583 if p.properties.UsePrebuilt {
584 if p.properties.SourceExists {
Paul Duffin80342d72020-06-26 22:08:43 +0100585 ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
Spandan Das81d95c52024-02-01 23:41:11 +0000586 if sdkLibrary, ok := m.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil {
587 // Do not replace deps to the top-level prebuilt java_sdk_library hook.
588 // This hook has been special-cased in #isSelected to be _always_ active, even in next builds
589 // for dexpreopt and hiddenapi processing.
590 // If we do not special-case this here, rdeps referring to a java_sdk_library in next builds via libs
591 // will get prebuilt stubs
592 // TODO (b/308187268): Remove this after the apexes have been added to apex_contributions
Spandan Dasf2c10572024-02-27 04:49:52 +0000593 if psi.IsSelected(name) {
Spandan Das81d95c52024-02-01 23:41:11 +0000594 return false
595 }
596 }
597
Paul Duffin80342d72020-06-26 22:08:43 +0100598 if t, ok := tag.(ReplaceSourceWithPrebuilt); ok {
599 return t.ReplaceSourceWithPrebuilt()
600 }
Paul Duffin80342d72020-06-26 22:08:43 +0100601 return true
602 })
Colin Cross0f3c72f2016-11-23 15:44:07 -0800603 }
604 } else {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800605 m.HideFromMake()
Colin Crossce75d2c2016-10-06 16:12:58 -0700606 }
607 }
608}
609
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000610// A wrapper around PrebuiltSelectionInfoMap.IsSelected with special handling for java_sdk_library
611// java_sdk_library is a macro that creates
612// 1. top-level impl library
613// 2. stub libraries (suffixed with .stubs...)
614//
615// java_sdk_library_import is a macro that creates
616// 1. top-level "impl" library
617// 2. stub libraries (suffixed with .stubs...)
618//
619// 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
620// to provide the jar deapxed from the prebuilt apex
621//
622// isSelected uses `all_apex_contributions` to supersede source vs prebuilts selection of the stub libraries. It does not supersede the
623// selection of the top-level "impl" library so that this hook can work
624//
625// TODO (b/308174306) - Fix this when we need to support multiple prebuilts in main
626func isSelected(psi PrebuiltSelectionInfoMap, m Module) bool {
627 if sdkLibrary, ok := m.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil {
628 sln := proptools.String(sdkLibrary.SdkLibraryName())
Spandan Das23956d12024-01-19 00:22:22 +0000629
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000630 // This is the top-level library
631 // Do not supersede the existing prebuilts vs source selection mechanisms
Spandan Das81d95c52024-02-01 23:41:11 +0000632 // TODO (b/308187268): Remove this after the apexes have been added to apex_contributions
Spandan Das23956d12024-01-19 00:22:22 +0000633 if bmn, ok := m.(baseModuleName); ok && sln == bmn.BaseModuleName() {
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000634 return false
635 }
636
637 // Stub library created by java_sdk_library_import
Spandan Das23956d12024-01-19 00:22:22 +0000638 // java_sdk_library creates several child modules (java_import + prebuilt_stubs_sources) dynamically.
639 // This code block ensures that these child modules are selected if the top-level java_sdk_library_import is listed
640 // in the selected apex_contributions.
641 if javaImport, ok := m.(createdByJavaSdkLibraryName); ok && javaImport.CreatedByJavaSdkLibraryName() != nil {
642 return psi.IsSelected(PrebuiltNameFromSource(proptools.String(javaImport.CreatedByJavaSdkLibraryName())))
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000643 }
644
645 // Stub library created by java_sdk_library
Spandan Das3576e762024-01-03 18:57:03 +0000646 return psi.IsSelected(sln)
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000647 }
Spandan Das3576e762024-01-03 18:57:03 +0000648 return psi.IsSelected(m.Name())
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000649}
650
Spandan Das23956d12024-01-19 00:22:22 +0000651// implemented by child modules of java_sdk_library_import
652type createdByJavaSdkLibraryName interface {
653 CreatedByJavaSdkLibraryName() *string
654}
655
Spandan Das972917d2024-02-27 09:31:51 +0000656// Returns true if the prebuilt variant is disabled
657// e.g. for a cc_prebuilt_library_shared, this will return
658// - true for the static variant of the module
659// - false for the shared variant of the module
660//
661// Even though this is a cc_prebuilt_library_shared, we create both the variants today
662// https://source.corp.google.com/h/googleplex-android/platform/build/soong/+/e08e32b45a18a77bc3c3e751f730539b1b374f1b:cc/library.go;l=2113-2116;drc=2c4a9779cd1921d0397a12b3d3521f4c9b30d747;bpv=1;bpt=0
663func (p *Prebuilt) variantIsDisabled(ctx BaseMutatorContext, prebuilt Module) bool {
664 return p.srcsSupplier != nil && len(p.srcsSupplier(ctx, prebuilt)) == 0
665}
666
Colin Crossa2f296f2016-11-29 15:16:18 -0800667// usePrebuilt returns true if a prebuilt should be used instead of the source module. The prebuilt
668// will be used if it is marked "prefer" or if the source module is disabled.
Spandan Das1c4d94d2023-10-26 22:38:34 +0000669func (p *Prebuilt) usePrebuilt(ctx BaseMutatorContext, source Module, prebuilt Module) bool {
670 // Use `all_apex_contributions` for source vs prebuilt selection.
671 psi := PrebuiltSelectionInfoMap{}
672 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(am Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800673 psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider)
Spandan Das1c4d94d2023-10-26 22:38:34 +0000674 })
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000675
Spandan Das1c4d94d2023-10-26 22:38:34 +0000676 // If the source module is explicitly listed in the metadata module, use that
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000677 if source != nil && isSelected(psi, source) {
Spandan Das1c4d94d2023-10-26 22:38:34 +0000678 return false
679 }
680 // If the prebuilt module is explicitly listed in the metadata module, use that
Spandan Das972917d2024-02-27 09:31:51 +0000681 if isSelected(psi, prebuilt) && !p.variantIsDisabled(ctx, prebuilt) {
Spandan Das1c4d94d2023-10-26 22:38:34 +0000682 return true
683 }
Spandan Dasfc12d2f2023-10-27 20:06:32 +0000684
Spandan Das1c4d94d2023-10-26 22:38:34 +0000685 // If the baseModuleName could not be found in the metadata module,
686 // fall back to the existing source vs prebuilt selection.
687 // TODO: Drop the fallback mechanisms
688
Spandan Das972917d2024-02-27 09:31:51 +0000689 if p.variantIsDisabled(ctx, prebuilt) {
Colin Crossb63d7b32023-12-07 16:54:51 -0800690 return false
691 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700692
Colin Crossb63d7b32023-12-07 16:54:51 -0800693 // Skip prebuilt modules under unexported namespaces so that we won't
694 // end up shadowing non-prebuilt module when prebuilt module under same
695 // name happens to have a `Prefer` property set to true.
696 if ctx.Config().KatiEnabled() && !prebuilt.ExportedToMake() {
697 return false
LuK1337fb545bf2021-01-10 17:47:46 +0100698 }
699
Paul Duffin0c52c7b2021-07-06 17:15:25 +0100700 // If source is not available or is disabled then always use the prebuilt.
Cole Fausta963b942024-04-11 17:43:00 -0700701 if source == nil || !source.Enabled(ctx) {
Colin Crossb63d7b32023-12-07 16:54:51 -0800702 return true
Colin Crossa2f296f2016-11-29 15:16:18 -0800703 }
704
Paul Duffin0c52c7b2021-07-06 17:15:25 +0100705 // TODO: use p.Properties.Name and ctx.ModuleDir to override preference
Cole Faust12ff57d2024-07-30 13:17:28 -0700706 return p.properties.Prefer.GetOrDefault(ctx, false)
Colin Crossce75d2c2016-10-06 16:12:58 -0700707}
Jiyong Park0a573d72019-07-07 12:39:16 +0900708
709func (p *Prebuilt) SourceExists() bool {
710 return p.properties.SourceExists
711}