blob: 4d9ddd4b1e7b867698135ab6b7cb1f8f3e5818bd [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 Crossfe4bc362018-09-12 10:02:13 -070026 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080027)
28
29var (
30 DeviceSharedLibrary = "shared_library"
31 DeviceStaticLibrary = "static_library"
32 DeviceExecutable = "executable"
33 HostSharedLibrary = "host_shared_library"
34 HostStaticLibrary = "host_static_library"
35 HostExecutable = "host_executable"
36)
37
Colin Crossae887032017-10-23 17:16:14 -070038type BuildParams struct {
Dan Willemsen9f3c5742016-11-03 14:28:31 -070039 Rule blueprint.Rule
Colin Cross33bfb0a2016-11-21 17:23:08 -080040 Deps blueprint.Deps
41 Depfile WritablePath
Colin Cross67a5c132017-05-09 13:45:28 -070042 Description string
Dan Willemsen9f3c5742016-11-03 14:28:31 -070043 Output WritablePath
44 Outputs WritablePaths
45 ImplicitOutput WritablePath
46 ImplicitOutputs WritablePaths
47 Input Path
48 Inputs Paths
49 Implicit Path
50 Implicits Paths
51 OrderOnly Paths
52 Default bool
53 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070054}
55
Colin Crossae887032017-10-23 17:16:14 -070056type ModuleBuildParams BuildParams
57
Colin Crossf6566ed2015-03-24 11:13:38 -070058type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070059 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070060 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070061 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070062 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070063 Host() bool
64 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070065 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070066 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070067 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070068 PrimaryArch() bool
Jiyong Park2db76922017-11-08 16:03:48 +090069 Platform() bool
70 DeviceSpecific() bool
71 SocSpecific() bool
72 ProductSpecific() bool
Dario Frenifd05a742018-05-29 13:28:54 +010073 ProductServicesSpecific() bool
Colin Cross1332b002015-04-07 17:11:30 -070074 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070075 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070076}
77
Colin Cross635c3b02016-05-18 15:37:25 -070078type BaseContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080079 BaseModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070080 androidBaseContext
81}
82
Colin Crossaabf6792017-11-29 00:27:14 -080083// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
84// a Config instead of an interface{}.
85type BaseModuleContext interface {
86 ModuleName() string
87 ModuleDir() string
88 Config() Config
89
90 ContainsProperty(name string) bool
91 Errorf(pos scanner.Position, fmt string, args ...interface{})
92 ModuleErrorf(fmt string, args ...interface{})
93 PropertyErrorf(property, fmt string, args ...interface{})
94 Failed() bool
95
96 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
97 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
98 // builder whenever a file matching the pattern as added or removed, without rerunning if a
99 // file that does not match the pattern is added to a searched directory.
100 GlobWithDeps(pattern string, excludes []string) ([]string, error)
101
102 Fs() pathtools.FileSystem
103 AddNinjaFileDeps(deps ...string)
104}
105
Colin Cross635c3b02016-05-18 15:37:25 -0700106type ModuleContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -0700107 androidBaseContext
Colin Crossaabf6792017-11-29 00:27:14 -0800108 BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -0800109
Colin Crossae887032017-10-23 17:16:14 -0700110 // Deprecated: use ModuleContext.Build instead.
Colin Cross0875c522017-11-28 17:34:01 -0800111 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -0700112
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700113 ExpandSources(srcFiles, excludes []string) Paths
Colin Cross366938f2017-12-11 16:29:02 -0800114 ExpandSource(srcFile, prop string) Path
Colin Cross2383f3b2018-02-06 14:40:13 -0800115 ExpandOptionalSource(srcFile *string, prop string) OptionalPath
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800116 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -0700117 Glob(globPattern string, excludes []string) Paths
Nan Zhang581fd212018-01-10 16:06:12 -0800118 GlobFiles(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700119
Colin Cross5c517922017-08-31 12:29:17 -0700120 InstallExecutable(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
121 InstallFile(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -0800122 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700123 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800124
125 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -0700126
Colin Cross8d8f8e22016-08-03 11:57:50 -0700127 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700128 InstallInSanitizerDir() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900129 InstallInRecovery() bool
Nan Zhang6d34b302017-02-04 17:47:46 -0800130
131 RequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -0700132
133 // android.ModuleContext methods
134 // These are duplicated instead of embedded so that can eventually be wrapped to take an
135 // android.Module instead of a blueprint.Module
136 OtherModuleName(m blueprint.Module) string
137 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
138 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
139
140 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
141 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
142
143 ModuleSubDir() string
144
Colin Cross35143d02017-11-16 00:11:20 -0800145 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700146 VisitDirectDeps(visit func(Module))
Colin Crossee6143c2017-12-30 17:54:27 -0800147 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700148 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -0700149 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Crossd11fcda2017-10-23 17:59:01 -0700150 VisitDepsDepthFirst(visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -0700151 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Crossd11fcda2017-10-23 17:59:01 -0700152 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
153 WalkDeps(visit func(Module, Module) bool)
Colin Cross3f68a132017-10-23 17:10:29 -0700154
Colin Cross0875c522017-11-28 17:34:01 -0800155 Variable(pctx PackageContext, name, value string)
156 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700157 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
158 // and performs more verification.
Colin Cross0875c522017-11-28 17:34:01 -0800159 Build(pctx PackageContext, params BuildParams)
Colin Cross3f68a132017-10-23 17:10:29 -0700160
Colin Cross0875c522017-11-28 17:34:01 -0800161 PrimaryModule() Module
162 FinalModule() Module
163 VisitAllModuleVariants(visit func(Module))
Colin Cross3f68a132017-10-23 17:10:29 -0700164
165 GetMissingDependencies() []string
Jeff Gaston088e29e2017-11-29 16:47:17 -0800166 Namespace() blueprint.Namespace
Colin Cross3f40fa42015-01-30 17:27:36 -0800167}
168
Colin Cross635c3b02016-05-18 15:37:25 -0700169type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800170 blueprint.Module
171
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700172 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
173 // but GenerateAndroidBuildActions also has access to Android-specific information.
174 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700175 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700176
Colin Cross1e676be2016-10-12 14:38:15 -0700177 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800178
Colin Cross635c3b02016-05-18 15:37:25 -0700179 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800180 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700181 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800182 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700183 InstallInSanitizerDir() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900184 InstallInRecovery() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800185 SkipInstall()
Jiyong Park374510b2018-03-19 18:23:01 +0900186 ExportedToMake() bool
Colin Cross36242852017-06-23 15:06:31 -0700187
188 AddProperties(props ...interface{})
189 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700190
Colin Crossae887032017-10-23 17:16:14 -0700191 BuildParamsForTests() []BuildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800192}
193
Colin Crossfc754582016-05-17 16:34:16 -0700194type nameProperties struct {
195 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800196 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700197}
198
199type commonProperties struct {
Dan Willemsen0effe062015-11-30 16:06:01 -0800200 // emit build rules for this module
201 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800202
Colin Cross7d5136f2015-05-11 13:39:40 -0700203 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800204 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
205 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
206 // platform
Colin Cross7d716ba2017-11-01 10:38:29 -0700207 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700208
209 Target struct {
210 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700211 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700212 }
213 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700214 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700215 }
216 }
217
218 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800219
Dan Willemsen782a2d12015-12-21 14:55:28 -0800220 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700221 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800222
Colin Cross55708f32017-03-20 13:23:34 -0700223 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700224 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700225
Jiyong Park2db76922017-11-08 16:03:48 +0900226 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
227 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
228 // Use `soc_specific` instead for better meaning.
Colin Cross7d716ba2017-11-01 10:38:29 -0700229 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700230
Jiyong Park2db76922017-11-08 16:03:48 +0900231 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
232 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
233 Soc_specific *bool
234
235 // whether this module is specific to a device, not only for SoC, but also for off-chip
236 // peripherals. When set to true, it is installed into /odm (or /vendor/odm if odm partition
237 // does not exist, or /system/vendor/odm if both odm and vendor partitions do not exist).
238 // This implies `soc_specific:true`.
239 Device_specific *bool
240
241 // whether this module is specific to a software configuration of a product (e.g. country,
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900242 // network operator, etc). When set to true, it is installed into /product (or
243 // /system/product if product partition does not exist).
Jiyong Park2db76922017-11-08 16:03:48 +0900244 Product_specific *bool
245
Dario Frenifd05a742018-05-29 13:28:54 +0100246 // whether this module provides services owned by the OS provider to the core platform. When set
Dario Freni95cf7672018-08-17 00:57:57 +0100247 // to true, it is installed into /product_services (or /system/product_services if
248 // product_services partition does not exist).
249 Product_services_specific *bool
Dario Frenifd05a742018-05-29 13:28:54 +0100250
Jiyong Parkf9332f12018-02-01 00:54:12 +0900251 // Whether this module is installed to recovery partition
252 Recovery *bool
253
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700254 // init.rc files to be installed if this module is installed
255 Init_rc []string
256
Steven Moreland57a23d22018-04-04 15:42:19 -0700257 // VINTF manifest fragments to be installed if this module is installed
258 Vintf_fragments []string
259
Chris Wolfe998306e2016-08-15 14:47:23 -0400260 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700261 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400262
Colin Cross5aac3622017-08-31 15:07:09 -0700263 // relative path to a file to include in the list of notices for the device
264 Notice *string
265
Colin Crossa1ad8d12016-06-01 17:09:44 -0700266 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700267 CompileTarget Target `blueprint:"mutated"`
268 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800269
270 // Set by InitAndroidModule
271 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700272 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700273
274 SkipInstall bool `blueprint:"mutated"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800275
276 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800277}
278
279type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700280 Host_supported *bool
281 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800282}
283
Colin Crossc472d572015-03-17 15:06:21 -0700284type Multilib string
285
286const (
Colin Cross6b4a32d2017-12-05 13:42:45 -0800287 MultilibBoth Multilib = "both"
288 MultilibFirst Multilib = "first"
289 MultilibCommon Multilib = "common"
290 MultilibCommonFirst Multilib = "common_first"
291 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700292)
293
Colin Crossa1ad8d12016-06-01 17:09:44 -0700294type HostOrDeviceSupported int
295
296const (
297 _ HostOrDeviceSupported = iota
Dan Albert0981b5c2018-08-02 13:46:35 -0700298
299 // Host and HostCross are built by default. Device is not supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700300 HostSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700301
302 // Host is built by default. HostCross and Device are not supported.
Dan Albertc6345fb2016-10-20 01:36:11 -0700303 HostSupportedNoCross
Dan Albert0981b5c2018-08-02 13:46:35 -0700304
305 // Device is built by default. Host and HostCross are not supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700306 DeviceSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700307
308 // Device is built by default. Host and HostCross are supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700309 HostAndDeviceSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700310
311 // Host, HostCross, and Device are built by default.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700312 HostAndDeviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700313
314 // Nothing is supported. This is not exposed to the user, but used to mark a
315 // host only module as unsupported when the module type is not supported on
316 // the host OS. E.g. benchmarks are supported on Linux but not Darwin.
Dan Willemsen0b24c742016-10-04 15:13:37 -0700317 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700318)
319
Jiyong Park2db76922017-11-08 16:03:48 +0900320type moduleKind int
321
322const (
323 platformModule moduleKind = iota
324 deviceSpecificModule
325 socSpecificModule
326 productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100327 productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900328)
329
330func (k moduleKind) String() string {
331 switch k {
332 case platformModule:
333 return "platform"
334 case deviceSpecificModule:
335 return "device-specific"
336 case socSpecificModule:
337 return "soc-specific"
338 case productSpecificModule:
339 return "product-specific"
Dario Frenifd05a742018-05-29 13:28:54 +0100340 case productServicesSpecificModule:
341 return "productservices-specific"
Jiyong Park2db76922017-11-08 16:03:48 +0900342 default:
343 panic(fmt.Errorf("unknown module kind %d", k))
344 }
345}
346
Colin Cross36242852017-06-23 15:06:31 -0700347func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800348 base := m.base()
349 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700350
Colin Cross36242852017-06-23 15:06:31 -0700351 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700352 &base.nameProperties,
353 &base.commonProperties,
354 &base.variableProperties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700355 base.customizableProperties = m.GetProperties()
Colin Cross5049f022015-03-18 13:28:46 -0700356}
357
Colin Cross36242852017-06-23 15:06:31 -0700358func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
359 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700360
361 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800362 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700363 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700364 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800365
Dan Willemsen218f6562015-07-08 18:13:11 -0700366 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700367 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700368 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800369 }
370
Colin Cross36242852017-06-23 15:06:31 -0700371 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800372}
373
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800374// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800375// modules. It should be included as an anonymous field in every module
376// struct definition. InitAndroidModule should then be called from the module's
377// factory function, and the return values from InitAndroidModule should be
378// returned from the factory function.
379//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800380// The ModuleBase type is responsible for implementing the GenerateBuildActions
381// method to support the blueprint.Module interface. This method will then call
382// the module's GenerateAndroidBuildActions method once for each build variant
383// that is to be built. GenerateAndroidBuildActions is passed a
384// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800385// AndroidModuleContext exposes extra functionality specific to the Android build
386// system including details about the particular build variant that is to be
387// generated.
388//
389// For example:
390//
391// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800392// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800393// )
394//
395// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800396// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800397// properties struct {
398// MyProperty string
399// }
400// }
401//
Colin Cross36242852017-06-23 15:06:31 -0700402// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800403// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700404// m.AddProperties(&m.properties)
405// android.InitAndroidModule(m)
406// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800407// }
408//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800409// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800410// // Get the CPU architecture for the current build variant.
411// variantArch := ctx.Arch()
412//
413// // ...
414// }
Colin Cross635c3b02016-05-18 15:37:25 -0700415type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800416 // Putting the curiously recurring thing pointing to the thing that contains
417 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700418 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700419 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800420
Colin Crossfc754582016-05-17 16:34:16 -0700421 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800422 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700423 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800424 hostAndDeviceProperties hostAndDeviceProperties
425 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700426 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700427 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800428
429 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700430 installFiles Paths
431 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700432
433 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
434 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800435 installTarget WritablePath
436 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700437 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700438
Colin Cross178a5092016-09-13 13:42:32 -0700439 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700440
441 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700442
443 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700444 buildParams []BuildParams
Colin Cross36242852017-06-23 15:06:31 -0700445}
446
447func (a *ModuleBase) AddProperties(props ...interface{}) {
448 a.registerProps = append(a.registerProps, props...)
449}
450
451func (a *ModuleBase) GetProperties() []interface{} {
452 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800453}
454
Colin Crossae887032017-10-23 17:16:14 -0700455func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700456 return a.buildParams
457}
458
Colin Crossce75d2c2016-10-06 16:12:58 -0700459// Name returns the name of the module. It may be overridden by individual module types, for
460// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700461func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800462 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700463}
464
Colin Crossce75d2c2016-10-06 16:12:58 -0700465// BaseModuleName returns the name of the module as specified in the blueprints file.
466func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800467 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700468}
469
Colin Cross635c3b02016-05-18 15:37:25 -0700470func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800471 return a
472}
473
Colin Cross8b74d172016-09-13 09:59:14 -0700474func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700475 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700476 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700477}
478
Colin Crossa1ad8d12016-06-01 17:09:44 -0700479func (a *ModuleBase) Target() Target {
480 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800481}
482
Colin Cross8b74d172016-09-13 09:59:14 -0700483func (a *ModuleBase) TargetPrimary() bool {
484 return a.commonProperties.CompilePrimary
485}
486
Colin Crossa1ad8d12016-06-01 17:09:44 -0700487func (a *ModuleBase) Os() OsType {
488 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800489}
490
Colin Cross635c3b02016-05-18 15:37:25 -0700491func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700492 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800493}
494
Colin Cross635c3b02016-05-18 15:37:25 -0700495func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700496 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800497}
498
Dan Willemsen0b24c742016-10-04 15:13:37 -0700499func (a *ModuleBase) ArchSpecific() bool {
500 return a.commonProperties.ArchSpecific
501}
502
Colin Crossa1ad8d12016-06-01 17:09:44 -0700503func (a *ModuleBase) OsClassSupported() []OsClass {
504 switch a.commonProperties.HostOrDeviceSupported {
505 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700506 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700507 case HostSupportedNoCross:
508 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700509 case DeviceSupported:
510 return []OsClass{Device}
Dan Albert0981b5c2018-08-02 13:46:35 -0700511 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700512 var supported []OsClass
Dan Albert0981b5c2018-08-02 13:46:35 -0700513 if Bool(a.hostAndDeviceProperties.Host_supported) ||
514 (a.commonProperties.HostOrDeviceSupported == HostAndDeviceDefault &&
515 a.hostAndDeviceProperties.Host_supported == nil) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700516 supported = append(supported, Host, HostCross)
517 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700518 if a.hostAndDeviceProperties.Device_supported == nil ||
519 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700520 supported = append(supported, Device)
521 }
522 return supported
523 default:
524 return nil
525 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800526}
527
Colin Cross635c3b02016-05-18 15:37:25 -0700528func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800529 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
530 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700531 (a.hostAndDeviceProperties.Device_supported == nil ||
532 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800533}
534
Jiyong Parkc678ad32018-04-10 13:07:10 +0900535func (a *ModuleBase) Platform() bool {
Dario Frenifd05a742018-05-29 13:28:54 +0100536 return !a.DeviceSpecific() && !a.SocSpecific() && !a.ProductSpecific() && !a.ProductServicesSpecific()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900537}
538
539func (a *ModuleBase) DeviceSpecific() bool {
540 return Bool(a.commonProperties.Device_specific)
541}
542
543func (a *ModuleBase) SocSpecific() bool {
544 return Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
545}
546
547func (a *ModuleBase) ProductSpecific() bool {
548 return Bool(a.commonProperties.Product_specific)
549}
550
Dario Frenifd05a742018-05-29 13:28:54 +0100551func (a *ModuleBase) ProductServicesSpecific() bool {
Dario Freni95cf7672018-08-17 00:57:57 +0100552 return Bool(a.commonProperties.Product_services_specific)
Dario Frenifd05a742018-05-29 13:28:54 +0100553}
554
Colin Cross635c3b02016-05-18 15:37:25 -0700555func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800556 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800557 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800558 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800559 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800560}
561
Colin Crossce75d2c2016-10-06 16:12:58 -0700562func (a *ModuleBase) SkipInstall() {
563 a.commonProperties.SkipInstall = true
564}
565
Jiyong Park374510b2018-03-19 18:23:01 +0900566func (a *ModuleBase) ExportedToMake() bool {
567 return a.commonProperties.NamespaceExportedToMake
568}
569
Colin Cross635c3b02016-05-18 15:37:25 -0700570func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700571 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800572
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700573 result := Paths{}
Colin Cross6b753602018-06-21 13:03:07 -0700574 // TODO(ccross): we need to use WalkDeps and have some way to know which dependencies require installation
Colin Cross3f40fa42015-01-30 17:27:36 -0800575 ctx.VisitDepsDepthFirstIf(isFileInstaller,
576 func(m blueprint.Module) {
577 fileInstaller := m.(fileInstaller)
578 files := fileInstaller.filesToInstall()
579 result = append(result, files...)
580 })
581
582 return result
583}
584
Colin Cross635c3b02016-05-18 15:37:25 -0700585func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800586 return a.installFiles
587}
588
Colin Cross635c3b02016-05-18 15:37:25 -0700589func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800590 return p.noAddressSanitizer
591}
592
Colin Cross635c3b02016-05-18 15:37:25 -0700593func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800594 return false
595}
596
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700597func (p *ModuleBase) InstallInSanitizerDir() bool {
598 return false
599}
600
Jiyong Parkf9332f12018-02-01 00:54:12 +0900601func (p *ModuleBase) InstallInRecovery() bool {
602 return Bool(p.commonProperties.Recovery)
603}
604
Colin Cross0875c522017-11-28 17:34:01 -0800605func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700606 allInstalledFiles := Paths{}
607 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800608 ctx.VisitAllModuleVariants(func(module Module) {
609 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700610 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
611 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800612 })
613
Colin Cross0875c522017-11-28 17:34:01 -0800614 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700615
Jeff Gaston088e29e2017-11-29 16:47:17 -0800616 namespacePrefix := ctx.Namespace().(*Namespace).id
617 if namespacePrefix != "" {
618 namespacePrefix = namespacePrefix + "-"
619 }
620
Colin Cross3f40fa42015-01-30 17:27:36 -0800621 if len(allInstalledFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800622 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800623 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700624 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800625 Output: name,
626 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800627 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700628 })
629 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700630 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700631 }
632
633 if len(allCheckbuildFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800634 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800635 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700636 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800637 Output: name,
638 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700639 })
640 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700641 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700642 }
643
644 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800645 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800646 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800647 suffix = "-soong"
648 }
649
Jeff Gaston088e29e2017-11-29 16:47:17 -0800650 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800651 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700652 Rule: blueprint.Phony,
Jeff Gaston088e29e2017-11-29 16:47:17 -0800653 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700654 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800655 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700656
657 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800658 }
659}
660
Jiyong Park2db76922017-11-08 16:03:48 +0900661func determineModuleKind(a *ModuleBase, ctx blueprint.BaseModuleContext) moduleKind {
662 var socSpecific = Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
663 var deviceSpecific = Bool(a.commonProperties.Device_specific)
664 var productSpecific = Bool(a.commonProperties.Product_specific)
Dario Freni95cf7672018-08-17 00:57:57 +0100665 var productServicesSpecific = Bool(a.commonProperties.Product_services_specific)
Jiyong Park2db76922017-11-08 16:03:48 +0900666
Dario Frenifd05a742018-05-29 13:28:54 +0100667 msg := "conflicting value set here"
668 if socSpecific && deviceSpecific {
669 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
Jiyong Park2db76922017-11-08 16:03:48 +0900670 if Bool(a.commonProperties.Vendor) {
671 ctx.PropertyErrorf("vendor", msg)
672 }
673 if Bool(a.commonProperties.Proprietary) {
674 ctx.PropertyErrorf("proprietary", msg)
675 }
676 if Bool(a.commonProperties.Soc_specific) {
677 ctx.PropertyErrorf("soc_specific", msg)
678 }
679 }
680
Dario Frenifd05a742018-05-29 13:28:54 +0100681 if productSpecific && productServicesSpecific {
682 ctx.PropertyErrorf("product_specific", "a module cannot be specific to product and product_services at the same time.")
683 ctx.PropertyErrorf("product_services_specific", msg)
684 }
685
686 if (socSpecific || deviceSpecific) && (productSpecific || productServicesSpecific) {
687 if productSpecific {
688 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
689 } else {
690 ctx.PropertyErrorf("product_services_specific", "a module cannot be specific to SoC or device and product_services at the same time.")
691 }
692 if deviceSpecific {
693 ctx.PropertyErrorf("device_specific", msg)
694 } else {
695 if Bool(a.commonProperties.Vendor) {
696 ctx.PropertyErrorf("vendor", msg)
697 }
698 if Bool(a.commonProperties.Proprietary) {
699 ctx.PropertyErrorf("proprietary", msg)
700 }
701 if Bool(a.commonProperties.Soc_specific) {
702 ctx.PropertyErrorf("soc_specific", msg)
703 }
704 }
705 }
706
Jiyong Park2db76922017-11-08 16:03:48 +0900707 if productSpecific {
708 return productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100709 } else if productServicesSpecific {
710 return productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900711 } else if deviceSpecific {
712 return deviceSpecificModule
713 } else if socSpecific {
714 return socSpecificModule
715 } else {
716 return platformModule
717 }
718}
719
Colin Cross635c3b02016-05-18 15:37:25 -0700720func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700721 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700722 target: a.commonProperties.CompileTarget,
723 targetPrimary: a.commonProperties.CompilePrimary,
Jiyong Park2db76922017-11-08 16:03:48 +0900724 kind: determineModuleKind(a, ctx),
Colin Cross8b74d172016-09-13 09:59:14 -0700725 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800726 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800727}
728
Colin Cross0875c522017-11-28 17:34:01 -0800729func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
730 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700731 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800732 ModuleContext: blueprintCtx,
733 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
734 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700735 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800736 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800737 }
738
Colin Cross67a5c132017-05-09 13:45:28 -0700739 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
740 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800741 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
742 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700743 }
Colin Cross0875c522017-11-28 17:34:01 -0800744 if !ctx.PrimaryArch() {
745 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700746 }
747
748 ctx.Variable(pctx, "moduleDesc", desc)
749
750 s := ""
751 if len(suffix) > 0 {
752 s = " [" + strings.Join(suffix, " ") + "]"
753 }
754 ctx.Variable(pctx, "moduleDescSuffix", s)
755
Colin Cross9b1d13d2016-09-19 15:18:11 -0700756 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800757 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700758 if ctx.Failed() {
759 return
760 }
761
Colin Cross0875c522017-11-28 17:34:01 -0800762 a.installFiles = append(a.installFiles, ctx.installFiles...)
763 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800764 }
765
Colin Cross9b1d13d2016-09-19 15:18:11 -0700766 if a == ctx.FinalModule().(Module).base() {
767 a.generateModuleTarget(ctx)
768 if ctx.Failed() {
769 return
770 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800771 }
Colin Crosscec81712017-07-13 14:43:27 -0700772
Colin Cross0875c522017-11-28 17:34:01 -0800773 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800774}
775
Colin Crossf6566ed2015-03-24 11:13:38 -0700776type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700777 target Target
778 targetPrimary bool
779 debug bool
Jiyong Park2db76922017-11-08 16:03:48 +0900780 kind moduleKind
Colin Cross8b74d172016-09-13 09:59:14 -0700781 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700782}
783
Colin Cross3f40fa42015-01-30 17:27:36 -0800784type androidModuleContext struct {
785 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700786 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700787 installDeps Paths
788 installFiles Paths
789 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800790 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700791 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700792
793 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700794 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800795}
796
Colin Cross67a5c132017-05-09 13:45:28 -0700797func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800798 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700799 Rule: ErrorRule,
800 Description: desc,
801 Outputs: outputs,
802 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800803 Args: map[string]string{
804 "error": err.Error(),
805 },
806 })
807 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800808}
809
Colin Crossaabf6792017-11-29 00:27:14 -0800810func (a *androidModuleContext) Config() Config {
811 return a.ModuleContext.Config().(Config)
812}
813
Colin Cross0875c522017-11-28 17:34:01 -0800814func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700815 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800816}
817
Colin Cross0875c522017-11-28 17:34:01 -0800818func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700819 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700820 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800821 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800822 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700823 Outputs: params.Outputs.Strings(),
824 ImplicitOutputs: params.ImplicitOutputs.Strings(),
825 Inputs: params.Inputs.Strings(),
826 Implicits: params.Implicits.Strings(),
827 OrderOnly: params.OrderOnly.Strings(),
828 Args: params.Args,
829 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700830 }
831
Colin Cross33bfb0a2016-11-21 17:23:08 -0800832 if params.Depfile != nil {
833 bparams.Depfile = params.Depfile.String()
834 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700835 if params.Output != nil {
836 bparams.Outputs = append(bparams.Outputs, params.Output.String())
837 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700838 if params.ImplicitOutput != nil {
839 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
840 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700841 if params.Input != nil {
842 bparams.Inputs = append(bparams.Inputs, params.Input.String())
843 }
844 if params.Implicit != nil {
845 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
846 }
847
Colin Crossfe4bc362018-09-12 10:02:13 -0700848 bparams.Outputs = proptools.NinjaEscape(bparams.Outputs)
849 bparams.ImplicitOutputs = proptools.NinjaEscape(bparams.ImplicitOutputs)
850 bparams.Inputs = proptools.NinjaEscape(bparams.Inputs)
851 bparams.Implicits = proptools.NinjaEscape(bparams.Implicits)
852 bparams.OrderOnly = proptools.NinjaEscape(bparams.OrderOnly)
853 bparams.Depfile = proptools.NinjaEscape([]string{bparams.Depfile})[0]
854
Colin Cross0875c522017-11-28 17:34:01 -0800855 return bparams
856}
857
858func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
859 a.ModuleContext.Variable(pctx.PackageContext, name, value)
860}
861
862func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
863 argNames ...string) blueprint.Rule {
864
865 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
866}
867
868func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
869 if a.config.captureBuild {
870 a.buildParams = append(a.buildParams, params)
871 }
872
873 bparams := convertBuildParams(params)
874
875 if bparams.Description != "" {
876 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
877 }
878
Colin Cross6ff51382015-12-17 16:39:19 -0800879 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700880 a.ninjaError(bparams.Description, bparams.Outputs,
881 fmt.Errorf("module %s missing dependencies: %s\n",
882 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800883 return
884 }
885
Colin Cross0875c522017-11-28 17:34:01 -0800886 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700887}
888
Colin Cross6ff51382015-12-17 16:39:19 -0800889func (a *androidModuleContext) GetMissingDependencies() []string {
890 return a.missingDeps
891}
892
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800893func (a *androidModuleContext) AddMissingDependencies(deps []string) {
894 if deps != nil {
895 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700896 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800897 }
898}
899
Colin Crossd11fcda2017-10-23 17:59:01 -0700900func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
901 aModule, _ := module.(Module)
902 if aModule == nil {
903 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
904 return nil
905 }
906
907 if !aModule.Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800908 if a.Config().AllowMissingDependencies() {
Colin Crossd11fcda2017-10-23 17:59:01 -0700909 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
910 } else {
911 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
912 }
913 return nil
914 }
915
916 return aModule
917}
918
Colin Cross35143d02017-11-16 00:11:20 -0800919func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
920 a.ModuleContext.VisitDirectDeps(visit)
921}
922
Colin Crossd11fcda2017-10-23 17:59:01 -0700923func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
924 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
925 if aModule := a.validateAndroidModule(module); aModule != nil {
926 visit(aModule)
927 }
928 })
929}
930
Colin Crossee6143c2017-12-30 17:54:27 -0800931func (a *androidModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
932 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
933 if aModule := a.validateAndroidModule(module); aModule != nil {
934 if a.ModuleContext.OtherModuleDependencyTag(aModule) == tag {
935 visit(aModule)
936 }
937 }
938 })
939}
940
Colin Crossd11fcda2017-10-23 17:59:01 -0700941func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
942 a.ModuleContext.VisitDirectDepsIf(
943 // pred
944 func(module blueprint.Module) bool {
945 if aModule := a.validateAndroidModule(module); aModule != nil {
946 return pred(aModule)
947 } else {
948 return false
949 }
950 },
951 // visit
952 func(module blueprint.Module) {
953 visit(module.(Module))
954 })
955}
956
957func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
958 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
959 if aModule := a.validateAndroidModule(module); aModule != nil {
960 visit(aModule)
961 }
962 })
963}
964
965func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
966 a.ModuleContext.VisitDepsDepthFirstIf(
967 // pred
968 func(module blueprint.Module) bool {
969 if aModule := a.validateAndroidModule(module); aModule != nil {
970 return pred(aModule)
971 } else {
972 return false
973 }
974 },
975 // visit
976 func(module blueprint.Module) {
977 visit(module.(Module))
978 })
979}
980
981func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
982 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
983 childAndroidModule := a.validateAndroidModule(child)
984 parentAndroidModule := a.validateAndroidModule(parent)
985 if childAndroidModule != nil && parentAndroidModule != nil {
986 return visit(childAndroidModule, parentAndroidModule)
987 } else {
988 return false
989 }
990 })
991}
992
Colin Cross0875c522017-11-28 17:34:01 -0800993func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
994 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
995 visit(module.(Module))
996 })
997}
998
999func (a *androidModuleContext) PrimaryModule() Module {
1000 return a.ModuleContext.PrimaryModule().(Module)
1001}
1002
1003func (a *androidModuleContext) FinalModule() Module {
1004 return a.ModuleContext.FinalModule().(Module)
1005}
1006
Colin Crossa1ad8d12016-06-01 17:09:44 -07001007func (a *androidBaseContextImpl) Target() Target {
1008 return a.target
1009}
1010
Colin Cross8b74d172016-09-13 09:59:14 -07001011func (a *androidBaseContextImpl) TargetPrimary() bool {
1012 return a.targetPrimary
1013}
1014
Colin Crossf6566ed2015-03-24 11:13:38 -07001015func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001016 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -08001017}
1018
Colin Crossa1ad8d12016-06-01 17:09:44 -07001019func (a *androidBaseContextImpl) Os() OsType {
1020 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -08001021}
1022
Colin Crossf6566ed2015-03-24 11:13:38 -07001023func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001024 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -07001025}
1026
1027func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001028 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -07001029}
1030
Colin Cross0af4b842015-04-30 16:36:18 -07001031func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001032 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -07001033}
1034
Colin Cross3edeee12017-04-04 12:59:48 -07001035func (a *androidBaseContextImpl) Windows() bool {
1036 return a.target.Os == Windows
1037}
1038
Colin Crossf6566ed2015-03-24 11:13:38 -07001039func (a *androidBaseContextImpl) Debug() bool {
1040 return a.debug
1041}
1042
Colin Cross1e7d3702016-08-24 15:25:47 -07001043func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -07001044 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
1045 return true
1046 }
Colin Cross1e7d3702016-08-24 15:25:47 -07001047 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
1048}
1049
Colin Cross1332b002015-04-07 17:11:30 -07001050func (a *androidBaseContextImpl) AConfig() Config {
1051 return a.config
1052}
1053
Colin Cross9272ade2016-08-17 15:24:12 -07001054func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
1055 return DeviceConfig{a.config.deviceConfig}
1056}
1057
Jiyong Park2db76922017-11-08 16:03:48 +09001058func (a *androidBaseContextImpl) Platform() bool {
1059 return a.kind == platformModule
1060}
1061
1062func (a *androidBaseContextImpl) DeviceSpecific() bool {
1063 return a.kind == deviceSpecificModule
1064}
1065
1066func (a *androidBaseContextImpl) SocSpecific() bool {
1067 return a.kind == socSpecificModule
1068}
1069
1070func (a *androidBaseContextImpl) ProductSpecific() bool {
1071 return a.kind == productSpecificModule
Dan Willemsen782a2d12015-12-21 14:55:28 -08001072}
1073
Dario Frenifd05a742018-05-29 13:28:54 +01001074func (a *androidBaseContextImpl) ProductServicesSpecific() bool {
1075 return a.kind == productServicesSpecificModule
1076}
1077
Jiyong Park5baac542018-08-28 09:55:37 +09001078// Makes this module a platform module, i.e. not specific to soc, device,
1079// product, or product_services.
1080func (a *ModuleBase) MakeAsPlatform() {
1081 a.commonProperties.Vendor = boolPtr(false)
1082 a.commonProperties.Proprietary = boolPtr(false)
1083 a.commonProperties.Soc_specific = boolPtr(false)
1084 a.commonProperties.Product_specific = boolPtr(false)
1085 a.commonProperties.Product_services_specific = boolPtr(false)
1086}
1087
Colin Cross8d8f8e22016-08-03 11:57:50 -07001088func (a *androidModuleContext) InstallInData() bool {
1089 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08001090}
1091
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001092func (a *androidModuleContext) InstallInSanitizerDir() bool {
1093 return a.module.InstallInSanitizerDir()
1094}
1095
Jiyong Parkf9332f12018-02-01 00:54:12 +09001096func (a *androidModuleContext) InstallInRecovery() bool {
1097 return a.module.InstallInRecovery()
1098}
1099
Colin Cross893d8162017-04-26 17:34:03 -07001100func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
1101 if a.module.base().commonProperties.SkipInstall {
1102 return true
1103 }
1104
Colin Cross3607f212018-05-07 15:28:05 -07001105 // We'll need a solution for choosing which of modules with the same name in different
1106 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
1107 // list of namespaces to install in a Soong-only build.
1108 if !a.module.base().commonProperties.NamespaceExportedToMake {
1109 return true
1110 }
1111
Colin Cross893d8162017-04-26 17:34:03 -07001112 if a.Device() {
Colin Cross6510f912017-11-29 00:27:14 -08001113 if a.Config().SkipDeviceInstall() {
Colin Cross893d8162017-04-26 17:34:03 -07001114 return true
1115 }
1116
Colin Cross6510f912017-11-29 00:27:14 -08001117 if a.Config().SkipMegaDeviceInstall(fullInstallPath.String()) {
Colin Cross893d8162017-04-26 17:34:03 -07001118 return true
1119 }
1120 }
1121
1122 return false
1123}
1124
Colin Cross5c517922017-08-31 12:29:17 -07001125func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -07001126 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -07001127 return a.installFile(installPath, name, srcPath, Cp, deps)
1128}
1129
1130func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
1131 deps ...Path) OutputPath {
1132 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
1133}
1134
1135func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
1136 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -07001137
Dan Willemsen782a2d12015-12-21 14:55:28 -08001138 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001139 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001140
Colin Cross893d8162017-04-26 17:34:03 -07001141 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001142
Dan Willemsen322acaf2016-01-12 23:07:05 -08001143 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -07001144
Colin Cross89562dc2016-10-03 17:47:19 -07001145 var implicitDeps, orderOnlyDeps Paths
1146
1147 if a.Host() {
1148 // Installed host modules might be used during the build, depend directly on their
1149 // dependencies so their timestamp is updated whenever their dependency is updated
1150 implicitDeps = deps
1151 } else {
1152 orderOnlyDeps = deps
1153 }
1154
Colin Crossae887032017-10-23 17:16:14 -07001155 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07001156 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07001157 Description: "install " + fullInstallPath.Base(),
1158 Output: fullInstallPath,
1159 Input: srcPath,
1160 Implicits: implicitDeps,
1161 OrderOnly: orderOnlyDeps,
Colin Cross6510f912017-11-29 00:27:14 -08001162 Default: !a.Config().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08001163 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001164
Dan Willemsen322acaf2016-01-12 23:07:05 -08001165 a.installFiles = append(a.installFiles, fullInstallPath)
1166 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001167 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -07001168 return fullInstallPath
1169}
1170
Colin Cross3854a602016-01-11 12:49:11 -08001171func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
1172 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001173 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08001174
Colin Cross893d8162017-04-26 17:34:03 -07001175 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001176
Colin Crossae887032017-10-23 17:16:14 -07001177 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001178 Rule: Symlink,
1179 Description: "install symlink " + fullInstallPath.Base(),
1180 Output: fullInstallPath,
1181 OrderOnly: Paths{srcPath},
Colin Cross6510f912017-11-29 00:27:14 -08001182 Default: !a.Config().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -08001183 Args: map[string]string{
1184 "fromPath": srcPath.String(),
1185 },
1186 })
Colin Cross3854a602016-01-11 12:49:11 -08001187
Colin Cross12fc4972016-01-11 12:49:11 -08001188 a.installFiles = append(a.installFiles, fullInstallPath)
1189 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1190 }
Colin Cross3854a602016-01-11 12:49:11 -08001191 return fullInstallPath
1192}
1193
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001194func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001195 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1196}
1197
Colin Cross3f40fa42015-01-30 17:27:36 -08001198type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001199 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001200}
1201
1202func isFileInstaller(m blueprint.Module) bool {
1203 _, ok := m.(fileInstaller)
1204 return ok
1205}
1206
1207func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -07001208 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -08001209 return ok
1210}
Colin Crossfce53272015-04-08 11:21:40 -07001211
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001212func findStringInSlice(str string, slice []string) int {
1213 for i, s := range slice {
1214 if s == str {
1215 return i
Colin Crossfce53272015-04-08 11:21:40 -07001216 }
1217 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001218 return -1
1219}
1220
Colin Cross068e0fe2016-12-13 15:23:47 -08001221func SrcIsModule(s string) string {
1222 if len(s) > 1 && s[0] == ':' {
1223 return s[1:]
1224 }
1225 return ""
1226}
1227
1228type sourceDependencyTag struct {
1229 blueprint.BaseDependencyTag
1230}
1231
1232var SourceDepTag sourceDependencyTag
1233
Colin Cross366938f2017-12-11 16:29:02 -08001234// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
1235// using ":module" syntax, if any.
Colin Cross068e0fe2016-12-13 15:23:47 -08001236func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1237 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001238 set := make(map[string]bool)
1239
Colin Cross068e0fe2016-12-13 15:23:47 -08001240 for _, s := range srcFiles {
1241 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001242 if _, found := set[m]; found {
1243 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1244 } else {
1245 set[m] = true
1246 deps = append(deps, m)
1247 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001248 }
1249 }
1250
1251 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1252}
1253
Colin Cross366938f2017-12-11 16:29:02 -08001254// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
1255// using ":module" syntax, if any.
1256func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
1257 if s != nil {
1258 if m := SrcIsModule(*s); m != "" {
1259 ctx.AddDependency(ctx.Module(), SourceDepTag, m)
1260 }
1261 }
1262}
1263
Colin Cross068e0fe2016-12-13 15:23:47 -08001264type SourceFileProducer interface {
1265 Srcs() Paths
1266}
1267
1268// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001269// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001270func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001271 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1272}
1273
Colin Cross366938f2017-12-11 16:29:02 -08001274// Returns a single path expanded from globs and modules referenced using ":module" syntax.
1275// ExtractSourceDeps must have already been called during the dependency resolution phase.
1276func (ctx *androidModuleContext) ExpandSource(srcFile, prop string) Path {
1277 srcFiles := ctx.ExpandSourcesSubDir([]string{srcFile}, nil, "")
1278 if len(srcFiles) == 1 {
1279 return srcFiles[0]
1280 } else {
1281 ctx.PropertyErrorf(prop, "module providing %s must produce exactly one file", prop)
1282 return nil
1283 }
1284}
1285
Colin Cross2383f3b2018-02-06 14:40:13 -08001286// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
1287// the srcFile is non-nil.
1288// ExtractSourceDeps must have already been called during the dependency resolution phase.
1289func (ctx *androidModuleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
1290 if srcFile != nil {
1291 return OptionalPathForPath(ctx.ExpandSource(*srcFile, prop))
1292 }
1293 return OptionalPath{}
1294}
1295
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001296func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001297 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001298
Colin Cross461b4452018-02-23 09:22:42 -08001299 var expandedExcludes []string
1300 if excludes != nil {
1301 expandedExcludes = make([]string, 0, len(excludes))
1302 }
Nan Zhang27e284d2018-02-09 21:03:53 +00001303
1304 for _, e := range excludes {
1305 if m := SrcIsModule(e); m != "" {
1306 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
1307 if module == nil {
1308 // Error will have been handled by ExtractSourcesDeps
1309 continue
1310 }
1311 if srcProducer, ok := module.(SourceFileProducer); ok {
1312 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
1313 } else {
1314 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1315 }
1316 } else {
1317 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001318 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001319 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001320 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001321 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001322 if m := SrcIsModule(s); m != "" {
1323 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001324 if module == nil {
1325 // Error will have been handled by ExtractSourcesDeps
1326 continue
1327 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001328 if srcProducer, ok := module.(SourceFileProducer); ok {
Nan Zhang27e284d2018-02-09 21:03:53 +00001329 moduleSrcs := srcProducer.Srcs()
1330 for _, e := range expandedExcludes {
1331 for j, ms := range moduleSrcs {
1332 if ms.String() == e {
1333 moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
1334 }
1335 }
1336 }
1337 expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001338 } else {
1339 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1340 }
1341 } else if pathtools.IsGlob(s) {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001342 globbedSrcFiles := ctx.GlobFiles(filepath.Join(prefix, s), expandedExcludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001343 for i, s := range globbedSrcFiles {
1344 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001345 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001346 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001347 } else {
Nan Zhang27e284d2018-02-09 21:03:53 +00001348 p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1349 j := findStringInSlice(p.String(), expandedExcludes)
1350 if j == -1 {
1351 expandedSrcFiles = append(expandedSrcFiles, p)
1352 }
1353
Colin Cross8f101b42015-06-17 15:09:06 -07001354 }
1355 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001356 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001357}
1358
Nan Zhang6d34b302017-02-04 17:47:46 -08001359func (ctx *androidModuleContext) RequiredModuleNames() []string {
1360 return ctx.module.base().commonProperties.Required
1361}
1362
Colin Cross7f19f372016-11-01 11:10:25 -07001363func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1364 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001365 if err != nil {
1366 ctx.ModuleErrorf("glob: %s", err.Error())
1367 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001368 return pathsForModuleSrcFromFullPath(ctx, ret, true)
Colin Crossfce53272015-04-08 11:21:40 -07001369}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001370
Nan Zhang581fd212018-01-10 16:06:12 -08001371func (ctx *androidModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001372 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Nan Zhang581fd212018-01-10 16:06:12 -08001373 if err != nil {
1374 ctx.ModuleErrorf("glob: %s", err.Error())
1375 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001376 return pathsForModuleSrcFromFullPath(ctx, ret, false)
Nan Zhang581fd212018-01-10 16:06:12 -08001377}
1378
Colin Cross463a90e2015-06-17 14:20:06 -07001379func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001380 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001381}
1382
Colin Cross0875c522017-11-28 17:34:01 -08001383func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001384 return &buildTargetSingleton{}
1385}
1386
Colin Cross87d8b562017-04-25 10:01:55 -07001387func parentDir(dir string) string {
1388 dir, _ = filepath.Split(dir)
1389 return filepath.Clean(dir)
1390}
1391
Colin Cross1f8c52b2015-06-16 16:38:17 -07001392type buildTargetSingleton struct{}
1393
Colin Cross0875c522017-11-28 17:34:01 -08001394func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1395 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001396
Colin Cross0875c522017-11-28 17:34:01 -08001397 mmTarget := func(dir string) WritablePath {
1398 return PathForPhony(ctx,
1399 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001400 }
1401
Colin Cross0875c522017-11-28 17:34:01 -08001402 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001403
Colin Cross0875c522017-11-28 17:34:01 -08001404 ctx.VisitAllModules(func(module Module) {
1405 blueprintDir := module.base().blueprintDir
1406 installTarget := module.base().installTarget
1407 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001408
Colin Cross0875c522017-11-28 17:34:01 -08001409 if checkbuildTarget != nil {
1410 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1411 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1412 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001413
Colin Cross0875c522017-11-28 17:34:01 -08001414 if installTarget != nil {
1415 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001416 }
1417 })
1418
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001419 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001420 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001421 suffix = "-soong"
1422 }
1423
Colin Cross1f8c52b2015-06-16 16:38:17 -07001424 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001425 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001426 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001427 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001428 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001429 })
1430
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001431 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001432 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001433 return
1434 }
1435
Colin Cross0875c522017-11-28 17:34:01 -08001436 sortedKeys := func(m map[string]Paths) []string {
1437 s := make([]string, 0, len(m))
1438 for k := range m {
1439 s = append(s, k)
1440 }
1441 sort.Strings(s)
1442 return s
1443 }
1444
Colin Cross87d8b562017-04-25 10:01:55 -07001445 // Ensure ancestor directories are in modulesInDir
1446 dirs := sortedKeys(modulesInDir)
1447 for _, dir := range dirs {
1448 dir := parentDir(dir)
1449 for dir != "." && dir != "/" {
1450 if _, exists := modulesInDir[dir]; exists {
1451 break
1452 }
1453 modulesInDir[dir] = nil
1454 dir = parentDir(dir)
1455 }
1456 }
1457
1458 // Make directories build their direct subdirectories
1459 dirs = sortedKeys(modulesInDir)
1460 for _, dir := range dirs {
1461 p := parentDir(dir)
1462 if p != "." && p != "/" {
1463 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1464 }
1465 }
1466
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001467 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1468 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1469 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001470 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001471 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001472 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001473 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001474 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001475 // HACK: checkbuild should be an optional build, but force it
1476 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001477 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001478 })
1479 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001480
1481 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1482 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001483 ctx.VisitAllModules(func(module Module) {
1484 if module.Enabled() {
1485 os := module.Target().Os
1486 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001487 }
1488 })
1489
Colin Cross0875c522017-11-28 17:34:01 -08001490 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001491 for os, deps := range osDeps {
1492 var className string
1493
1494 switch os.Class {
1495 case Host:
1496 className = "host"
1497 case HostCross:
1498 className = "host-cross"
1499 case Device:
1500 className = "target"
1501 default:
1502 continue
1503 }
1504
Colin Cross0875c522017-11-28 17:34:01 -08001505 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001506 osClass[className] = append(osClass[className], name)
1507
Colin Cross0875c522017-11-28 17:34:01 -08001508 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001509 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001510 Output: name,
1511 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001512 })
1513 }
1514
1515 // Wrap those into host|host-cross|target phony rules
1516 osClasses := sortedKeys(osClass)
1517 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001518 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001519 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001520 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001521 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001522 })
1523 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001524}
Colin Crossd779da42015-12-17 18:00:23 -08001525
1526type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001527 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -08001528 ctx interface {
1529 ModuleName(blueprint.Module) string
1530 ModuleSubDir(blueprint.Module) string
1531 }
1532}
1533
1534func (s AndroidModulesByName) Len() int { return len(s.slice) }
1535func (s AndroidModulesByName) Less(i, j int) bool {
1536 mi, mj := s.slice[i], s.slice[j]
1537 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1538
1539 if ni != nj {
1540 return ni < nj
1541 } else {
1542 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1543 }
1544}
1545func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }