blob: 92b11ed0dd5ebf44fc62c303a7d99f549ef6047e [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
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900605func (a *ModuleBase) Owner() string {
606 return String(a.commonProperties.Owner)
607}
608
Colin Cross0875c522017-11-28 17:34:01 -0800609func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700610 allInstalledFiles := Paths{}
611 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800612 ctx.VisitAllModuleVariants(func(module Module) {
613 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700614 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
615 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800616 })
617
Colin Cross0875c522017-11-28 17:34:01 -0800618 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700619
Jeff Gaston088e29e2017-11-29 16:47:17 -0800620 namespacePrefix := ctx.Namespace().(*Namespace).id
621 if namespacePrefix != "" {
622 namespacePrefix = namespacePrefix + "-"
623 }
624
Colin Cross3f40fa42015-01-30 17:27:36 -0800625 if len(allInstalledFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800626 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800627 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700628 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800629 Output: name,
630 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800631 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700632 })
633 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700634 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700635 }
636
637 if len(allCheckbuildFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800638 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800639 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700640 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800641 Output: name,
642 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700643 })
644 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700645 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700646 }
647
648 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800649 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800650 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800651 suffix = "-soong"
652 }
653
Jeff Gaston088e29e2017-11-29 16:47:17 -0800654 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800655 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700656 Rule: blueprint.Phony,
Jeff Gaston088e29e2017-11-29 16:47:17 -0800657 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700658 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800659 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700660
661 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800662 }
663}
664
Jiyong Park2db76922017-11-08 16:03:48 +0900665func determineModuleKind(a *ModuleBase, ctx blueprint.BaseModuleContext) moduleKind {
666 var socSpecific = Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
667 var deviceSpecific = Bool(a.commonProperties.Device_specific)
668 var productSpecific = Bool(a.commonProperties.Product_specific)
Dario Freni95cf7672018-08-17 00:57:57 +0100669 var productServicesSpecific = Bool(a.commonProperties.Product_services_specific)
Jiyong Park2db76922017-11-08 16:03:48 +0900670
Dario Frenifd05a742018-05-29 13:28:54 +0100671 msg := "conflicting value set here"
672 if socSpecific && deviceSpecific {
673 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
Jiyong Park2db76922017-11-08 16:03:48 +0900674 if Bool(a.commonProperties.Vendor) {
675 ctx.PropertyErrorf("vendor", msg)
676 }
677 if Bool(a.commonProperties.Proprietary) {
678 ctx.PropertyErrorf("proprietary", msg)
679 }
680 if Bool(a.commonProperties.Soc_specific) {
681 ctx.PropertyErrorf("soc_specific", msg)
682 }
683 }
684
Dario Frenifd05a742018-05-29 13:28:54 +0100685 if productSpecific && productServicesSpecific {
686 ctx.PropertyErrorf("product_specific", "a module cannot be specific to product and product_services at the same time.")
687 ctx.PropertyErrorf("product_services_specific", msg)
688 }
689
690 if (socSpecific || deviceSpecific) && (productSpecific || productServicesSpecific) {
691 if productSpecific {
692 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
693 } else {
694 ctx.PropertyErrorf("product_services_specific", "a module cannot be specific to SoC or device and product_services at the same time.")
695 }
696 if deviceSpecific {
697 ctx.PropertyErrorf("device_specific", msg)
698 } else {
699 if Bool(a.commonProperties.Vendor) {
700 ctx.PropertyErrorf("vendor", msg)
701 }
702 if Bool(a.commonProperties.Proprietary) {
703 ctx.PropertyErrorf("proprietary", msg)
704 }
705 if Bool(a.commonProperties.Soc_specific) {
706 ctx.PropertyErrorf("soc_specific", msg)
707 }
708 }
709 }
710
Jiyong Park2db76922017-11-08 16:03:48 +0900711 if productSpecific {
712 return productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100713 } else if productServicesSpecific {
714 return productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900715 } else if deviceSpecific {
716 return deviceSpecificModule
717 } else if socSpecific {
718 return socSpecificModule
719 } else {
720 return platformModule
721 }
722}
723
Colin Cross635c3b02016-05-18 15:37:25 -0700724func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700725 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700726 target: a.commonProperties.CompileTarget,
727 targetPrimary: a.commonProperties.CompilePrimary,
Jiyong Park2db76922017-11-08 16:03:48 +0900728 kind: determineModuleKind(a, ctx),
Colin Cross8b74d172016-09-13 09:59:14 -0700729 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800730 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800731}
732
Colin Cross0875c522017-11-28 17:34:01 -0800733func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
734 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700735 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800736 ModuleContext: blueprintCtx,
737 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
738 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700739 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800740 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800741 }
742
Colin Cross67a5c132017-05-09 13:45:28 -0700743 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
744 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800745 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
746 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700747 }
Colin Cross0875c522017-11-28 17:34:01 -0800748 if !ctx.PrimaryArch() {
749 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700750 }
751
752 ctx.Variable(pctx, "moduleDesc", desc)
753
754 s := ""
755 if len(suffix) > 0 {
756 s = " [" + strings.Join(suffix, " ") + "]"
757 }
758 ctx.Variable(pctx, "moduleDescSuffix", s)
759
Colin Cross9b1d13d2016-09-19 15:18:11 -0700760 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800761 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700762 if ctx.Failed() {
763 return
764 }
765
Colin Cross0875c522017-11-28 17:34:01 -0800766 a.installFiles = append(a.installFiles, ctx.installFiles...)
767 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800768 }
769
Colin Cross9b1d13d2016-09-19 15:18:11 -0700770 if a == ctx.FinalModule().(Module).base() {
771 a.generateModuleTarget(ctx)
772 if ctx.Failed() {
773 return
774 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800775 }
Colin Crosscec81712017-07-13 14:43:27 -0700776
Colin Cross0875c522017-11-28 17:34:01 -0800777 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800778}
779
Colin Crossf6566ed2015-03-24 11:13:38 -0700780type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700781 target Target
782 targetPrimary bool
783 debug bool
Jiyong Park2db76922017-11-08 16:03:48 +0900784 kind moduleKind
Colin Cross8b74d172016-09-13 09:59:14 -0700785 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700786}
787
Colin Cross3f40fa42015-01-30 17:27:36 -0800788type androidModuleContext struct {
789 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700790 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700791 installDeps Paths
792 installFiles Paths
793 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800794 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700795 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700796
797 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700798 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800799}
800
Colin Cross67a5c132017-05-09 13:45:28 -0700801func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800802 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700803 Rule: ErrorRule,
804 Description: desc,
805 Outputs: outputs,
806 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800807 Args: map[string]string{
808 "error": err.Error(),
809 },
810 })
811 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800812}
813
Colin Crossaabf6792017-11-29 00:27:14 -0800814func (a *androidModuleContext) Config() Config {
815 return a.ModuleContext.Config().(Config)
816}
817
Colin Cross0875c522017-11-28 17:34:01 -0800818func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700819 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800820}
821
Colin Cross0875c522017-11-28 17:34:01 -0800822func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700823 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700824 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800825 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800826 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700827 Outputs: params.Outputs.Strings(),
828 ImplicitOutputs: params.ImplicitOutputs.Strings(),
829 Inputs: params.Inputs.Strings(),
830 Implicits: params.Implicits.Strings(),
831 OrderOnly: params.OrderOnly.Strings(),
832 Args: params.Args,
833 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700834 }
835
Colin Cross33bfb0a2016-11-21 17:23:08 -0800836 if params.Depfile != nil {
837 bparams.Depfile = params.Depfile.String()
838 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700839 if params.Output != nil {
840 bparams.Outputs = append(bparams.Outputs, params.Output.String())
841 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700842 if params.ImplicitOutput != nil {
843 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
844 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700845 if params.Input != nil {
846 bparams.Inputs = append(bparams.Inputs, params.Input.String())
847 }
848 if params.Implicit != nil {
849 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
850 }
851
Colin Crossfe4bc362018-09-12 10:02:13 -0700852 bparams.Outputs = proptools.NinjaEscape(bparams.Outputs)
853 bparams.ImplicitOutputs = proptools.NinjaEscape(bparams.ImplicitOutputs)
854 bparams.Inputs = proptools.NinjaEscape(bparams.Inputs)
855 bparams.Implicits = proptools.NinjaEscape(bparams.Implicits)
856 bparams.OrderOnly = proptools.NinjaEscape(bparams.OrderOnly)
857 bparams.Depfile = proptools.NinjaEscape([]string{bparams.Depfile})[0]
858
Colin Cross0875c522017-11-28 17:34:01 -0800859 return bparams
860}
861
862func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
863 a.ModuleContext.Variable(pctx.PackageContext, name, value)
864}
865
866func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
867 argNames ...string) blueprint.Rule {
868
869 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
870}
871
872func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
873 if a.config.captureBuild {
874 a.buildParams = append(a.buildParams, params)
875 }
876
877 bparams := convertBuildParams(params)
878
879 if bparams.Description != "" {
880 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
881 }
882
Colin Cross6ff51382015-12-17 16:39:19 -0800883 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700884 a.ninjaError(bparams.Description, bparams.Outputs,
885 fmt.Errorf("module %s missing dependencies: %s\n",
886 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800887 return
888 }
889
Colin Cross0875c522017-11-28 17:34:01 -0800890 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700891}
892
Colin Cross6ff51382015-12-17 16:39:19 -0800893func (a *androidModuleContext) GetMissingDependencies() []string {
894 return a.missingDeps
895}
896
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800897func (a *androidModuleContext) AddMissingDependencies(deps []string) {
898 if deps != nil {
899 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700900 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800901 }
902}
903
Colin Crossd11fcda2017-10-23 17:59:01 -0700904func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
905 aModule, _ := module.(Module)
906 if aModule == nil {
907 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
908 return nil
909 }
910
911 if !aModule.Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800912 if a.Config().AllowMissingDependencies() {
Colin Crossd11fcda2017-10-23 17:59:01 -0700913 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
914 } else {
915 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
916 }
917 return nil
918 }
919
920 return aModule
921}
922
Colin Cross35143d02017-11-16 00:11:20 -0800923func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
924 a.ModuleContext.VisitDirectDeps(visit)
925}
926
Colin Crossd11fcda2017-10-23 17:59:01 -0700927func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
928 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
929 if aModule := a.validateAndroidModule(module); aModule != nil {
930 visit(aModule)
931 }
932 })
933}
934
Colin Crossee6143c2017-12-30 17:54:27 -0800935func (a *androidModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
936 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
937 if aModule := a.validateAndroidModule(module); aModule != nil {
938 if a.ModuleContext.OtherModuleDependencyTag(aModule) == tag {
939 visit(aModule)
940 }
941 }
942 })
943}
944
Colin Crossd11fcda2017-10-23 17:59:01 -0700945func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
946 a.ModuleContext.VisitDirectDepsIf(
947 // pred
948 func(module blueprint.Module) bool {
949 if aModule := a.validateAndroidModule(module); aModule != nil {
950 return pred(aModule)
951 } else {
952 return false
953 }
954 },
955 // visit
956 func(module blueprint.Module) {
957 visit(module.(Module))
958 })
959}
960
961func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
962 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
963 if aModule := a.validateAndroidModule(module); aModule != nil {
964 visit(aModule)
965 }
966 })
967}
968
969func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
970 a.ModuleContext.VisitDepsDepthFirstIf(
971 // pred
972 func(module blueprint.Module) bool {
973 if aModule := a.validateAndroidModule(module); aModule != nil {
974 return pred(aModule)
975 } else {
976 return false
977 }
978 },
979 // visit
980 func(module blueprint.Module) {
981 visit(module.(Module))
982 })
983}
984
985func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
986 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
987 childAndroidModule := a.validateAndroidModule(child)
988 parentAndroidModule := a.validateAndroidModule(parent)
989 if childAndroidModule != nil && parentAndroidModule != nil {
990 return visit(childAndroidModule, parentAndroidModule)
991 } else {
992 return false
993 }
994 })
995}
996
Colin Cross0875c522017-11-28 17:34:01 -0800997func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
998 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
999 visit(module.(Module))
1000 })
1001}
1002
1003func (a *androidModuleContext) PrimaryModule() Module {
1004 return a.ModuleContext.PrimaryModule().(Module)
1005}
1006
1007func (a *androidModuleContext) FinalModule() Module {
1008 return a.ModuleContext.FinalModule().(Module)
1009}
1010
Colin Crossa1ad8d12016-06-01 17:09:44 -07001011func (a *androidBaseContextImpl) Target() Target {
1012 return a.target
1013}
1014
Colin Cross8b74d172016-09-13 09:59:14 -07001015func (a *androidBaseContextImpl) TargetPrimary() bool {
1016 return a.targetPrimary
1017}
1018
Colin Crossf6566ed2015-03-24 11:13:38 -07001019func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001020 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -08001021}
1022
Colin Crossa1ad8d12016-06-01 17:09:44 -07001023func (a *androidBaseContextImpl) Os() OsType {
1024 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -08001025}
1026
Colin Crossf6566ed2015-03-24 11:13:38 -07001027func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001028 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -07001029}
1030
1031func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001032 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -07001033}
1034
Colin Cross0af4b842015-04-30 16:36:18 -07001035func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001036 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -07001037}
1038
Colin Cross3edeee12017-04-04 12:59:48 -07001039func (a *androidBaseContextImpl) Windows() bool {
1040 return a.target.Os == Windows
1041}
1042
Colin Crossf6566ed2015-03-24 11:13:38 -07001043func (a *androidBaseContextImpl) Debug() bool {
1044 return a.debug
1045}
1046
Colin Cross1e7d3702016-08-24 15:25:47 -07001047func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -07001048 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
1049 return true
1050 }
Colin Cross1e7d3702016-08-24 15:25:47 -07001051 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
1052}
1053
Colin Cross1332b002015-04-07 17:11:30 -07001054func (a *androidBaseContextImpl) AConfig() Config {
1055 return a.config
1056}
1057
Colin Cross9272ade2016-08-17 15:24:12 -07001058func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
1059 return DeviceConfig{a.config.deviceConfig}
1060}
1061
Jiyong Park2db76922017-11-08 16:03:48 +09001062func (a *androidBaseContextImpl) Platform() bool {
1063 return a.kind == platformModule
1064}
1065
1066func (a *androidBaseContextImpl) DeviceSpecific() bool {
1067 return a.kind == deviceSpecificModule
1068}
1069
1070func (a *androidBaseContextImpl) SocSpecific() bool {
1071 return a.kind == socSpecificModule
1072}
1073
1074func (a *androidBaseContextImpl) ProductSpecific() bool {
1075 return a.kind == productSpecificModule
Dan Willemsen782a2d12015-12-21 14:55:28 -08001076}
1077
Dario Frenifd05a742018-05-29 13:28:54 +01001078func (a *androidBaseContextImpl) ProductServicesSpecific() bool {
1079 return a.kind == productServicesSpecificModule
1080}
1081
Jiyong Park5baac542018-08-28 09:55:37 +09001082// Makes this module a platform module, i.e. not specific to soc, device,
1083// product, or product_services.
1084func (a *ModuleBase) MakeAsPlatform() {
1085 a.commonProperties.Vendor = boolPtr(false)
1086 a.commonProperties.Proprietary = boolPtr(false)
1087 a.commonProperties.Soc_specific = boolPtr(false)
1088 a.commonProperties.Product_specific = boolPtr(false)
1089 a.commonProperties.Product_services_specific = boolPtr(false)
1090}
1091
Colin Cross8d8f8e22016-08-03 11:57:50 -07001092func (a *androidModuleContext) InstallInData() bool {
1093 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08001094}
1095
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001096func (a *androidModuleContext) InstallInSanitizerDir() bool {
1097 return a.module.InstallInSanitizerDir()
1098}
1099
Jiyong Parkf9332f12018-02-01 00:54:12 +09001100func (a *androidModuleContext) InstallInRecovery() bool {
1101 return a.module.InstallInRecovery()
1102}
1103
Colin Cross893d8162017-04-26 17:34:03 -07001104func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
1105 if a.module.base().commonProperties.SkipInstall {
1106 return true
1107 }
1108
Colin Cross3607f212018-05-07 15:28:05 -07001109 // We'll need a solution for choosing which of modules with the same name in different
1110 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
1111 // list of namespaces to install in a Soong-only build.
1112 if !a.module.base().commonProperties.NamespaceExportedToMake {
1113 return true
1114 }
1115
Colin Cross893d8162017-04-26 17:34:03 -07001116 if a.Device() {
Colin Cross6510f912017-11-29 00:27:14 -08001117 if a.Config().SkipDeviceInstall() {
Colin Cross893d8162017-04-26 17:34:03 -07001118 return true
1119 }
1120
Colin Cross6510f912017-11-29 00:27:14 -08001121 if a.Config().SkipMegaDeviceInstall(fullInstallPath.String()) {
Colin Cross893d8162017-04-26 17:34:03 -07001122 return true
1123 }
1124 }
1125
1126 return false
1127}
1128
Colin Cross5c517922017-08-31 12:29:17 -07001129func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -07001130 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -07001131 return a.installFile(installPath, name, srcPath, Cp, deps)
1132}
1133
1134func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
1135 deps ...Path) OutputPath {
1136 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
1137}
1138
1139func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
1140 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -07001141
Dan Willemsen782a2d12015-12-21 14:55:28 -08001142 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001143 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001144
Colin Cross893d8162017-04-26 17:34:03 -07001145 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001146
Dan Willemsen322acaf2016-01-12 23:07:05 -08001147 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -07001148
Colin Cross89562dc2016-10-03 17:47:19 -07001149 var implicitDeps, orderOnlyDeps Paths
1150
1151 if a.Host() {
1152 // Installed host modules might be used during the build, depend directly on their
1153 // dependencies so their timestamp is updated whenever their dependency is updated
1154 implicitDeps = deps
1155 } else {
1156 orderOnlyDeps = deps
1157 }
1158
Colin Crossae887032017-10-23 17:16:14 -07001159 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07001160 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07001161 Description: "install " + fullInstallPath.Base(),
1162 Output: fullInstallPath,
1163 Input: srcPath,
1164 Implicits: implicitDeps,
1165 OrderOnly: orderOnlyDeps,
Colin Cross6510f912017-11-29 00:27:14 -08001166 Default: !a.Config().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08001167 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001168
Dan Willemsen322acaf2016-01-12 23:07:05 -08001169 a.installFiles = append(a.installFiles, fullInstallPath)
1170 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001171 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -07001172 return fullInstallPath
1173}
1174
Colin Cross3854a602016-01-11 12:49:11 -08001175func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
1176 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001177 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08001178
Colin Cross893d8162017-04-26 17:34:03 -07001179 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001180
Colin Crossae887032017-10-23 17:16:14 -07001181 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001182 Rule: Symlink,
1183 Description: "install symlink " + fullInstallPath.Base(),
1184 Output: fullInstallPath,
1185 OrderOnly: Paths{srcPath},
Colin Cross6510f912017-11-29 00:27:14 -08001186 Default: !a.Config().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -08001187 Args: map[string]string{
1188 "fromPath": srcPath.String(),
1189 },
1190 })
Colin Cross3854a602016-01-11 12:49:11 -08001191
Colin Cross12fc4972016-01-11 12:49:11 -08001192 a.installFiles = append(a.installFiles, fullInstallPath)
1193 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1194 }
Colin Cross3854a602016-01-11 12:49:11 -08001195 return fullInstallPath
1196}
1197
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001198func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001199 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1200}
1201
Colin Cross3f40fa42015-01-30 17:27:36 -08001202type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001203 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001204}
1205
1206func isFileInstaller(m blueprint.Module) bool {
1207 _, ok := m.(fileInstaller)
1208 return ok
1209}
1210
1211func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -07001212 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -08001213 return ok
1214}
Colin Crossfce53272015-04-08 11:21:40 -07001215
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001216func findStringInSlice(str string, slice []string) int {
1217 for i, s := range slice {
1218 if s == str {
1219 return i
Colin Crossfce53272015-04-08 11:21:40 -07001220 }
1221 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001222 return -1
1223}
1224
Colin Cross068e0fe2016-12-13 15:23:47 -08001225func SrcIsModule(s string) string {
1226 if len(s) > 1 && s[0] == ':' {
1227 return s[1:]
1228 }
1229 return ""
1230}
1231
1232type sourceDependencyTag struct {
1233 blueprint.BaseDependencyTag
1234}
1235
1236var SourceDepTag sourceDependencyTag
1237
Colin Cross366938f2017-12-11 16:29:02 -08001238// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
1239// using ":module" syntax, if any.
Colin Cross068e0fe2016-12-13 15:23:47 -08001240func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1241 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001242 set := make(map[string]bool)
1243
Colin Cross068e0fe2016-12-13 15:23:47 -08001244 for _, s := range srcFiles {
1245 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001246 if _, found := set[m]; found {
1247 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1248 } else {
1249 set[m] = true
1250 deps = append(deps, m)
1251 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001252 }
1253 }
1254
1255 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1256}
1257
Colin Cross366938f2017-12-11 16:29:02 -08001258// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
1259// using ":module" syntax, if any.
1260func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
1261 if s != nil {
1262 if m := SrcIsModule(*s); m != "" {
1263 ctx.AddDependency(ctx.Module(), SourceDepTag, m)
1264 }
1265 }
1266}
1267
Colin Cross068e0fe2016-12-13 15:23:47 -08001268type SourceFileProducer interface {
1269 Srcs() Paths
1270}
1271
1272// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001273// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001274func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001275 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1276}
1277
Colin Cross366938f2017-12-11 16:29:02 -08001278// Returns a single path expanded from globs and modules referenced using ":module" syntax.
1279// ExtractSourceDeps must have already been called during the dependency resolution phase.
1280func (ctx *androidModuleContext) ExpandSource(srcFile, prop string) Path {
1281 srcFiles := ctx.ExpandSourcesSubDir([]string{srcFile}, nil, "")
1282 if len(srcFiles) == 1 {
1283 return srcFiles[0]
1284 } else {
1285 ctx.PropertyErrorf(prop, "module providing %s must produce exactly one file", prop)
1286 return nil
1287 }
1288}
1289
Colin Cross2383f3b2018-02-06 14:40:13 -08001290// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
1291// the srcFile is non-nil.
1292// ExtractSourceDeps must have already been called during the dependency resolution phase.
1293func (ctx *androidModuleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
1294 if srcFile != nil {
1295 return OptionalPathForPath(ctx.ExpandSource(*srcFile, prop))
1296 }
1297 return OptionalPath{}
1298}
1299
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001300func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001301 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001302
Colin Cross461b4452018-02-23 09:22:42 -08001303 var expandedExcludes []string
1304 if excludes != nil {
1305 expandedExcludes = make([]string, 0, len(excludes))
1306 }
Nan Zhang27e284d2018-02-09 21:03:53 +00001307
1308 for _, e := range excludes {
1309 if m := SrcIsModule(e); m != "" {
1310 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
1311 if module == nil {
1312 // Error will have been handled by ExtractSourcesDeps
1313 continue
1314 }
1315 if srcProducer, ok := module.(SourceFileProducer); ok {
1316 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
1317 } else {
1318 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1319 }
1320 } else {
1321 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001322 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001323 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001324 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001325 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001326 if m := SrcIsModule(s); m != "" {
1327 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001328 if module == nil {
1329 // Error will have been handled by ExtractSourcesDeps
1330 continue
1331 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001332 if srcProducer, ok := module.(SourceFileProducer); ok {
Nan Zhang27e284d2018-02-09 21:03:53 +00001333 moduleSrcs := srcProducer.Srcs()
1334 for _, e := range expandedExcludes {
1335 for j, ms := range moduleSrcs {
1336 if ms.String() == e {
1337 moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
1338 }
1339 }
1340 }
1341 expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001342 } else {
1343 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1344 }
1345 } else if pathtools.IsGlob(s) {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001346 globbedSrcFiles := ctx.GlobFiles(filepath.Join(prefix, s), expandedExcludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001347 for i, s := range globbedSrcFiles {
1348 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001349 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001350 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001351 } else {
Nan Zhang27e284d2018-02-09 21:03:53 +00001352 p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1353 j := findStringInSlice(p.String(), expandedExcludes)
1354 if j == -1 {
1355 expandedSrcFiles = append(expandedSrcFiles, p)
1356 }
1357
Colin Cross8f101b42015-06-17 15:09:06 -07001358 }
1359 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001360 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001361}
1362
Nan Zhang6d34b302017-02-04 17:47:46 -08001363func (ctx *androidModuleContext) RequiredModuleNames() []string {
1364 return ctx.module.base().commonProperties.Required
1365}
1366
Colin Cross7f19f372016-11-01 11:10:25 -07001367func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1368 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001369 if err != nil {
1370 ctx.ModuleErrorf("glob: %s", err.Error())
1371 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001372 return pathsForModuleSrcFromFullPath(ctx, ret, true)
Colin Crossfce53272015-04-08 11:21:40 -07001373}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001374
Nan Zhang581fd212018-01-10 16:06:12 -08001375func (ctx *androidModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001376 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Nan Zhang581fd212018-01-10 16:06:12 -08001377 if err != nil {
1378 ctx.ModuleErrorf("glob: %s", err.Error())
1379 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001380 return pathsForModuleSrcFromFullPath(ctx, ret, false)
Nan Zhang581fd212018-01-10 16:06:12 -08001381}
1382
Colin Cross463a90e2015-06-17 14:20:06 -07001383func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001384 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001385}
1386
Colin Cross0875c522017-11-28 17:34:01 -08001387func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001388 return &buildTargetSingleton{}
1389}
1390
Colin Cross87d8b562017-04-25 10:01:55 -07001391func parentDir(dir string) string {
1392 dir, _ = filepath.Split(dir)
1393 return filepath.Clean(dir)
1394}
1395
Colin Cross1f8c52b2015-06-16 16:38:17 -07001396type buildTargetSingleton struct{}
1397
Colin Cross0875c522017-11-28 17:34:01 -08001398func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1399 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001400
Colin Cross0875c522017-11-28 17:34:01 -08001401 mmTarget := func(dir string) WritablePath {
1402 return PathForPhony(ctx,
1403 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001404 }
1405
Colin Cross0875c522017-11-28 17:34:01 -08001406 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001407
Colin Cross0875c522017-11-28 17:34:01 -08001408 ctx.VisitAllModules(func(module Module) {
1409 blueprintDir := module.base().blueprintDir
1410 installTarget := module.base().installTarget
1411 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001412
Colin Cross0875c522017-11-28 17:34:01 -08001413 if checkbuildTarget != nil {
1414 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1415 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1416 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001417
Colin Cross0875c522017-11-28 17:34:01 -08001418 if installTarget != nil {
1419 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001420 }
1421 })
1422
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001423 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001424 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001425 suffix = "-soong"
1426 }
1427
Colin Cross1f8c52b2015-06-16 16:38:17 -07001428 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001429 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001430 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001431 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001432 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001433 })
1434
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001435 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001436 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001437 return
1438 }
1439
Colin Cross0875c522017-11-28 17:34:01 -08001440 sortedKeys := func(m map[string]Paths) []string {
1441 s := make([]string, 0, len(m))
1442 for k := range m {
1443 s = append(s, k)
1444 }
1445 sort.Strings(s)
1446 return s
1447 }
1448
Colin Cross87d8b562017-04-25 10:01:55 -07001449 // Ensure ancestor directories are in modulesInDir
1450 dirs := sortedKeys(modulesInDir)
1451 for _, dir := range dirs {
1452 dir := parentDir(dir)
1453 for dir != "." && dir != "/" {
1454 if _, exists := modulesInDir[dir]; exists {
1455 break
1456 }
1457 modulesInDir[dir] = nil
1458 dir = parentDir(dir)
1459 }
1460 }
1461
1462 // Make directories build their direct subdirectories
1463 dirs = sortedKeys(modulesInDir)
1464 for _, dir := range dirs {
1465 p := parentDir(dir)
1466 if p != "." && p != "/" {
1467 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1468 }
1469 }
1470
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001471 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1472 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1473 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001474 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001475 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001476 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001477 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001478 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001479 // HACK: checkbuild should be an optional build, but force it
1480 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001481 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001482 })
1483 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001484
1485 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1486 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001487 ctx.VisitAllModules(func(module Module) {
1488 if module.Enabled() {
1489 os := module.Target().Os
1490 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001491 }
1492 })
1493
Colin Cross0875c522017-11-28 17:34:01 -08001494 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001495 for os, deps := range osDeps {
1496 var className string
1497
1498 switch os.Class {
1499 case Host:
1500 className = "host"
1501 case HostCross:
1502 className = "host-cross"
1503 case Device:
1504 className = "target"
1505 default:
1506 continue
1507 }
1508
Colin Cross0875c522017-11-28 17:34:01 -08001509 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001510 osClass[className] = append(osClass[className], name)
1511
Colin Cross0875c522017-11-28 17:34:01 -08001512 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001513 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001514 Output: name,
1515 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001516 })
1517 }
1518
1519 // Wrap those into host|host-cross|target phony rules
1520 osClasses := sortedKeys(osClass)
1521 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001522 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001523 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001524 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001525 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001526 })
1527 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001528}
Colin Crossd779da42015-12-17 18:00:23 -08001529
Colin Cross2465c3d2018-09-28 10:19:18 -07001530type ModulesByName struct {
1531 slice []blueprint.Module
Colin Crossd779da42015-12-17 18:00:23 -08001532 ctx interface {
1533 ModuleName(blueprint.Module) string
1534 ModuleSubDir(blueprint.Module) string
1535 }
1536}
1537
Colin Cross2465c3d2018-09-28 10:19:18 -07001538func (s ModulesByName) Len() int { return len(s.slice) }
1539func (s ModulesByName) Less(i, j int) bool {
Colin Crossd779da42015-12-17 18:00:23 -08001540 mi, mj := s.slice[i], s.slice[j]
1541 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1542
1543 if ni != nj {
1544 return ni < nj
1545 } else {
1546 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1547 }
1548}
Colin Cross2465c3d2018-09-28 10:19:18 -07001549func (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 -07001550
1551// Collect information for opening IDE project files in java/jdeps.go.
1552type IDEInfo interface {
1553 IDEInfo(ideInfo *IdeInfo)
1554 BaseModuleName() string
1555}
1556
1557// Extract the base module name from the Import name.
1558// Often the Import name has a prefix "prebuilt_".
1559// Remove the prefix explicitly if needed
1560// until we find a better solution to get the Import name.
1561type IDECustomizedModuleName interface {
1562 IDECustomizedModuleName() string
1563}
1564
1565type IdeInfo struct {
1566 Deps []string `json:"dependencies,omitempty"`
1567 Srcs []string `json:"srcs,omitempty"`
1568 Aidl_include_dirs []string `json:"aidl_include_dirs,omitempty"`
1569 Jarjar_rules []string `json:"jarjar_rules,omitempty"`
1570 Jars []string `json:"jars,omitempty"`
1571 Classes []string `json:"class,omitempty"`
1572 Installed_paths []string `json:"installed,omitempty"`
1573}