blob: 3316a444178327e98ebd0220212d313f92fa064c [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6ff51382015-12-17 16:39:19 -080018 "fmt"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "path/filepath"
Colin Cross0875c522017-11-28 17:34:01 -080020 "sort"
Colin Cross6ff51382015-12-17 16:39:19 -080021 "strings"
Colin Crossaabf6792017-11-29 00:27:14 -080022 "text/scanner"
Colin Crossf6566ed2015-03-24 11:13:38 -070023
24 "github.com/google/blueprint"
Colin Cross7f19f372016-11-01 11:10:25 -070025 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080026)
27
28var (
29 DeviceSharedLibrary = "shared_library"
30 DeviceStaticLibrary = "static_library"
31 DeviceExecutable = "executable"
32 HostSharedLibrary = "host_shared_library"
33 HostStaticLibrary = "host_static_library"
34 HostExecutable = "host_executable"
35)
36
Colin Crossae887032017-10-23 17:16:14 -070037type BuildParams struct {
Dan Willemsen9f3c5742016-11-03 14:28:31 -070038 Rule blueprint.Rule
Colin Cross33bfb0a2016-11-21 17:23:08 -080039 Deps blueprint.Deps
40 Depfile WritablePath
Colin Cross67a5c132017-05-09 13:45:28 -070041 Description string
Dan Willemsen9f3c5742016-11-03 14:28:31 -070042 Output WritablePath
43 Outputs WritablePaths
44 ImplicitOutput WritablePath
45 ImplicitOutputs WritablePaths
46 Input Path
47 Inputs Paths
48 Implicit Path
49 Implicits Paths
50 OrderOnly Paths
51 Default bool
52 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070053}
54
Colin Crossae887032017-10-23 17:16:14 -070055type ModuleBuildParams BuildParams
56
Colin Crossf6566ed2015-03-24 11:13:38 -070057type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070058 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070059 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070060 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070061 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070062 Host() bool
63 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070064 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070065 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070066 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070067 PrimaryArch() bool
Jiyong Park2db76922017-11-08 16:03:48 +090068 Platform() bool
69 DeviceSpecific() bool
70 SocSpecific() bool
71 ProductSpecific() bool
Colin Cross1332b002015-04-07 17:11:30 -070072 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070073 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070074}
75
Colin Cross635c3b02016-05-18 15:37:25 -070076type BaseContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080077 BaseModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070078 androidBaseContext
79}
80
Colin Crossaabf6792017-11-29 00:27:14 -080081// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
82// a Config instead of an interface{}.
83type BaseModuleContext interface {
84 ModuleName() string
85 ModuleDir() string
86 Config() Config
87
88 ContainsProperty(name string) bool
89 Errorf(pos scanner.Position, fmt string, args ...interface{})
90 ModuleErrorf(fmt string, args ...interface{})
91 PropertyErrorf(property, fmt string, args ...interface{})
92 Failed() bool
93
94 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
95 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
96 // builder whenever a file matching the pattern as added or removed, without rerunning if a
97 // file that does not match the pattern is added to a searched directory.
98 GlobWithDeps(pattern string, excludes []string) ([]string, error)
99
100 Fs() pathtools.FileSystem
101 AddNinjaFileDeps(deps ...string)
102}
103
Colin Cross635c3b02016-05-18 15:37:25 -0700104type ModuleContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -0700105 androidBaseContext
Colin Crossaabf6792017-11-29 00:27:14 -0800106 BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -0800107
Colin Crossae887032017-10-23 17:16:14 -0700108 // Deprecated: use ModuleContext.Build instead.
Colin Cross0875c522017-11-28 17:34:01 -0800109 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -0700110
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700111 ExpandSources(srcFiles, excludes []string) Paths
Colin Cross366938f2017-12-11 16:29:02 -0800112 ExpandSource(srcFile, prop string) Path
Colin Cross2383f3b2018-02-06 14:40:13 -0800113 ExpandOptionalSource(srcFile *string, prop string) OptionalPath
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800114 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -0700115 Glob(globPattern string, excludes []string) Paths
Nan Zhang581fd212018-01-10 16:06:12 -0800116 GlobFiles(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700117
Colin Cross5c517922017-08-31 12:29:17 -0700118 InstallExecutable(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
119 InstallFile(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -0800120 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800122
123 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -0700124
Colin Cross8d8f8e22016-08-03 11:57:50 -0700125 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700126 InstallInSanitizerDir() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900127 InstallInRecovery() bool
Nan Zhang6d34b302017-02-04 17:47:46 -0800128
129 RequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -0700130
131 // android.ModuleContext methods
132 // These are duplicated instead of embedded so that can eventually be wrapped to take an
133 // android.Module instead of a blueprint.Module
134 OtherModuleName(m blueprint.Module) string
135 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
136 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
137
138 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
139 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
140
141 ModuleSubDir() string
142
Colin Cross35143d02017-11-16 00:11:20 -0800143 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700144 VisitDirectDeps(visit func(Module))
Colin Crossee6143c2017-12-30 17:54:27 -0800145 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700146 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
147 VisitDepsDepthFirst(visit func(Module))
148 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
149 WalkDeps(visit func(Module, Module) bool)
Colin Cross3f68a132017-10-23 17:10:29 -0700150
Colin Cross0875c522017-11-28 17:34:01 -0800151 Variable(pctx PackageContext, name, value string)
152 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700153 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
154 // and performs more verification.
Colin Cross0875c522017-11-28 17:34:01 -0800155 Build(pctx PackageContext, params BuildParams)
Colin Cross3f68a132017-10-23 17:10:29 -0700156
Colin Cross0875c522017-11-28 17:34:01 -0800157 PrimaryModule() Module
158 FinalModule() Module
159 VisitAllModuleVariants(visit func(Module))
Colin Cross3f68a132017-10-23 17:10:29 -0700160
161 GetMissingDependencies() []string
Jeff Gaston088e29e2017-11-29 16:47:17 -0800162 Namespace() blueprint.Namespace
Colin Cross3f40fa42015-01-30 17:27:36 -0800163}
164
Colin Cross635c3b02016-05-18 15:37:25 -0700165type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800166 blueprint.Module
167
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700168 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
169 // but GenerateAndroidBuildActions also has access to Android-specific information.
170 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700171 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700172
Colin Cross1e676be2016-10-12 14:38:15 -0700173 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800174
Colin Cross635c3b02016-05-18 15:37:25 -0700175 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800176 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700177 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800178 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700179 InstallInSanitizerDir() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900180 InstallInRecovery() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800181 SkipInstall()
Jiyong Park374510b2018-03-19 18:23:01 +0900182 ExportedToMake() bool
Colin Cross36242852017-06-23 15:06:31 -0700183
184 AddProperties(props ...interface{})
185 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700186
Colin Crossae887032017-10-23 17:16:14 -0700187 BuildParamsForTests() []BuildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800188}
189
Colin Crossfc754582016-05-17 16:34:16 -0700190type nameProperties struct {
191 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800192 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700193}
194
195type commonProperties struct {
Dan Willemsen0effe062015-11-30 16:06:01 -0800196 // emit build rules for this module
197 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800198
Colin Cross7d5136f2015-05-11 13:39:40 -0700199 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800200 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
201 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
202 // platform
Colin Cross7d716ba2017-11-01 10:38:29 -0700203 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700204
205 Target struct {
206 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700207 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700208 }
209 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700210 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700211 }
212 }
213
214 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800215
Dan Willemsen782a2d12015-12-21 14:55:28 -0800216 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700217 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800218
Colin Cross55708f32017-03-20 13:23:34 -0700219 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700220 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700221
Jiyong Park2db76922017-11-08 16:03:48 +0900222 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
223 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
224 // Use `soc_specific` instead for better meaning.
Colin Cross7d716ba2017-11-01 10:38:29 -0700225 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700226
Jiyong Park2db76922017-11-08 16:03:48 +0900227 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
228 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
229 Soc_specific *bool
230
231 // whether this module is specific to a device, not only for SoC, but also for off-chip
232 // peripherals. When set to true, it is installed into /odm (or /vendor/odm if odm partition
233 // does not exist, or /system/vendor/odm if both odm and vendor partitions do not exist).
234 // This implies `soc_specific:true`.
235 Device_specific *bool
236
237 // whether this module is specific to a software configuration of a product (e.g. country,
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900238 // network operator, etc). When set to true, it is installed into /product (or
239 // /system/product if product partition does not exist).
Jiyong Park2db76922017-11-08 16:03:48 +0900240 Product_specific *bool
241
Jiyong Parkf9332f12018-02-01 00:54:12 +0900242 // Whether this module is installed to recovery partition
243 Recovery *bool
244
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700245 // init.rc files to be installed if this module is installed
246 Init_rc []string
247
Steven Moreland57a23d22018-04-04 15:42:19 -0700248 // VINTF manifest fragments to be installed if this module is installed
249 Vintf_fragments []string
250
Chris Wolfe998306e2016-08-15 14:47:23 -0400251 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700252 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400253
Colin Cross5aac3622017-08-31 15:07:09 -0700254 // relative path to a file to include in the list of notices for the device
255 Notice *string
256
Colin Crossa1ad8d12016-06-01 17:09:44 -0700257 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700258 CompileTarget Target `blueprint:"mutated"`
259 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800260
261 // Set by InitAndroidModule
262 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700263 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700264
265 SkipInstall bool `blueprint:"mutated"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800266
267 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800268}
269
270type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700271 Host_supported *bool
272 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800273}
274
Colin Crossc472d572015-03-17 15:06:21 -0700275type Multilib string
276
277const (
Colin Cross6b4a32d2017-12-05 13:42:45 -0800278 MultilibBoth Multilib = "both"
279 MultilibFirst Multilib = "first"
280 MultilibCommon Multilib = "common"
281 MultilibCommonFirst Multilib = "common_first"
282 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700283)
284
Colin Crossa1ad8d12016-06-01 17:09:44 -0700285type HostOrDeviceSupported int
286
287const (
288 _ HostOrDeviceSupported = iota
289 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700290 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700291 DeviceSupported
292 HostAndDeviceSupported
293 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700294 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700295)
296
Jiyong Park2db76922017-11-08 16:03:48 +0900297type moduleKind int
298
299const (
300 platformModule moduleKind = iota
301 deviceSpecificModule
302 socSpecificModule
303 productSpecificModule
304)
305
306func (k moduleKind) String() string {
307 switch k {
308 case platformModule:
309 return "platform"
310 case deviceSpecificModule:
311 return "device-specific"
312 case socSpecificModule:
313 return "soc-specific"
314 case productSpecificModule:
315 return "product-specific"
316 default:
317 panic(fmt.Errorf("unknown module kind %d", k))
318 }
319}
320
Colin Cross36242852017-06-23 15:06:31 -0700321func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800322 base := m.base()
323 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700324
Colin Cross36242852017-06-23 15:06:31 -0700325 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700326 &base.nameProperties,
327 &base.commonProperties,
328 &base.variableProperties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700329 base.customizableProperties = m.GetProperties()
Colin Cross5049f022015-03-18 13:28:46 -0700330}
331
Colin Cross36242852017-06-23 15:06:31 -0700332func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
333 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700334
335 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800336 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700337 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700338 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800339
Dan Willemsen218f6562015-07-08 18:13:11 -0700340 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700341 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700342 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800343 }
344
Colin Cross36242852017-06-23 15:06:31 -0700345 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800346}
347
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800348// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800349// modules. It should be included as an anonymous field in every module
350// struct definition. InitAndroidModule should then be called from the module's
351// factory function, and the return values from InitAndroidModule should be
352// returned from the factory function.
353//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800354// The ModuleBase type is responsible for implementing the GenerateBuildActions
355// method to support the blueprint.Module interface. This method will then call
356// the module's GenerateAndroidBuildActions method once for each build variant
357// that is to be built. GenerateAndroidBuildActions is passed a
358// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800359// AndroidModuleContext exposes extra functionality specific to the Android build
360// system including details about the particular build variant that is to be
361// generated.
362//
363// For example:
364//
365// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800366// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800367// )
368//
369// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800370// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800371// properties struct {
372// MyProperty string
373// }
374// }
375//
Colin Cross36242852017-06-23 15:06:31 -0700376// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800377// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700378// m.AddProperties(&m.properties)
379// android.InitAndroidModule(m)
380// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800381// }
382//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800383// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800384// // Get the CPU architecture for the current build variant.
385// variantArch := ctx.Arch()
386//
387// // ...
388// }
Colin Cross635c3b02016-05-18 15:37:25 -0700389type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800390 // Putting the curiously recurring thing pointing to the thing that contains
391 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700392 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700393 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800394
Colin Crossfc754582016-05-17 16:34:16 -0700395 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800396 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700397 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800398 hostAndDeviceProperties hostAndDeviceProperties
399 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700400 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700401 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800402
403 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700404 installFiles Paths
405 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700406
407 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
408 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800409 installTarget WritablePath
410 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700411 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700412
Colin Cross178a5092016-09-13 13:42:32 -0700413 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700414
415 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700416
417 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700418 buildParams []BuildParams
Colin Cross36242852017-06-23 15:06:31 -0700419}
420
421func (a *ModuleBase) AddProperties(props ...interface{}) {
422 a.registerProps = append(a.registerProps, props...)
423}
424
425func (a *ModuleBase) GetProperties() []interface{} {
426 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800427}
428
Colin Crossae887032017-10-23 17:16:14 -0700429func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700430 return a.buildParams
431}
432
Colin Crossce75d2c2016-10-06 16:12:58 -0700433// Name returns the name of the module. It may be overridden by individual module types, for
434// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700435func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800436 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700437}
438
Colin Crossce75d2c2016-10-06 16:12:58 -0700439// BaseModuleName returns the name of the module as specified in the blueprints file.
440func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800441 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700442}
443
Colin Cross635c3b02016-05-18 15:37:25 -0700444func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800445 return a
446}
447
Colin Cross8b74d172016-09-13 09:59:14 -0700448func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700449 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700450 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700451}
452
Colin Crossa1ad8d12016-06-01 17:09:44 -0700453func (a *ModuleBase) Target() Target {
454 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800455}
456
Colin Cross8b74d172016-09-13 09:59:14 -0700457func (a *ModuleBase) TargetPrimary() bool {
458 return a.commonProperties.CompilePrimary
459}
460
Colin Crossa1ad8d12016-06-01 17:09:44 -0700461func (a *ModuleBase) Os() OsType {
462 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800463}
464
Colin Cross635c3b02016-05-18 15:37:25 -0700465func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700466 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800467}
468
Colin Cross635c3b02016-05-18 15:37:25 -0700469func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700470 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800471}
472
Dan Willemsen0b24c742016-10-04 15:13:37 -0700473func (a *ModuleBase) ArchSpecific() bool {
474 return a.commonProperties.ArchSpecific
475}
476
Colin Crossa1ad8d12016-06-01 17:09:44 -0700477func (a *ModuleBase) OsClassSupported() []OsClass {
478 switch a.commonProperties.HostOrDeviceSupported {
479 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700480 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700481 case HostSupportedNoCross:
482 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700483 case DeviceSupported:
484 return []OsClass{Device}
485 case HostAndDeviceSupported:
486 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700487 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700488 supported = append(supported, Host, HostCross)
489 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700490 if a.hostAndDeviceProperties.Device_supported == nil ||
491 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700492 supported = append(supported, Device)
493 }
494 return supported
495 default:
496 return nil
497 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800498}
499
Colin Cross635c3b02016-05-18 15:37:25 -0700500func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800501 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
502 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700503 (a.hostAndDeviceProperties.Device_supported == nil ||
504 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800505}
506
Jiyong Parkc678ad32018-04-10 13:07:10 +0900507func (a *ModuleBase) Platform() bool {
508 return !a.DeviceSpecific() && !a.SocSpecific() && !a.ProductSpecific()
509}
510
511func (a *ModuleBase) DeviceSpecific() bool {
512 return Bool(a.commonProperties.Device_specific)
513}
514
515func (a *ModuleBase) SocSpecific() bool {
516 return Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
517}
518
519func (a *ModuleBase) ProductSpecific() bool {
520 return Bool(a.commonProperties.Product_specific)
521}
522
Colin Cross635c3b02016-05-18 15:37:25 -0700523func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800524 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800525 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800526 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800527 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800528}
529
Colin Crossce75d2c2016-10-06 16:12:58 -0700530func (a *ModuleBase) SkipInstall() {
531 a.commonProperties.SkipInstall = true
532}
533
Jiyong Park374510b2018-03-19 18:23:01 +0900534func (a *ModuleBase) ExportedToMake() bool {
535 return a.commonProperties.NamespaceExportedToMake
536}
537
Colin Cross635c3b02016-05-18 15:37:25 -0700538func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700539 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800540
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700541 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800542 ctx.VisitDepsDepthFirstIf(isFileInstaller,
543 func(m blueprint.Module) {
544 fileInstaller := m.(fileInstaller)
545 files := fileInstaller.filesToInstall()
546 result = append(result, files...)
547 })
548
549 return result
550}
551
Colin Cross635c3b02016-05-18 15:37:25 -0700552func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800553 return a.installFiles
554}
555
Colin Cross635c3b02016-05-18 15:37:25 -0700556func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800557 return p.noAddressSanitizer
558}
559
Colin Cross635c3b02016-05-18 15:37:25 -0700560func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800561 return false
562}
563
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700564func (p *ModuleBase) InstallInSanitizerDir() bool {
565 return false
566}
567
Jiyong Parkf9332f12018-02-01 00:54:12 +0900568func (p *ModuleBase) InstallInRecovery() bool {
569 return Bool(p.commonProperties.Recovery)
570}
571
Colin Cross0875c522017-11-28 17:34:01 -0800572func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700573 allInstalledFiles := Paths{}
574 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800575 ctx.VisitAllModuleVariants(func(module Module) {
576 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700577 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
578 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800579 })
580
Colin Cross0875c522017-11-28 17:34:01 -0800581 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700582
Jeff Gaston088e29e2017-11-29 16:47:17 -0800583 namespacePrefix := ctx.Namespace().(*Namespace).id
584 if namespacePrefix != "" {
585 namespacePrefix = namespacePrefix + "-"
586 }
587
Colin Cross3f40fa42015-01-30 17:27:36 -0800588 if len(allInstalledFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800589 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800590 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700591 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800592 Output: name,
593 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800594 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700595 })
596 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700597 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700598 }
599
600 if len(allCheckbuildFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800601 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800602 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700603 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800604 Output: name,
605 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700606 })
607 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700608 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700609 }
610
611 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800612 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800613 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800614 suffix = "-soong"
615 }
616
Jeff Gaston088e29e2017-11-29 16:47:17 -0800617 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800618 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700619 Rule: blueprint.Phony,
Jeff Gaston088e29e2017-11-29 16:47:17 -0800620 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700621 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800622 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700623
624 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800625 }
626}
627
Jiyong Park2db76922017-11-08 16:03:48 +0900628func determineModuleKind(a *ModuleBase, ctx blueprint.BaseModuleContext) moduleKind {
629 var socSpecific = Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
630 var deviceSpecific = Bool(a.commonProperties.Device_specific)
631 var productSpecific = Bool(a.commonProperties.Product_specific)
632
633 if ((socSpecific || deviceSpecific) && productSpecific) || (socSpecific && deviceSpecific) {
634 msg := "conflicting value set here"
635 if productSpecific {
636 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
637 if deviceSpecific {
638 ctx.PropertyErrorf("device_specific", msg)
639 }
640 } else {
641 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
642 }
643 if Bool(a.commonProperties.Vendor) {
644 ctx.PropertyErrorf("vendor", msg)
645 }
646 if Bool(a.commonProperties.Proprietary) {
647 ctx.PropertyErrorf("proprietary", msg)
648 }
649 if Bool(a.commonProperties.Soc_specific) {
650 ctx.PropertyErrorf("soc_specific", msg)
651 }
652 }
653
654 if productSpecific {
655 return productSpecificModule
656 } else if deviceSpecific {
657 return deviceSpecificModule
658 } else if socSpecific {
659 return socSpecificModule
660 } else {
661 return platformModule
662 }
663}
664
Colin Cross635c3b02016-05-18 15:37:25 -0700665func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700666 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700667 target: a.commonProperties.CompileTarget,
668 targetPrimary: a.commonProperties.CompilePrimary,
Jiyong Park2db76922017-11-08 16:03:48 +0900669 kind: determineModuleKind(a, ctx),
Colin Cross8b74d172016-09-13 09:59:14 -0700670 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800671 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800672}
673
Colin Cross0875c522017-11-28 17:34:01 -0800674func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
675 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700676 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800677 ModuleContext: blueprintCtx,
678 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
679 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700680 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800681 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800682 }
683
Colin Cross67a5c132017-05-09 13:45:28 -0700684 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
685 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800686 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
687 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700688 }
Colin Cross0875c522017-11-28 17:34:01 -0800689 if !ctx.PrimaryArch() {
690 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700691 }
692
693 ctx.Variable(pctx, "moduleDesc", desc)
694
695 s := ""
696 if len(suffix) > 0 {
697 s = " [" + strings.Join(suffix, " ") + "]"
698 }
699 ctx.Variable(pctx, "moduleDescSuffix", s)
700
Colin Cross9b1d13d2016-09-19 15:18:11 -0700701 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800702 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700703 if ctx.Failed() {
704 return
705 }
706
Colin Cross0875c522017-11-28 17:34:01 -0800707 a.installFiles = append(a.installFiles, ctx.installFiles...)
708 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800709 }
710
Colin Cross9b1d13d2016-09-19 15:18:11 -0700711 if a == ctx.FinalModule().(Module).base() {
712 a.generateModuleTarget(ctx)
713 if ctx.Failed() {
714 return
715 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800716 }
Colin Crosscec81712017-07-13 14:43:27 -0700717
Colin Cross0875c522017-11-28 17:34:01 -0800718 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800719}
720
Colin Crossf6566ed2015-03-24 11:13:38 -0700721type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700722 target Target
723 targetPrimary bool
724 debug bool
Jiyong Park2db76922017-11-08 16:03:48 +0900725 kind moduleKind
Colin Cross8b74d172016-09-13 09:59:14 -0700726 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700727}
728
Colin Cross3f40fa42015-01-30 17:27:36 -0800729type androidModuleContext struct {
730 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700731 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700732 installDeps Paths
733 installFiles Paths
734 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800735 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700736 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700737
738 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700739 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800740}
741
Colin Cross67a5c132017-05-09 13:45:28 -0700742func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800743 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700744 Rule: ErrorRule,
745 Description: desc,
746 Outputs: outputs,
747 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800748 Args: map[string]string{
749 "error": err.Error(),
750 },
751 })
752 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800753}
754
Colin Crossaabf6792017-11-29 00:27:14 -0800755func (a *androidModuleContext) Config() Config {
756 return a.ModuleContext.Config().(Config)
757}
758
Colin Cross0875c522017-11-28 17:34:01 -0800759func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700760 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800761}
762
Colin Cross0875c522017-11-28 17:34:01 -0800763func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700764 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700765 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800766 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800767 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700768 Outputs: params.Outputs.Strings(),
769 ImplicitOutputs: params.ImplicitOutputs.Strings(),
770 Inputs: params.Inputs.Strings(),
771 Implicits: params.Implicits.Strings(),
772 OrderOnly: params.OrderOnly.Strings(),
773 Args: params.Args,
774 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700775 }
776
Colin Cross33bfb0a2016-11-21 17:23:08 -0800777 if params.Depfile != nil {
778 bparams.Depfile = params.Depfile.String()
779 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700780 if params.Output != nil {
781 bparams.Outputs = append(bparams.Outputs, params.Output.String())
782 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700783 if params.ImplicitOutput != nil {
784 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
785 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700786 if params.Input != nil {
787 bparams.Inputs = append(bparams.Inputs, params.Input.String())
788 }
789 if params.Implicit != nil {
790 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
791 }
792
Colin Cross0875c522017-11-28 17:34:01 -0800793 return bparams
794}
795
796func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
797 a.ModuleContext.Variable(pctx.PackageContext, name, value)
798}
799
800func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
801 argNames ...string) blueprint.Rule {
802
803 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
804}
805
806func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
807 if a.config.captureBuild {
808 a.buildParams = append(a.buildParams, params)
809 }
810
811 bparams := convertBuildParams(params)
812
813 if bparams.Description != "" {
814 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
815 }
816
Colin Cross6ff51382015-12-17 16:39:19 -0800817 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700818 a.ninjaError(bparams.Description, bparams.Outputs,
819 fmt.Errorf("module %s missing dependencies: %s\n",
820 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800821 return
822 }
823
Colin Cross0875c522017-11-28 17:34:01 -0800824 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700825}
826
Colin Cross6ff51382015-12-17 16:39:19 -0800827func (a *androidModuleContext) GetMissingDependencies() []string {
828 return a.missingDeps
829}
830
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800831func (a *androidModuleContext) AddMissingDependencies(deps []string) {
832 if deps != nil {
833 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700834 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800835 }
836}
837
Colin Crossd11fcda2017-10-23 17:59:01 -0700838func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
839 aModule, _ := module.(Module)
840 if aModule == nil {
841 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
842 return nil
843 }
844
845 if !aModule.Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800846 if a.Config().AllowMissingDependencies() {
Colin Crossd11fcda2017-10-23 17:59:01 -0700847 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
848 } else {
849 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
850 }
851 return nil
852 }
853
854 return aModule
855}
856
Colin Cross35143d02017-11-16 00:11:20 -0800857func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
858 a.ModuleContext.VisitDirectDeps(visit)
859}
860
Colin Crossd11fcda2017-10-23 17:59:01 -0700861func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
862 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
863 if aModule := a.validateAndroidModule(module); aModule != nil {
864 visit(aModule)
865 }
866 })
867}
868
Colin Crossee6143c2017-12-30 17:54:27 -0800869func (a *androidModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
870 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
871 if aModule := a.validateAndroidModule(module); aModule != nil {
872 if a.ModuleContext.OtherModuleDependencyTag(aModule) == tag {
873 visit(aModule)
874 }
875 }
876 })
877}
878
Colin Crossd11fcda2017-10-23 17:59:01 -0700879func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
880 a.ModuleContext.VisitDirectDepsIf(
881 // pred
882 func(module blueprint.Module) bool {
883 if aModule := a.validateAndroidModule(module); aModule != nil {
884 return pred(aModule)
885 } else {
886 return false
887 }
888 },
889 // visit
890 func(module blueprint.Module) {
891 visit(module.(Module))
892 })
893}
894
895func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
896 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
897 if aModule := a.validateAndroidModule(module); aModule != nil {
898 visit(aModule)
899 }
900 })
901}
902
903func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
904 a.ModuleContext.VisitDepsDepthFirstIf(
905 // pred
906 func(module blueprint.Module) bool {
907 if aModule := a.validateAndroidModule(module); aModule != nil {
908 return pred(aModule)
909 } else {
910 return false
911 }
912 },
913 // visit
914 func(module blueprint.Module) {
915 visit(module.(Module))
916 })
917}
918
919func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
920 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
921 childAndroidModule := a.validateAndroidModule(child)
922 parentAndroidModule := a.validateAndroidModule(parent)
923 if childAndroidModule != nil && parentAndroidModule != nil {
924 return visit(childAndroidModule, parentAndroidModule)
925 } else {
926 return false
927 }
928 })
929}
930
Colin Cross0875c522017-11-28 17:34:01 -0800931func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
932 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
933 visit(module.(Module))
934 })
935}
936
937func (a *androidModuleContext) PrimaryModule() Module {
938 return a.ModuleContext.PrimaryModule().(Module)
939}
940
941func (a *androidModuleContext) FinalModule() Module {
942 return a.ModuleContext.FinalModule().(Module)
943}
944
Colin Crossa1ad8d12016-06-01 17:09:44 -0700945func (a *androidBaseContextImpl) Target() Target {
946 return a.target
947}
948
Colin Cross8b74d172016-09-13 09:59:14 -0700949func (a *androidBaseContextImpl) TargetPrimary() bool {
950 return a.targetPrimary
951}
952
Colin Crossf6566ed2015-03-24 11:13:38 -0700953func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700954 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800955}
956
Colin Crossa1ad8d12016-06-01 17:09:44 -0700957func (a *androidBaseContextImpl) Os() OsType {
958 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800959}
960
Colin Crossf6566ed2015-03-24 11:13:38 -0700961func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700962 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700963}
964
965func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700966 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700967}
968
Colin Cross0af4b842015-04-30 16:36:18 -0700969func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700970 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700971}
972
Colin Cross3edeee12017-04-04 12:59:48 -0700973func (a *androidBaseContextImpl) Windows() bool {
974 return a.target.Os == Windows
975}
976
Colin Crossf6566ed2015-03-24 11:13:38 -0700977func (a *androidBaseContextImpl) Debug() bool {
978 return a.debug
979}
980
Colin Cross1e7d3702016-08-24 15:25:47 -0700981func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700982 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
983 return true
984 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700985 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
986}
987
Colin Cross1332b002015-04-07 17:11:30 -0700988func (a *androidBaseContextImpl) AConfig() Config {
989 return a.config
990}
991
Colin Cross9272ade2016-08-17 15:24:12 -0700992func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
993 return DeviceConfig{a.config.deviceConfig}
994}
995
Jiyong Park2db76922017-11-08 16:03:48 +0900996func (a *androidBaseContextImpl) Platform() bool {
997 return a.kind == platformModule
998}
999
1000func (a *androidBaseContextImpl) DeviceSpecific() bool {
1001 return a.kind == deviceSpecificModule
1002}
1003
1004func (a *androidBaseContextImpl) SocSpecific() bool {
1005 return a.kind == socSpecificModule
1006}
1007
1008func (a *androidBaseContextImpl) ProductSpecific() bool {
1009 return a.kind == productSpecificModule
Dan Willemsen782a2d12015-12-21 14:55:28 -08001010}
1011
Colin Cross8d8f8e22016-08-03 11:57:50 -07001012func (a *androidModuleContext) InstallInData() bool {
1013 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08001014}
1015
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001016func (a *androidModuleContext) InstallInSanitizerDir() bool {
1017 return a.module.InstallInSanitizerDir()
1018}
1019
Jiyong Parkf9332f12018-02-01 00:54:12 +09001020func (a *androidModuleContext) InstallInRecovery() bool {
1021 return a.module.InstallInRecovery()
1022}
1023
Colin Cross893d8162017-04-26 17:34:03 -07001024func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
1025 if a.module.base().commonProperties.SkipInstall {
1026 return true
1027 }
1028
Colin Cross3607f212018-05-07 15:28:05 -07001029 // We'll need a solution for choosing which of modules with the same name in different
1030 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
1031 // list of namespaces to install in a Soong-only build.
1032 if !a.module.base().commonProperties.NamespaceExportedToMake {
1033 return true
1034 }
1035
Colin Cross893d8162017-04-26 17:34:03 -07001036 if a.Device() {
Colin Cross6510f912017-11-29 00:27:14 -08001037 if a.Config().SkipDeviceInstall() {
Colin Cross893d8162017-04-26 17:34:03 -07001038 return true
1039 }
1040
Colin Cross6510f912017-11-29 00:27:14 -08001041 if a.Config().SkipMegaDeviceInstall(fullInstallPath.String()) {
Colin Cross893d8162017-04-26 17:34:03 -07001042 return true
1043 }
1044 }
1045
1046 return false
1047}
1048
Colin Cross5c517922017-08-31 12:29:17 -07001049func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -07001050 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -07001051 return a.installFile(installPath, name, srcPath, Cp, deps)
1052}
1053
1054func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
1055 deps ...Path) OutputPath {
1056 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
1057}
1058
1059func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
1060 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -07001061
Dan Willemsen782a2d12015-12-21 14:55:28 -08001062 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001063 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001064
Colin Cross893d8162017-04-26 17:34:03 -07001065 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001066
Dan Willemsen322acaf2016-01-12 23:07:05 -08001067 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -07001068
Colin Cross89562dc2016-10-03 17:47:19 -07001069 var implicitDeps, orderOnlyDeps Paths
1070
1071 if a.Host() {
1072 // Installed host modules might be used during the build, depend directly on their
1073 // dependencies so their timestamp is updated whenever their dependency is updated
1074 implicitDeps = deps
1075 } else {
1076 orderOnlyDeps = deps
1077 }
1078
Colin Crossae887032017-10-23 17:16:14 -07001079 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07001080 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07001081 Description: "install " + fullInstallPath.Base(),
1082 Output: fullInstallPath,
1083 Input: srcPath,
1084 Implicits: implicitDeps,
1085 OrderOnly: orderOnlyDeps,
Colin Cross6510f912017-11-29 00:27:14 -08001086 Default: !a.Config().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08001087 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001088
Dan Willemsen322acaf2016-01-12 23:07:05 -08001089 a.installFiles = append(a.installFiles, fullInstallPath)
1090 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001091 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -07001092 return fullInstallPath
1093}
1094
Colin Cross3854a602016-01-11 12:49:11 -08001095func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
1096 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001097 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08001098
Colin Cross893d8162017-04-26 17:34:03 -07001099 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001100
Colin Crossae887032017-10-23 17:16:14 -07001101 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001102 Rule: Symlink,
1103 Description: "install symlink " + fullInstallPath.Base(),
1104 Output: fullInstallPath,
1105 OrderOnly: Paths{srcPath},
Colin Cross6510f912017-11-29 00:27:14 -08001106 Default: !a.Config().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -08001107 Args: map[string]string{
1108 "fromPath": srcPath.String(),
1109 },
1110 })
Colin Cross3854a602016-01-11 12:49:11 -08001111
Colin Cross12fc4972016-01-11 12:49:11 -08001112 a.installFiles = append(a.installFiles, fullInstallPath)
1113 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1114 }
Colin Cross3854a602016-01-11 12:49:11 -08001115 return fullInstallPath
1116}
1117
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001118func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001119 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1120}
1121
Colin Cross3f40fa42015-01-30 17:27:36 -08001122type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001123 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001124}
1125
1126func isFileInstaller(m blueprint.Module) bool {
1127 _, ok := m.(fileInstaller)
1128 return ok
1129}
1130
1131func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -07001132 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -08001133 return ok
1134}
Colin Crossfce53272015-04-08 11:21:40 -07001135
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001136func findStringInSlice(str string, slice []string) int {
1137 for i, s := range slice {
1138 if s == str {
1139 return i
Colin Crossfce53272015-04-08 11:21:40 -07001140 }
1141 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001142 return -1
1143}
1144
Colin Cross068e0fe2016-12-13 15:23:47 -08001145func SrcIsModule(s string) string {
1146 if len(s) > 1 && s[0] == ':' {
1147 return s[1:]
1148 }
1149 return ""
1150}
1151
1152type sourceDependencyTag struct {
1153 blueprint.BaseDependencyTag
1154}
1155
1156var SourceDepTag sourceDependencyTag
1157
Colin Cross366938f2017-12-11 16:29:02 -08001158// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
1159// using ":module" syntax, if any.
Colin Cross068e0fe2016-12-13 15:23:47 -08001160func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1161 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001162 set := make(map[string]bool)
1163
Colin Cross068e0fe2016-12-13 15:23:47 -08001164 for _, s := range srcFiles {
1165 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001166 if _, found := set[m]; found {
1167 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1168 } else {
1169 set[m] = true
1170 deps = append(deps, m)
1171 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001172 }
1173 }
1174
1175 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1176}
1177
Colin Cross366938f2017-12-11 16:29:02 -08001178// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
1179// using ":module" syntax, if any.
1180func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
1181 if s != nil {
1182 if m := SrcIsModule(*s); m != "" {
1183 ctx.AddDependency(ctx.Module(), SourceDepTag, m)
1184 }
1185 }
1186}
1187
Colin Cross068e0fe2016-12-13 15:23:47 -08001188type SourceFileProducer interface {
1189 Srcs() Paths
1190}
1191
1192// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001193// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001194func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001195 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1196}
1197
Colin Cross366938f2017-12-11 16:29:02 -08001198// Returns a single path expanded from globs and modules referenced using ":module" syntax.
1199// ExtractSourceDeps must have already been called during the dependency resolution phase.
1200func (ctx *androidModuleContext) ExpandSource(srcFile, prop string) Path {
1201 srcFiles := ctx.ExpandSourcesSubDir([]string{srcFile}, nil, "")
1202 if len(srcFiles) == 1 {
1203 return srcFiles[0]
1204 } else {
1205 ctx.PropertyErrorf(prop, "module providing %s must produce exactly one file", prop)
1206 return nil
1207 }
1208}
1209
Colin Cross2383f3b2018-02-06 14:40:13 -08001210// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
1211// the srcFile is non-nil.
1212// ExtractSourceDeps must have already been called during the dependency resolution phase.
1213func (ctx *androidModuleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
1214 if srcFile != nil {
1215 return OptionalPathForPath(ctx.ExpandSource(*srcFile, prop))
1216 }
1217 return OptionalPath{}
1218}
1219
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001220func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001221 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001222
Colin Cross461b4452018-02-23 09:22:42 -08001223 var expandedExcludes []string
1224 if excludes != nil {
1225 expandedExcludes = make([]string, 0, len(excludes))
1226 }
Nan Zhang27e284d2018-02-09 21:03:53 +00001227
1228 for _, e := range excludes {
1229 if m := SrcIsModule(e); m != "" {
1230 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
1231 if module == nil {
1232 // Error will have been handled by ExtractSourcesDeps
1233 continue
1234 }
1235 if srcProducer, ok := module.(SourceFileProducer); ok {
1236 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
1237 } else {
1238 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1239 }
1240 } else {
1241 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001242 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001243 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001244 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001245 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001246 if m := SrcIsModule(s); m != "" {
1247 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001248 if module == nil {
1249 // Error will have been handled by ExtractSourcesDeps
1250 continue
1251 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001252 if srcProducer, ok := module.(SourceFileProducer); ok {
Nan Zhang27e284d2018-02-09 21:03:53 +00001253 moduleSrcs := srcProducer.Srcs()
1254 for _, e := range expandedExcludes {
1255 for j, ms := range moduleSrcs {
1256 if ms.String() == e {
1257 moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
1258 }
1259 }
1260 }
1261 expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001262 } else {
1263 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1264 }
1265 } else if pathtools.IsGlob(s) {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001266 globbedSrcFiles := ctx.GlobFiles(filepath.Join(prefix, s), expandedExcludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001267 for i, s := range globbedSrcFiles {
1268 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001269 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001270 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001271 } else {
Nan Zhang27e284d2018-02-09 21:03:53 +00001272 p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1273 j := findStringInSlice(p.String(), expandedExcludes)
1274 if j == -1 {
1275 expandedSrcFiles = append(expandedSrcFiles, p)
1276 }
1277
Colin Cross8f101b42015-06-17 15:09:06 -07001278 }
1279 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001280 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001281}
1282
Nan Zhang6d34b302017-02-04 17:47:46 -08001283func (ctx *androidModuleContext) RequiredModuleNames() []string {
1284 return ctx.module.base().commonProperties.Required
1285}
1286
Colin Cross7f19f372016-11-01 11:10:25 -07001287func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1288 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001289 if err != nil {
1290 ctx.ModuleErrorf("glob: %s", err.Error())
1291 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001292 return pathsForModuleSrcFromFullPath(ctx, ret, true)
Colin Crossfce53272015-04-08 11:21:40 -07001293}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001294
Nan Zhang581fd212018-01-10 16:06:12 -08001295func (ctx *androidModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001296 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Nan Zhang581fd212018-01-10 16:06:12 -08001297 if err != nil {
1298 ctx.ModuleErrorf("glob: %s", err.Error())
1299 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001300 return pathsForModuleSrcFromFullPath(ctx, ret, false)
Nan Zhang581fd212018-01-10 16:06:12 -08001301}
1302
Colin Cross463a90e2015-06-17 14:20:06 -07001303func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001304 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001305}
1306
Colin Cross0875c522017-11-28 17:34:01 -08001307func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001308 return &buildTargetSingleton{}
1309}
1310
Colin Cross87d8b562017-04-25 10:01:55 -07001311func parentDir(dir string) string {
1312 dir, _ = filepath.Split(dir)
1313 return filepath.Clean(dir)
1314}
1315
Colin Cross1f8c52b2015-06-16 16:38:17 -07001316type buildTargetSingleton struct{}
1317
Colin Cross0875c522017-11-28 17:34:01 -08001318func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1319 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001320
Colin Cross0875c522017-11-28 17:34:01 -08001321 mmTarget := func(dir string) WritablePath {
1322 return PathForPhony(ctx,
1323 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001324 }
1325
Colin Cross0875c522017-11-28 17:34:01 -08001326 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001327
Colin Cross0875c522017-11-28 17:34:01 -08001328 ctx.VisitAllModules(func(module Module) {
1329 blueprintDir := module.base().blueprintDir
1330 installTarget := module.base().installTarget
1331 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001332
Colin Cross0875c522017-11-28 17:34:01 -08001333 if checkbuildTarget != nil {
1334 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1335 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1336 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001337
Colin Cross0875c522017-11-28 17:34:01 -08001338 if installTarget != nil {
1339 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001340 }
1341 })
1342
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001343 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001344 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001345 suffix = "-soong"
1346 }
1347
Colin Cross1f8c52b2015-06-16 16:38:17 -07001348 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001349 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001350 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001351 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001352 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001353 })
1354
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001355 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001356 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001357 return
1358 }
1359
Colin Cross0875c522017-11-28 17:34:01 -08001360 sortedKeys := func(m map[string]Paths) []string {
1361 s := make([]string, 0, len(m))
1362 for k := range m {
1363 s = append(s, k)
1364 }
1365 sort.Strings(s)
1366 return s
1367 }
1368
Colin Cross87d8b562017-04-25 10:01:55 -07001369 // Ensure ancestor directories are in modulesInDir
1370 dirs := sortedKeys(modulesInDir)
1371 for _, dir := range dirs {
1372 dir := parentDir(dir)
1373 for dir != "." && dir != "/" {
1374 if _, exists := modulesInDir[dir]; exists {
1375 break
1376 }
1377 modulesInDir[dir] = nil
1378 dir = parentDir(dir)
1379 }
1380 }
1381
1382 // Make directories build their direct subdirectories
1383 dirs = sortedKeys(modulesInDir)
1384 for _, dir := range dirs {
1385 p := parentDir(dir)
1386 if p != "." && p != "/" {
1387 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1388 }
1389 }
1390
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001391 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1392 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1393 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001394 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001395 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001396 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001397 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001398 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001399 // HACK: checkbuild should be an optional build, but force it
1400 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001401 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001402 })
1403 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001404
1405 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1406 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001407 ctx.VisitAllModules(func(module Module) {
1408 if module.Enabled() {
1409 os := module.Target().Os
1410 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001411 }
1412 })
1413
Colin Cross0875c522017-11-28 17:34:01 -08001414 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001415 for os, deps := range osDeps {
1416 var className string
1417
1418 switch os.Class {
1419 case Host:
1420 className = "host"
1421 case HostCross:
1422 className = "host-cross"
1423 case Device:
1424 className = "target"
1425 default:
1426 continue
1427 }
1428
Colin Cross0875c522017-11-28 17:34:01 -08001429 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001430 osClass[className] = append(osClass[className], name)
1431
Colin Cross0875c522017-11-28 17:34:01 -08001432 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001433 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001434 Output: name,
1435 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001436 })
1437 }
1438
1439 // Wrap those into host|host-cross|target phony rules
1440 osClasses := sortedKeys(osClass)
1441 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001442 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001443 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001444 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001445 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001446 })
1447 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001448}
Colin Crossd779da42015-12-17 18:00:23 -08001449
1450type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001451 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -08001452 ctx interface {
1453 ModuleName(blueprint.Module) string
1454 ModuleSubDir(blueprint.Module) string
1455 }
1456}
1457
1458func (s AndroidModulesByName) Len() int { return len(s.slice) }
1459func (s AndroidModulesByName) Less(i, j int) bool {
1460 mi, mj := s.slice[i], s.slice[j]
1461 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1462
1463 if ni != nj {
1464 return ni < nj
1465 } else {
1466 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1467 }
1468}
1469func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }