blob: bf9737aa2c464998f52babff89d029a7480bac43 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6ff51382015-12-17 16:39:19 -080018 "fmt"
Bob Badour4101c712022-02-09 11:54:35 -080019 "net/url"
Colin Cross988414c2020-01-11 01:11:46 +000020 "os"
Alex Lightfb4353d2019-01-17 13:57:45 -080021 "path"
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "path/filepath"
Liz Kammer9525e712022-01-05 13:46:24 -050023 "reflect"
Jiyong Park1c7e9622020-05-07 16:12:13 +090024 "regexp"
Bob Badour4101c712022-02-09 11:54:35 -080025 "sort"
Colin Cross6ff51382015-12-17 16:39:19 -080026 "strings"
Colin Crossaabf6792017-11-29 00:27:14 -080027 "text/scanner"
Colin Crossf6566ed2015-03-24 11:13:38 -070028
Paul Duffinb42fa672021-09-09 16:37:49 +010029 "android/soong/bazel"
Colin Crossf6566ed2015-03-24 11:13:38 -070030 "github.com/google/blueprint"
Colin Crossfe4bc362018-09-12 10:02:13 -070031 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
34var (
35 DeviceSharedLibrary = "shared_library"
36 DeviceStaticLibrary = "static_library"
Colin Cross3f40fa42015-01-30 17:27:36 -080037)
38
Colin Crossae887032017-10-23 17:16:14 -070039type BuildParams struct {
Dan Willemsen9f3c5742016-11-03 14:28:31 -070040 Rule blueprint.Rule
Colin Cross33bfb0a2016-11-21 17:23:08 -080041 Deps blueprint.Deps
42 Depfile WritablePath
Colin Cross67a5c132017-05-09 13:45:28 -070043 Description string
Dan Willemsen9f3c5742016-11-03 14:28:31 -070044 Output WritablePath
45 Outputs WritablePaths
Jingwen Chence679d22020-09-23 04:30:02 +000046 SymlinkOutput WritablePath
47 SymlinkOutputs WritablePaths
Dan Willemsen9f3c5742016-11-03 14:28:31 -070048 ImplicitOutput WritablePath
49 ImplicitOutputs WritablePaths
50 Input Path
51 Inputs Paths
52 Implicit Path
53 Implicits Paths
54 OrderOnly Paths
Colin Cross824f1162020-07-16 13:07:51 -070055 Validation Path
56 Validations Paths
Dan Willemsen9f3c5742016-11-03 14:28:31 -070057 Default bool
58 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070059}
60
Colin Crossae887032017-10-23 17:16:14 -070061type ModuleBuildParams BuildParams
62
Colin Cross1184b642019-12-30 18:43:07 -080063// EarlyModuleContext provides methods that can be called early, as soon as the properties have
64// been parsed into the module and before any mutators have run.
65type EarlyModuleContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -070066 // Module returns the current module as a Module. It should rarely be necessary, as the module already has a
67 // reference to itself.
Colin Cross1184b642019-12-30 18:43:07 -080068 Module() Module
Colin Cross9f35c3d2020-09-16 19:04:41 -070069
70 // ModuleName returns the name of the module. This is generally the value that was returned by Module.Name() when
71 // the module was created, but may have been modified by calls to BaseMutatorContext.Rename.
Colin Cross1184b642019-12-30 18:43:07 -080072 ModuleName() string
Colin Cross9f35c3d2020-09-16 19:04:41 -070073
74 // ModuleDir returns the path to the directory that contains the definition of the module.
Colin Cross1184b642019-12-30 18:43:07 -080075 ModuleDir() string
Colin Cross9f35c3d2020-09-16 19:04:41 -070076
77 // ModuleType returns the name of the module type that was used to create the module, as specified in
78 // RegisterModuleType.
Colin Cross1184b642019-12-30 18:43:07 -080079 ModuleType() string
Colin Cross9f35c3d2020-09-16 19:04:41 -070080
81 // BlueprintFile returns the name of the blueprint file that contains the definition of this
82 // module.
Colin Cross9d34f352019-11-22 16:03:51 -080083 BlueprintsFile() string
Colin Cross1184b642019-12-30 18:43:07 -080084
Colin Cross9f35c3d2020-09-16 19:04:41 -070085 // ContainsProperty returns true if the specified property name was set in the module definition.
Colin Cross1184b642019-12-30 18:43:07 -080086 ContainsProperty(name string) bool
Colin Cross9f35c3d2020-09-16 19:04:41 -070087
88 // Errorf reports an error at the specified position of the module definition file.
Colin Cross1184b642019-12-30 18:43:07 -080089 Errorf(pos scanner.Position, fmt string, args ...interface{})
Colin Cross9f35c3d2020-09-16 19:04:41 -070090
91 // ModuleErrorf reports an error at the line number of the module type in the module definition.
Colin Cross1184b642019-12-30 18:43:07 -080092 ModuleErrorf(fmt string, args ...interface{})
Colin Cross9f35c3d2020-09-16 19:04:41 -070093
94 // PropertyErrorf reports an error at the line number of a property in the module definition.
Colin Cross1184b642019-12-30 18:43:07 -080095 PropertyErrorf(property, fmt string, args ...interface{})
Colin Cross9f35c3d2020-09-16 19:04:41 -070096
97 // Failed returns true if any errors have been reported. In most cases the module can continue with generating
98 // build rules after an error, allowing it to report additional errors in a single run, but in cases where the error
99 // has prevented the module from creating necessary data it can return early when Failed returns true.
Colin Cross1184b642019-12-30 18:43:07 -0800100 Failed() bool
101
Colin Cross9f35c3d2020-09-16 19:04:41 -0700102 // AddNinjaFileDeps adds dependencies on the specified files to the rule that creates the ninja manifest. The
103 // primary builder will be rerun whenever the specified files are modified.
Colin Cross1184b642019-12-30 18:43:07 -0800104 AddNinjaFileDeps(deps ...string)
105
106 DeviceSpecific() bool
107 SocSpecific() bool
108 ProductSpecific() bool
109 SystemExtSpecific() bool
110 Platform() bool
111
112 Config() Config
113 DeviceConfig() DeviceConfig
114
115 // Deprecated: use Config()
116 AConfig() Config
117
118 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
119 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
120 // builder whenever a file matching the pattern as added or removed, without rerunning if a
121 // file that does not match the pattern is added to a searched directory.
122 GlobWithDeps(pattern string, excludes []string) ([]string, error)
123
124 Glob(globPattern string, excludes []string) Paths
125 GlobFiles(globPattern string, excludes []string) Paths
Colin Cross988414c2020-01-11 01:11:46 +0000126 IsSymlink(path Path) bool
127 Readlink(path Path) string
Colin Cross133ebef2020-08-14 17:38:45 -0700128
Colin Cross9f35c3d2020-09-16 19:04:41 -0700129 // Namespace returns the Namespace object provided by the NameInterface set by Context.SetNameInterface, or the
130 // default SimpleNameInterface if Context.SetNameInterface was not called.
Colin Cross133ebef2020-08-14 17:38:45 -0700131 Namespace() *Namespace
Colin Cross1184b642019-12-30 18:43:07 -0800132}
133
Colin Cross0ea8ba82019-06-06 14:33:29 -0700134// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
Colin Crossdc35e212019-06-06 16:13:11 -0700135// a Config instead of an interface{}, and some methods have been wrapped to use an android.Module
136// instead of a blueprint.Module, plus some extra methods that return Android-specific information
Colin Cross0ea8ba82019-06-06 14:33:29 -0700137// about the current module.
138type BaseModuleContext interface {
Colin Cross1184b642019-12-30 18:43:07 -0800139 EarlyModuleContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700140
Paul Duffinf88d8e02020-05-07 20:21:34 +0100141 blueprintBaseModuleContext() blueprint.BaseModuleContext
142
Colin Cross9f35c3d2020-09-16 19:04:41 -0700143 // OtherModuleName returns the name of another Module. See BaseModuleContext.ModuleName for more information.
144 // It is intended for use inside the visit functions of Visit* and WalkDeps.
Colin Crossdc35e212019-06-06 16:13:11 -0700145 OtherModuleName(m blueprint.Module) string
Colin Cross9f35c3d2020-09-16 19:04:41 -0700146
147 // OtherModuleDir returns the directory of another Module. See BaseModuleContext.ModuleDir for more information.
148 // It is intended for use inside the visit functions of Visit* and WalkDeps.
Colin Crossdc35e212019-06-06 16:13:11 -0700149 OtherModuleDir(m blueprint.Module) string
Colin Cross9f35c3d2020-09-16 19:04:41 -0700150
151 // OtherModuleErrorf reports an error on another Module. See BaseModuleContext.ModuleErrorf for more information.
152 // It is intended for use inside the visit functions of Visit* and WalkDeps.
Colin Crossdc35e212019-06-06 16:13:11 -0700153 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
Colin Cross9f35c3d2020-09-16 19:04:41 -0700154
155 // OtherModuleDependencyTag returns the dependency tag used to depend on a module, or nil if there is no dependency
156 // on the module. When called inside a Visit* method with current module being visited, and there are multiple
157 // dependencies on the module being visited, it returns the dependency tag used for the current dependency.
Colin Crossdc35e212019-06-06 16:13:11 -0700158 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
Colin Cross9f35c3d2020-09-16 19:04:41 -0700159
160 // OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface
161 // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
Colin Crossdc35e212019-06-06 16:13:11 -0700162 OtherModuleExists(name string) bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700163
164 // OtherModuleDependencyVariantExists returns true if a module with the
165 // specified name and variant exists. The variant must match the given
166 // variations. It must also match all the non-local variations of the current
Martin Stjernholm408ffd82021-05-05 15:27:31 +0100167 // module. In other words, it checks for the module that AddVariationDependencies
Colin Cross9f35c3d2020-09-16 19:04:41 -0700168 // would add a dependency on with the same arguments.
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000169 OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700170
Martin Stjernholm408ffd82021-05-05 15:27:31 +0100171 // OtherModuleFarDependencyVariantExists returns true if a module with the
172 // specified name and variant exists. The variant must match the given
173 // variations, but not the non-local variations of the current module. In
174 // other words, it checks for the module that AddFarVariationDependencies
175 // would add a dependency on with the same arguments.
176 OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool
177
Colin Cross9f35c3d2020-09-16 19:04:41 -0700178 // OtherModuleReverseDependencyVariantExists returns true if a module with the
179 // specified name exists with the same variations as the current module. In
Martin Stjernholm408ffd82021-05-05 15:27:31 +0100180 // other words, it checks for the module that AddReverseDependency would add a
Colin Cross9f35c3d2020-09-16 19:04:41 -0700181 // dependency on with the same argument.
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000182 OtherModuleReverseDependencyVariantExists(name string) bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700183
184 // OtherModuleType returns the type of another Module. See BaseModuleContext.ModuleType for more information.
185 // It is intended for use inside the visit functions of Visit* and WalkDeps.
Jiyong Park9e6c2422019-08-09 20:39:45 +0900186 OtherModuleType(m blueprint.Module) string
Colin Crossdc35e212019-06-06 16:13:11 -0700187
Colin Crossd27e7b82020-07-02 11:38:17 -0700188 // OtherModuleProvider returns the value for a provider for the given module. If the value is
189 // not set it returns the zero value of the type of the provider, so the return value can always
190 // be type asserted to the type of the provider. The value returned may be a deep copy of the
191 // value originally passed to SetProvider.
192 OtherModuleProvider(m blueprint.Module, provider blueprint.ProviderKey) interface{}
193
194 // OtherModuleHasProvider returns true if the provider for the given module has been set.
195 OtherModuleHasProvider(m blueprint.Module, provider blueprint.ProviderKey) bool
196
197 // Provider returns the value for a provider for the current module. If the value is
198 // not set it returns the zero value of the type of the provider, so the return value can always
199 // be type asserted to the type of the provider. It panics if called before the appropriate
200 // mutator or GenerateBuildActions pass for the provider. The value returned may be a deep
201 // copy of the value originally passed to SetProvider.
202 Provider(provider blueprint.ProviderKey) interface{}
203
204 // HasProvider returns true if the provider for the current module has been set.
205 HasProvider(provider blueprint.ProviderKey) bool
206
207 // SetProvider sets the value for a provider for the current module. It panics if not called
208 // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value
209 // is not of the appropriate type, or if the value has already been set. The value should not
210 // be modified after being passed to SetProvider.
211 SetProvider(provider blueprint.ProviderKey, value interface{})
212
Colin Crossdc35e212019-06-06 16:13:11 -0700213 GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700214
215 // GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if
216 // none exists. It panics if the dependency does not have the specified tag. It skips any
217 // dependencies that are not an android.Module.
Colin Crossdc35e212019-06-06 16:13:11 -0700218 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700219
220 // GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
221 // name, or nil if none exists. If there are multiple dependencies on the same module it returns
Liz Kammer2b50ce62021-04-26 15:47:28 -0400222 // the first DependencyTag.
Colin Crossdc35e212019-06-06 16:13:11 -0700223 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
224
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400225 ModuleFromName(name string) (blueprint.Module, bool)
226
Colin Cross9f35c3d2020-09-16 19:04:41 -0700227 // VisitDirectDepsBlueprint calls visit for each direct dependency. If there are multiple
228 // direct dependencies on the same module visit will be called multiple times on that module
229 // and OtherModuleDependencyTag will return a different tag for each.
230 //
231 // The Module passed to the visit function should not be retained outside of the visit
232 // function, it may be invalidated by future mutators.
Colin Crossdc35e212019-06-06 16:13:11 -0700233 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Cross9f35c3d2020-09-16 19:04:41 -0700234
235 // VisitDirectDeps calls visit for each direct dependency. If there are multiple
236 // direct dependencies on the same module visit will be called multiple times on that module
Spandan Dasda7f3622021-08-04 20:50:04 +0000237 // and OtherModuleDependencyTag will return a different tag for each. It raises an error if any of the
238 // dependencies are not an android.Module.
Colin Cross9f35c3d2020-09-16 19:04:41 -0700239 //
240 // The Module passed to the visit function should not be retained outside of the visit
241 // function, it may be invalidated by future mutators.
Colin Crossdc35e212019-06-06 16:13:11 -0700242 VisitDirectDeps(visit func(Module))
Colin Cross9f35c3d2020-09-16 19:04:41 -0700243
Colin Crossdc35e212019-06-06 16:13:11 -0700244 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Cross9f35c3d2020-09-16 19:04:41 -0700245
246 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
247 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
248 // OtherModuleDependencyTag will return a different tag for each. It skips any
249 // dependencies that are not an android.Module.
250 //
251 // The Module passed to the visit function should not be retained outside of the visit function, it may be
252 // invalidated by future mutators.
Colin Crossdc35e212019-06-06 16:13:11 -0700253 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
254 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
255 VisitDepsDepthFirst(visit func(Module))
256 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
257 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
Colin Cross9f35c3d2020-09-16 19:04:41 -0700258
259 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
260 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
261 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
262 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
263 // any dependencies that are not an android.Module.
264 //
265 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
266 // invalidated by future mutators.
Usta6b1ffa42021-12-15 12:45:49 -0500267 WalkDeps(visit func(child, parent Module) bool)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700268
269 // WalkDepsBlueprint calls visit for each transitive dependency, traversing the dependency
270 // tree in top down order. visit may be called multiple times for the same (child, parent)
271 // pair if there are multiple direct dependencies between the child and parent with different
272 // tags. OtherModuleDependencyTag will return the tag for the currently visited
273 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down
274 // to child.
275 //
276 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
277 // invalidated by future mutators.
Colin Crossdc35e212019-06-06 16:13:11 -0700278 WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700279
Colin Crossdc35e212019-06-06 16:13:11 -0700280 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
281 // and returns a top-down dependency path from a start module to current child module.
282 GetWalkPath() []Module
283
Colin Cross4dfacf92020-09-16 19:22:27 -0700284 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
285 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
286 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
287 // only done once for all variants of a module.
288 PrimaryModule() Module
289
290 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
291 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
292 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
293 // singleton actions that are only done once for all variants of a module.
294 FinalModule() Module
295
296 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
297 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
298 // from all variants if the current module == FinalModule(). Otherwise, care must be taken to not access any
299 // data modified by the current mutator.
300 VisitAllModuleVariants(visit func(Module))
301
Paul Duffinc5192442020-03-31 11:31:36 +0100302 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
303 // and returns a top-down dependency tags path from a start module to current child module.
304 // It has one less entry than GetWalkPath() as it contains the dependency tags that
305 // exist between each adjacent pair of modules in the GetWalkPath().
306 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
307 GetTagPath() []blueprint.DependencyTag
308
Jiyong Park1c7e9622020-05-07 16:12:13 +0900309 // GetPathString is supposed to be called in visit function passed in WalkDeps()
310 // and returns a multi-line string showing the modules and dependency tags
311 // among them along the top-down dependency path from a start module to current child module.
312 // skipFirst when set to true, the output doesn't include the start module,
313 // which is already printed when this function is used along with ModuleErrorf().
314 GetPathString(skipFirst bool) string
315
Colin Crossdc35e212019-06-06 16:13:11 -0700316 AddMissingDependencies(missingDeps []string)
317
Liz Kammer6eff3232021-08-26 08:37:59 -0400318 // AddUnconvertedBp2buildDep stores module name of a direct dependency that was not converted via bp2build
319 AddUnconvertedBp2buildDep(dep string)
320
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500321 // AddMissingBp2buildDep stores the module name of a direct dependency that was not found.
322 AddMissingBp2buildDep(dep string)
323
Colin Crossa1ad8d12016-06-01 17:09:44 -0700324 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -0700325 TargetPrimary() bool
Paul Duffinca7f0ef2020-02-25 15:50:49 +0000326
327 // The additional arch specific targets (e.g. 32/64 bit) that this module variant is
328 // responsible for creating.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700329 MultiTargets() []Target
Colin Crossf6566ed2015-03-24 11:13:38 -0700330 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -0700331 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -0700332 Host() bool
333 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -0700334 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -0700335 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -0700336 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -0700337 PrimaryArch() bool
Colin Crossf6566ed2015-03-24 11:13:38 -0700338}
339
Colin Cross1184b642019-12-30 18:43:07 -0800340// Deprecated: use EarlyModuleContext instead
Colin Cross635c3b02016-05-18 15:37:25 -0700341type BaseContext interface {
Colin Cross1184b642019-12-30 18:43:07 -0800342 EarlyModuleContext
Colin Crossaabf6792017-11-29 00:27:14 -0800343}
344
Colin Cross635c3b02016-05-18 15:37:25 -0700345type ModuleContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800346 BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -0800347
Colin Crossc20dc852020-11-10 12:27:45 -0800348 blueprintModuleContext() blueprint.ModuleContext
349
Colin Crossae887032017-10-23 17:16:14 -0700350 // Deprecated: use ModuleContext.Build instead.
Colin Cross0875c522017-11-28 17:34:01 -0800351 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -0700352
Paul Duffind5cf92e2021-07-09 17:38:55 +0100353 // Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
354 // be tagged with `android:"path" to support automatic source module dependency resolution.
355 //
356 // Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700357 ExpandSources(srcFiles, excludes []string) Paths
Paul Duffind5cf92e2021-07-09 17:38:55 +0100358
359 // Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
360 // be tagged with `android:"path" to support automatic source module dependency resolution.
361 //
362 // Deprecated: use PathForModuleSrc instead.
Colin Cross366938f2017-12-11 16:29:02 -0800363 ExpandSource(srcFile, prop string) Path
Paul Duffind5cf92e2021-07-09 17:38:55 +0100364
Colin Cross2383f3b2018-02-06 14:40:13 -0800365 ExpandOptionalSource(srcFile *string, prop string) OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700366
Colin Cross41589502020-12-01 14:00:21 -0800367 // InstallExecutable creates a rule to copy srcPath to name in the installPath directory,
368 // with the given additional dependencies. The file is marked executable after copying.
369 //
370 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
371 // installed file will be returned by PackagingSpecs() on this module or by
372 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
373 // for which IsInstallDepNeeded returns true.
Colin Cross70dda7e2019-10-01 22:05:35 -0700374 InstallExecutable(installPath InstallPath, name string, srcPath Path, deps ...Path) InstallPath
Colin Cross41589502020-12-01 14:00:21 -0800375
376 // InstallFile creates a rule to copy srcPath to name in the installPath directory,
377 // with the given additional dependencies.
378 //
379 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
380 // installed file will be returned by PackagingSpecs() on this module or by
381 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
382 // for which IsInstallDepNeeded returns true.
Colin Cross70dda7e2019-10-01 22:05:35 -0700383 InstallFile(installPath InstallPath, name string, srcPath Path, deps ...Path) InstallPath
Colin Cross41589502020-12-01 14:00:21 -0800384
Colin Cross50ed1f92021-11-12 17:41:02 -0800385 // InstallFileWithExtraFilesZip creates a rule to copy srcPath to name in the installPath
386 // directory, and also unzip a zip file containing extra files to install into the same
387 // directory.
388 //
389 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
390 // installed file will be returned by PackagingSpecs() on this module or by
391 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
392 // for which IsInstallDepNeeded returns true.
393 InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path, extraZip Path, deps ...Path) InstallPath
394
Colin Cross41589502020-12-01 14:00:21 -0800395 // InstallSymlink creates a rule to create a symlink from src srcPath to name in the installPath
396 // directory.
397 //
398 // The installed symlink will be returned by FilesToInstall(), and the PackagingSpec for the
399 // installed file will be returned by PackagingSpecs() on this module or by
400 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
401 // for which IsInstallDepNeeded returns true.
Colin Cross70dda7e2019-10-01 22:05:35 -0700402 InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath
Colin Cross41589502020-12-01 14:00:21 -0800403
404 // InstallAbsoluteSymlink creates a rule to create an absolute symlink from src srcPath to name
405 // in the installPath directory.
406 //
407 // The installed symlink will be returned by FilesToInstall(), and the PackagingSpec for the
408 // installed file will be returned by PackagingSpecs() on this module or by
409 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
410 // for which IsInstallDepNeeded returns true.
Colin Cross70dda7e2019-10-01 22:05:35 -0700411 InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath
Colin Cross41589502020-12-01 14:00:21 -0800412
413 // PackageFile creates a PackagingSpec as if InstallFile was called, but without creating
414 // the rule to copy the file. This is useful to define how a module would be packaged
415 // without installing it into the global installation directories.
416 //
417 // The created PackagingSpec for the will be returned by PackagingSpecs() on this module or by
418 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
419 // for which IsInstallDepNeeded returns true.
420 PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec
421
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700422 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800423
Colin Cross8d8f8e22016-08-03 11:57:50 -0700424 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700425 InstallInTestcases() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700426 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800427 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700428 InstallInVendorRamdisk() bool
Inseob Kim08758f02021-04-08 21:13:22 +0900429 InstallInDebugRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900430 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700431 InstallInRoot() bool
Kiyoung Kimae11c232021-07-19 11:38:04 +0900432 InstallInVendor() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900433 InstallForceOS() (*OsType, *ArchType)
Nan Zhang6d34b302017-02-04 17:47:46 -0800434
435 RequiredModuleNames() []string
Sasha Smundakb6d23052019-04-01 18:37:36 -0700436 HostRequiredModuleNames() []string
437 TargetRequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -0700438
Colin Cross3f68a132017-10-23 17:10:29 -0700439 ModuleSubDir() string
440
Colin Cross0875c522017-11-28 17:34:01 -0800441 Variable(pctx PackageContext, name, value string)
442 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700443 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
444 // and performs more verification.
Colin Cross0875c522017-11-28 17:34:01 -0800445 Build(pctx PackageContext, params BuildParams)
Colin Crossc3d87d32020-06-04 13:25:17 -0700446 // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
447 // phony rules or real files. Phony can be called on the same name multiple times to add
448 // additional dependencies.
449 Phony(phony string, deps ...Path)
Colin Cross3f68a132017-10-23 17:10:29 -0700450
Colin Cross9f35c3d2020-09-16 19:04:41 -0700451 // GetMissingDependencies returns the list of dependencies that were passed to AddDependencies or related methods,
452 // but do not exist.
Colin Cross3f68a132017-10-23 17:10:29 -0700453 GetMissingDependencies() []string
Colin Crosse7fe0962022-03-15 17:49:24 -0700454
455 // LicenseMetadataFile returns the path where the license metadata for this module will be
456 // generated.
457 LicenseMetadataFile() Path
Colin Cross3f40fa42015-01-30 17:27:36 -0800458}
459
Colin Cross635c3b02016-05-18 15:37:25 -0700460type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800461 blueprint.Module
462
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700463 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
464 // but GenerateAndroidBuildActions also has access to Android-specific information.
465 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700466 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700467
Paul Duffin44f1d842020-06-26 20:17:02 +0100468 // Add dependencies to the components of a module, i.e. modules that are created
469 // by the module and which are considered to be part of the creating module.
470 //
471 // This is called before prebuilts are renamed so as to allow a dependency to be
472 // added directly to a prebuilt child module instead of depending on a source module
473 // and relying on prebuilt processing to switch to the prebuilt module if preferred.
474 //
475 // A dependency on a prebuilt must include the "prebuilt_" prefix.
476 ComponentDepsMutator(ctx BottomUpMutatorContext)
477
Colin Cross1e676be2016-10-12 14:38:15 -0700478 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800479
Colin Cross635c3b02016-05-18 15:37:25 -0700480 base() *ModuleBase
Inseob Kimeec88e12020-01-22 11:11:29 +0900481 Disable()
Dan Willemsen0effe062015-11-30 16:06:01 -0800482 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700483 Target() Target
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000484 MultiTargets() []Target
Paul Duffinb42fa672021-09-09 16:37:49 +0100485
486 // ImageVariation returns the image variation of this module.
487 //
488 // The returned structure has its Mutator field set to "image" and its Variation field set to the
489 // image variation, e.g. recovery, ramdisk, etc.. The Variation field is "" for host modules and
490 // device modules that have no image variation.
491 ImageVariation() blueprint.Variation
492
Anton Hansson1ee62c02020-06-30 11:51:53 +0100493 Owner() string
Dan Willemsen782a2d12015-12-21 14:55:28 -0800494 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700495 InstallInTestcases() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700496 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800497 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700498 InstallInVendorRamdisk() bool
Inseob Kim08758f02021-04-08 21:13:22 +0900499 InstallInDebugRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900500 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700501 InstallInRoot() bool
Kiyoung Kimae11c232021-07-19 11:38:04 +0900502 InstallInVendor() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900503 InstallForceOS() (*OsType, *ArchType)
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800504 HideFromMake()
505 IsHideFromMake() bool
Martin Stjernholm9e7f45e2020-12-23 03:50:30 +0000506 IsSkipInstall() bool
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100507 MakeUninstallable()
Liz Kammer5ca3a622020-08-05 15:40:41 -0700508 ReplacedByPrebuilt()
509 IsReplacedByPrebuilt() bool
Jiyong Park374510b2018-03-19 18:23:01 +0900510 ExportedToMake() bool
Inseob Kim8471cda2019-11-15 09:59:12 +0900511 InitRc() Paths
512 VintfFragments() Paths
Justin Yun885a7de2021-06-29 20:34:53 +0900513 EffectiveLicenseFiles() Paths
Colin Cross36242852017-06-23 15:06:31 -0700514
515 AddProperties(props ...interface{})
516 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700517
Liz Kammer2ada09a2021-08-11 00:17:36 -0400518 // IsConvertedByBp2build returns whether this module was converted via bp2build
519 IsConvertedByBp2build() bool
520 // Bp2buildTargets returns the target(s) generated for Bazel via bp2build for this module
521 Bp2buildTargets() []bp2buildInfo
Liz Kammer6eff3232021-08-26 08:37:59 -0400522 GetUnconvertedBp2buildDeps() []string
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500523 GetMissingBp2buildDeps() []string
Liz Kammer2ada09a2021-08-11 00:17:36 -0400524
Colin Crossae887032017-10-23 17:16:14 -0700525 BuildParamsForTests() []BuildParams
Colin Cross4c83e5c2019-02-25 14:54:28 -0800526 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800527 VariablesForTests() map[string]string
Paul Duffine2453c72019-05-31 14:00:04 +0100528
Colin Cross9a362232019-07-01 15:32:45 -0700529 // String returns a string that includes the module name and variants for printing during debugging.
530 String() string
531
Paul Duffine2453c72019-05-31 14:00:04 +0100532 // Get the qualified module id for this module.
533 qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName
534
535 // Get information about the properties that can contain visibility rules.
536 visibilityProperties() []visibilityProperty
Paul Duffin63c6e182019-07-24 14:24:38 +0100537
Jiyong Park6a8cf5f2019-12-30 16:31:09 +0900538 RequiredModuleNames() []string
539 HostRequiredModuleNames() []string
540 TargetRequiredModuleNames() []string
Colin Cross897266e2020-02-13 13:22:08 -0800541
Jiyong Park4dc2a1a2020-09-28 17:46:22 +0900542 FilesToInstall() InstallPaths
Jiyong Park073ea552020-11-09 14:08:34 +0900543 PackagingSpecs() []PackagingSpec
Colin Crossffe6b9d2020-12-01 15:40:06 -0800544
545 // TransitivePackagingSpecs returns the PackagingSpecs for this module and any transitive
546 // dependencies with dependency tags for which IsInstallDepNeeded() returns true.
547 TransitivePackagingSpecs() []PackagingSpec
Paul Duffine2453c72019-05-31 14:00:04 +0100548}
549
550// Qualified id for a module
551type qualifiedModuleName struct {
552 // The package (i.e. directory) in which the module is defined, without trailing /
553 pkg string
554
555 // The name of the module, empty string if package.
556 name string
557}
558
559func (q qualifiedModuleName) String() string {
560 if q.name == "" {
561 return "//" + q.pkg
562 }
563 return "//" + q.pkg + ":" + q.name
564}
565
Paul Duffine484f472019-06-20 16:38:08 +0100566func (q qualifiedModuleName) isRootPackage() bool {
567 return q.pkg == "" && q.name == ""
568}
569
Paul Duffine2453c72019-05-31 14:00:04 +0100570// Get the id for the package containing this module.
571func (q qualifiedModuleName) getContainingPackageId() qualifiedModuleName {
572 pkg := q.pkg
573 if q.name == "" {
Paul Duffine484f472019-06-20 16:38:08 +0100574 if pkg == "" {
575 panic(fmt.Errorf("Cannot get containing package id of root package"))
576 }
577
578 index := strings.LastIndex(pkg, "/")
579 if index == -1 {
580 pkg = ""
581 } else {
582 pkg = pkg[:index]
583 }
Paul Duffine2453c72019-05-31 14:00:04 +0100584 }
585 return newPackageId(pkg)
586}
587
588func newPackageId(pkg string) qualifiedModuleName {
589 // A qualified id for a package module has no name.
590 return qualifiedModuleName{pkg: pkg, name: ""}
Colin Cross3f40fa42015-01-30 17:27:36 -0800591}
592
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000593type Dist struct {
594 // Copy the output of this module to the $DIST_DIR when `dist` is specified on the
595 // command line and any of these targets are also on the command line, or otherwise
596 // built
597 Targets []string `android:"arch_variant"`
598
599 // The name of the output artifact. This defaults to the basename of the output of
600 // the module.
601 Dest *string `android:"arch_variant"`
602
603 // The directory within the dist directory to store the artifact. Defaults to the
604 // top level directory ("").
605 Dir *string `android:"arch_variant"`
606
607 // A suffix to add to the artifact file name (before any extension).
608 Suffix *string `android:"arch_variant"`
609
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000610 // If true, then the artifact file will be appended with _<product name>. For
611 // example, if the product is coral and the module is an android_app module
612 // of name foo, then the artifact would be foo_coral.apk. If false, there is
613 // no change to the artifact file name.
614 Append_artifact_with_product *bool `android:"arch_variant"`
615
Paul Duffin74f05592020-11-25 16:37:46 +0000616 // A string tag to select the OutputFiles associated with the tag.
617 //
618 // If no tag is specified then it will select the default dist paths provided
619 // by the module type. If a tag of "" is specified then it will return the
620 // default output files provided by the modules, i.e. the result of calling
621 // OutputFiles("").
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000622 Tag *string `android:"arch_variant"`
623}
624
Bob Badour4101c712022-02-09 11:54:35 -0800625// NamedPath associates a path with a name. e.g. a license text path with a package name
626type NamedPath struct {
627 Path Path
628 Name string
629}
630
631// String returns an escaped string representing the `NamedPath`.
632func (p NamedPath) String() string {
633 if len(p.Name) > 0 {
634 return p.Path.String() + ":" + url.QueryEscape(p.Name)
635 }
636 return p.Path.String()
637}
638
639// NamedPaths describes a list of paths each associated with a name.
640type NamedPaths []NamedPath
641
642// Strings returns a list of escaped strings representing each `NamedPath` in the list.
643func (l NamedPaths) Strings() []string {
644 result := make([]string, 0, len(l))
645 for _, p := range l {
646 result = append(result, p.String())
647 }
648 return result
649}
650
651// SortedUniqueNamedPaths modifies `l` in place to return the sorted unique subset.
652func SortedUniqueNamedPaths(l NamedPaths) NamedPaths {
653 if len(l) == 0 {
654 return l
655 }
656 sort.Slice(l, func(i, j int) bool {
657 return l[i].String() < l[j].String()
658 })
659 k := 0
660 for i := 1; i < len(l); i++ {
661 if l[i].String() == l[k].String() {
662 continue
663 }
664 k++
665 if k < i {
666 l[k] = l[i]
667 }
668 }
669 return l[:k+1]
670}
671
Colin Crossfc754582016-05-17 16:34:16 -0700672type nameProperties struct {
673 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800674 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700675}
676
Colin Cross08d6f8f2020-11-19 02:33:19 +0000677type commonProperties struct {
Dan Willemsen0effe062015-11-30 16:06:01 -0800678 // emit build rules for this module
Paul Duffin54d9bb72020-02-12 10:20:56 +0000679 //
680 // Disabling a module should only be done for those modules that cannot be built
681 // in the current environment. Modules that can build in the current environment
682 // but are not usually required (e.g. superceded by a prebuilt) should not be
683 // disabled as that will prevent them from being built by the checkbuild target
684 // and so prevent early detection of changes that have broken those modules.
Dan Willemsen0effe062015-11-30 16:06:01 -0800685 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800686
Paul Duffin2e61fa62019-03-28 14:10:57 +0000687 // Controls the visibility of this module to other modules. Allowable values are one or more of
688 // these formats:
689 //
690 // ["//visibility:public"]: Anyone can use this module.
691 // ["//visibility:private"]: Only rules in the module's package (not its subpackages) can use
692 // this module.
Paul Duffin51084ff2020-05-05 19:19:22 +0100693 // ["//visibility:override"]: Discards any rules inherited from defaults or a creating module.
694 // Can only be used at the beginning of a list of visibility rules.
Paul Duffin2e61fa62019-03-28 14:10:57 +0000695 // ["//some/package:__pkg__", "//other/package:__pkg__"]: Only modules in some/package and
696 // other/package (defined in some/package/*.bp and other/package/*.bp) have access to
697 // this module. Note that sub-packages do not have access to the rule; for example,
698 // //some/package/foo:bar or //other/package/testing:bla wouldn't have access. __pkg__
699 // is a special module and must be used verbatim. It represents all of the modules in the
700 // package.
701 // ["//project:__subpackages__", "//other:__subpackages__"]: Only modules in packages project
702 // or other or in one of their sub-packages have access to this module. For example,
703 // //project:rule, //project/library:lib or //other/testing/internal:munge are allowed
704 // to depend on this rule (but not //independent:evil)
705 // ["//project"]: This is shorthand for ["//project:__pkg__"]
706 // [":__subpackages__"]: This is shorthand for ["//project:__subpackages__"] where
707 // //project is the module's package. e.g. using [":__subpackages__"] in
708 // packages/apps/Settings/Android.bp is equivalent to
709 // //packages/apps/Settings:__subpackages__.
710 // ["//visibility:legacy_public"]: The default visibility, behaves as //visibility:public
711 // for now. It is an error if it is used in a module.
Paul Duffine2453c72019-05-31 14:00:04 +0100712 //
713 // If a module does not specify the `visibility` property then it uses the
714 // `default_visibility` property of the `package` module in the module's package.
715 //
716 // If the `default_visibility` property is not set for the module's package then
Paul Duffine484f472019-06-20 16:38:08 +0100717 // it will use the `default_visibility` of its closest ancestor package for which
718 // a `default_visibility` property is specified.
719 //
720 // If no `default_visibility` property can be found then the module uses the
721 // global default of `//visibility:legacy_public`.
Paul Duffine2453c72019-05-31 14:00:04 +0100722 //
Paul Duffin95d53b52019-07-24 13:45:05 +0100723 // The `visibility` property has no effect on a defaults module although it does
724 // apply to any non-defaults module that uses it. To set the visibility of a
725 // defaults module, use the `defaults_visibility` property on the defaults module;
726 // not to be confused with the `default_visibility` property on the package module.
727 //
Paul Duffin2e61fa62019-03-28 14:10:57 +0000728 // See https://android.googlesource.com/platform/build/soong/+/master/README.md#visibility for
729 // more details.
730 Visibility []string
731
Bob Badour37af0462021-01-07 03:34:31 +0000732 // Describes the licenses applicable to this module. Must reference license modules.
733 Licenses []string
734
735 // Flattened from direct license dependencies. Equal to Licenses unless particular module adds more.
736 Effective_licenses []string `blueprint:"mutated"`
737 // Override of module name when reporting licenses
738 Effective_package_name *string `blueprint:"mutated"`
739 // Notice files
Bob Badour4101c712022-02-09 11:54:35 -0800740 Effective_license_text NamedPaths `blueprint:"mutated"`
Bob Badour37af0462021-01-07 03:34:31 +0000741 // License names
742 Effective_license_kinds []string `blueprint:"mutated"`
743 // License conditions
744 Effective_license_conditions []string `blueprint:"mutated"`
745
Colin Cross7d5136f2015-05-11 13:39:40 -0700746 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800747 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
748 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
Roland Levillain24bb2e62020-09-22 11:18:38 +0000749 // platform).
Colin Cross7d716ba2017-11-01 10:38:29 -0700750 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700751
752 Target struct {
753 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700754 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700755 }
756 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700757 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700758 }
759 }
760
Paul Duffinca7f0ef2020-02-25 15:50:49 +0000761 // If set to true then the archMutator will create variants for each arch specific target
762 // (e.g. 32/64) that the module is required to produce. If set to false then it will only
763 // create a variant for the architecture and will list the additional arch specific targets
764 // that the variant needs to produce in the CompileMultiTargets property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700765 UseTargetVariants bool `blueprint:"mutated"`
766 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800767
Dan Willemsen782a2d12015-12-21 14:55:28 -0800768 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700769 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800770
Colin Cross55708f32017-03-20 13:23:34 -0700771 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700772 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700773
Jiyong Park2db76922017-11-08 16:03:48 +0900774 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
775 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
776 // Use `soc_specific` instead for better meaning.
Colin Cross7d716ba2017-11-01 10:38:29 -0700777 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700778
Jiyong Park2db76922017-11-08 16:03:48 +0900779 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
780 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
781 Soc_specific *bool
782
783 // whether this module is specific to a device, not only for SoC, but also for off-chip
784 // peripherals. When set to true, it is installed into /odm (or /vendor/odm if odm partition
785 // does not exist, or /system/vendor/odm if both odm and vendor partitions do not exist).
786 // This implies `soc_specific:true`.
787 Device_specific *bool
788
789 // whether this module is specific to a software configuration of a product (e.g. country,
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900790 // network operator, etc). When set to true, it is installed into /product (or
791 // /system/product if product partition does not exist).
Jiyong Park2db76922017-11-08 16:03:48 +0900792 Product_specific *bool
793
Justin Yund5f6c822019-06-25 16:47:17 +0900794 // whether this module extends system. When set to true, it is installed into /system_ext
795 // (or /system/system_ext if system_ext partition does not exist).
796 System_ext_specific *bool
797
Jiyong Parkf9332f12018-02-01 00:54:12 +0900798 // Whether this module is installed to recovery partition
799 Recovery *bool
800
Yifan Hong1b3348d2020-01-21 15:53:22 -0800801 // Whether this module is installed to ramdisk
802 Ramdisk *bool
803
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700804 // Whether this module is installed to vendor ramdisk
805 Vendor_ramdisk *bool
806
Inseob Kim08758f02021-04-08 21:13:22 +0900807 // Whether this module is installed to debug ramdisk
808 Debug_ramdisk *bool
809
Jaewoong Jung8e93aba2021-03-02 16:58:08 -0800810 // Whether this module is built for non-native architectures (also known as native bridge binary)
dimitry1f33e402019-03-26 12:39:31 +0100811 Native_bridge_supported *bool `android:"arch_variant"`
812
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700813 // init.rc files to be installed if this module is installed
Colin Cross0bab8772020-09-25 14:01:21 -0700814 Init_rc []string `android:"arch_variant,path"`
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700815
Steven Moreland57a23d22018-04-04 15:42:19 -0700816 // VINTF manifest fragments to be installed if this module is installed
Colin Cross27b922f2019-03-04 22:35:41 -0800817 Vintf_fragments []string `android:"path"`
Steven Moreland57a23d22018-04-04 15:42:19 -0700818
Chris Wolfe998306e2016-08-15 14:47:23 -0400819 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700820 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400821
Sasha Smundakb6d23052019-04-01 18:37:36 -0700822 // names of other modules to install on host if this module is installed
823 Host_required []string `android:"arch_variant"`
824
825 // names of other modules to install on target if this module is installed
826 Target_required []string `android:"arch_variant"`
827
Paul Duffinca7f0ef2020-02-25 15:50:49 +0000828 // The OsType of artifacts that this module variant is responsible for creating.
829 //
830 // Set by osMutator
831 CompileOS OsType `blueprint:"mutated"`
832
833 // The Target of artifacts that this module variant is responsible for creating.
834 //
835 // Set by archMutator
836 CompileTarget Target `blueprint:"mutated"`
837
838 // The additional arch specific targets (e.g. 32/64 bit) that this module variant is
839 // responsible for creating.
840 //
841 // By default this is nil as, where necessary, separate variants are created for the
842 // different multilib types supported and that information is encapsulated in the
843 // CompileTarget so the module variant simply needs to create artifacts for that.
844 //
845 // However, if UseTargetVariants is set to false (e.g. by
846 // InitAndroidMultiTargetsArchModule) then no separate variants are created for the
847 // multilib targets. Instead a single variant is created for the architecture and
848 // this contains the multilib specific targets that this variant should create.
849 //
850 // Set by archMutator
Colin Crossee0bc3b2018-10-02 22:01:37 -0700851 CompileMultiTargets []Target `blueprint:"mutated"`
Paul Duffinca7f0ef2020-02-25 15:50:49 +0000852
853 // True if the module variant's CompileTarget is the primary target
854 //
855 // Set by archMutator
856 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800857
858 // Set by InitAndroidModule
859 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700860 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700861
Paul Duffin1356d8c2020-02-25 19:26:33 +0000862 // If set to true then a CommonOS variant will be created which will have dependencies
863 // on all its OsType specific variants. Used by sdk/module_exports to create a snapshot
864 // that covers all os and architecture variants.
865 //
866 // The OsType specific variants can be retrieved by calling
867 // GetOsSpecificVariantsOfCommonOSVariant
868 //
869 // Set at module initialization time by calling InitCommonOSAndroidMultiTargetsArchModule
870 CreateCommonOSVariant bool `blueprint:"mutated"`
871
872 // If set to true then this variant is the CommonOS variant that has dependencies on its
873 // OsType specific variants.
874 //
875 // Set by osMutator.
876 CommonOSVariant bool `blueprint:"mutated"`
877
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800878 // When HideFromMake is set to true, no entry for this variant will be emitted in the
879 // generated Android.mk file.
880 HideFromMake bool `blueprint:"mutated"`
881
882 // When SkipInstall is set to true, calls to ctx.InstallFile, ctx.InstallExecutable,
883 // ctx.InstallSymlink and ctx.InstallAbsoluteSymlink act like calls to ctx.PackageFile
884 // and don't create a rule to install the file.
Colin Crossce75d2c2016-10-06 16:12:58 -0700885 SkipInstall bool `blueprint:"mutated"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800886
Liz Kammer5ca3a622020-08-05 15:40:41 -0700887 // Whether the module has been replaced by a prebuilt
888 ReplacedByPrebuilt bool `blueprint:"mutated"`
889
Justin Yun32f053b2020-07-31 23:07:17 +0900890 // Disabled by mutators. If set to true, it overrides Enabled property.
891 ForcedDisabled bool `blueprint:"mutated"`
892
Jeff Gaston088e29e2017-11-29 16:47:17 -0800893 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross6c4f21f2019-06-06 15:41:36 -0700894
895 MissingDeps []string `blueprint:"mutated"`
Colin Cross9a362232019-07-01 15:32:45 -0700896
897 // Name and variant strings stored by mutators to enable Module.String()
898 DebugName string `blueprint:"mutated"`
899 DebugMutators []string `blueprint:"mutated"`
900 DebugVariations []string `blueprint:"mutated"`
Colin Cross7228ecd2019-11-18 16:00:16 -0800901
Colin Crossa6845402020-11-16 15:08:19 -0800902 // ImageVariation is set by ImageMutator to specify which image this variation is for,
903 // for example "" for core or "recovery" for recovery. It will often be set to one of the
904 // constants in image.go, but can also be set to a custom value by individual module types.
Colin Cross7228ecd2019-11-18 16:00:16 -0800905 ImageVariation string `blueprint:"mutated"`
Liz Kammer2ada09a2021-08-11 00:17:36 -0400906
Sasha Smundaka0954062022-08-02 18:23:58 -0700907 // Bazel conversion status
908 BazelConversionStatus BazelConversionStatus `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800909}
910
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000911// CommonAttributes represents the common Bazel attributes from which properties
912// in `commonProperties` are translated/mapped; such properties are annotated in
913// a list their corresponding attribute. It is embedded within `bp2buildInfo`.
914type CommonAttributes struct {
915 // Soong nameProperties -> Bazel name
916 Name string
Spandan Das4238c652022-09-09 01:38:47 +0000917
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000918 // Data mapped from: Required
919 Data bazel.LabelListAttribute
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000920
Spandan Das4238c652022-09-09 01:38:47 +0000921 // SkipData is neither a Soong nor Bazel target attribute
922 // If true, this will not fill the data attribute automatically
923 // This is useful for Soong modules that have 1:many Bazel targets
924 // Some of the generated Bazel targets might not have a data attribute
925 SkipData *bool
926
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000927 Tags bazel.StringListAttribute
Sasha Smundak05b0ba62022-09-26 18:15:45 -0700928
929 Applicable_licenses bazel.LabelListAttribute
Yu Liu4c212ce2022-10-14 12:20:20 -0700930
931 Testonly *bool
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000932}
933
Chris Parsons58852a02021-12-09 18:10:18 -0500934// constraintAttributes represents Bazel attributes pertaining to build constraints,
935// which make restrict building a Bazel target for some set of platforms.
936type constraintAttributes struct {
937 // Constraint values this target can be built for.
938 Target_compatible_with bazel.LabelListAttribute
939}
940
Paul Duffined875132020-09-02 13:08:57 +0100941type distProperties struct {
942 // configuration to distribute output files from this module to the distribution
943 // directory (default: $OUT/dist, configurable with $DIST_DIR)
944 Dist Dist `android:"arch_variant"`
945
946 // a list of configurations to distribute output files from this module to the
947 // distribution directory (default: $OUT/dist, configurable with $DIST_DIR)
948 Dists []Dist `android:"arch_variant"`
949}
950
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800951// CommonTestOptions represents the common `test_options` properties in
952// Android.bp.
953type CommonTestOptions struct {
954 // If the test is a hostside (no device required) unittest that shall be run
955 // during presubmit check.
956 Unit_test *bool
Zhenhuang Wang409d2772022-08-22 16:00:05 +0800957
958 // Tags provide additional metadata to customize test execution by downstream
959 // test runners. The tags have no special meaning to Soong.
960 Tags []string
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800961}
962
963// SetAndroidMkEntries sets AndroidMkEntries according to the value of base
964// `test_options`.
965func (t *CommonTestOptions) SetAndroidMkEntries(entries *AndroidMkEntries) {
966 entries.SetBoolIfTrue("LOCAL_IS_UNIT_TEST", Bool(t.Unit_test))
Zhenhuang Wang409d2772022-08-22 16:00:05 +0800967 if len(t.Tags) > 0 {
968 entries.AddStrings("LOCAL_TEST_OPTIONS_TAGS", t.Tags...)
969 }
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800970}
971
Paul Duffin74f05592020-11-25 16:37:46 +0000972// The key to use in TaggedDistFiles when a Dist structure does not specify a
973// tag property. This intentionally does not use "" as the default because that
974// would mean that an empty tag would have a different meaning when used in a dist
975// structure that when used to reference a specific set of output paths using the
976// :module{tag} syntax, which passes tag to the OutputFiles(tag) method.
977const DefaultDistTag = "<default-dist-tag>"
978
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000979// A map of OutputFile tag keys to Paths, for disting purposes.
980type TaggedDistFiles map[string]Paths
981
Paul Duffin74f05592020-11-25 16:37:46 +0000982// addPathsForTag adds a mapping from the tag to the paths. If the map is nil
983// then it will create a map, update it and then return it. If a mapping already
984// exists for the tag then the paths are appended to the end of the current list
985// of paths, ignoring any duplicates.
986func (t TaggedDistFiles) addPathsForTag(tag string, paths ...Path) TaggedDistFiles {
987 if t == nil {
988 t = make(TaggedDistFiles)
989 }
990
991 for _, distFile := range paths {
992 if distFile != nil && !t[tag].containsPath(distFile) {
993 t[tag] = append(t[tag], distFile)
994 }
995 }
996
997 return t
998}
999
1000// merge merges the entries from the other TaggedDistFiles object into this one.
1001// If the TaggedDistFiles is nil then it will create a new instance, merge the
1002// other into it, and then return it.
1003func (t TaggedDistFiles) merge(other TaggedDistFiles) TaggedDistFiles {
1004 for tag, paths := range other {
1005 t = t.addPathsForTag(tag, paths...)
1006 }
1007
1008 return t
1009}
1010
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001011func MakeDefaultDistFiles(paths ...Path) TaggedDistFiles {
Sasha Smundake198eaf2022-08-04 13:07:02 -07001012 for _, p := range paths {
1013 if p == nil {
Jingwen Chen7b27ca72020-07-24 09:13:49 +00001014 panic("The path to a dist file cannot be nil.")
1015 }
1016 }
1017
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001018 // The default OutputFile tag is the empty "" string.
Paul Duffin74f05592020-11-25 16:37:46 +00001019 return TaggedDistFiles{DefaultDistTag: paths}
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001020}
1021
Colin Cross3f40fa42015-01-30 17:27:36 -08001022type hostAndDeviceProperties struct {
Colin Cross4e81d702018-11-09 10:36:55 -08001023 // If set to true, build a variant of the module for the host. Defaults to false.
1024 Host_supported *bool
1025
1026 // If set to true, build a variant of the module for the device. Defaults to true.
Colin Crossa4190c12016-07-12 13:11:25 -07001027 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -08001028}
1029
Colin Crossc472d572015-03-17 15:06:21 -07001030type Multilib string
1031
1032const (
Colin Cross6b4a32d2017-12-05 13:42:45 -08001033 MultilibBoth Multilib = "both"
1034 MultilibFirst Multilib = "first"
1035 MultilibCommon Multilib = "common"
1036 MultilibCommonFirst Multilib = "common_first"
Colin Crossc472d572015-03-17 15:06:21 -07001037)
1038
Colin Crossa1ad8d12016-06-01 17:09:44 -07001039type HostOrDeviceSupported int
1040
1041const (
Colin Cross34037c62020-11-17 13:19:17 -08001042 hostSupported = 1 << iota
1043 hostCrossSupported
1044 deviceSupported
1045 hostDefault
1046 deviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -07001047
1048 // Host and HostCross are built by default. Device is not supported.
Colin Cross34037c62020-11-17 13:19:17 -08001049 HostSupported = hostSupported | hostCrossSupported | hostDefault
Dan Albert0981b5c2018-08-02 13:46:35 -07001050
1051 // Host is built by default. HostCross and Device are not supported.
Colin Cross34037c62020-11-17 13:19:17 -08001052 HostSupportedNoCross = hostSupported | hostDefault
Dan Albert0981b5c2018-08-02 13:46:35 -07001053
1054 // Device is built by default. Host and HostCross are not supported.
Colin Cross34037c62020-11-17 13:19:17 -08001055 DeviceSupported = deviceSupported | deviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -07001056
Liz Kammer8631cc72021-08-23 21:12:07 +00001057 // By default, _only_ device variant is built. Device variant can be disabled with `device_supported: false`
Chih-Hung Hsiehd0f82fe2021-09-05 20:15:38 -07001058 // Host and HostCross are disabled by default and can be enabled with `host_supported: true`
Colin Cross34037c62020-11-17 13:19:17 -08001059 HostAndDeviceSupported = hostSupported | hostCrossSupported | deviceSupported | deviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -07001060
1061 // Host, HostCross, and Device are built by default.
Chih-Hung Hsiehd0f82fe2021-09-05 20:15:38 -07001062 // Building Device can be disabled with `device_supported: false`
1063 // Building Host and HostCross can be disabled with `host_supported: false`
Colin Cross34037c62020-11-17 13:19:17 -08001064 HostAndDeviceDefault = hostSupported | hostCrossSupported | hostDefault |
1065 deviceSupported | deviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -07001066
1067 // Nothing is supported. This is not exposed to the user, but used to mark a
1068 // host only module as unsupported when the module type is not supported on
1069 // the host OS. E.g. benchmarks are supported on Linux but not Darwin.
Colin Cross34037c62020-11-17 13:19:17 -08001070 NeitherHostNorDeviceSupported = 0
Colin Crossa1ad8d12016-06-01 17:09:44 -07001071)
1072
Jiyong Park2db76922017-11-08 16:03:48 +09001073type moduleKind int
1074
1075const (
1076 platformModule moduleKind = iota
1077 deviceSpecificModule
1078 socSpecificModule
1079 productSpecificModule
Justin Yund5f6c822019-06-25 16:47:17 +09001080 systemExtSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +09001081)
1082
1083func (k moduleKind) String() string {
1084 switch k {
1085 case platformModule:
1086 return "platform"
1087 case deviceSpecificModule:
1088 return "device-specific"
1089 case socSpecificModule:
1090 return "soc-specific"
1091 case productSpecificModule:
1092 return "product-specific"
Justin Yund5f6c822019-06-25 16:47:17 +09001093 case systemExtSpecificModule:
1094 return "systemext-specific"
Jiyong Park2db76922017-11-08 16:03:48 +09001095 default:
1096 panic(fmt.Errorf("unknown module kind %d", k))
1097 }
1098}
1099
Colin Cross9d34f352019-11-22 16:03:51 -08001100func initAndroidModuleBase(m Module) {
1101 m.base().module = m
1102}
1103
Colin Crossa6845402020-11-16 15:08:19 -08001104// InitAndroidModule initializes the Module as an Android module that is not architecture-specific.
1105// It adds the common properties, for example "name" and "enabled".
Colin Cross36242852017-06-23 15:06:31 -07001106func InitAndroidModule(m Module) {
Colin Cross9d34f352019-11-22 16:03:51 -08001107 initAndroidModuleBase(m)
Colin Cross3f40fa42015-01-30 17:27:36 -08001108 base := m.base()
Colin Cross5049f022015-03-18 13:28:46 -07001109
Colin Cross36242852017-06-23 15:06:31 -07001110 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -07001111 &base.nameProperties,
Paul Duffined875132020-09-02 13:08:57 +01001112 &base.commonProperties,
1113 &base.distProperties)
Colin Cross18c46802019-09-24 22:19:02 -07001114
Colin Crosseabaedd2020-02-06 17:01:55 -08001115 initProductVariableModule(m)
Colin Cross18c46802019-09-24 22:19:02 -07001116
Paul Duffin63c6e182019-07-24 14:24:38 +01001117 // The default_visibility property needs to be checked and parsed by the visibility module during
Paul Duffin5ec73ec2020-05-01 17:52:01 +01001118 // its checking and parsing phases so make it the primary visibility property.
1119 setPrimaryVisibilityProperty(m, "visibility", &base.commonProperties.Visibility)
Bob Badour37af0462021-01-07 03:34:31 +00001120
1121 // The default_applicable_licenses property needs to be checked and parsed by the licenses module during
1122 // its checking and parsing phases so make it the primary licenses property.
1123 setPrimaryLicensesProperty(m, "licenses", &base.commonProperties.Licenses)
Colin Cross5049f022015-03-18 13:28:46 -07001124}
1125
Colin Crossa6845402020-11-16 15:08:19 -08001126// InitAndroidArchModule initializes the Module as an Android module that is architecture-specific.
1127// It adds the common properties, for example "name" and "enabled", as well as runtime generated
1128// property structs for architecture-specific versions of generic properties tagged with
1129// `android:"arch_variant"`.
1130//
Colin Crossd079e0b2022-08-16 10:27:33 -07001131// InitAndroidModule should not be called if InitAndroidArchModule was called.
Colin Cross36242852017-06-23 15:06:31 -07001132func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
1133 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -07001134
1135 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -08001136 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -07001137 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -07001138 base.commonProperties.ArchSpecific = true
Colin Crossee0bc3b2018-10-02 22:01:37 -07001139 base.commonProperties.UseTargetVariants = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001140
Colin Cross34037c62020-11-17 13:19:17 -08001141 if hod&hostSupported != 0 && hod&deviceSupported != 0 {
Colin Cross36242852017-06-23 15:06:31 -07001142 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001143 }
1144
Colin Crossa6845402020-11-16 15:08:19 -08001145 initArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -08001146}
1147
Colin Crossa6845402020-11-16 15:08:19 -08001148// InitAndroidMultiTargetsArchModule initializes the Module as an Android module that is
1149// architecture-specific, but will only have a single variant per OS that handles all the
1150// architectures simultaneously. The list of Targets that it must handle will be available from
1151// ModuleContext.MultiTargets. It adds the common properties, for example "name" and "enabled", as
1152// well as runtime generated property structs for architecture-specific versions of generic
1153// properties tagged with `android:"arch_variant"`.
1154//
1155// InitAndroidModule or InitAndroidArchModule should not be called if
1156// InitAndroidMultiTargetsArchModule was called.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001157func InitAndroidMultiTargetsArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
1158 InitAndroidArchModule(m, hod, defaultMultilib)
1159 m.base().commonProperties.UseTargetVariants = false
1160}
1161
Colin Crossa6845402020-11-16 15:08:19 -08001162// InitCommonOSAndroidMultiTargetsArchModule initializes the Module as an Android module that is
1163// architecture-specific, but will only have a single variant per OS that handles all the
1164// architectures simultaneously, and will also have an additional CommonOS variant that has
1165// dependencies on all the OS-specific variants. The list of Targets that it must handle will be
1166// available from ModuleContext.MultiTargets. It adds the common properties, for example "name" and
1167// "enabled", as well as runtime generated property structs for architecture-specific versions of
1168// generic properties tagged with `android:"arch_variant"`.
1169//
1170// InitAndroidModule, InitAndroidArchModule or InitAndroidMultiTargetsArchModule should not be
1171// called if InitCommonOSAndroidMultiTargetsArchModule was called.
Paul Duffin1356d8c2020-02-25 19:26:33 +00001172func InitCommonOSAndroidMultiTargetsArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
1173 InitAndroidArchModule(m, hod, defaultMultilib)
1174 m.base().commonProperties.UseTargetVariants = false
1175 m.base().commonProperties.CreateCommonOSVariant = true
1176}
1177
Chris Parsons58852a02021-12-09 18:10:18 -05001178func (attrs *CommonAttributes) fillCommonBp2BuildModuleAttrs(ctx *topDownMutatorContext,
1179 enabledPropertyOverrides bazel.BoolAttribute) constraintAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001180
1181 mod := ctx.Module().base()
Sasha Smundake198eaf2022-08-04 13:07:02 -07001182 // Assert passed-in attributes include Name
1183 if len(attrs.Name) == 0 {
Sasha Smundakfb589492022-08-04 11:13:27 -07001184 if ctx.ModuleType() != "package" {
1185 ctx.ModuleErrorf("CommonAttributes in fillCommonBp2BuildModuleAttrs expects a `.Name`!")
1186 }
Sasha Smundake198eaf2022-08-04 13:07:02 -07001187 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001188
1189 depsToLabelList := func(deps []string) bazel.LabelListAttribute {
1190 return bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, deps))
1191 }
1192
Chris Parsons58852a02021-12-09 18:10:18 -05001193 var enabledProperty bazel.BoolAttribute
Liz Kammerdfeb1202022-05-13 17:20:20 -04001194
1195 onlyAndroid := false
1196 neitherHostNorDevice := false
1197
1198 osSupport := map[string]bool{}
1199
1200 // if the target is enabled and supports arch variance, determine the defaults based on the module
1201 // type's host or device property and host_supported/device_supported properties
1202 if mod.commonProperties.ArchSpecific {
1203 moduleSupportsDevice := mod.DeviceSupported()
1204 moduleSupportsHost := mod.HostSupported()
1205 if moduleSupportsHost && !moduleSupportsDevice {
1206 // for host only, we specify as unsupported on android rather than listing all host osSupport
1207 // TODO(b/220874839): consider replacing this with a constraint that covers all host osSupport
1208 // instead
1209 enabledProperty.SetSelectValue(bazel.OsConfigurationAxis, Android.Name, proptools.BoolPtr(false))
1210 } else if moduleSupportsDevice && !moduleSupportsHost {
1211 enabledProperty.SetSelectValue(bazel.OsConfigurationAxis, Android.Name, proptools.BoolPtr(true))
1212 // specify as a positive to ensure any target-specific enabled can be resolved
1213 // also save that a target is only android, as if there is only the positive restriction on
1214 // android, it'll be dropped, so we may need to add it back later
1215 onlyAndroid = true
1216 } else if !moduleSupportsHost && !moduleSupportsDevice {
1217 neitherHostNorDevice = true
1218 }
1219
Sasha Smundake198eaf2022-08-04 13:07:02 -07001220 for _, osType := range OsTypeList() {
1221 if osType.Class == Host {
1222 osSupport[osType.Name] = moduleSupportsHost
1223 } else if osType.Class == Device {
1224 osSupport[osType.Name] = moduleSupportsDevice
Liz Kammerdfeb1202022-05-13 17:20:20 -04001225 }
1226 }
1227 }
1228
1229 if neitherHostNorDevice {
1230 // we can't build this, disable
1231 enabledProperty.Value = proptools.BoolPtr(false)
Sasha Smundake198eaf2022-08-04 13:07:02 -07001232 } else if mod.commonProperties.Enabled != nil {
1233 enabledProperty.SetValue(mod.commonProperties.Enabled)
1234 if !*mod.commonProperties.Enabled {
1235 for oss, enabled := range osSupport {
1236 if val := enabledProperty.SelectValue(bazel.OsConfigurationAxis, oss); enabled && val != nil && *val {
Liz Kammerdfeb1202022-05-13 17:20:20 -04001237 // if this should be disabled by default, clear out any enabling we've done
Sasha Smundake198eaf2022-08-04 13:07:02 -07001238 enabledProperty.SetSelectValue(bazel.OsConfigurationAxis, oss, nil)
Liz Kammerdfeb1202022-05-13 17:20:20 -04001239 }
1240 }
1241 }
Chris Parsons58852a02021-12-09 18:10:18 -05001242 }
1243
Sasha Smundak05b0ba62022-09-26 18:15:45 -07001244 attrs.Applicable_licenses = bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, mod.commonProperties.Licenses))
1245
Jingwen Chena5ecb372022-09-21 09:05:37 +00001246 // The required property can contain the module itself. This causes a cycle
1247 // when generated as the 'data' label list attribute in Bazel. Remove it if
1248 // it exists. See b/247985196.
1249 _, requiredWithoutCycles := RemoveFromList(ctx.ModuleName(), mod.commonProperties.Required)
Wei Li7d8f6182022-10-11 14:38:16 -07001250 requiredWithoutCycles = FirstUniqueStrings(requiredWithoutCycles)
Jingwen Chena5ecb372022-09-21 09:05:37 +00001251 required := depsToLabelList(requiredWithoutCycles)
Sasha Smundake198eaf2022-08-04 13:07:02 -07001252 archVariantProps := mod.GetArchVariantProperties(ctx, &commonProperties{})
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001253 for axis, configToProps := range archVariantProps {
1254 for config, _props := range configToProps {
1255 if archProps, ok := _props.(*commonProperties); ok {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001256 _, requiredWithoutCycles := RemoveFromList(ctx.ModuleName(), archProps.Required)
Wei Li7d8f6182022-10-11 14:38:16 -07001257 requiredWithoutCycles = FirstUniqueStrings(requiredWithoutCycles)
Jingwen Chena5ecb372022-09-21 09:05:37 +00001258 required.SetSelectValue(axis, config, depsToLabelList(requiredWithoutCycles).Value)
Liz Kammerdfeb1202022-05-13 17:20:20 -04001259 if !neitherHostNorDevice {
1260 if archProps.Enabled != nil {
1261 if axis != bazel.OsConfigurationAxis || osSupport[config] {
1262 enabledProperty.SetSelectValue(axis, config, archProps.Enabled)
1263 }
1264 }
Chris Parsons58852a02021-12-09 18:10:18 -05001265 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001266 }
1267 }
1268 }
Chris Parsons58852a02021-12-09 18:10:18 -05001269
Liz Kammerdfeb1202022-05-13 17:20:20 -04001270 if !neitherHostNorDevice {
1271 if enabledPropertyOverrides.Value != nil {
1272 enabledProperty.Value = enabledPropertyOverrides.Value
1273 }
1274 for _, axis := range enabledPropertyOverrides.SortedConfigurationAxes() {
1275 configToBools := enabledPropertyOverrides.ConfigurableValues[axis]
1276 for cfg, val := range configToBools {
1277 if axis != bazel.OsConfigurationAxis || osSupport[cfg] {
1278 enabledProperty.SetSelectValue(axis, cfg, &val)
1279 }
1280 }
Chris Parsons58852a02021-12-09 18:10:18 -05001281 }
1282 }
1283
Sam Delmerico0e33c9d2022-01-07 20:39:21 +00001284 productConfigEnabledLabels := []bazel.Label{}
Liz Kammerdfeb1202022-05-13 17:20:20 -04001285 // TODO(b/234497586): Soong config variables and product variables have different overriding behavior, we
1286 // should handle it correctly
1287 if !proptools.BoolDefault(enabledProperty.Value, true) && !neitherHostNorDevice {
Sam Delmerico0e33c9d2022-01-07 20:39:21 +00001288 // If the module is not enabled by default, then we can check if a
1289 // product variable enables it
1290 productConfigEnabledLabels = productVariableConfigEnableLabels(ctx)
Chris Parsons58852a02021-12-09 18:10:18 -05001291
Sam Delmerico0e33c9d2022-01-07 20:39:21 +00001292 if len(productConfigEnabledLabels) > 0 {
1293 // In this case, an existing product variable configuration overrides any
1294 // module-level `enable: false` definition
1295 newValue := true
1296 enabledProperty.Value = &newValue
1297 }
1298 }
1299
1300 productConfigEnabledAttribute := bazel.MakeLabelListAttribute(bazel.LabelList{
1301 productConfigEnabledLabels, nil,
1302 })
1303
1304 platformEnabledAttribute, err := enabledProperty.ToLabelListAttribute(
Sasha Smundake198eaf2022-08-04 13:07:02 -07001305 bazel.LabelList{[]bazel.Label{{Label: "@platforms//:incompatible"}}, nil},
Chris Parsons58852a02021-12-09 18:10:18 -05001306 bazel.LabelList{[]bazel.Label{}, nil})
Chris Parsons58852a02021-12-09 18:10:18 -05001307 if err != nil {
Sam Delmerico0e33c9d2022-01-07 20:39:21 +00001308 ctx.ModuleErrorf("Error processing platform enabled attribute: %s", err)
Chris Parsons58852a02021-12-09 18:10:18 -05001309 }
Sam Delmerico0e33c9d2022-01-07 20:39:21 +00001310
Liz Kammerdfeb1202022-05-13 17:20:20 -04001311 // if android is the only arch/os enabled, then add a restriction to only be compatible with android
1312 if platformEnabledAttribute.IsNil() && onlyAndroid {
1313 l := bazel.LabelAttribute{}
1314 l.SetValue(bazel.Label{Label: bazel.OsConfigurationAxis.SelectKey(Android.Name)})
1315 platformEnabledAttribute.Add(&l)
1316 }
1317
Spandan Das4238c652022-09-09 01:38:47 +00001318 if !proptools.Bool(attrs.SkipData) {
1319 attrs.Data.Append(required)
1320 }
1321 // SkipData is not an attribute of any Bazel target
1322 // Set this to nil so that it does not appear in the generated build file
1323 attrs.SkipData = nil
Sam Delmerico0e33c9d2022-01-07 20:39:21 +00001324
Sam Delmerico0e33c9d2022-01-07 20:39:21 +00001325 moduleEnableConstraints := bazel.LabelListAttribute{}
1326 moduleEnableConstraints.Append(platformEnabledAttribute)
1327 moduleEnableConstraints.Append(productConfigEnabledAttribute)
Sam Delmerico0e33c9d2022-01-07 20:39:21 +00001328
Sasha Smundake198eaf2022-08-04 13:07:02 -07001329 return constraintAttributes{Target_compatible_with: moduleEnableConstraints}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001330}
1331
Sam Delmerico0e33c9d2022-01-07 20:39:21 +00001332// Check product variables for `enabled: true` flag override.
1333// Returns a list of the constraint_value targets who enable this override.
1334func productVariableConfigEnableLabels(ctx *topDownMutatorContext) []bazel.Label {
1335 productVariableProps := ProductVariableProperties(ctx)
1336 productConfigEnablingTargets := []bazel.Label{}
1337 const propName = "Enabled"
1338 if productConfigProps, exists := productVariableProps[propName]; exists {
1339 for productConfigProp, prop := range productConfigProps {
1340 flag, ok := prop.(*bool)
1341 if !ok {
1342 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
1343 }
1344
1345 if *flag {
1346 axis := productConfigProp.ConfigurationAxis()
1347 targetLabel := axis.SelectKey(productConfigProp.SelectKey())
1348 productConfigEnablingTargets = append(productConfigEnablingTargets, bazel.Label{
1349 Label: targetLabel,
1350 })
1351 } else {
1352 // TODO(b/210546943): handle negative case where `enabled: false`
1353 ctx.ModuleErrorf("`enabled: false` is not currently supported for configuration variables. See b/210546943", proptools.PropertyNameForField(propName))
1354 }
1355 }
1356 }
1357
1358 return productConfigEnablingTargets
1359}
1360
Nan Zhangb9eeb1d2017-02-02 10:46:07 -08001361// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -08001362// modules. It should be included as an anonymous field in every module
1363// struct definition. InitAndroidModule should then be called from the module's
1364// factory function, and the return values from InitAndroidModule should be
1365// returned from the factory function.
1366//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -08001367// The ModuleBase type is responsible for implementing the GenerateBuildActions
1368// method to support the blueprint.Module interface. This method will then call
1369// the module's GenerateAndroidBuildActions method once for each build variant
Colin Cross25de6c32019-06-06 14:29:25 -07001370// that is to be built. GenerateAndroidBuildActions is passed a ModuleContext
1371// rather than the usual blueprint.ModuleContext.
1372// ModuleContext exposes extra functionality specific to the Android build
Colin Cross3f40fa42015-01-30 17:27:36 -08001373// system including details about the particular build variant that is to be
1374// generated.
1375//
1376// For example:
1377//
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +08001378// import (
1379// "android/soong/android"
1380// )
Colin Cross3f40fa42015-01-30 17:27:36 -08001381//
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +08001382// type myModule struct {
1383// android.ModuleBase
1384// properties struct {
1385// MyProperty string
1386// }
1387// }
Colin Cross3f40fa42015-01-30 17:27:36 -08001388//
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +08001389// func NewMyModule() android.Module {
1390// m := &myModule{}
1391// m.AddProperties(&m.properties)
1392// android.InitAndroidModule(m)
1393// return m
1394// }
Colin Cross3f40fa42015-01-30 17:27:36 -08001395//
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +08001396// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1397// // Get the CPU architecture for the current build variant.
1398// variantArch := ctx.Arch()
Colin Cross3f40fa42015-01-30 17:27:36 -08001399//
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +08001400// // ...
1401// }
Colin Cross635c3b02016-05-18 15:37:25 -07001402type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -08001403 // Putting the curiously recurring thing pointing to the thing that contains
1404 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -07001405 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -07001406 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -08001407
Colin Crossfc754582016-05-17 16:34:16 -07001408 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001409 commonProperties commonProperties
Paul Duffined875132020-09-02 13:08:57 +01001410 distProperties distProperties
Colin Cross18c46802019-09-24 22:19:02 -07001411 variableProperties interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001412 hostAndDeviceProperties hostAndDeviceProperties
Jingwen Chen5d864492021-02-24 07:20:12 -05001413
Usta851a3272022-01-05 23:42:33 -05001414 // Arch specific versions of structs in GetProperties() prior to
1415 // initialization in InitAndroidArchModule, lets call it `generalProperties`.
1416 // The outer index has the same order as generalProperties and the inner index
1417 // chooses the props specific to the architecture. The interface{} value is an
1418 // archPropRoot that is filled with arch specific values by the arch mutator.
Jingwen Chen5d864492021-02-24 07:20:12 -05001419 archProperties [][]interface{}
1420
Jingwen Chen73850672020-12-14 08:25:34 -05001421 // Properties specific to the Blueprint to BUILD migration.
1422 bazelTargetModuleProperties bazel.BazelTargetModuleProperties
1423
Paul Duffin63c6e182019-07-24 14:24:38 +01001424 // Information about all the properties on the module that contains visibility rules that need
1425 // checking.
1426 visibilityPropertyInfo []visibilityProperty
1427
1428 // The primary visibility property, may be nil, that controls access to the module.
1429 primaryVisibilityProperty visibilityProperty
1430
Bob Badour37af0462021-01-07 03:34:31 +00001431 // The primary licenses property, may be nil, records license metadata for the module.
1432 primaryLicensesProperty applicableLicensesProperty
1433
Colin Crossffe6b9d2020-12-01 15:40:06 -08001434 noAddressSanitizer bool
1435 installFiles InstallPaths
1436 installFilesDepSet *installPathsDepSet
1437 checkbuildFiles Paths
1438 packagingSpecs []PackagingSpec
1439 packagingSpecsDepSet *packagingSpecsDepSet
Colin Cross6301c3c2021-09-28 17:40:21 -07001440 // katiInstalls tracks the install rules that were created by Soong but are being exported
1441 // to Make to convert to ninja rules so that Make can add additional dependencies.
1442 katiInstalls katiInstalls
1443 katiSymlinks katiInstalls
Colin Cross1f8c52b2015-06-16 16:38:17 -07001444
Paul Duffinaf970a22020-11-23 23:32:56 +00001445 // The files to copy to the dist as explicitly specified in the .bp file.
1446 distFiles TaggedDistFiles
1447
Colin Cross1f8c52b2015-06-16 16:38:17 -07001448 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
1449 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -08001450 installTarget WritablePath
1451 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -07001452 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -07001453
Colin Cross178a5092016-09-13 13:42:32 -07001454 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -07001455
1456 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -07001457
1458 // For tests
Colin Crossae887032017-10-23 17:16:14 -07001459 buildParams []BuildParams
Colin Cross4c83e5c2019-02-25 14:54:28 -08001460 ruleParams map[blueprint.Rule]blueprint.RuleParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001461 variables map[string]string
Colin Crossa9d8bee2018-10-02 13:59:46 -07001462
Inseob Kim8471cda2019-11-15 09:59:12 +09001463 initRcPaths Paths
1464 vintfFragmentsPaths Paths
Colin Cross4acaea92021-12-10 23:05:02 +00001465
1466 // set of dependency module:location mappings used to populate the license metadata for
1467 // apex containers.
1468 licenseInstallMap []string
Colin Crossaa1cab02022-01-28 14:49:24 -08001469
1470 // The path to the generated license metadata file for the module.
1471 licenseMetadataFile WritablePath
Colin Cross36242852017-06-23 15:06:31 -07001472}
1473
Liz Kammer2ada09a2021-08-11 00:17:36 -04001474// A struct containing all relevant information about a Bazel target converted via bp2build.
1475type bp2buildInfo struct {
Chris Parsons58852a02021-12-09 18:10:18 -05001476 Dir string
1477 BazelProps bazel.BazelTargetModuleProperties
1478 CommonAttrs CommonAttributes
1479 ConstraintAttrs constraintAttributes
1480 Attrs interface{}
Liz Kammer2ada09a2021-08-11 00:17:36 -04001481}
1482
1483// TargetName returns the Bazel target name of a bp2build converted target.
1484func (b bp2buildInfo) TargetName() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001485 return b.CommonAttrs.Name
Liz Kammer2ada09a2021-08-11 00:17:36 -04001486}
1487
1488// TargetPackage returns the Bazel package of a bp2build converted target.
1489func (b bp2buildInfo) TargetPackage() string {
1490 return b.Dir
1491}
1492
1493// BazelRuleClass returns the Bazel rule class of a bp2build converted target.
1494func (b bp2buildInfo) BazelRuleClass() string {
1495 return b.BazelProps.Rule_class
1496}
1497
1498// BazelRuleLoadLocation returns the location of the Bazel rule of a bp2build converted target.
1499// This may be empty as native Bazel rules do not need to be loaded.
1500func (b bp2buildInfo) BazelRuleLoadLocation() string {
1501 return b.BazelProps.Bzl_load_location
1502}
1503
1504// BazelAttributes returns the Bazel attributes of a bp2build converted target.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001505func (b bp2buildInfo) BazelAttributes() []interface{} {
Chris Parsons58852a02021-12-09 18:10:18 -05001506 return []interface{}{&b.CommonAttrs, &b.ConstraintAttrs, b.Attrs}
Liz Kammer2ada09a2021-08-11 00:17:36 -04001507}
1508
1509func (m *ModuleBase) addBp2buildInfo(info bp2buildInfo) {
Sasha Smundaka0954062022-08-02 18:23:58 -07001510 m.commonProperties.BazelConversionStatus.Bp2buildInfo = append(m.commonProperties.BazelConversionStatus.Bp2buildInfo, info)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001511}
1512
1513// IsConvertedByBp2build returns whether this module was converted via bp2build.
1514func (m *ModuleBase) IsConvertedByBp2build() bool {
Sasha Smundaka0954062022-08-02 18:23:58 -07001515 return len(m.commonProperties.BazelConversionStatus.Bp2buildInfo) > 0
Liz Kammer2ada09a2021-08-11 00:17:36 -04001516}
1517
1518// Bp2buildTargets returns the Bazel targets bp2build generated for this module.
1519func (m *ModuleBase) Bp2buildTargets() []bp2buildInfo {
Sasha Smundaka0954062022-08-02 18:23:58 -07001520 return m.commonProperties.BazelConversionStatus.Bp2buildInfo
Liz Kammer2ada09a2021-08-11 00:17:36 -04001521}
1522
Liz Kammer6eff3232021-08-26 08:37:59 -04001523// AddUnconvertedBp2buildDep stores module name of a dependency that was not converted to Bazel.
1524func (b *baseModuleContext) AddUnconvertedBp2buildDep(dep string) {
Sasha Smundaka0954062022-08-02 18:23:58 -07001525 unconvertedDeps := &b.Module().base().commonProperties.BazelConversionStatus.UnconvertedDeps
Liz Kammer6eff3232021-08-26 08:37:59 -04001526 *unconvertedDeps = append(*unconvertedDeps, dep)
1527}
1528
Liz Kammerdaa09ef2021-12-15 15:35:38 -05001529// AddMissingBp2buildDep stores module name of a dependency that was not found in a Android.bp file.
1530func (b *baseModuleContext) AddMissingBp2buildDep(dep string) {
Sasha Smundaka0954062022-08-02 18:23:58 -07001531 missingDeps := &b.Module().base().commonProperties.BazelConversionStatus.MissingDeps
Liz Kammerdaa09ef2021-12-15 15:35:38 -05001532 *missingDeps = append(*missingDeps, dep)
1533}
1534
Liz Kammer6eff3232021-08-26 08:37:59 -04001535// GetUnconvertedBp2buildDeps returns the list of module names of this module's direct dependencies that
1536// were not converted to Bazel.
1537func (m *ModuleBase) GetUnconvertedBp2buildDeps() []string {
Sasha Smundaka0954062022-08-02 18:23:58 -07001538 return FirstUniqueStrings(m.commonProperties.BazelConversionStatus.UnconvertedDeps)
Liz Kammer6eff3232021-08-26 08:37:59 -04001539}
1540
Usta Shrestha56b84e72022-09-24 00:26:47 -04001541// GetMissingBp2buildDeps returns the list of module names that were not found in Android.bp files.
Liz Kammerdaa09ef2021-12-15 15:35:38 -05001542func (m *ModuleBase) GetMissingBp2buildDeps() []string {
Sasha Smundaka0954062022-08-02 18:23:58 -07001543 return FirstUniqueStrings(m.commonProperties.BazelConversionStatus.MissingDeps)
Liz Kammerdaa09ef2021-12-15 15:35:38 -05001544}
1545
Lukacs T. Berkid18d8ca2021-06-25 09:11:22 +02001546func (m *ModuleBase) AddJSONData(d *map[string]interface{}) {
Liz Kammer9525e712022-01-05 13:46:24 -05001547 (*d)["Android"] = map[string]interface{}{
1548 // Properties set in Blueprint or in blueprint of a defaults modules
1549 "SetProperties": m.propertiesWithValues(),
1550 }
1551}
1552
1553type propInfo struct {
Liz Kammer898e0762022-03-22 11:27:26 -04001554 Name string
1555 Type string
1556 Value string
1557 Values []string
Liz Kammer9525e712022-01-05 13:46:24 -05001558}
1559
1560func (m *ModuleBase) propertiesWithValues() []propInfo {
1561 var info []propInfo
1562 props := m.GetProperties()
1563
1564 var propsWithValues func(name string, v reflect.Value)
1565 propsWithValues = func(name string, v reflect.Value) {
1566 kind := v.Kind()
1567 switch kind {
1568 case reflect.Ptr, reflect.Interface:
1569 if v.IsNil() {
1570 return
1571 }
1572 propsWithValues(name, v.Elem())
1573 case reflect.Struct:
1574 if v.IsZero() {
1575 return
1576 }
1577 for i := 0; i < v.NumField(); i++ {
1578 namePrefix := name
1579 sTyp := v.Type().Field(i)
1580 if proptools.ShouldSkipProperty(sTyp) {
1581 continue
1582 }
1583 if name != "" && !strings.HasSuffix(namePrefix, ".") {
1584 namePrefix += "."
1585 }
1586 if !proptools.IsEmbedded(sTyp) {
1587 namePrefix += sTyp.Name
1588 }
1589 sVal := v.Field(i)
1590 propsWithValues(namePrefix, sVal)
1591 }
1592 case reflect.Array, reflect.Slice:
1593 if v.IsNil() {
1594 return
1595 }
1596 elKind := v.Type().Elem().Kind()
Liz Kammer898e0762022-03-22 11:27:26 -04001597 info = append(info, propInfo{Name: name, Type: elKind.String() + " " + kind.String(), Values: sliceReflectionValue(v)})
Liz Kammer9525e712022-01-05 13:46:24 -05001598 default:
Liz Kammer898e0762022-03-22 11:27:26 -04001599 info = append(info, propInfo{Name: name, Type: kind.String(), Value: reflectionValue(v)})
Liz Kammer9525e712022-01-05 13:46:24 -05001600 }
1601 }
1602
1603 for _, p := range props {
1604 propsWithValues("", reflect.ValueOf(p).Elem())
1605 }
Liz Kammer898e0762022-03-22 11:27:26 -04001606 sort.Slice(info, func(i, j int) bool {
1607 return info[i].Name < info[j].Name
1608 })
Liz Kammer9525e712022-01-05 13:46:24 -05001609 return info
Lukacs T. Berkid18d8ca2021-06-25 09:11:22 +02001610}
1611
Liz Kammer898e0762022-03-22 11:27:26 -04001612func reflectionValue(value reflect.Value) string {
1613 switch value.Kind() {
1614 case reflect.Bool:
1615 return fmt.Sprintf("%t", value.Bool())
1616 case reflect.Int64:
1617 return fmt.Sprintf("%d", value.Int())
1618 case reflect.String:
1619 return fmt.Sprintf("%s", value.String())
1620 case reflect.Struct:
1621 if value.IsZero() {
1622 return "{}"
1623 }
1624 length := value.NumField()
1625 vals := make([]string, length, length)
1626 for i := 0; i < length; i++ {
1627 sTyp := value.Type().Field(i)
1628 if proptools.ShouldSkipProperty(sTyp) {
1629 continue
1630 }
1631 name := sTyp.Name
1632 vals[i] = fmt.Sprintf("%s: %s", name, reflectionValue(value.Field(i)))
1633 }
1634 return fmt.Sprintf("%s{%s}", value.Type(), strings.Join(vals, ", "))
1635 case reflect.Array, reflect.Slice:
1636 vals := sliceReflectionValue(value)
1637 return fmt.Sprintf("[%s]", strings.Join(vals, ", "))
1638 }
1639 return ""
1640}
1641
1642func sliceReflectionValue(value reflect.Value) []string {
1643 length := value.Len()
1644 vals := make([]string, length, length)
1645 for i := 0; i < length; i++ {
1646 vals[i] = reflectionValue(value.Index(i))
1647 }
1648 return vals
1649}
1650
Paul Duffin44f1d842020-06-26 20:17:02 +01001651func (m *ModuleBase) ComponentDepsMutator(BottomUpMutatorContext) {}
1652
Colin Cross4157e882019-06-06 16:57:04 -07001653func (m *ModuleBase) DepsMutator(BottomUpMutatorContext) {}
Colin Cross5f692ec2019-02-01 16:53:07 -08001654
Usta355a5872021-12-01 15:16:32 -05001655// AddProperties "registers" the provided props
1656// each value in props MUST be a pointer to a struct
Colin Cross4157e882019-06-06 16:57:04 -07001657func (m *ModuleBase) AddProperties(props ...interface{}) {
1658 m.registerProps = append(m.registerProps, props...)
Colin Cross36242852017-06-23 15:06:31 -07001659}
1660
Colin Cross4157e882019-06-06 16:57:04 -07001661func (m *ModuleBase) GetProperties() []interface{} {
1662 return m.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -08001663}
1664
Colin Cross4157e882019-06-06 16:57:04 -07001665func (m *ModuleBase) BuildParamsForTests() []BuildParams {
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001666 // Expand the references to module variables like $flags[0-9]*,
1667 // so we do not need to change many existing unit tests.
1668 // This looks like undoing the shareFlags optimization in cc's
1669 // transformSourceToObj, and should only affects unit tests.
1670 vars := m.VariablesForTests()
1671 buildParams := append([]BuildParams(nil), m.buildParams...)
Sasha Smundake198eaf2022-08-04 13:07:02 -07001672 for i := range buildParams {
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001673 newArgs := make(map[string]string)
1674 for k, v := range buildParams[i].Args {
1675 newArgs[k] = v
1676 // Replaces both ${flags1} and $flags1 syntax.
1677 if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") {
1678 if value, found := vars[v[2:len(v)-1]]; found {
1679 newArgs[k] = value
1680 }
1681 } else if strings.HasPrefix(v, "$") {
1682 if value, found := vars[v[1:]]; found {
1683 newArgs[k] = value
1684 }
1685 }
1686 }
1687 buildParams[i].Args = newArgs
1688 }
1689 return buildParams
Colin Crosscec81712017-07-13 14:43:27 -07001690}
1691
Colin Cross4157e882019-06-06 16:57:04 -07001692func (m *ModuleBase) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams {
1693 return m.ruleParams
Colin Cross4c83e5c2019-02-25 14:54:28 -08001694}
1695
Colin Cross4157e882019-06-06 16:57:04 -07001696func (m *ModuleBase) VariablesForTests() map[string]string {
1697 return m.variables
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001698}
1699
Colin Crossce75d2c2016-10-06 16:12:58 -07001700// Name returns the name of the module. It may be overridden by individual module types, for
1701// example prebuilts will prepend prebuilt_ to the name.
Colin Cross4157e882019-06-06 16:57:04 -07001702func (m *ModuleBase) Name() string {
1703 return String(m.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -07001704}
1705
Colin Cross9a362232019-07-01 15:32:45 -07001706// String returns a string that includes the module name and variants for printing during debugging.
1707func (m *ModuleBase) String() string {
1708 sb := strings.Builder{}
1709 sb.WriteString(m.commonProperties.DebugName)
1710 sb.WriteString("{")
1711 for i := range m.commonProperties.DebugMutators {
1712 if i != 0 {
1713 sb.WriteString(",")
1714 }
1715 sb.WriteString(m.commonProperties.DebugMutators[i])
1716 sb.WriteString(":")
1717 sb.WriteString(m.commonProperties.DebugVariations[i])
1718 }
1719 sb.WriteString("}")
1720 return sb.String()
1721}
1722
Colin Crossce75d2c2016-10-06 16:12:58 -07001723// BaseModuleName returns the name of the module as specified in the blueprints file.
Colin Cross4157e882019-06-06 16:57:04 -07001724func (m *ModuleBase) BaseModuleName() string {
1725 return String(m.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -07001726}
1727
Colin Cross4157e882019-06-06 16:57:04 -07001728func (m *ModuleBase) base() *ModuleBase {
1729 return m
Colin Cross3f40fa42015-01-30 17:27:36 -08001730}
1731
Paul Duffine2453c72019-05-31 14:00:04 +01001732func (m *ModuleBase) qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName {
1733 return qualifiedModuleName{pkg: ctx.ModuleDir(), name: ctx.ModuleName()}
1734}
1735
1736func (m *ModuleBase) visibilityProperties() []visibilityProperty {
Paul Duffin63c6e182019-07-24 14:24:38 +01001737 return m.visibilityPropertyInfo
1738}
1739
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001740func (m *ModuleBase) Dists() []Dist {
Paul Duffined875132020-09-02 13:08:57 +01001741 if len(m.distProperties.Dist.Targets) > 0 {
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001742 // Make a copy of the underlying Dists slice to protect against
1743 // backing array modifications with repeated calls to this method.
Paul Duffined875132020-09-02 13:08:57 +01001744 distsCopy := append([]Dist(nil), m.distProperties.Dists...)
1745 return append(distsCopy, m.distProperties.Dist)
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001746 } else {
Paul Duffined875132020-09-02 13:08:57 +01001747 return m.distProperties.Dists
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001748 }
1749}
1750
1751func (m *ModuleBase) GenerateTaggedDistFiles(ctx BaseModuleContext) TaggedDistFiles {
Paul Duffin74f05592020-11-25 16:37:46 +00001752 var distFiles TaggedDistFiles
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001753 for _, dist := range m.Dists() {
Paul Duffin74f05592020-11-25 16:37:46 +00001754 // If no tag is specified then it means to use the default dist paths so use
1755 // the special tag name which represents that.
1756 tag := proptools.StringDefault(dist.Tag, DefaultDistTag)
1757
Paul Duffinaf970a22020-11-23 23:32:56 +00001758 if outputFileProducer, ok := m.module.(OutputFileProducer); ok {
1759 // Call the OutputFiles(tag) method to get the paths associated with the tag.
1760 distFilesForTag, err := outputFileProducer.OutputFiles(tag)
Paul Duffin74f05592020-11-25 16:37:46 +00001761
Paul Duffinaf970a22020-11-23 23:32:56 +00001762 // If the tag was not supported and is not DefaultDistTag then it is an error.
1763 // Failing to find paths for DefaultDistTag is not an error. It just means
1764 // that the module type requires the legacy behavior.
1765 if err != nil && tag != DefaultDistTag {
1766 ctx.PropertyErrorf("dist.tag", "%s", err.Error())
1767 }
1768
1769 distFiles = distFiles.addPathsForTag(tag, distFilesForTag...)
1770 } else if tag != DefaultDistTag {
1771 // If the tag was specified then it is an error if the module does not
1772 // implement OutputFileProducer because there is no other way of accessing
1773 // the paths for the specified tag.
1774 ctx.PropertyErrorf("dist.tag",
1775 "tag %s not supported because the module does not implement OutputFileProducer", tag)
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001776 }
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001777 }
1778
1779 return distFiles
1780}
1781
Colin Cross4157e882019-06-06 16:57:04 -07001782func (m *ModuleBase) Target() Target {
1783 return m.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -08001784}
1785
Colin Cross4157e882019-06-06 16:57:04 -07001786func (m *ModuleBase) TargetPrimary() bool {
1787 return m.commonProperties.CompilePrimary
Colin Cross8b74d172016-09-13 09:59:14 -07001788}
1789
Colin Cross4157e882019-06-06 16:57:04 -07001790func (m *ModuleBase) MultiTargets() []Target {
1791 return m.commonProperties.CompileMultiTargets
Colin Crossee0bc3b2018-10-02 22:01:37 -07001792}
1793
Colin Cross4157e882019-06-06 16:57:04 -07001794func (m *ModuleBase) Os() OsType {
1795 return m.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -08001796}
1797
Colin Cross4157e882019-06-06 16:57:04 -07001798func (m *ModuleBase) Host() bool {
Jiyong Park1613e552020-09-14 19:43:17 +09001799 return m.Os().Class == Host
Dan Willemsen97750522016-02-09 17:43:51 -08001800}
1801
Yo Chiangbba545e2020-06-09 16:15:37 +08001802func (m *ModuleBase) Device() bool {
1803 return m.Os().Class == Device
1804}
1805
Colin Cross4157e882019-06-06 16:57:04 -07001806func (m *ModuleBase) Arch() Arch {
1807 return m.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -08001808}
1809
Colin Cross4157e882019-06-06 16:57:04 -07001810func (m *ModuleBase) ArchSpecific() bool {
1811 return m.commonProperties.ArchSpecific
Dan Willemsen0b24c742016-10-04 15:13:37 -07001812}
1813
Paul Duffin1356d8c2020-02-25 19:26:33 +00001814// True if the current variant is a CommonOS variant, false otherwise.
1815func (m *ModuleBase) IsCommonOSVariant() bool {
1816 return m.commonProperties.CommonOSVariant
1817}
1818
Colin Cross34037c62020-11-17 13:19:17 -08001819// supportsTarget returns true if the given Target is supported by the current module.
1820func (m *ModuleBase) supportsTarget(target Target) bool {
1821 switch target.Os.Class {
1822 case Host:
1823 if target.HostCross {
1824 return m.HostCrossSupported()
1825 } else {
1826 return m.HostSupported()
Colin Crossa1ad8d12016-06-01 17:09:44 -07001827 }
Colin Cross34037c62020-11-17 13:19:17 -08001828 case Device:
1829 return m.DeviceSupported()
Colin Crossa1ad8d12016-06-01 17:09:44 -07001830 default:
Jiyong Park1613e552020-09-14 19:43:17 +09001831 return false
Colin Crossa1ad8d12016-06-01 17:09:44 -07001832 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001833}
1834
Colin Cross34037c62020-11-17 13:19:17 -08001835// DeviceSupported returns true if the current module is supported and enabled for device targets,
1836// i.e. the factory method set the HostOrDeviceSupported value to include device support and
1837// the device support is enabled by default or enabled by the device_supported property.
Colin Cross4157e882019-06-06 16:57:04 -07001838func (m *ModuleBase) DeviceSupported() bool {
Colin Cross34037c62020-11-17 13:19:17 -08001839 hod := m.commonProperties.HostOrDeviceSupported
1840 // deviceEnabled is true if the device_supported property is true or the HostOrDeviceSupported
1841 // value has the deviceDefault bit set.
1842 deviceEnabled := proptools.BoolDefault(m.hostAndDeviceProperties.Device_supported, hod&deviceDefault != 0)
1843 return hod&deviceSupported != 0 && deviceEnabled
Colin Cross3f40fa42015-01-30 17:27:36 -08001844}
1845
Colin Cross34037c62020-11-17 13:19:17 -08001846// HostSupported returns true if the current module is supported and enabled for host targets,
1847// i.e. the factory method set the HostOrDeviceSupported value to include host support and
1848// the host support is enabled by default or enabled by the host_supported property.
Paul Duffine44358f2019-11-26 18:04:12 +00001849func (m *ModuleBase) HostSupported() bool {
Colin Cross34037c62020-11-17 13:19:17 -08001850 hod := m.commonProperties.HostOrDeviceSupported
1851 // hostEnabled is true if the host_supported property is true or the HostOrDeviceSupported
1852 // value has the hostDefault bit set.
1853 hostEnabled := proptools.BoolDefault(m.hostAndDeviceProperties.Host_supported, hod&hostDefault != 0)
1854 return hod&hostSupported != 0 && hostEnabled
1855}
1856
1857// HostCrossSupported returns true if the current module is supported and enabled for host cross
1858// targets, i.e. the factory method set the HostOrDeviceSupported value to include host cross
1859// support and the host cross support is enabled by default or enabled by the
1860// host_supported property.
1861func (m *ModuleBase) HostCrossSupported() bool {
1862 hod := m.commonProperties.HostOrDeviceSupported
1863 // hostEnabled is true if the host_supported property is true or the HostOrDeviceSupported
1864 // value has the hostDefault bit set.
1865 hostEnabled := proptools.BoolDefault(m.hostAndDeviceProperties.Host_supported, hod&hostDefault != 0)
1866 return hod&hostCrossSupported != 0 && hostEnabled
Paul Duffine44358f2019-11-26 18:04:12 +00001867}
1868
Colin Cross4157e882019-06-06 16:57:04 -07001869func (m *ModuleBase) Platform() bool {
Justin Yund5f6c822019-06-25 16:47:17 +09001870 return !m.DeviceSpecific() && !m.SocSpecific() && !m.ProductSpecific() && !m.SystemExtSpecific()
Jiyong Parkc678ad32018-04-10 13:07:10 +09001871}
1872
Colin Cross4157e882019-06-06 16:57:04 -07001873func (m *ModuleBase) DeviceSpecific() bool {
1874 return Bool(m.commonProperties.Device_specific)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001875}
1876
Colin Cross4157e882019-06-06 16:57:04 -07001877func (m *ModuleBase) SocSpecific() bool {
1878 return Bool(m.commonProperties.Vendor) || Bool(m.commonProperties.Proprietary) || Bool(m.commonProperties.Soc_specific)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001879}
1880
Colin Cross4157e882019-06-06 16:57:04 -07001881func (m *ModuleBase) ProductSpecific() bool {
1882 return Bool(m.commonProperties.Product_specific)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001883}
1884
Justin Yund5f6c822019-06-25 16:47:17 +09001885func (m *ModuleBase) SystemExtSpecific() bool {
1886 return Bool(m.commonProperties.System_ext_specific)
Dario Frenifd05a742018-05-29 13:28:54 +01001887}
1888
Colin Crossc2d24052020-05-13 11:05:02 -07001889// RequiresStableAPIs returns true if the module will be installed to a partition that may
1890// be updated separately from the system image.
1891func (m *ModuleBase) RequiresStableAPIs(ctx BaseModuleContext) bool {
1892 return m.SocSpecific() || m.DeviceSpecific() ||
1893 (m.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface())
1894}
1895
Bill Peckhamfff3f8a2020-03-20 18:33:20 -07001896func (m *ModuleBase) PartitionTag(config DeviceConfig) string {
1897 partition := "system"
1898 if m.SocSpecific() {
1899 // A SoC-specific module could be on the vendor partition at
1900 // "vendor" or the system partition at "system/vendor".
1901 if config.VendorPath() == "vendor" {
1902 partition = "vendor"
1903 }
1904 } else if m.DeviceSpecific() {
1905 // A device-specific module could be on the odm partition at
1906 // "odm", the vendor partition at "vendor/odm", or the system
1907 // partition at "system/vendor/odm".
1908 if config.OdmPath() == "odm" {
1909 partition = "odm"
Ramy Medhat944839a2020-03-31 22:14:52 -04001910 } else if strings.HasPrefix(config.OdmPath(), "vendor/") {
Bill Peckhamfff3f8a2020-03-20 18:33:20 -07001911 partition = "vendor"
1912 }
1913 } else if m.ProductSpecific() {
1914 // A product-specific module could be on the product partition
1915 // at "product" or the system partition at "system/product".
1916 if config.ProductPath() == "product" {
1917 partition = "product"
1918 }
1919 } else if m.SystemExtSpecific() {
1920 // A system_ext-specific module could be on the system_ext
1921 // partition at "system_ext" or the system partition at
1922 // "system/system_ext".
1923 if config.SystemExtPath() == "system_ext" {
1924 partition = "system_ext"
1925 }
1926 }
1927 return partition
1928}
1929
Colin Cross4157e882019-06-06 16:57:04 -07001930func (m *ModuleBase) Enabled() bool {
Justin Yun32f053b2020-07-31 23:07:17 +09001931 if m.commonProperties.ForcedDisabled {
1932 return false
1933 }
Colin Cross08d6f8f2020-11-19 02:33:19 +00001934 if m.commonProperties.Enabled == nil {
1935 return !m.Os().DefaultDisabled
1936 }
1937 return *m.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -08001938}
1939
Inseob Kimeec88e12020-01-22 11:11:29 +09001940func (m *ModuleBase) Disable() {
Justin Yun32f053b2020-07-31 23:07:17 +09001941 m.commonProperties.ForcedDisabled = true
Inseob Kimeec88e12020-01-22 11:11:29 +09001942}
1943
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001944// HideFromMake marks this variant so that it is not emitted in the generated Android.mk file.
1945func (m *ModuleBase) HideFromMake() {
1946 m.commonProperties.HideFromMake = true
1947}
1948
1949// IsHideFromMake returns true if HideFromMake was previously called.
1950func (m *ModuleBase) IsHideFromMake() bool {
1951 return m.commonProperties.HideFromMake == true
1952}
1953
1954// SkipInstall marks this variant to not create install rules when ctx.Install* are called.
Colin Cross4157e882019-06-06 16:57:04 -07001955func (m *ModuleBase) SkipInstall() {
1956 m.commonProperties.SkipInstall = true
Colin Crossce75d2c2016-10-06 16:12:58 -07001957}
1958
Martin Stjernholm9e7f45e2020-12-23 03:50:30 +00001959// IsSkipInstall returns true if this variant is marked to not create install
1960// rules when ctx.Install* are called.
1961func (m *ModuleBase) IsSkipInstall() bool {
1962 return m.commonProperties.SkipInstall
1963}
1964
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001965// Similar to HideFromMake, but if the AndroidMk entry would set
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01001966// LOCAL_UNINSTALLABLE_MODULE then this variant may still output that entry
1967// rather than leaving it out altogether. That happens in cases where it would
1968// have other side effects, in particular when it adds a NOTICE file target,
1969// which other install targets might depend on.
1970func (m *ModuleBase) MakeUninstallable() {
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001971 m.HideFromMake()
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01001972}
1973
Liz Kammer5ca3a622020-08-05 15:40:41 -07001974func (m *ModuleBase) ReplacedByPrebuilt() {
1975 m.commonProperties.ReplacedByPrebuilt = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001976 m.HideFromMake()
Liz Kammer5ca3a622020-08-05 15:40:41 -07001977}
1978
1979func (m *ModuleBase) IsReplacedByPrebuilt() bool {
1980 return m.commonProperties.ReplacedByPrebuilt
1981}
1982
Colin Cross4157e882019-06-06 16:57:04 -07001983func (m *ModuleBase) ExportedToMake() bool {
1984 return m.commonProperties.NamespaceExportedToMake
Jiyong Park374510b2018-03-19 18:23:01 +09001985}
1986
Justin Yun885a7de2021-06-29 20:34:53 +09001987func (m *ModuleBase) EffectiveLicenseFiles() Paths {
Bob Badour4101c712022-02-09 11:54:35 -08001988 result := make(Paths, 0, len(m.commonProperties.Effective_license_text))
1989 for _, p := range m.commonProperties.Effective_license_text {
1990 result = append(result, p.Path)
1991 }
1992 return result
Justin Yun885a7de2021-06-29 20:34:53 +09001993}
1994
Colin Crosse9fe2942020-11-10 18:12:15 -08001995// computeInstallDeps finds the installed paths of all dependencies that have a dependency
1996// tag that is annotated as needing installation via the IsInstallDepNeeded method.
Colin Crossffe6b9d2020-12-01 15:40:06 -08001997func (m *ModuleBase) computeInstallDeps(ctx ModuleContext) ([]*installPathsDepSet, []*packagingSpecsDepSet) {
Colin Cross5d583952020-11-24 16:21:24 -08001998 var installDeps []*installPathsDepSet
Colin Crossffe6b9d2020-12-01 15:40:06 -08001999 var packagingSpecs []*packagingSpecsDepSet
Colin Cross5d583952020-11-24 16:21:24 -08002000 ctx.VisitDirectDeps(func(dep Module) {
Jooyung Han8707cd72021-07-23 02:49:46 +09002001 if IsInstallDepNeeded(ctx.OtherModuleDependencyTag(dep)) && !dep.IsHideFromMake() && !dep.IsSkipInstall() {
Colin Cross5d583952020-11-24 16:21:24 -08002002 installDeps = append(installDeps, dep.base().installFilesDepSet)
Colin Crossffe6b9d2020-12-01 15:40:06 -08002003 packagingSpecs = append(packagingSpecs, dep.base().packagingSpecsDepSet)
Colin Cross897266e2020-02-13 13:22:08 -08002004 }
2005 })
Colin Cross3f40fa42015-01-30 17:27:36 -08002006
Colin Crossffe6b9d2020-12-01 15:40:06 -08002007 return installDeps, packagingSpecs
Colin Cross3f40fa42015-01-30 17:27:36 -08002008}
2009
Jiyong Park4dc2a1a2020-09-28 17:46:22 +09002010func (m *ModuleBase) FilesToInstall() InstallPaths {
Colin Cross4157e882019-06-06 16:57:04 -07002011 return m.installFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08002012}
2013
Jiyong Park073ea552020-11-09 14:08:34 +09002014func (m *ModuleBase) PackagingSpecs() []PackagingSpec {
2015 return m.packagingSpecs
2016}
2017
Colin Crossffe6b9d2020-12-01 15:40:06 -08002018func (m *ModuleBase) TransitivePackagingSpecs() []PackagingSpec {
2019 return m.packagingSpecsDepSet.ToList()
2020}
2021
Colin Cross4157e882019-06-06 16:57:04 -07002022func (m *ModuleBase) NoAddressSanitizer() bool {
2023 return m.noAddressSanitizer
Colin Cross3f40fa42015-01-30 17:27:36 -08002024}
2025
Colin Cross4157e882019-06-06 16:57:04 -07002026func (m *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -08002027 return false
2028}
2029
Jaewoong Jung0949f312019-09-11 10:25:18 -07002030func (m *ModuleBase) InstallInTestcases() bool {
2031 return false
2032}
2033
Colin Cross4157e882019-06-06 16:57:04 -07002034func (m *ModuleBase) InstallInSanitizerDir() bool {
Vishwath Mohan1dd88392017-03-29 22:00:18 -07002035 return false
2036}
2037
Yifan Hong1b3348d2020-01-21 15:53:22 -08002038func (m *ModuleBase) InstallInRamdisk() bool {
2039 return Bool(m.commonProperties.Ramdisk)
2040}
2041
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002042func (m *ModuleBase) InstallInVendorRamdisk() bool {
2043 return Bool(m.commonProperties.Vendor_ramdisk)
2044}
2045
Inseob Kim08758f02021-04-08 21:13:22 +09002046func (m *ModuleBase) InstallInDebugRamdisk() bool {
2047 return Bool(m.commonProperties.Debug_ramdisk)
2048}
2049
Colin Cross4157e882019-06-06 16:57:04 -07002050func (m *ModuleBase) InstallInRecovery() bool {
2051 return Bool(m.commonProperties.Recovery)
Jiyong Parkf9332f12018-02-01 00:54:12 +09002052}
2053
Kiyoung Kimae11c232021-07-19 11:38:04 +09002054func (m *ModuleBase) InstallInVendor() bool {
Kiyoung Kimf160f7f2022-11-29 10:58:08 +09002055 return Bool(m.commonProperties.Vendor) || Bool(m.commonProperties.Soc_specific) || Bool(m.commonProperties.Proprietary)
Kiyoung Kimae11c232021-07-19 11:38:04 +09002056}
2057
Colin Cross90ba5f42019-10-02 11:10:58 -07002058func (m *ModuleBase) InstallInRoot() bool {
2059 return false
2060}
2061
Jiyong Park87788b52020-09-01 12:37:45 +09002062func (m *ModuleBase) InstallForceOS() (*OsType, *ArchType) {
2063 return nil, nil
Colin Cross6e359402020-02-10 15:29:54 -08002064}
2065
Colin Cross4157e882019-06-06 16:57:04 -07002066func (m *ModuleBase) Owner() string {
2067 return String(m.commonProperties.Owner)
Sundong Ahn4fd04bb2018-08-31 18:01:37 +09002068}
2069
Colin Cross7228ecd2019-11-18 16:00:16 -08002070func (m *ModuleBase) setImageVariation(variant string) {
2071 m.commonProperties.ImageVariation = variant
2072}
2073
2074func (m *ModuleBase) ImageVariation() blueprint.Variation {
2075 return blueprint.Variation{
2076 Mutator: "image",
2077 Variation: m.base().commonProperties.ImageVariation,
2078 }
2079}
2080
Paul Duffin9b76c0b2020-03-12 10:24:35 +00002081func (m *ModuleBase) getVariationByMutatorName(mutator string) string {
2082 for i, v := range m.commonProperties.DebugMutators {
2083 if v == mutator {
2084 return m.commonProperties.DebugVariations[i]
2085 }
2086 }
2087
2088 return ""
2089}
2090
Yifan Hong1b3348d2020-01-21 15:53:22 -08002091func (m *ModuleBase) InRamdisk() bool {
2092 return m.base().commonProperties.ImageVariation == RamdiskVariation
2093}
2094
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002095func (m *ModuleBase) InVendorRamdisk() bool {
2096 return m.base().commonProperties.ImageVariation == VendorRamdiskVariation
2097}
2098
Inseob Kim08758f02021-04-08 21:13:22 +09002099func (m *ModuleBase) InDebugRamdisk() bool {
2100 return m.base().commonProperties.ImageVariation == DebugRamdiskVariation
2101}
2102
Colin Cross7228ecd2019-11-18 16:00:16 -08002103func (m *ModuleBase) InRecovery() bool {
2104 return m.base().commonProperties.ImageVariation == RecoveryVariation
2105}
2106
Jiyong Park6a8cf5f2019-12-30 16:31:09 +09002107func (m *ModuleBase) RequiredModuleNames() []string {
2108 return m.base().commonProperties.Required
2109}
2110
2111func (m *ModuleBase) HostRequiredModuleNames() []string {
2112 return m.base().commonProperties.Host_required
2113}
2114
2115func (m *ModuleBase) TargetRequiredModuleNames() []string {
2116 return m.base().commonProperties.Target_required
2117}
2118
Inseob Kim8471cda2019-11-15 09:59:12 +09002119func (m *ModuleBase) InitRc() Paths {
2120 return append(Paths{}, m.initRcPaths...)
2121}
2122
2123func (m *ModuleBase) VintfFragments() Paths {
2124 return append(Paths{}, m.vintfFragmentsPaths...)
2125}
2126
Yu Liu4ae55d12022-01-05 17:17:23 -08002127func (m *ModuleBase) CompileMultilib() *string {
2128 return m.base().commonProperties.Compile_multilib
2129}
2130
Colin Cross4acaea92021-12-10 23:05:02 +00002131// SetLicenseInstallMap stores the set of dependency module:location mappings for files in an
2132// apex container for use when generation the license metadata file.
2133func (m *ModuleBase) SetLicenseInstallMap(installMap []string) {
2134 m.licenseInstallMap = append(m.licenseInstallMap, installMap...)
2135}
2136
Colin Cross4157e882019-06-06 16:57:04 -07002137func (m *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Colin Cross897266e2020-02-13 13:22:08 -08002138 var allInstalledFiles InstallPaths
2139 var allCheckbuildFiles Paths
Colin Cross0875c522017-11-28 17:34:01 -08002140 ctx.VisitAllModuleVariants(func(module Module) {
2141 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -07002142 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07002143 // A module's -checkbuild phony targets should
Chih-Hung Hsieha3d135b2021-10-14 20:32:53 -07002144 // not be created if the module is not exported to make.
2145 // Those could depend on the build target and fail to compile
2146 // for the current build target.
2147 if !ctx.Config().KatiEnabled() || !shouldSkipAndroidMkProcessing(a) {
2148 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Chih-Hung Hsieha3d135b2021-10-14 20:32:53 -07002149 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002150 })
2151
Colin Cross0875c522017-11-28 17:34:01 -08002152 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -07002153
Colin Cross133ebef2020-08-14 17:38:45 -07002154 namespacePrefix := ctx.Namespace().id
Jeff Gaston088e29e2017-11-29 16:47:17 -08002155 if namespacePrefix != "" {
2156 namespacePrefix = namespacePrefix + "-"
2157 }
2158
Colin Cross3f40fa42015-01-30 17:27:36 -08002159 if len(allInstalledFiles) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07002160 name := namespacePrefix + ctx.ModuleName() + "-install"
2161 ctx.Phony(name, allInstalledFiles.Paths()...)
2162 m.installTarget = PathForPhony(ctx, name)
2163 deps = append(deps, m.installTarget)
Colin Cross9454bfa2015-03-17 13:24:18 -07002164 }
2165
2166 if len(allCheckbuildFiles) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07002167 name := namespacePrefix + ctx.ModuleName() + "-checkbuild"
2168 ctx.Phony(name, allCheckbuildFiles...)
2169 m.checkbuildTarget = PathForPhony(ctx, name)
2170 deps = append(deps, m.checkbuildTarget)
Colin Cross9454bfa2015-03-17 13:24:18 -07002171 }
2172
2173 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08002174 suffix := ""
Jingwen Chencda22c92020-11-23 00:22:30 -05002175 if ctx.Config().KatiEnabled() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08002176 suffix = "-soong"
2177 }
2178
Colin Crossc3d87d32020-06-04 13:25:17 -07002179 ctx.Phony(namespacePrefix+ctx.ModuleName()+suffix, deps...)
Colin Cross1f8c52b2015-06-16 16:38:17 -07002180
Colin Cross4157e882019-06-06 16:57:04 -07002181 m.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -08002182 }
2183}
2184
Colin Crossc34d2322020-01-03 15:23:27 -08002185func determineModuleKind(m *ModuleBase, ctx blueprint.EarlyModuleContext) moduleKind {
Colin Cross4157e882019-06-06 16:57:04 -07002186 var socSpecific = Bool(m.commonProperties.Vendor) || Bool(m.commonProperties.Proprietary) || Bool(m.commonProperties.Soc_specific)
2187 var deviceSpecific = Bool(m.commonProperties.Device_specific)
2188 var productSpecific = Bool(m.commonProperties.Product_specific)
Justin Yund5f6c822019-06-25 16:47:17 +09002189 var systemExtSpecific = Bool(m.commonProperties.System_ext_specific)
Jiyong Park2db76922017-11-08 16:03:48 +09002190
Dario Frenifd05a742018-05-29 13:28:54 +01002191 msg := "conflicting value set here"
2192 if socSpecific && deviceSpecific {
2193 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
Colin Cross4157e882019-06-06 16:57:04 -07002194 if Bool(m.commonProperties.Vendor) {
Jiyong Park2db76922017-11-08 16:03:48 +09002195 ctx.PropertyErrorf("vendor", msg)
2196 }
Colin Cross4157e882019-06-06 16:57:04 -07002197 if Bool(m.commonProperties.Proprietary) {
Jiyong Park2db76922017-11-08 16:03:48 +09002198 ctx.PropertyErrorf("proprietary", msg)
2199 }
Colin Cross4157e882019-06-06 16:57:04 -07002200 if Bool(m.commonProperties.Soc_specific) {
Jiyong Park2db76922017-11-08 16:03:48 +09002201 ctx.PropertyErrorf("soc_specific", msg)
2202 }
2203 }
2204
Justin Yund5f6c822019-06-25 16:47:17 +09002205 if productSpecific && systemExtSpecific {
2206 ctx.PropertyErrorf("product_specific", "a module cannot be specific to product and system_ext at the same time.")
2207 ctx.PropertyErrorf("system_ext_specific", msg)
Dario Frenifd05a742018-05-29 13:28:54 +01002208 }
2209
Justin Yund5f6c822019-06-25 16:47:17 +09002210 if (socSpecific || deviceSpecific) && (productSpecific || systemExtSpecific) {
Dario Frenifd05a742018-05-29 13:28:54 +01002211 if productSpecific {
2212 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
2213 } else {
Justin Yund5f6c822019-06-25 16:47:17 +09002214 ctx.PropertyErrorf("system_ext_specific", "a module cannot be specific to SoC or device and system_ext at the same time.")
Dario Frenifd05a742018-05-29 13:28:54 +01002215 }
2216 if deviceSpecific {
2217 ctx.PropertyErrorf("device_specific", msg)
2218 } else {
Colin Cross4157e882019-06-06 16:57:04 -07002219 if Bool(m.commonProperties.Vendor) {
Dario Frenifd05a742018-05-29 13:28:54 +01002220 ctx.PropertyErrorf("vendor", msg)
2221 }
Colin Cross4157e882019-06-06 16:57:04 -07002222 if Bool(m.commonProperties.Proprietary) {
Dario Frenifd05a742018-05-29 13:28:54 +01002223 ctx.PropertyErrorf("proprietary", msg)
2224 }
Colin Cross4157e882019-06-06 16:57:04 -07002225 if Bool(m.commonProperties.Soc_specific) {
Dario Frenifd05a742018-05-29 13:28:54 +01002226 ctx.PropertyErrorf("soc_specific", msg)
2227 }
2228 }
2229 }
2230
Jiyong Park2db76922017-11-08 16:03:48 +09002231 if productSpecific {
2232 return productSpecificModule
Justin Yund5f6c822019-06-25 16:47:17 +09002233 } else if systemExtSpecific {
2234 return systemExtSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +09002235 } else if deviceSpecific {
2236 return deviceSpecificModule
2237 } else if socSpecific {
2238 return socSpecificModule
2239 } else {
2240 return platformModule
2241 }
2242}
2243
Colin Crossc34d2322020-01-03 15:23:27 -08002244func (m *ModuleBase) earlyModuleContextFactory(ctx blueprint.EarlyModuleContext) earlyModuleContext {
Colin Cross1184b642019-12-30 18:43:07 -08002245 return earlyModuleContext{
Colin Crossc34d2322020-01-03 15:23:27 -08002246 EarlyModuleContext: ctx,
2247 kind: determineModuleKind(m, ctx),
2248 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -08002249 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002250}
2251
Colin Cross1184b642019-12-30 18:43:07 -08002252func (m *ModuleBase) baseModuleContextFactory(ctx blueprint.BaseModuleContext) baseModuleContext {
2253 return baseModuleContext{
2254 bp: ctx,
2255 earlyModuleContext: m.earlyModuleContextFactory(ctx),
2256 os: m.commonProperties.CompileOS,
2257 target: m.commonProperties.CompileTarget,
2258 targetPrimary: m.commonProperties.CompilePrimary,
2259 multiTargets: m.commonProperties.CompileMultiTargets,
2260 }
2261}
2262
Colin Cross4157e882019-06-06 16:57:04 -07002263func (m *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
Colin Cross25de6c32019-06-06 14:29:25 -07002264 ctx := &moduleContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -07002265 module: m.module,
Colin Crossdc35e212019-06-06 16:13:11 -07002266 bp: blueprintCtx,
Colin Cross0ea8ba82019-06-06 14:33:29 -07002267 baseModuleContext: m.baseModuleContextFactory(blueprintCtx),
Colin Cross0ea8ba82019-06-06 14:33:29 -07002268 variables: make(map[string]string),
Colin Cross3f40fa42015-01-30 17:27:36 -08002269 }
2270
Colin Crossaa1cab02022-01-28 14:49:24 -08002271 m.licenseMetadataFile = PathForModuleOut(ctx, "meta_lic")
2272
Colin Crossffe6b9d2020-12-01 15:40:06 -08002273 dependencyInstallFiles, dependencyPackagingSpecs := m.computeInstallDeps(ctx)
Colin Cross5d583952020-11-24 16:21:24 -08002274 // set m.installFilesDepSet to only the transitive dependencies to be used as the dependencies
2275 // of installed files of this module. It will be replaced by a depset including the installed
2276 // files of this module at the end for use by modules that depend on this one.
2277 m.installFilesDepSet = newInstallPathsDepSet(nil, dependencyInstallFiles)
2278
Colin Cross6c4f21f2019-06-06 15:41:36 -07002279 // Temporarily continue to call blueprintCtx.GetMissingDependencies() to maintain the previous behavior of never
2280 // reporting missing dependency errors in Blueprint when AllowMissingDependencies == true.
2281 // TODO: This will be removed once defaults modules handle missing dependency errors
2282 blueprintCtx.GetMissingDependencies()
2283
Colin Crossdc35e212019-06-06 16:13:11 -07002284 // For the final GenerateAndroidBuildActions pass, require that all visited dependencies Soong modules and
Paul Duffin1356d8c2020-02-25 19:26:33 +00002285 // are enabled. Unless the module is a CommonOS variant which may have dependencies on disabled variants
2286 // (because the dependencies are added before the modules are disabled). The
2287 // GetOsSpecificVariantsOfCommonOSVariant(...) method will ensure that the disabled variants are
2288 // ignored.
2289 ctx.baseModuleContext.strictVisitDeps = !m.IsCommonOSVariant()
Colin Crossdc35e212019-06-06 16:13:11 -07002290
Colin Cross4c83e5c2019-02-25 14:54:28 -08002291 if ctx.config.captureBuild {
2292 ctx.ruleParams = make(map[blueprint.Rule]blueprint.RuleParams)
2293 }
2294
Colin Cross67a5c132017-05-09 13:45:28 -07002295 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
2296 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -08002297 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
2298 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -07002299 }
Colin Cross0875c522017-11-28 17:34:01 -08002300 if !ctx.PrimaryArch() {
2301 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -07002302 }
Colin Cross56a83212020-09-15 18:30:11 -07002303 if apexInfo := ctx.Provider(ApexInfoProvider).(ApexInfo); !apexInfo.IsForPlatform() {
2304 suffix = append(suffix, apexInfo.ApexVariationName)
Dan Willemsenb13a9482020-02-14 11:25:54 -08002305 }
Colin Cross67a5c132017-05-09 13:45:28 -07002306
2307 ctx.Variable(pctx, "moduleDesc", desc)
2308
2309 s := ""
2310 if len(suffix) > 0 {
2311 s = " [" + strings.Join(suffix, " ") + "]"
2312 }
2313 ctx.Variable(pctx, "moduleDescSuffix", s)
2314
Dan Willemsen569edc52018-11-19 09:33:29 -08002315 // Some common property checks for properties that will be used later in androidmk.go
Paul Duffin89968e32020-11-23 18:17:03 +00002316 checkDistProperties(ctx, "dist", &m.distProperties.Dist)
Sasha Smundake198eaf2022-08-04 13:07:02 -07002317 for i := range m.distProperties.Dists {
Paul Duffin89968e32020-11-23 18:17:03 +00002318 checkDistProperties(ctx, fmt.Sprintf("dists[%d]", i), &m.distProperties.Dists[i])
Dan Willemsen569edc52018-11-19 09:33:29 -08002319 }
2320
Colin Cross4157e882019-06-06 16:57:04 -07002321 if m.Enabled() {
Jooyung Hand48f3c32019-08-23 11:18:57 +09002322 // ensure all direct android.Module deps are enabled
2323 ctx.VisitDirectDepsBlueprint(func(bm blueprint.Module) {
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +01002324 if m, ok := bm.(Module); ok {
2325 ctx.validateAndroidModule(bm, ctx.OtherModuleDependencyTag(m), ctx.baseModuleContext.strictVisitDeps)
Jooyung Hand48f3c32019-08-23 11:18:57 +09002326 }
2327 })
2328
Bob Badour37af0462021-01-07 03:34:31 +00002329 licensesPropertyFlattener(ctx)
2330 if ctx.Failed() {
2331 return
2332 }
2333
Chris Parsonsf874e462022-05-10 13:50:12 -04002334 if mixedBuildMod, handled := m.isHandledByBazel(ctx); handled {
2335 mixedBuildMod.ProcessBazelQueryResponse(ctx)
2336 } else {
2337 m.module.GenerateAndroidBuildActions(ctx)
2338 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002339 if ctx.Failed() {
2340 return
2341 }
2342
Jiyong Park4d861072021-03-03 20:02:42 +09002343 m.initRcPaths = PathsForModuleSrc(ctx, m.commonProperties.Init_rc)
2344 rcDir := PathForModuleInstall(ctx, "etc", "init")
2345 for _, src := range m.initRcPaths {
2346 ctx.PackageFile(rcDir, filepath.Base(src.String()), src)
2347 }
2348
2349 m.vintfFragmentsPaths = PathsForModuleSrc(ctx, m.commonProperties.Vintf_fragments)
2350 vintfDir := PathForModuleInstall(ctx, "etc", "vintf", "manifest")
2351 for _, src := range m.vintfFragmentsPaths {
2352 ctx.PackageFile(vintfDir, filepath.Base(src.String()), src)
2353 }
2354
Paul Duffinaf970a22020-11-23 23:32:56 +00002355 // Create the set of tagged dist files after calling GenerateAndroidBuildActions
2356 // as GenerateTaggedDistFiles() calls OutputFiles(tag) and so relies on the
2357 // output paths being set which must be done before or during
2358 // GenerateAndroidBuildActions.
2359 m.distFiles = m.GenerateTaggedDistFiles(ctx)
2360 if ctx.Failed() {
2361 return
2362 }
2363
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002364 m.installFiles = append(m.installFiles, ctx.installFiles...)
2365 m.checkbuildFiles = append(m.checkbuildFiles, ctx.checkbuildFiles...)
Jiyong Park073ea552020-11-09 14:08:34 +09002366 m.packagingSpecs = append(m.packagingSpecs, ctx.packagingSpecs...)
Colin Cross6301c3c2021-09-28 17:40:21 -07002367 m.katiInstalls = append(m.katiInstalls, ctx.katiInstalls...)
2368 m.katiSymlinks = append(m.katiSymlinks, ctx.katiSymlinks...)
Colin Crossdc35e212019-06-06 16:13:11 -07002369 } else if ctx.Config().AllowMissingDependencies() {
2370 // If the module is not enabled it will not create any build rules, nothing will call
2371 // ctx.GetMissingDependencies(), and blueprint will consider the missing dependencies to be unhandled
2372 // and report them as an error even when AllowMissingDependencies = true. Call
2373 // ctx.GetMissingDependencies() here to tell blueprint not to handle them.
2374 ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08002375 }
2376
Colin Cross4157e882019-06-06 16:57:04 -07002377 if m == ctx.FinalModule().(Module).base() {
2378 m.generateModuleTarget(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -07002379 if ctx.Failed() {
2380 return
2381 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002382 }
Colin Crosscec81712017-07-13 14:43:27 -07002383
Colin Cross5d583952020-11-24 16:21:24 -08002384 m.installFilesDepSet = newInstallPathsDepSet(m.installFiles, dependencyInstallFiles)
Colin Crossffe6b9d2020-12-01 15:40:06 -08002385 m.packagingSpecsDepSet = newPackagingSpecsDepSet(m.packagingSpecs, dependencyPackagingSpecs)
Colin Cross5d583952020-11-24 16:21:24 -08002386
Colin Crossaa1cab02022-01-28 14:49:24 -08002387 buildLicenseMetadata(ctx, m.licenseMetadataFile)
Colin Cross4acaea92021-12-10 23:05:02 +00002388
Colin Cross4157e882019-06-06 16:57:04 -07002389 m.buildParams = ctx.buildParams
2390 m.ruleParams = ctx.ruleParams
2391 m.variables = ctx.variables
Colin Cross3f40fa42015-01-30 17:27:36 -08002392}
2393
Chris Parsonsf874e462022-05-10 13:50:12 -04002394func (m *ModuleBase) isHandledByBazel(ctx ModuleContext) (MixedBuildBuildable, bool) {
Chris Parsonsf874e462022-05-10 13:50:12 -04002395 if mixedBuildMod, ok := m.module.(MixedBuildBuildable); ok {
2396 if mixedBuildMod.IsMixedBuildSupported(ctx) && MixedBuildsEnabled(ctx) {
2397 return mixedBuildMod, true
2398 }
2399 }
2400 return nil, false
2401}
2402
Paul Duffin89968e32020-11-23 18:17:03 +00002403// Check the supplied dist structure to make sure that it is valid.
2404//
2405// property - the base property, e.g. dist or dists[1], which is combined with the
2406// name of the nested property to produce the full property, e.g. dist.dest or
2407// dists[1].dir.
2408func checkDistProperties(ctx *moduleContext, property string, dist *Dist) {
2409 if dist.Dest != nil {
2410 _, err := validateSafePath(*dist.Dest)
2411 if err != nil {
2412 ctx.PropertyErrorf(property+".dest", "%s", err.Error())
2413 }
2414 }
2415 if dist.Dir != nil {
2416 _, err := validateSafePath(*dist.Dir)
2417 if err != nil {
2418 ctx.PropertyErrorf(property+".dir", "%s", err.Error())
2419 }
2420 }
2421 if dist.Suffix != nil {
2422 if strings.Contains(*dist.Suffix, "/") {
2423 ctx.PropertyErrorf(property+".suffix", "Suffix may not contain a '/' character.")
2424 }
2425 }
2426
2427}
2428
Colin Cross1184b642019-12-30 18:43:07 -08002429type earlyModuleContext struct {
Colin Crossc34d2322020-01-03 15:23:27 -08002430 blueprint.EarlyModuleContext
Colin Cross1184b642019-12-30 18:43:07 -08002431
2432 kind moduleKind
2433 config Config
2434}
2435
2436func (e *earlyModuleContext) Glob(globPattern string, excludes []string) Paths {
Liz Kammera830f3a2020-11-10 10:50:34 -08002437 return Glob(e, globPattern, excludes)
Colin Cross1184b642019-12-30 18:43:07 -08002438}
2439
2440func (e *earlyModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Liz Kammera830f3a2020-11-10 10:50:34 -08002441 return GlobFiles(e, globPattern, excludes)
Colin Cross1184b642019-12-30 18:43:07 -08002442}
2443
Ustaeabf0f32021-12-06 15:17:23 -05002444func (e *earlyModuleContext) IsSymlink(path Path) bool {
2445 fileInfo, err := e.config.fs.Lstat(path.String())
Colin Cross988414c2020-01-11 01:11:46 +00002446 if err != nil {
Ustaeabf0f32021-12-06 15:17:23 -05002447 e.ModuleErrorf("os.Lstat(%q) failed: %s", path.String(), err)
Colin Cross988414c2020-01-11 01:11:46 +00002448 }
2449 return fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink
2450}
2451
Ustaeabf0f32021-12-06 15:17:23 -05002452func (e *earlyModuleContext) Readlink(path Path) string {
2453 dest, err := e.config.fs.Readlink(path.String())
Colin Cross988414c2020-01-11 01:11:46 +00002454 if err != nil {
Ustaeabf0f32021-12-06 15:17:23 -05002455 e.ModuleErrorf("os.Readlink(%q) failed: %s", path.String(), err)
Colin Cross988414c2020-01-11 01:11:46 +00002456 }
2457 return dest
2458}
2459
Colin Cross1184b642019-12-30 18:43:07 -08002460func (e *earlyModuleContext) Module() Module {
Colin Crossc34d2322020-01-03 15:23:27 -08002461 module, _ := e.EarlyModuleContext.Module().(Module)
Colin Cross1184b642019-12-30 18:43:07 -08002462 return module
2463}
2464
2465func (e *earlyModuleContext) Config() Config {
Colin Crossc34d2322020-01-03 15:23:27 -08002466 return e.EarlyModuleContext.Config().(Config)
Colin Cross1184b642019-12-30 18:43:07 -08002467}
2468
2469func (e *earlyModuleContext) AConfig() Config {
2470 return e.config
2471}
2472
2473func (e *earlyModuleContext) DeviceConfig() DeviceConfig {
2474 return DeviceConfig{e.config.deviceConfig}
2475}
2476
2477func (e *earlyModuleContext) Platform() bool {
2478 return e.kind == platformModule
2479}
2480
2481func (e *earlyModuleContext) DeviceSpecific() bool {
2482 return e.kind == deviceSpecificModule
2483}
2484
2485func (e *earlyModuleContext) SocSpecific() bool {
2486 return e.kind == socSpecificModule
2487}
2488
2489func (e *earlyModuleContext) ProductSpecific() bool {
2490 return e.kind == productSpecificModule
2491}
2492
2493func (e *earlyModuleContext) SystemExtSpecific() bool {
2494 return e.kind == systemExtSpecificModule
2495}
2496
Colin Cross133ebef2020-08-14 17:38:45 -07002497func (e *earlyModuleContext) Namespace() *Namespace {
2498 return e.EarlyModuleContext.Namespace().(*Namespace)
2499}
2500
Colin Cross1184b642019-12-30 18:43:07 -08002501type baseModuleContext struct {
2502 bp blueprint.BaseModuleContext
2503 earlyModuleContext
Colin Crossfb0c16e2019-11-20 17:12:35 -08002504 os OsType
Colin Cross8b74d172016-09-13 09:59:14 -07002505 target Target
Colin Crossee0bc3b2018-10-02 22:01:37 -07002506 multiTargets []Target
Colin Cross8b74d172016-09-13 09:59:14 -07002507 targetPrimary bool
2508 debug bool
Colin Crossdc35e212019-06-06 16:13:11 -07002509
2510 walkPath []Module
Paul Duffinc5192442020-03-31 11:31:36 +01002511 tagPath []blueprint.DependencyTag
Colin Crossdc35e212019-06-06 16:13:11 -07002512
2513 strictVisitDeps bool // If true, enforce that all dependencies are enabled
Chris Parsons5a34ffb2021-07-21 14:34:58 -04002514
2515 bazelConversionMode bool
Colin Crossf6566ed2015-03-24 11:13:38 -07002516}
2517
Liz Kammer3bf97bd2022-04-26 09:38:20 -04002518func (b *baseModuleContext) isBazelConversionMode() bool {
Chris Parsons5a34ffb2021-07-21 14:34:58 -04002519 return b.bazelConversionMode
2520}
Paul Duffinca7f0ef2020-02-25 15:50:49 +00002521func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
2522 return b.bp.OtherModuleName(m)
2523}
2524func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string { return b.bp.OtherModuleDir(m) }
Colin Cross1184b642019-12-30 18:43:07 -08002525func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
Jooyung Hancd87c692020-02-26 02:05:18 +09002526 b.bp.OtherModuleErrorf(m, fmt, args...)
Colin Cross1184b642019-12-30 18:43:07 -08002527}
2528func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
2529 return b.bp.OtherModuleDependencyTag(m)
2530}
Paul Duffinca7f0ef2020-02-25 15:50:49 +00002531func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +00002532func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
2533 return b.bp.OtherModuleDependencyVariantExists(variations, name)
2534}
Martin Stjernholm408ffd82021-05-05 15:27:31 +01002535func (b *baseModuleContext) OtherModuleFarDependencyVariantExists(variations []blueprint.Variation, name string) bool {
2536 return b.bp.OtherModuleFarDependencyVariantExists(variations, name)
2537}
Martin Stjernholm009a9dc2020-03-05 17:34:13 +00002538func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
2539 return b.bp.OtherModuleReverseDependencyVariantExists(name)
2540}
Paul Duffinca7f0ef2020-02-25 15:50:49 +00002541func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
2542 return b.bp.OtherModuleType(m)
2543}
Colin Crossd27e7b82020-07-02 11:38:17 -07002544func (b *baseModuleContext) OtherModuleProvider(m blueprint.Module, provider blueprint.ProviderKey) interface{} {
2545 return b.bp.OtherModuleProvider(m, provider)
2546}
2547func (b *baseModuleContext) OtherModuleHasProvider(m blueprint.Module, provider blueprint.ProviderKey) bool {
2548 return b.bp.OtherModuleHasProvider(m, provider)
2549}
2550func (b *baseModuleContext) Provider(provider blueprint.ProviderKey) interface{} {
2551 return b.bp.Provider(provider)
2552}
2553func (b *baseModuleContext) HasProvider(provider blueprint.ProviderKey) bool {
2554 return b.bp.HasProvider(provider)
2555}
2556func (b *baseModuleContext) SetProvider(provider blueprint.ProviderKey, value interface{}) {
2557 b.bp.SetProvider(provider, value)
2558}
Colin Cross1184b642019-12-30 18:43:07 -08002559
2560func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
2561 return b.bp.GetDirectDepWithTag(name, tag)
2562}
2563
Paul Duffinf88d8e02020-05-07 20:21:34 +01002564func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
2565 return b.bp
2566}
2567
Colin Cross25de6c32019-06-06 14:29:25 -07002568type moduleContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -07002569 bp blueprint.ModuleContext
Colin Cross0ea8ba82019-06-06 14:33:29 -07002570 baseModuleContext
Jiyong Park073ea552020-11-09 14:08:34 +09002571 packagingSpecs []PackagingSpec
Colin Cross897266e2020-02-13 13:22:08 -08002572 installFiles InstallPaths
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002573 checkbuildFiles Paths
Colin Cross8d8f8e22016-08-03 11:57:50 -07002574 module Module
Colin Crossc3d87d32020-06-04 13:25:17 -07002575 phonies map[string]Paths
Colin Crosscec81712017-07-13 14:43:27 -07002576
Colin Cross6301c3c2021-09-28 17:40:21 -07002577 katiInstalls []katiInstall
2578 katiSymlinks []katiInstall
2579
Colin Crosscec81712017-07-13 14:43:27 -07002580 // For tests
Colin Crossae887032017-10-23 17:16:14 -07002581 buildParams []BuildParams
Colin Cross4c83e5c2019-02-25 14:54:28 -08002582 ruleParams map[blueprint.Rule]blueprint.RuleParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002583 variables map[string]string
Colin Cross6ff51382015-12-17 16:39:19 -08002584}
2585
Colin Cross6301c3c2021-09-28 17:40:21 -07002586// katiInstall stores a request from Soong to Make to create an install rule.
2587type katiInstall struct {
2588 from Path
2589 to InstallPath
2590 implicitDeps Paths
2591 orderOnlyDeps Paths
2592 executable bool
Colin Cross50ed1f92021-11-12 17:41:02 -08002593 extraFiles *extraFilesZip
Colin Cross6301c3c2021-09-28 17:40:21 -07002594
2595 absFrom string
2596}
2597
Colin Cross50ed1f92021-11-12 17:41:02 -08002598type extraFilesZip struct {
2599 zip Path
2600 dir InstallPath
2601}
2602
Colin Cross6301c3c2021-09-28 17:40:21 -07002603type katiInstalls []katiInstall
2604
2605// BuiltInstalled returns the katiInstalls in the form used by $(call copy-many-files) in Make, a
2606// space separated list of from:to tuples.
2607func (installs katiInstalls) BuiltInstalled() string {
2608 sb := strings.Builder{}
2609 for i, install := range installs {
2610 if i != 0 {
2611 sb.WriteRune(' ')
2612 }
2613 sb.WriteString(install.from.String())
2614 sb.WriteRune(':')
2615 sb.WriteString(install.to.String())
2616 }
2617 return sb.String()
2618}
2619
2620// InstallPaths returns the install path of each entry.
2621func (installs katiInstalls) InstallPaths() InstallPaths {
2622 paths := make(InstallPaths, 0, len(installs))
2623 for _, install := range installs {
2624 paths = append(paths, install.to)
2625 }
2626 return paths
2627}
2628
Colin Crossb88b3c52019-06-10 15:15:17 -07002629func (m *moduleContext) ninjaError(params BuildParams, err error) (PackageContext, BuildParams) {
2630 return pctx, BuildParams{
Colin Cross4b69c492019-06-07 13:06:06 -07002631 Rule: ErrorRule,
2632 Description: params.Description,
2633 Output: params.Output,
2634 Outputs: params.Outputs,
2635 ImplicitOutput: params.ImplicitOutput,
2636 ImplicitOutputs: params.ImplicitOutputs,
Colin Cross6ff51382015-12-17 16:39:19 -08002637 Args: map[string]string{
2638 "error": err.Error(),
2639 },
Colin Crossb88b3c52019-06-10 15:15:17 -07002640 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002641}
2642
Colin Cross25de6c32019-06-06 14:29:25 -07002643func (m *moduleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
2644 m.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -08002645}
2646
Jingwen Chence679d22020-09-23 04:30:02 +00002647func validateBuildParams(params blueprint.BuildParams) error {
2648 // Validate that the symlink outputs are declared outputs or implicit outputs
2649 allOutputs := map[string]bool{}
2650 for _, output := range params.Outputs {
2651 allOutputs[output] = true
2652 }
2653 for _, output := range params.ImplicitOutputs {
2654 allOutputs[output] = true
2655 }
2656 for _, symlinkOutput := range params.SymlinkOutputs {
2657 if !allOutputs[symlinkOutput] {
2658 return fmt.Errorf(
2659 "Symlink output %s is not a declared output or implicit output",
2660 symlinkOutput)
2661 }
2662 }
2663 return nil
2664}
2665
2666// Convert build parameters from their concrete Android types into their string representations,
2667// and combine the singular and plural fields of the same type (e.g. Output and Outputs).
Colin Cross0875c522017-11-28 17:34:01 -08002668func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002669 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002670 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -08002671 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -08002672 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002673 Outputs: params.Outputs.Strings(),
2674 ImplicitOutputs: params.ImplicitOutputs.Strings(),
Jingwen Chence679d22020-09-23 04:30:02 +00002675 SymlinkOutputs: params.SymlinkOutputs.Strings(),
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002676 Inputs: params.Inputs.Strings(),
2677 Implicits: params.Implicits.Strings(),
2678 OrderOnly: params.OrderOnly.Strings(),
Colin Cross824f1162020-07-16 13:07:51 -07002679 Validations: params.Validations.Strings(),
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002680 Args: params.Args,
2681 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002682 }
2683
Colin Cross33bfb0a2016-11-21 17:23:08 -08002684 if params.Depfile != nil {
2685 bparams.Depfile = params.Depfile.String()
2686 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002687 if params.Output != nil {
2688 bparams.Outputs = append(bparams.Outputs, params.Output.String())
2689 }
Jingwen Chence679d22020-09-23 04:30:02 +00002690 if params.SymlinkOutput != nil {
2691 bparams.SymlinkOutputs = append(bparams.SymlinkOutputs, params.SymlinkOutput.String())
2692 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002693 if params.ImplicitOutput != nil {
2694 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
2695 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002696 if params.Input != nil {
2697 bparams.Inputs = append(bparams.Inputs, params.Input.String())
2698 }
2699 if params.Implicit != nil {
2700 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
2701 }
Colin Cross824f1162020-07-16 13:07:51 -07002702 if params.Validation != nil {
2703 bparams.Validations = append(bparams.Validations, params.Validation.String())
2704 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002705
Colin Cross0b9f31f2019-02-28 11:00:01 -08002706 bparams.Outputs = proptools.NinjaEscapeList(bparams.Outputs)
2707 bparams.ImplicitOutputs = proptools.NinjaEscapeList(bparams.ImplicitOutputs)
Jingwen Chence679d22020-09-23 04:30:02 +00002708 bparams.SymlinkOutputs = proptools.NinjaEscapeList(bparams.SymlinkOutputs)
Colin Cross0b9f31f2019-02-28 11:00:01 -08002709 bparams.Inputs = proptools.NinjaEscapeList(bparams.Inputs)
2710 bparams.Implicits = proptools.NinjaEscapeList(bparams.Implicits)
2711 bparams.OrderOnly = proptools.NinjaEscapeList(bparams.OrderOnly)
Colin Cross824f1162020-07-16 13:07:51 -07002712 bparams.Validations = proptools.NinjaEscapeList(bparams.Validations)
2713 bparams.Depfile = proptools.NinjaEscape(bparams.Depfile)
Colin Crossfe4bc362018-09-12 10:02:13 -07002714
Colin Cross0875c522017-11-28 17:34:01 -08002715 return bparams
2716}
2717
Colin Cross25de6c32019-06-06 14:29:25 -07002718func (m *moduleContext) Variable(pctx PackageContext, name, value string) {
2719 if m.config.captureBuild {
2720 m.variables[name] = value
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002721 }
2722
Colin Crossdc35e212019-06-06 16:13:11 -07002723 m.bp.Variable(pctx.PackageContext, name, value)
Colin Cross0875c522017-11-28 17:34:01 -08002724}
2725
Colin Cross25de6c32019-06-06 14:29:25 -07002726func (m *moduleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
Colin Cross0875c522017-11-28 17:34:01 -08002727 argNames ...string) blueprint.Rule {
2728
Ramy Medhat944839a2020-03-31 22:14:52 -04002729 if m.config.UseRemoteBuild() {
2730 if params.Pool == nil {
2731 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
2732 // jobs to the local parallelism value
2733 params.Pool = localPool
2734 } else if params.Pool == remotePool {
2735 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
2736 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
2737 // parallelism.
2738 params.Pool = nil
2739 }
Colin Cross2e2dbc22019-09-25 13:31:46 -07002740 }
2741
Colin Crossdc35e212019-06-06 16:13:11 -07002742 rule := m.bp.Rule(pctx.PackageContext, name, params, argNames...)
Colin Cross4c83e5c2019-02-25 14:54:28 -08002743
Colin Cross25de6c32019-06-06 14:29:25 -07002744 if m.config.captureBuild {
2745 m.ruleParams[rule] = params
Colin Cross4c83e5c2019-02-25 14:54:28 -08002746 }
2747
2748 return rule
Colin Cross0875c522017-11-28 17:34:01 -08002749}
2750
Colin Cross25de6c32019-06-06 14:29:25 -07002751func (m *moduleContext) Build(pctx PackageContext, params BuildParams) {
Colin Crossb88b3c52019-06-10 15:15:17 -07002752 if params.Description != "" {
2753 params.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
2754 }
2755
2756 if missingDeps := m.GetMissingDependencies(); len(missingDeps) > 0 {
2757 pctx, params = m.ninjaError(params, fmt.Errorf("module %s missing dependencies: %s\n",
2758 m.ModuleName(), strings.Join(missingDeps, ", ")))
2759 }
2760
Colin Cross25de6c32019-06-06 14:29:25 -07002761 if m.config.captureBuild {
2762 m.buildParams = append(m.buildParams, params)
Colin Cross0875c522017-11-28 17:34:01 -08002763 }
2764
Jingwen Chence679d22020-09-23 04:30:02 +00002765 bparams := convertBuildParams(params)
2766 err := validateBuildParams(bparams)
2767 if err != nil {
2768 m.ModuleErrorf(
2769 "%s: build parameter validation failed: %s",
2770 m.ModuleName(),
2771 err.Error())
2772 }
2773 m.bp.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002774}
Colin Crossc3d87d32020-06-04 13:25:17 -07002775
2776func (m *moduleContext) Phony(name string, deps ...Path) {
2777 addPhony(m.config, name, deps...)
2778}
2779
Colin Cross25de6c32019-06-06 14:29:25 -07002780func (m *moduleContext) GetMissingDependencies() []string {
Colin Cross6c4f21f2019-06-06 15:41:36 -07002781 var missingDeps []string
2782 missingDeps = append(missingDeps, m.Module().base().commonProperties.MissingDeps...)
Colin Crossdc35e212019-06-06 16:13:11 -07002783 missingDeps = append(missingDeps, m.bp.GetMissingDependencies()...)
Colin Cross6c4f21f2019-06-06 15:41:36 -07002784 missingDeps = FirstUniqueStrings(missingDeps)
2785 return missingDeps
Colin Cross6ff51382015-12-17 16:39:19 -08002786}
2787
Colin Crossdc35e212019-06-06 16:13:11 -07002788func (b *baseModuleContext) AddMissingDependencies(deps []string) {
Dan Willemsen6553f5e2016-03-10 18:14:25 -08002789 if deps != nil {
Colin Crossdc35e212019-06-06 16:13:11 -07002790 missingDeps := &b.Module().base().commonProperties.MissingDeps
Colin Cross6c4f21f2019-06-06 15:41:36 -07002791 *missingDeps = append(*missingDeps, deps...)
2792 *missingDeps = FirstUniqueStrings(*missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -08002793 }
2794}
2795
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +01002796type AllowDisabledModuleDependency interface {
2797 blueprint.DependencyTag
2798 AllowDisabledModuleDependency(target Module) bool
2799}
2800
2801func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, tag blueprint.DependencyTag, strict bool) Module {
Colin Crossd11fcda2017-10-23 17:59:01 -07002802 aModule, _ := module.(Module)
Colin Crossdc35e212019-06-06 16:13:11 -07002803
2804 if !strict {
2805 return aModule
2806 }
2807
Colin Cross380c69a2019-06-10 17:49:58 +00002808 if aModule == nil {
Liz Kammer55146982022-01-24 16:17:30 -05002809 b.ModuleErrorf("module %q (%#v) not an android module", b.OtherModuleName(module), tag)
Colin Cross380c69a2019-06-10 17:49:58 +00002810 return nil
2811 }
2812
2813 if !aModule.Enabled() {
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +01002814 if t, ok := tag.(AllowDisabledModuleDependency); !ok || !t.AllowDisabledModuleDependency(aModule) {
2815 if b.Config().AllowMissingDependencies() {
2816 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
2817 } else {
2818 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
2819 }
Colin Cross380c69a2019-06-10 17:49:58 +00002820 }
2821 return nil
2822 }
Colin Crossd11fcda2017-10-23 17:59:01 -07002823 return aModule
2824}
2825
Liz Kammer2b50ce62021-04-26 15:47:28 -04002826type dep struct {
2827 mod blueprint.Module
2828 tag blueprint.DependencyTag
2829}
2830
2831func (b *baseModuleContext) getDirectDepsInternal(name string, tag blueprint.DependencyTag) []dep {
Jiyong Parkf2976302019-04-17 21:47:37 +09002832 var deps []dep
Colin Crossdc35e212019-06-06 16:13:11 -07002833 b.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Liz Kammer356f7d42021-01-26 09:18:53 -05002834 if aModule, _ := module.(Module); aModule != nil {
2835 if aModule.base().BaseModuleName() == name {
2836 returnedTag := b.bp.OtherModuleDependencyTag(aModule)
2837 if tag == nil || returnedTag == tag {
2838 deps = append(deps, dep{aModule, returnedTag})
2839 }
2840 }
2841 } else if b.bp.OtherModuleName(module) == name {
2842 returnedTag := b.bp.OtherModuleDependencyTag(module)
Jiyong Parkf2976302019-04-17 21:47:37 +09002843 if tag == nil || returnedTag == tag {
Liz Kammer356f7d42021-01-26 09:18:53 -05002844 deps = append(deps, dep{module, returnedTag})
Jiyong Parkf2976302019-04-17 21:47:37 +09002845 }
2846 }
2847 })
Liz Kammer2b50ce62021-04-26 15:47:28 -04002848 return deps
2849}
2850
2851func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
2852 deps := b.getDirectDepsInternal(name, tag)
Jiyong Parkf2976302019-04-17 21:47:37 +09002853 if len(deps) == 1 {
2854 return deps[0].mod, deps[0].tag
2855 } else if len(deps) >= 2 {
2856 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
Colin Crossdc35e212019-06-06 16:13:11 -07002857 name, b.ModuleName()))
Jiyong Parkf2976302019-04-17 21:47:37 +09002858 } else {
2859 return nil, nil
2860 }
2861}
2862
Liz Kammer2b50ce62021-04-26 15:47:28 -04002863func (b *baseModuleContext) getDirectDepFirstTag(name string) (blueprint.Module, blueprint.DependencyTag) {
2864 foundDeps := b.getDirectDepsInternal(name, nil)
2865 deps := map[blueprint.Module]bool{}
2866 for _, dep := range foundDeps {
2867 deps[dep.mod] = true
2868 }
2869 if len(deps) == 1 {
2870 return foundDeps[0].mod, foundDeps[0].tag
2871 } else if len(deps) >= 2 {
2872 // this could happen if two dependencies have the same name in different namespaces
2873 // TODO(b/186554727): this should not occur if namespaces are handled within
2874 // getDirectDepsInternal.
2875 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
2876 name, b.ModuleName()))
2877 } else {
2878 return nil, nil
2879 }
2880}
2881
Colin Crossdc35e212019-06-06 16:13:11 -07002882func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
Colin Cross0ef08162019-05-01 15:50:51 -07002883 var deps []Module
Colin Crossdc35e212019-06-06 16:13:11 -07002884 b.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross25de6c32019-06-06 14:29:25 -07002885 if aModule, _ := module.(Module); aModule != nil {
Colin Cross1184b642019-12-30 18:43:07 -08002886 if b.bp.OtherModuleDependencyTag(aModule) == tag {
Colin Cross0ef08162019-05-01 15:50:51 -07002887 deps = append(deps, aModule)
2888 }
2889 }
2890 })
2891 return deps
2892}
2893
Colin Cross25de6c32019-06-06 14:29:25 -07002894func (m *moduleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
2895 module, _ := m.getDirectDepInternal(name, tag)
2896 return module
Jiyong Parkf2976302019-04-17 21:47:37 +09002897}
2898
Liz Kammer2b50ce62021-04-26 15:47:28 -04002899// GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
2900// name, or nil if none exists. If there are multiple dependencies on the same module it returns the
2901// first DependencyTag.
Colin Crossdc35e212019-06-06 16:13:11 -07002902func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) {
Liz Kammer2b50ce62021-04-26 15:47:28 -04002903 return b.getDirectDepFirstTag(name)
Jiyong Parkf2976302019-04-17 21:47:37 +09002904}
2905
Chris Parsons5a34ffb2021-07-21 14:34:58 -04002906func (b *baseModuleContext) ModuleFromName(name string) (blueprint.Module, bool) {
Liz Kammer3bf97bd2022-04-26 09:38:20 -04002907 if !b.isBazelConversionMode() {
Chris Parsons5a34ffb2021-07-21 14:34:58 -04002908 panic("cannot call ModuleFromName if not in bazel conversion mode")
2909 }
Chris Parsonsa66c0b52021-07-23 11:02:07 -04002910 if moduleName, _ := SrcIsModuleWithTag(name); moduleName != "" {
Chris Parsons5a34ffb2021-07-21 14:34:58 -04002911 return b.bp.ModuleFromName(moduleName)
2912 } else {
2913 return b.bp.ModuleFromName(name)
2914 }
2915}
2916
Colin Crossdc35e212019-06-06 16:13:11 -07002917func (b *baseModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002918 b.bp.VisitDirectDeps(visit)
Colin Cross35143d02017-11-16 00:11:20 -08002919}
2920
Colin Crossdc35e212019-06-06 16:13:11 -07002921func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002922 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +01002923 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Crossd11fcda2017-10-23 17:59:01 -07002924 visit(aModule)
2925 }
2926 })
2927}
2928
Colin Crossdc35e212019-06-06 16:13:11 -07002929func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002930 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Liz Kammer55146982022-01-24 16:17:30 -05002931 if b.bp.OtherModuleDependencyTag(module) == tag {
2932 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Crossee6143c2017-12-30 17:54:27 -08002933 visit(aModule)
2934 }
2935 }
2936 })
2937}
2938
Colin Crossdc35e212019-06-06 16:13:11 -07002939func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002940 b.bp.VisitDirectDepsIf(
Colin Crossd11fcda2017-10-23 17:59:01 -07002941 // pred
2942 func(module blueprint.Module) bool {
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +01002943 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Crossd11fcda2017-10-23 17:59:01 -07002944 return pred(aModule)
2945 } else {
2946 return false
2947 }
2948 },
2949 // visit
2950 func(module blueprint.Module) {
2951 visit(module.(Module))
2952 })
2953}
2954
Colin Crossdc35e212019-06-06 16:13:11 -07002955func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002956 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +01002957 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Crossd11fcda2017-10-23 17:59:01 -07002958 visit(aModule)
2959 }
2960 })
2961}
2962
Colin Crossdc35e212019-06-06 16:13:11 -07002963func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002964 b.bp.VisitDepsDepthFirstIf(
Colin Crossd11fcda2017-10-23 17:59:01 -07002965 // pred
2966 func(module blueprint.Module) bool {
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +01002967 if aModule := b.validateAndroidModule(module, b.bp.OtherModuleDependencyTag(module), b.strictVisitDeps); aModule != nil {
Colin Crossd11fcda2017-10-23 17:59:01 -07002968 return pred(aModule)
2969 } else {
2970 return false
2971 }
2972 },
2973 // visit
2974 func(module blueprint.Module) {
2975 visit(module.(Module))
2976 })
2977}
2978
Colin Crossdc35e212019-06-06 16:13:11 -07002979func (b *baseModuleContext) WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool) {
Colin Cross1184b642019-12-30 18:43:07 -08002980 b.bp.WalkDeps(visit)
Alex Light778127a2019-02-27 14:19:50 -08002981}
2982
Colin Crossdc35e212019-06-06 16:13:11 -07002983func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
2984 b.walkPath = []Module{b.Module()}
Paul Duffinc5192442020-03-31 11:31:36 +01002985 b.tagPath = []blueprint.DependencyTag{}
Colin Cross1184b642019-12-30 18:43:07 -08002986 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
Colin Crossdc35e212019-06-06 16:13:11 -07002987 childAndroidModule, _ := child.(Module)
2988 parentAndroidModule, _ := parent.(Module)
Colin Crossd11fcda2017-10-23 17:59:01 -07002989 if childAndroidModule != nil && parentAndroidModule != nil {
Colin Crossdc35e212019-06-06 16:13:11 -07002990 // record walkPath before visit
2991 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
2992 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
Paul Duffinc5192442020-03-31 11:31:36 +01002993 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
Colin Crossdc35e212019-06-06 16:13:11 -07002994 }
2995 b.walkPath = append(b.walkPath, childAndroidModule)
Paul Duffinc5192442020-03-31 11:31:36 +01002996 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
Colin Crossd11fcda2017-10-23 17:59:01 -07002997 return visit(childAndroidModule, parentAndroidModule)
2998 } else {
2999 return false
3000 }
3001 })
3002}
3003
Colin Crossdc35e212019-06-06 16:13:11 -07003004func (b *baseModuleContext) GetWalkPath() []Module {
3005 return b.walkPath
3006}
3007
Paul Duffinc5192442020-03-31 11:31:36 +01003008func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
3009 return b.tagPath
3010}
3011
Colin Cross4dfacf92020-09-16 19:22:27 -07003012func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
3013 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
3014 visit(module.(Module))
3015 })
3016}
3017
3018func (b *baseModuleContext) PrimaryModule() Module {
3019 return b.bp.PrimaryModule().(Module)
3020}
3021
3022func (b *baseModuleContext) FinalModule() Module {
3023 return b.bp.FinalModule().(Module)
3024}
3025
Bob Badour07065cd2021-02-05 19:59:11 -08003026// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
3027func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
3028 if tag == licenseKindTag {
3029 return true
3030 } else if tag == licensesTag {
3031 return true
3032 }
3033 return false
3034}
3035
Jiyong Park1c7e9622020-05-07 16:12:13 +09003036// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
3037// a dependency tag.
Colin Cross6e511a92020-07-27 21:26:48 -07003038var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
Jiyong Park1c7e9622020-05-07 16:12:13 +09003039
3040// PrettyPrintTag returns string representation of the tag, but prefers
3041// custom String() method if available.
3042func PrettyPrintTag(tag blueprint.DependencyTag) string {
3043 // Use tag's custom String() method if available.
3044 if stringer, ok := tag.(fmt.Stringer); ok {
3045 return stringer.String()
3046 }
3047
3048 // Otherwise, get a default string representation of the tag's struct.
Colin Cross6e511a92020-07-27 21:26:48 -07003049 tagString := fmt.Sprintf("%T: %+v", tag, tag)
Jiyong Park1c7e9622020-05-07 16:12:13 +09003050
3051 // Remove the boilerplate from BaseDependencyTag as it adds no value.
3052 tagString = tagCleaner.ReplaceAllString(tagString, "")
3053 return tagString
3054}
3055
3056func (b *baseModuleContext) GetPathString(skipFirst bool) string {
3057 sb := strings.Builder{}
3058 tagPath := b.GetTagPath()
3059 walkPath := b.GetWalkPath()
3060 if !skipFirst {
3061 sb.WriteString(walkPath[0].String())
3062 }
3063 for i, m := range walkPath[1:] {
3064 sb.WriteString("\n")
3065 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
3066 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
3067 }
3068 return sb.String()
3069}
3070
Colin Crossdc35e212019-06-06 16:13:11 -07003071func (m *moduleContext) ModuleSubDir() string {
3072 return m.bp.ModuleSubDir()
Colin Cross0875c522017-11-28 17:34:01 -08003073}
3074
Colin Cross0ea8ba82019-06-06 14:33:29 -07003075func (b *baseModuleContext) Target() Target {
Colin Cross25de6c32019-06-06 14:29:25 -07003076 return b.target
Colin Crossa1ad8d12016-06-01 17:09:44 -07003077}
3078
Colin Cross0ea8ba82019-06-06 14:33:29 -07003079func (b *baseModuleContext) TargetPrimary() bool {
Colin Cross25de6c32019-06-06 14:29:25 -07003080 return b.targetPrimary
Colin Cross8b74d172016-09-13 09:59:14 -07003081}
3082
Colin Cross0ea8ba82019-06-06 14:33:29 -07003083func (b *baseModuleContext) MultiTargets() []Target {
Colin Cross25de6c32019-06-06 14:29:25 -07003084 return b.multiTargets
Colin Crossee0bc3b2018-10-02 22:01:37 -07003085}
3086
Colin Cross0ea8ba82019-06-06 14:33:29 -07003087func (b *baseModuleContext) Arch() Arch {
Colin Cross25de6c32019-06-06 14:29:25 -07003088 return b.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -08003089}
3090
Colin Cross0ea8ba82019-06-06 14:33:29 -07003091func (b *baseModuleContext) Os() OsType {
Colin Crossfb0c16e2019-11-20 17:12:35 -08003092 return b.os
Dan Willemsen490fd492015-11-24 17:53:15 -08003093}
3094
Colin Cross0ea8ba82019-06-06 14:33:29 -07003095func (b *baseModuleContext) Host() bool {
Jiyong Park1613e552020-09-14 19:43:17 +09003096 return b.os.Class == Host
Colin Crossf6566ed2015-03-24 11:13:38 -07003097}
3098
Colin Cross0ea8ba82019-06-06 14:33:29 -07003099func (b *baseModuleContext) Device() bool {
Colin Crossfb0c16e2019-11-20 17:12:35 -08003100 return b.os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -07003101}
3102
Colin Cross0ea8ba82019-06-06 14:33:29 -07003103func (b *baseModuleContext) Darwin() bool {
Colin Crossfb0c16e2019-11-20 17:12:35 -08003104 return b.os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -07003105}
3106
Colin Cross0ea8ba82019-06-06 14:33:29 -07003107func (b *baseModuleContext) Windows() bool {
Colin Crossfb0c16e2019-11-20 17:12:35 -08003108 return b.os == Windows
Colin Cross3edeee12017-04-04 12:59:48 -07003109}
3110
Colin Cross0ea8ba82019-06-06 14:33:29 -07003111func (b *baseModuleContext) Debug() bool {
Colin Cross25de6c32019-06-06 14:29:25 -07003112 return b.debug
Colin Crossf6566ed2015-03-24 11:13:38 -07003113}
3114
Colin Cross0ea8ba82019-06-06 14:33:29 -07003115func (b *baseModuleContext) PrimaryArch() bool {
Colin Cross25de6c32019-06-06 14:29:25 -07003116 if len(b.config.Targets[b.target.Os]) <= 1 {
Colin Cross67a5c132017-05-09 13:45:28 -07003117 return true
3118 }
Colin Cross25de6c32019-06-06 14:29:25 -07003119 return b.target.Arch.ArchType == b.config.Targets[b.target.Os][0].Arch.ArchType
Colin Cross1e7d3702016-08-24 15:25:47 -07003120}
3121
Jiyong Park5baac542018-08-28 09:55:37 +09003122// Makes this module a platform module, i.e. not specific to soc, device,
Justin Yund5f6c822019-06-25 16:47:17 +09003123// product, or system_ext.
Colin Cross4157e882019-06-06 16:57:04 -07003124func (m *ModuleBase) MakeAsPlatform() {
3125 m.commonProperties.Vendor = boolPtr(false)
3126 m.commonProperties.Proprietary = boolPtr(false)
3127 m.commonProperties.Soc_specific = boolPtr(false)
3128 m.commonProperties.Product_specific = boolPtr(false)
Justin Yund5f6c822019-06-25 16:47:17 +09003129 m.commonProperties.System_ext_specific = boolPtr(false)
Jiyong Park5baac542018-08-28 09:55:37 +09003130}
3131
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09003132func (m *ModuleBase) MakeAsSystemExt() {
Jooyung Han91df2082019-11-20 01:49:42 +09003133 m.commonProperties.Vendor = boolPtr(false)
3134 m.commonProperties.Proprietary = boolPtr(false)
3135 m.commonProperties.Soc_specific = boolPtr(false)
3136 m.commonProperties.Product_specific = boolPtr(false)
3137 m.commonProperties.System_ext_specific = boolPtr(true)
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09003138}
3139
Jooyung Han344d5432019-08-23 11:17:39 +09003140// IsNativeBridgeSupported returns true if "native_bridge_supported" is explicitly set as "true"
3141func (m *ModuleBase) IsNativeBridgeSupported() bool {
3142 return proptools.Bool(m.commonProperties.Native_bridge_supported)
3143}
3144
Colin Cross25de6c32019-06-06 14:29:25 -07003145func (m *moduleContext) InstallInData() bool {
3146 return m.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08003147}
3148
Jaewoong Jung0949f312019-09-11 10:25:18 -07003149func (m *moduleContext) InstallInTestcases() bool {
3150 return m.module.InstallInTestcases()
3151}
3152
Colin Cross25de6c32019-06-06 14:29:25 -07003153func (m *moduleContext) InstallInSanitizerDir() bool {
3154 return m.module.InstallInSanitizerDir()
Vishwath Mohan1dd88392017-03-29 22:00:18 -07003155}
3156
Yifan Hong1b3348d2020-01-21 15:53:22 -08003157func (m *moduleContext) InstallInRamdisk() bool {
3158 return m.module.InstallInRamdisk()
3159}
3160
Yifan Hong60e0cfb2020-10-21 15:17:56 -07003161func (m *moduleContext) InstallInVendorRamdisk() bool {
3162 return m.module.InstallInVendorRamdisk()
3163}
3164
Inseob Kim08758f02021-04-08 21:13:22 +09003165func (m *moduleContext) InstallInDebugRamdisk() bool {
3166 return m.module.InstallInDebugRamdisk()
3167}
3168
Colin Cross25de6c32019-06-06 14:29:25 -07003169func (m *moduleContext) InstallInRecovery() bool {
3170 return m.module.InstallInRecovery()
Jiyong Parkf9332f12018-02-01 00:54:12 +09003171}
3172
Colin Cross90ba5f42019-10-02 11:10:58 -07003173func (m *moduleContext) InstallInRoot() bool {
3174 return m.module.InstallInRoot()
3175}
3176
Jiyong Park87788b52020-09-01 12:37:45 +09003177func (m *moduleContext) InstallForceOS() (*OsType, *ArchType) {
Colin Cross6e359402020-02-10 15:29:54 -08003178 return m.module.InstallForceOS()
3179}
3180
Kiyoung Kimae11c232021-07-19 11:38:04 +09003181func (m *moduleContext) InstallInVendor() bool {
3182 return m.module.InstallInVendor()
3183}
3184
Colin Crossa9c8c9f2020-12-16 10:20:23 -08003185func (m *moduleContext) skipInstall() bool {
Colin Cross25de6c32019-06-06 14:29:25 -07003186 if m.module.base().commonProperties.SkipInstall {
Colin Cross893d8162017-04-26 17:34:03 -07003187 return true
3188 }
3189
Colin Crossa9c8c9f2020-12-16 10:20:23 -08003190 if m.module.base().commonProperties.HideFromMake {
3191 return true
3192 }
3193
Colin Cross3607f212018-05-07 15:28:05 -07003194 // We'll need a solution for choosing which of modules with the same name in different
3195 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
3196 // list of namespaces to install in a Soong-only build.
Colin Cross25de6c32019-06-06 14:29:25 -07003197 if !m.module.base().commonProperties.NamespaceExportedToMake {
Colin Cross3607f212018-05-07 15:28:05 -07003198 return true
3199 }
3200
Colin Cross893d8162017-04-26 17:34:03 -07003201 return false
3202}
3203
Colin Cross70dda7e2019-10-01 22:05:35 -07003204func (m *moduleContext) InstallFile(installPath InstallPath, name string, srcPath Path,
3205 deps ...Path) InstallPath {
Colin Cross50ed1f92021-11-12 17:41:02 -08003206 return m.installFile(installPath, name, srcPath, deps, false, nil)
Colin Cross5c517922017-08-31 12:29:17 -07003207}
3208
Colin Cross70dda7e2019-10-01 22:05:35 -07003209func (m *moduleContext) InstallExecutable(installPath InstallPath, name string, srcPath Path,
3210 deps ...Path) InstallPath {
Colin Cross50ed1f92021-11-12 17:41:02 -08003211 return m.installFile(installPath, name, srcPath, deps, true, nil)
3212}
3213
3214func (m *moduleContext) InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path,
3215 extraZip Path, deps ...Path) InstallPath {
3216 return m.installFile(installPath, name, srcPath, deps, false, &extraFilesZip{
3217 zip: extraZip,
3218 dir: installPath,
3219 })
Colin Cross5c517922017-08-31 12:29:17 -07003220}
3221
Colin Cross41589502020-12-01 14:00:21 -08003222func (m *moduleContext) PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec {
3223 fullInstallPath := installPath.Join(m, name)
3224 return m.packageFile(fullInstallPath, srcPath, false)
3225}
3226
3227func (m *moduleContext) packageFile(fullInstallPath InstallPath, srcPath Path, executable bool) PackagingSpec {
Dan Willemsen9fe14102021-07-13 21:52:04 -07003228 licenseFiles := m.Module().EffectiveLicenseFiles()
Colin Cross41589502020-12-01 14:00:21 -08003229 spec := PackagingSpec{
Dan Willemsen9fe14102021-07-13 21:52:04 -07003230 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
3231 srcPath: srcPath,
3232 symlinkTarget: "",
3233 executable: executable,
3234 effectiveLicenseFiles: &licenseFiles,
Jooyung Han99c5fe62022-03-21 15:13:38 +09003235 partition: fullInstallPath.partition,
Colin Cross41589502020-12-01 14:00:21 -08003236 }
3237 m.packagingSpecs = append(m.packagingSpecs, spec)
3238 return spec
3239}
3240
Colin Cross50ed1f92021-11-12 17:41:02 -08003241func (m *moduleContext) installFile(installPath InstallPath, name string, srcPath Path, deps []Path,
3242 executable bool, extraZip *extraFilesZip) InstallPath {
Colin Cross35cec122015-04-02 14:37:16 -07003243
Colin Cross25de6c32019-06-06 14:29:25 -07003244 fullInstallPath := installPath.Join(m, name)
David Srbecky07656412020-06-04 01:26:16 +01003245 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08003246
Colin Crossa9c8c9f2020-12-16 10:20:23 -08003247 if !m.skipInstall() {
Colin Cross5d583952020-11-24 16:21:24 -08003248 deps = append(deps, m.module.base().installFilesDepSet.ToList().Paths()...)
Colin Cross35cec122015-04-02 14:37:16 -07003249
Colin Cross89562dc2016-10-03 17:47:19 -07003250 var implicitDeps, orderOnlyDeps Paths
3251
Colin Cross25de6c32019-06-06 14:29:25 -07003252 if m.Host() {
Colin Cross89562dc2016-10-03 17:47:19 -07003253 // Installed host modules might be used during the build, depend directly on their
3254 // dependencies so their timestamp is updated whenever their dependency is updated
3255 implicitDeps = deps
3256 } else {
3257 orderOnlyDeps = deps
3258 }
3259
Colin Crossc68db4b2021-11-11 18:59:15 -08003260 if m.Config().KatiEnabled() {
Colin Cross6301c3c2021-09-28 17:40:21 -07003261 // When creating the install rule in Soong but embedding in Make, write the rule to a
3262 // makefile instead of directly to the ninja file so that main.mk can add the
3263 // dependencies from the `required` property that are hard to resolve in Soong.
3264 m.katiInstalls = append(m.katiInstalls, katiInstall{
3265 from: srcPath,
3266 to: fullInstallPath,
3267 implicitDeps: implicitDeps,
3268 orderOnlyDeps: orderOnlyDeps,
3269 executable: executable,
Colin Cross50ed1f92021-11-12 17:41:02 -08003270 extraFiles: extraZip,
Colin Cross6301c3c2021-09-28 17:40:21 -07003271 })
3272 } else {
3273 rule := Cp
3274 if executable {
3275 rule = CpExecutable
3276 }
Jiyong Park073ea552020-11-09 14:08:34 +09003277
Colin Cross50ed1f92021-11-12 17:41:02 -08003278 extraCmds := ""
3279 if extraZip != nil {
Romain Jobredeaux1cef6292022-05-19 11:11:51 -04003280 extraCmds += fmt.Sprintf(" && ( unzip -qDD -d '%s' '%s' 2>&1 | grep -v \"zipfile is empty\"; exit $${PIPESTATUS[0]} )",
Colin Cross50ed1f92021-11-12 17:41:02 -08003281 extraZip.dir.String(), extraZip.zip.String())
Romain Jobredeaux1cef6292022-05-19 11:11:51 -04003282 extraCmds += " || ( code=$$?; if [ $$code -ne 0 -a $$code -ne 1 ]; then exit $$code; fi )"
Colin Cross50ed1f92021-11-12 17:41:02 -08003283 implicitDeps = append(implicitDeps, extraZip.zip)
3284 }
3285
Colin Cross6301c3c2021-09-28 17:40:21 -07003286 m.Build(pctx, BuildParams{
3287 Rule: rule,
3288 Description: "install " + fullInstallPath.Base(),
3289 Output: fullInstallPath,
3290 Input: srcPath,
3291 Implicits: implicitDeps,
3292 OrderOnly: orderOnlyDeps,
3293 Default: !m.Config().KatiEnabled(),
Colin Cross50ed1f92021-11-12 17:41:02 -08003294 Args: map[string]string{
3295 "extraCmds": extraCmds,
3296 },
Colin Cross6301c3c2021-09-28 17:40:21 -07003297 })
3298 }
Colin Cross3f40fa42015-01-30 17:27:36 -08003299
Colin Cross25de6c32019-06-06 14:29:25 -07003300 m.installFiles = append(m.installFiles, fullInstallPath)
Dan Willemsen322acaf2016-01-12 23:07:05 -08003301 }
Jiyong Park073ea552020-11-09 14:08:34 +09003302
Colin Cross41589502020-12-01 14:00:21 -08003303 m.packageFile(fullInstallPath, srcPath, executable)
Jiyong Park073ea552020-11-09 14:08:34 +09003304
Colin Cross25de6c32019-06-06 14:29:25 -07003305 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08003306
Colin Cross35cec122015-04-02 14:37:16 -07003307 return fullInstallPath
3308}
3309
Colin Cross70dda7e2019-10-01 22:05:35 -07003310func (m *moduleContext) InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath {
Colin Cross25de6c32019-06-06 14:29:25 -07003311 fullInstallPath := installPath.Join(m, name)
David Srbecky07656412020-06-04 01:26:16 +01003312 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08003313
Jiyong Park073ea552020-11-09 14:08:34 +09003314 relPath, err := filepath.Rel(path.Dir(fullInstallPath.String()), srcPath.String())
3315 if err != nil {
3316 panic(fmt.Sprintf("Unable to generate symlink between %q and %q: %s", fullInstallPath.Base(), srcPath.Base(), err))
3317 }
Colin Crossa9c8c9f2020-12-16 10:20:23 -08003318 if !m.skipInstall() {
Colin Crossce75d2c2016-10-06 16:12:58 -07003319
Colin Crossc68db4b2021-11-11 18:59:15 -08003320 if m.Config().KatiEnabled() {
Colin Cross6301c3c2021-09-28 17:40:21 -07003321 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
3322 // makefile instead of directly to the ninja file so that main.mk can add the
3323 // dependencies from the `required` property that are hard to resolve in Soong.
3324 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
3325 from: srcPath,
3326 to: fullInstallPath,
3327 })
3328 } else {
Colin Cross64002af2021-11-09 16:37:52 -08003329 // The symlink doesn't need updating when the target is modified, but we sometimes
3330 // have a dependency on a symlink to a binary instead of to the binary directly, and
3331 // the mtime of the symlink must be updated when the binary is modified, so use a
3332 // normal dependency here instead of an order-only dependency.
Colin Cross6301c3c2021-09-28 17:40:21 -07003333 m.Build(pctx, BuildParams{
3334 Rule: Symlink,
3335 Description: "install symlink " + fullInstallPath.Base(),
3336 Output: fullInstallPath,
3337 Input: srcPath,
3338 Default: !m.Config().KatiEnabled(),
3339 Args: map[string]string{
3340 "fromPath": relPath,
3341 },
3342 })
3343 }
Colin Cross3854a602016-01-11 12:49:11 -08003344
Colin Cross25de6c32019-06-06 14:29:25 -07003345 m.installFiles = append(m.installFiles, fullInstallPath)
3346 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
Colin Cross12fc4972016-01-11 12:49:11 -08003347 }
Jiyong Park073ea552020-11-09 14:08:34 +09003348
3349 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
3350 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
3351 srcPath: nil,
3352 symlinkTarget: relPath,
3353 executable: false,
Jooyung Han99c5fe62022-03-21 15:13:38 +09003354 partition: fullInstallPath.partition,
Jiyong Park073ea552020-11-09 14:08:34 +09003355 })
3356
Colin Cross3854a602016-01-11 12:49:11 -08003357 return fullInstallPath
3358}
3359
Jiyong Parkf1194352019-02-25 11:05:47 +09003360// installPath/name -> absPath where absPath might be a path that is available only at runtime
3361// (e.g. /apex/...)
Colin Cross70dda7e2019-10-01 22:05:35 -07003362func (m *moduleContext) InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath {
Colin Cross25de6c32019-06-06 14:29:25 -07003363 fullInstallPath := installPath.Join(m, name)
David Srbecky07656412020-06-04 01:26:16 +01003364 m.module.base().hooks.runInstallHooks(m, nil, fullInstallPath, true)
Jiyong Parkf1194352019-02-25 11:05:47 +09003365
Colin Crossa9c8c9f2020-12-16 10:20:23 -08003366 if !m.skipInstall() {
Colin Crossc68db4b2021-11-11 18:59:15 -08003367 if m.Config().KatiEnabled() {
Colin Cross6301c3c2021-09-28 17:40:21 -07003368 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
3369 // makefile instead of directly to the ninja file so that main.mk can add the
3370 // dependencies from the `required` property that are hard to resolve in Soong.
3371 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
3372 absFrom: absPath,
3373 to: fullInstallPath,
3374 })
3375 } else {
3376 m.Build(pctx, BuildParams{
3377 Rule: Symlink,
3378 Description: "install symlink " + fullInstallPath.Base() + " -> " + absPath,
3379 Output: fullInstallPath,
3380 Default: !m.Config().KatiEnabled(),
3381 Args: map[string]string{
3382 "fromPath": absPath,
3383 },
3384 })
3385 }
Jiyong Parkf1194352019-02-25 11:05:47 +09003386
Colin Cross25de6c32019-06-06 14:29:25 -07003387 m.installFiles = append(m.installFiles, fullInstallPath)
Jiyong Parkf1194352019-02-25 11:05:47 +09003388 }
Jiyong Park073ea552020-11-09 14:08:34 +09003389
3390 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
3391 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
3392 srcPath: nil,
3393 symlinkTarget: absPath,
3394 executable: false,
Jooyung Han99c5fe62022-03-21 15:13:38 +09003395 partition: fullInstallPath.partition,
Jiyong Park073ea552020-11-09 14:08:34 +09003396 })
3397
Jiyong Parkf1194352019-02-25 11:05:47 +09003398 return fullInstallPath
3399}
3400
Colin Cross25de6c32019-06-06 14:29:25 -07003401func (m *moduleContext) CheckbuildFile(srcPath Path) {
3402 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
Colin Cross3f40fa42015-01-30 17:27:36 -08003403}
3404
Colin Crossc20dc852020-11-10 12:27:45 -08003405func (m *moduleContext) blueprintModuleContext() blueprint.ModuleContext {
3406 return m.bp
3407}
3408
Colin Crosse7fe0962022-03-15 17:49:24 -07003409func (m *moduleContext) LicenseMetadataFile() Path {
3410 return m.module.base().licenseMetadataFile
3411}
3412
Paul Duffine6ba0722021-07-12 20:12:12 +01003413// SrcIsModule decodes module references in the format ":unqualified-name" or "//namespace:name"
3414// into the module name, or empty string if the input was not a module reference.
Colin Cross41955e82019-05-29 14:40:35 -07003415func SrcIsModule(s string) (module string) {
Paul Duffine6ba0722021-07-12 20:12:12 +01003416 if len(s) > 1 {
3417 if s[0] == ':' {
3418 module = s[1:]
3419 if !isUnqualifiedModuleName(module) {
3420 // The module name should be unqualified but is not so do not treat it as a module.
3421 module = ""
3422 }
3423 } else if s[0] == '/' && s[1] == '/' {
3424 module = s
3425 }
Colin Cross068e0fe2016-12-13 15:23:47 -08003426 }
Paul Duffine6ba0722021-07-12 20:12:12 +01003427 return module
Colin Cross068e0fe2016-12-13 15:23:47 -08003428}
3429
Yi-Yo Chiangba9ea322021-07-15 17:18:21 +08003430// SrcIsModuleWithTag decodes module references in the format ":unqualified-name{.tag}" or
3431// "//namespace:name{.tag}" into the module name and tag, ":unqualified-name" or "//namespace:name"
3432// into the module name and an empty string for the tag, or empty strings if the input was not a
3433// module reference.
Colin Cross41955e82019-05-29 14:40:35 -07003434func SrcIsModuleWithTag(s string) (module, tag string) {
Paul Duffine6ba0722021-07-12 20:12:12 +01003435 if len(s) > 1 {
3436 if s[0] == ':' {
3437 module = s[1:]
3438 } else if s[0] == '/' && s[1] == '/' {
3439 module = s
3440 }
3441
3442 if module != "" {
3443 if tagStart := strings.IndexByte(module, '{'); tagStart > 0 {
3444 if module[len(module)-1] == '}' {
3445 tag = module[tagStart+1 : len(module)-1]
3446 module = module[:tagStart]
3447 }
3448 }
3449
3450 if s[0] == ':' && !isUnqualifiedModuleName(module) {
3451 // The module name should be unqualified but is not so do not treat it as a module.
3452 module = ""
3453 tag = ""
Colin Cross41955e82019-05-29 14:40:35 -07003454 }
3455 }
Colin Cross41955e82019-05-29 14:40:35 -07003456 }
Paul Duffine6ba0722021-07-12 20:12:12 +01003457
3458 return module, tag
3459}
3460
3461// isUnqualifiedModuleName makes sure that the supplied module is an unqualified module name, i.e.
3462// does not contain any /.
3463func isUnqualifiedModuleName(module string) bool {
3464 return strings.IndexByte(module, '/') == -1
Colin Cross068e0fe2016-12-13 15:23:47 -08003465}
3466
Paul Duffin40131a32021-07-09 17:10:35 +01003467// sourceOrOutputDependencyTag is the dependency tag added automatically by pathDepsMutator for any
3468// module reference in a property annotated with `android:"path"` or passed to ExtractSourceDeps
3469// or ExtractSourcesDeps.
3470//
3471// If uniquely identifies the dependency that was added as it contains both the module name used to
3472// add the dependency as well as the tag. That makes it very simple to find the matching dependency
3473// in GetModuleFromPathDep as all it needs to do is find the dependency whose tag matches the tag
3474// used to add it. It does not need to check that the module name as returned by one of
3475// Module.Name(), BaseModuleContext.OtherModuleName() or ModuleBase.BaseModuleName() matches the
3476// name supplied in the tag. That means it does not need to handle differences in module names
3477// caused by prebuilt_ prefix, or fully qualified module names.
Colin Cross41955e82019-05-29 14:40:35 -07003478type sourceOrOutputDependencyTag struct {
3479 blueprint.BaseDependencyTag
Paul Duffin40131a32021-07-09 17:10:35 +01003480
3481 // The name of the module.
3482 moduleName string
3483
3484 // The tag that will be passed to the module's OutputFileProducer.OutputFiles(tag) method.
Colin Cross41955e82019-05-29 14:40:35 -07003485 tag string
3486}
3487
Paul Duffin40131a32021-07-09 17:10:35 +01003488func sourceOrOutputDepTag(moduleName, tag string) blueprint.DependencyTag {
3489 return sourceOrOutputDependencyTag{moduleName: moduleName, tag: tag}
Colin Cross41955e82019-05-29 14:40:35 -07003490}
3491
Paul Duffind5cf92e2021-07-09 17:38:55 +01003492// IsSourceDepTagWithOutputTag returns true if the supplied blueprint.DependencyTag is one that was
3493// used to add dependencies by either ExtractSourceDeps, ExtractSourcesDeps or automatically for
3494// properties tagged with `android:"path"` AND it was added using a module reference of
3495// :moduleName{outputTag}.
3496func IsSourceDepTagWithOutputTag(depTag blueprint.DependencyTag, outputTag string) bool {
3497 t, ok := depTag.(sourceOrOutputDependencyTag)
3498 return ok && t.tag == outputTag
3499}
3500
Colin Cross366938f2017-12-11 16:29:02 -08003501// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
3502// using ":module" syntax, if any.
Colin Cross27b922f2019-03-04 22:35:41 -08003503//
3504// Deprecated: tag the property with `android:"path"` instead.
Colin Cross068e0fe2016-12-13 15:23:47 -08003505func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
Nan Zhang2439eb72017-04-10 11:27:50 -07003506 set := make(map[string]bool)
3507
Colin Cross068e0fe2016-12-13 15:23:47 -08003508 for _, s := range srcFiles {
Colin Cross41955e82019-05-29 14:40:35 -07003509 if m, t := SrcIsModuleWithTag(s); m != "" {
3510 if _, found := set[s]; found {
3511 ctx.ModuleErrorf("found source dependency duplicate: %q!", s)
Nan Zhang2439eb72017-04-10 11:27:50 -07003512 } else {
Colin Cross41955e82019-05-29 14:40:35 -07003513 set[s] = true
Paul Duffin40131a32021-07-09 17:10:35 +01003514 ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(m, t), m)
Nan Zhang2439eb72017-04-10 11:27:50 -07003515 }
Colin Cross068e0fe2016-12-13 15:23:47 -08003516 }
3517 }
Colin Cross068e0fe2016-12-13 15:23:47 -08003518}
3519
Colin Cross366938f2017-12-11 16:29:02 -08003520// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
3521// using ":module" syntax, if any.
Colin Cross27b922f2019-03-04 22:35:41 -08003522//
3523// Deprecated: tag the property with `android:"path"` instead.
Colin Cross366938f2017-12-11 16:29:02 -08003524func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
3525 if s != nil {
Colin Cross41955e82019-05-29 14:40:35 -07003526 if m, t := SrcIsModuleWithTag(*s); m != "" {
Paul Duffin40131a32021-07-09 17:10:35 +01003527 ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(m, t), m)
Colin Cross366938f2017-12-11 16:29:02 -08003528 }
3529 }
3530}
3531
Colin Cross41955e82019-05-29 14:40:35 -07003532// A module that implements SourceFileProducer can be referenced from any property that is tagged with `android:"path"`
3533// using the ":module" syntax and provides a list of paths to be used as if they were listed in the property.
Colin Cross068e0fe2016-12-13 15:23:47 -08003534type SourceFileProducer interface {
3535 Srcs() Paths
3536}
3537
Colin Cross41955e82019-05-29 14:40:35 -07003538// A module that implements OutputFileProducer can be referenced from any property that is tagged with `android:"path"`
Roland Levillain97c1f342019-11-22 14:20:54 +00003539// using the ":module" syntax or ":module{.tag}" syntax and provides a list of output files to be used as if they were
Colin Cross41955e82019-05-29 14:40:35 -07003540// listed in the property.
3541type OutputFileProducer interface {
3542 OutputFiles(tag string) (Paths, error)
3543}
3544
Colin Cross5e708052019-08-06 13:59:50 -07003545// OutputFilesForModule returns the paths from an OutputFileProducer with the given tag. On error, including if the
3546// module produced zero paths, it reports errors to the ctx and returns nil.
3547func OutputFilesForModule(ctx PathContext, module blueprint.Module, tag string) Paths {
3548 paths, err := outputFilesForModule(ctx, module, tag)
3549 if err != nil {
3550 reportPathError(ctx, err)
3551 return nil
3552 }
3553 return paths
3554}
3555
3556// OutputFileForModule returns the path from an OutputFileProducer with the given tag. On error, including if the
3557// module produced zero or multiple paths, it reports errors to the ctx and returns nil.
3558func OutputFileForModule(ctx PathContext, module blueprint.Module, tag string) Path {
3559 paths, err := outputFilesForModule(ctx, module, tag)
3560 if err != nil {
3561 reportPathError(ctx, err)
3562 return nil
3563 }
Colin Cross14ec66c2022-10-03 21:02:27 -07003564 if len(paths) == 0 {
3565 type addMissingDependenciesIntf interface {
3566 AddMissingDependencies([]string)
3567 OtherModuleName(blueprint.Module) string
3568 }
3569 if mctx, ok := ctx.(addMissingDependenciesIntf); ok && ctx.Config().AllowMissingDependencies() {
3570 mctx.AddMissingDependencies([]string{mctx.OtherModuleName(module)})
3571 } else {
3572 ReportPathErrorf(ctx, "failed to get output files from module %q", pathContextName(ctx, module))
3573 }
3574 // Return a fake output file to avoid nil dereferences of Path objects later.
3575 // This should never get used for an actual build as the error or missing
3576 // dependency has already been reported.
3577 p, err := pathForSource(ctx, filepath.Join("missing_output_file", pathContextName(ctx, module)))
3578 if err != nil {
3579 reportPathError(ctx, err)
3580 return nil
3581 }
3582 return p
3583 }
Colin Cross5e708052019-08-06 13:59:50 -07003584 if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01003585 ReportPathErrorf(ctx, "got multiple output files from module %q, expected exactly one",
Colin Cross5e708052019-08-06 13:59:50 -07003586 pathContextName(ctx, module))
Colin Cross5e708052019-08-06 13:59:50 -07003587 }
3588 return paths[0]
3589}
3590
3591func outputFilesForModule(ctx PathContext, module blueprint.Module, tag string) (Paths, error) {
3592 if outputFileProducer, ok := module.(OutputFileProducer); ok {
3593 paths, err := outputFileProducer.OutputFiles(tag)
3594 if err != nil {
3595 return nil, fmt.Errorf("failed to get output file from module %q: %s",
3596 pathContextName(ctx, module), err.Error())
3597 }
Colin Cross5e708052019-08-06 13:59:50 -07003598 return paths, nil
Colin Cross74b1e2b2020-11-22 20:23:02 -08003599 } else if sourceFileProducer, ok := module.(SourceFileProducer); ok {
3600 if tag != "" {
3601 return nil, fmt.Errorf("module %q is a SourceFileProducer, not an OutputFileProducer, and so does not support tag %q", pathContextName(ctx, module), tag)
3602 }
3603 paths := sourceFileProducer.Srcs()
Colin Cross74b1e2b2020-11-22 20:23:02 -08003604 return paths, nil
Colin Cross5e708052019-08-06 13:59:50 -07003605 } else {
3606 return nil, fmt.Errorf("module %q is not an OutputFileProducer", pathContextName(ctx, module))
3607 }
3608}
3609
Colin Cross41589502020-12-01 14:00:21 -08003610// Modules can implement HostToolProvider and return a valid OptionalPath from HostToolPath() to
3611// specify that they can be used as a tool by a genrule module.
Colin Crossfe17f6f2019-03-28 19:30:56 -07003612type HostToolProvider interface {
Colin Crossba9e4032020-11-24 16:32:22 -08003613 Module
Colin Cross41589502020-12-01 14:00:21 -08003614 // HostToolPath returns the path to the host tool for the module if it is one, or an invalid
3615 // OptionalPath.
Colin Crossfe17f6f2019-03-28 19:30:56 -07003616 HostToolPath() OptionalPath
3617}
3618
Colin Cross27b922f2019-03-04 22:35:41 -08003619// Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
3620// be tagged with `android:"path" to support automatic source module dependency resolution.
Colin Cross8a497952019-03-05 22:25:09 -08003621//
3622// Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
Colin Cross25de6c32019-06-06 14:29:25 -07003623func (m *moduleContext) ExpandSources(srcFiles, excludes []string) Paths {
3624 return PathsForModuleSrcExcludes(m, srcFiles, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07003625}
3626
Colin Cross2fafa3e2019-03-05 12:39:51 -08003627// Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
3628// be tagged with `android:"path" to support automatic source module dependency resolution.
Colin Cross8a497952019-03-05 22:25:09 -08003629//
3630// Deprecated: use PathForModuleSrc instead.
Sasha Smundake198eaf2022-08-04 13:07:02 -07003631func (m *moduleContext) ExpandSource(srcFile, _ string) Path {
Colin Cross25de6c32019-06-06 14:29:25 -07003632 return PathForModuleSrc(m, srcFile)
Colin Cross2fafa3e2019-03-05 12:39:51 -08003633}
3634
3635// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
3636// the srcFile is non-nil. The property must be tagged with `android:"path" to support automatic source module
3637// dependency resolution.
Sasha Smundake198eaf2022-08-04 13:07:02 -07003638func (m *moduleContext) ExpandOptionalSource(srcFile *string, _ string) OptionalPath {
Colin Cross2fafa3e2019-03-05 12:39:51 -08003639 if srcFile != nil {
Colin Cross25de6c32019-06-06 14:29:25 -07003640 return OptionalPathForPath(PathForModuleSrc(m, *srcFile))
Colin Cross2fafa3e2019-03-05 12:39:51 -08003641 }
3642 return OptionalPath{}
3643}
3644
Colin Cross25de6c32019-06-06 14:29:25 -07003645func (m *moduleContext) RequiredModuleNames() []string {
Jiyong Park6a8cf5f2019-12-30 16:31:09 +09003646 return m.module.RequiredModuleNames()
Nan Zhang6d34b302017-02-04 17:47:46 -08003647}
3648
Colin Cross25de6c32019-06-06 14:29:25 -07003649func (m *moduleContext) HostRequiredModuleNames() []string {
Jiyong Park6a8cf5f2019-12-30 16:31:09 +09003650 return m.module.HostRequiredModuleNames()
Sasha Smundakb6d23052019-04-01 18:37:36 -07003651}
3652
Colin Cross25de6c32019-06-06 14:29:25 -07003653func (m *moduleContext) TargetRequiredModuleNames() []string {
Jiyong Park6a8cf5f2019-12-30 16:31:09 +09003654 return m.module.TargetRequiredModuleNames()
Sasha Smundakb6d23052019-04-01 18:37:36 -07003655}
3656
Colin Cross463a90e2015-06-17 14:20:06 -07003657func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07003658 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07003659}
3660
Colin Cross0875c522017-11-28 17:34:01 -08003661func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07003662 return &buildTargetSingleton{}
3663}
3664
Colin Cross87d8b562017-04-25 10:01:55 -07003665func parentDir(dir string) string {
3666 dir, _ = filepath.Split(dir)
3667 return filepath.Clean(dir)
3668}
3669
Colin Cross1f8c52b2015-06-16 16:38:17 -07003670type buildTargetSingleton struct{}
3671
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07003672func AddAncestors(ctx SingletonContext, dirMap map[string]Paths, mmName func(string) string) ([]string, []string) {
Chih-Hung Hsiehd0f82fe2021-09-05 20:15:38 -07003673 // Ensure ancestor directories are in dirMap
3674 // Make directories build their direct subdirectories
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07003675 // Returns a slice of all directories and a slice of top-level directories.
Chih-Hung Hsiehd0f82fe2021-09-05 20:15:38 -07003676 dirs := SortedStringKeys(dirMap)
3677 for _, dir := range dirs {
3678 dir := parentDir(dir)
3679 for dir != "." && dir != "/" {
3680 if _, exists := dirMap[dir]; exists {
3681 break
3682 }
3683 dirMap[dir] = nil
3684 dir = parentDir(dir)
3685 }
3686 }
3687 dirs = SortedStringKeys(dirMap)
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07003688 var topDirs []string
Chih-Hung Hsiehd0f82fe2021-09-05 20:15:38 -07003689 for _, dir := range dirs {
3690 p := parentDir(dir)
3691 if p != "." && p != "/" {
3692 dirMap[p] = append(dirMap[p], PathForPhony(ctx, mmName(dir)))
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07003693 } else if dir != "." && dir != "/" && dir != "" {
3694 topDirs = append(topDirs, dir)
Chih-Hung Hsiehd0f82fe2021-09-05 20:15:38 -07003695 }
3696 }
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07003697 return SortedStringKeys(dirMap), topDirs
Chih-Hung Hsiehd0f82fe2021-09-05 20:15:38 -07003698}
3699
Colin Cross0875c522017-11-28 17:34:01 -08003700func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
3701 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07003702
Colin Crossc3d87d32020-06-04 13:25:17 -07003703 mmTarget := func(dir string) string {
3704 return "MODULES-IN-" + strings.Replace(filepath.Clean(dir), "/", "-", -1)
Colin Cross87d8b562017-04-25 10:01:55 -07003705 }
3706
Colin Cross0875c522017-11-28 17:34:01 -08003707 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07003708
Colin Cross0875c522017-11-28 17:34:01 -08003709 ctx.VisitAllModules(func(module Module) {
3710 blueprintDir := module.base().blueprintDir
3711 installTarget := module.base().installTarget
3712 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07003713
Colin Cross0875c522017-11-28 17:34:01 -08003714 if checkbuildTarget != nil {
3715 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
3716 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
3717 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07003718
Colin Cross0875c522017-11-28 17:34:01 -08003719 if installTarget != nil {
3720 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07003721 }
3722 })
3723
Dan Willemsen5ba07e82015-12-11 13:51:06 -08003724 suffix := ""
Jingwen Chencda22c92020-11-23 00:22:30 -05003725 if ctx.Config().KatiEnabled() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08003726 suffix = "-soong"
3727 }
3728
Colin Cross1f8c52b2015-06-16 16:38:17 -07003729 // Create a top-level checkbuild target that depends on all modules
Colin Crossc3d87d32020-06-04 13:25:17 -07003730 ctx.Phony("checkbuild"+suffix, checkbuildDeps...)
Colin Cross1f8c52b2015-06-16 16:38:17 -07003731
Dan Willemsend2e95fb2017-09-20 14:30:50 -07003732 // Make will generate the MODULES-IN-* targets
Jingwen Chencda22c92020-11-23 00:22:30 -05003733 if ctx.Config().KatiEnabled() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07003734 return
3735 }
3736
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07003737 dirs, _ := AddAncestors(ctx, modulesInDir, mmTarget)
Colin Cross87d8b562017-04-25 10:01:55 -07003738
Dan Willemsend2e95fb2017-09-20 14:30:50 -07003739 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
3740 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
3741 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07003742 for _, dir := range dirs {
Colin Crossc3d87d32020-06-04 13:25:17 -07003743 ctx.Phony(mmTarget(dir), modulesInDir[dir]...)
Colin Cross1f8c52b2015-06-16 16:38:17 -07003744 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07003745
3746 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
Jiyong Park1613e552020-09-14 19:43:17 +09003747 type osAndCross struct {
3748 os OsType
3749 hostCross bool
3750 }
3751 osDeps := map[osAndCross]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08003752 ctx.VisitAllModules(func(module Module) {
3753 if module.Enabled() {
Jiyong Park1613e552020-09-14 19:43:17 +09003754 key := osAndCross{os: module.Target().Os, hostCross: module.Target().HostCross}
3755 osDeps[key] = append(osDeps[key], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07003756 }
3757 })
3758
Colin Cross0875c522017-11-28 17:34:01 -08003759 osClass := make(map[string]Paths)
Jiyong Park1613e552020-09-14 19:43:17 +09003760 for key, deps := range osDeps {
Dan Willemsen61d88b82017-09-20 17:29:08 -07003761 var className string
3762
Jiyong Park1613e552020-09-14 19:43:17 +09003763 switch key.os.Class {
Dan Willemsen61d88b82017-09-20 17:29:08 -07003764 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +09003765 if key.hostCross {
3766 className = "host-cross"
3767 } else {
3768 className = "host"
3769 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07003770 case Device:
3771 className = "target"
3772 default:
3773 continue
3774 }
3775
Jiyong Park1613e552020-09-14 19:43:17 +09003776 name := className + "-" + key.os.Name
Colin Crossc3d87d32020-06-04 13:25:17 -07003777 osClass[className] = append(osClass[className], PathForPhony(ctx, name))
Dan Willemsen61d88b82017-09-20 17:29:08 -07003778
Colin Crossc3d87d32020-06-04 13:25:17 -07003779 ctx.Phony(name, deps...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07003780 }
3781
3782 // Wrap those into host|host-cross|target phony rules
Inseob Kim1a365c62019-06-08 15:47:51 +09003783 for _, class := range SortedStringKeys(osClass) {
Colin Crossc3d87d32020-06-04 13:25:17 -07003784 ctx.Phony(class, osClass[class]...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07003785 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07003786}
Colin Crossd779da42015-12-17 18:00:23 -08003787
Brandon Lee5d45c6f2018-08-15 15:35:38 -07003788// Collect information for opening IDE project files in java/jdeps.go.
3789type IDEInfo interface {
3790 IDEInfo(ideInfo *IdeInfo)
3791 BaseModuleName() string
3792}
3793
3794// Extract the base module name from the Import name.
3795// Often the Import name has a prefix "prebuilt_".
3796// Remove the prefix explicitly if needed
3797// until we find a better solution to get the Import name.
3798type IDECustomizedModuleName interface {
3799 IDECustomizedModuleName() string
3800}
3801
3802type IdeInfo struct {
3803 Deps []string `json:"dependencies,omitempty"`
3804 Srcs []string `json:"srcs,omitempty"`
3805 Aidl_include_dirs []string `json:"aidl_include_dirs,omitempty"`
3806 Jarjar_rules []string `json:"jarjar_rules,omitempty"`
3807 Jars []string `json:"jars,omitempty"`
3808 Classes []string `json:"class,omitempty"`
3809 Installed_paths []string `json:"installed,omitempty"`
patricktu18c82ff2019-05-10 15:48:50 +08003810 SrcJars []string `json:"srcjars,omitempty"`
bralee1fbf4402020-05-21 10:11:59 +08003811 Paths []string `json:"path,omitempty"`
Yikef6282022022-04-13 20:41:01 +08003812 Static_libs []string `json:"static_libs,omitempty"`
3813 Libs []string `json:"libs,omitempty"`
Brandon Lee5d45c6f2018-08-15 15:35:38 -07003814}
Paul Duffinf88d8e02020-05-07 20:21:34 +01003815
3816func CheckBlueprintSyntax(ctx BaseModuleContext, filename string, contents string) []error {
3817 bpctx := ctx.blueprintBaseModuleContext()
3818 return blueprint.CheckBlueprintSyntax(bpctx.ModuleFactories(), filename, contents)
3819}
Colin Cross5d583952020-11-24 16:21:24 -08003820
3821// installPathsDepSet is a thin type-safe wrapper around the generic depSet. It always uses
3822// topological order.
3823type installPathsDepSet struct {
3824 depSet
3825}
3826
3827// newInstallPathsDepSet returns an immutable packagingSpecsDepSet with the given direct and
3828// transitive contents.
3829func newInstallPathsDepSet(direct InstallPaths, transitive []*installPathsDepSet) *installPathsDepSet {
3830 return &installPathsDepSet{*newDepSet(TOPOLOGICAL, direct, transitive)}
3831}
3832
3833// ToList returns the installPathsDepSet flattened to a list in topological order.
3834func (d *installPathsDepSet) ToList() InstallPaths {
3835 if d == nil {
3836 return nil
3837 }
3838 return d.depSet.ToList().(InstallPaths)
3839}