blob: 9f923e2d0f4553361163a314ed40699470750941 [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 (
Jingwen Chen73850672020-12-14 08:25:34 -050018 "android/soong/bazel"
Colin Cross6ff51382015-12-17 16:39:19 -080019 "fmt"
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"
Jiyong Park1c7e9622020-05-07 16:12:13 +090023 "regexp"
Colin Cross6ff51382015-12-17 16:39:19 -080024 "strings"
Colin Crossaabf6792017-11-29 00:27:14 -080025 "text/scanner"
Colin Crossf6566ed2015-03-24 11:13:38 -070026
27 "github.com/google/blueprint"
Colin Crossfe4bc362018-09-12 10:02:13 -070028 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080029)
30
31var (
32 DeviceSharedLibrary = "shared_library"
33 DeviceStaticLibrary = "static_library"
34 DeviceExecutable = "executable"
35 HostSharedLibrary = "host_shared_library"
36 HostStaticLibrary = "host_static_library"
37 HostExecutable = "host_executable"
38)
39
Colin Crossae887032017-10-23 17:16:14 -070040type BuildParams struct {
Dan Willemsen9f3c5742016-11-03 14:28:31 -070041 Rule blueprint.Rule
Colin Cross33bfb0a2016-11-21 17:23:08 -080042 Deps blueprint.Deps
43 Depfile WritablePath
Colin Cross67a5c132017-05-09 13:45:28 -070044 Description string
Dan Willemsen9f3c5742016-11-03 14:28:31 -070045 Output WritablePath
46 Outputs WritablePaths
Jingwen Chence679d22020-09-23 04:30:02 +000047 SymlinkOutput WritablePath
48 SymlinkOutputs WritablePaths
Dan Willemsen9f3c5742016-11-03 14:28:31 -070049 ImplicitOutput WritablePath
50 ImplicitOutputs WritablePaths
51 Input Path
52 Inputs Paths
53 Implicit Path
54 Implicits Paths
55 OrderOnly Paths
Colin Cross824f1162020-07-16 13:07:51 -070056 Validation Path
57 Validations Paths
Dan Willemsen9f3c5742016-11-03 14:28:31 -070058 Default bool
59 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070060}
61
Colin Crossae887032017-10-23 17:16:14 -070062type ModuleBuildParams BuildParams
63
Colin Cross1184b642019-12-30 18:43:07 -080064// EarlyModuleContext provides methods that can be called early, as soon as the properties have
65// been parsed into the module and before any mutators have run.
66type EarlyModuleContext interface {
Colin Cross9f35c3d2020-09-16 19:04:41 -070067 // Module returns the current module as a Module. It should rarely be necessary, as the module already has a
68 // reference to itself.
Colin Cross1184b642019-12-30 18:43:07 -080069 Module() Module
Colin Cross9f35c3d2020-09-16 19:04:41 -070070
71 // ModuleName returns the name of the module. This is generally the value that was returned by Module.Name() when
72 // the module was created, but may have been modified by calls to BaseMutatorContext.Rename.
Colin Cross1184b642019-12-30 18:43:07 -080073 ModuleName() string
Colin Cross9f35c3d2020-09-16 19:04:41 -070074
75 // ModuleDir returns the path to the directory that contains the definition of the module.
Colin Cross1184b642019-12-30 18:43:07 -080076 ModuleDir() string
Colin Cross9f35c3d2020-09-16 19:04:41 -070077
78 // ModuleType returns the name of the module type that was used to create the module, as specified in
79 // RegisterModuleType.
Colin Cross1184b642019-12-30 18:43:07 -080080 ModuleType() string
Colin Cross9f35c3d2020-09-16 19:04:41 -070081
82 // BlueprintFile returns the name of the blueprint file that contains the definition of this
83 // module.
Colin Cross9d34f352019-11-22 16:03:51 -080084 BlueprintsFile() string
Colin Cross1184b642019-12-30 18:43:07 -080085
Colin Cross9f35c3d2020-09-16 19:04:41 -070086 // ContainsProperty returns true if the specified property name was set in the module definition.
Colin Cross1184b642019-12-30 18:43:07 -080087 ContainsProperty(name string) bool
Colin Cross9f35c3d2020-09-16 19:04:41 -070088
89 // Errorf reports an error at the specified position of the module definition file.
Colin Cross1184b642019-12-30 18:43:07 -080090 Errorf(pos scanner.Position, fmt string, args ...interface{})
Colin Cross9f35c3d2020-09-16 19:04:41 -070091
92 // ModuleErrorf reports an error at the line number of the module type in the module definition.
Colin Cross1184b642019-12-30 18:43:07 -080093 ModuleErrorf(fmt string, args ...interface{})
Colin Cross9f35c3d2020-09-16 19:04:41 -070094
95 // PropertyErrorf reports an error at the line number of a property in the module definition.
Colin Cross1184b642019-12-30 18:43:07 -080096 PropertyErrorf(property, fmt string, args ...interface{})
Colin Cross9f35c3d2020-09-16 19:04:41 -070097
98 // Failed returns true if any errors have been reported. In most cases the module can continue with generating
99 // build rules after an error, allowing it to report additional errors in a single run, but in cases where the error
100 // has prevented the module from creating necessary data it can return early when Failed returns true.
Colin Cross1184b642019-12-30 18:43:07 -0800101 Failed() bool
102
Colin Cross9f35c3d2020-09-16 19:04:41 -0700103 // AddNinjaFileDeps adds dependencies on the specified files to the rule that creates the ninja manifest. The
104 // primary builder will be rerun whenever the specified files are modified.
Colin Cross1184b642019-12-30 18:43:07 -0800105 AddNinjaFileDeps(deps ...string)
106
107 DeviceSpecific() bool
108 SocSpecific() bool
109 ProductSpecific() bool
110 SystemExtSpecific() bool
111 Platform() bool
112
113 Config() Config
114 DeviceConfig() DeviceConfig
115
116 // Deprecated: use Config()
117 AConfig() Config
118
119 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
120 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
121 // builder whenever a file matching the pattern as added or removed, without rerunning if a
122 // file that does not match the pattern is added to a searched directory.
123 GlobWithDeps(pattern string, excludes []string) ([]string, error)
124
125 Glob(globPattern string, excludes []string) Paths
126 GlobFiles(globPattern string, excludes []string) Paths
Colin Cross988414c2020-01-11 01:11:46 +0000127 IsSymlink(path Path) bool
128 Readlink(path Path) string
Colin Cross133ebef2020-08-14 17:38:45 -0700129
Colin Cross9f35c3d2020-09-16 19:04:41 -0700130 // Namespace returns the Namespace object provided by the NameInterface set by Context.SetNameInterface, or the
131 // default SimpleNameInterface if Context.SetNameInterface was not called.
Colin Cross133ebef2020-08-14 17:38:45 -0700132 Namespace() *Namespace
Colin Cross1184b642019-12-30 18:43:07 -0800133}
134
Colin Cross0ea8ba82019-06-06 14:33:29 -0700135// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
Colin Crossdc35e212019-06-06 16:13:11 -0700136// a Config instead of an interface{}, and some methods have been wrapped to use an android.Module
137// instead of a blueprint.Module, plus some extra methods that return Android-specific information
Colin Cross0ea8ba82019-06-06 14:33:29 -0700138// about the current module.
139type BaseModuleContext interface {
Colin Cross1184b642019-12-30 18:43:07 -0800140 EarlyModuleContext
Colin Cross0ea8ba82019-06-06 14:33:29 -0700141
Paul Duffinf88d8e02020-05-07 20:21:34 +0100142 blueprintBaseModuleContext() blueprint.BaseModuleContext
143
Colin Cross9f35c3d2020-09-16 19:04:41 -0700144 // OtherModuleName returns the name of another Module. See BaseModuleContext.ModuleName for more information.
145 // It is intended for use inside the visit functions of Visit* and WalkDeps.
Colin Crossdc35e212019-06-06 16:13:11 -0700146 OtherModuleName(m blueprint.Module) string
Colin Cross9f35c3d2020-09-16 19:04:41 -0700147
148 // OtherModuleDir returns the directory of another Module. See BaseModuleContext.ModuleDir for more information.
149 // It is intended for use inside the visit functions of Visit* and WalkDeps.
Colin Crossdc35e212019-06-06 16:13:11 -0700150 OtherModuleDir(m blueprint.Module) string
Colin Cross9f35c3d2020-09-16 19:04:41 -0700151
152 // OtherModuleErrorf reports an error on another Module. See BaseModuleContext.ModuleErrorf for more information.
153 // It is intended for use inside the visit functions of Visit* and WalkDeps.
Colin Crossdc35e212019-06-06 16:13:11 -0700154 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
Colin Cross9f35c3d2020-09-16 19:04:41 -0700155
156 // OtherModuleDependencyTag returns the dependency tag used to depend on a module, or nil if there is no dependency
157 // on the module. When called inside a Visit* method with current module being visited, and there are multiple
158 // dependencies on the module being visited, it returns the dependency tag used for the current dependency.
Colin Crossdc35e212019-06-06 16:13:11 -0700159 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
Colin Cross9f35c3d2020-09-16 19:04:41 -0700160
161 // OtherModuleExists returns true if a module with the specified name exists, as determined by the NameInterface
162 // passed to Context.SetNameInterface, or SimpleNameInterface if it was not called.
Colin Crossdc35e212019-06-06 16:13:11 -0700163 OtherModuleExists(name string) bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700164
165 // OtherModuleDependencyVariantExists returns true if a module with the
166 // specified name and variant exists. The variant must match the given
167 // variations. It must also match all the non-local variations of the current
168 // module. In other words, it checks for the module AddVariationDependencies
169 // would add a dependency on with the same arguments.
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000170 OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700171
172 // OtherModuleReverseDependencyVariantExists returns true if a module with the
173 // specified name exists with the same variations as the current module. In
174 // other words, it checks for the module AddReverseDependency would add a
175 // dependency on with the same argument.
Martin Stjernholm009a9dc2020-03-05 17:34:13 +0000176 OtherModuleReverseDependencyVariantExists(name string) bool
Colin Cross9f35c3d2020-09-16 19:04:41 -0700177
178 // OtherModuleType returns the type of another Module. See BaseModuleContext.ModuleType for more information.
179 // It is intended for use inside the visit functions of Visit* and WalkDeps.
Jiyong Park9e6c2422019-08-09 20:39:45 +0900180 OtherModuleType(m blueprint.Module) string
Colin Crossdc35e212019-06-06 16:13:11 -0700181
Colin Crossd27e7b82020-07-02 11:38:17 -0700182 // OtherModuleProvider returns the value for a provider for the given module. If the value is
183 // not set it returns the zero value of the type of the provider, so the return value can always
184 // be type asserted to the type of the provider. The value returned may be a deep copy of the
185 // value originally passed to SetProvider.
186 OtherModuleProvider(m blueprint.Module, provider blueprint.ProviderKey) interface{}
187
188 // OtherModuleHasProvider returns true if the provider for the given module has been set.
189 OtherModuleHasProvider(m blueprint.Module, provider blueprint.ProviderKey) bool
190
191 // Provider returns the value for a provider for the current module. If the value is
192 // not set it returns the zero value of the type of the provider, so the return value can always
193 // be type asserted to the type of the provider. It panics if called before the appropriate
194 // mutator or GenerateBuildActions pass for the provider. The value returned may be a deep
195 // copy of the value originally passed to SetProvider.
196 Provider(provider blueprint.ProviderKey) interface{}
197
198 // HasProvider returns true if the provider for the current module has been set.
199 HasProvider(provider blueprint.ProviderKey) bool
200
201 // SetProvider sets the value for a provider for the current module. It panics if not called
202 // during the appropriate mutator or GenerateBuildActions pass for the provider, if the value
203 // is not of the appropriate type, or if the value has already been set. The value should not
204 // be modified after being passed to SetProvider.
205 SetProvider(provider blueprint.ProviderKey, value interface{})
206
Colin Crossdc35e212019-06-06 16:13:11 -0700207 GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700208
209 // GetDirectDepWithTag returns the Module the direct dependency with the specified name, or nil if
210 // none exists. It panics if the dependency does not have the specified tag. It skips any
211 // dependencies that are not an android.Module.
Colin Crossdc35e212019-06-06 16:13:11 -0700212 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
Colin Cross9f35c3d2020-09-16 19:04:41 -0700213
214 // GetDirectDep returns the Module and DependencyTag for the direct dependency with the specified
215 // name, or nil if none exists. If there are multiple dependencies on the same module it returns
216 // the first DependencyTag. It skips any dependencies that are not an android.Module.
Colin Crossdc35e212019-06-06 16:13:11 -0700217 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
218
Colin Cross9f35c3d2020-09-16 19:04:41 -0700219 // VisitDirectDepsBlueprint calls visit for each direct dependency. If there are multiple
220 // direct dependencies on the same module visit will be called multiple times on that module
221 // and OtherModuleDependencyTag will return a different tag for each.
222 //
223 // The Module passed to the visit function should not be retained outside of the visit
224 // function, it may be invalidated by future mutators.
Colin Crossdc35e212019-06-06 16:13:11 -0700225 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Cross9f35c3d2020-09-16 19:04:41 -0700226
227 // VisitDirectDeps 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. It skips any
230 // dependencies that are not an android.Module.
231 //
232 // The Module passed to the visit function should not be retained outside of the visit
233 // function, it may be invalidated by future mutators.
Colin Crossdc35e212019-06-06 16:13:11 -0700234 VisitDirectDeps(visit func(Module))
Colin Cross9f35c3d2020-09-16 19:04:41 -0700235
Colin Crossdc35e212019-06-06 16:13:11 -0700236 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Cross9f35c3d2020-09-16 19:04:41 -0700237
238 // VisitDirectDepsIf calls pred for each direct dependency, and if pred returns true calls visit. If there are
239 // multiple direct dependencies on the same module pred and visit will be called multiple times on that module and
240 // OtherModuleDependencyTag will return a different tag for each. It skips any
241 // dependencies that are not an android.Module.
242 //
243 // The Module passed to the visit function should not be retained outside of the visit function, it may be
244 // invalidated by future mutators.
Colin Crossdc35e212019-06-06 16:13:11 -0700245 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
246 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
247 VisitDepsDepthFirst(visit func(Module))
248 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
249 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
Colin Cross9f35c3d2020-09-16 19:04:41 -0700250
251 // WalkDeps calls visit for each transitive dependency, traversing the dependency tree in top down order. visit may
252 // be called multiple times for the same (child, parent) pair if there are multiple direct dependencies between the
253 // child and parent with different tags. OtherModuleDependencyTag will return the tag for the currently visited
254 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down to child. It skips
255 // any dependencies that are not an android.Module.
256 //
257 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
258 // invalidated by future mutators.
Colin Crossdc35e212019-06-06 16:13:11 -0700259 WalkDeps(visit func(Module, Module) bool)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700260
261 // WalkDepsBlueprint calls visit for each transitive dependency, traversing the dependency
262 // tree in top down order. visit may be called multiple times for the same (child, parent)
263 // pair if there are multiple direct dependencies between the child and parent with different
264 // tags. OtherModuleDependencyTag will return the tag for the currently visited
265 // (child, parent) pair. If visit returns false WalkDeps will not continue recursing down
266 // to child.
267 //
268 // The Modules passed to the visit function should not be retained outside of the visit function, they may be
269 // invalidated by future mutators.
Colin Crossdc35e212019-06-06 16:13:11 -0700270 WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool)
Colin Cross9f35c3d2020-09-16 19:04:41 -0700271
Colin Crossdc35e212019-06-06 16:13:11 -0700272 // GetWalkPath is supposed to be called in visit function passed in WalkDeps()
273 // and returns a top-down dependency path from a start module to current child module.
274 GetWalkPath() []Module
275
Colin Cross4dfacf92020-09-16 19:22:27 -0700276 // PrimaryModule returns the first variant of the current module. Variants of a module are always visited in
277 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from the
278 // Module returned by PrimaryModule without data races. This can be used to perform singleton actions that are
279 // only done once for all variants of a module.
280 PrimaryModule() Module
281
282 // FinalModule returns the last variant of the current module. Variants of a module are always visited in
283 // order by mutators and GenerateBuildActions, so the data created by the current mutator can be read from all
284 // variants using VisitAllModuleVariants if the current module == FinalModule(). This can be used to perform
285 // singleton actions that are only done once for all variants of a module.
286 FinalModule() Module
287
288 // VisitAllModuleVariants calls visit for each variant of the current module. Variants of a module are always
289 // visited in order by mutators and GenerateBuildActions, so the data created by the current mutator can be read
290 // from all variants if the current module == FinalModule(). Otherwise, care must be taken to not access any
291 // data modified by the current mutator.
292 VisitAllModuleVariants(visit func(Module))
293
Paul Duffinc5192442020-03-31 11:31:36 +0100294 // GetTagPath is supposed to be called in visit function passed in WalkDeps()
295 // and returns a top-down dependency tags path from a start module to current child module.
296 // It has one less entry than GetWalkPath() as it contains the dependency tags that
297 // exist between each adjacent pair of modules in the GetWalkPath().
298 // GetTagPath()[i] is the tag between GetWalkPath()[i] and GetWalkPath()[i+1]
299 GetTagPath() []blueprint.DependencyTag
300
Jiyong Park1c7e9622020-05-07 16:12:13 +0900301 // GetPathString is supposed to be called in visit function passed in WalkDeps()
302 // and returns a multi-line string showing the modules and dependency tags
303 // among them along the top-down dependency path from a start module to current child module.
304 // skipFirst when set to true, the output doesn't include the start module,
305 // which is already printed when this function is used along with ModuleErrorf().
306 GetPathString(skipFirst bool) string
307
Colin Crossdc35e212019-06-06 16:13:11 -0700308 AddMissingDependencies(missingDeps []string)
309
Colin Crossa1ad8d12016-06-01 17:09:44 -0700310 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -0700311 TargetPrimary() bool
Paul Duffinca7f0ef2020-02-25 15:50:49 +0000312
313 // The additional arch specific targets (e.g. 32/64 bit) that this module variant is
314 // responsible for creating.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700315 MultiTargets() []Target
Colin Crossf6566ed2015-03-24 11:13:38 -0700316 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -0700317 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -0700318 Host() bool
319 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -0700320 Darwin() bool
Doug Horn21b94272019-01-16 12:06:11 -0800321 Fuchsia() bool
Colin Cross3edeee12017-04-04 12:59:48 -0700322 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -0700323 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -0700324 PrimaryArch() bool
Colin Crossf6566ed2015-03-24 11:13:38 -0700325}
326
Colin Cross1184b642019-12-30 18:43:07 -0800327// Deprecated: use EarlyModuleContext instead
Colin Cross635c3b02016-05-18 15:37:25 -0700328type BaseContext interface {
Colin Cross1184b642019-12-30 18:43:07 -0800329 EarlyModuleContext
Colin Crossaabf6792017-11-29 00:27:14 -0800330}
331
Colin Cross635c3b02016-05-18 15:37:25 -0700332type ModuleContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -0800333 BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -0800334
Colin Crossc20dc852020-11-10 12:27:45 -0800335 blueprintModuleContext() blueprint.ModuleContext
336
Colin Crossae887032017-10-23 17:16:14 -0700337 // Deprecated: use ModuleContext.Build instead.
Colin Cross0875c522017-11-28 17:34:01 -0800338 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -0700339
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700340 ExpandSources(srcFiles, excludes []string) Paths
Colin Cross366938f2017-12-11 16:29:02 -0800341 ExpandSource(srcFile, prop string) Path
Colin Cross2383f3b2018-02-06 14:40:13 -0800342 ExpandOptionalSource(srcFile *string, prop string) OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700343
Colin Cross41589502020-12-01 14:00:21 -0800344 // InstallExecutable creates a rule to copy srcPath to name in the installPath directory,
345 // with the given additional dependencies. The file is marked executable after copying.
346 //
347 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
348 // installed file will be returned by PackagingSpecs() on this module or by
349 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
350 // for which IsInstallDepNeeded returns true.
Colin Cross70dda7e2019-10-01 22:05:35 -0700351 InstallExecutable(installPath InstallPath, name string, srcPath Path, deps ...Path) InstallPath
Colin Cross41589502020-12-01 14:00:21 -0800352
353 // InstallFile creates a rule to copy srcPath to name in the installPath directory,
354 // with the given additional dependencies.
355 //
356 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
357 // installed file will be returned by PackagingSpecs() on this module or by
358 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
359 // for which IsInstallDepNeeded returns true.
Colin Cross70dda7e2019-10-01 22:05:35 -0700360 InstallFile(installPath InstallPath, name string, srcPath Path, deps ...Path) InstallPath
Colin Cross41589502020-12-01 14:00:21 -0800361
362 // InstallSymlink creates a rule to create a symlink from src srcPath to name in the installPath
363 // directory.
364 //
365 // The installed symlink will be returned by FilesToInstall(), and the PackagingSpec for the
366 // installed file will be returned by PackagingSpecs() on this module or by
367 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
368 // for which IsInstallDepNeeded returns true.
Colin Cross70dda7e2019-10-01 22:05:35 -0700369 InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath
Colin Cross41589502020-12-01 14:00:21 -0800370
371 // InstallAbsoluteSymlink creates a rule to create an absolute symlink from src srcPath to name
372 // in the installPath directory.
373 //
374 // The installed symlink will be returned by FilesToInstall(), and the PackagingSpec for the
375 // installed file will be returned by PackagingSpecs() on this module or by
376 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
377 // for which IsInstallDepNeeded returns true.
Colin Cross70dda7e2019-10-01 22:05:35 -0700378 InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath
Colin Cross41589502020-12-01 14:00:21 -0800379
380 // PackageFile creates a PackagingSpec as if InstallFile was called, but without creating
381 // the rule to copy the file. This is useful to define how a module would be packaged
382 // without installing it into the global installation directories.
383 //
384 // The created PackagingSpec for the will be returned by PackagingSpecs() on this module or by
385 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
386 // for which IsInstallDepNeeded returns true.
387 PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec
388
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700389 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800390
Colin Cross8d8f8e22016-08-03 11:57:50 -0700391 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700392 InstallInTestcases() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700393 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800394 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700395 InstallInVendorRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900396 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700397 InstallInRoot() bool
Colin Cross607d8582019-07-29 16:44:46 -0700398 InstallBypassMake() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900399 InstallForceOS() (*OsType, *ArchType)
Nan Zhang6d34b302017-02-04 17:47:46 -0800400
401 RequiredModuleNames() []string
Sasha Smundakb6d23052019-04-01 18:37:36 -0700402 HostRequiredModuleNames() []string
403 TargetRequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -0700404
Colin Cross3f68a132017-10-23 17:10:29 -0700405 ModuleSubDir() string
406
Colin Cross0875c522017-11-28 17:34:01 -0800407 Variable(pctx PackageContext, name, value string)
408 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700409 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
410 // and performs more verification.
Colin Cross0875c522017-11-28 17:34:01 -0800411 Build(pctx PackageContext, params BuildParams)
Colin Crossc3d87d32020-06-04 13:25:17 -0700412 // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
413 // phony rules or real files. Phony can be called on the same name multiple times to add
414 // additional dependencies.
415 Phony(phony string, deps ...Path)
Colin Cross3f68a132017-10-23 17:10:29 -0700416
Colin Cross9f35c3d2020-09-16 19:04:41 -0700417 // GetMissingDependencies returns the list of dependencies that were passed to AddDependencies or related methods,
418 // but do not exist.
Colin Cross3f68a132017-10-23 17:10:29 -0700419 GetMissingDependencies() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800420}
421
Colin Cross635c3b02016-05-18 15:37:25 -0700422type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800423 blueprint.Module
424
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700425 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
426 // but GenerateAndroidBuildActions also has access to Android-specific information.
427 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700428 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700429
Paul Duffin44f1d842020-06-26 20:17:02 +0100430 // Add dependencies to the components of a module, i.e. modules that are created
431 // by the module and which are considered to be part of the creating module.
432 //
433 // This is called before prebuilts are renamed so as to allow a dependency to be
434 // added directly to a prebuilt child module instead of depending on a source module
435 // and relying on prebuilt processing to switch to the prebuilt module if preferred.
436 //
437 // A dependency on a prebuilt must include the "prebuilt_" prefix.
438 ComponentDepsMutator(ctx BottomUpMutatorContext)
439
Colin Cross1e676be2016-10-12 14:38:15 -0700440 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800441
Colin Cross635c3b02016-05-18 15:37:25 -0700442 base() *ModuleBase
Inseob Kimeec88e12020-01-22 11:11:29 +0900443 Disable()
Dan Willemsen0effe062015-11-30 16:06:01 -0800444 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700445 Target() Target
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000446 MultiTargets() []Target
Anton Hansson1ee62c02020-06-30 11:51:53 +0100447 Owner() string
Dan Willemsen782a2d12015-12-21 14:55:28 -0800448 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700449 InstallInTestcases() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700450 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800451 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700452 InstallInVendorRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900453 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700454 InstallInRoot() bool
Colin Cross607d8582019-07-29 16:44:46 -0700455 InstallBypassMake() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900456 InstallForceOS() (*OsType, *ArchType)
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800457 HideFromMake()
458 IsHideFromMake() bool
Martin Stjernholm9e7f45e2020-12-23 03:50:30 +0000459 IsSkipInstall() bool
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100460 MakeUninstallable()
Liz Kammer5ca3a622020-08-05 15:40:41 -0700461 ReplacedByPrebuilt()
462 IsReplacedByPrebuilt() bool
Jiyong Park374510b2018-03-19 18:23:01 +0900463 ExportedToMake() bool
Inseob Kim8471cda2019-11-15 09:59:12 +0900464 InitRc() Paths
465 VintfFragments() Paths
Bob Badoura75b0572020-02-18 20:21:55 -0800466 NoticeFiles() Paths
Colin Cross36242852017-06-23 15:06:31 -0700467
468 AddProperties(props ...interface{})
469 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700470
Colin Crossae887032017-10-23 17:16:14 -0700471 BuildParamsForTests() []BuildParams
Colin Cross4c83e5c2019-02-25 14:54:28 -0800472 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800473 VariablesForTests() map[string]string
Paul Duffine2453c72019-05-31 14:00:04 +0100474
Colin Cross9a362232019-07-01 15:32:45 -0700475 // String returns a string that includes the module name and variants for printing during debugging.
476 String() string
477
Paul Duffine2453c72019-05-31 14:00:04 +0100478 // Get the qualified module id for this module.
479 qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName
480
481 // Get information about the properties that can contain visibility rules.
482 visibilityProperties() []visibilityProperty
Paul Duffin63c6e182019-07-24 14:24:38 +0100483
Jiyong Park6a8cf5f2019-12-30 16:31:09 +0900484 RequiredModuleNames() []string
485 HostRequiredModuleNames() []string
486 TargetRequiredModuleNames() []string
Colin Cross897266e2020-02-13 13:22:08 -0800487
Jiyong Park4dc2a1a2020-09-28 17:46:22 +0900488 FilesToInstall() InstallPaths
Jiyong Park073ea552020-11-09 14:08:34 +0900489 PackagingSpecs() []PackagingSpec
Colin Crossffe6b9d2020-12-01 15:40:06 -0800490
491 // TransitivePackagingSpecs returns the PackagingSpecs for this module and any transitive
492 // dependencies with dependency tags for which IsInstallDepNeeded() returns true.
493 TransitivePackagingSpecs() []PackagingSpec
Paul Duffine2453c72019-05-31 14:00:04 +0100494}
495
Jingwen Chenab60f122021-01-24 21:21:45 -0500496// BazelTargetModule is a lightweight wrapper interface around Module for
497// bp2build conversion purposes.
498//
499// In bp2build's bootstrap.Main execution, Soong runs an alternate pipeline of
500// mutators that creates BazelTargetModules from regular Module objects,
501// performing the mapping from Soong properties to Bazel rule attributes in the
502// process. This process may optionally create additional BazelTargetModules,
503// resulting in a 1:many mapping.
504//
505// bp2build.Codegen is then responsible for visiting all modules in the graph,
506// filtering for BazelTargetModules, and code-generating BUILD targets from
507// them.
Jingwen Chen73850672020-12-14 08:25:34 -0500508type BazelTargetModule interface {
509 Module
510
Liz Kammerfc46bc12021-02-19 11:06:17 -0500511 bazelTargetModuleProperties() *bazel.BazelTargetModuleProperties
512 SetBazelTargetModuleProperties(props bazel.BazelTargetModuleProperties)
513
514 RuleClass() string
515 BzlLoadLocation() string
Jingwen Chen73850672020-12-14 08:25:34 -0500516}
517
Jingwen Chenab60f122021-01-24 21:21:45 -0500518// InitBazelTargetModule is a wrapper function that decorates BazelTargetModule
519// with property structs containing metadata for bp2build conversion.
Jingwen Chen73850672020-12-14 08:25:34 -0500520func InitBazelTargetModule(module BazelTargetModule) {
Liz Kammerfc46bc12021-02-19 11:06:17 -0500521 module.AddProperties(module.bazelTargetModuleProperties())
Jingwen Chen73850672020-12-14 08:25:34 -0500522 InitAndroidModule(module)
523}
524
Jingwen Chenab60f122021-01-24 21:21:45 -0500525// BazelTargetModuleBase contains the property structs with metadata for
526// bp2build conversion.
Jingwen Chen73850672020-12-14 08:25:34 -0500527type BazelTargetModuleBase struct {
528 ModuleBase
529 Properties bazel.BazelTargetModuleProperties
530}
531
Liz Kammerfc46bc12021-02-19 11:06:17 -0500532// bazelTargetModuleProperties getter.
533func (btmb *BazelTargetModuleBase) bazelTargetModuleProperties() *bazel.BazelTargetModuleProperties {
Jingwen Chen73850672020-12-14 08:25:34 -0500534 return &btmb.Properties
535}
536
Liz Kammerfc46bc12021-02-19 11:06:17 -0500537// SetBazelTargetModuleProperties setter for BazelTargetModuleProperties
538func (btmb *BazelTargetModuleBase) SetBazelTargetModuleProperties(props bazel.BazelTargetModuleProperties) {
539 btmb.Properties = props
540}
541
542// RuleClass returns the rule class for this Bazel target
543func (b *BazelTargetModuleBase) RuleClass() string {
544 return b.bazelTargetModuleProperties().Rule_class
545}
546
547// BzlLoadLocation returns the rule class for this Bazel target
548func (b *BazelTargetModuleBase) BzlLoadLocation() string {
549 return b.bazelTargetModuleProperties().Bzl_load_location
550}
551
Paul Duffine2453c72019-05-31 14:00:04 +0100552// Qualified id for a module
553type qualifiedModuleName struct {
554 // The package (i.e. directory) in which the module is defined, without trailing /
555 pkg string
556
557 // The name of the module, empty string if package.
558 name string
559}
560
561func (q qualifiedModuleName) String() string {
562 if q.name == "" {
563 return "//" + q.pkg
564 }
565 return "//" + q.pkg + ":" + q.name
566}
567
Paul Duffine484f472019-06-20 16:38:08 +0100568func (q qualifiedModuleName) isRootPackage() bool {
569 return q.pkg == "" && q.name == ""
570}
571
Paul Duffine2453c72019-05-31 14:00:04 +0100572// Get the id for the package containing this module.
573func (q qualifiedModuleName) getContainingPackageId() qualifiedModuleName {
574 pkg := q.pkg
575 if q.name == "" {
Paul Duffine484f472019-06-20 16:38:08 +0100576 if pkg == "" {
577 panic(fmt.Errorf("Cannot get containing package id of root package"))
578 }
579
580 index := strings.LastIndex(pkg, "/")
581 if index == -1 {
582 pkg = ""
583 } else {
584 pkg = pkg[:index]
585 }
Paul Duffine2453c72019-05-31 14:00:04 +0100586 }
587 return newPackageId(pkg)
588}
589
590func newPackageId(pkg string) qualifiedModuleName {
591 // A qualified id for a package module has no name.
592 return qualifiedModuleName{pkg: pkg, name: ""}
Colin Cross3f40fa42015-01-30 17:27:36 -0800593}
594
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000595type Dist struct {
596 // Copy the output of this module to the $DIST_DIR when `dist` is specified on the
597 // command line and any of these targets are also on the command line, or otherwise
598 // built
599 Targets []string `android:"arch_variant"`
600
601 // The name of the output artifact. This defaults to the basename of the output of
602 // the module.
603 Dest *string `android:"arch_variant"`
604
605 // The directory within the dist directory to store the artifact. Defaults to the
606 // top level directory ("").
607 Dir *string `android:"arch_variant"`
608
609 // A suffix to add to the artifact file name (before any extension).
610 Suffix *string `android:"arch_variant"`
611
Paul Duffin74f05592020-11-25 16:37:46 +0000612 // A string tag to select the OutputFiles associated with the tag.
613 //
614 // If no tag is specified then it will select the default dist paths provided
615 // by the module type. If a tag of "" is specified then it will return the
616 // default output files provided by the modules, i.e. the result of calling
617 // OutputFiles("").
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000618 Tag *string `android:"arch_variant"`
619}
620
Colin Crossfc754582016-05-17 16:34:16 -0700621type nameProperties struct {
622 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800623 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700624}
625
Colin Cross08d6f8f2020-11-19 02:33:19 +0000626type commonProperties struct {
Dan Willemsen0effe062015-11-30 16:06:01 -0800627 // emit build rules for this module
Paul Duffin54d9bb72020-02-12 10:20:56 +0000628 //
629 // Disabling a module should only be done for those modules that cannot be built
630 // in the current environment. Modules that can build in the current environment
631 // but are not usually required (e.g. superceded by a prebuilt) should not be
632 // disabled as that will prevent them from being built by the checkbuild target
633 // and so prevent early detection of changes that have broken those modules.
Dan Willemsen0effe062015-11-30 16:06:01 -0800634 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800635
Paul Duffin2e61fa62019-03-28 14:10:57 +0000636 // Controls the visibility of this module to other modules. Allowable values are one or more of
637 // these formats:
638 //
639 // ["//visibility:public"]: Anyone can use this module.
640 // ["//visibility:private"]: Only rules in the module's package (not its subpackages) can use
641 // this module.
Paul Duffin51084ff2020-05-05 19:19:22 +0100642 // ["//visibility:override"]: Discards any rules inherited from defaults or a creating module.
643 // Can only be used at the beginning of a list of visibility rules.
Paul Duffin2e61fa62019-03-28 14:10:57 +0000644 // ["//some/package:__pkg__", "//other/package:__pkg__"]: Only modules in some/package and
645 // other/package (defined in some/package/*.bp and other/package/*.bp) have access to
646 // this module. Note that sub-packages do not have access to the rule; for example,
647 // //some/package/foo:bar or //other/package/testing:bla wouldn't have access. __pkg__
648 // is a special module and must be used verbatim. It represents all of the modules in the
649 // package.
650 // ["//project:__subpackages__", "//other:__subpackages__"]: Only modules in packages project
651 // or other or in one of their sub-packages have access to this module. For example,
652 // //project:rule, //project/library:lib or //other/testing/internal:munge are allowed
653 // to depend on this rule (but not //independent:evil)
654 // ["//project"]: This is shorthand for ["//project:__pkg__"]
655 // [":__subpackages__"]: This is shorthand for ["//project:__subpackages__"] where
656 // //project is the module's package. e.g. using [":__subpackages__"] in
657 // packages/apps/Settings/Android.bp is equivalent to
658 // //packages/apps/Settings:__subpackages__.
659 // ["//visibility:legacy_public"]: The default visibility, behaves as //visibility:public
660 // for now. It is an error if it is used in a module.
Paul Duffine2453c72019-05-31 14:00:04 +0100661 //
662 // If a module does not specify the `visibility` property then it uses the
663 // `default_visibility` property of the `package` module in the module's package.
664 //
665 // If the `default_visibility` property is not set for the module's package then
Paul Duffine484f472019-06-20 16:38:08 +0100666 // it will use the `default_visibility` of its closest ancestor package for which
667 // a `default_visibility` property is specified.
668 //
669 // If no `default_visibility` property can be found then the module uses the
670 // global default of `//visibility:legacy_public`.
Paul Duffine2453c72019-05-31 14:00:04 +0100671 //
Paul Duffin95d53b52019-07-24 13:45:05 +0100672 // The `visibility` property has no effect on a defaults module although it does
673 // apply to any non-defaults module that uses it. To set the visibility of a
674 // defaults module, use the `defaults_visibility` property on the defaults module;
675 // not to be confused with the `default_visibility` property on the package module.
676 //
Paul Duffin2e61fa62019-03-28 14:10:57 +0000677 // See https://android.googlesource.com/platform/build/soong/+/master/README.md#visibility for
678 // more details.
679 Visibility []string
680
Bob Badour37af0462021-01-07 03:34:31 +0000681 // Describes the licenses applicable to this module. Must reference license modules.
682 Licenses []string
683
684 // Flattened from direct license dependencies. Equal to Licenses unless particular module adds more.
685 Effective_licenses []string `blueprint:"mutated"`
686 // Override of module name when reporting licenses
687 Effective_package_name *string `blueprint:"mutated"`
688 // Notice files
689 Effective_license_text []string `blueprint:"mutated"`
690 // License names
691 Effective_license_kinds []string `blueprint:"mutated"`
692 // License conditions
693 Effective_license_conditions []string `blueprint:"mutated"`
694
Colin Cross7d5136f2015-05-11 13:39:40 -0700695 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800696 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
697 // 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 +0000698 // platform).
Colin Cross7d716ba2017-11-01 10:38:29 -0700699 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700700
701 Target struct {
702 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700703 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700704 }
705 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700706 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700707 }
708 }
709
Paul Duffinca7f0ef2020-02-25 15:50:49 +0000710 // If set to true then the archMutator will create variants for each arch specific target
711 // (e.g. 32/64) that the module is required to produce. If set to false then it will only
712 // create a variant for the architecture and will list the additional arch specific targets
713 // that the variant needs to produce in the CompileMultiTargets property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700714 UseTargetVariants bool `blueprint:"mutated"`
715 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800716
Dan Willemsen782a2d12015-12-21 14:55:28 -0800717 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700718 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800719
Colin Cross55708f32017-03-20 13:23:34 -0700720 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700721 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700722
Jiyong Park2db76922017-11-08 16:03:48 +0900723 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
724 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
725 // Use `soc_specific` instead for better meaning.
Colin Cross7d716ba2017-11-01 10:38:29 -0700726 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700727
Jiyong Park2db76922017-11-08 16:03:48 +0900728 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
729 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
730 Soc_specific *bool
731
732 // whether this module is specific to a device, not only for SoC, but also for off-chip
733 // peripherals. When set to true, it is installed into /odm (or /vendor/odm if odm partition
734 // does not exist, or /system/vendor/odm if both odm and vendor partitions do not exist).
735 // This implies `soc_specific:true`.
736 Device_specific *bool
737
738 // whether this module is specific to a software configuration of a product (e.g. country,
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900739 // network operator, etc). When set to true, it is installed into /product (or
740 // /system/product if product partition does not exist).
Jiyong Park2db76922017-11-08 16:03:48 +0900741 Product_specific *bool
742
Justin Yund5f6c822019-06-25 16:47:17 +0900743 // whether this module extends system. When set to true, it is installed into /system_ext
744 // (or /system/system_ext if system_ext partition does not exist).
745 System_ext_specific *bool
746
Jiyong Parkf9332f12018-02-01 00:54:12 +0900747 // Whether this module is installed to recovery partition
748 Recovery *bool
749
Yifan Hong1b3348d2020-01-21 15:53:22 -0800750 // Whether this module is installed to ramdisk
751 Ramdisk *bool
752
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700753 // Whether this module is installed to vendor ramdisk
754 Vendor_ramdisk *bool
755
Jaewoong Jung8e93aba2021-03-02 16:58:08 -0800756 // Whether this module is built for non-native architectures (also known as native bridge binary)
dimitry1f33e402019-03-26 12:39:31 +0100757 Native_bridge_supported *bool `android:"arch_variant"`
758
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700759 // init.rc files to be installed if this module is installed
Colin Cross0bab8772020-09-25 14:01:21 -0700760 Init_rc []string `android:"arch_variant,path"`
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700761
Steven Moreland57a23d22018-04-04 15:42:19 -0700762 // VINTF manifest fragments to be installed if this module is installed
Colin Cross27b922f2019-03-04 22:35:41 -0800763 Vintf_fragments []string `android:"path"`
Steven Moreland57a23d22018-04-04 15:42:19 -0700764
Chris Wolfe998306e2016-08-15 14:47:23 -0400765 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700766 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400767
Sasha Smundakb6d23052019-04-01 18:37:36 -0700768 // names of other modules to install on host if this module is installed
769 Host_required []string `android:"arch_variant"`
770
771 // names of other modules to install on target if this module is installed
772 Target_required []string `android:"arch_variant"`
773
Colin Cross5aac3622017-08-31 15:07:09 -0700774 // relative path to a file to include in the list of notices for the device
Colin Cross27b922f2019-03-04 22:35:41 -0800775 Notice *string `android:"path"`
Colin Cross5aac3622017-08-31 15:07:09 -0700776
Paul Duffinca7f0ef2020-02-25 15:50:49 +0000777 // The OsType of artifacts that this module variant is responsible for creating.
778 //
779 // Set by osMutator
780 CompileOS OsType `blueprint:"mutated"`
781
782 // The Target of artifacts that this module variant is responsible for creating.
783 //
784 // Set by archMutator
785 CompileTarget Target `blueprint:"mutated"`
786
787 // The additional arch specific targets (e.g. 32/64 bit) that this module variant is
788 // responsible for creating.
789 //
790 // By default this is nil as, where necessary, separate variants are created for the
791 // different multilib types supported and that information is encapsulated in the
792 // CompileTarget so the module variant simply needs to create artifacts for that.
793 //
794 // However, if UseTargetVariants is set to false (e.g. by
795 // InitAndroidMultiTargetsArchModule) then no separate variants are created for the
796 // multilib targets. Instead a single variant is created for the architecture and
797 // this contains the multilib specific targets that this variant should create.
798 //
799 // Set by archMutator
Colin Crossee0bc3b2018-10-02 22:01:37 -0700800 CompileMultiTargets []Target `blueprint:"mutated"`
Paul Duffinca7f0ef2020-02-25 15:50:49 +0000801
802 // True if the module variant's CompileTarget is the primary target
803 //
804 // Set by archMutator
805 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800806
807 // Set by InitAndroidModule
808 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700809 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700810
Paul Duffin1356d8c2020-02-25 19:26:33 +0000811 // If set to true then a CommonOS variant will be created which will have dependencies
812 // on all its OsType specific variants. Used by sdk/module_exports to create a snapshot
813 // that covers all os and architecture variants.
814 //
815 // The OsType specific variants can be retrieved by calling
816 // GetOsSpecificVariantsOfCommonOSVariant
817 //
818 // Set at module initialization time by calling InitCommonOSAndroidMultiTargetsArchModule
819 CreateCommonOSVariant bool `blueprint:"mutated"`
820
821 // If set to true then this variant is the CommonOS variant that has dependencies on its
822 // OsType specific variants.
823 //
824 // Set by osMutator.
825 CommonOSVariant bool `blueprint:"mutated"`
826
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800827 // When HideFromMake is set to true, no entry for this variant will be emitted in the
828 // generated Android.mk file.
829 HideFromMake bool `blueprint:"mutated"`
830
831 // When SkipInstall is set to true, calls to ctx.InstallFile, ctx.InstallExecutable,
832 // ctx.InstallSymlink and ctx.InstallAbsoluteSymlink act like calls to ctx.PackageFile
833 // and don't create a rule to install the file.
Colin Crossce75d2c2016-10-06 16:12:58 -0700834 SkipInstall bool `blueprint:"mutated"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800835
Liz Kammer5ca3a622020-08-05 15:40:41 -0700836 // Whether the module has been replaced by a prebuilt
837 ReplacedByPrebuilt bool `blueprint:"mutated"`
838
Justin Yun32f053b2020-07-31 23:07:17 +0900839 // Disabled by mutators. If set to true, it overrides Enabled property.
840 ForcedDisabled bool `blueprint:"mutated"`
841
Jeff Gaston088e29e2017-11-29 16:47:17 -0800842 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross6c4f21f2019-06-06 15:41:36 -0700843
844 MissingDeps []string `blueprint:"mutated"`
Colin Cross9a362232019-07-01 15:32:45 -0700845
846 // Name and variant strings stored by mutators to enable Module.String()
847 DebugName string `blueprint:"mutated"`
848 DebugMutators []string `blueprint:"mutated"`
849 DebugVariations []string `blueprint:"mutated"`
Colin Cross7228ecd2019-11-18 16:00:16 -0800850
Colin Crossa6845402020-11-16 15:08:19 -0800851 // ImageVariation is set by ImageMutator to specify which image this variation is for,
852 // for example "" for core or "recovery" for recovery. It will often be set to one of the
853 // constants in image.go, but can also be set to a custom value by individual module types.
Colin Cross7228ecd2019-11-18 16:00:16 -0800854 ImageVariation string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800855}
856
Paul Duffined875132020-09-02 13:08:57 +0100857type distProperties struct {
858 // configuration to distribute output files from this module to the distribution
859 // directory (default: $OUT/dist, configurable with $DIST_DIR)
860 Dist Dist `android:"arch_variant"`
861
862 // a list of configurations to distribute output files from this module to the
863 // distribution directory (default: $OUT/dist, configurable with $DIST_DIR)
864 Dists []Dist `android:"arch_variant"`
865}
866
Paul Duffin74f05592020-11-25 16:37:46 +0000867// The key to use in TaggedDistFiles when a Dist structure does not specify a
868// tag property. This intentionally does not use "" as the default because that
869// would mean that an empty tag would have a different meaning when used in a dist
870// structure that when used to reference a specific set of output paths using the
871// :module{tag} syntax, which passes tag to the OutputFiles(tag) method.
872const DefaultDistTag = "<default-dist-tag>"
873
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000874// A map of OutputFile tag keys to Paths, for disting purposes.
875type TaggedDistFiles map[string]Paths
876
Paul Duffin74f05592020-11-25 16:37:46 +0000877// addPathsForTag adds a mapping from the tag to the paths. If the map is nil
878// then it will create a map, update it and then return it. If a mapping already
879// exists for the tag then the paths are appended to the end of the current list
880// of paths, ignoring any duplicates.
881func (t TaggedDistFiles) addPathsForTag(tag string, paths ...Path) TaggedDistFiles {
882 if t == nil {
883 t = make(TaggedDistFiles)
884 }
885
886 for _, distFile := range paths {
887 if distFile != nil && !t[tag].containsPath(distFile) {
888 t[tag] = append(t[tag], distFile)
889 }
890 }
891
892 return t
893}
894
895// merge merges the entries from the other TaggedDistFiles object into this one.
896// If the TaggedDistFiles is nil then it will create a new instance, merge the
897// other into it, and then return it.
898func (t TaggedDistFiles) merge(other TaggedDistFiles) TaggedDistFiles {
899 for tag, paths := range other {
900 t = t.addPathsForTag(tag, paths...)
901 }
902
903 return t
904}
905
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000906func MakeDefaultDistFiles(paths ...Path) TaggedDistFiles {
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000907 for _, path := range paths {
908 if path == nil {
909 panic("The path to a dist file cannot be nil.")
910 }
911 }
912
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000913 // The default OutputFile tag is the empty "" string.
Paul Duffin74f05592020-11-25 16:37:46 +0000914 return TaggedDistFiles{DefaultDistTag: paths}
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000915}
916
Colin Cross3f40fa42015-01-30 17:27:36 -0800917type hostAndDeviceProperties struct {
Colin Cross4e81d702018-11-09 10:36:55 -0800918 // If set to true, build a variant of the module for the host. Defaults to false.
919 Host_supported *bool
920
921 // If set to true, build a variant of the module for the device. Defaults to true.
Colin Crossa4190c12016-07-12 13:11:25 -0700922 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800923}
924
Colin Crossc472d572015-03-17 15:06:21 -0700925type Multilib string
926
927const (
Colin Cross6b4a32d2017-12-05 13:42:45 -0800928 MultilibBoth Multilib = "both"
929 MultilibFirst Multilib = "first"
930 MultilibCommon Multilib = "common"
931 MultilibCommonFirst Multilib = "common_first"
932 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700933)
934
Colin Crossa1ad8d12016-06-01 17:09:44 -0700935type HostOrDeviceSupported int
936
937const (
Colin Cross34037c62020-11-17 13:19:17 -0800938 hostSupported = 1 << iota
939 hostCrossSupported
940 deviceSupported
941 hostDefault
942 deviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700943
944 // Host and HostCross are built by default. Device is not supported.
Colin Cross34037c62020-11-17 13:19:17 -0800945 HostSupported = hostSupported | hostCrossSupported | hostDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700946
947 // Host is built by default. HostCross and Device are not supported.
Colin Cross34037c62020-11-17 13:19:17 -0800948 HostSupportedNoCross = hostSupported | hostDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700949
950 // Device is built by default. Host and HostCross are not supported.
Colin Cross34037c62020-11-17 13:19:17 -0800951 DeviceSupported = deviceSupported | deviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700952
953 // Device is built by default. Host and HostCross are supported.
Colin Cross34037c62020-11-17 13:19:17 -0800954 HostAndDeviceSupported = hostSupported | hostCrossSupported | deviceSupported | deviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700955
956 // Host, HostCross, and Device are built by default.
Colin Cross34037c62020-11-17 13:19:17 -0800957 HostAndDeviceDefault = hostSupported | hostCrossSupported | hostDefault |
958 deviceSupported | deviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700959
960 // Nothing is supported. This is not exposed to the user, but used to mark a
961 // host only module as unsupported when the module type is not supported on
962 // the host OS. E.g. benchmarks are supported on Linux but not Darwin.
Colin Cross34037c62020-11-17 13:19:17 -0800963 NeitherHostNorDeviceSupported = 0
Colin Crossa1ad8d12016-06-01 17:09:44 -0700964)
965
Jiyong Park2db76922017-11-08 16:03:48 +0900966type moduleKind int
967
968const (
969 platformModule moduleKind = iota
970 deviceSpecificModule
971 socSpecificModule
972 productSpecificModule
Justin Yund5f6c822019-06-25 16:47:17 +0900973 systemExtSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900974)
975
976func (k moduleKind) String() string {
977 switch k {
978 case platformModule:
979 return "platform"
980 case deviceSpecificModule:
981 return "device-specific"
982 case socSpecificModule:
983 return "soc-specific"
984 case productSpecificModule:
985 return "product-specific"
Justin Yund5f6c822019-06-25 16:47:17 +0900986 case systemExtSpecificModule:
987 return "systemext-specific"
Jiyong Park2db76922017-11-08 16:03:48 +0900988 default:
989 panic(fmt.Errorf("unknown module kind %d", k))
990 }
991}
992
Colin Cross9d34f352019-11-22 16:03:51 -0800993func initAndroidModuleBase(m Module) {
994 m.base().module = m
995}
996
Colin Crossa6845402020-11-16 15:08:19 -0800997// InitAndroidModule initializes the Module as an Android module that is not architecture-specific.
998// It adds the common properties, for example "name" and "enabled".
Colin Cross36242852017-06-23 15:06:31 -0700999func InitAndroidModule(m Module) {
Colin Cross9d34f352019-11-22 16:03:51 -08001000 initAndroidModuleBase(m)
Colin Cross3f40fa42015-01-30 17:27:36 -08001001 base := m.base()
Colin Cross5049f022015-03-18 13:28:46 -07001002
Colin Cross36242852017-06-23 15:06:31 -07001003 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -07001004 &base.nameProperties,
Paul Duffined875132020-09-02 13:08:57 +01001005 &base.commonProperties,
1006 &base.distProperties)
Colin Cross18c46802019-09-24 22:19:02 -07001007
Colin Crosseabaedd2020-02-06 17:01:55 -08001008 initProductVariableModule(m)
Colin Cross18c46802019-09-24 22:19:02 -07001009
Colin Crossa3a97412019-03-18 12:24:29 -07001010 base.generalProperties = m.GetProperties()
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -07001011 base.customizableProperties = m.GetProperties()
Paul Duffin63c6e182019-07-24 14:24:38 +01001012
1013 // The default_visibility property needs to be checked and parsed by the visibility module during
Paul Duffin5ec73ec2020-05-01 17:52:01 +01001014 // its checking and parsing phases so make it the primary visibility property.
1015 setPrimaryVisibilityProperty(m, "visibility", &base.commonProperties.Visibility)
Bob Badour37af0462021-01-07 03:34:31 +00001016
1017 // The default_applicable_licenses property needs to be checked and parsed by the licenses module during
1018 // its checking and parsing phases so make it the primary licenses property.
1019 setPrimaryLicensesProperty(m, "licenses", &base.commonProperties.Licenses)
Colin Cross5049f022015-03-18 13:28:46 -07001020}
1021
Colin Crossa6845402020-11-16 15:08:19 -08001022// InitAndroidArchModule initializes the Module as an Android module that is architecture-specific.
1023// It adds the common properties, for example "name" and "enabled", as well as runtime generated
1024// property structs for architecture-specific versions of generic properties tagged with
1025// `android:"arch_variant"`.
1026//
1027// InitAndroidModule should not be called if InitAndroidArchModule was called.
Colin Cross36242852017-06-23 15:06:31 -07001028func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
1029 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -07001030
1031 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -08001032 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -07001033 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -07001034 base.commonProperties.ArchSpecific = true
Colin Crossee0bc3b2018-10-02 22:01:37 -07001035 base.commonProperties.UseTargetVariants = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001036
Colin Cross34037c62020-11-17 13:19:17 -08001037 if hod&hostSupported != 0 && hod&deviceSupported != 0 {
Colin Cross36242852017-06-23 15:06:31 -07001038 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001039 }
1040
Colin Crossa6845402020-11-16 15:08:19 -08001041 initArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -08001042}
1043
Colin Crossa6845402020-11-16 15:08:19 -08001044// InitAndroidMultiTargetsArchModule initializes the Module as an Android module that is
1045// architecture-specific, but will only have a single variant per OS that handles all the
1046// architectures simultaneously. The list of Targets that it must handle will be available from
1047// ModuleContext.MultiTargets. It adds the common properties, for example "name" and "enabled", as
1048// well as runtime generated property structs for architecture-specific versions of generic
1049// properties tagged with `android:"arch_variant"`.
1050//
1051// InitAndroidModule or InitAndroidArchModule should not be called if
1052// InitAndroidMultiTargetsArchModule was called.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001053func InitAndroidMultiTargetsArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
1054 InitAndroidArchModule(m, hod, defaultMultilib)
1055 m.base().commonProperties.UseTargetVariants = false
1056}
1057
Colin Crossa6845402020-11-16 15:08:19 -08001058// InitCommonOSAndroidMultiTargetsArchModule initializes the Module as an Android module that is
1059// architecture-specific, but will only have a single variant per OS that handles all the
1060// architectures simultaneously, and will also have an additional CommonOS variant that has
1061// dependencies on all the OS-specific variants. The list of Targets that it must handle will be
1062// available from ModuleContext.MultiTargets. It adds the common properties, for example "name" and
1063// "enabled", as well as runtime generated property structs for architecture-specific versions of
1064// generic properties tagged with `android:"arch_variant"`.
1065//
1066// InitAndroidModule, InitAndroidArchModule or InitAndroidMultiTargetsArchModule should not be
1067// called if InitCommonOSAndroidMultiTargetsArchModule was called.
Paul Duffin1356d8c2020-02-25 19:26:33 +00001068func InitCommonOSAndroidMultiTargetsArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
1069 InitAndroidArchModule(m, hod, defaultMultilib)
1070 m.base().commonProperties.UseTargetVariants = false
1071 m.base().commonProperties.CreateCommonOSVariant = true
1072}
1073
Nan Zhangb9eeb1d2017-02-02 10:46:07 -08001074// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -08001075// modules. It should be included as an anonymous field in every module
1076// struct definition. InitAndroidModule should then be called from the module's
1077// factory function, and the return values from InitAndroidModule should be
1078// returned from the factory function.
1079//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -08001080// The ModuleBase type is responsible for implementing the GenerateBuildActions
1081// method to support the blueprint.Module interface. This method will then call
1082// the module's GenerateAndroidBuildActions method once for each build variant
Colin Cross25de6c32019-06-06 14:29:25 -07001083// that is to be built. GenerateAndroidBuildActions is passed a ModuleContext
1084// rather than the usual blueprint.ModuleContext.
1085// ModuleContext exposes extra functionality specific to the Android build
Colin Cross3f40fa42015-01-30 17:27:36 -08001086// system including details about the particular build variant that is to be
1087// generated.
1088//
1089// For example:
1090//
1091// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -08001092// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001093// )
1094//
1095// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -08001096// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -08001097// properties struct {
1098// MyProperty string
1099// }
1100// }
1101//
Colin Cross36242852017-06-23 15:06:31 -07001102// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001103// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -07001104// m.AddProperties(&m.properties)
1105// android.InitAndroidModule(m)
1106// return m
Colin Cross3f40fa42015-01-30 17:27:36 -08001107// }
1108//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -08001109// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001110// // Get the CPU architecture for the current build variant.
1111// variantArch := ctx.Arch()
1112//
1113// // ...
1114// }
Colin Cross635c3b02016-05-18 15:37:25 -07001115type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -08001116 // Putting the curiously recurring thing pointing to the thing that contains
1117 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -07001118 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -07001119 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -08001120
Colin Crossfc754582016-05-17 16:34:16 -07001121 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001122 commonProperties commonProperties
Paul Duffined875132020-09-02 13:08:57 +01001123 distProperties distProperties
Colin Cross18c46802019-09-24 22:19:02 -07001124 variableProperties interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001125 hostAndDeviceProperties hostAndDeviceProperties
1126 generalProperties []interface{}
Jingwen Chen5d864492021-02-24 07:20:12 -05001127
1128 // Arch specific versions of structs in generalProperties. The outer index
1129 // has the same order as generalProperties as initialized in
1130 // InitAndroidArchModule, and the inner index chooses the props specific to
1131 // the architecture. The interface{} value is an archPropRoot that is
1132 // filled with arch specific values by the arch mutator.
1133 archProperties [][]interface{}
1134
1135 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001136
Jingwen Chen73850672020-12-14 08:25:34 -05001137 // Properties specific to the Blueprint to BUILD migration.
1138 bazelTargetModuleProperties bazel.BazelTargetModuleProperties
1139
Paul Duffin63c6e182019-07-24 14:24:38 +01001140 // Information about all the properties on the module that contains visibility rules that need
1141 // checking.
1142 visibilityPropertyInfo []visibilityProperty
1143
1144 // The primary visibility property, may be nil, that controls access to the module.
1145 primaryVisibilityProperty visibilityProperty
1146
Bob Badour37af0462021-01-07 03:34:31 +00001147 // The primary licenses property, may be nil, records license metadata for the module.
1148 primaryLicensesProperty applicableLicensesProperty
1149
Colin Crossffe6b9d2020-12-01 15:40:06 -08001150 noAddressSanitizer bool
1151 installFiles InstallPaths
1152 installFilesDepSet *installPathsDepSet
1153 checkbuildFiles Paths
1154 packagingSpecs []PackagingSpec
1155 packagingSpecsDepSet *packagingSpecsDepSet
1156 noticeFiles Paths
1157 phonies map[string]Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001158
Paul Duffinaf970a22020-11-23 23:32:56 +00001159 // The files to copy to the dist as explicitly specified in the .bp file.
1160 distFiles TaggedDistFiles
1161
Colin Cross1f8c52b2015-06-16 16:38:17 -07001162 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
1163 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -08001164 installTarget WritablePath
1165 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -07001166 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -07001167
Colin Cross178a5092016-09-13 13:42:32 -07001168 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -07001169
1170 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -07001171
1172 // For tests
Colin Crossae887032017-10-23 17:16:14 -07001173 buildParams []BuildParams
Colin Cross4c83e5c2019-02-25 14:54:28 -08001174 ruleParams map[blueprint.Rule]blueprint.RuleParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001175 variables map[string]string
Colin Crossa9d8bee2018-10-02 13:59:46 -07001176
Inseob Kim8471cda2019-11-15 09:59:12 +09001177 initRcPaths Paths
1178 vintfFragmentsPaths Paths
Colin Cross36242852017-06-23 15:06:31 -07001179}
1180
Paul Duffin44f1d842020-06-26 20:17:02 +01001181func (m *ModuleBase) ComponentDepsMutator(BottomUpMutatorContext) {}
1182
Colin Cross4157e882019-06-06 16:57:04 -07001183func (m *ModuleBase) DepsMutator(BottomUpMutatorContext) {}
Colin Cross5f692ec2019-02-01 16:53:07 -08001184
Colin Cross4157e882019-06-06 16:57:04 -07001185func (m *ModuleBase) AddProperties(props ...interface{}) {
1186 m.registerProps = append(m.registerProps, props...)
Colin Cross36242852017-06-23 15:06:31 -07001187}
1188
Colin Cross4157e882019-06-06 16:57:04 -07001189func (m *ModuleBase) GetProperties() []interface{} {
1190 return m.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -08001191}
1192
Colin Cross4157e882019-06-06 16:57:04 -07001193func (m *ModuleBase) BuildParamsForTests() []BuildParams {
1194 return m.buildParams
Colin Crosscec81712017-07-13 14:43:27 -07001195}
1196
Colin Cross4157e882019-06-06 16:57:04 -07001197func (m *ModuleBase) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams {
1198 return m.ruleParams
Colin Cross4c83e5c2019-02-25 14:54:28 -08001199}
1200
Colin Cross4157e882019-06-06 16:57:04 -07001201func (m *ModuleBase) VariablesForTests() map[string]string {
1202 return m.variables
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001203}
1204
Colin Crossce75d2c2016-10-06 16:12:58 -07001205// Name returns the name of the module. It may be overridden by individual module types, for
1206// example prebuilts will prepend prebuilt_ to the name.
Colin Cross4157e882019-06-06 16:57:04 -07001207func (m *ModuleBase) Name() string {
1208 return String(m.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -07001209}
1210
Colin Cross9a362232019-07-01 15:32:45 -07001211// String returns a string that includes the module name and variants for printing during debugging.
1212func (m *ModuleBase) String() string {
1213 sb := strings.Builder{}
1214 sb.WriteString(m.commonProperties.DebugName)
1215 sb.WriteString("{")
1216 for i := range m.commonProperties.DebugMutators {
1217 if i != 0 {
1218 sb.WriteString(",")
1219 }
1220 sb.WriteString(m.commonProperties.DebugMutators[i])
1221 sb.WriteString(":")
1222 sb.WriteString(m.commonProperties.DebugVariations[i])
1223 }
1224 sb.WriteString("}")
1225 return sb.String()
1226}
1227
Colin Crossce75d2c2016-10-06 16:12:58 -07001228// BaseModuleName returns the name of the module as specified in the blueprints file.
Colin Cross4157e882019-06-06 16:57:04 -07001229func (m *ModuleBase) BaseModuleName() string {
1230 return String(m.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -07001231}
1232
Colin Cross4157e882019-06-06 16:57:04 -07001233func (m *ModuleBase) base() *ModuleBase {
1234 return m
Colin Cross3f40fa42015-01-30 17:27:36 -08001235}
1236
Paul Duffine2453c72019-05-31 14:00:04 +01001237func (m *ModuleBase) qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName {
1238 return qualifiedModuleName{pkg: ctx.ModuleDir(), name: ctx.ModuleName()}
1239}
1240
1241func (m *ModuleBase) visibilityProperties() []visibilityProperty {
Paul Duffin63c6e182019-07-24 14:24:38 +01001242 return m.visibilityPropertyInfo
1243}
1244
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001245func (m *ModuleBase) Dists() []Dist {
Paul Duffined875132020-09-02 13:08:57 +01001246 if len(m.distProperties.Dist.Targets) > 0 {
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001247 // Make a copy of the underlying Dists slice to protect against
1248 // backing array modifications with repeated calls to this method.
Paul Duffined875132020-09-02 13:08:57 +01001249 distsCopy := append([]Dist(nil), m.distProperties.Dists...)
1250 return append(distsCopy, m.distProperties.Dist)
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001251 } else {
Paul Duffined875132020-09-02 13:08:57 +01001252 return m.distProperties.Dists
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001253 }
1254}
1255
1256func (m *ModuleBase) GenerateTaggedDistFiles(ctx BaseModuleContext) TaggedDistFiles {
Paul Duffin74f05592020-11-25 16:37:46 +00001257 var distFiles TaggedDistFiles
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001258 for _, dist := range m.Dists() {
Paul Duffin74f05592020-11-25 16:37:46 +00001259 // If no tag is specified then it means to use the default dist paths so use
1260 // the special tag name which represents that.
1261 tag := proptools.StringDefault(dist.Tag, DefaultDistTag)
1262
Paul Duffinaf970a22020-11-23 23:32:56 +00001263 if outputFileProducer, ok := m.module.(OutputFileProducer); ok {
1264 // Call the OutputFiles(tag) method to get the paths associated with the tag.
1265 distFilesForTag, err := outputFileProducer.OutputFiles(tag)
Paul Duffin74f05592020-11-25 16:37:46 +00001266
Paul Duffinaf970a22020-11-23 23:32:56 +00001267 // If the tag was not supported and is not DefaultDistTag then it is an error.
1268 // Failing to find paths for DefaultDistTag is not an error. It just means
1269 // that the module type requires the legacy behavior.
1270 if err != nil && tag != DefaultDistTag {
1271 ctx.PropertyErrorf("dist.tag", "%s", err.Error())
1272 }
1273
1274 distFiles = distFiles.addPathsForTag(tag, distFilesForTag...)
1275 } else if tag != DefaultDistTag {
1276 // If the tag was specified then it is an error if the module does not
1277 // implement OutputFileProducer because there is no other way of accessing
1278 // the paths for the specified tag.
1279 ctx.PropertyErrorf("dist.tag",
1280 "tag %s not supported because the module does not implement OutputFileProducer", tag)
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001281 }
Jingwen Chen40fd90a2020-06-15 05:24:19 +00001282 }
1283
1284 return distFiles
1285}
1286
Colin Cross4157e882019-06-06 16:57:04 -07001287func (m *ModuleBase) Target() Target {
1288 return m.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -08001289}
1290
Colin Cross4157e882019-06-06 16:57:04 -07001291func (m *ModuleBase) TargetPrimary() bool {
1292 return m.commonProperties.CompilePrimary
Colin Cross8b74d172016-09-13 09:59:14 -07001293}
1294
Colin Cross4157e882019-06-06 16:57:04 -07001295func (m *ModuleBase) MultiTargets() []Target {
1296 return m.commonProperties.CompileMultiTargets
Colin Crossee0bc3b2018-10-02 22:01:37 -07001297}
1298
Colin Cross4157e882019-06-06 16:57:04 -07001299func (m *ModuleBase) Os() OsType {
1300 return m.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -08001301}
1302
Colin Cross4157e882019-06-06 16:57:04 -07001303func (m *ModuleBase) Host() bool {
Jiyong Park1613e552020-09-14 19:43:17 +09001304 return m.Os().Class == Host
Dan Willemsen97750522016-02-09 17:43:51 -08001305}
1306
Yo Chiangbba545e2020-06-09 16:15:37 +08001307func (m *ModuleBase) Device() bool {
1308 return m.Os().Class == Device
1309}
1310
Colin Cross4157e882019-06-06 16:57:04 -07001311func (m *ModuleBase) Arch() Arch {
1312 return m.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -08001313}
1314
Colin Cross4157e882019-06-06 16:57:04 -07001315func (m *ModuleBase) ArchSpecific() bool {
1316 return m.commonProperties.ArchSpecific
Dan Willemsen0b24c742016-10-04 15:13:37 -07001317}
1318
Paul Duffin1356d8c2020-02-25 19:26:33 +00001319// True if the current variant is a CommonOS variant, false otherwise.
1320func (m *ModuleBase) IsCommonOSVariant() bool {
1321 return m.commonProperties.CommonOSVariant
1322}
1323
Colin Cross34037c62020-11-17 13:19:17 -08001324// supportsTarget returns true if the given Target is supported by the current module.
1325func (m *ModuleBase) supportsTarget(target Target) bool {
1326 switch target.Os.Class {
1327 case Host:
1328 if target.HostCross {
1329 return m.HostCrossSupported()
1330 } else {
1331 return m.HostSupported()
Colin Crossa1ad8d12016-06-01 17:09:44 -07001332 }
Colin Cross34037c62020-11-17 13:19:17 -08001333 case Device:
1334 return m.DeviceSupported()
Colin Crossa1ad8d12016-06-01 17:09:44 -07001335 default:
Jiyong Park1613e552020-09-14 19:43:17 +09001336 return false
Colin Crossa1ad8d12016-06-01 17:09:44 -07001337 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001338}
1339
Colin Cross34037c62020-11-17 13:19:17 -08001340// DeviceSupported returns true if the current module is supported and enabled for device targets,
1341// i.e. the factory method set the HostOrDeviceSupported value to include device support and
1342// the device support is enabled by default or enabled by the device_supported property.
Colin Cross4157e882019-06-06 16:57:04 -07001343func (m *ModuleBase) DeviceSupported() bool {
Colin Cross34037c62020-11-17 13:19:17 -08001344 hod := m.commonProperties.HostOrDeviceSupported
1345 // deviceEnabled is true if the device_supported property is true or the HostOrDeviceSupported
1346 // value has the deviceDefault bit set.
1347 deviceEnabled := proptools.BoolDefault(m.hostAndDeviceProperties.Device_supported, hod&deviceDefault != 0)
1348 return hod&deviceSupported != 0 && deviceEnabled
Colin Cross3f40fa42015-01-30 17:27:36 -08001349}
1350
Colin Cross34037c62020-11-17 13:19:17 -08001351// HostSupported returns true if the current module is supported and enabled for host targets,
1352// i.e. the factory method set the HostOrDeviceSupported value to include host support and
1353// the host support is enabled by default or enabled by the host_supported property.
Paul Duffine44358f2019-11-26 18:04:12 +00001354func (m *ModuleBase) HostSupported() bool {
Colin Cross34037c62020-11-17 13:19:17 -08001355 hod := m.commonProperties.HostOrDeviceSupported
1356 // hostEnabled is true if the host_supported property is true or the HostOrDeviceSupported
1357 // value has the hostDefault bit set.
1358 hostEnabled := proptools.BoolDefault(m.hostAndDeviceProperties.Host_supported, hod&hostDefault != 0)
1359 return hod&hostSupported != 0 && hostEnabled
1360}
1361
1362// HostCrossSupported returns true if the current module is supported and enabled for host cross
1363// targets, i.e. the factory method set the HostOrDeviceSupported value to include host cross
1364// support and the host cross support is enabled by default or enabled by the
1365// host_supported property.
1366func (m *ModuleBase) HostCrossSupported() bool {
1367 hod := m.commonProperties.HostOrDeviceSupported
1368 // hostEnabled is true if the host_supported property is true or the HostOrDeviceSupported
1369 // value has the hostDefault bit set.
1370 hostEnabled := proptools.BoolDefault(m.hostAndDeviceProperties.Host_supported, hod&hostDefault != 0)
1371 return hod&hostCrossSupported != 0 && hostEnabled
Paul Duffine44358f2019-11-26 18:04:12 +00001372}
1373
Colin Cross4157e882019-06-06 16:57:04 -07001374func (m *ModuleBase) Platform() bool {
Justin Yund5f6c822019-06-25 16:47:17 +09001375 return !m.DeviceSpecific() && !m.SocSpecific() && !m.ProductSpecific() && !m.SystemExtSpecific()
Jiyong Parkc678ad32018-04-10 13:07:10 +09001376}
1377
Colin Cross4157e882019-06-06 16:57:04 -07001378func (m *ModuleBase) DeviceSpecific() bool {
1379 return Bool(m.commonProperties.Device_specific)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001380}
1381
Colin Cross4157e882019-06-06 16:57:04 -07001382func (m *ModuleBase) SocSpecific() bool {
1383 return Bool(m.commonProperties.Vendor) || Bool(m.commonProperties.Proprietary) || Bool(m.commonProperties.Soc_specific)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001384}
1385
Colin Cross4157e882019-06-06 16:57:04 -07001386func (m *ModuleBase) ProductSpecific() bool {
1387 return Bool(m.commonProperties.Product_specific)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001388}
1389
Justin Yund5f6c822019-06-25 16:47:17 +09001390func (m *ModuleBase) SystemExtSpecific() bool {
1391 return Bool(m.commonProperties.System_ext_specific)
Dario Frenifd05a742018-05-29 13:28:54 +01001392}
1393
Colin Crossc2d24052020-05-13 11:05:02 -07001394// RequiresStableAPIs returns true if the module will be installed to a partition that may
1395// be updated separately from the system image.
1396func (m *ModuleBase) RequiresStableAPIs(ctx BaseModuleContext) bool {
1397 return m.SocSpecific() || m.DeviceSpecific() ||
1398 (m.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface())
1399}
1400
Bill Peckhamfff3f8a2020-03-20 18:33:20 -07001401func (m *ModuleBase) PartitionTag(config DeviceConfig) string {
1402 partition := "system"
1403 if m.SocSpecific() {
1404 // A SoC-specific module could be on the vendor partition at
1405 // "vendor" or the system partition at "system/vendor".
1406 if config.VendorPath() == "vendor" {
1407 partition = "vendor"
1408 }
1409 } else if m.DeviceSpecific() {
1410 // A device-specific module could be on the odm partition at
1411 // "odm", the vendor partition at "vendor/odm", or the system
1412 // partition at "system/vendor/odm".
1413 if config.OdmPath() == "odm" {
1414 partition = "odm"
Ramy Medhat944839a2020-03-31 22:14:52 -04001415 } else if strings.HasPrefix(config.OdmPath(), "vendor/") {
Bill Peckhamfff3f8a2020-03-20 18:33:20 -07001416 partition = "vendor"
1417 }
1418 } else if m.ProductSpecific() {
1419 // A product-specific module could be on the product partition
1420 // at "product" or the system partition at "system/product".
1421 if config.ProductPath() == "product" {
1422 partition = "product"
1423 }
1424 } else if m.SystemExtSpecific() {
1425 // A system_ext-specific module could be on the system_ext
1426 // partition at "system_ext" or the system partition at
1427 // "system/system_ext".
1428 if config.SystemExtPath() == "system_ext" {
1429 partition = "system_ext"
1430 }
1431 }
1432 return partition
1433}
1434
Colin Cross4157e882019-06-06 16:57:04 -07001435func (m *ModuleBase) Enabled() bool {
Justin Yun32f053b2020-07-31 23:07:17 +09001436 if m.commonProperties.ForcedDisabled {
1437 return false
1438 }
Colin Cross08d6f8f2020-11-19 02:33:19 +00001439 if m.commonProperties.Enabled == nil {
1440 return !m.Os().DefaultDisabled
1441 }
1442 return *m.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -08001443}
1444
Inseob Kimeec88e12020-01-22 11:11:29 +09001445func (m *ModuleBase) Disable() {
Justin Yun32f053b2020-07-31 23:07:17 +09001446 m.commonProperties.ForcedDisabled = true
Inseob Kimeec88e12020-01-22 11:11:29 +09001447}
1448
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001449// HideFromMake marks this variant so that it is not emitted in the generated Android.mk file.
1450func (m *ModuleBase) HideFromMake() {
1451 m.commonProperties.HideFromMake = true
1452}
1453
1454// IsHideFromMake returns true if HideFromMake was previously called.
1455func (m *ModuleBase) IsHideFromMake() bool {
1456 return m.commonProperties.HideFromMake == true
1457}
1458
1459// SkipInstall marks this variant to not create install rules when ctx.Install* are called.
Colin Cross4157e882019-06-06 16:57:04 -07001460func (m *ModuleBase) SkipInstall() {
1461 m.commonProperties.SkipInstall = true
Colin Crossce75d2c2016-10-06 16:12:58 -07001462}
1463
Martin Stjernholm9e7f45e2020-12-23 03:50:30 +00001464// IsSkipInstall returns true if this variant is marked to not create install
1465// rules when ctx.Install* are called.
1466func (m *ModuleBase) IsSkipInstall() bool {
1467 return m.commonProperties.SkipInstall
1468}
1469
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001470// Similar to HideFromMake, but if the AndroidMk entry would set
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01001471// LOCAL_UNINSTALLABLE_MODULE then this variant may still output that entry
1472// rather than leaving it out altogether. That happens in cases where it would
1473// have other side effects, in particular when it adds a NOTICE file target,
1474// which other install targets might depend on.
1475func (m *ModuleBase) MakeUninstallable() {
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001476 m.HideFromMake()
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +01001477}
1478
Liz Kammer5ca3a622020-08-05 15:40:41 -07001479func (m *ModuleBase) ReplacedByPrebuilt() {
1480 m.commonProperties.ReplacedByPrebuilt = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001481 m.HideFromMake()
Liz Kammer5ca3a622020-08-05 15:40:41 -07001482}
1483
1484func (m *ModuleBase) IsReplacedByPrebuilt() bool {
1485 return m.commonProperties.ReplacedByPrebuilt
1486}
1487
Colin Cross4157e882019-06-06 16:57:04 -07001488func (m *ModuleBase) ExportedToMake() bool {
1489 return m.commonProperties.NamespaceExportedToMake
Jiyong Park374510b2018-03-19 18:23:01 +09001490}
1491
Colin Crosse9fe2942020-11-10 18:12:15 -08001492// computeInstallDeps finds the installed paths of all dependencies that have a dependency
1493// tag that is annotated as needing installation via the IsInstallDepNeeded method.
Colin Crossffe6b9d2020-12-01 15:40:06 -08001494func (m *ModuleBase) computeInstallDeps(ctx ModuleContext) ([]*installPathsDepSet, []*packagingSpecsDepSet) {
Colin Cross5d583952020-11-24 16:21:24 -08001495 var installDeps []*installPathsDepSet
Colin Crossffe6b9d2020-12-01 15:40:06 -08001496 var packagingSpecs []*packagingSpecsDepSet
Colin Cross5d583952020-11-24 16:21:24 -08001497 ctx.VisitDirectDeps(func(dep Module) {
1498 if IsInstallDepNeeded(ctx.OtherModuleDependencyTag(dep)) {
1499 installDeps = append(installDeps, dep.base().installFilesDepSet)
Colin Crossffe6b9d2020-12-01 15:40:06 -08001500 packagingSpecs = append(packagingSpecs, dep.base().packagingSpecsDepSet)
Colin Cross897266e2020-02-13 13:22:08 -08001501 }
1502 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001503
Colin Crossffe6b9d2020-12-01 15:40:06 -08001504 return installDeps, packagingSpecs
Colin Cross3f40fa42015-01-30 17:27:36 -08001505}
1506
Jiyong Park4dc2a1a2020-09-28 17:46:22 +09001507func (m *ModuleBase) FilesToInstall() InstallPaths {
Colin Cross4157e882019-06-06 16:57:04 -07001508 return m.installFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001509}
1510
Jiyong Park073ea552020-11-09 14:08:34 +09001511func (m *ModuleBase) PackagingSpecs() []PackagingSpec {
1512 return m.packagingSpecs
1513}
1514
Colin Crossffe6b9d2020-12-01 15:40:06 -08001515func (m *ModuleBase) TransitivePackagingSpecs() []PackagingSpec {
1516 return m.packagingSpecsDepSet.ToList()
1517}
1518
Colin Cross4157e882019-06-06 16:57:04 -07001519func (m *ModuleBase) NoAddressSanitizer() bool {
1520 return m.noAddressSanitizer
Colin Cross3f40fa42015-01-30 17:27:36 -08001521}
1522
Colin Cross4157e882019-06-06 16:57:04 -07001523func (m *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001524 return false
1525}
1526
Jaewoong Jung0949f312019-09-11 10:25:18 -07001527func (m *ModuleBase) InstallInTestcases() bool {
1528 return false
1529}
1530
Colin Cross4157e882019-06-06 16:57:04 -07001531func (m *ModuleBase) InstallInSanitizerDir() bool {
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001532 return false
1533}
1534
Yifan Hong1b3348d2020-01-21 15:53:22 -08001535func (m *ModuleBase) InstallInRamdisk() bool {
1536 return Bool(m.commonProperties.Ramdisk)
1537}
1538
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001539func (m *ModuleBase) InstallInVendorRamdisk() bool {
1540 return Bool(m.commonProperties.Vendor_ramdisk)
1541}
1542
Colin Cross4157e882019-06-06 16:57:04 -07001543func (m *ModuleBase) InstallInRecovery() bool {
1544 return Bool(m.commonProperties.Recovery)
Jiyong Parkf9332f12018-02-01 00:54:12 +09001545}
1546
Colin Cross90ba5f42019-10-02 11:10:58 -07001547func (m *ModuleBase) InstallInRoot() bool {
1548 return false
1549}
1550
Colin Cross607d8582019-07-29 16:44:46 -07001551func (m *ModuleBase) InstallBypassMake() bool {
1552 return false
1553}
1554
Jiyong Park87788b52020-09-01 12:37:45 +09001555func (m *ModuleBase) InstallForceOS() (*OsType, *ArchType) {
1556 return nil, nil
Colin Cross6e359402020-02-10 15:29:54 -08001557}
1558
Colin Cross4157e882019-06-06 16:57:04 -07001559func (m *ModuleBase) Owner() string {
1560 return String(m.commonProperties.Owner)
Sundong Ahn4fd04bb2018-08-31 18:01:37 +09001561}
1562
Bob Badoura75b0572020-02-18 20:21:55 -08001563func (m *ModuleBase) NoticeFiles() Paths {
1564 return m.noticeFiles
Jiyong Park52818fc2019-03-18 12:01:38 +09001565}
1566
Colin Cross7228ecd2019-11-18 16:00:16 -08001567func (m *ModuleBase) setImageVariation(variant string) {
1568 m.commonProperties.ImageVariation = variant
1569}
1570
1571func (m *ModuleBase) ImageVariation() blueprint.Variation {
1572 return blueprint.Variation{
1573 Mutator: "image",
1574 Variation: m.base().commonProperties.ImageVariation,
1575 }
1576}
1577
Paul Duffin9b76c0b2020-03-12 10:24:35 +00001578func (m *ModuleBase) getVariationByMutatorName(mutator string) string {
1579 for i, v := range m.commonProperties.DebugMutators {
1580 if v == mutator {
1581 return m.commonProperties.DebugVariations[i]
1582 }
1583 }
1584
1585 return ""
1586}
1587
Yifan Hong1b3348d2020-01-21 15:53:22 -08001588func (m *ModuleBase) InRamdisk() bool {
1589 return m.base().commonProperties.ImageVariation == RamdiskVariation
1590}
1591
Yifan Hong60e0cfb2020-10-21 15:17:56 -07001592func (m *ModuleBase) InVendorRamdisk() bool {
1593 return m.base().commonProperties.ImageVariation == VendorRamdiskVariation
1594}
1595
Colin Cross7228ecd2019-11-18 16:00:16 -08001596func (m *ModuleBase) InRecovery() bool {
1597 return m.base().commonProperties.ImageVariation == RecoveryVariation
1598}
1599
Jiyong Park6a8cf5f2019-12-30 16:31:09 +09001600func (m *ModuleBase) RequiredModuleNames() []string {
1601 return m.base().commonProperties.Required
1602}
1603
1604func (m *ModuleBase) HostRequiredModuleNames() []string {
1605 return m.base().commonProperties.Host_required
1606}
1607
1608func (m *ModuleBase) TargetRequiredModuleNames() []string {
1609 return m.base().commonProperties.Target_required
1610}
1611
Inseob Kim8471cda2019-11-15 09:59:12 +09001612func (m *ModuleBase) InitRc() Paths {
1613 return append(Paths{}, m.initRcPaths...)
1614}
1615
1616func (m *ModuleBase) VintfFragments() Paths {
1617 return append(Paths{}, m.vintfFragmentsPaths...)
1618}
1619
Colin Cross4157e882019-06-06 16:57:04 -07001620func (m *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Colin Cross897266e2020-02-13 13:22:08 -08001621 var allInstalledFiles InstallPaths
1622 var allCheckbuildFiles Paths
Colin Cross0875c522017-11-28 17:34:01 -08001623 ctx.VisitAllModuleVariants(func(module Module) {
1624 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -07001625 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
1626 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001627 })
1628
Colin Cross0875c522017-11-28 17:34:01 -08001629 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -07001630
Colin Cross133ebef2020-08-14 17:38:45 -07001631 namespacePrefix := ctx.Namespace().id
Jeff Gaston088e29e2017-11-29 16:47:17 -08001632 if namespacePrefix != "" {
1633 namespacePrefix = namespacePrefix + "-"
1634 }
1635
Colin Cross3f40fa42015-01-30 17:27:36 -08001636 if len(allInstalledFiles) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001637 name := namespacePrefix + ctx.ModuleName() + "-install"
1638 ctx.Phony(name, allInstalledFiles.Paths()...)
1639 m.installTarget = PathForPhony(ctx, name)
1640 deps = append(deps, m.installTarget)
Colin Cross9454bfa2015-03-17 13:24:18 -07001641 }
1642
1643 if len(allCheckbuildFiles) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001644 name := namespacePrefix + ctx.ModuleName() + "-checkbuild"
1645 ctx.Phony(name, allCheckbuildFiles...)
1646 m.checkbuildTarget = PathForPhony(ctx, name)
1647 deps = append(deps, m.checkbuildTarget)
Colin Cross9454bfa2015-03-17 13:24:18 -07001648 }
1649
1650 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001651 suffix := ""
Jingwen Chencda22c92020-11-23 00:22:30 -05001652 if ctx.Config().KatiEnabled() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001653 suffix = "-soong"
1654 }
1655
Colin Crossc3d87d32020-06-04 13:25:17 -07001656 ctx.Phony(namespacePrefix+ctx.ModuleName()+suffix, deps...)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001657
Colin Cross4157e882019-06-06 16:57:04 -07001658 m.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -08001659 }
1660}
1661
Colin Crossc34d2322020-01-03 15:23:27 -08001662func determineModuleKind(m *ModuleBase, ctx blueprint.EarlyModuleContext) moduleKind {
Colin Cross4157e882019-06-06 16:57:04 -07001663 var socSpecific = Bool(m.commonProperties.Vendor) || Bool(m.commonProperties.Proprietary) || Bool(m.commonProperties.Soc_specific)
1664 var deviceSpecific = Bool(m.commonProperties.Device_specific)
1665 var productSpecific = Bool(m.commonProperties.Product_specific)
Justin Yund5f6c822019-06-25 16:47:17 +09001666 var systemExtSpecific = Bool(m.commonProperties.System_ext_specific)
Jiyong Park2db76922017-11-08 16:03:48 +09001667
Dario Frenifd05a742018-05-29 13:28:54 +01001668 msg := "conflicting value set here"
1669 if socSpecific && deviceSpecific {
1670 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
Colin Cross4157e882019-06-06 16:57:04 -07001671 if Bool(m.commonProperties.Vendor) {
Jiyong Park2db76922017-11-08 16:03:48 +09001672 ctx.PropertyErrorf("vendor", msg)
1673 }
Colin Cross4157e882019-06-06 16:57:04 -07001674 if Bool(m.commonProperties.Proprietary) {
Jiyong Park2db76922017-11-08 16:03:48 +09001675 ctx.PropertyErrorf("proprietary", msg)
1676 }
Colin Cross4157e882019-06-06 16:57:04 -07001677 if Bool(m.commonProperties.Soc_specific) {
Jiyong Park2db76922017-11-08 16:03:48 +09001678 ctx.PropertyErrorf("soc_specific", msg)
1679 }
1680 }
1681
Justin Yund5f6c822019-06-25 16:47:17 +09001682 if productSpecific && systemExtSpecific {
1683 ctx.PropertyErrorf("product_specific", "a module cannot be specific to product and system_ext at the same time.")
1684 ctx.PropertyErrorf("system_ext_specific", msg)
Dario Frenifd05a742018-05-29 13:28:54 +01001685 }
1686
Justin Yund5f6c822019-06-25 16:47:17 +09001687 if (socSpecific || deviceSpecific) && (productSpecific || systemExtSpecific) {
Dario Frenifd05a742018-05-29 13:28:54 +01001688 if productSpecific {
1689 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
1690 } else {
Justin Yund5f6c822019-06-25 16:47:17 +09001691 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 +01001692 }
1693 if deviceSpecific {
1694 ctx.PropertyErrorf("device_specific", msg)
1695 } else {
Colin Cross4157e882019-06-06 16:57:04 -07001696 if Bool(m.commonProperties.Vendor) {
Dario Frenifd05a742018-05-29 13:28:54 +01001697 ctx.PropertyErrorf("vendor", msg)
1698 }
Colin Cross4157e882019-06-06 16:57:04 -07001699 if Bool(m.commonProperties.Proprietary) {
Dario Frenifd05a742018-05-29 13:28:54 +01001700 ctx.PropertyErrorf("proprietary", msg)
1701 }
Colin Cross4157e882019-06-06 16:57:04 -07001702 if Bool(m.commonProperties.Soc_specific) {
Dario Frenifd05a742018-05-29 13:28:54 +01001703 ctx.PropertyErrorf("soc_specific", msg)
1704 }
1705 }
1706 }
1707
Jiyong Park2db76922017-11-08 16:03:48 +09001708 if productSpecific {
1709 return productSpecificModule
Justin Yund5f6c822019-06-25 16:47:17 +09001710 } else if systemExtSpecific {
1711 return systemExtSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +09001712 } else if deviceSpecific {
1713 return deviceSpecificModule
1714 } else if socSpecific {
1715 return socSpecificModule
1716 } else {
1717 return platformModule
1718 }
1719}
1720
Colin Crossc34d2322020-01-03 15:23:27 -08001721func (m *ModuleBase) earlyModuleContextFactory(ctx blueprint.EarlyModuleContext) earlyModuleContext {
Colin Cross1184b642019-12-30 18:43:07 -08001722 return earlyModuleContext{
Colin Crossc34d2322020-01-03 15:23:27 -08001723 EarlyModuleContext: ctx,
1724 kind: determineModuleKind(m, ctx),
1725 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -08001726 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001727}
1728
Colin Cross1184b642019-12-30 18:43:07 -08001729func (m *ModuleBase) baseModuleContextFactory(ctx blueprint.BaseModuleContext) baseModuleContext {
1730 return baseModuleContext{
1731 bp: ctx,
1732 earlyModuleContext: m.earlyModuleContextFactory(ctx),
1733 os: m.commonProperties.CompileOS,
1734 target: m.commonProperties.CompileTarget,
1735 targetPrimary: m.commonProperties.CompilePrimary,
1736 multiTargets: m.commonProperties.CompileMultiTargets,
1737 }
1738}
1739
Colin Cross4157e882019-06-06 16:57:04 -07001740func (m *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
Colin Cross25de6c32019-06-06 14:29:25 -07001741 ctx := &moduleContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -07001742 module: m.module,
Colin Crossdc35e212019-06-06 16:13:11 -07001743 bp: blueprintCtx,
Colin Cross0ea8ba82019-06-06 14:33:29 -07001744 baseModuleContext: m.baseModuleContextFactory(blueprintCtx),
Colin Cross0ea8ba82019-06-06 14:33:29 -07001745 variables: make(map[string]string),
Colin Cross3f40fa42015-01-30 17:27:36 -08001746 }
1747
Colin Crossffe6b9d2020-12-01 15:40:06 -08001748 dependencyInstallFiles, dependencyPackagingSpecs := m.computeInstallDeps(ctx)
Colin Cross5d583952020-11-24 16:21:24 -08001749 // set m.installFilesDepSet to only the transitive dependencies to be used as the dependencies
1750 // of installed files of this module. It will be replaced by a depset including the installed
1751 // files of this module at the end for use by modules that depend on this one.
1752 m.installFilesDepSet = newInstallPathsDepSet(nil, dependencyInstallFiles)
1753
Colin Cross6c4f21f2019-06-06 15:41:36 -07001754 // Temporarily continue to call blueprintCtx.GetMissingDependencies() to maintain the previous behavior of never
1755 // reporting missing dependency errors in Blueprint when AllowMissingDependencies == true.
1756 // TODO: This will be removed once defaults modules handle missing dependency errors
1757 blueprintCtx.GetMissingDependencies()
1758
Colin Crossdc35e212019-06-06 16:13:11 -07001759 // For the final GenerateAndroidBuildActions pass, require that all visited dependencies Soong modules and
Paul Duffin1356d8c2020-02-25 19:26:33 +00001760 // are enabled. Unless the module is a CommonOS variant which may have dependencies on disabled variants
1761 // (because the dependencies are added before the modules are disabled). The
1762 // GetOsSpecificVariantsOfCommonOSVariant(...) method will ensure that the disabled variants are
1763 // ignored.
1764 ctx.baseModuleContext.strictVisitDeps = !m.IsCommonOSVariant()
Colin Crossdc35e212019-06-06 16:13:11 -07001765
Colin Cross4c83e5c2019-02-25 14:54:28 -08001766 if ctx.config.captureBuild {
1767 ctx.ruleParams = make(map[blueprint.Rule]blueprint.RuleParams)
1768 }
1769
Colin Cross67a5c132017-05-09 13:45:28 -07001770 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
1771 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -08001772 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
1773 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -07001774 }
Colin Cross0875c522017-11-28 17:34:01 -08001775 if !ctx.PrimaryArch() {
1776 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -07001777 }
Colin Cross56a83212020-09-15 18:30:11 -07001778 if apexInfo := ctx.Provider(ApexInfoProvider).(ApexInfo); !apexInfo.IsForPlatform() {
1779 suffix = append(suffix, apexInfo.ApexVariationName)
Dan Willemsenb13a9482020-02-14 11:25:54 -08001780 }
Colin Cross67a5c132017-05-09 13:45:28 -07001781
1782 ctx.Variable(pctx, "moduleDesc", desc)
1783
1784 s := ""
1785 if len(suffix) > 0 {
1786 s = " [" + strings.Join(suffix, " ") + "]"
1787 }
1788 ctx.Variable(pctx, "moduleDescSuffix", s)
1789
Dan Willemsen569edc52018-11-19 09:33:29 -08001790 // Some common property checks for properties that will be used later in androidmk.go
Paul Duffin89968e32020-11-23 18:17:03 +00001791 checkDistProperties(ctx, "dist", &m.distProperties.Dist)
1792 for i, _ := range m.distProperties.Dists {
1793 checkDistProperties(ctx, fmt.Sprintf("dists[%d]", i), &m.distProperties.Dists[i])
Dan Willemsen569edc52018-11-19 09:33:29 -08001794 }
1795
Colin Cross4157e882019-06-06 16:57:04 -07001796 if m.Enabled() {
Jooyung Hand48f3c32019-08-23 11:18:57 +09001797 // ensure all direct android.Module deps are enabled
1798 ctx.VisitDirectDepsBlueprint(func(bm blueprint.Module) {
1799 if _, ok := bm.(Module); ok {
1800 ctx.validateAndroidModule(bm, ctx.baseModuleContext.strictVisitDeps)
1801 }
1802 })
1803
Bob Badoura75b0572020-02-18 20:21:55 -08001804 m.noticeFiles = make([]Path, 0)
1805 optPath := OptionalPath{}
1806 notice := proptools.StringDefault(m.commonProperties.Notice, "")
Colin Cross4157e882019-06-06 16:57:04 -07001807 if module := SrcIsModule(notice); module != "" {
Bob Badoura75b0572020-02-18 20:21:55 -08001808 optPath = ctx.ExpandOptionalSource(&notice, "notice")
1809 } else if notice != "" {
Jiyong Park52818fc2019-03-18 12:01:38 +09001810 noticePath := filepath.Join(ctx.ModuleDir(), notice)
Bob Badoura75b0572020-02-18 20:21:55 -08001811 optPath = ExistentPathForSource(ctx, noticePath)
1812 }
1813 if optPath.Valid() {
1814 m.noticeFiles = append(m.noticeFiles, optPath.Path())
1815 } else {
1816 for _, notice = range []string{"LICENSE", "LICENCE", "NOTICE"} {
1817 noticePath := filepath.Join(ctx.ModuleDir(), notice)
1818 optPath = ExistentPathForSource(ctx, noticePath)
1819 if optPath.Valid() {
1820 m.noticeFiles = append(m.noticeFiles, optPath.Path())
1821 }
1822 }
Jaewoong Jung62707f72018-11-16 13:26:43 -08001823 }
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001824
Bob Badour37af0462021-01-07 03:34:31 +00001825 licensesPropertyFlattener(ctx)
1826 if ctx.Failed() {
1827 return
1828 }
1829
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001830 m.module.GenerateAndroidBuildActions(ctx)
1831 if ctx.Failed() {
1832 return
1833 }
1834
Jiyong Park4d861072021-03-03 20:02:42 +09001835 m.initRcPaths = PathsForModuleSrc(ctx, m.commonProperties.Init_rc)
1836 rcDir := PathForModuleInstall(ctx, "etc", "init")
1837 for _, src := range m.initRcPaths {
1838 ctx.PackageFile(rcDir, filepath.Base(src.String()), src)
1839 }
1840
1841 m.vintfFragmentsPaths = PathsForModuleSrc(ctx, m.commonProperties.Vintf_fragments)
1842 vintfDir := PathForModuleInstall(ctx, "etc", "vintf", "manifest")
1843 for _, src := range m.vintfFragmentsPaths {
1844 ctx.PackageFile(vintfDir, filepath.Base(src.String()), src)
1845 }
1846
Paul Duffinaf970a22020-11-23 23:32:56 +00001847 // Create the set of tagged dist files after calling GenerateAndroidBuildActions
1848 // as GenerateTaggedDistFiles() calls OutputFiles(tag) and so relies on the
1849 // output paths being set which must be done before or during
1850 // GenerateAndroidBuildActions.
1851 m.distFiles = m.GenerateTaggedDistFiles(ctx)
1852 if ctx.Failed() {
1853 return
1854 }
1855
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001856 m.installFiles = append(m.installFiles, ctx.installFiles...)
1857 m.checkbuildFiles = append(m.checkbuildFiles, ctx.checkbuildFiles...)
Jiyong Park073ea552020-11-09 14:08:34 +09001858 m.packagingSpecs = append(m.packagingSpecs, ctx.packagingSpecs...)
Colin Crossc3d87d32020-06-04 13:25:17 -07001859 for k, v := range ctx.phonies {
1860 m.phonies[k] = append(m.phonies[k], v...)
1861 }
Colin Crossdc35e212019-06-06 16:13:11 -07001862 } else if ctx.Config().AllowMissingDependencies() {
1863 // If the module is not enabled it will not create any build rules, nothing will call
1864 // ctx.GetMissingDependencies(), and blueprint will consider the missing dependencies to be unhandled
1865 // and report them as an error even when AllowMissingDependencies = true. Call
1866 // ctx.GetMissingDependencies() here to tell blueprint not to handle them.
1867 ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001868 }
1869
Colin Cross4157e882019-06-06 16:57:04 -07001870 if m == ctx.FinalModule().(Module).base() {
1871 m.generateModuleTarget(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -07001872 if ctx.Failed() {
1873 return
1874 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001875 }
Colin Crosscec81712017-07-13 14:43:27 -07001876
Colin Cross5d583952020-11-24 16:21:24 -08001877 m.installFilesDepSet = newInstallPathsDepSet(m.installFiles, dependencyInstallFiles)
Colin Crossffe6b9d2020-12-01 15:40:06 -08001878 m.packagingSpecsDepSet = newPackagingSpecsDepSet(m.packagingSpecs, dependencyPackagingSpecs)
Colin Cross5d583952020-11-24 16:21:24 -08001879
Colin Cross4157e882019-06-06 16:57:04 -07001880 m.buildParams = ctx.buildParams
1881 m.ruleParams = ctx.ruleParams
1882 m.variables = ctx.variables
Colin Cross3f40fa42015-01-30 17:27:36 -08001883}
1884
Paul Duffin89968e32020-11-23 18:17:03 +00001885// Check the supplied dist structure to make sure that it is valid.
1886//
1887// property - the base property, e.g. dist or dists[1], which is combined with the
1888// name of the nested property to produce the full property, e.g. dist.dest or
1889// dists[1].dir.
1890func checkDistProperties(ctx *moduleContext, property string, dist *Dist) {
1891 if dist.Dest != nil {
1892 _, err := validateSafePath(*dist.Dest)
1893 if err != nil {
1894 ctx.PropertyErrorf(property+".dest", "%s", err.Error())
1895 }
1896 }
1897 if dist.Dir != nil {
1898 _, err := validateSafePath(*dist.Dir)
1899 if err != nil {
1900 ctx.PropertyErrorf(property+".dir", "%s", err.Error())
1901 }
1902 }
1903 if dist.Suffix != nil {
1904 if strings.Contains(*dist.Suffix, "/") {
1905 ctx.PropertyErrorf(property+".suffix", "Suffix may not contain a '/' character.")
1906 }
1907 }
1908
1909}
1910
Colin Cross1184b642019-12-30 18:43:07 -08001911type earlyModuleContext struct {
Colin Crossc34d2322020-01-03 15:23:27 -08001912 blueprint.EarlyModuleContext
Colin Cross1184b642019-12-30 18:43:07 -08001913
1914 kind moduleKind
1915 config Config
1916}
1917
1918func (e *earlyModuleContext) Glob(globPattern string, excludes []string) Paths {
Liz Kammera830f3a2020-11-10 10:50:34 -08001919 return Glob(e, globPattern, excludes)
Colin Cross1184b642019-12-30 18:43:07 -08001920}
1921
1922func (e *earlyModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Liz Kammera830f3a2020-11-10 10:50:34 -08001923 return GlobFiles(e, globPattern, excludes)
Colin Cross1184b642019-12-30 18:43:07 -08001924}
1925
Colin Cross988414c2020-01-11 01:11:46 +00001926func (b *earlyModuleContext) IsSymlink(path Path) bool {
1927 fileInfo, err := b.config.fs.Lstat(path.String())
1928 if err != nil {
1929 b.ModuleErrorf("os.Lstat(%q) failed: %s", path.String(), err)
1930 }
1931 return fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink
1932}
1933
1934func (b *earlyModuleContext) Readlink(path Path) string {
1935 dest, err := b.config.fs.Readlink(path.String())
1936 if err != nil {
1937 b.ModuleErrorf("os.Readlink(%q) failed: %s", path.String(), err)
1938 }
1939 return dest
1940}
1941
Colin Cross1184b642019-12-30 18:43:07 -08001942func (e *earlyModuleContext) Module() Module {
Colin Crossc34d2322020-01-03 15:23:27 -08001943 module, _ := e.EarlyModuleContext.Module().(Module)
Colin Cross1184b642019-12-30 18:43:07 -08001944 return module
1945}
1946
1947func (e *earlyModuleContext) Config() Config {
Colin Crossc34d2322020-01-03 15:23:27 -08001948 return e.EarlyModuleContext.Config().(Config)
Colin Cross1184b642019-12-30 18:43:07 -08001949}
1950
1951func (e *earlyModuleContext) AConfig() Config {
1952 return e.config
1953}
1954
1955func (e *earlyModuleContext) DeviceConfig() DeviceConfig {
1956 return DeviceConfig{e.config.deviceConfig}
1957}
1958
1959func (e *earlyModuleContext) Platform() bool {
1960 return e.kind == platformModule
1961}
1962
1963func (e *earlyModuleContext) DeviceSpecific() bool {
1964 return e.kind == deviceSpecificModule
1965}
1966
1967func (e *earlyModuleContext) SocSpecific() bool {
1968 return e.kind == socSpecificModule
1969}
1970
1971func (e *earlyModuleContext) ProductSpecific() bool {
1972 return e.kind == productSpecificModule
1973}
1974
1975func (e *earlyModuleContext) SystemExtSpecific() bool {
1976 return e.kind == systemExtSpecificModule
1977}
1978
Colin Cross133ebef2020-08-14 17:38:45 -07001979func (e *earlyModuleContext) Namespace() *Namespace {
1980 return e.EarlyModuleContext.Namespace().(*Namespace)
1981}
1982
Colin Cross1184b642019-12-30 18:43:07 -08001983type baseModuleContext struct {
1984 bp blueprint.BaseModuleContext
1985 earlyModuleContext
Colin Crossfb0c16e2019-11-20 17:12:35 -08001986 os OsType
Colin Cross8b74d172016-09-13 09:59:14 -07001987 target Target
Colin Crossee0bc3b2018-10-02 22:01:37 -07001988 multiTargets []Target
Colin Cross8b74d172016-09-13 09:59:14 -07001989 targetPrimary bool
1990 debug bool
Colin Crossdc35e212019-06-06 16:13:11 -07001991
1992 walkPath []Module
Paul Duffinc5192442020-03-31 11:31:36 +01001993 tagPath []blueprint.DependencyTag
Colin Crossdc35e212019-06-06 16:13:11 -07001994
1995 strictVisitDeps bool // If true, enforce that all dependencies are enabled
Colin Crossf6566ed2015-03-24 11:13:38 -07001996}
1997
Paul Duffinca7f0ef2020-02-25 15:50:49 +00001998func (b *baseModuleContext) OtherModuleName(m blueprint.Module) string {
1999 return b.bp.OtherModuleName(m)
2000}
2001func (b *baseModuleContext) OtherModuleDir(m blueprint.Module) string { return b.bp.OtherModuleDir(m) }
Colin Cross1184b642019-12-30 18:43:07 -08002002func (b *baseModuleContext) OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{}) {
Jooyung Hancd87c692020-02-26 02:05:18 +09002003 b.bp.OtherModuleErrorf(m, fmt, args...)
Colin Cross1184b642019-12-30 18:43:07 -08002004}
2005func (b *baseModuleContext) OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag {
2006 return b.bp.OtherModuleDependencyTag(m)
2007}
Paul Duffinca7f0ef2020-02-25 15:50:49 +00002008func (b *baseModuleContext) OtherModuleExists(name string) bool { return b.bp.OtherModuleExists(name) }
Martin Stjernholm009a9dc2020-03-05 17:34:13 +00002009func (b *baseModuleContext) OtherModuleDependencyVariantExists(variations []blueprint.Variation, name string) bool {
2010 return b.bp.OtherModuleDependencyVariantExists(variations, name)
2011}
2012func (b *baseModuleContext) OtherModuleReverseDependencyVariantExists(name string) bool {
2013 return b.bp.OtherModuleReverseDependencyVariantExists(name)
2014}
Paul Duffinca7f0ef2020-02-25 15:50:49 +00002015func (b *baseModuleContext) OtherModuleType(m blueprint.Module) string {
2016 return b.bp.OtherModuleType(m)
2017}
Colin Crossd27e7b82020-07-02 11:38:17 -07002018func (b *baseModuleContext) OtherModuleProvider(m blueprint.Module, provider blueprint.ProviderKey) interface{} {
2019 return b.bp.OtherModuleProvider(m, provider)
2020}
2021func (b *baseModuleContext) OtherModuleHasProvider(m blueprint.Module, provider blueprint.ProviderKey) bool {
2022 return b.bp.OtherModuleHasProvider(m, provider)
2023}
2024func (b *baseModuleContext) Provider(provider blueprint.ProviderKey) interface{} {
2025 return b.bp.Provider(provider)
2026}
2027func (b *baseModuleContext) HasProvider(provider blueprint.ProviderKey) bool {
2028 return b.bp.HasProvider(provider)
2029}
2030func (b *baseModuleContext) SetProvider(provider blueprint.ProviderKey, value interface{}) {
2031 b.bp.SetProvider(provider, value)
2032}
Colin Cross1184b642019-12-30 18:43:07 -08002033
2034func (b *baseModuleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
2035 return b.bp.GetDirectDepWithTag(name, tag)
2036}
2037
Paul Duffinf88d8e02020-05-07 20:21:34 +01002038func (b *baseModuleContext) blueprintBaseModuleContext() blueprint.BaseModuleContext {
2039 return b.bp
2040}
2041
Colin Cross25de6c32019-06-06 14:29:25 -07002042type moduleContext struct {
Colin Crossdc35e212019-06-06 16:13:11 -07002043 bp blueprint.ModuleContext
Colin Cross0ea8ba82019-06-06 14:33:29 -07002044 baseModuleContext
Jiyong Park073ea552020-11-09 14:08:34 +09002045 packagingSpecs []PackagingSpec
Colin Cross897266e2020-02-13 13:22:08 -08002046 installFiles InstallPaths
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002047 checkbuildFiles Paths
Colin Cross8d8f8e22016-08-03 11:57:50 -07002048 module Module
Colin Crossc3d87d32020-06-04 13:25:17 -07002049 phonies map[string]Paths
Colin Crosscec81712017-07-13 14:43:27 -07002050
2051 // For tests
Colin Crossae887032017-10-23 17:16:14 -07002052 buildParams []BuildParams
Colin Cross4c83e5c2019-02-25 14:54:28 -08002053 ruleParams map[blueprint.Rule]blueprint.RuleParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002054 variables map[string]string
Colin Cross6ff51382015-12-17 16:39:19 -08002055}
2056
Colin Crossb88b3c52019-06-10 15:15:17 -07002057func (m *moduleContext) ninjaError(params BuildParams, err error) (PackageContext, BuildParams) {
2058 return pctx, BuildParams{
Colin Cross4b69c492019-06-07 13:06:06 -07002059 Rule: ErrorRule,
2060 Description: params.Description,
2061 Output: params.Output,
2062 Outputs: params.Outputs,
2063 ImplicitOutput: params.ImplicitOutput,
2064 ImplicitOutputs: params.ImplicitOutputs,
Colin Cross6ff51382015-12-17 16:39:19 -08002065 Args: map[string]string{
2066 "error": err.Error(),
2067 },
Colin Crossb88b3c52019-06-10 15:15:17 -07002068 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002069}
2070
Colin Cross25de6c32019-06-06 14:29:25 -07002071func (m *moduleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
2072 m.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -08002073}
2074
Jingwen Chence679d22020-09-23 04:30:02 +00002075func validateBuildParams(params blueprint.BuildParams) error {
2076 // Validate that the symlink outputs are declared outputs or implicit outputs
2077 allOutputs := map[string]bool{}
2078 for _, output := range params.Outputs {
2079 allOutputs[output] = true
2080 }
2081 for _, output := range params.ImplicitOutputs {
2082 allOutputs[output] = true
2083 }
2084 for _, symlinkOutput := range params.SymlinkOutputs {
2085 if !allOutputs[symlinkOutput] {
2086 return fmt.Errorf(
2087 "Symlink output %s is not a declared output or implicit output",
2088 symlinkOutput)
2089 }
2090 }
2091 return nil
2092}
2093
2094// Convert build parameters from their concrete Android types into their string representations,
2095// and combine the singular and plural fields of the same type (e.g. Output and Outputs).
Colin Cross0875c522017-11-28 17:34:01 -08002096func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002097 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002098 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -08002099 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -08002100 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002101 Outputs: params.Outputs.Strings(),
2102 ImplicitOutputs: params.ImplicitOutputs.Strings(),
Jingwen Chence679d22020-09-23 04:30:02 +00002103 SymlinkOutputs: params.SymlinkOutputs.Strings(),
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002104 Inputs: params.Inputs.Strings(),
2105 Implicits: params.Implicits.Strings(),
2106 OrderOnly: params.OrderOnly.Strings(),
Colin Cross824f1162020-07-16 13:07:51 -07002107 Validations: params.Validations.Strings(),
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002108 Args: params.Args,
2109 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002110 }
2111
Colin Cross33bfb0a2016-11-21 17:23:08 -08002112 if params.Depfile != nil {
2113 bparams.Depfile = params.Depfile.String()
2114 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002115 if params.Output != nil {
2116 bparams.Outputs = append(bparams.Outputs, params.Output.String())
2117 }
Jingwen Chence679d22020-09-23 04:30:02 +00002118 if params.SymlinkOutput != nil {
2119 bparams.SymlinkOutputs = append(bparams.SymlinkOutputs, params.SymlinkOutput.String())
2120 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -07002121 if params.ImplicitOutput != nil {
2122 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
2123 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002124 if params.Input != nil {
2125 bparams.Inputs = append(bparams.Inputs, params.Input.String())
2126 }
2127 if params.Implicit != nil {
2128 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
2129 }
Colin Cross824f1162020-07-16 13:07:51 -07002130 if params.Validation != nil {
2131 bparams.Validations = append(bparams.Validations, params.Validation.String())
2132 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002133
Colin Cross0b9f31f2019-02-28 11:00:01 -08002134 bparams.Outputs = proptools.NinjaEscapeList(bparams.Outputs)
2135 bparams.ImplicitOutputs = proptools.NinjaEscapeList(bparams.ImplicitOutputs)
Jingwen Chence679d22020-09-23 04:30:02 +00002136 bparams.SymlinkOutputs = proptools.NinjaEscapeList(bparams.SymlinkOutputs)
Colin Cross0b9f31f2019-02-28 11:00:01 -08002137 bparams.Inputs = proptools.NinjaEscapeList(bparams.Inputs)
2138 bparams.Implicits = proptools.NinjaEscapeList(bparams.Implicits)
2139 bparams.OrderOnly = proptools.NinjaEscapeList(bparams.OrderOnly)
Colin Cross824f1162020-07-16 13:07:51 -07002140 bparams.Validations = proptools.NinjaEscapeList(bparams.Validations)
2141 bparams.Depfile = proptools.NinjaEscape(bparams.Depfile)
Colin Crossfe4bc362018-09-12 10:02:13 -07002142
Colin Cross0875c522017-11-28 17:34:01 -08002143 return bparams
2144}
2145
Colin Cross25de6c32019-06-06 14:29:25 -07002146func (m *moduleContext) Variable(pctx PackageContext, name, value string) {
2147 if m.config.captureBuild {
2148 m.variables[name] = value
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002149 }
2150
Colin Crossdc35e212019-06-06 16:13:11 -07002151 m.bp.Variable(pctx.PackageContext, name, value)
Colin Cross0875c522017-11-28 17:34:01 -08002152}
2153
Colin Cross25de6c32019-06-06 14:29:25 -07002154func (m *moduleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
Colin Cross0875c522017-11-28 17:34:01 -08002155 argNames ...string) blueprint.Rule {
2156
Ramy Medhat944839a2020-03-31 22:14:52 -04002157 if m.config.UseRemoteBuild() {
2158 if params.Pool == nil {
2159 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
2160 // jobs to the local parallelism value
2161 params.Pool = localPool
2162 } else if params.Pool == remotePool {
2163 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
2164 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
2165 // parallelism.
2166 params.Pool = nil
2167 }
Colin Cross2e2dbc22019-09-25 13:31:46 -07002168 }
2169
Colin Crossdc35e212019-06-06 16:13:11 -07002170 rule := m.bp.Rule(pctx.PackageContext, name, params, argNames...)
Colin Cross4c83e5c2019-02-25 14:54:28 -08002171
Colin Cross25de6c32019-06-06 14:29:25 -07002172 if m.config.captureBuild {
2173 m.ruleParams[rule] = params
Colin Cross4c83e5c2019-02-25 14:54:28 -08002174 }
2175
2176 return rule
Colin Cross0875c522017-11-28 17:34:01 -08002177}
2178
Colin Cross25de6c32019-06-06 14:29:25 -07002179func (m *moduleContext) Build(pctx PackageContext, params BuildParams) {
Colin Crossb88b3c52019-06-10 15:15:17 -07002180 if params.Description != "" {
2181 params.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
2182 }
2183
2184 if missingDeps := m.GetMissingDependencies(); len(missingDeps) > 0 {
2185 pctx, params = m.ninjaError(params, fmt.Errorf("module %s missing dependencies: %s\n",
2186 m.ModuleName(), strings.Join(missingDeps, ", ")))
2187 }
2188
Colin Cross25de6c32019-06-06 14:29:25 -07002189 if m.config.captureBuild {
2190 m.buildParams = append(m.buildParams, params)
Colin Cross0875c522017-11-28 17:34:01 -08002191 }
2192
Jingwen Chence679d22020-09-23 04:30:02 +00002193 bparams := convertBuildParams(params)
2194 err := validateBuildParams(bparams)
2195 if err != nil {
2196 m.ModuleErrorf(
2197 "%s: build parameter validation failed: %s",
2198 m.ModuleName(),
2199 err.Error())
2200 }
2201 m.bp.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002202}
Colin Crossc3d87d32020-06-04 13:25:17 -07002203
2204func (m *moduleContext) Phony(name string, deps ...Path) {
2205 addPhony(m.config, name, deps...)
2206}
2207
Colin Cross25de6c32019-06-06 14:29:25 -07002208func (m *moduleContext) GetMissingDependencies() []string {
Colin Cross6c4f21f2019-06-06 15:41:36 -07002209 var missingDeps []string
2210 missingDeps = append(missingDeps, m.Module().base().commonProperties.MissingDeps...)
Colin Crossdc35e212019-06-06 16:13:11 -07002211 missingDeps = append(missingDeps, m.bp.GetMissingDependencies()...)
Colin Cross6c4f21f2019-06-06 15:41:36 -07002212 missingDeps = FirstUniqueStrings(missingDeps)
2213 return missingDeps
Colin Cross6ff51382015-12-17 16:39:19 -08002214}
2215
Colin Crossdc35e212019-06-06 16:13:11 -07002216func (b *baseModuleContext) AddMissingDependencies(deps []string) {
Dan Willemsen6553f5e2016-03-10 18:14:25 -08002217 if deps != nil {
Colin Crossdc35e212019-06-06 16:13:11 -07002218 missingDeps := &b.Module().base().commonProperties.MissingDeps
Colin Cross6c4f21f2019-06-06 15:41:36 -07002219 *missingDeps = append(*missingDeps, deps...)
2220 *missingDeps = FirstUniqueStrings(*missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -08002221 }
2222}
2223
Colin Crossdc35e212019-06-06 16:13:11 -07002224func (b *baseModuleContext) validateAndroidModule(module blueprint.Module, strict bool) Module {
Colin Crossd11fcda2017-10-23 17:59:01 -07002225 aModule, _ := module.(Module)
Colin Crossdc35e212019-06-06 16:13:11 -07002226
2227 if !strict {
2228 return aModule
2229 }
2230
Colin Cross380c69a2019-06-10 17:49:58 +00002231 if aModule == nil {
Colin Crossdc35e212019-06-06 16:13:11 -07002232 b.ModuleErrorf("module %q not an android module", b.OtherModuleName(module))
Colin Cross380c69a2019-06-10 17:49:58 +00002233 return nil
2234 }
2235
2236 if !aModule.Enabled() {
Colin Crossdc35e212019-06-06 16:13:11 -07002237 if b.Config().AllowMissingDependencies() {
2238 b.AddMissingDependencies([]string{b.OtherModuleName(aModule)})
Colin Cross380c69a2019-06-10 17:49:58 +00002239 } else {
Colin Crossdc35e212019-06-06 16:13:11 -07002240 b.ModuleErrorf("depends on disabled module %q", b.OtherModuleName(aModule))
Colin Cross380c69a2019-06-10 17:49:58 +00002241 }
2242 return nil
2243 }
Colin Crossd11fcda2017-10-23 17:59:01 -07002244 return aModule
2245}
2246
Colin Crossdc35e212019-06-06 16:13:11 -07002247func (b *baseModuleContext) getDirectDepInternal(name string, tag blueprint.DependencyTag) (blueprint.Module, blueprint.DependencyTag) {
Jiyong Parkf2976302019-04-17 21:47:37 +09002248 type dep struct {
2249 mod blueprint.Module
2250 tag blueprint.DependencyTag
2251 }
2252 var deps []dep
Colin Crossdc35e212019-06-06 16:13:11 -07002253 b.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Liz Kammer356f7d42021-01-26 09:18:53 -05002254 if aModule, _ := module.(Module); aModule != nil {
2255 if aModule.base().BaseModuleName() == name {
2256 returnedTag := b.bp.OtherModuleDependencyTag(aModule)
2257 if tag == nil || returnedTag == tag {
2258 deps = append(deps, dep{aModule, returnedTag})
2259 }
2260 }
2261 } else if b.bp.OtherModuleName(module) == name {
2262 returnedTag := b.bp.OtherModuleDependencyTag(module)
Jiyong Parkf2976302019-04-17 21:47:37 +09002263 if tag == nil || returnedTag == tag {
Liz Kammer356f7d42021-01-26 09:18:53 -05002264 deps = append(deps, dep{module, returnedTag})
Jiyong Parkf2976302019-04-17 21:47:37 +09002265 }
2266 }
2267 })
2268 if len(deps) == 1 {
2269 return deps[0].mod, deps[0].tag
2270 } else if len(deps) >= 2 {
2271 panic(fmt.Errorf("Multiple dependencies having same BaseModuleName() %q found from %q",
Colin Crossdc35e212019-06-06 16:13:11 -07002272 name, b.ModuleName()))
Jiyong Parkf2976302019-04-17 21:47:37 +09002273 } else {
2274 return nil, nil
2275 }
2276}
2277
Colin Crossdc35e212019-06-06 16:13:11 -07002278func (b *baseModuleContext) GetDirectDepsWithTag(tag blueprint.DependencyTag) []Module {
Colin Cross0ef08162019-05-01 15:50:51 -07002279 var deps []Module
Colin Crossdc35e212019-06-06 16:13:11 -07002280 b.VisitDirectDepsBlueprint(func(module blueprint.Module) {
Colin Cross25de6c32019-06-06 14:29:25 -07002281 if aModule, _ := module.(Module); aModule != nil {
Colin Cross1184b642019-12-30 18:43:07 -08002282 if b.bp.OtherModuleDependencyTag(aModule) == tag {
Colin Cross0ef08162019-05-01 15:50:51 -07002283 deps = append(deps, aModule)
2284 }
2285 }
2286 })
2287 return deps
2288}
2289
Colin Cross25de6c32019-06-06 14:29:25 -07002290func (m *moduleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
2291 module, _ := m.getDirectDepInternal(name, tag)
2292 return module
Jiyong Parkf2976302019-04-17 21:47:37 +09002293}
2294
Colin Crossdc35e212019-06-06 16:13:11 -07002295func (b *baseModuleContext) GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) {
2296 return b.getDirectDepInternal(name, nil)
Jiyong Parkf2976302019-04-17 21:47:37 +09002297}
2298
Colin Crossdc35e212019-06-06 16:13:11 -07002299func (b *baseModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002300 b.bp.VisitDirectDeps(visit)
Colin Cross35143d02017-11-16 00:11:20 -08002301}
2302
Colin Crossdc35e212019-06-06 16:13:11 -07002303func (b *baseModuleContext) VisitDirectDeps(visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002304 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Crossdc35e212019-06-06 16:13:11 -07002305 if aModule := b.validateAndroidModule(module, b.strictVisitDeps); aModule != nil {
Colin Crossd11fcda2017-10-23 17:59:01 -07002306 visit(aModule)
2307 }
2308 })
2309}
2310
Colin Crossdc35e212019-06-06 16:13:11 -07002311func (b *baseModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002312 b.bp.VisitDirectDeps(func(module blueprint.Module) {
Colin Crossdc35e212019-06-06 16:13:11 -07002313 if aModule := b.validateAndroidModule(module, b.strictVisitDeps); aModule != nil {
Colin Cross1184b642019-12-30 18:43:07 -08002314 if b.bp.OtherModuleDependencyTag(aModule) == tag {
Colin Crossee6143c2017-12-30 17:54:27 -08002315 visit(aModule)
2316 }
2317 }
2318 })
2319}
2320
Colin Crossdc35e212019-06-06 16:13:11 -07002321func (b *baseModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002322 b.bp.VisitDirectDepsIf(
Colin Crossd11fcda2017-10-23 17:59:01 -07002323 // pred
2324 func(module blueprint.Module) bool {
Colin Crossdc35e212019-06-06 16:13:11 -07002325 if aModule := b.validateAndroidModule(module, b.strictVisitDeps); aModule != nil {
Colin Crossd11fcda2017-10-23 17:59:01 -07002326 return pred(aModule)
2327 } else {
2328 return false
2329 }
2330 },
2331 // visit
2332 func(module blueprint.Module) {
2333 visit(module.(Module))
2334 })
2335}
2336
Colin Crossdc35e212019-06-06 16:13:11 -07002337func (b *baseModuleContext) VisitDepsDepthFirst(visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002338 b.bp.VisitDepsDepthFirst(func(module blueprint.Module) {
Colin Crossdc35e212019-06-06 16:13:11 -07002339 if aModule := b.validateAndroidModule(module, b.strictVisitDeps); aModule != nil {
Colin Crossd11fcda2017-10-23 17:59:01 -07002340 visit(aModule)
2341 }
2342 })
2343}
2344
Colin Crossdc35e212019-06-06 16:13:11 -07002345func (b *baseModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
Colin Cross1184b642019-12-30 18:43:07 -08002346 b.bp.VisitDepsDepthFirstIf(
Colin Crossd11fcda2017-10-23 17:59:01 -07002347 // pred
2348 func(module blueprint.Module) bool {
Colin Crossdc35e212019-06-06 16:13:11 -07002349 if aModule := b.validateAndroidModule(module, b.strictVisitDeps); aModule != nil {
Colin Crossd11fcda2017-10-23 17:59:01 -07002350 return pred(aModule)
2351 } else {
2352 return false
2353 }
2354 },
2355 // visit
2356 func(module blueprint.Module) {
2357 visit(module.(Module))
2358 })
2359}
2360
Colin Crossdc35e212019-06-06 16:13:11 -07002361func (b *baseModuleContext) WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool) {
Colin Cross1184b642019-12-30 18:43:07 -08002362 b.bp.WalkDeps(visit)
Alex Light778127a2019-02-27 14:19:50 -08002363}
2364
Colin Crossdc35e212019-06-06 16:13:11 -07002365func (b *baseModuleContext) WalkDeps(visit func(Module, Module) bool) {
2366 b.walkPath = []Module{b.Module()}
Paul Duffinc5192442020-03-31 11:31:36 +01002367 b.tagPath = []blueprint.DependencyTag{}
Colin Cross1184b642019-12-30 18:43:07 -08002368 b.bp.WalkDeps(func(child, parent blueprint.Module) bool {
Colin Crossdc35e212019-06-06 16:13:11 -07002369 childAndroidModule, _ := child.(Module)
2370 parentAndroidModule, _ := parent.(Module)
Colin Crossd11fcda2017-10-23 17:59:01 -07002371 if childAndroidModule != nil && parentAndroidModule != nil {
Colin Crossdc35e212019-06-06 16:13:11 -07002372 // record walkPath before visit
2373 for b.walkPath[len(b.walkPath)-1] != parentAndroidModule {
2374 b.walkPath = b.walkPath[0 : len(b.walkPath)-1]
Paul Duffinc5192442020-03-31 11:31:36 +01002375 b.tagPath = b.tagPath[0 : len(b.tagPath)-1]
Colin Crossdc35e212019-06-06 16:13:11 -07002376 }
2377 b.walkPath = append(b.walkPath, childAndroidModule)
Paul Duffinc5192442020-03-31 11:31:36 +01002378 b.tagPath = append(b.tagPath, b.OtherModuleDependencyTag(childAndroidModule))
Colin Crossd11fcda2017-10-23 17:59:01 -07002379 return visit(childAndroidModule, parentAndroidModule)
2380 } else {
2381 return false
2382 }
2383 })
2384}
2385
Colin Crossdc35e212019-06-06 16:13:11 -07002386func (b *baseModuleContext) GetWalkPath() []Module {
2387 return b.walkPath
2388}
2389
Paul Duffinc5192442020-03-31 11:31:36 +01002390func (b *baseModuleContext) GetTagPath() []blueprint.DependencyTag {
2391 return b.tagPath
2392}
2393
Colin Cross4dfacf92020-09-16 19:22:27 -07002394func (b *baseModuleContext) VisitAllModuleVariants(visit func(Module)) {
2395 b.bp.VisitAllModuleVariants(func(module blueprint.Module) {
2396 visit(module.(Module))
2397 })
2398}
2399
2400func (b *baseModuleContext) PrimaryModule() Module {
2401 return b.bp.PrimaryModule().(Module)
2402}
2403
2404func (b *baseModuleContext) FinalModule() Module {
2405 return b.bp.FinalModule().(Module)
2406}
2407
Bob Badour07065cd2021-02-05 19:59:11 -08002408// IsMetaDependencyTag returns true for cross-cutting metadata dependencies.
2409func IsMetaDependencyTag(tag blueprint.DependencyTag) bool {
2410 if tag == licenseKindTag {
2411 return true
2412 } else if tag == licensesTag {
2413 return true
2414 }
2415 return false
2416}
2417
Jiyong Park1c7e9622020-05-07 16:12:13 +09002418// A regexp for removing boilerplate from BaseDependencyTag from the string representation of
2419// a dependency tag.
Colin Cross6e511a92020-07-27 21:26:48 -07002420var tagCleaner = regexp.MustCompile(`\QBaseDependencyTag:{}\E(, )?`)
Jiyong Park1c7e9622020-05-07 16:12:13 +09002421
2422// PrettyPrintTag returns string representation of the tag, but prefers
2423// custom String() method if available.
2424func PrettyPrintTag(tag blueprint.DependencyTag) string {
2425 // Use tag's custom String() method if available.
2426 if stringer, ok := tag.(fmt.Stringer); ok {
2427 return stringer.String()
2428 }
2429
2430 // Otherwise, get a default string representation of the tag's struct.
Colin Cross6e511a92020-07-27 21:26:48 -07002431 tagString := fmt.Sprintf("%T: %+v", tag, tag)
Jiyong Park1c7e9622020-05-07 16:12:13 +09002432
2433 // Remove the boilerplate from BaseDependencyTag as it adds no value.
2434 tagString = tagCleaner.ReplaceAllString(tagString, "")
2435 return tagString
2436}
2437
2438func (b *baseModuleContext) GetPathString(skipFirst bool) string {
2439 sb := strings.Builder{}
2440 tagPath := b.GetTagPath()
2441 walkPath := b.GetWalkPath()
2442 if !skipFirst {
2443 sb.WriteString(walkPath[0].String())
2444 }
2445 for i, m := range walkPath[1:] {
2446 sb.WriteString("\n")
2447 sb.WriteString(fmt.Sprintf(" via tag %s\n", PrettyPrintTag(tagPath[i])))
2448 sb.WriteString(fmt.Sprintf(" -> %s", m.String()))
2449 }
2450 return sb.String()
2451}
2452
Colin Crossdc35e212019-06-06 16:13:11 -07002453func (m *moduleContext) ModuleSubDir() string {
2454 return m.bp.ModuleSubDir()
Colin Cross0875c522017-11-28 17:34:01 -08002455}
2456
Colin Cross0ea8ba82019-06-06 14:33:29 -07002457func (b *baseModuleContext) Target() Target {
Colin Cross25de6c32019-06-06 14:29:25 -07002458 return b.target
Colin Crossa1ad8d12016-06-01 17:09:44 -07002459}
2460
Colin Cross0ea8ba82019-06-06 14:33:29 -07002461func (b *baseModuleContext) TargetPrimary() bool {
Colin Cross25de6c32019-06-06 14:29:25 -07002462 return b.targetPrimary
Colin Cross8b74d172016-09-13 09:59:14 -07002463}
2464
Colin Cross0ea8ba82019-06-06 14:33:29 -07002465func (b *baseModuleContext) MultiTargets() []Target {
Colin Cross25de6c32019-06-06 14:29:25 -07002466 return b.multiTargets
Colin Crossee0bc3b2018-10-02 22:01:37 -07002467}
2468
Colin Cross0ea8ba82019-06-06 14:33:29 -07002469func (b *baseModuleContext) Arch() Arch {
Colin Cross25de6c32019-06-06 14:29:25 -07002470 return b.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -08002471}
2472
Colin Cross0ea8ba82019-06-06 14:33:29 -07002473func (b *baseModuleContext) Os() OsType {
Colin Crossfb0c16e2019-11-20 17:12:35 -08002474 return b.os
Dan Willemsen490fd492015-11-24 17:53:15 -08002475}
2476
Colin Cross0ea8ba82019-06-06 14:33:29 -07002477func (b *baseModuleContext) Host() bool {
Jiyong Park1613e552020-09-14 19:43:17 +09002478 return b.os.Class == Host
Colin Crossf6566ed2015-03-24 11:13:38 -07002479}
2480
Colin Cross0ea8ba82019-06-06 14:33:29 -07002481func (b *baseModuleContext) Device() bool {
Colin Crossfb0c16e2019-11-20 17:12:35 -08002482 return b.os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -07002483}
2484
Colin Cross0ea8ba82019-06-06 14:33:29 -07002485func (b *baseModuleContext) Darwin() bool {
Colin Crossfb0c16e2019-11-20 17:12:35 -08002486 return b.os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -07002487}
2488
Colin Cross0ea8ba82019-06-06 14:33:29 -07002489func (b *baseModuleContext) Fuchsia() bool {
Colin Crossfb0c16e2019-11-20 17:12:35 -08002490 return b.os == Fuchsia
Doug Horn21b94272019-01-16 12:06:11 -08002491}
2492
Colin Cross0ea8ba82019-06-06 14:33:29 -07002493func (b *baseModuleContext) Windows() bool {
Colin Crossfb0c16e2019-11-20 17:12:35 -08002494 return b.os == Windows
Colin Cross3edeee12017-04-04 12:59:48 -07002495}
2496
Colin Cross0ea8ba82019-06-06 14:33:29 -07002497func (b *baseModuleContext) Debug() bool {
Colin Cross25de6c32019-06-06 14:29:25 -07002498 return b.debug
Colin Crossf6566ed2015-03-24 11:13:38 -07002499}
2500
Colin Cross0ea8ba82019-06-06 14:33:29 -07002501func (b *baseModuleContext) PrimaryArch() bool {
Colin Cross25de6c32019-06-06 14:29:25 -07002502 if len(b.config.Targets[b.target.Os]) <= 1 {
Colin Cross67a5c132017-05-09 13:45:28 -07002503 return true
2504 }
Colin Cross25de6c32019-06-06 14:29:25 -07002505 return b.target.Arch.ArchType == b.config.Targets[b.target.Os][0].Arch.ArchType
Colin Cross1e7d3702016-08-24 15:25:47 -07002506}
2507
Jiyong Park5baac542018-08-28 09:55:37 +09002508// Makes this module a platform module, i.e. not specific to soc, device,
Justin Yund5f6c822019-06-25 16:47:17 +09002509// product, or system_ext.
Colin Cross4157e882019-06-06 16:57:04 -07002510func (m *ModuleBase) MakeAsPlatform() {
2511 m.commonProperties.Vendor = boolPtr(false)
2512 m.commonProperties.Proprietary = boolPtr(false)
2513 m.commonProperties.Soc_specific = boolPtr(false)
2514 m.commonProperties.Product_specific = boolPtr(false)
Justin Yund5f6c822019-06-25 16:47:17 +09002515 m.commonProperties.System_ext_specific = boolPtr(false)
Jiyong Park5baac542018-08-28 09:55:37 +09002516}
2517
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09002518func (m *ModuleBase) MakeAsSystemExt() {
Jooyung Han91df2082019-11-20 01:49:42 +09002519 m.commonProperties.Vendor = boolPtr(false)
2520 m.commonProperties.Proprietary = boolPtr(false)
2521 m.commonProperties.Soc_specific = boolPtr(false)
2522 m.commonProperties.Product_specific = boolPtr(false)
2523 m.commonProperties.System_ext_specific = boolPtr(true)
Sundong Ahnd95aa2d2019-10-08 19:34:03 +09002524}
2525
Jooyung Han344d5432019-08-23 11:17:39 +09002526// IsNativeBridgeSupported returns true if "native_bridge_supported" is explicitly set as "true"
2527func (m *ModuleBase) IsNativeBridgeSupported() bool {
2528 return proptools.Bool(m.commonProperties.Native_bridge_supported)
2529}
2530
Colin Cross25de6c32019-06-06 14:29:25 -07002531func (m *moduleContext) InstallInData() bool {
2532 return m.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08002533}
2534
Jaewoong Jung0949f312019-09-11 10:25:18 -07002535func (m *moduleContext) InstallInTestcases() bool {
2536 return m.module.InstallInTestcases()
2537}
2538
Colin Cross25de6c32019-06-06 14:29:25 -07002539func (m *moduleContext) InstallInSanitizerDir() bool {
2540 return m.module.InstallInSanitizerDir()
Vishwath Mohan1dd88392017-03-29 22:00:18 -07002541}
2542
Yifan Hong1b3348d2020-01-21 15:53:22 -08002543func (m *moduleContext) InstallInRamdisk() bool {
2544 return m.module.InstallInRamdisk()
2545}
2546
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002547func (m *moduleContext) InstallInVendorRamdisk() bool {
2548 return m.module.InstallInVendorRamdisk()
2549}
2550
Colin Cross25de6c32019-06-06 14:29:25 -07002551func (m *moduleContext) InstallInRecovery() bool {
2552 return m.module.InstallInRecovery()
Jiyong Parkf9332f12018-02-01 00:54:12 +09002553}
2554
Colin Cross90ba5f42019-10-02 11:10:58 -07002555func (m *moduleContext) InstallInRoot() bool {
2556 return m.module.InstallInRoot()
2557}
2558
Colin Cross607d8582019-07-29 16:44:46 -07002559func (m *moduleContext) InstallBypassMake() bool {
2560 return m.module.InstallBypassMake()
2561}
2562
Jiyong Park87788b52020-09-01 12:37:45 +09002563func (m *moduleContext) InstallForceOS() (*OsType, *ArchType) {
Colin Cross6e359402020-02-10 15:29:54 -08002564 return m.module.InstallForceOS()
2565}
2566
Colin Crossa9c8c9f2020-12-16 10:20:23 -08002567func (m *moduleContext) skipInstall() bool {
Colin Cross25de6c32019-06-06 14:29:25 -07002568 if m.module.base().commonProperties.SkipInstall {
Colin Cross893d8162017-04-26 17:34:03 -07002569 return true
2570 }
2571
Colin Crossa9c8c9f2020-12-16 10:20:23 -08002572 if m.module.base().commonProperties.HideFromMake {
2573 return true
2574 }
2575
Colin Cross3607f212018-05-07 15:28:05 -07002576 // We'll need a solution for choosing which of modules with the same name in different
2577 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
2578 // list of namespaces to install in a Soong-only build.
Colin Cross25de6c32019-06-06 14:29:25 -07002579 if !m.module.base().commonProperties.NamespaceExportedToMake {
Colin Cross3607f212018-05-07 15:28:05 -07002580 return true
2581 }
2582
Colin Cross25de6c32019-06-06 14:29:25 -07002583 if m.Device() {
Jingwen Chencda22c92020-11-23 00:22:30 -05002584 if m.Config().KatiEnabled() && !m.InstallBypassMake() {
Colin Cross893d8162017-04-26 17:34:03 -07002585 return true
2586 }
Colin Cross893d8162017-04-26 17:34:03 -07002587 }
2588
2589 return false
2590}
2591
Colin Cross70dda7e2019-10-01 22:05:35 -07002592func (m *moduleContext) InstallFile(installPath InstallPath, name string, srcPath Path,
2593 deps ...Path) InstallPath {
Jiyong Park073ea552020-11-09 14:08:34 +09002594 return m.installFile(installPath, name, srcPath, deps, false)
Colin Cross5c517922017-08-31 12:29:17 -07002595}
2596
Colin Cross70dda7e2019-10-01 22:05:35 -07002597func (m *moduleContext) InstallExecutable(installPath InstallPath, name string, srcPath Path,
2598 deps ...Path) InstallPath {
Jiyong Park073ea552020-11-09 14:08:34 +09002599 return m.installFile(installPath, name, srcPath, deps, true)
Colin Cross5c517922017-08-31 12:29:17 -07002600}
2601
Colin Cross41589502020-12-01 14:00:21 -08002602func (m *moduleContext) PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec {
2603 fullInstallPath := installPath.Join(m, name)
2604 return m.packageFile(fullInstallPath, srcPath, false)
2605}
2606
2607func (m *moduleContext) packageFile(fullInstallPath InstallPath, srcPath Path, executable bool) PackagingSpec {
2608 spec := PackagingSpec{
2609 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
2610 srcPath: srcPath,
2611 symlinkTarget: "",
2612 executable: executable,
2613 }
2614 m.packagingSpecs = append(m.packagingSpecs, spec)
2615 return spec
2616}
2617
Jiyong Park073ea552020-11-09 14:08:34 +09002618func (m *moduleContext) installFile(installPath InstallPath, name string, srcPath Path, deps []Path, executable bool) InstallPath {
Colin Cross35cec122015-04-02 14:37:16 -07002619
Colin Cross25de6c32019-06-06 14:29:25 -07002620 fullInstallPath := installPath.Join(m, name)
David Srbecky07656412020-06-04 01:26:16 +01002621 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002622
Colin Crossa9c8c9f2020-12-16 10:20:23 -08002623 if !m.skipInstall() {
Colin Cross5d583952020-11-24 16:21:24 -08002624 deps = append(deps, m.module.base().installFilesDepSet.ToList().Paths()...)
Colin Cross35cec122015-04-02 14:37:16 -07002625
Colin Cross89562dc2016-10-03 17:47:19 -07002626 var implicitDeps, orderOnlyDeps Paths
2627
Colin Cross25de6c32019-06-06 14:29:25 -07002628 if m.Host() {
Colin Cross89562dc2016-10-03 17:47:19 -07002629 // Installed host modules might be used during the build, depend directly on their
2630 // dependencies so their timestamp is updated whenever their dependency is updated
2631 implicitDeps = deps
2632 } else {
2633 orderOnlyDeps = deps
2634 }
2635
Jiyong Park073ea552020-11-09 14:08:34 +09002636 rule := Cp
2637 if executable {
2638 rule = CpExecutable
2639 }
2640
Colin Cross25de6c32019-06-06 14:29:25 -07002641 m.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07002642 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07002643 Description: "install " + fullInstallPath.Base(),
2644 Output: fullInstallPath,
2645 Input: srcPath,
2646 Implicits: implicitDeps,
2647 OrderOnly: orderOnlyDeps,
Jingwen Chencda22c92020-11-23 00:22:30 -05002648 Default: !m.Config().KatiEnabled(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08002649 })
Colin Cross3f40fa42015-01-30 17:27:36 -08002650
Colin Cross25de6c32019-06-06 14:29:25 -07002651 m.installFiles = append(m.installFiles, fullInstallPath)
Dan Willemsen322acaf2016-01-12 23:07:05 -08002652 }
Jiyong Park073ea552020-11-09 14:08:34 +09002653
Colin Cross41589502020-12-01 14:00:21 -08002654 m.packageFile(fullInstallPath, srcPath, executable)
Jiyong Park073ea552020-11-09 14:08:34 +09002655
Colin Cross25de6c32019-06-06 14:29:25 -07002656 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08002657
Colin Cross35cec122015-04-02 14:37:16 -07002658 return fullInstallPath
2659}
2660
Colin Cross70dda7e2019-10-01 22:05:35 -07002661func (m *moduleContext) InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath {
Colin Cross25de6c32019-06-06 14:29:25 -07002662 fullInstallPath := installPath.Join(m, name)
David Srbecky07656412020-06-04 01:26:16 +01002663 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08002664
Jiyong Park073ea552020-11-09 14:08:34 +09002665 relPath, err := filepath.Rel(path.Dir(fullInstallPath.String()), srcPath.String())
2666 if err != nil {
2667 panic(fmt.Sprintf("Unable to generate symlink between %q and %q: %s", fullInstallPath.Base(), srcPath.Base(), err))
2668 }
Colin Crossa9c8c9f2020-12-16 10:20:23 -08002669 if !m.skipInstall() {
Colin Crossce75d2c2016-10-06 16:12:58 -07002670
Colin Cross25de6c32019-06-06 14:29:25 -07002671 m.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07002672 Rule: Symlink,
2673 Description: "install symlink " + fullInstallPath.Base(),
2674 Output: fullInstallPath,
Dan Willemsen40efa1c2020-01-14 15:19:52 -08002675 Input: srcPath,
Jingwen Chencda22c92020-11-23 00:22:30 -05002676 Default: !m.Config().KatiEnabled(),
Colin Cross12fc4972016-01-11 12:49:11 -08002677 Args: map[string]string{
Alex Lightfb4353d2019-01-17 13:57:45 -08002678 "fromPath": relPath,
Colin Cross12fc4972016-01-11 12:49:11 -08002679 },
2680 })
Colin Cross3854a602016-01-11 12:49:11 -08002681
Colin Cross25de6c32019-06-06 14:29:25 -07002682 m.installFiles = append(m.installFiles, fullInstallPath)
2683 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
Colin Cross12fc4972016-01-11 12:49:11 -08002684 }
Jiyong Park073ea552020-11-09 14:08:34 +09002685
2686 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
2687 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
2688 srcPath: nil,
2689 symlinkTarget: relPath,
2690 executable: false,
2691 })
2692
Colin Cross3854a602016-01-11 12:49:11 -08002693 return fullInstallPath
2694}
2695
Jiyong Parkf1194352019-02-25 11:05:47 +09002696// installPath/name -> absPath where absPath might be a path that is available only at runtime
2697// (e.g. /apex/...)
Colin Cross70dda7e2019-10-01 22:05:35 -07002698func (m *moduleContext) InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath {
Colin Cross25de6c32019-06-06 14:29:25 -07002699 fullInstallPath := installPath.Join(m, name)
David Srbecky07656412020-06-04 01:26:16 +01002700 m.module.base().hooks.runInstallHooks(m, nil, fullInstallPath, true)
Jiyong Parkf1194352019-02-25 11:05:47 +09002701
Colin Crossa9c8c9f2020-12-16 10:20:23 -08002702 if !m.skipInstall() {
Colin Cross25de6c32019-06-06 14:29:25 -07002703 m.Build(pctx, BuildParams{
Jiyong Parkf1194352019-02-25 11:05:47 +09002704 Rule: Symlink,
2705 Description: "install symlink " + fullInstallPath.Base() + " -> " + absPath,
2706 Output: fullInstallPath,
Jingwen Chencda22c92020-11-23 00:22:30 -05002707 Default: !m.Config().KatiEnabled(),
Jiyong Parkf1194352019-02-25 11:05:47 +09002708 Args: map[string]string{
2709 "fromPath": absPath,
2710 },
2711 })
2712
Colin Cross25de6c32019-06-06 14:29:25 -07002713 m.installFiles = append(m.installFiles, fullInstallPath)
Jiyong Parkf1194352019-02-25 11:05:47 +09002714 }
Jiyong Park073ea552020-11-09 14:08:34 +09002715
2716 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
2717 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
2718 srcPath: nil,
2719 symlinkTarget: absPath,
2720 executable: false,
2721 })
2722
Jiyong Parkf1194352019-02-25 11:05:47 +09002723 return fullInstallPath
2724}
2725
Colin Cross25de6c32019-06-06 14:29:25 -07002726func (m *moduleContext) CheckbuildFile(srcPath Path) {
2727 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
Colin Cross3f40fa42015-01-30 17:27:36 -08002728}
2729
Colin Crossc20dc852020-11-10 12:27:45 -08002730func (m *moduleContext) blueprintModuleContext() blueprint.ModuleContext {
2731 return m.bp
2732}
2733
Colin Cross41955e82019-05-29 14:40:35 -07002734// SrcIsModule decodes module references in the format ":name" into the module name, or empty string if the input
2735// was not a module reference.
2736func SrcIsModule(s string) (module string) {
Colin Cross068e0fe2016-12-13 15:23:47 -08002737 if len(s) > 1 && s[0] == ':' {
2738 return s[1:]
2739 }
2740 return ""
2741}
2742
Colin Cross41955e82019-05-29 14:40:35 -07002743// SrcIsModule decodes module references in the format ":name{.tag}" into the module name and tag, ":name" into the
2744// module name and an empty string for the tag, or empty strings if the input was not a module reference.
2745func SrcIsModuleWithTag(s string) (module, tag string) {
2746 if len(s) > 1 && s[0] == ':' {
2747 module = s[1:]
2748 if tagStart := strings.IndexByte(module, '{'); tagStart > 0 {
2749 if module[len(module)-1] == '}' {
2750 tag = module[tagStart+1 : len(module)-1]
2751 module = module[:tagStart]
2752 return module, tag
2753 }
2754 }
2755 return module, ""
2756 }
2757 return "", ""
Colin Cross068e0fe2016-12-13 15:23:47 -08002758}
2759
Colin Cross41955e82019-05-29 14:40:35 -07002760type sourceOrOutputDependencyTag struct {
2761 blueprint.BaseDependencyTag
2762 tag string
2763}
2764
2765func sourceOrOutputDepTag(tag string) blueprint.DependencyTag {
2766 return sourceOrOutputDependencyTag{tag: tag}
2767}
2768
2769var SourceDepTag = sourceOrOutputDepTag("")
Colin Cross068e0fe2016-12-13 15:23:47 -08002770
Colin Cross366938f2017-12-11 16:29:02 -08002771// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
2772// using ":module" syntax, if any.
Colin Cross27b922f2019-03-04 22:35:41 -08002773//
2774// Deprecated: tag the property with `android:"path"` instead.
Colin Cross068e0fe2016-12-13 15:23:47 -08002775func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
Nan Zhang2439eb72017-04-10 11:27:50 -07002776 set := make(map[string]bool)
2777
Colin Cross068e0fe2016-12-13 15:23:47 -08002778 for _, s := range srcFiles {
Colin Cross41955e82019-05-29 14:40:35 -07002779 if m, t := SrcIsModuleWithTag(s); m != "" {
2780 if _, found := set[s]; found {
2781 ctx.ModuleErrorf("found source dependency duplicate: %q!", s)
Nan Zhang2439eb72017-04-10 11:27:50 -07002782 } else {
Colin Cross41955e82019-05-29 14:40:35 -07002783 set[s] = true
2784 ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(t), m)
Nan Zhang2439eb72017-04-10 11:27:50 -07002785 }
Colin Cross068e0fe2016-12-13 15:23:47 -08002786 }
2787 }
Colin Cross068e0fe2016-12-13 15:23:47 -08002788}
2789
Colin Cross366938f2017-12-11 16:29:02 -08002790// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
2791// using ":module" syntax, if any.
Colin Cross27b922f2019-03-04 22:35:41 -08002792//
2793// Deprecated: tag the property with `android:"path"` instead.
Colin Cross366938f2017-12-11 16:29:02 -08002794func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
2795 if s != nil {
Colin Cross41955e82019-05-29 14:40:35 -07002796 if m, t := SrcIsModuleWithTag(*s); m != "" {
2797 ctx.AddDependency(ctx.Module(), sourceOrOutputDepTag(t), m)
Colin Cross366938f2017-12-11 16:29:02 -08002798 }
2799 }
2800}
2801
Colin Cross41955e82019-05-29 14:40:35 -07002802// A module that implements SourceFileProducer can be referenced from any property that is tagged with `android:"path"`
2803// 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 -08002804type SourceFileProducer interface {
2805 Srcs() Paths
2806}
2807
Colin Cross41955e82019-05-29 14:40:35 -07002808// A module that implements OutputFileProducer can be referenced from any property that is tagged with `android:"path"`
Roland Levillain97c1f342019-11-22 14:20:54 +00002809// 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 -07002810// listed in the property.
2811type OutputFileProducer interface {
2812 OutputFiles(tag string) (Paths, error)
2813}
2814
Colin Cross5e708052019-08-06 13:59:50 -07002815// OutputFilesForModule returns the paths from an OutputFileProducer with the given tag. On error, including if the
2816// module produced zero paths, it reports errors to the ctx and returns nil.
2817func OutputFilesForModule(ctx PathContext, module blueprint.Module, tag string) Paths {
2818 paths, err := outputFilesForModule(ctx, module, tag)
2819 if err != nil {
2820 reportPathError(ctx, err)
2821 return nil
2822 }
2823 return paths
2824}
2825
2826// OutputFileForModule returns the path from an OutputFileProducer with the given tag. On error, including if the
2827// module produced zero or multiple paths, it reports errors to the ctx and returns nil.
2828func OutputFileForModule(ctx PathContext, module blueprint.Module, tag string) Path {
2829 paths, err := outputFilesForModule(ctx, module, tag)
2830 if err != nil {
2831 reportPathError(ctx, err)
2832 return nil
2833 }
2834 if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002835 ReportPathErrorf(ctx, "got multiple output files from module %q, expected exactly one",
Colin Cross5e708052019-08-06 13:59:50 -07002836 pathContextName(ctx, module))
2837 return nil
2838 }
2839 return paths[0]
2840}
2841
2842func outputFilesForModule(ctx PathContext, module blueprint.Module, tag string) (Paths, error) {
2843 if outputFileProducer, ok := module.(OutputFileProducer); ok {
2844 paths, err := outputFileProducer.OutputFiles(tag)
2845 if err != nil {
2846 return nil, fmt.Errorf("failed to get output file from module %q: %s",
2847 pathContextName(ctx, module), err.Error())
2848 }
2849 if len(paths) == 0 {
2850 return nil, fmt.Errorf("failed to get output files from module %q", pathContextName(ctx, module))
2851 }
2852 return paths, nil
Colin Cross74b1e2b2020-11-22 20:23:02 -08002853 } else if sourceFileProducer, ok := module.(SourceFileProducer); ok {
2854 if tag != "" {
2855 return nil, fmt.Errorf("module %q is a SourceFileProducer, not an OutputFileProducer, and so does not support tag %q", pathContextName(ctx, module), tag)
2856 }
2857 paths := sourceFileProducer.Srcs()
2858 if len(paths) == 0 {
2859 return nil, fmt.Errorf("failed to get output files from module %q", pathContextName(ctx, module))
2860 }
2861 return paths, nil
Colin Cross5e708052019-08-06 13:59:50 -07002862 } else {
2863 return nil, fmt.Errorf("module %q is not an OutputFileProducer", pathContextName(ctx, module))
2864 }
2865}
2866
Colin Cross41589502020-12-01 14:00:21 -08002867// Modules can implement HostToolProvider and return a valid OptionalPath from HostToolPath() to
2868// specify that they can be used as a tool by a genrule module.
Colin Crossfe17f6f2019-03-28 19:30:56 -07002869type HostToolProvider interface {
Colin Crossba9e4032020-11-24 16:32:22 -08002870 Module
Colin Cross41589502020-12-01 14:00:21 -08002871 // HostToolPath returns the path to the host tool for the module if it is one, or an invalid
2872 // OptionalPath.
Colin Crossfe17f6f2019-03-28 19:30:56 -07002873 HostToolPath() OptionalPath
2874}
2875
Colin Cross27b922f2019-03-04 22:35:41 -08002876// Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
2877// be tagged with `android:"path" to support automatic source module dependency resolution.
Colin Cross8a497952019-03-05 22:25:09 -08002878//
2879// Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
Colin Cross25de6c32019-06-06 14:29:25 -07002880func (m *moduleContext) ExpandSources(srcFiles, excludes []string) Paths {
2881 return PathsForModuleSrcExcludes(m, srcFiles, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07002882}
2883
Colin Cross2fafa3e2019-03-05 12:39:51 -08002884// Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
2885// be tagged with `android:"path" to support automatic source module dependency resolution.
Colin Cross8a497952019-03-05 22:25:09 -08002886//
2887// Deprecated: use PathForModuleSrc instead.
Colin Cross25de6c32019-06-06 14:29:25 -07002888func (m *moduleContext) ExpandSource(srcFile, prop string) Path {
2889 return PathForModuleSrc(m, srcFile)
Colin Cross2fafa3e2019-03-05 12:39:51 -08002890}
2891
2892// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
2893// the srcFile is non-nil. The property must be tagged with `android:"path" to support automatic source module
2894// dependency resolution.
Colin Cross25de6c32019-06-06 14:29:25 -07002895func (m *moduleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
Colin Cross2fafa3e2019-03-05 12:39:51 -08002896 if srcFile != nil {
Colin Cross25de6c32019-06-06 14:29:25 -07002897 return OptionalPathForPath(PathForModuleSrc(m, *srcFile))
Colin Cross2fafa3e2019-03-05 12:39:51 -08002898 }
2899 return OptionalPath{}
2900}
2901
Colin Cross25de6c32019-06-06 14:29:25 -07002902func (m *moduleContext) RequiredModuleNames() []string {
Jiyong Park6a8cf5f2019-12-30 16:31:09 +09002903 return m.module.RequiredModuleNames()
Nan Zhang6d34b302017-02-04 17:47:46 -08002904}
2905
Colin Cross25de6c32019-06-06 14:29:25 -07002906func (m *moduleContext) HostRequiredModuleNames() []string {
Jiyong Park6a8cf5f2019-12-30 16:31:09 +09002907 return m.module.HostRequiredModuleNames()
Sasha Smundakb6d23052019-04-01 18:37:36 -07002908}
2909
Colin Cross25de6c32019-06-06 14:29:25 -07002910func (m *moduleContext) TargetRequiredModuleNames() []string {
Jiyong Park6a8cf5f2019-12-30 16:31:09 +09002911 return m.module.TargetRequiredModuleNames()
Sasha Smundakb6d23052019-04-01 18:37:36 -07002912}
2913
Colin Cross463a90e2015-06-17 14:20:06 -07002914func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07002915 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07002916}
2917
Colin Cross0875c522017-11-28 17:34:01 -08002918func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07002919 return &buildTargetSingleton{}
2920}
2921
Colin Cross87d8b562017-04-25 10:01:55 -07002922func parentDir(dir string) string {
2923 dir, _ = filepath.Split(dir)
2924 return filepath.Clean(dir)
2925}
2926
Colin Cross1f8c52b2015-06-16 16:38:17 -07002927type buildTargetSingleton struct{}
2928
Colin Cross0875c522017-11-28 17:34:01 -08002929func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
2930 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07002931
Colin Crossc3d87d32020-06-04 13:25:17 -07002932 mmTarget := func(dir string) string {
2933 return "MODULES-IN-" + strings.Replace(filepath.Clean(dir), "/", "-", -1)
Colin Cross87d8b562017-04-25 10:01:55 -07002934 }
2935
Colin Cross0875c522017-11-28 17:34:01 -08002936 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07002937
Colin Cross0875c522017-11-28 17:34:01 -08002938 ctx.VisitAllModules(func(module Module) {
2939 blueprintDir := module.base().blueprintDir
2940 installTarget := module.base().installTarget
2941 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07002942
Colin Cross0875c522017-11-28 17:34:01 -08002943 if checkbuildTarget != nil {
2944 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
2945 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
2946 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07002947
Colin Cross0875c522017-11-28 17:34:01 -08002948 if installTarget != nil {
2949 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07002950 }
2951 })
2952
Dan Willemsen5ba07e82015-12-11 13:51:06 -08002953 suffix := ""
Jingwen Chencda22c92020-11-23 00:22:30 -05002954 if ctx.Config().KatiEnabled() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08002955 suffix = "-soong"
2956 }
2957
Colin Cross1f8c52b2015-06-16 16:38:17 -07002958 // Create a top-level checkbuild target that depends on all modules
Colin Crossc3d87d32020-06-04 13:25:17 -07002959 ctx.Phony("checkbuild"+suffix, checkbuildDeps...)
Colin Cross1f8c52b2015-06-16 16:38:17 -07002960
Dan Willemsend2e95fb2017-09-20 14:30:50 -07002961 // Make will generate the MODULES-IN-* targets
Jingwen Chencda22c92020-11-23 00:22:30 -05002962 if ctx.Config().KatiEnabled() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07002963 return
2964 }
2965
Colin Cross87d8b562017-04-25 10:01:55 -07002966 // Ensure ancestor directories are in modulesInDir
Inseob Kim1a365c62019-06-08 15:47:51 +09002967 dirs := SortedStringKeys(modulesInDir)
Colin Cross87d8b562017-04-25 10:01:55 -07002968 for _, dir := range dirs {
2969 dir := parentDir(dir)
2970 for dir != "." && dir != "/" {
2971 if _, exists := modulesInDir[dir]; exists {
2972 break
2973 }
2974 modulesInDir[dir] = nil
2975 dir = parentDir(dir)
2976 }
2977 }
2978
2979 // Make directories build their direct subdirectories
Colin Cross87d8b562017-04-25 10:01:55 -07002980 for _, dir := range dirs {
2981 p := parentDir(dir)
2982 if p != "." && p != "/" {
Colin Crossc3d87d32020-06-04 13:25:17 -07002983 modulesInDir[p] = append(modulesInDir[p], PathForPhony(ctx, mmTarget(dir)))
Colin Cross87d8b562017-04-25 10:01:55 -07002984 }
2985 }
2986
Dan Willemsend2e95fb2017-09-20 14:30:50 -07002987 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
2988 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
2989 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07002990 for _, dir := range dirs {
Colin Crossc3d87d32020-06-04 13:25:17 -07002991 ctx.Phony(mmTarget(dir), modulesInDir[dir]...)
Colin Cross1f8c52b2015-06-16 16:38:17 -07002992 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07002993
2994 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
Jiyong Park1613e552020-09-14 19:43:17 +09002995 type osAndCross struct {
2996 os OsType
2997 hostCross bool
2998 }
2999 osDeps := map[osAndCross]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08003000 ctx.VisitAllModules(func(module Module) {
3001 if module.Enabled() {
Jiyong Park1613e552020-09-14 19:43:17 +09003002 key := osAndCross{os: module.Target().Os, hostCross: module.Target().HostCross}
3003 osDeps[key] = append(osDeps[key], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07003004 }
3005 })
3006
Colin Cross0875c522017-11-28 17:34:01 -08003007 osClass := make(map[string]Paths)
Jiyong Park1613e552020-09-14 19:43:17 +09003008 for key, deps := range osDeps {
Dan Willemsen61d88b82017-09-20 17:29:08 -07003009 var className string
3010
Jiyong Park1613e552020-09-14 19:43:17 +09003011 switch key.os.Class {
Dan Willemsen61d88b82017-09-20 17:29:08 -07003012 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +09003013 if key.hostCross {
3014 className = "host-cross"
3015 } else {
3016 className = "host"
3017 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07003018 case Device:
3019 className = "target"
3020 default:
3021 continue
3022 }
3023
Jiyong Park1613e552020-09-14 19:43:17 +09003024 name := className + "-" + key.os.Name
Colin Crossc3d87d32020-06-04 13:25:17 -07003025 osClass[className] = append(osClass[className], PathForPhony(ctx, name))
Dan Willemsen61d88b82017-09-20 17:29:08 -07003026
Colin Crossc3d87d32020-06-04 13:25:17 -07003027 ctx.Phony(name, deps...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07003028 }
3029
3030 // Wrap those into host|host-cross|target phony rules
Inseob Kim1a365c62019-06-08 15:47:51 +09003031 for _, class := range SortedStringKeys(osClass) {
Colin Crossc3d87d32020-06-04 13:25:17 -07003032 ctx.Phony(class, osClass[class]...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07003033 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07003034}
Colin Crossd779da42015-12-17 18:00:23 -08003035
Brandon Lee5d45c6f2018-08-15 15:35:38 -07003036// Collect information for opening IDE project files in java/jdeps.go.
3037type IDEInfo interface {
3038 IDEInfo(ideInfo *IdeInfo)
3039 BaseModuleName() string
3040}
3041
3042// Extract the base module name from the Import name.
3043// Often the Import name has a prefix "prebuilt_".
3044// Remove the prefix explicitly if needed
3045// until we find a better solution to get the Import name.
3046type IDECustomizedModuleName interface {
3047 IDECustomizedModuleName() string
3048}
3049
3050type IdeInfo struct {
3051 Deps []string `json:"dependencies,omitempty"`
3052 Srcs []string `json:"srcs,omitempty"`
3053 Aidl_include_dirs []string `json:"aidl_include_dirs,omitempty"`
3054 Jarjar_rules []string `json:"jarjar_rules,omitempty"`
3055 Jars []string `json:"jars,omitempty"`
3056 Classes []string `json:"class,omitempty"`
3057 Installed_paths []string `json:"installed,omitempty"`
patricktu18c82ff2019-05-10 15:48:50 +08003058 SrcJars []string `json:"srcjars,omitempty"`
bralee1fbf4402020-05-21 10:11:59 +08003059 Paths []string `json:"path,omitempty"`
Brandon Lee5d45c6f2018-08-15 15:35:38 -07003060}
Paul Duffinf88d8e02020-05-07 20:21:34 +01003061
3062func CheckBlueprintSyntax(ctx BaseModuleContext, filename string, contents string) []error {
3063 bpctx := ctx.blueprintBaseModuleContext()
3064 return blueprint.CheckBlueprintSyntax(bpctx.ModuleFactories(), filename, contents)
3065}
Colin Cross5d583952020-11-24 16:21:24 -08003066
3067// installPathsDepSet is a thin type-safe wrapper around the generic depSet. It always uses
3068// topological order.
3069type installPathsDepSet struct {
3070 depSet
3071}
3072
3073// newInstallPathsDepSet returns an immutable packagingSpecsDepSet with the given direct and
3074// transitive contents.
3075func newInstallPathsDepSet(direct InstallPaths, transitive []*installPathsDepSet) *installPathsDepSet {
3076 return &installPathsDepSet{*newDepSet(TOPOLOGICAL, direct, transitive)}
3077}
3078
3079// ToList returns the installPathsDepSet flattened to a list in topological order.
3080func (d *installPathsDepSet) ToList() InstallPaths {
3081 if d == nil {
3082 return nil
3083 }
3084 return d.depSet.ToList().(InstallPaths)
3085}