blob: 688d8b2231bec5bca7f289675799c36131a7e4d2 [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 Crossee0bc3b2018-10-02 22:01:37 -070061 MultiTargets() []Target
Colin Crossf6566ed2015-03-24 11:13:38 -070062 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070063 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070064 Host() bool
65 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070066 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070067 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070068 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070069 PrimaryArch() bool
Jiyong Park2db76922017-11-08 16:03:48 +090070 Platform() bool
71 DeviceSpecific() bool
72 SocSpecific() bool
73 ProductSpecific() bool
Dario Frenifd05a742018-05-29 13:28:54 +010074 ProductServicesSpecific() bool
Colin Cross1332b002015-04-07 17:11:30 -070075 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070076 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070077}
78
Colin Cross635c3b02016-05-18 15:37:25 -070079type BaseContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080080 BaseModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070081 androidBaseContext
82}
83
Colin Crossaabf6792017-11-29 00:27:14 -080084// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
85// a Config instead of an interface{}.
86type BaseModuleContext interface {
87 ModuleName() string
88 ModuleDir() string
89 Config() Config
90
91 ContainsProperty(name string) bool
92 Errorf(pos scanner.Position, fmt string, args ...interface{})
93 ModuleErrorf(fmt string, args ...interface{})
94 PropertyErrorf(property, fmt string, args ...interface{})
95 Failed() bool
96
97 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
98 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
99 // builder whenever a file matching the pattern as added or removed, without rerunning if a
100 // file that does not match the pattern is added to a searched directory.
101 GlobWithDeps(pattern string, excludes []string) ([]string, error)
102
103 Fs() pathtools.FileSystem
104 AddNinjaFileDeps(deps ...string)
105}
106
Colin Cross635c3b02016-05-18 15:37:25 -0700107type ModuleContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -0700108 androidBaseContext
Colin Crossaabf6792017-11-29 00:27:14 -0800109 BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -0800110
Colin Crossae887032017-10-23 17:16:14 -0700111 // Deprecated: use ModuleContext.Build instead.
Colin Cross0875c522017-11-28 17:34:01 -0800112 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -0700113
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700114 ExpandSources(srcFiles, excludes []string) Paths
Colin Cross366938f2017-12-11 16:29:02 -0800115 ExpandSource(srcFile, prop string) Path
Colin Cross2383f3b2018-02-06 14:40:13 -0800116 ExpandOptionalSource(srcFile *string, prop string) OptionalPath
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800117 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -0700118 Glob(globPattern string, excludes []string) Paths
Nan Zhang581fd212018-01-10 16:06:12 -0800119 GlobFiles(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700120
Colin Cross5c517922017-08-31 12:29:17 -0700121 InstallExecutable(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
122 InstallFile(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -0800123 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700124 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800125
126 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -0700127
Colin Cross8d8f8e22016-08-03 11:57:50 -0700128 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700129 InstallInSanitizerDir() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900130 InstallInRecovery() bool
Nan Zhang6d34b302017-02-04 17:47:46 -0800131
132 RequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -0700133
134 // android.ModuleContext methods
135 // These are duplicated instead of embedded so that can eventually be wrapped to take an
136 // android.Module instead of a blueprint.Module
137 OtherModuleName(m blueprint.Module) string
138 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
139 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
140
141 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
142 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
143
144 ModuleSubDir() string
145
Colin Cross35143d02017-11-16 00:11:20 -0800146 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700147 VisitDirectDeps(visit func(Module))
Colin Crossee6143c2017-12-30 17:54:27 -0800148 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700149 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -0700150 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Crossd11fcda2017-10-23 17:59:01 -0700151 VisitDepsDepthFirst(visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -0700152 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Crossd11fcda2017-10-23 17:59:01 -0700153 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
154 WalkDeps(visit func(Module, Module) bool)
Colin Cross3f68a132017-10-23 17:10:29 -0700155
Colin Cross0875c522017-11-28 17:34:01 -0800156 Variable(pctx PackageContext, name, value string)
157 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700158 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
159 // and performs more verification.
Colin Cross0875c522017-11-28 17:34:01 -0800160 Build(pctx PackageContext, params BuildParams)
Colin Cross3f68a132017-10-23 17:10:29 -0700161
Colin Cross0875c522017-11-28 17:34:01 -0800162 PrimaryModule() Module
163 FinalModule() Module
164 VisitAllModuleVariants(visit func(Module))
Colin Cross3f68a132017-10-23 17:10:29 -0700165
166 GetMissingDependencies() []string
Jeff Gaston088e29e2017-11-29 16:47:17 -0800167 Namespace() blueprint.Namespace
Colin Cross3f40fa42015-01-30 17:27:36 -0800168}
169
Colin Cross635c3b02016-05-18 15:37:25 -0700170type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800171 blueprint.Module
172
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700173 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
174 // but GenerateAndroidBuildActions also has access to Android-specific information.
175 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700176 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700177
Colin Cross1e676be2016-10-12 14:38:15 -0700178 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800179
Colin Cross635c3b02016-05-18 15:37:25 -0700180 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800181 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700182 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800183 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700184 InstallInSanitizerDir() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900185 InstallInRecovery() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800186 SkipInstall()
Jiyong Park374510b2018-03-19 18:23:01 +0900187 ExportedToMake() bool
Colin Cross36242852017-06-23 15:06:31 -0700188
189 AddProperties(props ...interface{})
190 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700191
Colin Crossae887032017-10-23 17:16:14 -0700192 BuildParamsForTests() []BuildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800193}
194
Colin Crossfc754582016-05-17 16:34:16 -0700195type nameProperties struct {
196 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800197 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700198}
199
200type commonProperties struct {
Dan Willemsen0effe062015-11-30 16:06:01 -0800201 // emit build rules for this module
202 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800203
Colin Cross7d5136f2015-05-11 13:39:40 -0700204 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800205 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
206 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
207 // platform
Colin Cross7d716ba2017-11-01 10:38:29 -0700208 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700209
210 Target struct {
211 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700212 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700213 }
214 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700215 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700216 }
217 }
218
Colin Crossee0bc3b2018-10-02 22:01:37 -0700219 UseTargetVariants bool `blueprint:"mutated"`
220 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800221
Dan Willemsen782a2d12015-12-21 14:55:28 -0800222 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700223 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800224
Colin Cross55708f32017-03-20 13:23:34 -0700225 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700226 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700227
Jiyong Park2db76922017-11-08 16:03:48 +0900228 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
229 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
230 // Use `soc_specific` instead for better meaning.
Colin Cross7d716ba2017-11-01 10:38:29 -0700231 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700232
Jiyong Park2db76922017-11-08 16:03:48 +0900233 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
234 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
235 Soc_specific *bool
236
237 // whether this module is specific to a device, not only for SoC, but also for off-chip
238 // peripherals. When set to true, it is installed into /odm (or /vendor/odm if odm partition
239 // does not exist, or /system/vendor/odm if both odm and vendor partitions do not exist).
240 // This implies `soc_specific:true`.
241 Device_specific *bool
242
243 // whether this module is specific to a software configuration of a product (e.g. country,
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900244 // network operator, etc). When set to true, it is installed into /product (or
245 // /system/product if product partition does not exist).
Jiyong Park2db76922017-11-08 16:03:48 +0900246 Product_specific *bool
247
Dario Frenifd05a742018-05-29 13:28:54 +0100248 // whether this module provides services owned by the OS provider to the core platform. When set
Dario Freni95cf7672018-08-17 00:57:57 +0100249 // to true, it is installed into /product_services (or /system/product_services if
250 // product_services partition does not exist).
251 Product_services_specific *bool
Dario Frenifd05a742018-05-29 13:28:54 +0100252
Jiyong Parkf9332f12018-02-01 00:54:12 +0900253 // Whether this module is installed to recovery partition
254 Recovery *bool
255
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700256 // init.rc files to be installed if this module is installed
257 Init_rc []string
258
Steven Moreland57a23d22018-04-04 15:42:19 -0700259 // VINTF manifest fragments to be installed if this module is installed
260 Vintf_fragments []string
261
Chris Wolfe998306e2016-08-15 14:47:23 -0400262 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700263 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400264
Colin Cross5aac3622017-08-31 15:07:09 -0700265 // relative path to a file to include in the list of notices for the device
266 Notice *string
267
Colin Crossa1ad8d12016-06-01 17:09:44 -0700268 // Set by TargetMutator
Colin Crossee0bc3b2018-10-02 22:01:37 -0700269 CompileTarget Target `blueprint:"mutated"`
270 CompileMultiTargets []Target `blueprint:"mutated"`
271 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800272
273 // Set by InitAndroidModule
274 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700275 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700276
277 SkipInstall bool `blueprint:"mutated"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800278
279 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800280}
281
282type hostAndDeviceProperties struct {
Colin Cross4e81d702018-11-09 10:36:55 -0800283 // If set to true, build a variant of the module for the host. Defaults to false.
284 Host_supported *bool
285
286 // If set to true, build a variant of the module for the device. Defaults to true.
Colin Crossa4190c12016-07-12 13:11:25 -0700287 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800288}
289
Colin Crossc472d572015-03-17 15:06:21 -0700290type Multilib string
291
292const (
Colin Cross6b4a32d2017-12-05 13:42:45 -0800293 MultilibBoth Multilib = "both"
294 MultilibFirst Multilib = "first"
295 MultilibCommon Multilib = "common"
296 MultilibCommonFirst Multilib = "common_first"
297 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700298)
299
Colin Crossa1ad8d12016-06-01 17:09:44 -0700300type HostOrDeviceSupported int
301
302const (
303 _ HostOrDeviceSupported = iota
Dan Albert0981b5c2018-08-02 13:46:35 -0700304
305 // Host and HostCross are built by default. Device is not supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700306 HostSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700307
308 // Host is built by default. HostCross and Device are not supported.
Dan Albertc6345fb2016-10-20 01:36:11 -0700309 HostSupportedNoCross
Dan Albert0981b5c2018-08-02 13:46:35 -0700310
311 // Device is built by default. Host and HostCross are not supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700312 DeviceSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700313
314 // Device is built by default. Host and HostCross are supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700315 HostAndDeviceSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700316
317 // Host, HostCross, and Device are built by default.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700318 HostAndDeviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700319
320 // Nothing is supported. This is not exposed to the user, but used to mark a
321 // host only module as unsupported when the module type is not supported on
322 // the host OS. E.g. benchmarks are supported on Linux but not Darwin.
Dan Willemsen0b24c742016-10-04 15:13:37 -0700323 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700324)
325
Jiyong Park2db76922017-11-08 16:03:48 +0900326type moduleKind int
327
328const (
329 platformModule moduleKind = iota
330 deviceSpecificModule
331 socSpecificModule
332 productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100333 productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900334)
335
336func (k moduleKind) String() string {
337 switch k {
338 case platformModule:
339 return "platform"
340 case deviceSpecificModule:
341 return "device-specific"
342 case socSpecificModule:
343 return "soc-specific"
344 case productSpecificModule:
345 return "product-specific"
Dario Frenifd05a742018-05-29 13:28:54 +0100346 case productServicesSpecificModule:
347 return "productservices-specific"
Jiyong Park2db76922017-11-08 16:03:48 +0900348 default:
349 panic(fmt.Errorf("unknown module kind %d", k))
350 }
351}
352
Colin Cross36242852017-06-23 15:06:31 -0700353func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800354 base := m.base()
355 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700356
Colin Cross36242852017-06-23 15:06:31 -0700357 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700358 &base.nameProperties,
359 &base.commonProperties,
360 &base.variableProperties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700361 base.customizableProperties = m.GetProperties()
Colin Cross5049f022015-03-18 13:28:46 -0700362}
363
Colin Cross36242852017-06-23 15:06:31 -0700364func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
365 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700366
367 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800368 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700369 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700370 base.commonProperties.ArchSpecific = true
Colin Crossee0bc3b2018-10-02 22:01:37 -0700371 base.commonProperties.UseTargetVariants = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800372
Dan Willemsen218f6562015-07-08 18:13:11 -0700373 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700374 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700375 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800376 }
377
Colin Cross36242852017-06-23 15:06:31 -0700378 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800379}
380
Colin Crossee0bc3b2018-10-02 22:01:37 -0700381func InitAndroidMultiTargetsArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
382 InitAndroidArchModule(m, hod, defaultMultilib)
383 m.base().commonProperties.UseTargetVariants = false
384}
385
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800386// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800387// modules. It should be included as an anonymous field in every module
388// struct definition. InitAndroidModule should then be called from the module's
389// factory function, and the return values from InitAndroidModule should be
390// returned from the factory function.
391//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800392// The ModuleBase type is responsible for implementing the GenerateBuildActions
393// method to support the blueprint.Module interface. This method will then call
394// the module's GenerateAndroidBuildActions method once for each build variant
395// that is to be built. GenerateAndroidBuildActions is passed a
396// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800397// AndroidModuleContext exposes extra functionality specific to the Android build
398// system including details about the particular build variant that is to be
399// generated.
400//
401// For example:
402//
403// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800404// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800405// )
406//
407// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800408// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800409// properties struct {
410// MyProperty string
411// }
412// }
413//
Colin Cross36242852017-06-23 15:06:31 -0700414// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800415// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700416// m.AddProperties(&m.properties)
417// android.InitAndroidModule(m)
418// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800419// }
420//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800421// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800422// // Get the CPU architecture for the current build variant.
423// variantArch := ctx.Arch()
424//
425// // ...
426// }
Colin Cross635c3b02016-05-18 15:37:25 -0700427type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800428 // Putting the curiously recurring thing pointing to the thing that contains
429 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700430 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700431 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800432
Colin Crossfc754582016-05-17 16:34:16 -0700433 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800434 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700435 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800436 hostAndDeviceProperties hostAndDeviceProperties
437 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700438 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700439 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800440
441 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700442 installFiles Paths
443 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700444
445 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
446 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800447 installTarget WritablePath
448 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700449 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700450
Colin Cross178a5092016-09-13 13:42:32 -0700451 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700452
453 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700454
455 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700456 buildParams []BuildParams
Colin Crossa9d8bee2018-10-02 13:59:46 -0700457
458 prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool
Colin Cross36242852017-06-23 15:06:31 -0700459}
460
461func (a *ModuleBase) AddProperties(props ...interface{}) {
462 a.registerProps = append(a.registerProps, props...)
463}
464
465func (a *ModuleBase) GetProperties() []interface{} {
466 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800467}
468
Colin Crossae887032017-10-23 17:16:14 -0700469func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700470 return a.buildParams
471}
472
Colin Crossa9d8bee2018-10-02 13:59:46 -0700473func (a *ModuleBase) Prefer32(prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool) {
474 a.prefer32 = prefer32
475}
476
Colin Crossce75d2c2016-10-06 16:12:58 -0700477// Name returns the name of the module. It may be overridden by individual module types, for
478// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700479func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800480 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700481}
482
Colin Crossce75d2c2016-10-06 16:12:58 -0700483// BaseModuleName returns the name of the module as specified in the blueprints file.
484func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800485 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700486}
487
Colin Cross635c3b02016-05-18 15:37:25 -0700488func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800489 return a
490}
491
Colin Crossee0bc3b2018-10-02 22:01:37 -0700492func (a *ModuleBase) SetTarget(target Target, multiTargets []Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700493 a.commonProperties.CompileTarget = target
Colin Crossee0bc3b2018-10-02 22:01:37 -0700494 a.commonProperties.CompileMultiTargets = multiTargets
Colin Cross8b74d172016-09-13 09:59:14 -0700495 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700496}
497
Colin Crossa1ad8d12016-06-01 17:09:44 -0700498func (a *ModuleBase) Target() Target {
499 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800500}
501
Colin Cross8b74d172016-09-13 09:59:14 -0700502func (a *ModuleBase) TargetPrimary() bool {
503 return a.commonProperties.CompilePrimary
504}
505
Colin Crossee0bc3b2018-10-02 22:01:37 -0700506func (a *ModuleBase) MultiTargets() []Target {
507 return a.commonProperties.CompileMultiTargets
508}
509
Colin Crossa1ad8d12016-06-01 17:09:44 -0700510func (a *ModuleBase) Os() OsType {
511 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800512}
513
Colin Cross635c3b02016-05-18 15:37:25 -0700514func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700515 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800516}
517
Colin Cross635c3b02016-05-18 15:37:25 -0700518func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700519 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800520}
521
Dan Willemsen0b24c742016-10-04 15:13:37 -0700522func (a *ModuleBase) ArchSpecific() bool {
523 return a.commonProperties.ArchSpecific
524}
525
Colin Crossa1ad8d12016-06-01 17:09:44 -0700526func (a *ModuleBase) OsClassSupported() []OsClass {
527 switch a.commonProperties.HostOrDeviceSupported {
528 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700529 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700530 case HostSupportedNoCross:
531 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700532 case DeviceSupported:
533 return []OsClass{Device}
Dan Albert0981b5c2018-08-02 13:46:35 -0700534 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700535 var supported []OsClass
Dan Albert0981b5c2018-08-02 13:46:35 -0700536 if Bool(a.hostAndDeviceProperties.Host_supported) ||
537 (a.commonProperties.HostOrDeviceSupported == HostAndDeviceDefault &&
538 a.hostAndDeviceProperties.Host_supported == nil) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700539 supported = append(supported, Host, HostCross)
540 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700541 if a.hostAndDeviceProperties.Device_supported == nil ||
542 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700543 supported = append(supported, Device)
544 }
545 return supported
546 default:
547 return nil
548 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800549}
550
Colin Cross635c3b02016-05-18 15:37:25 -0700551func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800552 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
553 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700554 (a.hostAndDeviceProperties.Device_supported == nil ||
555 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800556}
557
Jiyong Parkc678ad32018-04-10 13:07:10 +0900558func (a *ModuleBase) Platform() bool {
Dario Frenifd05a742018-05-29 13:28:54 +0100559 return !a.DeviceSpecific() && !a.SocSpecific() && !a.ProductSpecific() && !a.ProductServicesSpecific()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900560}
561
562func (a *ModuleBase) DeviceSpecific() bool {
563 return Bool(a.commonProperties.Device_specific)
564}
565
566func (a *ModuleBase) SocSpecific() bool {
567 return Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
568}
569
570func (a *ModuleBase) ProductSpecific() bool {
571 return Bool(a.commonProperties.Product_specific)
572}
573
Dario Frenifd05a742018-05-29 13:28:54 +0100574func (a *ModuleBase) ProductServicesSpecific() bool {
Dario Freni95cf7672018-08-17 00:57:57 +0100575 return Bool(a.commonProperties.Product_services_specific)
Dario Frenifd05a742018-05-29 13:28:54 +0100576}
577
Colin Cross635c3b02016-05-18 15:37:25 -0700578func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800579 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800580 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800581 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800582 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800583}
584
Colin Crossce75d2c2016-10-06 16:12:58 -0700585func (a *ModuleBase) SkipInstall() {
586 a.commonProperties.SkipInstall = true
587}
588
Jiyong Park374510b2018-03-19 18:23:01 +0900589func (a *ModuleBase) ExportedToMake() bool {
590 return a.commonProperties.NamespaceExportedToMake
591}
592
Colin Cross635c3b02016-05-18 15:37:25 -0700593func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700594 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800595
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700596 result := Paths{}
Colin Cross6b753602018-06-21 13:03:07 -0700597 // TODO(ccross): we need to use WalkDeps and have some way to know which dependencies require installation
Colin Cross3f40fa42015-01-30 17:27:36 -0800598 ctx.VisitDepsDepthFirstIf(isFileInstaller,
599 func(m blueprint.Module) {
600 fileInstaller := m.(fileInstaller)
601 files := fileInstaller.filesToInstall()
602 result = append(result, files...)
603 })
604
605 return result
606}
607
Colin Cross635c3b02016-05-18 15:37:25 -0700608func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800609 return a.installFiles
610}
611
Colin Cross635c3b02016-05-18 15:37:25 -0700612func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800613 return p.noAddressSanitizer
614}
615
Colin Cross635c3b02016-05-18 15:37:25 -0700616func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800617 return false
618}
619
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700620func (p *ModuleBase) InstallInSanitizerDir() bool {
621 return false
622}
623
Jiyong Parkf9332f12018-02-01 00:54:12 +0900624func (p *ModuleBase) InstallInRecovery() bool {
625 return Bool(p.commonProperties.Recovery)
626}
627
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900628func (a *ModuleBase) Owner() string {
629 return String(a.commonProperties.Owner)
630}
631
Colin Cross0875c522017-11-28 17:34:01 -0800632func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700633 allInstalledFiles := Paths{}
634 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800635 ctx.VisitAllModuleVariants(func(module Module) {
636 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700637 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
638 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800639 })
640
Colin Cross0875c522017-11-28 17:34:01 -0800641 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700642
Jeff Gaston088e29e2017-11-29 16:47:17 -0800643 namespacePrefix := ctx.Namespace().(*Namespace).id
644 if namespacePrefix != "" {
645 namespacePrefix = namespacePrefix + "-"
646 }
647
Colin Cross3f40fa42015-01-30 17:27:36 -0800648 if len(allInstalledFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800649 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800650 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700651 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800652 Output: name,
653 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800654 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700655 })
656 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700657 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700658 }
659
660 if len(allCheckbuildFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800661 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800662 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700663 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800664 Output: name,
665 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700666 })
667 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700668 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700669 }
670
671 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800672 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800673 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800674 suffix = "-soong"
675 }
676
Jeff Gaston088e29e2017-11-29 16:47:17 -0800677 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800678 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700679 Rule: blueprint.Phony,
Jeff Gaston088e29e2017-11-29 16:47:17 -0800680 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700681 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800682 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700683
684 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800685 }
686}
687
Jiyong Park2db76922017-11-08 16:03:48 +0900688func determineModuleKind(a *ModuleBase, ctx blueprint.BaseModuleContext) moduleKind {
689 var socSpecific = Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
690 var deviceSpecific = Bool(a.commonProperties.Device_specific)
691 var productSpecific = Bool(a.commonProperties.Product_specific)
Dario Freni95cf7672018-08-17 00:57:57 +0100692 var productServicesSpecific = Bool(a.commonProperties.Product_services_specific)
Jiyong Park2db76922017-11-08 16:03:48 +0900693
Dario Frenifd05a742018-05-29 13:28:54 +0100694 msg := "conflicting value set here"
695 if socSpecific && deviceSpecific {
696 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
Jiyong Park2db76922017-11-08 16:03:48 +0900697 if Bool(a.commonProperties.Vendor) {
698 ctx.PropertyErrorf("vendor", msg)
699 }
700 if Bool(a.commonProperties.Proprietary) {
701 ctx.PropertyErrorf("proprietary", msg)
702 }
703 if Bool(a.commonProperties.Soc_specific) {
704 ctx.PropertyErrorf("soc_specific", msg)
705 }
706 }
707
Dario Frenifd05a742018-05-29 13:28:54 +0100708 if productSpecific && productServicesSpecific {
709 ctx.PropertyErrorf("product_specific", "a module cannot be specific to product and product_services at the same time.")
710 ctx.PropertyErrorf("product_services_specific", msg)
711 }
712
713 if (socSpecific || deviceSpecific) && (productSpecific || productServicesSpecific) {
714 if productSpecific {
715 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
716 } else {
717 ctx.PropertyErrorf("product_services_specific", "a module cannot be specific to SoC or device and product_services at the same time.")
718 }
719 if deviceSpecific {
720 ctx.PropertyErrorf("device_specific", msg)
721 } else {
722 if Bool(a.commonProperties.Vendor) {
723 ctx.PropertyErrorf("vendor", msg)
724 }
725 if Bool(a.commonProperties.Proprietary) {
726 ctx.PropertyErrorf("proprietary", msg)
727 }
728 if Bool(a.commonProperties.Soc_specific) {
729 ctx.PropertyErrorf("soc_specific", msg)
730 }
731 }
732 }
733
Jiyong Park2db76922017-11-08 16:03:48 +0900734 if productSpecific {
735 return productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100736 } else if productServicesSpecific {
737 return productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900738 } else if deviceSpecific {
739 return deviceSpecificModule
740 } else if socSpecific {
741 return socSpecificModule
742 } else {
743 return platformModule
744 }
745}
746
Colin Cross635c3b02016-05-18 15:37:25 -0700747func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700748 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700749 target: a.commonProperties.CompileTarget,
750 targetPrimary: a.commonProperties.CompilePrimary,
Colin Crossee0bc3b2018-10-02 22:01:37 -0700751 multiTargets: a.commonProperties.CompileMultiTargets,
Jiyong Park2db76922017-11-08 16:03:48 +0900752 kind: determineModuleKind(a, ctx),
Colin Cross8b74d172016-09-13 09:59:14 -0700753 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800754 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800755}
756
Colin Cross0875c522017-11-28 17:34:01 -0800757func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
758 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700759 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800760 ModuleContext: blueprintCtx,
761 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
762 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700763 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800764 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800765 }
766
Colin Cross67a5c132017-05-09 13:45:28 -0700767 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
768 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800769 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
770 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700771 }
Colin Cross0875c522017-11-28 17:34:01 -0800772 if !ctx.PrimaryArch() {
773 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700774 }
775
776 ctx.Variable(pctx, "moduleDesc", desc)
777
778 s := ""
779 if len(suffix) > 0 {
780 s = " [" + strings.Join(suffix, " ") + "]"
781 }
782 ctx.Variable(pctx, "moduleDescSuffix", s)
783
Colin Cross9b1d13d2016-09-19 15:18:11 -0700784 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800785 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700786 if ctx.Failed() {
787 return
788 }
789
Colin Cross0875c522017-11-28 17:34:01 -0800790 a.installFiles = append(a.installFiles, ctx.installFiles...)
791 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800792 }
793
Colin Cross9b1d13d2016-09-19 15:18:11 -0700794 if a == ctx.FinalModule().(Module).base() {
795 a.generateModuleTarget(ctx)
796 if ctx.Failed() {
797 return
798 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800799 }
Colin Crosscec81712017-07-13 14:43:27 -0700800
Colin Cross0875c522017-11-28 17:34:01 -0800801 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800802}
803
Colin Crossf6566ed2015-03-24 11:13:38 -0700804type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700805 target Target
Colin Crossee0bc3b2018-10-02 22:01:37 -0700806 multiTargets []Target
Colin Cross8b74d172016-09-13 09:59:14 -0700807 targetPrimary bool
808 debug bool
Jiyong Park2db76922017-11-08 16:03:48 +0900809 kind moduleKind
Colin Cross8b74d172016-09-13 09:59:14 -0700810 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700811}
812
Colin Cross3f40fa42015-01-30 17:27:36 -0800813type androidModuleContext struct {
814 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700815 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700816 installDeps Paths
817 installFiles Paths
818 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800819 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700820 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700821
822 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700823 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800824}
825
Colin Cross67a5c132017-05-09 13:45:28 -0700826func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800827 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700828 Rule: ErrorRule,
829 Description: desc,
830 Outputs: outputs,
831 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800832 Args: map[string]string{
833 "error": err.Error(),
834 },
835 })
836 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800837}
838
Colin Crossaabf6792017-11-29 00:27:14 -0800839func (a *androidModuleContext) Config() Config {
840 return a.ModuleContext.Config().(Config)
841}
842
Colin Cross0875c522017-11-28 17:34:01 -0800843func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700844 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800845}
846
Colin Cross0875c522017-11-28 17:34:01 -0800847func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700848 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700849 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800850 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800851 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700852 Outputs: params.Outputs.Strings(),
853 ImplicitOutputs: params.ImplicitOutputs.Strings(),
854 Inputs: params.Inputs.Strings(),
855 Implicits: params.Implicits.Strings(),
856 OrderOnly: params.OrderOnly.Strings(),
857 Args: params.Args,
858 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700859 }
860
Colin Cross33bfb0a2016-11-21 17:23:08 -0800861 if params.Depfile != nil {
862 bparams.Depfile = params.Depfile.String()
863 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700864 if params.Output != nil {
865 bparams.Outputs = append(bparams.Outputs, params.Output.String())
866 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700867 if params.ImplicitOutput != nil {
868 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
869 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700870 if params.Input != nil {
871 bparams.Inputs = append(bparams.Inputs, params.Input.String())
872 }
873 if params.Implicit != nil {
874 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
875 }
876
Colin Crossfe4bc362018-09-12 10:02:13 -0700877 bparams.Outputs = proptools.NinjaEscape(bparams.Outputs)
878 bparams.ImplicitOutputs = proptools.NinjaEscape(bparams.ImplicitOutputs)
879 bparams.Inputs = proptools.NinjaEscape(bparams.Inputs)
880 bparams.Implicits = proptools.NinjaEscape(bparams.Implicits)
881 bparams.OrderOnly = proptools.NinjaEscape(bparams.OrderOnly)
882 bparams.Depfile = proptools.NinjaEscape([]string{bparams.Depfile})[0]
883
Colin Cross0875c522017-11-28 17:34:01 -0800884 return bparams
885}
886
887func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
888 a.ModuleContext.Variable(pctx.PackageContext, name, value)
889}
890
891func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
892 argNames ...string) blueprint.Rule {
893
894 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
895}
896
897func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
898 if a.config.captureBuild {
899 a.buildParams = append(a.buildParams, params)
900 }
901
902 bparams := convertBuildParams(params)
903
904 if bparams.Description != "" {
905 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
906 }
907
Colin Cross6ff51382015-12-17 16:39:19 -0800908 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700909 a.ninjaError(bparams.Description, bparams.Outputs,
910 fmt.Errorf("module %s missing dependencies: %s\n",
911 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800912 return
913 }
914
Colin Cross0875c522017-11-28 17:34:01 -0800915 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700916}
917
Colin Cross6ff51382015-12-17 16:39:19 -0800918func (a *androidModuleContext) GetMissingDependencies() []string {
919 return a.missingDeps
920}
921
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800922func (a *androidModuleContext) AddMissingDependencies(deps []string) {
923 if deps != nil {
924 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700925 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800926 }
927}
928
Colin Crossd11fcda2017-10-23 17:59:01 -0700929func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
930 aModule, _ := module.(Module)
931 if aModule == nil {
932 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
933 return nil
934 }
935
936 if !aModule.Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800937 if a.Config().AllowMissingDependencies() {
Colin Crossd11fcda2017-10-23 17:59:01 -0700938 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
939 } else {
940 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
941 }
942 return nil
943 }
944
945 return aModule
946}
947
Colin Cross35143d02017-11-16 00:11:20 -0800948func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
949 a.ModuleContext.VisitDirectDeps(visit)
950}
951
Colin Crossd11fcda2017-10-23 17:59:01 -0700952func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
953 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
954 if aModule := a.validateAndroidModule(module); aModule != nil {
955 visit(aModule)
956 }
957 })
958}
959
Colin Crossee6143c2017-12-30 17:54:27 -0800960func (a *androidModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
961 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
962 if aModule := a.validateAndroidModule(module); aModule != nil {
963 if a.ModuleContext.OtherModuleDependencyTag(aModule) == tag {
964 visit(aModule)
965 }
966 }
967 })
968}
969
Colin Crossd11fcda2017-10-23 17:59:01 -0700970func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
971 a.ModuleContext.VisitDirectDepsIf(
972 // pred
973 func(module blueprint.Module) bool {
974 if aModule := a.validateAndroidModule(module); aModule != nil {
975 return pred(aModule)
976 } else {
977 return false
978 }
979 },
980 // visit
981 func(module blueprint.Module) {
982 visit(module.(Module))
983 })
984}
985
986func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
987 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
988 if aModule := a.validateAndroidModule(module); aModule != nil {
989 visit(aModule)
990 }
991 })
992}
993
994func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
995 a.ModuleContext.VisitDepsDepthFirstIf(
996 // pred
997 func(module blueprint.Module) bool {
998 if aModule := a.validateAndroidModule(module); aModule != nil {
999 return pred(aModule)
1000 } else {
1001 return false
1002 }
1003 },
1004 // visit
1005 func(module blueprint.Module) {
1006 visit(module.(Module))
1007 })
1008}
1009
1010func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
1011 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
1012 childAndroidModule := a.validateAndroidModule(child)
1013 parentAndroidModule := a.validateAndroidModule(parent)
1014 if childAndroidModule != nil && parentAndroidModule != nil {
1015 return visit(childAndroidModule, parentAndroidModule)
1016 } else {
1017 return false
1018 }
1019 })
1020}
1021
Colin Cross0875c522017-11-28 17:34:01 -08001022func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
1023 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
1024 visit(module.(Module))
1025 })
1026}
1027
1028func (a *androidModuleContext) PrimaryModule() Module {
1029 return a.ModuleContext.PrimaryModule().(Module)
1030}
1031
1032func (a *androidModuleContext) FinalModule() Module {
1033 return a.ModuleContext.FinalModule().(Module)
1034}
1035
Colin Crossa1ad8d12016-06-01 17:09:44 -07001036func (a *androidBaseContextImpl) Target() Target {
1037 return a.target
1038}
1039
Colin Cross8b74d172016-09-13 09:59:14 -07001040func (a *androidBaseContextImpl) TargetPrimary() bool {
1041 return a.targetPrimary
1042}
1043
Colin Crossee0bc3b2018-10-02 22:01:37 -07001044func (a *androidBaseContextImpl) MultiTargets() []Target {
1045 return a.multiTargets
1046}
1047
Colin Crossf6566ed2015-03-24 11:13:38 -07001048func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001049 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -08001050}
1051
Colin Crossa1ad8d12016-06-01 17:09:44 -07001052func (a *androidBaseContextImpl) Os() OsType {
1053 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -08001054}
1055
Colin Crossf6566ed2015-03-24 11:13:38 -07001056func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001057 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -07001058}
1059
1060func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001061 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -07001062}
1063
Colin Cross0af4b842015-04-30 16:36:18 -07001064func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001065 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -07001066}
1067
Colin Cross3edeee12017-04-04 12:59:48 -07001068func (a *androidBaseContextImpl) Windows() bool {
1069 return a.target.Os == Windows
1070}
1071
Colin Crossf6566ed2015-03-24 11:13:38 -07001072func (a *androidBaseContextImpl) Debug() bool {
1073 return a.debug
1074}
1075
Colin Cross1e7d3702016-08-24 15:25:47 -07001076func (a *androidBaseContextImpl) PrimaryArch() bool {
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001077 if len(a.config.Targets[a.target.Os]) <= 1 {
Colin Cross67a5c132017-05-09 13:45:28 -07001078 return true
1079 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001080 return a.target.Arch.ArchType == a.config.Targets[a.target.Os][0].Arch.ArchType
Colin Cross1e7d3702016-08-24 15:25:47 -07001081}
1082
Colin Cross1332b002015-04-07 17:11:30 -07001083func (a *androidBaseContextImpl) AConfig() Config {
1084 return a.config
1085}
1086
Colin Cross9272ade2016-08-17 15:24:12 -07001087func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
1088 return DeviceConfig{a.config.deviceConfig}
1089}
1090
Jiyong Park2db76922017-11-08 16:03:48 +09001091func (a *androidBaseContextImpl) Platform() bool {
1092 return a.kind == platformModule
1093}
1094
1095func (a *androidBaseContextImpl) DeviceSpecific() bool {
1096 return a.kind == deviceSpecificModule
1097}
1098
1099func (a *androidBaseContextImpl) SocSpecific() bool {
1100 return a.kind == socSpecificModule
1101}
1102
1103func (a *androidBaseContextImpl) ProductSpecific() bool {
1104 return a.kind == productSpecificModule
Dan Willemsen782a2d12015-12-21 14:55:28 -08001105}
1106
Dario Frenifd05a742018-05-29 13:28:54 +01001107func (a *androidBaseContextImpl) ProductServicesSpecific() bool {
1108 return a.kind == productServicesSpecificModule
1109}
1110
Jiyong Park5baac542018-08-28 09:55:37 +09001111// Makes this module a platform module, i.e. not specific to soc, device,
1112// product, or product_services.
1113func (a *ModuleBase) MakeAsPlatform() {
1114 a.commonProperties.Vendor = boolPtr(false)
1115 a.commonProperties.Proprietary = boolPtr(false)
1116 a.commonProperties.Soc_specific = boolPtr(false)
1117 a.commonProperties.Product_specific = boolPtr(false)
1118 a.commonProperties.Product_services_specific = boolPtr(false)
1119}
1120
Colin Cross8d8f8e22016-08-03 11:57:50 -07001121func (a *androidModuleContext) InstallInData() bool {
1122 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08001123}
1124
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001125func (a *androidModuleContext) InstallInSanitizerDir() bool {
1126 return a.module.InstallInSanitizerDir()
1127}
1128
Jiyong Parkf9332f12018-02-01 00:54:12 +09001129func (a *androidModuleContext) InstallInRecovery() bool {
1130 return a.module.InstallInRecovery()
1131}
1132
Colin Cross893d8162017-04-26 17:34:03 -07001133func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
1134 if a.module.base().commonProperties.SkipInstall {
1135 return true
1136 }
1137
Colin Cross3607f212018-05-07 15:28:05 -07001138 // We'll need a solution for choosing which of modules with the same name in different
1139 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
1140 // list of namespaces to install in a Soong-only build.
1141 if !a.module.base().commonProperties.NamespaceExportedToMake {
1142 return true
1143 }
1144
Colin Cross893d8162017-04-26 17:34:03 -07001145 if a.Device() {
Colin Cross6510f912017-11-29 00:27:14 -08001146 if a.Config().SkipDeviceInstall() {
Colin Cross893d8162017-04-26 17:34:03 -07001147 return true
1148 }
1149
Colin Cross6510f912017-11-29 00:27:14 -08001150 if a.Config().SkipMegaDeviceInstall(fullInstallPath.String()) {
Colin Cross893d8162017-04-26 17:34:03 -07001151 return true
1152 }
1153 }
1154
1155 return false
1156}
1157
Colin Cross5c517922017-08-31 12:29:17 -07001158func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -07001159 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -07001160 return a.installFile(installPath, name, srcPath, Cp, deps)
1161}
1162
1163func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
1164 deps ...Path) OutputPath {
1165 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
1166}
1167
1168func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
1169 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -07001170
Dan Willemsen782a2d12015-12-21 14:55:28 -08001171 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001172 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001173
Colin Cross893d8162017-04-26 17:34:03 -07001174 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001175
Dan Willemsen322acaf2016-01-12 23:07:05 -08001176 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -07001177
Colin Cross89562dc2016-10-03 17:47:19 -07001178 var implicitDeps, orderOnlyDeps Paths
1179
1180 if a.Host() {
1181 // Installed host modules might be used during the build, depend directly on their
1182 // dependencies so their timestamp is updated whenever their dependency is updated
1183 implicitDeps = deps
1184 } else {
1185 orderOnlyDeps = deps
1186 }
1187
Colin Crossae887032017-10-23 17:16:14 -07001188 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07001189 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07001190 Description: "install " + fullInstallPath.Base(),
1191 Output: fullInstallPath,
1192 Input: srcPath,
1193 Implicits: implicitDeps,
1194 OrderOnly: orderOnlyDeps,
Colin Cross6510f912017-11-29 00:27:14 -08001195 Default: !a.Config().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08001196 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001197
Dan Willemsen322acaf2016-01-12 23:07:05 -08001198 a.installFiles = append(a.installFiles, fullInstallPath)
1199 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001200 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -07001201 return fullInstallPath
1202}
1203
Colin Cross3854a602016-01-11 12:49:11 -08001204func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
1205 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001206 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08001207
Colin Cross893d8162017-04-26 17:34:03 -07001208 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001209
Colin Crossae887032017-10-23 17:16:14 -07001210 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001211 Rule: Symlink,
1212 Description: "install symlink " + fullInstallPath.Base(),
1213 Output: fullInstallPath,
1214 OrderOnly: Paths{srcPath},
Colin Cross6510f912017-11-29 00:27:14 -08001215 Default: !a.Config().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -08001216 Args: map[string]string{
1217 "fromPath": srcPath.String(),
1218 },
1219 })
Colin Cross3854a602016-01-11 12:49:11 -08001220
Colin Cross12fc4972016-01-11 12:49:11 -08001221 a.installFiles = append(a.installFiles, fullInstallPath)
1222 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1223 }
Colin Cross3854a602016-01-11 12:49:11 -08001224 return fullInstallPath
1225}
1226
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001227func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001228 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1229}
1230
Colin Cross3f40fa42015-01-30 17:27:36 -08001231type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001232 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001233}
1234
1235func isFileInstaller(m blueprint.Module) bool {
1236 _, ok := m.(fileInstaller)
1237 return ok
1238}
1239
1240func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -07001241 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -08001242 return ok
1243}
Colin Crossfce53272015-04-08 11:21:40 -07001244
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001245func findStringInSlice(str string, slice []string) int {
1246 for i, s := range slice {
1247 if s == str {
1248 return i
Colin Crossfce53272015-04-08 11:21:40 -07001249 }
1250 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001251 return -1
1252}
1253
Colin Cross068e0fe2016-12-13 15:23:47 -08001254func SrcIsModule(s string) string {
1255 if len(s) > 1 && s[0] == ':' {
1256 return s[1:]
1257 }
1258 return ""
1259}
1260
1261type sourceDependencyTag struct {
1262 blueprint.BaseDependencyTag
1263}
1264
1265var SourceDepTag sourceDependencyTag
1266
Colin Cross366938f2017-12-11 16:29:02 -08001267// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
1268// using ":module" syntax, if any.
Colin Cross068e0fe2016-12-13 15:23:47 -08001269func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1270 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001271 set := make(map[string]bool)
1272
Colin Cross068e0fe2016-12-13 15:23:47 -08001273 for _, s := range srcFiles {
1274 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001275 if _, found := set[m]; found {
1276 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1277 } else {
1278 set[m] = true
1279 deps = append(deps, m)
1280 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001281 }
1282 }
1283
1284 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1285}
1286
Colin Cross366938f2017-12-11 16:29:02 -08001287// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
1288// using ":module" syntax, if any.
1289func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
1290 if s != nil {
1291 if m := SrcIsModule(*s); m != "" {
1292 ctx.AddDependency(ctx.Module(), SourceDepTag, m)
1293 }
1294 }
1295}
1296
Colin Cross068e0fe2016-12-13 15:23:47 -08001297type SourceFileProducer interface {
1298 Srcs() Paths
1299}
1300
1301// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001302// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001303func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001304 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1305}
1306
Colin Cross366938f2017-12-11 16:29:02 -08001307// Returns a single path expanded from globs and modules referenced using ":module" syntax.
1308// ExtractSourceDeps must have already been called during the dependency resolution phase.
1309func (ctx *androidModuleContext) ExpandSource(srcFile, prop string) Path {
1310 srcFiles := ctx.ExpandSourcesSubDir([]string{srcFile}, nil, "")
1311 if len(srcFiles) == 1 {
1312 return srcFiles[0]
1313 } else {
1314 ctx.PropertyErrorf(prop, "module providing %s must produce exactly one file", prop)
1315 return nil
1316 }
1317}
1318
Colin Cross2383f3b2018-02-06 14:40:13 -08001319// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
1320// the srcFile is non-nil.
1321// ExtractSourceDeps must have already been called during the dependency resolution phase.
1322func (ctx *androidModuleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
1323 if srcFile != nil {
1324 return OptionalPathForPath(ctx.ExpandSource(*srcFile, prop))
1325 }
1326 return OptionalPath{}
1327}
1328
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001329func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001330 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001331
Colin Cross461b4452018-02-23 09:22:42 -08001332 var expandedExcludes []string
1333 if excludes != nil {
1334 expandedExcludes = make([]string, 0, len(excludes))
1335 }
Nan Zhang27e284d2018-02-09 21:03:53 +00001336
1337 for _, e := range excludes {
1338 if m := SrcIsModule(e); m != "" {
1339 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
1340 if module == nil {
1341 // Error will have been handled by ExtractSourcesDeps
1342 continue
1343 }
1344 if srcProducer, ok := module.(SourceFileProducer); ok {
1345 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
1346 } else {
1347 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1348 }
1349 } else {
1350 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001351 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001352 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001353 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001354 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001355 if m := SrcIsModule(s); m != "" {
1356 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001357 if module == nil {
1358 // Error will have been handled by ExtractSourcesDeps
1359 continue
1360 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001361 if srcProducer, ok := module.(SourceFileProducer); ok {
Nan Zhang27e284d2018-02-09 21:03:53 +00001362 moduleSrcs := srcProducer.Srcs()
1363 for _, e := range expandedExcludes {
1364 for j, ms := range moduleSrcs {
1365 if ms.String() == e {
1366 moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
1367 }
1368 }
1369 }
1370 expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001371 } else {
1372 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1373 }
1374 } else if pathtools.IsGlob(s) {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001375 globbedSrcFiles := ctx.GlobFiles(filepath.Join(prefix, s), expandedExcludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001376 for i, s := range globbedSrcFiles {
1377 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001378 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001379 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001380 } else {
Nan Zhang27e284d2018-02-09 21:03:53 +00001381 p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1382 j := findStringInSlice(p.String(), expandedExcludes)
1383 if j == -1 {
1384 expandedSrcFiles = append(expandedSrcFiles, p)
1385 }
1386
Colin Cross8f101b42015-06-17 15:09:06 -07001387 }
1388 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001389 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001390}
1391
Nan Zhang6d34b302017-02-04 17:47:46 -08001392func (ctx *androidModuleContext) RequiredModuleNames() []string {
1393 return ctx.module.base().commonProperties.Required
1394}
1395
Colin Cross7f19f372016-11-01 11:10:25 -07001396func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1397 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001398 if err != nil {
1399 ctx.ModuleErrorf("glob: %s", err.Error())
1400 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001401 return pathsForModuleSrcFromFullPath(ctx, ret, true)
Colin Crossfce53272015-04-08 11:21:40 -07001402}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001403
Nan Zhang581fd212018-01-10 16:06:12 -08001404func (ctx *androidModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001405 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Nan Zhang581fd212018-01-10 16:06:12 -08001406 if err != nil {
1407 ctx.ModuleErrorf("glob: %s", err.Error())
1408 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001409 return pathsForModuleSrcFromFullPath(ctx, ret, false)
Nan Zhang581fd212018-01-10 16:06:12 -08001410}
1411
Colin Cross463a90e2015-06-17 14:20:06 -07001412func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001413 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001414}
1415
Colin Cross0875c522017-11-28 17:34:01 -08001416func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001417 return &buildTargetSingleton{}
1418}
1419
Colin Cross87d8b562017-04-25 10:01:55 -07001420func parentDir(dir string) string {
1421 dir, _ = filepath.Split(dir)
1422 return filepath.Clean(dir)
1423}
1424
Colin Cross1f8c52b2015-06-16 16:38:17 -07001425type buildTargetSingleton struct{}
1426
Colin Cross0875c522017-11-28 17:34:01 -08001427func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1428 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001429
Colin Cross0875c522017-11-28 17:34:01 -08001430 mmTarget := func(dir string) WritablePath {
1431 return PathForPhony(ctx,
1432 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001433 }
1434
Colin Cross0875c522017-11-28 17:34:01 -08001435 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001436
Colin Cross0875c522017-11-28 17:34:01 -08001437 ctx.VisitAllModules(func(module Module) {
1438 blueprintDir := module.base().blueprintDir
1439 installTarget := module.base().installTarget
1440 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001441
Colin Cross0875c522017-11-28 17:34:01 -08001442 if checkbuildTarget != nil {
1443 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1444 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1445 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001446
Colin Cross0875c522017-11-28 17:34:01 -08001447 if installTarget != nil {
1448 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001449 }
1450 })
1451
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001452 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001453 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001454 suffix = "-soong"
1455 }
1456
Colin Cross1f8c52b2015-06-16 16:38:17 -07001457 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001458 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001459 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001460 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001461 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001462 })
1463
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001464 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001465 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001466 return
1467 }
1468
Colin Cross0875c522017-11-28 17:34:01 -08001469 sortedKeys := func(m map[string]Paths) []string {
1470 s := make([]string, 0, len(m))
1471 for k := range m {
1472 s = append(s, k)
1473 }
1474 sort.Strings(s)
1475 return s
1476 }
1477
Colin Cross87d8b562017-04-25 10:01:55 -07001478 // Ensure ancestor directories are in modulesInDir
1479 dirs := sortedKeys(modulesInDir)
1480 for _, dir := range dirs {
1481 dir := parentDir(dir)
1482 for dir != "." && dir != "/" {
1483 if _, exists := modulesInDir[dir]; exists {
1484 break
1485 }
1486 modulesInDir[dir] = nil
1487 dir = parentDir(dir)
1488 }
1489 }
1490
1491 // Make directories build their direct subdirectories
1492 dirs = sortedKeys(modulesInDir)
1493 for _, dir := range dirs {
1494 p := parentDir(dir)
1495 if p != "." && p != "/" {
1496 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1497 }
1498 }
1499
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001500 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1501 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1502 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001503 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001504 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001505 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001506 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001507 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001508 // HACK: checkbuild should be an optional build, but force it
1509 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001510 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001511 })
1512 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001513
1514 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1515 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001516 ctx.VisitAllModules(func(module Module) {
1517 if module.Enabled() {
1518 os := module.Target().Os
1519 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001520 }
1521 })
1522
Colin Cross0875c522017-11-28 17:34:01 -08001523 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001524 for os, deps := range osDeps {
1525 var className string
1526
1527 switch os.Class {
1528 case Host:
1529 className = "host"
1530 case HostCross:
1531 className = "host-cross"
1532 case Device:
1533 className = "target"
1534 default:
1535 continue
1536 }
1537
Colin Cross0875c522017-11-28 17:34:01 -08001538 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001539 osClass[className] = append(osClass[className], name)
1540
Colin Cross0875c522017-11-28 17:34:01 -08001541 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001542 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001543 Output: name,
1544 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001545 })
1546 }
1547
1548 // Wrap those into host|host-cross|target phony rules
1549 osClasses := sortedKeys(osClass)
1550 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001551 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001552 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001553 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001554 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001555 })
1556 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001557}
Colin Crossd779da42015-12-17 18:00:23 -08001558
Colin Cross2465c3d2018-09-28 10:19:18 -07001559type ModulesByName struct {
1560 slice []blueprint.Module
Colin Crossd779da42015-12-17 18:00:23 -08001561 ctx interface {
1562 ModuleName(blueprint.Module) string
1563 ModuleSubDir(blueprint.Module) string
1564 }
1565}
1566
Colin Cross2465c3d2018-09-28 10:19:18 -07001567func (s ModulesByName) Len() int { return len(s.slice) }
1568func (s ModulesByName) Less(i, j int) bool {
Colin Crossd779da42015-12-17 18:00:23 -08001569 mi, mj := s.slice[i], s.slice[j]
1570 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1571
1572 if ni != nj {
1573 return ni < nj
1574 } else {
1575 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1576 }
1577}
Colin Cross2465c3d2018-09-28 10:19:18 -07001578func (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 -07001579
1580// Collect information for opening IDE project files in java/jdeps.go.
1581type IDEInfo interface {
1582 IDEInfo(ideInfo *IdeInfo)
1583 BaseModuleName() string
1584}
1585
1586// Extract the base module name from the Import name.
1587// Often the Import name has a prefix "prebuilt_".
1588// Remove the prefix explicitly if needed
1589// until we find a better solution to get the Import name.
1590type IDECustomizedModuleName interface {
1591 IDECustomizedModuleName() string
1592}
1593
1594type IdeInfo struct {
1595 Deps []string `json:"dependencies,omitempty"`
1596 Srcs []string `json:"srcs,omitempty"`
1597 Aidl_include_dirs []string `json:"aidl_include_dirs,omitempty"`
1598 Jarjar_rules []string `json:"jarjar_rules,omitempty"`
1599 Jars []string `json:"jars,omitempty"`
1600 Classes []string `json:"class,omitempty"`
1601 Installed_paths []string `json:"installed,omitempty"`
1602}