blob: fba19179dcfd99d082302ba9ecfe2abea437bef1 [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
Nan Zhang6d34b302017-02-04 17:47:46 -0800127
128 RequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -0700129
130 // android.ModuleContext methods
131 // These are duplicated instead of embedded so that can eventually be wrapped to take an
132 // android.Module instead of a blueprint.Module
133 OtherModuleName(m blueprint.Module) string
134 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
135 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
136
137 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
138 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
139
140 ModuleSubDir() string
141
Colin Cross35143d02017-11-16 00:11:20 -0800142 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700143 VisitDirectDeps(visit func(Module))
Colin Crossee6143c2017-12-30 17:54:27 -0800144 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700145 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
146 VisitDepsDepthFirst(visit func(Module))
147 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
148 WalkDeps(visit func(Module, Module) bool)
Colin Cross3f68a132017-10-23 17:10:29 -0700149
Colin Cross0875c522017-11-28 17:34:01 -0800150 Variable(pctx PackageContext, name, value string)
151 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700152 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
153 // and performs more verification.
Colin Cross0875c522017-11-28 17:34:01 -0800154 Build(pctx PackageContext, params BuildParams)
Colin Cross3f68a132017-10-23 17:10:29 -0700155
Colin Cross0875c522017-11-28 17:34:01 -0800156 PrimaryModule() Module
157 FinalModule() Module
158 VisitAllModuleVariants(visit func(Module))
Colin Cross3f68a132017-10-23 17:10:29 -0700159
160 GetMissingDependencies() []string
Jeff Gaston088e29e2017-11-29 16:47:17 -0800161 Namespace() blueprint.Namespace
Colin Cross3f40fa42015-01-30 17:27:36 -0800162}
163
Colin Cross635c3b02016-05-18 15:37:25 -0700164type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800165 blueprint.Module
166
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700167 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
168 // but GenerateAndroidBuildActions also has access to Android-specific information.
169 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700170 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700171
Colin Cross1e676be2016-10-12 14:38:15 -0700172 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800173
Colin Cross635c3b02016-05-18 15:37:25 -0700174 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800175 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700176 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800177 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700178 InstallInSanitizerDir() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800179 SkipInstall()
Jiyong Park374510b2018-03-19 18:23:01 +0900180 ExportedToMake() bool
Colin Cross36242852017-06-23 15:06:31 -0700181
182 AddProperties(props ...interface{})
183 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700184
Colin Crossae887032017-10-23 17:16:14 -0700185 BuildParamsForTests() []BuildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800186}
187
Colin Crossfc754582016-05-17 16:34:16 -0700188type nameProperties struct {
189 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800190 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700191}
192
193type commonProperties struct {
Dan Willemsen0effe062015-11-30 16:06:01 -0800194 // emit build rules for this module
195 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800196
Colin Cross7d5136f2015-05-11 13:39:40 -0700197 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800198 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
199 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
200 // platform
Colin Cross7d716ba2017-11-01 10:38:29 -0700201 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700202
203 Target struct {
204 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700205 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700206 }
207 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700208 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700209 }
210 }
211
212 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800213
Dan Willemsen782a2d12015-12-21 14:55:28 -0800214 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700215 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800216
Colin Cross55708f32017-03-20 13:23:34 -0700217 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700218 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700219
Jiyong Park2db76922017-11-08 16:03:48 +0900220 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
221 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
222 // Use `soc_specific` instead for better meaning.
Colin Cross7d716ba2017-11-01 10:38:29 -0700223 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700224
Jiyong Park2db76922017-11-08 16:03:48 +0900225 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
226 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
227 Soc_specific *bool
228
229 // whether this module is specific to a device, not only for SoC, but also for off-chip
230 // peripherals. When set to true, it is installed into /odm (or /vendor/odm if odm partition
231 // does not exist, or /system/vendor/odm if both odm and vendor partitions do not exist).
232 // This implies `soc_specific:true`.
233 Device_specific *bool
234
235 // whether this module is specific to a software configuration of a product (e.g. country,
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900236 // network operator, etc). When set to true, it is installed into /product (or
237 // /system/product if product partition does not exist).
Jiyong Park2db76922017-11-08 16:03:48 +0900238 Product_specific *bool
239
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700240 // init.rc files to be installed if this module is installed
241 Init_rc []string
242
Steven Moreland57a23d22018-04-04 15:42:19 -0700243 // VINTF manifest fragments to be installed if this module is installed
244 Vintf_fragments []string
245
Chris Wolfe998306e2016-08-15 14:47:23 -0400246 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700247 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400248
Colin Cross5aac3622017-08-31 15:07:09 -0700249 // relative path to a file to include in the list of notices for the device
250 Notice *string
251
Colin Crossa1ad8d12016-06-01 17:09:44 -0700252 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700253 CompileTarget Target `blueprint:"mutated"`
254 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800255
256 // Set by InitAndroidModule
257 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700258 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700259
260 SkipInstall bool `blueprint:"mutated"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800261
262 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800263}
264
265type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700266 Host_supported *bool
267 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800268}
269
Colin Crossc472d572015-03-17 15:06:21 -0700270type Multilib string
271
272const (
Colin Cross6b4a32d2017-12-05 13:42:45 -0800273 MultilibBoth Multilib = "both"
274 MultilibFirst Multilib = "first"
275 MultilibCommon Multilib = "common"
276 MultilibCommonFirst Multilib = "common_first"
277 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700278)
279
Colin Crossa1ad8d12016-06-01 17:09:44 -0700280type HostOrDeviceSupported int
281
282const (
283 _ HostOrDeviceSupported = iota
284 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700285 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700286 DeviceSupported
287 HostAndDeviceSupported
288 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700289 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700290)
291
Jiyong Park2db76922017-11-08 16:03:48 +0900292type moduleKind int
293
294const (
295 platformModule moduleKind = iota
296 deviceSpecificModule
297 socSpecificModule
298 productSpecificModule
299)
300
301func (k moduleKind) String() string {
302 switch k {
303 case platformModule:
304 return "platform"
305 case deviceSpecificModule:
306 return "device-specific"
307 case socSpecificModule:
308 return "soc-specific"
309 case productSpecificModule:
310 return "product-specific"
311 default:
312 panic(fmt.Errorf("unknown module kind %d", k))
313 }
314}
315
Colin Cross36242852017-06-23 15:06:31 -0700316func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800317 base := m.base()
318 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700319
Colin Cross36242852017-06-23 15:06:31 -0700320 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700321 &base.nameProperties,
322 &base.commonProperties,
323 &base.variableProperties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700324 base.customizableProperties = m.GetProperties()
Colin Cross5049f022015-03-18 13:28:46 -0700325}
326
Colin Cross36242852017-06-23 15:06:31 -0700327func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
328 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700329
330 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800331 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700332 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700333 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800334
Dan Willemsen218f6562015-07-08 18:13:11 -0700335 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700336 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700337 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800338 }
339
Colin Cross36242852017-06-23 15:06:31 -0700340 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800341}
342
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800343// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800344// modules. It should be included as an anonymous field in every module
345// struct definition. InitAndroidModule should then be called from the module's
346// factory function, and the return values from InitAndroidModule should be
347// returned from the factory function.
348//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800349// The ModuleBase type is responsible for implementing the GenerateBuildActions
350// method to support the blueprint.Module interface. This method will then call
351// the module's GenerateAndroidBuildActions method once for each build variant
352// that is to be built. GenerateAndroidBuildActions is passed a
353// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800354// AndroidModuleContext exposes extra functionality specific to the Android build
355// system including details about the particular build variant that is to be
356// generated.
357//
358// For example:
359//
360// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800361// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800362// )
363//
364// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800365// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800366// properties struct {
367// MyProperty string
368// }
369// }
370//
Colin Cross36242852017-06-23 15:06:31 -0700371// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800372// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700373// m.AddProperties(&m.properties)
374// android.InitAndroidModule(m)
375// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800376// }
377//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800378// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800379// // Get the CPU architecture for the current build variant.
380// variantArch := ctx.Arch()
381//
382// // ...
383// }
Colin Cross635c3b02016-05-18 15:37:25 -0700384type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800385 // Putting the curiously recurring thing pointing to the thing that contains
386 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700387 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700388 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800389
Colin Crossfc754582016-05-17 16:34:16 -0700390 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700392 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 hostAndDeviceProperties hostAndDeviceProperties
394 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700395 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700396 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800397
398 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700399 installFiles Paths
400 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700401
402 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
403 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800404 installTarget WritablePath
405 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700406 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700407
Colin Cross178a5092016-09-13 13:42:32 -0700408 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700409
410 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700411
412 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700413 buildParams []BuildParams
Colin Cross36242852017-06-23 15:06:31 -0700414}
415
416func (a *ModuleBase) AddProperties(props ...interface{}) {
417 a.registerProps = append(a.registerProps, props...)
418}
419
420func (a *ModuleBase) GetProperties() []interface{} {
421 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800422}
423
Colin Crossae887032017-10-23 17:16:14 -0700424func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700425 return a.buildParams
426}
427
Colin Crossce75d2c2016-10-06 16:12:58 -0700428// Name returns the name of the module. It may be overridden by individual module types, for
429// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700430func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800431 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700432}
433
Colin Crossce75d2c2016-10-06 16:12:58 -0700434// BaseModuleName returns the name of the module as specified in the blueprints file.
435func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800436 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700437}
438
Colin Cross635c3b02016-05-18 15:37:25 -0700439func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800440 return a
441}
442
Colin Cross8b74d172016-09-13 09:59:14 -0700443func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700444 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700445 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700446}
447
Colin Crossa1ad8d12016-06-01 17:09:44 -0700448func (a *ModuleBase) Target() Target {
449 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800450}
451
Colin Cross8b74d172016-09-13 09:59:14 -0700452func (a *ModuleBase) TargetPrimary() bool {
453 return a.commonProperties.CompilePrimary
454}
455
Colin Crossa1ad8d12016-06-01 17:09:44 -0700456func (a *ModuleBase) Os() OsType {
457 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800458}
459
Colin Cross635c3b02016-05-18 15:37:25 -0700460func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700461 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800462}
463
Colin Cross635c3b02016-05-18 15:37:25 -0700464func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700465 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800466}
467
Dan Willemsen0b24c742016-10-04 15:13:37 -0700468func (a *ModuleBase) ArchSpecific() bool {
469 return a.commonProperties.ArchSpecific
470}
471
Colin Crossa1ad8d12016-06-01 17:09:44 -0700472func (a *ModuleBase) OsClassSupported() []OsClass {
473 switch a.commonProperties.HostOrDeviceSupported {
474 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700475 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700476 case HostSupportedNoCross:
477 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700478 case DeviceSupported:
479 return []OsClass{Device}
480 case HostAndDeviceSupported:
481 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700482 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700483 supported = append(supported, Host, HostCross)
484 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700485 if a.hostAndDeviceProperties.Device_supported == nil ||
486 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700487 supported = append(supported, Device)
488 }
489 return supported
490 default:
491 return nil
492 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800493}
494
Colin Cross635c3b02016-05-18 15:37:25 -0700495func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800496 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
497 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700498 (a.hostAndDeviceProperties.Device_supported == nil ||
499 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800500}
501
Jiyong Parkc678ad32018-04-10 13:07:10 +0900502func (a *ModuleBase) Platform() bool {
503 return !a.DeviceSpecific() && !a.SocSpecific() && !a.ProductSpecific()
504}
505
506func (a *ModuleBase) DeviceSpecific() bool {
507 return Bool(a.commonProperties.Device_specific)
508}
509
510func (a *ModuleBase) SocSpecific() bool {
511 return Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
512}
513
514func (a *ModuleBase) ProductSpecific() bool {
515 return Bool(a.commonProperties.Product_specific)
516}
517
Colin Cross635c3b02016-05-18 15:37:25 -0700518func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800519 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800520 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800521 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800522 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800523}
524
Colin Crossce75d2c2016-10-06 16:12:58 -0700525func (a *ModuleBase) SkipInstall() {
526 a.commonProperties.SkipInstall = true
527}
528
Jiyong Park374510b2018-03-19 18:23:01 +0900529func (a *ModuleBase) ExportedToMake() bool {
530 return a.commonProperties.NamespaceExportedToMake
531}
532
Colin Cross635c3b02016-05-18 15:37:25 -0700533func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700534 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800535
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700536 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800537 ctx.VisitDepsDepthFirstIf(isFileInstaller,
538 func(m blueprint.Module) {
539 fileInstaller := m.(fileInstaller)
540 files := fileInstaller.filesToInstall()
541 result = append(result, files...)
542 })
543
544 return result
545}
546
Colin Cross635c3b02016-05-18 15:37:25 -0700547func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800548 return a.installFiles
549}
550
Colin Cross635c3b02016-05-18 15:37:25 -0700551func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800552 return p.noAddressSanitizer
553}
554
Colin Cross635c3b02016-05-18 15:37:25 -0700555func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800556 return false
557}
558
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700559func (p *ModuleBase) InstallInSanitizerDir() bool {
560 return false
561}
562
Colin Cross0875c522017-11-28 17:34:01 -0800563func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700564 allInstalledFiles := Paths{}
565 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800566 ctx.VisitAllModuleVariants(func(module Module) {
567 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700568 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
569 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 })
571
Colin Cross0875c522017-11-28 17:34:01 -0800572 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700573
Jeff Gaston088e29e2017-11-29 16:47:17 -0800574 namespacePrefix := ctx.Namespace().(*Namespace).id
575 if namespacePrefix != "" {
576 namespacePrefix = namespacePrefix + "-"
577 }
578
Colin Cross3f40fa42015-01-30 17:27:36 -0800579 if len(allInstalledFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800580 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800581 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700582 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800583 Output: name,
584 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800585 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700586 })
587 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700588 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700589 }
590
591 if len(allCheckbuildFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800592 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800593 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700594 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800595 Output: name,
596 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700597 })
598 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700599 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700600 }
601
602 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800603 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800604 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800605 suffix = "-soong"
606 }
607
Jeff Gaston088e29e2017-11-29 16:47:17 -0800608 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800609 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700610 Rule: blueprint.Phony,
Jeff Gaston088e29e2017-11-29 16:47:17 -0800611 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700612 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800613 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700614
615 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800616 }
617}
618
Jiyong Park2db76922017-11-08 16:03:48 +0900619func determineModuleKind(a *ModuleBase, ctx blueprint.BaseModuleContext) moduleKind {
620 var socSpecific = Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
621 var deviceSpecific = Bool(a.commonProperties.Device_specific)
622 var productSpecific = Bool(a.commonProperties.Product_specific)
623
624 if ((socSpecific || deviceSpecific) && productSpecific) || (socSpecific && deviceSpecific) {
625 msg := "conflicting value set here"
626 if productSpecific {
627 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
628 if deviceSpecific {
629 ctx.PropertyErrorf("device_specific", msg)
630 }
631 } else {
632 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
633 }
634 if Bool(a.commonProperties.Vendor) {
635 ctx.PropertyErrorf("vendor", msg)
636 }
637 if Bool(a.commonProperties.Proprietary) {
638 ctx.PropertyErrorf("proprietary", msg)
639 }
640 if Bool(a.commonProperties.Soc_specific) {
641 ctx.PropertyErrorf("soc_specific", msg)
642 }
643 }
644
645 if productSpecific {
646 return productSpecificModule
647 } else if deviceSpecific {
648 return deviceSpecificModule
649 } else if socSpecific {
650 return socSpecificModule
651 } else {
652 return platformModule
653 }
654}
655
Colin Cross635c3b02016-05-18 15:37:25 -0700656func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700657 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700658 target: a.commonProperties.CompileTarget,
659 targetPrimary: a.commonProperties.CompilePrimary,
Jiyong Park2db76922017-11-08 16:03:48 +0900660 kind: determineModuleKind(a, ctx),
Colin Cross8b74d172016-09-13 09:59:14 -0700661 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800662 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800663}
664
Colin Cross0875c522017-11-28 17:34:01 -0800665func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
666 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700667 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800668 ModuleContext: blueprintCtx,
669 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
670 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700671 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800672 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800673 }
674
Colin Cross67a5c132017-05-09 13:45:28 -0700675 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
676 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800677 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
678 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700679 }
Colin Cross0875c522017-11-28 17:34:01 -0800680 if !ctx.PrimaryArch() {
681 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700682 }
683
684 ctx.Variable(pctx, "moduleDesc", desc)
685
686 s := ""
687 if len(suffix) > 0 {
688 s = " [" + strings.Join(suffix, " ") + "]"
689 }
690 ctx.Variable(pctx, "moduleDescSuffix", s)
691
Colin Cross9b1d13d2016-09-19 15:18:11 -0700692 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800693 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700694 if ctx.Failed() {
695 return
696 }
697
Colin Cross0875c522017-11-28 17:34:01 -0800698 a.installFiles = append(a.installFiles, ctx.installFiles...)
699 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800700 }
701
Colin Cross9b1d13d2016-09-19 15:18:11 -0700702 if a == ctx.FinalModule().(Module).base() {
703 a.generateModuleTarget(ctx)
704 if ctx.Failed() {
705 return
706 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800707 }
Colin Crosscec81712017-07-13 14:43:27 -0700708
Colin Cross0875c522017-11-28 17:34:01 -0800709 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800710}
711
Colin Crossf6566ed2015-03-24 11:13:38 -0700712type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700713 target Target
714 targetPrimary bool
715 debug bool
Jiyong Park2db76922017-11-08 16:03:48 +0900716 kind moduleKind
Colin Cross8b74d172016-09-13 09:59:14 -0700717 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700718}
719
Colin Cross3f40fa42015-01-30 17:27:36 -0800720type androidModuleContext struct {
721 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700722 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700723 installDeps Paths
724 installFiles Paths
725 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800726 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700727 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700728
729 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700730 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800731}
732
Colin Cross67a5c132017-05-09 13:45:28 -0700733func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800734 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700735 Rule: ErrorRule,
736 Description: desc,
737 Outputs: outputs,
738 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800739 Args: map[string]string{
740 "error": err.Error(),
741 },
742 })
743 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800744}
745
Colin Crossaabf6792017-11-29 00:27:14 -0800746func (a *androidModuleContext) Config() Config {
747 return a.ModuleContext.Config().(Config)
748}
749
Colin Cross0875c522017-11-28 17:34:01 -0800750func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700751 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800752}
753
Colin Cross0875c522017-11-28 17:34:01 -0800754func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700755 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700756 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800757 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800758 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700759 Outputs: params.Outputs.Strings(),
760 ImplicitOutputs: params.ImplicitOutputs.Strings(),
761 Inputs: params.Inputs.Strings(),
762 Implicits: params.Implicits.Strings(),
763 OrderOnly: params.OrderOnly.Strings(),
764 Args: params.Args,
765 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700766 }
767
Colin Cross33bfb0a2016-11-21 17:23:08 -0800768 if params.Depfile != nil {
769 bparams.Depfile = params.Depfile.String()
770 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700771 if params.Output != nil {
772 bparams.Outputs = append(bparams.Outputs, params.Output.String())
773 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700774 if params.ImplicitOutput != nil {
775 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
776 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700777 if params.Input != nil {
778 bparams.Inputs = append(bparams.Inputs, params.Input.String())
779 }
780 if params.Implicit != nil {
781 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
782 }
783
Colin Cross0875c522017-11-28 17:34:01 -0800784 return bparams
785}
786
787func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
788 a.ModuleContext.Variable(pctx.PackageContext, name, value)
789}
790
791func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
792 argNames ...string) blueprint.Rule {
793
794 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
795}
796
797func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
798 if a.config.captureBuild {
799 a.buildParams = append(a.buildParams, params)
800 }
801
802 bparams := convertBuildParams(params)
803
804 if bparams.Description != "" {
805 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
806 }
807
Colin Cross6ff51382015-12-17 16:39:19 -0800808 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700809 a.ninjaError(bparams.Description, bparams.Outputs,
810 fmt.Errorf("module %s missing dependencies: %s\n",
811 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800812 return
813 }
814
Colin Cross0875c522017-11-28 17:34:01 -0800815 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700816}
817
Colin Cross6ff51382015-12-17 16:39:19 -0800818func (a *androidModuleContext) GetMissingDependencies() []string {
819 return a.missingDeps
820}
821
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800822func (a *androidModuleContext) AddMissingDependencies(deps []string) {
823 if deps != nil {
824 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700825 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800826 }
827}
828
Colin Crossd11fcda2017-10-23 17:59:01 -0700829func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
830 aModule, _ := module.(Module)
831 if aModule == nil {
832 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
833 return nil
834 }
835
836 if !aModule.Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800837 if a.Config().AllowMissingDependencies() {
Colin Crossd11fcda2017-10-23 17:59:01 -0700838 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
839 } else {
840 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
841 }
842 return nil
843 }
844
845 return aModule
846}
847
Colin Cross35143d02017-11-16 00:11:20 -0800848func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
849 a.ModuleContext.VisitDirectDeps(visit)
850}
851
Colin Crossd11fcda2017-10-23 17:59:01 -0700852func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
853 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
854 if aModule := a.validateAndroidModule(module); aModule != nil {
855 visit(aModule)
856 }
857 })
858}
859
Colin Crossee6143c2017-12-30 17:54:27 -0800860func (a *androidModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
861 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
862 if aModule := a.validateAndroidModule(module); aModule != nil {
863 if a.ModuleContext.OtherModuleDependencyTag(aModule) == tag {
864 visit(aModule)
865 }
866 }
867 })
868}
869
Colin Crossd11fcda2017-10-23 17:59:01 -0700870func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
871 a.ModuleContext.VisitDirectDepsIf(
872 // pred
873 func(module blueprint.Module) bool {
874 if aModule := a.validateAndroidModule(module); aModule != nil {
875 return pred(aModule)
876 } else {
877 return false
878 }
879 },
880 // visit
881 func(module blueprint.Module) {
882 visit(module.(Module))
883 })
884}
885
886func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
887 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
888 if aModule := a.validateAndroidModule(module); aModule != nil {
889 visit(aModule)
890 }
891 })
892}
893
894func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
895 a.ModuleContext.VisitDepsDepthFirstIf(
896 // pred
897 func(module blueprint.Module) bool {
898 if aModule := a.validateAndroidModule(module); aModule != nil {
899 return pred(aModule)
900 } else {
901 return false
902 }
903 },
904 // visit
905 func(module blueprint.Module) {
906 visit(module.(Module))
907 })
908}
909
910func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
911 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
912 childAndroidModule := a.validateAndroidModule(child)
913 parentAndroidModule := a.validateAndroidModule(parent)
914 if childAndroidModule != nil && parentAndroidModule != nil {
915 return visit(childAndroidModule, parentAndroidModule)
916 } else {
917 return false
918 }
919 })
920}
921
Colin Cross0875c522017-11-28 17:34:01 -0800922func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
923 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
924 visit(module.(Module))
925 })
926}
927
928func (a *androidModuleContext) PrimaryModule() Module {
929 return a.ModuleContext.PrimaryModule().(Module)
930}
931
932func (a *androidModuleContext) FinalModule() Module {
933 return a.ModuleContext.FinalModule().(Module)
934}
935
Colin Crossa1ad8d12016-06-01 17:09:44 -0700936func (a *androidBaseContextImpl) Target() Target {
937 return a.target
938}
939
Colin Cross8b74d172016-09-13 09:59:14 -0700940func (a *androidBaseContextImpl) TargetPrimary() bool {
941 return a.targetPrimary
942}
943
Colin Crossf6566ed2015-03-24 11:13:38 -0700944func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700945 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800946}
947
Colin Crossa1ad8d12016-06-01 17:09:44 -0700948func (a *androidBaseContextImpl) Os() OsType {
949 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800950}
951
Colin Crossf6566ed2015-03-24 11:13:38 -0700952func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700953 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700954}
955
956func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700957 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700958}
959
Colin Cross0af4b842015-04-30 16:36:18 -0700960func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700961 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700962}
963
Colin Cross3edeee12017-04-04 12:59:48 -0700964func (a *androidBaseContextImpl) Windows() bool {
965 return a.target.Os == Windows
966}
967
Colin Crossf6566ed2015-03-24 11:13:38 -0700968func (a *androidBaseContextImpl) Debug() bool {
969 return a.debug
970}
971
Colin Cross1e7d3702016-08-24 15:25:47 -0700972func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700973 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
974 return true
975 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700976 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
977}
978
Colin Cross1332b002015-04-07 17:11:30 -0700979func (a *androidBaseContextImpl) AConfig() Config {
980 return a.config
981}
982
Colin Cross9272ade2016-08-17 15:24:12 -0700983func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
984 return DeviceConfig{a.config.deviceConfig}
985}
986
Jiyong Park2db76922017-11-08 16:03:48 +0900987func (a *androidBaseContextImpl) Platform() bool {
988 return a.kind == platformModule
989}
990
991func (a *androidBaseContextImpl) DeviceSpecific() bool {
992 return a.kind == deviceSpecificModule
993}
994
995func (a *androidBaseContextImpl) SocSpecific() bool {
996 return a.kind == socSpecificModule
997}
998
999func (a *androidBaseContextImpl) ProductSpecific() bool {
1000 return a.kind == productSpecificModule
Dan Willemsen782a2d12015-12-21 14:55:28 -08001001}
1002
Colin Cross8d8f8e22016-08-03 11:57:50 -07001003func (a *androidModuleContext) InstallInData() bool {
1004 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08001005}
1006
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001007func (a *androidModuleContext) InstallInSanitizerDir() bool {
1008 return a.module.InstallInSanitizerDir()
1009}
1010
Colin Cross893d8162017-04-26 17:34:03 -07001011func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
1012 if a.module.base().commonProperties.SkipInstall {
1013 return true
1014 }
1015
Colin Cross3607f212018-05-07 15:28:05 -07001016 // We'll need a solution for choosing which of modules with the same name in different
1017 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
1018 // list of namespaces to install in a Soong-only build.
1019 if !a.module.base().commonProperties.NamespaceExportedToMake {
1020 return true
1021 }
1022
Colin Cross893d8162017-04-26 17:34:03 -07001023 if a.Device() {
Colin Cross6510f912017-11-29 00:27:14 -08001024 if a.Config().SkipDeviceInstall() {
Colin Cross893d8162017-04-26 17:34:03 -07001025 return true
1026 }
1027
Colin Cross6510f912017-11-29 00:27:14 -08001028 if a.Config().SkipMegaDeviceInstall(fullInstallPath.String()) {
Colin Cross893d8162017-04-26 17:34:03 -07001029 return true
1030 }
1031 }
1032
1033 return false
1034}
1035
Colin Cross5c517922017-08-31 12:29:17 -07001036func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -07001037 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -07001038 return a.installFile(installPath, name, srcPath, Cp, deps)
1039}
1040
1041func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
1042 deps ...Path) OutputPath {
1043 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
1044}
1045
1046func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
1047 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -07001048
Dan Willemsen782a2d12015-12-21 14:55:28 -08001049 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001050 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001051
Colin Cross893d8162017-04-26 17:34:03 -07001052 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001053
Dan Willemsen322acaf2016-01-12 23:07:05 -08001054 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -07001055
Colin Cross89562dc2016-10-03 17:47:19 -07001056 var implicitDeps, orderOnlyDeps Paths
1057
1058 if a.Host() {
1059 // Installed host modules might be used during the build, depend directly on their
1060 // dependencies so their timestamp is updated whenever their dependency is updated
1061 implicitDeps = deps
1062 } else {
1063 orderOnlyDeps = deps
1064 }
1065
Colin Crossae887032017-10-23 17:16:14 -07001066 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07001067 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07001068 Description: "install " + fullInstallPath.Base(),
1069 Output: fullInstallPath,
1070 Input: srcPath,
1071 Implicits: implicitDeps,
1072 OrderOnly: orderOnlyDeps,
Colin Cross6510f912017-11-29 00:27:14 -08001073 Default: !a.Config().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08001074 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001075
Dan Willemsen322acaf2016-01-12 23:07:05 -08001076 a.installFiles = append(a.installFiles, fullInstallPath)
1077 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001078 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -07001079 return fullInstallPath
1080}
1081
Colin Cross3854a602016-01-11 12:49:11 -08001082func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
1083 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001084 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08001085
Colin Cross893d8162017-04-26 17:34:03 -07001086 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001087
Colin Crossae887032017-10-23 17:16:14 -07001088 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001089 Rule: Symlink,
1090 Description: "install symlink " + fullInstallPath.Base(),
1091 Output: fullInstallPath,
1092 OrderOnly: Paths{srcPath},
Colin Cross6510f912017-11-29 00:27:14 -08001093 Default: !a.Config().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -08001094 Args: map[string]string{
1095 "fromPath": srcPath.String(),
1096 },
1097 })
Colin Cross3854a602016-01-11 12:49:11 -08001098
Colin Cross12fc4972016-01-11 12:49:11 -08001099 a.installFiles = append(a.installFiles, fullInstallPath)
1100 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1101 }
Colin Cross3854a602016-01-11 12:49:11 -08001102 return fullInstallPath
1103}
1104
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001105func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001106 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1107}
1108
Colin Cross3f40fa42015-01-30 17:27:36 -08001109type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001110 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001111}
1112
1113func isFileInstaller(m blueprint.Module) bool {
1114 _, ok := m.(fileInstaller)
1115 return ok
1116}
1117
1118func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -07001119 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -08001120 return ok
1121}
Colin Crossfce53272015-04-08 11:21:40 -07001122
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001123func findStringInSlice(str string, slice []string) int {
1124 for i, s := range slice {
1125 if s == str {
1126 return i
Colin Crossfce53272015-04-08 11:21:40 -07001127 }
1128 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001129 return -1
1130}
1131
Colin Cross068e0fe2016-12-13 15:23:47 -08001132func SrcIsModule(s string) string {
1133 if len(s) > 1 && s[0] == ':' {
1134 return s[1:]
1135 }
1136 return ""
1137}
1138
1139type sourceDependencyTag struct {
1140 blueprint.BaseDependencyTag
1141}
1142
1143var SourceDepTag sourceDependencyTag
1144
Colin Cross366938f2017-12-11 16:29:02 -08001145// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
1146// using ":module" syntax, if any.
Colin Cross068e0fe2016-12-13 15:23:47 -08001147func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1148 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001149 set := make(map[string]bool)
1150
Colin Cross068e0fe2016-12-13 15:23:47 -08001151 for _, s := range srcFiles {
1152 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001153 if _, found := set[m]; found {
1154 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1155 } else {
1156 set[m] = true
1157 deps = append(deps, m)
1158 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001159 }
1160 }
1161
1162 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1163}
1164
Colin Cross366938f2017-12-11 16:29:02 -08001165// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
1166// using ":module" syntax, if any.
1167func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
1168 if s != nil {
1169 if m := SrcIsModule(*s); m != "" {
1170 ctx.AddDependency(ctx.Module(), SourceDepTag, m)
1171 }
1172 }
1173}
1174
Colin Cross068e0fe2016-12-13 15:23:47 -08001175type SourceFileProducer interface {
1176 Srcs() Paths
1177}
1178
1179// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001180// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001181func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001182 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1183}
1184
Colin Cross366938f2017-12-11 16:29:02 -08001185// Returns a single path expanded from globs and modules referenced using ":module" syntax.
1186// ExtractSourceDeps must have already been called during the dependency resolution phase.
1187func (ctx *androidModuleContext) ExpandSource(srcFile, prop string) Path {
1188 srcFiles := ctx.ExpandSourcesSubDir([]string{srcFile}, nil, "")
1189 if len(srcFiles) == 1 {
1190 return srcFiles[0]
1191 } else {
1192 ctx.PropertyErrorf(prop, "module providing %s must produce exactly one file", prop)
1193 return nil
1194 }
1195}
1196
Colin Cross2383f3b2018-02-06 14:40:13 -08001197// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
1198// the srcFile is non-nil.
1199// ExtractSourceDeps must have already been called during the dependency resolution phase.
1200func (ctx *androidModuleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
1201 if srcFile != nil {
1202 return OptionalPathForPath(ctx.ExpandSource(*srcFile, prop))
1203 }
1204 return OptionalPath{}
1205}
1206
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001207func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001208 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001209
Colin Cross461b4452018-02-23 09:22:42 -08001210 var expandedExcludes []string
1211 if excludes != nil {
1212 expandedExcludes = make([]string, 0, len(excludes))
1213 }
Nan Zhang27e284d2018-02-09 21:03:53 +00001214
1215 for _, e := range excludes {
1216 if m := SrcIsModule(e); m != "" {
1217 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
1218 if module == nil {
1219 // Error will have been handled by ExtractSourcesDeps
1220 continue
1221 }
1222 if srcProducer, ok := module.(SourceFileProducer); ok {
1223 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
1224 } else {
1225 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1226 }
1227 } else {
1228 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001229 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001230 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001231 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001232 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001233 if m := SrcIsModule(s); m != "" {
1234 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001235 if module == nil {
1236 // Error will have been handled by ExtractSourcesDeps
1237 continue
1238 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001239 if srcProducer, ok := module.(SourceFileProducer); ok {
Nan Zhang27e284d2018-02-09 21:03:53 +00001240 moduleSrcs := srcProducer.Srcs()
1241 for _, e := range expandedExcludes {
1242 for j, ms := range moduleSrcs {
1243 if ms.String() == e {
1244 moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
1245 }
1246 }
1247 }
1248 expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001249 } else {
1250 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1251 }
1252 } else if pathtools.IsGlob(s) {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001253 globbedSrcFiles := ctx.GlobFiles(filepath.Join(prefix, s), expandedExcludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001254 for i, s := range globbedSrcFiles {
1255 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001256 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001257 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001258 } else {
Nan Zhang27e284d2018-02-09 21:03:53 +00001259 p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1260 j := findStringInSlice(p.String(), expandedExcludes)
1261 if j == -1 {
1262 expandedSrcFiles = append(expandedSrcFiles, p)
1263 }
1264
Colin Cross8f101b42015-06-17 15:09:06 -07001265 }
1266 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001267 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001268}
1269
Nan Zhang6d34b302017-02-04 17:47:46 -08001270func (ctx *androidModuleContext) RequiredModuleNames() []string {
1271 return ctx.module.base().commonProperties.Required
1272}
1273
Colin Cross7f19f372016-11-01 11:10:25 -07001274func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1275 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001276 if err != nil {
1277 ctx.ModuleErrorf("glob: %s", err.Error())
1278 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001279 return pathsForModuleSrcFromFullPath(ctx, ret, true)
Colin Crossfce53272015-04-08 11:21:40 -07001280}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001281
Nan Zhang581fd212018-01-10 16:06:12 -08001282func (ctx *androidModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001283 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Nan Zhang581fd212018-01-10 16:06:12 -08001284 if err != nil {
1285 ctx.ModuleErrorf("glob: %s", err.Error())
1286 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001287 return pathsForModuleSrcFromFullPath(ctx, ret, false)
Nan Zhang581fd212018-01-10 16:06:12 -08001288}
1289
Colin Cross463a90e2015-06-17 14:20:06 -07001290func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001291 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001292}
1293
Colin Cross0875c522017-11-28 17:34:01 -08001294func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001295 return &buildTargetSingleton{}
1296}
1297
Colin Cross87d8b562017-04-25 10:01:55 -07001298func parentDir(dir string) string {
1299 dir, _ = filepath.Split(dir)
1300 return filepath.Clean(dir)
1301}
1302
Colin Cross1f8c52b2015-06-16 16:38:17 -07001303type buildTargetSingleton struct{}
1304
Colin Cross0875c522017-11-28 17:34:01 -08001305func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1306 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001307
Colin Cross0875c522017-11-28 17:34:01 -08001308 mmTarget := func(dir string) WritablePath {
1309 return PathForPhony(ctx,
1310 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001311 }
1312
Colin Cross0875c522017-11-28 17:34:01 -08001313 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001314
Colin Cross0875c522017-11-28 17:34:01 -08001315 ctx.VisitAllModules(func(module Module) {
1316 blueprintDir := module.base().blueprintDir
1317 installTarget := module.base().installTarget
1318 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001319
Colin Cross0875c522017-11-28 17:34:01 -08001320 if checkbuildTarget != nil {
1321 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1322 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1323 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001324
Colin Cross0875c522017-11-28 17:34:01 -08001325 if installTarget != nil {
1326 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001327 }
1328 })
1329
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001330 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001331 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001332 suffix = "-soong"
1333 }
1334
Colin Cross1f8c52b2015-06-16 16:38:17 -07001335 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001336 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001337 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001338 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001339 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001340 })
1341
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001342 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001343 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001344 return
1345 }
1346
Colin Cross0875c522017-11-28 17:34:01 -08001347 sortedKeys := func(m map[string]Paths) []string {
1348 s := make([]string, 0, len(m))
1349 for k := range m {
1350 s = append(s, k)
1351 }
1352 sort.Strings(s)
1353 return s
1354 }
1355
Colin Cross87d8b562017-04-25 10:01:55 -07001356 // Ensure ancestor directories are in modulesInDir
1357 dirs := sortedKeys(modulesInDir)
1358 for _, dir := range dirs {
1359 dir := parentDir(dir)
1360 for dir != "." && dir != "/" {
1361 if _, exists := modulesInDir[dir]; exists {
1362 break
1363 }
1364 modulesInDir[dir] = nil
1365 dir = parentDir(dir)
1366 }
1367 }
1368
1369 // Make directories build their direct subdirectories
1370 dirs = sortedKeys(modulesInDir)
1371 for _, dir := range dirs {
1372 p := parentDir(dir)
1373 if p != "." && p != "/" {
1374 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1375 }
1376 }
1377
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001378 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1379 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1380 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001381 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001382 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001383 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001384 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001385 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001386 // HACK: checkbuild should be an optional build, but force it
1387 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001388 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001389 })
1390 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001391
1392 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1393 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001394 ctx.VisitAllModules(func(module Module) {
1395 if module.Enabled() {
1396 os := module.Target().Os
1397 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001398 }
1399 })
1400
Colin Cross0875c522017-11-28 17:34:01 -08001401 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001402 for os, deps := range osDeps {
1403 var className string
1404
1405 switch os.Class {
1406 case Host:
1407 className = "host"
1408 case HostCross:
1409 className = "host-cross"
1410 case Device:
1411 className = "target"
1412 default:
1413 continue
1414 }
1415
Colin Cross0875c522017-11-28 17:34:01 -08001416 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001417 osClass[className] = append(osClass[className], name)
1418
Colin Cross0875c522017-11-28 17:34:01 -08001419 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001420 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001421 Output: name,
1422 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001423 })
1424 }
1425
1426 // Wrap those into host|host-cross|target phony rules
1427 osClasses := sortedKeys(osClass)
1428 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001429 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001430 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001431 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001432 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001433 })
1434 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001435}
Colin Crossd779da42015-12-17 18:00:23 -08001436
1437type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001438 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -08001439 ctx interface {
1440 ModuleName(blueprint.Module) string
1441 ModuleSubDir(blueprint.Module) string
1442 }
1443}
1444
1445func (s AndroidModulesByName) Len() int { return len(s.slice) }
1446func (s AndroidModulesByName) Less(i, j int) bool {
1447 mi, mj := s.slice[i], s.slice[j]
1448 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1449
1450 if ni != nj {
1451 return ni < nj
1452 } else {
1453 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1454 }
1455}
1456func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }