blob: 4797c0c230a4413a21ce0e2edc4b9b75e9d783eb [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 {
Colin Crossc77f9d12015-04-02 13:54:39 -0700194 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800195
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
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700242 // init.rc files to be installed if this module is installed
243 Init_rc []string
244
Steven Moreland57a23d22018-04-04 15:42:19 -0700245 // VINTF manifest fragments to be installed if this module is installed
246 Vintf_fragments []string
247
Chris Wolfe998306e2016-08-15 14:47:23 -0400248 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700249 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400250
Colin Cross5aac3622017-08-31 15:07:09 -0700251 // relative path to a file to include in the list of notices for the device
252 Notice *string
253
Colin Crossa1ad8d12016-06-01 17:09:44 -0700254 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700255 CompileTarget Target `blueprint:"mutated"`
256 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800257
258 // Set by InitAndroidModule
259 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700260 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700261
262 SkipInstall bool `blueprint:"mutated"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800263
264 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800265}
266
267type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700268 Host_supported *bool
269 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800270}
271
Colin Crossc472d572015-03-17 15:06:21 -0700272type Multilib string
273
274const (
Colin Cross6b4a32d2017-12-05 13:42:45 -0800275 MultilibBoth Multilib = "both"
276 MultilibFirst Multilib = "first"
277 MultilibCommon Multilib = "common"
278 MultilibCommonFirst Multilib = "common_first"
279 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700280)
281
Colin Crossa1ad8d12016-06-01 17:09:44 -0700282type HostOrDeviceSupported int
283
284const (
285 _ HostOrDeviceSupported = iota
286 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700287 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700288 DeviceSupported
289 HostAndDeviceSupported
290 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700291 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700292)
293
Jiyong Park2db76922017-11-08 16:03:48 +0900294type moduleKind int
295
296const (
297 platformModule moduleKind = iota
298 deviceSpecificModule
299 socSpecificModule
300 productSpecificModule
301)
302
303func (k moduleKind) String() string {
304 switch k {
305 case platformModule:
306 return "platform"
307 case deviceSpecificModule:
308 return "device-specific"
309 case socSpecificModule:
310 return "soc-specific"
311 case productSpecificModule:
312 return "product-specific"
313 default:
314 panic(fmt.Errorf("unknown module kind %d", k))
315 }
316}
317
Colin Cross36242852017-06-23 15:06:31 -0700318func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800319 base := m.base()
320 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700321
Colin Cross36242852017-06-23 15:06:31 -0700322 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700323 &base.nameProperties,
324 &base.commonProperties,
325 &base.variableProperties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700326 base.customizableProperties = m.GetProperties()
Colin Cross5049f022015-03-18 13:28:46 -0700327}
328
Colin Cross36242852017-06-23 15:06:31 -0700329func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
330 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700331
332 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800333 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700334 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700335 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800336
Dan Willemsen218f6562015-07-08 18:13:11 -0700337 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700338 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700339 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800340 }
341
Colin Cross36242852017-06-23 15:06:31 -0700342 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800343}
344
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800345// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800346// modules. It should be included as an anonymous field in every module
347// struct definition. InitAndroidModule should then be called from the module's
348// factory function, and the return values from InitAndroidModule should be
349// returned from the factory function.
350//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800351// The ModuleBase type is responsible for implementing the GenerateBuildActions
352// method to support the blueprint.Module interface. This method will then call
353// the module's GenerateAndroidBuildActions method once for each build variant
354// that is to be built. GenerateAndroidBuildActions is passed a
355// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800356// AndroidModuleContext exposes extra functionality specific to the Android build
357// system including details about the particular build variant that is to be
358// generated.
359//
360// For example:
361//
362// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800363// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800364// )
365//
366// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800367// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800368// properties struct {
369// MyProperty string
370// }
371// }
372//
Colin Cross36242852017-06-23 15:06:31 -0700373// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800374// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700375// m.AddProperties(&m.properties)
376// android.InitAndroidModule(m)
377// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800378// }
379//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800380// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800381// // Get the CPU architecture for the current build variant.
382// variantArch := ctx.Arch()
383//
384// // ...
385// }
Colin Cross635c3b02016-05-18 15:37:25 -0700386type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800387 // Putting the curiously recurring thing pointing to the thing that contains
388 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700389 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700390 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800391
Colin Crossfc754582016-05-17 16:34:16 -0700392 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700394 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800395 hostAndDeviceProperties hostAndDeviceProperties
396 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700397 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700398 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800399
400 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700401 installFiles Paths
402 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700403
404 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
405 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800406 installTarget WritablePath
407 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700408 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700409
Colin Cross178a5092016-09-13 13:42:32 -0700410 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700411
412 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700413
414 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700415 buildParams []BuildParams
Colin Cross36242852017-06-23 15:06:31 -0700416}
417
418func (a *ModuleBase) AddProperties(props ...interface{}) {
419 a.registerProps = append(a.registerProps, props...)
420}
421
422func (a *ModuleBase) GetProperties() []interface{} {
423 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800424}
425
Colin Crossae887032017-10-23 17:16:14 -0700426func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700427 return a.buildParams
428}
429
Colin Crossce75d2c2016-10-06 16:12:58 -0700430// Name returns the name of the module. It may be overridden by individual module types, for
431// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700432func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800433 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700434}
435
Colin Crossce75d2c2016-10-06 16:12:58 -0700436// BaseModuleName returns the name of the module as specified in the blueprints file.
437func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800438 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700439}
440
Colin Cross635c3b02016-05-18 15:37:25 -0700441func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800442 return a
443}
444
Colin Cross8b74d172016-09-13 09:59:14 -0700445func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700446 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700447 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700448}
449
Colin Crossa1ad8d12016-06-01 17:09:44 -0700450func (a *ModuleBase) Target() Target {
451 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800452}
453
Colin Cross8b74d172016-09-13 09:59:14 -0700454func (a *ModuleBase) TargetPrimary() bool {
455 return a.commonProperties.CompilePrimary
456}
457
Colin Crossa1ad8d12016-06-01 17:09:44 -0700458func (a *ModuleBase) Os() OsType {
459 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800460}
461
Colin Cross635c3b02016-05-18 15:37:25 -0700462func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700463 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800464}
465
Colin Cross635c3b02016-05-18 15:37:25 -0700466func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700467 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800468}
469
Dan Willemsen0b24c742016-10-04 15:13:37 -0700470func (a *ModuleBase) ArchSpecific() bool {
471 return a.commonProperties.ArchSpecific
472}
473
Colin Crossa1ad8d12016-06-01 17:09:44 -0700474func (a *ModuleBase) OsClassSupported() []OsClass {
475 switch a.commonProperties.HostOrDeviceSupported {
476 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700477 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700478 case HostSupportedNoCross:
479 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700480 case DeviceSupported:
481 return []OsClass{Device}
482 case HostAndDeviceSupported:
483 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700484 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700485 supported = append(supported, Host, HostCross)
486 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700487 if a.hostAndDeviceProperties.Device_supported == nil ||
488 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700489 supported = append(supported, Device)
490 }
491 return supported
492 default:
493 return nil
494 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800495}
496
Colin Cross635c3b02016-05-18 15:37:25 -0700497func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800498 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
499 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700500 (a.hostAndDeviceProperties.Device_supported == nil ||
501 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800502}
503
Jiyong Parkc678ad32018-04-10 13:07:10 +0900504func (a *ModuleBase) Platform() bool {
505 return !a.DeviceSpecific() && !a.SocSpecific() && !a.ProductSpecific()
506}
507
508func (a *ModuleBase) DeviceSpecific() bool {
509 return Bool(a.commonProperties.Device_specific)
510}
511
512func (a *ModuleBase) SocSpecific() bool {
513 return Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
514}
515
516func (a *ModuleBase) ProductSpecific() bool {
517 return Bool(a.commonProperties.Product_specific)
518}
519
Colin Cross635c3b02016-05-18 15:37:25 -0700520func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800521 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800522 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800523 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800524 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800525}
526
Colin Crossce75d2c2016-10-06 16:12:58 -0700527func (a *ModuleBase) SkipInstall() {
528 a.commonProperties.SkipInstall = true
529}
530
Jiyong Park374510b2018-03-19 18:23:01 +0900531func (a *ModuleBase) ExportedToMake() bool {
532 return a.commonProperties.NamespaceExportedToMake
533}
534
Colin Cross635c3b02016-05-18 15:37:25 -0700535func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700536 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800537
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700538 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800539 ctx.VisitDepsDepthFirstIf(isFileInstaller,
540 func(m blueprint.Module) {
541 fileInstaller := m.(fileInstaller)
542 files := fileInstaller.filesToInstall()
543 result = append(result, files...)
544 })
545
546 return result
547}
548
Colin Cross635c3b02016-05-18 15:37:25 -0700549func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800550 return a.installFiles
551}
552
Colin Cross635c3b02016-05-18 15:37:25 -0700553func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800554 return p.noAddressSanitizer
555}
556
Colin Cross635c3b02016-05-18 15:37:25 -0700557func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800558 return false
559}
560
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700561func (p *ModuleBase) InstallInSanitizerDir() bool {
562 return false
563}
564
Colin Cross0875c522017-11-28 17:34:01 -0800565func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700566 allInstalledFiles := Paths{}
567 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800568 ctx.VisitAllModuleVariants(func(module Module) {
569 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700570 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
571 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800572 })
573
Colin Cross0875c522017-11-28 17:34:01 -0800574 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700575
Jeff Gaston088e29e2017-11-29 16:47:17 -0800576 namespacePrefix := ctx.Namespace().(*Namespace).id
577 if namespacePrefix != "" {
578 namespacePrefix = namespacePrefix + "-"
579 }
580
Colin Cross3f40fa42015-01-30 17:27:36 -0800581 if len(allInstalledFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800582 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800583 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700584 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800585 Output: name,
586 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800587 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700588 })
589 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700590 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700591 }
592
593 if len(allCheckbuildFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800594 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800595 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700596 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800597 Output: name,
598 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700599 })
600 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700601 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700602 }
603
604 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800605 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800606 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800607 suffix = "-soong"
608 }
609
Jeff Gaston088e29e2017-11-29 16:47:17 -0800610 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800611 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700612 Rule: blueprint.Phony,
Jeff Gaston088e29e2017-11-29 16:47:17 -0800613 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700614 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800615 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700616
617 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800618 }
619}
620
Jiyong Park2db76922017-11-08 16:03:48 +0900621func determineModuleKind(a *ModuleBase, ctx blueprint.BaseModuleContext) moduleKind {
622 var socSpecific = Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
623 var deviceSpecific = Bool(a.commonProperties.Device_specific)
624 var productSpecific = Bool(a.commonProperties.Product_specific)
625
626 if ((socSpecific || deviceSpecific) && productSpecific) || (socSpecific && deviceSpecific) {
627 msg := "conflicting value set here"
628 if productSpecific {
629 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
630 if deviceSpecific {
631 ctx.PropertyErrorf("device_specific", msg)
632 }
633 } else {
634 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
635 }
636 if Bool(a.commonProperties.Vendor) {
637 ctx.PropertyErrorf("vendor", msg)
638 }
639 if Bool(a.commonProperties.Proprietary) {
640 ctx.PropertyErrorf("proprietary", msg)
641 }
642 if Bool(a.commonProperties.Soc_specific) {
643 ctx.PropertyErrorf("soc_specific", msg)
644 }
645 }
646
647 if productSpecific {
648 return productSpecificModule
649 } else if deviceSpecific {
650 return deviceSpecificModule
651 } else if socSpecific {
652 return socSpecificModule
653 } else {
654 return platformModule
655 }
656}
657
Colin Cross635c3b02016-05-18 15:37:25 -0700658func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700659 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700660 target: a.commonProperties.CompileTarget,
661 targetPrimary: a.commonProperties.CompilePrimary,
Jiyong Park2db76922017-11-08 16:03:48 +0900662 kind: determineModuleKind(a, ctx),
Colin Cross8b74d172016-09-13 09:59:14 -0700663 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800664 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800665}
666
Colin Cross0875c522017-11-28 17:34:01 -0800667func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
668 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700669 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800670 ModuleContext: blueprintCtx,
671 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
672 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700673 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800674 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800675 }
676
Colin Cross67a5c132017-05-09 13:45:28 -0700677 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
678 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800679 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
680 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700681 }
Colin Cross0875c522017-11-28 17:34:01 -0800682 if !ctx.PrimaryArch() {
683 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700684 }
685
686 ctx.Variable(pctx, "moduleDesc", desc)
687
688 s := ""
689 if len(suffix) > 0 {
690 s = " [" + strings.Join(suffix, " ") + "]"
691 }
692 ctx.Variable(pctx, "moduleDescSuffix", s)
693
Colin Cross9b1d13d2016-09-19 15:18:11 -0700694 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800695 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700696 if ctx.Failed() {
697 return
698 }
699
Colin Cross0875c522017-11-28 17:34:01 -0800700 a.installFiles = append(a.installFiles, ctx.installFiles...)
701 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800702 }
703
Colin Cross9b1d13d2016-09-19 15:18:11 -0700704 if a == ctx.FinalModule().(Module).base() {
705 a.generateModuleTarget(ctx)
706 if ctx.Failed() {
707 return
708 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800709 }
Colin Crosscec81712017-07-13 14:43:27 -0700710
Colin Cross0875c522017-11-28 17:34:01 -0800711 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800712}
713
Colin Crossf6566ed2015-03-24 11:13:38 -0700714type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700715 target Target
716 targetPrimary bool
717 debug bool
Jiyong Park2db76922017-11-08 16:03:48 +0900718 kind moduleKind
Colin Cross8b74d172016-09-13 09:59:14 -0700719 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700720}
721
Colin Cross3f40fa42015-01-30 17:27:36 -0800722type androidModuleContext struct {
723 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700724 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700725 installDeps Paths
726 installFiles Paths
727 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800728 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700729 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700730
731 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700732 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800733}
734
Colin Cross67a5c132017-05-09 13:45:28 -0700735func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800736 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700737 Rule: ErrorRule,
738 Description: desc,
739 Outputs: outputs,
740 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800741 Args: map[string]string{
742 "error": err.Error(),
743 },
744 })
745 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800746}
747
Colin Crossaabf6792017-11-29 00:27:14 -0800748func (a *androidModuleContext) Config() Config {
749 return a.ModuleContext.Config().(Config)
750}
751
Colin Cross0875c522017-11-28 17:34:01 -0800752func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700753 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800754}
755
Colin Cross0875c522017-11-28 17:34:01 -0800756func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700757 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700758 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800759 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800760 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700761 Outputs: params.Outputs.Strings(),
762 ImplicitOutputs: params.ImplicitOutputs.Strings(),
763 Inputs: params.Inputs.Strings(),
764 Implicits: params.Implicits.Strings(),
765 OrderOnly: params.OrderOnly.Strings(),
766 Args: params.Args,
767 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700768 }
769
Colin Cross33bfb0a2016-11-21 17:23:08 -0800770 if params.Depfile != nil {
771 bparams.Depfile = params.Depfile.String()
772 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700773 if params.Output != nil {
774 bparams.Outputs = append(bparams.Outputs, params.Output.String())
775 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700776 if params.ImplicitOutput != nil {
777 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
778 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700779 if params.Input != nil {
780 bparams.Inputs = append(bparams.Inputs, params.Input.String())
781 }
782 if params.Implicit != nil {
783 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
784 }
785
Colin Cross0875c522017-11-28 17:34:01 -0800786 return bparams
787}
788
789func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
790 a.ModuleContext.Variable(pctx.PackageContext, name, value)
791}
792
793func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
794 argNames ...string) blueprint.Rule {
795
796 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
797}
798
799func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
800 if a.config.captureBuild {
801 a.buildParams = append(a.buildParams, params)
802 }
803
804 bparams := convertBuildParams(params)
805
806 if bparams.Description != "" {
807 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
808 }
809
Colin Cross6ff51382015-12-17 16:39:19 -0800810 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700811 a.ninjaError(bparams.Description, bparams.Outputs,
812 fmt.Errorf("module %s missing dependencies: %s\n",
813 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800814 return
815 }
816
Colin Cross0875c522017-11-28 17:34:01 -0800817 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700818}
819
Colin Cross6ff51382015-12-17 16:39:19 -0800820func (a *androidModuleContext) GetMissingDependencies() []string {
821 return a.missingDeps
822}
823
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800824func (a *androidModuleContext) AddMissingDependencies(deps []string) {
825 if deps != nil {
826 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700827 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800828 }
829}
830
Colin Crossd11fcda2017-10-23 17:59:01 -0700831func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
832 aModule, _ := module.(Module)
833 if aModule == nil {
834 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
835 return nil
836 }
837
838 if !aModule.Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800839 if a.Config().AllowMissingDependencies() {
Colin Crossd11fcda2017-10-23 17:59:01 -0700840 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
841 } else {
842 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
843 }
844 return nil
845 }
846
847 return aModule
848}
849
Colin Cross35143d02017-11-16 00:11:20 -0800850func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
851 a.ModuleContext.VisitDirectDeps(visit)
852}
853
Colin Crossd11fcda2017-10-23 17:59:01 -0700854func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
855 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
856 if aModule := a.validateAndroidModule(module); aModule != nil {
857 visit(aModule)
858 }
859 })
860}
861
Colin Crossee6143c2017-12-30 17:54:27 -0800862func (a *androidModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
863 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
864 if aModule := a.validateAndroidModule(module); aModule != nil {
865 if a.ModuleContext.OtherModuleDependencyTag(aModule) == tag {
866 visit(aModule)
867 }
868 }
869 })
870}
871
Colin Crossd11fcda2017-10-23 17:59:01 -0700872func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
873 a.ModuleContext.VisitDirectDepsIf(
874 // pred
875 func(module blueprint.Module) bool {
876 if aModule := a.validateAndroidModule(module); aModule != nil {
877 return pred(aModule)
878 } else {
879 return false
880 }
881 },
882 // visit
883 func(module blueprint.Module) {
884 visit(module.(Module))
885 })
886}
887
888func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
889 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
890 if aModule := a.validateAndroidModule(module); aModule != nil {
891 visit(aModule)
892 }
893 })
894}
895
896func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
897 a.ModuleContext.VisitDepsDepthFirstIf(
898 // pred
899 func(module blueprint.Module) bool {
900 if aModule := a.validateAndroidModule(module); aModule != nil {
901 return pred(aModule)
902 } else {
903 return false
904 }
905 },
906 // visit
907 func(module blueprint.Module) {
908 visit(module.(Module))
909 })
910}
911
912func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
913 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
914 childAndroidModule := a.validateAndroidModule(child)
915 parentAndroidModule := a.validateAndroidModule(parent)
916 if childAndroidModule != nil && parentAndroidModule != nil {
917 return visit(childAndroidModule, parentAndroidModule)
918 } else {
919 return false
920 }
921 })
922}
923
Colin Cross0875c522017-11-28 17:34:01 -0800924func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
925 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
926 visit(module.(Module))
927 })
928}
929
930func (a *androidModuleContext) PrimaryModule() Module {
931 return a.ModuleContext.PrimaryModule().(Module)
932}
933
934func (a *androidModuleContext) FinalModule() Module {
935 return a.ModuleContext.FinalModule().(Module)
936}
937
Colin Crossa1ad8d12016-06-01 17:09:44 -0700938func (a *androidBaseContextImpl) Target() Target {
939 return a.target
940}
941
Colin Cross8b74d172016-09-13 09:59:14 -0700942func (a *androidBaseContextImpl) TargetPrimary() bool {
943 return a.targetPrimary
944}
945
Colin Crossf6566ed2015-03-24 11:13:38 -0700946func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700947 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800948}
949
Colin Crossa1ad8d12016-06-01 17:09:44 -0700950func (a *androidBaseContextImpl) Os() OsType {
951 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800952}
953
Colin Crossf6566ed2015-03-24 11:13:38 -0700954func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700955 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700956}
957
958func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700959 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700960}
961
Colin Cross0af4b842015-04-30 16:36:18 -0700962func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700963 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700964}
965
Colin Cross3edeee12017-04-04 12:59:48 -0700966func (a *androidBaseContextImpl) Windows() bool {
967 return a.target.Os == Windows
968}
969
Colin Crossf6566ed2015-03-24 11:13:38 -0700970func (a *androidBaseContextImpl) Debug() bool {
971 return a.debug
972}
973
Colin Cross1e7d3702016-08-24 15:25:47 -0700974func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700975 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
976 return true
977 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700978 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
979}
980
Colin Cross1332b002015-04-07 17:11:30 -0700981func (a *androidBaseContextImpl) AConfig() Config {
982 return a.config
983}
984
Colin Cross9272ade2016-08-17 15:24:12 -0700985func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
986 return DeviceConfig{a.config.deviceConfig}
987}
988
Jiyong Park2db76922017-11-08 16:03:48 +0900989func (a *androidBaseContextImpl) Platform() bool {
990 return a.kind == platformModule
991}
992
993func (a *androidBaseContextImpl) DeviceSpecific() bool {
994 return a.kind == deviceSpecificModule
995}
996
997func (a *androidBaseContextImpl) SocSpecific() bool {
998 return a.kind == socSpecificModule
999}
1000
1001func (a *androidBaseContextImpl) ProductSpecific() bool {
1002 return a.kind == productSpecificModule
Dan Willemsen782a2d12015-12-21 14:55:28 -08001003}
1004
Colin Cross8d8f8e22016-08-03 11:57:50 -07001005func (a *androidModuleContext) InstallInData() bool {
1006 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08001007}
1008
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001009func (a *androidModuleContext) InstallInSanitizerDir() bool {
1010 return a.module.InstallInSanitizerDir()
1011}
1012
Colin Cross893d8162017-04-26 17:34:03 -07001013func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
1014 if a.module.base().commonProperties.SkipInstall {
1015 return true
1016 }
1017
Colin Cross3607f212018-05-07 15:28:05 -07001018 // We'll need a solution for choosing which of modules with the same name in different
1019 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
1020 // list of namespaces to install in a Soong-only build.
1021 if !a.module.base().commonProperties.NamespaceExportedToMake {
1022 return true
1023 }
1024
Colin Cross893d8162017-04-26 17:34:03 -07001025 if a.Device() {
Colin Cross6510f912017-11-29 00:27:14 -08001026 if a.Config().SkipDeviceInstall() {
Colin Cross893d8162017-04-26 17:34:03 -07001027 return true
1028 }
1029
Colin Cross6510f912017-11-29 00:27:14 -08001030 if a.Config().SkipMegaDeviceInstall(fullInstallPath.String()) {
Colin Cross893d8162017-04-26 17:34:03 -07001031 return true
1032 }
1033 }
1034
1035 return false
1036}
1037
Colin Cross5c517922017-08-31 12:29:17 -07001038func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -07001039 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -07001040 return a.installFile(installPath, name, srcPath, Cp, deps)
1041}
1042
1043func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
1044 deps ...Path) OutputPath {
1045 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
1046}
1047
1048func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
1049 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -07001050
Dan Willemsen782a2d12015-12-21 14:55:28 -08001051 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001052 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001053
Colin Cross893d8162017-04-26 17:34:03 -07001054 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001055
Dan Willemsen322acaf2016-01-12 23:07:05 -08001056 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -07001057
Colin Cross89562dc2016-10-03 17:47:19 -07001058 var implicitDeps, orderOnlyDeps Paths
1059
1060 if a.Host() {
1061 // Installed host modules might be used during the build, depend directly on their
1062 // dependencies so their timestamp is updated whenever their dependency is updated
1063 implicitDeps = deps
1064 } else {
1065 orderOnlyDeps = deps
1066 }
1067
Colin Crossae887032017-10-23 17:16:14 -07001068 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07001069 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07001070 Description: "install " + fullInstallPath.Base(),
1071 Output: fullInstallPath,
1072 Input: srcPath,
1073 Implicits: implicitDeps,
1074 OrderOnly: orderOnlyDeps,
Colin Cross6510f912017-11-29 00:27:14 -08001075 Default: !a.Config().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08001076 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001077
Dan Willemsen322acaf2016-01-12 23:07:05 -08001078 a.installFiles = append(a.installFiles, fullInstallPath)
1079 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001080 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -07001081 return fullInstallPath
1082}
1083
Colin Cross3854a602016-01-11 12:49:11 -08001084func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
1085 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001086 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08001087
Colin Cross893d8162017-04-26 17:34:03 -07001088 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001089
Colin Crossae887032017-10-23 17:16:14 -07001090 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001091 Rule: Symlink,
1092 Description: "install symlink " + fullInstallPath.Base(),
1093 Output: fullInstallPath,
1094 OrderOnly: Paths{srcPath},
Colin Cross6510f912017-11-29 00:27:14 -08001095 Default: !a.Config().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -08001096 Args: map[string]string{
1097 "fromPath": srcPath.String(),
1098 },
1099 })
Colin Cross3854a602016-01-11 12:49:11 -08001100
Colin Cross12fc4972016-01-11 12:49:11 -08001101 a.installFiles = append(a.installFiles, fullInstallPath)
1102 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1103 }
Colin Cross3854a602016-01-11 12:49:11 -08001104 return fullInstallPath
1105}
1106
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001107func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001108 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1109}
1110
Colin Cross3f40fa42015-01-30 17:27:36 -08001111type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001112 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001113}
1114
1115func isFileInstaller(m blueprint.Module) bool {
1116 _, ok := m.(fileInstaller)
1117 return ok
1118}
1119
1120func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -07001121 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -08001122 return ok
1123}
Colin Crossfce53272015-04-08 11:21:40 -07001124
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001125func findStringInSlice(str string, slice []string) int {
1126 for i, s := range slice {
1127 if s == str {
1128 return i
Colin Crossfce53272015-04-08 11:21:40 -07001129 }
1130 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001131 return -1
1132}
1133
Colin Cross068e0fe2016-12-13 15:23:47 -08001134func SrcIsModule(s string) string {
1135 if len(s) > 1 && s[0] == ':' {
1136 return s[1:]
1137 }
1138 return ""
1139}
1140
1141type sourceDependencyTag struct {
1142 blueprint.BaseDependencyTag
1143}
1144
1145var SourceDepTag sourceDependencyTag
1146
Colin Cross366938f2017-12-11 16:29:02 -08001147// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
1148// using ":module" syntax, if any.
Colin Cross068e0fe2016-12-13 15:23:47 -08001149func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1150 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001151 set := make(map[string]bool)
1152
Colin Cross068e0fe2016-12-13 15:23:47 -08001153 for _, s := range srcFiles {
1154 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001155 if _, found := set[m]; found {
1156 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1157 } else {
1158 set[m] = true
1159 deps = append(deps, m)
1160 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001161 }
1162 }
1163
1164 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1165}
1166
Colin Cross366938f2017-12-11 16:29:02 -08001167// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
1168// using ":module" syntax, if any.
1169func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
1170 if s != nil {
1171 if m := SrcIsModule(*s); m != "" {
1172 ctx.AddDependency(ctx.Module(), SourceDepTag, m)
1173 }
1174 }
1175}
1176
Colin Cross068e0fe2016-12-13 15:23:47 -08001177type SourceFileProducer interface {
1178 Srcs() Paths
1179}
1180
1181// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001182// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001183func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001184 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1185}
1186
Colin Cross366938f2017-12-11 16:29:02 -08001187// Returns a single path expanded from globs and modules referenced using ":module" syntax.
1188// ExtractSourceDeps must have already been called during the dependency resolution phase.
1189func (ctx *androidModuleContext) ExpandSource(srcFile, prop string) Path {
1190 srcFiles := ctx.ExpandSourcesSubDir([]string{srcFile}, nil, "")
1191 if len(srcFiles) == 1 {
1192 return srcFiles[0]
1193 } else {
1194 ctx.PropertyErrorf(prop, "module providing %s must produce exactly one file", prop)
1195 return nil
1196 }
1197}
1198
Colin Cross2383f3b2018-02-06 14:40:13 -08001199// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
1200// the srcFile is non-nil.
1201// ExtractSourceDeps must have already been called during the dependency resolution phase.
1202func (ctx *androidModuleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
1203 if srcFile != nil {
1204 return OptionalPathForPath(ctx.ExpandSource(*srcFile, prop))
1205 }
1206 return OptionalPath{}
1207}
1208
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001209func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001210 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001211
Colin Cross461b4452018-02-23 09:22:42 -08001212 var expandedExcludes []string
1213 if excludes != nil {
1214 expandedExcludes = make([]string, 0, len(excludes))
1215 }
Nan Zhang27e284d2018-02-09 21:03:53 +00001216
1217 for _, e := range excludes {
1218 if m := SrcIsModule(e); m != "" {
1219 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
1220 if module == nil {
1221 // Error will have been handled by ExtractSourcesDeps
1222 continue
1223 }
1224 if srcProducer, ok := module.(SourceFileProducer); ok {
1225 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
1226 } else {
1227 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1228 }
1229 } else {
1230 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001231 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001232 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001233 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001234 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001235 if m := SrcIsModule(s); m != "" {
1236 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001237 if module == nil {
1238 // Error will have been handled by ExtractSourcesDeps
1239 continue
1240 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001241 if srcProducer, ok := module.(SourceFileProducer); ok {
Nan Zhang27e284d2018-02-09 21:03:53 +00001242 moduleSrcs := srcProducer.Srcs()
1243 for _, e := range expandedExcludes {
1244 for j, ms := range moduleSrcs {
1245 if ms.String() == e {
1246 moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
1247 }
1248 }
1249 }
1250 expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001251 } else {
1252 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1253 }
1254 } else if pathtools.IsGlob(s) {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001255 globbedSrcFiles := ctx.GlobFiles(filepath.Join(prefix, s), expandedExcludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001256 for i, s := range globbedSrcFiles {
1257 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001258 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001259 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001260 } else {
Nan Zhang27e284d2018-02-09 21:03:53 +00001261 p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1262 j := findStringInSlice(p.String(), expandedExcludes)
1263 if j == -1 {
1264 expandedSrcFiles = append(expandedSrcFiles, p)
1265 }
1266
Colin Cross8f101b42015-06-17 15:09:06 -07001267 }
1268 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001269 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001270}
1271
Nan Zhang6d34b302017-02-04 17:47:46 -08001272func (ctx *androidModuleContext) RequiredModuleNames() []string {
1273 return ctx.module.base().commonProperties.Required
1274}
1275
Colin Cross7f19f372016-11-01 11:10:25 -07001276func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1277 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001278 if err != nil {
1279 ctx.ModuleErrorf("glob: %s", err.Error())
1280 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001281 return pathsForModuleSrcFromFullPath(ctx, ret, true)
Colin Crossfce53272015-04-08 11:21:40 -07001282}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001283
Nan Zhang581fd212018-01-10 16:06:12 -08001284func (ctx *androidModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001285 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Nan Zhang581fd212018-01-10 16:06:12 -08001286 if err != nil {
1287 ctx.ModuleErrorf("glob: %s", err.Error())
1288 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001289 return pathsForModuleSrcFromFullPath(ctx, ret, false)
Nan Zhang581fd212018-01-10 16:06:12 -08001290}
1291
Colin Cross463a90e2015-06-17 14:20:06 -07001292func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001293 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001294}
1295
Colin Cross0875c522017-11-28 17:34:01 -08001296func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001297 return &buildTargetSingleton{}
1298}
1299
Colin Cross87d8b562017-04-25 10:01:55 -07001300func parentDir(dir string) string {
1301 dir, _ = filepath.Split(dir)
1302 return filepath.Clean(dir)
1303}
1304
Colin Cross1f8c52b2015-06-16 16:38:17 -07001305type buildTargetSingleton struct{}
1306
Colin Cross0875c522017-11-28 17:34:01 -08001307func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1308 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001309
Colin Cross0875c522017-11-28 17:34:01 -08001310 mmTarget := func(dir string) WritablePath {
1311 return PathForPhony(ctx,
1312 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001313 }
1314
Colin Cross0875c522017-11-28 17:34:01 -08001315 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001316
Colin Cross0875c522017-11-28 17:34:01 -08001317 ctx.VisitAllModules(func(module Module) {
1318 blueprintDir := module.base().blueprintDir
1319 installTarget := module.base().installTarget
1320 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001321
Colin Cross0875c522017-11-28 17:34:01 -08001322 if checkbuildTarget != nil {
1323 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1324 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1325 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001326
Colin Cross0875c522017-11-28 17:34:01 -08001327 if installTarget != nil {
1328 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001329 }
1330 })
1331
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001332 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001333 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001334 suffix = "-soong"
1335 }
1336
Colin Cross1f8c52b2015-06-16 16:38:17 -07001337 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001338 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001339 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001340 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001341 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001342 })
1343
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001344 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001345 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001346 return
1347 }
1348
Colin Cross0875c522017-11-28 17:34:01 -08001349 sortedKeys := func(m map[string]Paths) []string {
1350 s := make([]string, 0, len(m))
1351 for k := range m {
1352 s = append(s, k)
1353 }
1354 sort.Strings(s)
1355 return s
1356 }
1357
Colin Cross87d8b562017-04-25 10:01:55 -07001358 // Ensure ancestor directories are in modulesInDir
1359 dirs := sortedKeys(modulesInDir)
1360 for _, dir := range dirs {
1361 dir := parentDir(dir)
1362 for dir != "." && dir != "/" {
1363 if _, exists := modulesInDir[dir]; exists {
1364 break
1365 }
1366 modulesInDir[dir] = nil
1367 dir = parentDir(dir)
1368 }
1369 }
1370
1371 // Make directories build their direct subdirectories
1372 dirs = sortedKeys(modulesInDir)
1373 for _, dir := range dirs {
1374 p := parentDir(dir)
1375 if p != "." && p != "/" {
1376 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1377 }
1378 }
1379
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001380 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1381 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1382 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001383 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001384 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001385 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001386 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001387 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001388 // HACK: checkbuild should be an optional build, but force it
1389 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001390 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001391 })
1392 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001393
1394 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1395 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001396 ctx.VisitAllModules(func(module Module) {
1397 if module.Enabled() {
1398 os := module.Target().Os
1399 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001400 }
1401 })
1402
Colin Cross0875c522017-11-28 17:34:01 -08001403 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001404 for os, deps := range osDeps {
1405 var className string
1406
1407 switch os.Class {
1408 case Host:
1409 className = "host"
1410 case HostCross:
1411 className = "host-cross"
1412 case Device:
1413 className = "target"
1414 default:
1415 continue
1416 }
1417
Colin Cross0875c522017-11-28 17:34:01 -08001418 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001419 osClass[className] = append(osClass[className], name)
1420
Colin Cross0875c522017-11-28 17:34:01 -08001421 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001422 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001423 Output: name,
1424 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001425 })
1426 }
1427
1428 // Wrap those into host|host-cross|target phony rules
1429 osClasses := sortedKeys(osClass)
1430 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001431 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001432 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001433 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001434 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001435 })
1436 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001437}
Colin Crossd779da42015-12-17 18:00:23 -08001438
1439type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001440 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -08001441 ctx interface {
1442 ModuleName(blueprint.Module) string
1443 ModuleSubDir(blueprint.Module) string
1444 }
1445}
1446
1447func (s AndroidModulesByName) Len() int { return len(s.slice) }
1448func (s AndroidModulesByName) Less(i, j int) bool {
1449 mi, mj := s.slice[i], s.slice[j]
1450 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1451
1452 if ni != nj {
1453 return ni < nj
1454 } else {
1455 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1456 }
1457}
1458func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }