blob: 01766b4478f8548a202f2590a23d6bc806786cbb [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 Crossa9d8bee2018-10-02 13:59:46 -0700445
446 prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool
Colin Cross36242852017-06-23 15:06:31 -0700447}
448
449func (a *ModuleBase) AddProperties(props ...interface{}) {
450 a.registerProps = append(a.registerProps, props...)
451}
452
453func (a *ModuleBase) GetProperties() []interface{} {
454 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800455}
456
Colin Crossae887032017-10-23 17:16:14 -0700457func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700458 return a.buildParams
459}
460
Colin Crossa9d8bee2018-10-02 13:59:46 -0700461func (a *ModuleBase) Prefer32(prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool) {
462 a.prefer32 = prefer32
463}
464
Colin Crossce75d2c2016-10-06 16:12:58 -0700465// Name returns the name of the module. It may be overridden by individual module types, for
466// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700467func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800468 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700469}
470
Colin Crossce75d2c2016-10-06 16:12:58 -0700471// BaseModuleName returns the name of the module as specified in the blueprints file.
472func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800473 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700474}
475
Colin Cross635c3b02016-05-18 15:37:25 -0700476func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800477 return a
478}
479
Colin Cross8b74d172016-09-13 09:59:14 -0700480func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700481 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700482 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700483}
484
Colin Crossa1ad8d12016-06-01 17:09:44 -0700485func (a *ModuleBase) Target() Target {
486 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800487}
488
Colin Cross8b74d172016-09-13 09:59:14 -0700489func (a *ModuleBase) TargetPrimary() bool {
490 return a.commonProperties.CompilePrimary
491}
492
Colin Crossa1ad8d12016-06-01 17:09:44 -0700493func (a *ModuleBase) Os() OsType {
494 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800495}
496
Colin Cross635c3b02016-05-18 15:37:25 -0700497func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700498 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800499}
500
Colin Cross635c3b02016-05-18 15:37:25 -0700501func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700502 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800503}
504
Dan Willemsen0b24c742016-10-04 15:13:37 -0700505func (a *ModuleBase) ArchSpecific() bool {
506 return a.commonProperties.ArchSpecific
507}
508
Colin Crossa1ad8d12016-06-01 17:09:44 -0700509func (a *ModuleBase) OsClassSupported() []OsClass {
510 switch a.commonProperties.HostOrDeviceSupported {
511 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700512 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700513 case HostSupportedNoCross:
514 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700515 case DeviceSupported:
516 return []OsClass{Device}
Dan Albert0981b5c2018-08-02 13:46:35 -0700517 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700518 var supported []OsClass
Dan Albert0981b5c2018-08-02 13:46:35 -0700519 if Bool(a.hostAndDeviceProperties.Host_supported) ||
520 (a.commonProperties.HostOrDeviceSupported == HostAndDeviceDefault &&
521 a.hostAndDeviceProperties.Host_supported == nil) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700522 supported = append(supported, Host, HostCross)
523 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700524 if a.hostAndDeviceProperties.Device_supported == nil ||
525 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700526 supported = append(supported, Device)
527 }
528 return supported
529 default:
530 return nil
531 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800532}
533
Colin Cross635c3b02016-05-18 15:37:25 -0700534func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800535 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
536 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700537 (a.hostAndDeviceProperties.Device_supported == nil ||
538 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800539}
540
Jiyong Parkc678ad32018-04-10 13:07:10 +0900541func (a *ModuleBase) Platform() bool {
Dario Frenifd05a742018-05-29 13:28:54 +0100542 return !a.DeviceSpecific() && !a.SocSpecific() && !a.ProductSpecific() && !a.ProductServicesSpecific()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900543}
544
545func (a *ModuleBase) DeviceSpecific() bool {
546 return Bool(a.commonProperties.Device_specific)
547}
548
549func (a *ModuleBase) SocSpecific() bool {
550 return Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
551}
552
553func (a *ModuleBase) ProductSpecific() bool {
554 return Bool(a.commonProperties.Product_specific)
555}
556
Dario Frenifd05a742018-05-29 13:28:54 +0100557func (a *ModuleBase) ProductServicesSpecific() bool {
Dario Freni95cf7672018-08-17 00:57:57 +0100558 return Bool(a.commonProperties.Product_services_specific)
Dario Frenifd05a742018-05-29 13:28:54 +0100559}
560
Colin Cross635c3b02016-05-18 15:37:25 -0700561func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800562 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800563 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800564 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800565 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800566}
567
Colin Crossce75d2c2016-10-06 16:12:58 -0700568func (a *ModuleBase) SkipInstall() {
569 a.commonProperties.SkipInstall = true
570}
571
Jiyong Park374510b2018-03-19 18:23:01 +0900572func (a *ModuleBase) ExportedToMake() bool {
573 return a.commonProperties.NamespaceExportedToMake
574}
575
Colin Cross635c3b02016-05-18 15:37:25 -0700576func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700577 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800578
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700579 result := Paths{}
Colin Cross6b753602018-06-21 13:03:07 -0700580 // TODO(ccross): we need to use WalkDeps and have some way to know which dependencies require installation
Colin Cross3f40fa42015-01-30 17:27:36 -0800581 ctx.VisitDepsDepthFirstIf(isFileInstaller,
582 func(m blueprint.Module) {
583 fileInstaller := m.(fileInstaller)
584 files := fileInstaller.filesToInstall()
585 result = append(result, files...)
586 })
587
588 return result
589}
590
Colin Cross635c3b02016-05-18 15:37:25 -0700591func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800592 return a.installFiles
593}
594
Colin Cross635c3b02016-05-18 15:37:25 -0700595func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800596 return p.noAddressSanitizer
597}
598
Colin Cross635c3b02016-05-18 15:37:25 -0700599func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800600 return false
601}
602
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700603func (p *ModuleBase) InstallInSanitizerDir() bool {
604 return false
605}
606
Jiyong Parkf9332f12018-02-01 00:54:12 +0900607func (p *ModuleBase) InstallInRecovery() bool {
608 return Bool(p.commonProperties.Recovery)
609}
610
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900611func (a *ModuleBase) Owner() string {
612 return String(a.commonProperties.Owner)
613}
614
Colin Cross0875c522017-11-28 17:34:01 -0800615func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700616 allInstalledFiles := Paths{}
617 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800618 ctx.VisitAllModuleVariants(func(module Module) {
619 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700620 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
621 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800622 })
623
Colin Cross0875c522017-11-28 17:34:01 -0800624 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700625
Jeff Gaston088e29e2017-11-29 16:47:17 -0800626 namespacePrefix := ctx.Namespace().(*Namespace).id
627 if namespacePrefix != "" {
628 namespacePrefix = namespacePrefix + "-"
629 }
630
Colin Cross3f40fa42015-01-30 17:27:36 -0800631 if len(allInstalledFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800632 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800633 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700634 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800635 Output: name,
636 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800637 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700638 })
639 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700640 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700641 }
642
643 if len(allCheckbuildFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800644 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800645 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700646 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800647 Output: name,
648 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700649 })
650 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700651 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700652 }
653
654 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800655 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800656 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800657 suffix = "-soong"
658 }
659
Jeff Gaston088e29e2017-11-29 16:47:17 -0800660 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800661 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700662 Rule: blueprint.Phony,
Jeff Gaston088e29e2017-11-29 16:47:17 -0800663 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700664 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800665 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700666
667 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800668 }
669}
670
Jiyong Park2db76922017-11-08 16:03:48 +0900671func determineModuleKind(a *ModuleBase, ctx blueprint.BaseModuleContext) moduleKind {
672 var socSpecific = Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
673 var deviceSpecific = Bool(a.commonProperties.Device_specific)
674 var productSpecific = Bool(a.commonProperties.Product_specific)
Dario Freni95cf7672018-08-17 00:57:57 +0100675 var productServicesSpecific = Bool(a.commonProperties.Product_services_specific)
Jiyong Park2db76922017-11-08 16:03:48 +0900676
Dario Frenifd05a742018-05-29 13:28:54 +0100677 msg := "conflicting value set here"
678 if socSpecific && deviceSpecific {
679 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
Jiyong Park2db76922017-11-08 16:03:48 +0900680 if Bool(a.commonProperties.Vendor) {
681 ctx.PropertyErrorf("vendor", msg)
682 }
683 if Bool(a.commonProperties.Proprietary) {
684 ctx.PropertyErrorf("proprietary", msg)
685 }
686 if Bool(a.commonProperties.Soc_specific) {
687 ctx.PropertyErrorf("soc_specific", msg)
688 }
689 }
690
Dario Frenifd05a742018-05-29 13:28:54 +0100691 if productSpecific && productServicesSpecific {
692 ctx.PropertyErrorf("product_specific", "a module cannot be specific to product and product_services at the same time.")
693 ctx.PropertyErrorf("product_services_specific", msg)
694 }
695
696 if (socSpecific || deviceSpecific) && (productSpecific || productServicesSpecific) {
697 if productSpecific {
698 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
699 } else {
700 ctx.PropertyErrorf("product_services_specific", "a module cannot be specific to SoC or device and product_services at the same time.")
701 }
702 if deviceSpecific {
703 ctx.PropertyErrorf("device_specific", msg)
704 } else {
705 if Bool(a.commonProperties.Vendor) {
706 ctx.PropertyErrorf("vendor", msg)
707 }
708 if Bool(a.commonProperties.Proprietary) {
709 ctx.PropertyErrorf("proprietary", msg)
710 }
711 if Bool(a.commonProperties.Soc_specific) {
712 ctx.PropertyErrorf("soc_specific", msg)
713 }
714 }
715 }
716
Jiyong Park2db76922017-11-08 16:03:48 +0900717 if productSpecific {
718 return productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100719 } else if productServicesSpecific {
720 return productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900721 } else if deviceSpecific {
722 return deviceSpecificModule
723 } else if socSpecific {
724 return socSpecificModule
725 } else {
726 return platformModule
727 }
728}
729
Colin Cross635c3b02016-05-18 15:37:25 -0700730func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700731 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700732 target: a.commonProperties.CompileTarget,
733 targetPrimary: a.commonProperties.CompilePrimary,
Jiyong Park2db76922017-11-08 16:03:48 +0900734 kind: determineModuleKind(a, ctx),
Colin Cross8b74d172016-09-13 09:59:14 -0700735 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800736 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800737}
738
Colin Cross0875c522017-11-28 17:34:01 -0800739func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
740 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700741 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800742 ModuleContext: blueprintCtx,
743 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
744 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700745 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800746 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800747 }
748
Colin Cross67a5c132017-05-09 13:45:28 -0700749 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
750 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800751 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
752 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700753 }
Colin Cross0875c522017-11-28 17:34:01 -0800754 if !ctx.PrimaryArch() {
755 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700756 }
757
758 ctx.Variable(pctx, "moduleDesc", desc)
759
760 s := ""
761 if len(suffix) > 0 {
762 s = " [" + strings.Join(suffix, " ") + "]"
763 }
764 ctx.Variable(pctx, "moduleDescSuffix", s)
765
Colin Cross9b1d13d2016-09-19 15:18:11 -0700766 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800767 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700768 if ctx.Failed() {
769 return
770 }
771
Colin Cross0875c522017-11-28 17:34:01 -0800772 a.installFiles = append(a.installFiles, ctx.installFiles...)
773 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800774 }
775
Colin Cross9b1d13d2016-09-19 15:18:11 -0700776 if a == ctx.FinalModule().(Module).base() {
777 a.generateModuleTarget(ctx)
778 if ctx.Failed() {
779 return
780 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800781 }
Colin Crosscec81712017-07-13 14:43:27 -0700782
Colin Cross0875c522017-11-28 17:34:01 -0800783 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800784}
785
Colin Crossf6566ed2015-03-24 11:13:38 -0700786type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700787 target Target
788 targetPrimary bool
789 debug bool
Jiyong Park2db76922017-11-08 16:03:48 +0900790 kind moduleKind
Colin Cross8b74d172016-09-13 09:59:14 -0700791 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700792}
793
Colin Cross3f40fa42015-01-30 17:27:36 -0800794type androidModuleContext struct {
795 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700796 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700797 installDeps Paths
798 installFiles Paths
799 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800800 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700801 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700802
803 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700804 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800805}
806
Colin Cross67a5c132017-05-09 13:45:28 -0700807func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800808 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700809 Rule: ErrorRule,
810 Description: desc,
811 Outputs: outputs,
812 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800813 Args: map[string]string{
814 "error": err.Error(),
815 },
816 })
817 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800818}
819
Colin Crossaabf6792017-11-29 00:27:14 -0800820func (a *androidModuleContext) Config() Config {
821 return a.ModuleContext.Config().(Config)
822}
823
Colin Cross0875c522017-11-28 17:34:01 -0800824func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700825 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800826}
827
Colin Cross0875c522017-11-28 17:34:01 -0800828func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700829 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700830 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800831 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800832 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700833 Outputs: params.Outputs.Strings(),
834 ImplicitOutputs: params.ImplicitOutputs.Strings(),
835 Inputs: params.Inputs.Strings(),
836 Implicits: params.Implicits.Strings(),
837 OrderOnly: params.OrderOnly.Strings(),
838 Args: params.Args,
839 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700840 }
841
Colin Cross33bfb0a2016-11-21 17:23:08 -0800842 if params.Depfile != nil {
843 bparams.Depfile = params.Depfile.String()
844 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700845 if params.Output != nil {
846 bparams.Outputs = append(bparams.Outputs, params.Output.String())
847 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700848 if params.ImplicitOutput != nil {
849 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
850 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700851 if params.Input != nil {
852 bparams.Inputs = append(bparams.Inputs, params.Input.String())
853 }
854 if params.Implicit != nil {
855 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
856 }
857
Colin Crossfe4bc362018-09-12 10:02:13 -0700858 bparams.Outputs = proptools.NinjaEscape(bparams.Outputs)
859 bparams.ImplicitOutputs = proptools.NinjaEscape(bparams.ImplicitOutputs)
860 bparams.Inputs = proptools.NinjaEscape(bparams.Inputs)
861 bparams.Implicits = proptools.NinjaEscape(bparams.Implicits)
862 bparams.OrderOnly = proptools.NinjaEscape(bparams.OrderOnly)
863 bparams.Depfile = proptools.NinjaEscape([]string{bparams.Depfile})[0]
864
Colin Cross0875c522017-11-28 17:34:01 -0800865 return bparams
866}
867
868func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
869 a.ModuleContext.Variable(pctx.PackageContext, name, value)
870}
871
872func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
873 argNames ...string) blueprint.Rule {
874
875 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
876}
877
878func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
879 if a.config.captureBuild {
880 a.buildParams = append(a.buildParams, params)
881 }
882
883 bparams := convertBuildParams(params)
884
885 if bparams.Description != "" {
886 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
887 }
888
Colin Cross6ff51382015-12-17 16:39:19 -0800889 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700890 a.ninjaError(bparams.Description, bparams.Outputs,
891 fmt.Errorf("module %s missing dependencies: %s\n",
892 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800893 return
894 }
895
Colin Cross0875c522017-11-28 17:34:01 -0800896 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700897}
898
Colin Cross6ff51382015-12-17 16:39:19 -0800899func (a *androidModuleContext) GetMissingDependencies() []string {
900 return a.missingDeps
901}
902
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800903func (a *androidModuleContext) AddMissingDependencies(deps []string) {
904 if deps != nil {
905 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700906 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800907 }
908}
909
Colin Crossd11fcda2017-10-23 17:59:01 -0700910func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
911 aModule, _ := module.(Module)
912 if aModule == nil {
913 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
914 return nil
915 }
916
917 if !aModule.Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800918 if a.Config().AllowMissingDependencies() {
Colin Crossd11fcda2017-10-23 17:59:01 -0700919 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
920 } else {
921 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
922 }
923 return nil
924 }
925
926 return aModule
927}
928
Colin Cross35143d02017-11-16 00:11:20 -0800929func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
930 a.ModuleContext.VisitDirectDeps(visit)
931}
932
Colin Crossd11fcda2017-10-23 17:59:01 -0700933func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
934 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
935 if aModule := a.validateAndroidModule(module); aModule != nil {
936 visit(aModule)
937 }
938 })
939}
940
Colin Crossee6143c2017-12-30 17:54:27 -0800941func (a *androidModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
942 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
943 if aModule := a.validateAndroidModule(module); aModule != nil {
944 if a.ModuleContext.OtherModuleDependencyTag(aModule) == tag {
945 visit(aModule)
946 }
947 }
948 })
949}
950
Colin Crossd11fcda2017-10-23 17:59:01 -0700951func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
952 a.ModuleContext.VisitDirectDepsIf(
953 // pred
954 func(module blueprint.Module) bool {
955 if aModule := a.validateAndroidModule(module); aModule != nil {
956 return pred(aModule)
957 } else {
958 return false
959 }
960 },
961 // visit
962 func(module blueprint.Module) {
963 visit(module.(Module))
964 })
965}
966
967func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
968 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
969 if aModule := a.validateAndroidModule(module); aModule != nil {
970 visit(aModule)
971 }
972 })
973}
974
975func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
976 a.ModuleContext.VisitDepsDepthFirstIf(
977 // pred
978 func(module blueprint.Module) bool {
979 if aModule := a.validateAndroidModule(module); aModule != nil {
980 return pred(aModule)
981 } else {
982 return false
983 }
984 },
985 // visit
986 func(module blueprint.Module) {
987 visit(module.(Module))
988 })
989}
990
991func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
992 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
993 childAndroidModule := a.validateAndroidModule(child)
994 parentAndroidModule := a.validateAndroidModule(parent)
995 if childAndroidModule != nil && parentAndroidModule != nil {
996 return visit(childAndroidModule, parentAndroidModule)
997 } else {
998 return false
999 }
1000 })
1001}
1002
Colin Cross0875c522017-11-28 17:34:01 -08001003func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
1004 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
1005 visit(module.(Module))
1006 })
1007}
1008
1009func (a *androidModuleContext) PrimaryModule() Module {
1010 return a.ModuleContext.PrimaryModule().(Module)
1011}
1012
1013func (a *androidModuleContext) FinalModule() Module {
1014 return a.ModuleContext.FinalModule().(Module)
1015}
1016
Colin Crossa1ad8d12016-06-01 17:09:44 -07001017func (a *androidBaseContextImpl) Target() Target {
1018 return a.target
1019}
1020
Colin Cross8b74d172016-09-13 09:59:14 -07001021func (a *androidBaseContextImpl) TargetPrimary() bool {
1022 return a.targetPrimary
1023}
1024
Colin Crossf6566ed2015-03-24 11:13:38 -07001025func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001026 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -08001027}
1028
Colin Crossa1ad8d12016-06-01 17:09:44 -07001029func (a *androidBaseContextImpl) Os() OsType {
1030 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -08001031}
1032
Colin Crossf6566ed2015-03-24 11:13:38 -07001033func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001034 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -07001035}
1036
1037func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001038 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -07001039}
1040
Colin Cross0af4b842015-04-30 16:36:18 -07001041func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001042 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -07001043}
1044
Colin Cross3edeee12017-04-04 12:59:48 -07001045func (a *androidBaseContextImpl) Windows() bool {
1046 return a.target.Os == Windows
1047}
1048
Colin Crossf6566ed2015-03-24 11:13:38 -07001049func (a *androidBaseContextImpl) Debug() bool {
1050 return a.debug
1051}
1052
Colin Cross1e7d3702016-08-24 15:25:47 -07001053func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -07001054 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
1055 return true
1056 }
Colin Cross1e7d3702016-08-24 15:25:47 -07001057 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
1058}
1059
Colin Cross1332b002015-04-07 17:11:30 -07001060func (a *androidBaseContextImpl) AConfig() Config {
1061 return a.config
1062}
1063
Colin Cross9272ade2016-08-17 15:24:12 -07001064func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
1065 return DeviceConfig{a.config.deviceConfig}
1066}
1067
Jiyong Park2db76922017-11-08 16:03:48 +09001068func (a *androidBaseContextImpl) Platform() bool {
1069 return a.kind == platformModule
1070}
1071
1072func (a *androidBaseContextImpl) DeviceSpecific() bool {
1073 return a.kind == deviceSpecificModule
1074}
1075
1076func (a *androidBaseContextImpl) SocSpecific() bool {
1077 return a.kind == socSpecificModule
1078}
1079
1080func (a *androidBaseContextImpl) ProductSpecific() bool {
1081 return a.kind == productSpecificModule
Dan Willemsen782a2d12015-12-21 14:55:28 -08001082}
1083
Dario Frenifd05a742018-05-29 13:28:54 +01001084func (a *androidBaseContextImpl) ProductServicesSpecific() bool {
1085 return a.kind == productServicesSpecificModule
1086}
1087
Jiyong Park5baac542018-08-28 09:55:37 +09001088// Makes this module a platform module, i.e. not specific to soc, device,
1089// product, or product_services.
1090func (a *ModuleBase) MakeAsPlatform() {
1091 a.commonProperties.Vendor = boolPtr(false)
1092 a.commonProperties.Proprietary = boolPtr(false)
1093 a.commonProperties.Soc_specific = boolPtr(false)
1094 a.commonProperties.Product_specific = boolPtr(false)
1095 a.commonProperties.Product_services_specific = boolPtr(false)
1096}
1097
Colin Cross8d8f8e22016-08-03 11:57:50 -07001098func (a *androidModuleContext) InstallInData() bool {
1099 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08001100}
1101
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001102func (a *androidModuleContext) InstallInSanitizerDir() bool {
1103 return a.module.InstallInSanitizerDir()
1104}
1105
Jiyong Parkf9332f12018-02-01 00:54:12 +09001106func (a *androidModuleContext) InstallInRecovery() bool {
1107 return a.module.InstallInRecovery()
1108}
1109
Colin Cross893d8162017-04-26 17:34:03 -07001110func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
1111 if a.module.base().commonProperties.SkipInstall {
1112 return true
1113 }
1114
Colin Cross3607f212018-05-07 15:28:05 -07001115 // We'll need a solution for choosing which of modules with the same name in different
1116 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
1117 // list of namespaces to install in a Soong-only build.
1118 if !a.module.base().commonProperties.NamespaceExportedToMake {
1119 return true
1120 }
1121
Colin Cross893d8162017-04-26 17:34:03 -07001122 if a.Device() {
Colin Cross6510f912017-11-29 00:27:14 -08001123 if a.Config().SkipDeviceInstall() {
Colin Cross893d8162017-04-26 17:34:03 -07001124 return true
1125 }
1126
Colin Cross6510f912017-11-29 00:27:14 -08001127 if a.Config().SkipMegaDeviceInstall(fullInstallPath.String()) {
Colin Cross893d8162017-04-26 17:34:03 -07001128 return true
1129 }
1130 }
1131
1132 return false
1133}
1134
Colin Cross5c517922017-08-31 12:29:17 -07001135func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -07001136 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -07001137 return a.installFile(installPath, name, srcPath, Cp, deps)
1138}
1139
1140func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
1141 deps ...Path) OutputPath {
1142 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
1143}
1144
1145func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
1146 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -07001147
Dan Willemsen782a2d12015-12-21 14:55:28 -08001148 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001149 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001150
Colin Cross893d8162017-04-26 17:34:03 -07001151 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001152
Dan Willemsen322acaf2016-01-12 23:07:05 -08001153 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -07001154
Colin Cross89562dc2016-10-03 17:47:19 -07001155 var implicitDeps, orderOnlyDeps Paths
1156
1157 if a.Host() {
1158 // Installed host modules might be used during the build, depend directly on their
1159 // dependencies so their timestamp is updated whenever their dependency is updated
1160 implicitDeps = deps
1161 } else {
1162 orderOnlyDeps = deps
1163 }
1164
Colin Crossae887032017-10-23 17:16:14 -07001165 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07001166 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07001167 Description: "install " + fullInstallPath.Base(),
1168 Output: fullInstallPath,
1169 Input: srcPath,
1170 Implicits: implicitDeps,
1171 OrderOnly: orderOnlyDeps,
Colin Cross6510f912017-11-29 00:27:14 -08001172 Default: !a.Config().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08001173 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001174
Dan Willemsen322acaf2016-01-12 23:07:05 -08001175 a.installFiles = append(a.installFiles, fullInstallPath)
1176 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001177 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -07001178 return fullInstallPath
1179}
1180
Colin Cross3854a602016-01-11 12:49:11 -08001181func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
1182 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001183 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08001184
Colin Cross893d8162017-04-26 17:34:03 -07001185 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001186
Colin Crossae887032017-10-23 17:16:14 -07001187 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001188 Rule: Symlink,
1189 Description: "install symlink " + fullInstallPath.Base(),
1190 Output: fullInstallPath,
1191 OrderOnly: Paths{srcPath},
Colin Cross6510f912017-11-29 00:27:14 -08001192 Default: !a.Config().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -08001193 Args: map[string]string{
1194 "fromPath": srcPath.String(),
1195 },
1196 })
Colin Cross3854a602016-01-11 12:49:11 -08001197
Colin Cross12fc4972016-01-11 12:49:11 -08001198 a.installFiles = append(a.installFiles, fullInstallPath)
1199 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1200 }
Colin Cross3854a602016-01-11 12:49:11 -08001201 return fullInstallPath
1202}
1203
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001204func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001205 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1206}
1207
Colin Cross3f40fa42015-01-30 17:27:36 -08001208type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001209 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001210}
1211
1212func isFileInstaller(m blueprint.Module) bool {
1213 _, ok := m.(fileInstaller)
1214 return ok
1215}
1216
1217func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -07001218 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -08001219 return ok
1220}
Colin Crossfce53272015-04-08 11:21:40 -07001221
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001222func findStringInSlice(str string, slice []string) int {
1223 for i, s := range slice {
1224 if s == str {
1225 return i
Colin Crossfce53272015-04-08 11:21:40 -07001226 }
1227 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001228 return -1
1229}
1230
Colin Cross068e0fe2016-12-13 15:23:47 -08001231func SrcIsModule(s string) string {
1232 if len(s) > 1 && s[0] == ':' {
1233 return s[1:]
1234 }
1235 return ""
1236}
1237
1238type sourceDependencyTag struct {
1239 blueprint.BaseDependencyTag
1240}
1241
1242var SourceDepTag sourceDependencyTag
1243
Colin Cross366938f2017-12-11 16:29:02 -08001244// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
1245// using ":module" syntax, if any.
Colin Cross068e0fe2016-12-13 15:23:47 -08001246func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1247 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001248 set := make(map[string]bool)
1249
Colin Cross068e0fe2016-12-13 15:23:47 -08001250 for _, s := range srcFiles {
1251 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001252 if _, found := set[m]; found {
1253 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1254 } else {
1255 set[m] = true
1256 deps = append(deps, m)
1257 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001258 }
1259 }
1260
1261 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1262}
1263
Colin Cross366938f2017-12-11 16:29:02 -08001264// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
1265// using ":module" syntax, if any.
1266func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
1267 if s != nil {
1268 if m := SrcIsModule(*s); m != "" {
1269 ctx.AddDependency(ctx.Module(), SourceDepTag, m)
1270 }
1271 }
1272}
1273
Colin Cross068e0fe2016-12-13 15:23:47 -08001274type SourceFileProducer interface {
1275 Srcs() Paths
1276}
1277
1278// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001279// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001280func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001281 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1282}
1283
Colin Cross366938f2017-12-11 16:29:02 -08001284// Returns a single path expanded from globs and modules referenced using ":module" syntax.
1285// ExtractSourceDeps must have already been called during the dependency resolution phase.
1286func (ctx *androidModuleContext) ExpandSource(srcFile, prop string) Path {
1287 srcFiles := ctx.ExpandSourcesSubDir([]string{srcFile}, nil, "")
1288 if len(srcFiles) == 1 {
1289 return srcFiles[0]
1290 } else {
1291 ctx.PropertyErrorf(prop, "module providing %s must produce exactly one file", prop)
1292 return nil
1293 }
1294}
1295
Colin Cross2383f3b2018-02-06 14:40:13 -08001296// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
1297// the srcFile is non-nil.
1298// ExtractSourceDeps must have already been called during the dependency resolution phase.
1299func (ctx *androidModuleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
1300 if srcFile != nil {
1301 return OptionalPathForPath(ctx.ExpandSource(*srcFile, prop))
1302 }
1303 return OptionalPath{}
1304}
1305
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001306func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001307 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001308
Colin Cross461b4452018-02-23 09:22:42 -08001309 var expandedExcludes []string
1310 if excludes != nil {
1311 expandedExcludes = make([]string, 0, len(excludes))
1312 }
Nan Zhang27e284d2018-02-09 21:03:53 +00001313
1314 for _, e := range excludes {
1315 if m := SrcIsModule(e); m != "" {
1316 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
1317 if module == nil {
1318 // Error will have been handled by ExtractSourcesDeps
1319 continue
1320 }
1321 if srcProducer, ok := module.(SourceFileProducer); ok {
1322 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
1323 } else {
1324 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1325 }
1326 } else {
1327 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001328 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001329 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001330 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001331 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001332 if m := SrcIsModule(s); m != "" {
1333 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001334 if module == nil {
1335 // Error will have been handled by ExtractSourcesDeps
1336 continue
1337 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001338 if srcProducer, ok := module.(SourceFileProducer); ok {
Nan Zhang27e284d2018-02-09 21:03:53 +00001339 moduleSrcs := srcProducer.Srcs()
1340 for _, e := range expandedExcludes {
1341 for j, ms := range moduleSrcs {
1342 if ms.String() == e {
1343 moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
1344 }
1345 }
1346 }
1347 expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001348 } else {
1349 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1350 }
1351 } else if pathtools.IsGlob(s) {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001352 globbedSrcFiles := ctx.GlobFiles(filepath.Join(prefix, s), expandedExcludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001353 for i, s := range globbedSrcFiles {
1354 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001355 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001356 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001357 } else {
Nan Zhang27e284d2018-02-09 21:03:53 +00001358 p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1359 j := findStringInSlice(p.String(), expandedExcludes)
1360 if j == -1 {
1361 expandedSrcFiles = append(expandedSrcFiles, p)
1362 }
1363
Colin Cross8f101b42015-06-17 15:09:06 -07001364 }
1365 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001366 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001367}
1368
Nan Zhang6d34b302017-02-04 17:47:46 -08001369func (ctx *androidModuleContext) RequiredModuleNames() []string {
1370 return ctx.module.base().commonProperties.Required
1371}
1372
Colin Cross7f19f372016-11-01 11:10:25 -07001373func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1374 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001375 if err != nil {
1376 ctx.ModuleErrorf("glob: %s", err.Error())
1377 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001378 return pathsForModuleSrcFromFullPath(ctx, ret, true)
Colin Crossfce53272015-04-08 11:21:40 -07001379}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001380
Nan Zhang581fd212018-01-10 16:06:12 -08001381func (ctx *androidModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001382 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Nan Zhang581fd212018-01-10 16:06:12 -08001383 if err != nil {
1384 ctx.ModuleErrorf("glob: %s", err.Error())
1385 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001386 return pathsForModuleSrcFromFullPath(ctx, ret, false)
Nan Zhang581fd212018-01-10 16:06:12 -08001387}
1388
Colin Cross463a90e2015-06-17 14:20:06 -07001389func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001390 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001391}
1392
Colin Cross0875c522017-11-28 17:34:01 -08001393func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001394 return &buildTargetSingleton{}
1395}
1396
Colin Cross87d8b562017-04-25 10:01:55 -07001397func parentDir(dir string) string {
1398 dir, _ = filepath.Split(dir)
1399 return filepath.Clean(dir)
1400}
1401
Colin Cross1f8c52b2015-06-16 16:38:17 -07001402type buildTargetSingleton struct{}
1403
Colin Cross0875c522017-11-28 17:34:01 -08001404func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1405 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001406
Colin Cross0875c522017-11-28 17:34:01 -08001407 mmTarget := func(dir string) WritablePath {
1408 return PathForPhony(ctx,
1409 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001410 }
1411
Colin Cross0875c522017-11-28 17:34:01 -08001412 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001413
Colin Cross0875c522017-11-28 17:34:01 -08001414 ctx.VisitAllModules(func(module Module) {
1415 blueprintDir := module.base().blueprintDir
1416 installTarget := module.base().installTarget
1417 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001418
Colin Cross0875c522017-11-28 17:34:01 -08001419 if checkbuildTarget != nil {
1420 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1421 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1422 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001423
Colin Cross0875c522017-11-28 17:34:01 -08001424 if installTarget != nil {
1425 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001426 }
1427 })
1428
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001429 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001430 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001431 suffix = "-soong"
1432 }
1433
Colin Cross1f8c52b2015-06-16 16:38:17 -07001434 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001435 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001436 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001437 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001438 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001439 })
1440
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001441 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001442 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001443 return
1444 }
1445
Colin Cross0875c522017-11-28 17:34:01 -08001446 sortedKeys := func(m map[string]Paths) []string {
1447 s := make([]string, 0, len(m))
1448 for k := range m {
1449 s = append(s, k)
1450 }
1451 sort.Strings(s)
1452 return s
1453 }
1454
Colin Cross87d8b562017-04-25 10:01:55 -07001455 // Ensure ancestor directories are in modulesInDir
1456 dirs := sortedKeys(modulesInDir)
1457 for _, dir := range dirs {
1458 dir := parentDir(dir)
1459 for dir != "." && dir != "/" {
1460 if _, exists := modulesInDir[dir]; exists {
1461 break
1462 }
1463 modulesInDir[dir] = nil
1464 dir = parentDir(dir)
1465 }
1466 }
1467
1468 // Make directories build their direct subdirectories
1469 dirs = sortedKeys(modulesInDir)
1470 for _, dir := range dirs {
1471 p := parentDir(dir)
1472 if p != "." && p != "/" {
1473 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1474 }
1475 }
1476
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001477 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1478 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1479 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001480 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001481 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001482 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001483 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001484 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001485 // HACK: checkbuild should be an optional build, but force it
1486 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001487 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001488 })
1489 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001490
1491 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1492 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001493 ctx.VisitAllModules(func(module Module) {
1494 if module.Enabled() {
1495 os := module.Target().Os
1496 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001497 }
1498 })
1499
Colin Cross0875c522017-11-28 17:34:01 -08001500 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001501 for os, deps := range osDeps {
1502 var className string
1503
1504 switch os.Class {
1505 case Host:
1506 className = "host"
1507 case HostCross:
1508 className = "host-cross"
1509 case Device:
1510 className = "target"
1511 default:
1512 continue
1513 }
1514
Colin Cross0875c522017-11-28 17:34:01 -08001515 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001516 osClass[className] = append(osClass[className], name)
1517
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: name,
1521 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001522 })
1523 }
1524
1525 // Wrap those into host|host-cross|target phony rules
1526 osClasses := sortedKeys(osClass)
1527 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001528 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001529 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001530 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001531 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001532 })
1533 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001534}
Colin Crossd779da42015-12-17 18:00:23 -08001535
Colin Cross2465c3d2018-09-28 10:19:18 -07001536type ModulesByName struct {
1537 slice []blueprint.Module
Colin Crossd779da42015-12-17 18:00:23 -08001538 ctx interface {
1539 ModuleName(blueprint.Module) string
1540 ModuleSubDir(blueprint.Module) string
1541 }
1542}
1543
Colin Cross2465c3d2018-09-28 10:19:18 -07001544func (s ModulesByName) Len() int { return len(s.slice) }
1545func (s ModulesByName) Less(i, j int) bool {
Colin Crossd779da42015-12-17 18:00:23 -08001546 mi, mj := s.slice[i], s.slice[j]
1547 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1548
1549 if ni != nj {
1550 return ni < nj
1551 } else {
1552 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1553 }
1554}
Colin Cross2465c3d2018-09-28 10:19:18 -07001555func (s ModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001556
1557// Collect information for opening IDE project files in java/jdeps.go.
1558type IDEInfo interface {
1559 IDEInfo(ideInfo *IdeInfo)
1560 BaseModuleName() string
1561}
1562
1563// Extract the base module name from the Import name.
1564// Often the Import name has a prefix "prebuilt_".
1565// Remove the prefix explicitly if needed
1566// until we find a better solution to get the Import name.
1567type IDECustomizedModuleName interface {
1568 IDECustomizedModuleName() string
1569}
1570
1571type IdeInfo struct {
1572 Deps []string `json:"dependencies,omitempty"`
1573 Srcs []string `json:"srcs,omitempty"`
1574 Aidl_include_dirs []string `json:"aidl_include_dirs,omitempty"`
1575 Jarjar_rules []string `json:"jarjar_rules,omitempty"`
1576 Jars []string `json:"jars,omitempty"`
1577 Classes []string `json:"class,omitempty"`
1578 Installed_paths []string `json:"installed,omitempty"`
1579}