blob: dc0c85603e5b944342ce35ff30a8a1e2e1aebd75 [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
Dan Willemsen569edc52018-11-19 09:33:29 -0800268 Dist struct {
269 // copy the output of this module to the $DIST_DIR when `dist` is specified on the
270 // command line and any of these targets are also on the command line, or otherwise
271 // built
272 Targets []string `android:"arch_variant"`
273
274 // The name of the output artifact. This defaults to the basename of the output of
275 // the module.
276 Dest *string `android:"arch_variant"`
277
278 // The directory within the dist directory to store the artifact. Defaults to the
279 // top level directory ("").
280 Dir *string `android:"arch_variant"`
281
282 // A suffix to add to the artifact file name (before any extension).
283 Suffix *string `android:"arch_variant"`
284 } `android:"arch_variant"`
285
Colin Crossa1ad8d12016-06-01 17:09:44 -0700286 // Set by TargetMutator
Colin Crossee0bc3b2018-10-02 22:01:37 -0700287 CompileTarget Target `blueprint:"mutated"`
288 CompileMultiTargets []Target `blueprint:"mutated"`
289 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800290
291 // Set by InitAndroidModule
292 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700293 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700294
295 SkipInstall bool `blueprint:"mutated"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800296
297 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800298}
299
300type hostAndDeviceProperties struct {
Colin Cross4e81d702018-11-09 10:36:55 -0800301 // If set to true, build a variant of the module for the host. Defaults to false.
302 Host_supported *bool
303
304 // If set to true, build a variant of the module for the device. Defaults to true.
Colin Crossa4190c12016-07-12 13:11:25 -0700305 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800306}
307
Colin Crossc472d572015-03-17 15:06:21 -0700308type Multilib string
309
310const (
Colin Cross6b4a32d2017-12-05 13:42:45 -0800311 MultilibBoth Multilib = "both"
312 MultilibFirst Multilib = "first"
313 MultilibCommon Multilib = "common"
314 MultilibCommonFirst Multilib = "common_first"
315 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700316)
317
Colin Crossa1ad8d12016-06-01 17:09:44 -0700318type HostOrDeviceSupported int
319
320const (
321 _ HostOrDeviceSupported = iota
Dan Albert0981b5c2018-08-02 13:46:35 -0700322
323 // Host and HostCross are built by default. Device is not supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700324 HostSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700325
326 // Host is built by default. HostCross and Device are not supported.
Dan Albertc6345fb2016-10-20 01:36:11 -0700327 HostSupportedNoCross
Dan Albert0981b5c2018-08-02 13:46:35 -0700328
329 // Device is built by default. Host and HostCross are not supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700330 DeviceSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700331
332 // Device is built by default. Host and HostCross are supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700333 HostAndDeviceSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700334
335 // Host, HostCross, and Device are built by default.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700336 HostAndDeviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700337
338 // Nothing is supported. This is not exposed to the user, but used to mark a
339 // host only module as unsupported when the module type is not supported on
340 // the host OS. E.g. benchmarks are supported on Linux but not Darwin.
Dan Willemsen0b24c742016-10-04 15:13:37 -0700341 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700342)
343
Jiyong Park2db76922017-11-08 16:03:48 +0900344type moduleKind int
345
346const (
347 platformModule moduleKind = iota
348 deviceSpecificModule
349 socSpecificModule
350 productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100351 productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900352)
353
354func (k moduleKind) String() string {
355 switch k {
356 case platformModule:
357 return "platform"
358 case deviceSpecificModule:
359 return "device-specific"
360 case socSpecificModule:
361 return "soc-specific"
362 case productSpecificModule:
363 return "product-specific"
Dario Frenifd05a742018-05-29 13:28:54 +0100364 case productServicesSpecificModule:
365 return "productservices-specific"
Jiyong Park2db76922017-11-08 16:03:48 +0900366 default:
367 panic(fmt.Errorf("unknown module kind %d", k))
368 }
369}
370
Colin Cross36242852017-06-23 15:06:31 -0700371func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800372 base := m.base()
373 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700374
Colin Cross36242852017-06-23 15:06:31 -0700375 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700376 &base.nameProperties,
377 &base.commonProperties,
378 &base.variableProperties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700379 base.customizableProperties = m.GetProperties()
Colin Cross5049f022015-03-18 13:28:46 -0700380}
381
Colin Cross36242852017-06-23 15:06:31 -0700382func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
383 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700384
385 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800386 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700387 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700388 base.commonProperties.ArchSpecific = true
Colin Crossee0bc3b2018-10-02 22:01:37 -0700389 base.commonProperties.UseTargetVariants = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800390
Dan Willemsen218f6562015-07-08 18:13:11 -0700391 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700392 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700393 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800394 }
395
Colin Cross36242852017-06-23 15:06:31 -0700396 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800397}
398
Colin Crossee0bc3b2018-10-02 22:01:37 -0700399func InitAndroidMultiTargetsArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
400 InitAndroidArchModule(m, hod, defaultMultilib)
401 m.base().commonProperties.UseTargetVariants = false
402}
403
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800404// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800405// modules. It should be included as an anonymous field in every module
406// struct definition. InitAndroidModule should then be called from the module's
407// factory function, and the return values from InitAndroidModule should be
408// returned from the factory function.
409//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800410// The ModuleBase type is responsible for implementing the GenerateBuildActions
411// method to support the blueprint.Module interface. This method will then call
412// the module's GenerateAndroidBuildActions method once for each build variant
413// that is to be built. GenerateAndroidBuildActions is passed a
414// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800415// AndroidModuleContext exposes extra functionality specific to the Android build
416// system including details about the particular build variant that is to be
417// generated.
418//
419// For example:
420//
421// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800422// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800423// )
424//
425// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800426// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800427// properties struct {
428// MyProperty string
429// }
430// }
431//
Colin Cross36242852017-06-23 15:06:31 -0700432// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800433// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700434// m.AddProperties(&m.properties)
435// android.InitAndroidModule(m)
436// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800437// }
438//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800439// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800440// // Get the CPU architecture for the current build variant.
441// variantArch := ctx.Arch()
442//
443// // ...
444// }
Colin Cross635c3b02016-05-18 15:37:25 -0700445type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800446 // Putting the curiously recurring thing pointing to the thing that contains
447 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700448 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700449 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800450
Colin Crossfc754582016-05-17 16:34:16 -0700451 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800452 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700453 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800454 hostAndDeviceProperties hostAndDeviceProperties
455 generalProperties []interface{}
Colin Crossc17727d2018-10-24 12:42:09 -0700456 archProperties [][]interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700457 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800458
459 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700460 installFiles Paths
461 checkbuildFiles Paths
Jaewoong Jung62707f72018-11-16 13:26:43 -0800462 noticeFile Path
Colin Cross1f8c52b2015-06-16 16:38:17 -0700463
464 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
465 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800466 installTarget WritablePath
467 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700468 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700469
Colin Cross178a5092016-09-13 13:42:32 -0700470 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700471
472 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700473
474 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700475 buildParams []BuildParams
Colin Crossa9d8bee2018-10-02 13:59:46 -0700476
477 prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool
Colin Cross36242852017-06-23 15:06:31 -0700478}
479
480func (a *ModuleBase) AddProperties(props ...interface{}) {
481 a.registerProps = append(a.registerProps, props...)
482}
483
484func (a *ModuleBase) GetProperties() []interface{} {
485 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800486}
487
Colin Crossae887032017-10-23 17:16:14 -0700488func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700489 return a.buildParams
490}
491
Colin Crossa9d8bee2018-10-02 13:59:46 -0700492func (a *ModuleBase) Prefer32(prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool) {
493 a.prefer32 = prefer32
494}
495
Colin Crossce75d2c2016-10-06 16:12:58 -0700496// Name returns the name of the module. It may be overridden by individual module types, for
497// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700498func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800499 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700500}
501
Colin Crossce75d2c2016-10-06 16:12:58 -0700502// BaseModuleName returns the name of the module as specified in the blueprints file.
503func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800504 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700505}
506
Colin Cross635c3b02016-05-18 15:37:25 -0700507func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800508 return a
509}
510
Colin Crossee0bc3b2018-10-02 22:01:37 -0700511func (a *ModuleBase) SetTarget(target Target, multiTargets []Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700512 a.commonProperties.CompileTarget = target
Colin Crossee0bc3b2018-10-02 22:01:37 -0700513 a.commonProperties.CompileMultiTargets = multiTargets
Colin Cross8b74d172016-09-13 09:59:14 -0700514 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700515}
516
Colin Crossa1ad8d12016-06-01 17:09:44 -0700517func (a *ModuleBase) Target() Target {
518 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800519}
520
Colin Cross8b74d172016-09-13 09:59:14 -0700521func (a *ModuleBase) TargetPrimary() bool {
522 return a.commonProperties.CompilePrimary
523}
524
Colin Crossee0bc3b2018-10-02 22:01:37 -0700525func (a *ModuleBase) MultiTargets() []Target {
526 return a.commonProperties.CompileMultiTargets
527}
528
Colin Crossa1ad8d12016-06-01 17:09:44 -0700529func (a *ModuleBase) Os() OsType {
530 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800531}
532
Colin Cross635c3b02016-05-18 15:37:25 -0700533func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700534 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800535}
536
Colin Cross635c3b02016-05-18 15:37:25 -0700537func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700538 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800539}
540
Dan Willemsen0b24c742016-10-04 15:13:37 -0700541func (a *ModuleBase) ArchSpecific() bool {
542 return a.commonProperties.ArchSpecific
543}
544
Colin Crossa1ad8d12016-06-01 17:09:44 -0700545func (a *ModuleBase) OsClassSupported() []OsClass {
546 switch a.commonProperties.HostOrDeviceSupported {
547 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700548 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700549 case HostSupportedNoCross:
550 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700551 case DeviceSupported:
552 return []OsClass{Device}
Dan Albert0981b5c2018-08-02 13:46:35 -0700553 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700554 var supported []OsClass
Dan Albert0981b5c2018-08-02 13:46:35 -0700555 if Bool(a.hostAndDeviceProperties.Host_supported) ||
556 (a.commonProperties.HostOrDeviceSupported == HostAndDeviceDefault &&
557 a.hostAndDeviceProperties.Host_supported == nil) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700558 supported = append(supported, Host, HostCross)
559 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700560 if a.hostAndDeviceProperties.Device_supported == nil ||
561 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700562 supported = append(supported, Device)
563 }
564 return supported
565 default:
566 return nil
567 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800568}
569
Colin Cross635c3b02016-05-18 15:37:25 -0700570func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800571 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
572 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700573 (a.hostAndDeviceProperties.Device_supported == nil ||
574 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800575}
576
Jiyong Parkc678ad32018-04-10 13:07:10 +0900577func (a *ModuleBase) Platform() bool {
Dario Frenifd05a742018-05-29 13:28:54 +0100578 return !a.DeviceSpecific() && !a.SocSpecific() && !a.ProductSpecific() && !a.ProductServicesSpecific()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900579}
580
581func (a *ModuleBase) DeviceSpecific() bool {
582 return Bool(a.commonProperties.Device_specific)
583}
584
585func (a *ModuleBase) SocSpecific() bool {
586 return Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
587}
588
589func (a *ModuleBase) ProductSpecific() bool {
590 return Bool(a.commonProperties.Product_specific)
591}
592
Dario Frenifd05a742018-05-29 13:28:54 +0100593func (a *ModuleBase) ProductServicesSpecific() bool {
Dario Freni95cf7672018-08-17 00:57:57 +0100594 return Bool(a.commonProperties.Product_services_specific)
Dario Frenifd05a742018-05-29 13:28:54 +0100595}
596
Colin Cross635c3b02016-05-18 15:37:25 -0700597func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800598 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800599 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800600 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800601 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800602}
603
Colin Crossce75d2c2016-10-06 16:12:58 -0700604func (a *ModuleBase) SkipInstall() {
605 a.commonProperties.SkipInstall = true
606}
607
Jiyong Park374510b2018-03-19 18:23:01 +0900608func (a *ModuleBase) ExportedToMake() bool {
609 return a.commonProperties.NamespaceExportedToMake
610}
611
Colin Cross635c3b02016-05-18 15:37:25 -0700612func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700613 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800614
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700615 result := Paths{}
Colin Cross6b753602018-06-21 13:03:07 -0700616 // TODO(ccross): we need to use WalkDeps and have some way to know which dependencies require installation
Colin Cross3f40fa42015-01-30 17:27:36 -0800617 ctx.VisitDepsDepthFirstIf(isFileInstaller,
618 func(m blueprint.Module) {
619 fileInstaller := m.(fileInstaller)
620 files := fileInstaller.filesToInstall()
621 result = append(result, files...)
622 })
623
624 return result
625}
626
Colin Cross635c3b02016-05-18 15:37:25 -0700627func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800628 return a.installFiles
629}
630
Colin Cross635c3b02016-05-18 15:37:25 -0700631func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800632 return p.noAddressSanitizer
633}
634
Colin Cross635c3b02016-05-18 15:37:25 -0700635func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800636 return false
637}
638
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700639func (p *ModuleBase) InstallInSanitizerDir() bool {
640 return false
641}
642
Jiyong Parkf9332f12018-02-01 00:54:12 +0900643func (p *ModuleBase) InstallInRecovery() bool {
644 return Bool(p.commonProperties.Recovery)
645}
646
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900647func (a *ModuleBase) Owner() string {
648 return String(a.commonProperties.Owner)
649}
650
Colin Cross0875c522017-11-28 17:34:01 -0800651func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700652 allInstalledFiles := Paths{}
653 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800654 ctx.VisitAllModuleVariants(func(module Module) {
655 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700656 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
657 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800658 })
659
Colin Cross0875c522017-11-28 17:34:01 -0800660 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700661
Jeff Gaston088e29e2017-11-29 16:47:17 -0800662 namespacePrefix := ctx.Namespace().(*Namespace).id
663 if namespacePrefix != "" {
664 namespacePrefix = namespacePrefix + "-"
665 }
666
Colin Cross3f40fa42015-01-30 17:27:36 -0800667 if len(allInstalledFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800668 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800669 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700670 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800671 Output: name,
672 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800673 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700674 })
675 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700676 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700677 }
678
679 if len(allCheckbuildFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800680 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800681 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700682 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800683 Output: name,
684 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700685 })
686 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700687 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700688 }
689
690 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800691 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800692 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800693 suffix = "-soong"
694 }
695
Jeff Gaston088e29e2017-11-29 16:47:17 -0800696 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800697 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700698 Rule: blueprint.Phony,
Jeff Gaston088e29e2017-11-29 16:47:17 -0800699 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700700 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800701 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700702
703 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800704 }
705}
706
Jiyong Park2db76922017-11-08 16:03:48 +0900707func determineModuleKind(a *ModuleBase, ctx blueprint.BaseModuleContext) moduleKind {
708 var socSpecific = Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
709 var deviceSpecific = Bool(a.commonProperties.Device_specific)
710 var productSpecific = Bool(a.commonProperties.Product_specific)
Dario Freni95cf7672018-08-17 00:57:57 +0100711 var productServicesSpecific = Bool(a.commonProperties.Product_services_specific)
Jiyong Park2db76922017-11-08 16:03:48 +0900712
Dario Frenifd05a742018-05-29 13:28:54 +0100713 msg := "conflicting value set here"
714 if socSpecific && deviceSpecific {
715 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
Jiyong Park2db76922017-11-08 16:03:48 +0900716 if Bool(a.commonProperties.Vendor) {
717 ctx.PropertyErrorf("vendor", msg)
718 }
719 if Bool(a.commonProperties.Proprietary) {
720 ctx.PropertyErrorf("proprietary", msg)
721 }
722 if Bool(a.commonProperties.Soc_specific) {
723 ctx.PropertyErrorf("soc_specific", msg)
724 }
725 }
726
Dario Frenifd05a742018-05-29 13:28:54 +0100727 if productSpecific && productServicesSpecific {
728 ctx.PropertyErrorf("product_specific", "a module cannot be specific to product and product_services at the same time.")
729 ctx.PropertyErrorf("product_services_specific", msg)
730 }
731
732 if (socSpecific || deviceSpecific) && (productSpecific || productServicesSpecific) {
733 if productSpecific {
734 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
735 } else {
736 ctx.PropertyErrorf("product_services_specific", "a module cannot be specific to SoC or device and product_services at the same time.")
737 }
738 if deviceSpecific {
739 ctx.PropertyErrorf("device_specific", msg)
740 } else {
741 if Bool(a.commonProperties.Vendor) {
742 ctx.PropertyErrorf("vendor", msg)
743 }
744 if Bool(a.commonProperties.Proprietary) {
745 ctx.PropertyErrorf("proprietary", msg)
746 }
747 if Bool(a.commonProperties.Soc_specific) {
748 ctx.PropertyErrorf("soc_specific", msg)
749 }
750 }
751 }
752
Jiyong Park2db76922017-11-08 16:03:48 +0900753 if productSpecific {
754 return productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100755 } else if productServicesSpecific {
756 return productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900757 } else if deviceSpecific {
758 return deviceSpecificModule
759 } else if socSpecific {
760 return socSpecificModule
761 } else {
762 return platformModule
763 }
764}
765
Colin Cross635c3b02016-05-18 15:37:25 -0700766func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700767 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700768 target: a.commonProperties.CompileTarget,
769 targetPrimary: a.commonProperties.CompilePrimary,
Colin Crossee0bc3b2018-10-02 22:01:37 -0700770 multiTargets: a.commonProperties.CompileMultiTargets,
Jiyong Park2db76922017-11-08 16:03:48 +0900771 kind: determineModuleKind(a, ctx),
Colin Cross8b74d172016-09-13 09:59:14 -0700772 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800773 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800774}
775
Colin Cross0875c522017-11-28 17:34:01 -0800776func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
777 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700778 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800779 ModuleContext: blueprintCtx,
780 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
781 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700782 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800783 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800784 }
785
Colin Cross67a5c132017-05-09 13:45:28 -0700786 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
787 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800788 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
789 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700790 }
Colin Cross0875c522017-11-28 17:34:01 -0800791 if !ctx.PrimaryArch() {
792 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700793 }
794
795 ctx.Variable(pctx, "moduleDesc", desc)
796
797 s := ""
798 if len(suffix) > 0 {
799 s = " [" + strings.Join(suffix, " ") + "]"
800 }
801 ctx.Variable(pctx, "moduleDescSuffix", s)
802
Dan Willemsen569edc52018-11-19 09:33:29 -0800803 // Some common property checks for properties that will be used later in androidmk.go
804 if a.commonProperties.Dist.Dest != nil {
805 _, err := validateSafePath(*a.commonProperties.Dist.Dest)
806 if err != nil {
807 ctx.PropertyErrorf("dist.dest", "%s", err.Error())
808 }
809 }
810 if a.commonProperties.Dist.Dir != nil {
811 _, err := validateSafePath(*a.commonProperties.Dist.Dir)
812 if err != nil {
813 ctx.PropertyErrorf("dist.dir", "%s", err.Error())
814 }
815 }
816 if a.commonProperties.Dist.Suffix != nil {
817 if strings.Contains(*a.commonProperties.Dist.Suffix, "/") {
818 ctx.PropertyErrorf("dist.suffix", "Suffix may not contain a '/' character.")
819 }
820 }
821
Colin Cross9b1d13d2016-09-19 15:18:11 -0700822 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800823 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700824 if ctx.Failed() {
825 return
826 }
827
Colin Cross0875c522017-11-28 17:34:01 -0800828 a.installFiles = append(a.installFiles, ctx.installFiles...)
829 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Jaewoong Jung62707f72018-11-16 13:26:43 -0800830
831 if a.commonProperties.Notice != nil {
832 // For filegroup-based notice file references.
833 a.noticeFile = ctx.ExpandSource(*a.commonProperties.Notice, "notice")
834 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800835 }
836
Colin Cross9b1d13d2016-09-19 15:18:11 -0700837 if a == ctx.FinalModule().(Module).base() {
838 a.generateModuleTarget(ctx)
839 if ctx.Failed() {
840 return
841 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800842 }
Colin Crosscec81712017-07-13 14:43:27 -0700843
Colin Cross0875c522017-11-28 17:34:01 -0800844 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800845}
846
Colin Crossf6566ed2015-03-24 11:13:38 -0700847type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700848 target Target
Colin Crossee0bc3b2018-10-02 22:01:37 -0700849 multiTargets []Target
Colin Cross8b74d172016-09-13 09:59:14 -0700850 targetPrimary bool
851 debug bool
Jiyong Park2db76922017-11-08 16:03:48 +0900852 kind moduleKind
Colin Cross8b74d172016-09-13 09:59:14 -0700853 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700854}
855
Colin Cross3f40fa42015-01-30 17:27:36 -0800856type androidModuleContext struct {
857 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700858 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700859 installDeps Paths
860 installFiles Paths
861 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800862 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700863 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700864
865 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700866 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800867}
868
Colin Cross67a5c132017-05-09 13:45:28 -0700869func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800870 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700871 Rule: ErrorRule,
872 Description: desc,
873 Outputs: outputs,
874 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800875 Args: map[string]string{
876 "error": err.Error(),
877 },
878 })
879 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800880}
881
Colin Crossaabf6792017-11-29 00:27:14 -0800882func (a *androidModuleContext) Config() Config {
883 return a.ModuleContext.Config().(Config)
884}
885
Colin Cross0875c522017-11-28 17:34:01 -0800886func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700887 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800888}
889
Colin Cross0875c522017-11-28 17:34:01 -0800890func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700891 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700892 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800893 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800894 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700895 Outputs: params.Outputs.Strings(),
896 ImplicitOutputs: params.ImplicitOutputs.Strings(),
897 Inputs: params.Inputs.Strings(),
898 Implicits: params.Implicits.Strings(),
899 OrderOnly: params.OrderOnly.Strings(),
900 Args: params.Args,
901 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700902 }
903
Colin Cross33bfb0a2016-11-21 17:23:08 -0800904 if params.Depfile != nil {
905 bparams.Depfile = params.Depfile.String()
906 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700907 if params.Output != nil {
908 bparams.Outputs = append(bparams.Outputs, params.Output.String())
909 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700910 if params.ImplicitOutput != nil {
911 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
912 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700913 if params.Input != nil {
914 bparams.Inputs = append(bparams.Inputs, params.Input.String())
915 }
916 if params.Implicit != nil {
917 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
918 }
919
Colin Crossfe4bc362018-09-12 10:02:13 -0700920 bparams.Outputs = proptools.NinjaEscape(bparams.Outputs)
921 bparams.ImplicitOutputs = proptools.NinjaEscape(bparams.ImplicitOutputs)
922 bparams.Inputs = proptools.NinjaEscape(bparams.Inputs)
923 bparams.Implicits = proptools.NinjaEscape(bparams.Implicits)
924 bparams.OrderOnly = proptools.NinjaEscape(bparams.OrderOnly)
925 bparams.Depfile = proptools.NinjaEscape([]string{bparams.Depfile})[0]
926
Colin Cross0875c522017-11-28 17:34:01 -0800927 return bparams
928}
929
930func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
931 a.ModuleContext.Variable(pctx.PackageContext, name, value)
932}
933
934func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
935 argNames ...string) blueprint.Rule {
936
937 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
938}
939
940func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
941 if a.config.captureBuild {
942 a.buildParams = append(a.buildParams, params)
943 }
944
945 bparams := convertBuildParams(params)
946
947 if bparams.Description != "" {
948 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
949 }
950
Colin Cross6ff51382015-12-17 16:39:19 -0800951 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700952 a.ninjaError(bparams.Description, bparams.Outputs,
953 fmt.Errorf("module %s missing dependencies: %s\n",
954 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800955 return
956 }
957
Colin Cross0875c522017-11-28 17:34:01 -0800958 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700959}
960
Colin Cross6ff51382015-12-17 16:39:19 -0800961func (a *androidModuleContext) GetMissingDependencies() []string {
962 return a.missingDeps
963}
964
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800965func (a *androidModuleContext) AddMissingDependencies(deps []string) {
966 if deps != nil {
967 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700968 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800969 }
970}
971
Colin Crossd11fcda2017-10-23 17:59:01 -0700972func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
973 aModule, _ := module.(Module)
974 if aModule == nil {
975 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
976 return nil
977 }
978
979 if !aModule.Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800980 if a.Config().AllowMissingDependencies() {
Colin Crossd11fcda2017-10-23 17:59:01 -0700981 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
982 } else {
983 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
984 }
985 return nil
986 }
987
988 return aModule
989}
990
Colin Cross35143d02017-11-16 00:11:20 -0800991func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
992 a.ModuleContext.VisitDirectDeps(visit)
993}
994
Colin Crossd11fcda2017-10-23 17:59:01 -0700995func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
996 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
997 if aModule := a.validateAndroidModule(module); aModule != nil {
998 visit(aModule)
999 }
1000 })
1001}
1002
Colin Crossee6143c2017-12-30 17:54:27 -08001003func (a *androidModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
1004 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
1005 if aModule := a.validateAndroidModule(module); aModule != nil {
1006 if a.ModuleContext.OtherModuleDependencyTag(aModule) == tag {
1007 visit(aModule)
1008 }
1009 }
1010 })
1011}
1012
Colin Crossd11fcda2017-10-23 17:59:01 -07001013func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
1014 a.ModuleContext.VisitDirectDepsIf(
1015 // pred
1016 func(module blueprint.Module) bool {
1017 if aModule := a.validateAndroidModule(module); aModule != nil {
1018 return pred(aModule)
1019 } else {
1020 return false
1021 }
1022 },
1023 // visit
1024 func(module blueprint.Module) {
1025 visit(module.(Module))
1026 })
1027}
1028
1029func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
1030 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
1031 if aModule := a.validateAndroidModule(module); aModule != nil {
1032 visit(aModule)
1033 }
1034 })
1035}
1036
1037func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
1038 a.ModuleContext.VisitDepsDepthFirstIf(
1039 // pred
1040 func(module blueprint.Module) bool {
1041 if aModule := a.validateAndroidModule(module); aModule != nil {
1042 return pred(aModule)
1043 } else {
1044 return false
1045 }
1046 },
1047 // visit
1048 func(module blueprint.Module) {
1049 visit(module.(Module))
1050 })
1051}
1052
1053func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
1054 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
1055 childAndroidModule := a.validateAndroidModule(child)
1056 parentAndroidModule := a.validateAndroidModule(parent)
1057 if childAndroidModule != nil && parentAndroidModule != nil {
1058 return visit(childAndroidModule, parentAndroidModule)
1059 } else {
1060 return false
1061 }
1062 })
1063}
1064
Colin Cross0875c522017-11-28 17:34:01 -08001065func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
1066 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
1067 visit(module.(Module))
1068 })
1069}
1070
1071func (a *androidModuleContext) PrimaryModule() Module {
1072 return a.ModuleContext.PrimaryModule().(Module)
1073}
1074
1075func (a *androidModuleContext) FinalModule() Module {
1076 return a.ModuleContext.FinalModule().(Module)
1077}
1078
Colin Crossa1ad8d12016-06-01 17:09:44 -07001079func (a *androidBaseContextImpl) Target() Target {
1080 return a.target
1081}
1082
Colin Cross8b74d172016-09-13 09:59:14 -07001083func (a *androidBaseContextImpl) TargetPrimary() bool {
1084 return a.targetPrimary
1085}
1086
Colin Crossee0bc3b2018-10-02 22:01:37 -07001087func (a *androidBaseContextImpl) MultiTargets() []Target {
1088 return a.multiTargets
1089}
1090
Colin Crossf6566ed2015-03-24 11:13:38 -07001091func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001092 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -08001093}
1094
Colin Crossa1ad8d12016-06-01 17:09:44 -07001095func (a *androidBaseContextImpl) Os() OsType {
1096 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -08001097}
1098
Colin Crossf6566ed2015-03-24 11:13:38 -07001099func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001100 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -07001101}
1102
1103func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001104 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -07001105}
1106
Colin Cross0af4b842015-04-30 16:36:18 -07001107func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001108 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -07001109}
1110
Colin Cross3edeee12017-04-04 12:59:48 -07001111func (a *androidBaseContextImpl) Windows() bool {
1112 return a.target.Os == Windows
1113}
1114
Colin Crossf6566ed2015-03-24 11:13:38 -07001115func (a *androidBaseContextImpl) Debug() bool {
1116 return a.debug
1117}
1118
Colin Cross1e7d3702016-08-24 15:25:47 -07001119func (a *androidBaseContextImpl) PrimaryArch() bool {
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001120 if len(a.config.Targets[a.target.Os]) <= 1 {
Colin Cross67a5c132017-05-09 13:45:28 -07001121 return true
1122 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001123 return a.target.Arch.ArchType == a.config.Targets[a.target.Os][0].Arch.ArchType
Colin Cross1e7d3702016-08-24 15:25:47 -07001124}
1125
Colin Cross1332b002015-04-07 17:11:30 -07001126func (a *androidBaseContextImpl) AConfig() Config {
1127 return a.config
1128}
1129
Colin Cross9272ade2016-08-17 15:24:12 -07001130func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
1131 return DeviceConfig{a.config.deviceConfig}
1132}
1133
Jiyong Park2db76922017-11-08 16:03:48 +09001134func (a *androidBaseContextImpl) Platform() bool {
1135 return a.kind == platformModule
1136}
1137
1138func (a *androidBaseContextImpl) DeviceSpecific() bool {
1139 return a.kind == deviceSpecificModule
1140}
1141
1142func (a *androidBaseContextImpl) SocSpecific() bool {
1143 return a.kind == socSpecificModule
1144}
1145
1146func (a *androidBaseContextImpl) ProductSpecific() bool {
1147 return a.kind == productSpecificModule
Dan Willemsen782a2d12015-12-21 14:55:28 -08001148}
1149
Dario Frenifd05a742018-05-29 13:28:54 +01001150func (a *androidBaseContextImpl) ProductServicesSpecific() bool {
1151 return a.kind == productServicesSpecificModule
1152}
1153
Jiyong Park5baac542018-08-28 09:55:37 +09001154// Makes this module a platform module, i.e. not specific to soc, device,
1155// product, or product_services.
1156func (a *ModuleBase) MakeAsPlatform() {
1157 a.commonProperties.Vendor = boolPtr(false)
1158 a.commonProperties.Proprietary = boolPtr(false)
1159 a.commonProperties.Soc_specific = boolPtr(false)
1160 a.commonProperties.Product_specific = boolPtr(false)
1161 a.commonProperties.Product_services_specific = boolPtr(false)
1162}
1163
Colin Cross8d8f8e22016-08-03 11:57:50 -07001164func (a *androidModuleContext) InstallInData() bool {
1165 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08001166}
1167
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001168func (a *androidModuleContext) InstallInSanitizerDir() bool {
1169 return a.module.InstallInSanitizerDir()
1170}
1171
Jiyong Parkf9332f12018-02-01 00:54:12 +09001172func (a *androidModuleContext) InstallInRecovery() bool {
1173 return a.module.InstallInRecovery()
1174}
1175
Colin Cross893d8162017-04-26 17:34:03 -07001176func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
1177 if a.module.base().commonProperties.SkipInstall {
1178 return true
1179 }
1180
Colin Cross3607f212018-05-07 15:28:05 -07001181 // We'll need a solution for choosing which of modules with the same name in different
1182 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
1183 // list of namespaces to install in a Soong-only build.
1184 if !a.module.base().commonProperties.NamespaceExportedToMake {
1185 return true
1186 }
1187
Colin Cross893d8162017-04-26 17:34:03 -07001188 if a.Device() {
Colin Cross6510f912017-11-29 00:27:14 -08001189 if a.Config().SkipDeviceInstall() {
Colin Cross893d8162017-04-26 17:34:03 -07001190 return true
1191 }
1192
Colin Cross6510f912017-11-29 00:27:14 -08001193 if a.Config().SkipMegaDeviceInstall(fullInstallPath.String()) {
Colin Cross893d8162017-04-26 17:34:03 -07001194 return true
1195 }
1196 }
1197
1198 return false
1199}
1200
Colin Cross5c517922017-08-31 12:29:17 -07001201func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -07001202 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -07001203 return a.installFile(installPath, name, srcPath, Cp, deps)
1204}
1205
1206func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
1207 deps ...Path) OutputPath {
1208 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
1209}
1210
1211func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
1212 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -07001213
Dan Willemsen782a2d12015-12-21 14:55:28 -08001214 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001215 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001216
Colin Cross893d8162017-04-26 17:34:03 -07001217 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001218
Dan Willemsen322acaf2016-01-12 23:07:05 -08001219 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -07001220
Colin Cross89562dc2016-10-03 17:47:19 -07001221 var implicitDeps, orderOnlyDeps Paths
1222
1223 if a.Host() {
1224 // Installed host modules might be used during the build, depend directly on their
1225 // dependencies so their timestamp is updated whenever their dependency is updated
1226 implicitDeps = deps
1227 } else {
1228 orderOnlyDeps = deps
1229 }
1230
Colin Crossae887032017-10-23 17:16:14 -07001231 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07001232 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07001233 Description: "install " + fullInstallPath.Base(),
1234 Output: fullInstallPath,
1235 Input: srcPath,
1236 Implicits: implicitDeps,
1237 OrderOnly: orderOnlyDeps,
Colin Cross6510f912017-11-29 00:27:14 -08001238 Default: !a.Config().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08001239 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001240
Dan Willemsen322acaf2016-01-12 23:07:05 -08001241 a.installFiles = append(a.installFiles, fullInstallPath)
1242 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001243 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -07001244 return fullInstallPath
1245}
1246
Colin Cross3854a602016-01-11 12:49:11 -08001247func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
1248 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001249 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08001250
Colin Cross893d8162017-04-26 17:34:03 -07001251 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001252
Colin Crossae887032017-10-23 17:16:14 -07001253 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001254 Rule: Symlink,
1255 Description: "install symlink " + fullInstallPath.Base(),
1256 Output: fullInstallPath,
1257 OrderOnly: Paths{srcPath},
Colin Cross6510f912017-11-29 00:27:14 -08001258 Default: !a.Config().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -08001259 Args: map[string]string{
1260 "fromPath": srcPath.String(),
1261 },
1262 })
Colin Cross3854a602016-01-11 12:49:11 -08001263
Colin Cross12fc4972016-01-11 12:49:11 -08001264 a.installFiles = append(a.installFiles, fullInstallPath)
1265 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1266 }
Colin Cross3854a602016-01-11 12:49:11 -08001267 return fullInstallPath
1268}
1269
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001270func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001271 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1272}
1273
Colin Cross3f40fa42015-01-30 17:27:36 -08001274type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001275 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001276}
1277
1278func isFileInstaller(m blueprint.Module) bool {
1279 _, ok := m.(fileInstaller)
1280 return ok
1281}
1282
1283func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -07001284 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -08001285 return ok
1286}
Colin Crossfce53272015-04-08 11:21:40 -07001287
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001288func findStringInSlice(str string, slice []string) int {
1289 for i, s := range slice {
1290 if s == str {
1291 return i
Colin Crossfce53272015-04-08 11:21:40 -07001292 }
1293 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001294 return -1
1295}
1296
Colin Cross068e0fe2016-12-13 15:23:47 -08001297func SrcIsModule(s string) string {
1298 if len(s) > 1 && s[0] == ':' {
1299 return s[1:]
1300 }
1301 return ""
1302}
1303
1304type sourceDependencyTag struct {
1305 blueprint.BaseDependencyTag
1306}
1307
1308var SourceDepTag sourceDependencyTag
1309
Colin Cross366938f2017-12-11 16:29:02 -08001310// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
1311// using ":module" syntax, if any.
Colin Cross068e0fe2016-12-13 15:23:47 -08001312func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1313 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001314 set := make(map[string]bool)
1315
Colin Cross068e0fe2016-12-13 15:23:47 -08001316 for _, s := range srcFiles {
1317 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001318 if _, found := set[m]; found {
1319 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1320 } else {
1321 set[m] = true
1322 deps = append(deps, m)
1323 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001324 }
1325 }
1326
1327 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1328}
1329
Colin Cross366938f2017-12-11 16:29:02 -08001330// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
1331// using ":module" syntax, if any.
1332func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
1333 if s != nil {
1334 if m := SrcIsModule(*s); m != "" {
1335 ctx.AddDependency(ctx.Module(), SourceDepTag, m)
1336 }
1337 }
1338}
1339
Colin Cross068e0fe2016-12-13 15:23:47 -08001340type SourceFileProducer interface {
1341 Srcs() Paths
1342}
1343
1344// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001345// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001346func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001347 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1348}
1349
Colin Cross366938f2017-12-11 16:29:02 -08001350// Returns a single path expanded from globs and modules referenced using ":module" syntax.
1351// ExtractSourceDeps must have already been called during the dependency resolution phase.
1352func (ctx *androidModuleContext) ExpandSource(srcFile, prop string) Path {
1353 srcFiles := ctx.ExpandSourcesSubDir([]string{srcFile}, nil, "")
1354 if len(srcFiles) == 1 {
1355 return srcFiles[0]
Jaewoong Jung62707f72018-11-16 13:26:43 -08001356 } else if len(srcFiles) == 0 {
1357 if ctx.Config().AllowMissingDependencies() {
1358 ctx.AddMissingDependencies([]string{srcFile})
1359 } else {
1360 ctx.PropertyErrorf(prop, "%s path %s does not exist", prop, srcFile)
1361 }
1362 return nil
Colin Cross366938f2017-12-11 16:29:02 -08001363 } else {
1364 ctx.PropertyErrorf(prop, "module providing %s must produce exactly one file", prop)
1365 return nil
1366 }
1367}
1368
Colin Cross2383f3b2018-02-06 14:40:13 -08001369// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
1370// the srcFile is non-nil.
1371// ExtractSourceDeps must have already been called during the dependency resolution phase.
1372func (ctx *androidModuleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
1373 if srcFile != nil {
1374 return OptionalPathForPath(ctx.ExpandSource(*srcFile, prop))
1375 }
1376 return OptionalPath{}
1377}
1378
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001379func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001380 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001381
Colin Cross461b4452018-02-23 09:22:42 -08001382 var expandedExcludes []string
1383 if excludes != nil {
1384 expandedExcludes = make([]string, 0, len(excludes))
1385 }
Nan Zhang27e284d2018-02-09 21:03:53 +00001386
1387 for _, e := range excludes {
1388 if m := SrcIsModule(e); m != "" {
1389 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
1390 if module == nil {
1391 // Error will have been handled by ExtractSourcesDeps
1392 continue
1393 }
1394 if srcProducer, ok := module.(SourceFileProducer); ok {
1395 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
1396 } else {
1397 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1398 }
1399 } else {
1400 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001401 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001402 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001403 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001404 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001405 if m := SrcIsModule(s); m != "" {
1406 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001407 if module == nil {
1408 // Error will have been handled by ExtractSourcesDeps
1409 continue
1410 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001411 if srcProducer, ok := module.(SourceFileProducer); ok {
Nan Zhang27e284d2018-02-09 21:03:53 +00001412 moduleSrcs := srcProducer.Srcs()
1413 for _, e := range expandedExcludes {
1414 for j, ms := range moduleSrcs {
1415 if ms.String() == e {
1416 moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
1417 }
1418 }
1419 }
1420 expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001421 } else {
1422 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1423 }
1424 } else if pathtools.IsGlob(s) {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001425 globbedSrcFiles := ctx.GlobFiles(filepath.Join(prefix, s), expandedExcludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001426 for i, s := range globbedSrcFiles {
1427 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001428 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001429 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001430 } else {
Nan Zhang27e284d2018-02-09 21:03:53 +00001431 p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1432 j := findStringInSlice(p.String(), expandedExcludes)
1433 if j == -1 {
1434 expandedSrcFiles = append(expandedSrcFiles, p)
1435 }
1436
Colin Cross8f101b42015-06-17 15:09:06 -07001437 }
1438 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001439 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001440}
1441
Nan Zhang6d34b302017-02-04 17:47:46 -08001442func (ctx *androidModuleContext) RequiredModuleNames() []string {
1443 return ctx.module.base().commonProperties.Required
1444}
1445
Colin Cross7f19f372016-11-01 11:10:25 -07001446func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1447 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001448 if err != nil {
1449 ctx.ModuleErrorf("glob: %s", err.Error())
1450 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001451 return pathsForModuleSrcFromFullPath(ctx, ret, true)
Colin Crossfce53272015-04-08 11:21:40 -07001452}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001453
Nan Zhang581fd212018-01-10 16:06:12 -08001454func (ctx *androidModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001455 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Nan Zhang581fd212018-01-10 16:06:12 -08001456 if err != nil {
1457 ctx.ModuleErrorf("glob: %s", err.Error())
1458 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001459 return pathsForModuleSrcFromFullPath(ctx, ret, false)
Nan Zhang581fd212018-01-10 16:06:12 -08001460}
1461
Colin Cross463a90e2015-06-17 14:20:06 -07001462func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001463 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001464}
1465
Colin Cross0875c522017-11-28 17:34:01 -08001466func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001467 return &buildTargetSingleton{}
1468}
1469
Colin Cross87d8b562017-04-25 10:01:55 -07001470func parentDir(dir string) string {
1471 dir, _ = filepath.Split(dir)
1472 return filepath.Clean(dir)
1473}
1474
Colin Cross1f8c52b2015-06-16 16:38:17 -07001475type buildTargetSingleton struct{}
1476
Colin Cross0875c522017-11-28 17:34:01 -08001477func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1478 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001479
Colin Cross0875c522017-11-28 17:34:01 -08001480 mmTarget := func(dir string) WritablePath {
1481 return PathForPhony(ctx,
1482 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001483 }
1484
Colin Cross0875c522017-11-28 17:34:01 -08001485 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001486
Colin Cross0875c522017-11-28 17:34:01 -08001487 ctx.VisitAllModules(func(module Module) {
1488 blueprintDir := module.base().blueprintDir
1489 installTarget := module.base().installTarget
1490 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001491
Colin Cross0875c522017-11-28 17:34:01 -08001492 if checkbuildTarget != nil {
1493 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1494 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1495 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001496
Colin Cross0875c522017-11-28 17:34:01 -08001497 if installTarget != nil {
1498 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001499 }
1500 })
1501
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001502 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001503 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001504 suffix = "-soong"
1505 }
1506
Colin Cross1f8c52b2015-06-16 16:38:17 -07001507 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001508 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001509 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001510 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001511 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001512 })
1513
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001514 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001515 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001516 return
1517 }
1518
Colin Cross0875c522017-11-28 17:34:01 -08001519 sortedKeys := func(m map[string]Paths) []string {
1520 s := make([]string, 0, len(m))
1521 for k := range m {
1522 s = append(s, k)
1523 }
1524 sort.Strings(s)
1525 return s
1526 }
1527
Colin Cross87d8b562017-04-25 10:01:55 -07001528 // Ensure ancestor directories are in modulesInDir
1529 dirs := sortedKeys(modulesInDir)
1530 for _, dir := range dirs {
1531 dir := parentDir(dir)
1532 for dir != "." && dir != "/" {
1533 if _, exists := modulesInDir[dir]; exists {
1534 break
1535 }
1536 modulesInDir[dir] = nil
1537 dir = parentDir(dir)
1538 }
1539 }
1540
1541 // Make directories build their direct subdirectories
1542 dirs = sortedKeys(modulesInDir)
1543 for _, dir := range dirs {
1544 p := parentDir(dir)
1545 if p != "." && p != "/" {
1546 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1547 }
1548 }
1549
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001550 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1551 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1552 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001553 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001554 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001555 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001556 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001557 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001558 // HACK: checkbuild should be an optional build, but force it
1559 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001560 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001561 })
1562 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001563
1564 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1565 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001566 ctx.VisitAllModules(func(module Module) {
1567 if module.Enabled() {
1568 os := module.Target().Os
1569 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001570 }
1571 })
1572
Colin Cross0875c522017-11-28 17:34:01 -08001573 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001574 for os, deps := range osDeps {
1575 var className string
1576
1577 switch os.Class {
1578 case Host:
1579 className = "host"
1580 case HostCross:
1581 className = "host-cross"
1582 case Device:
1583 className = "target"
1584 default:
1585 continue
1586 }
1587
Colin Cross0875c522017-11-28 17:34:01 -08001588 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001589 osClass[className] = append(osClass[className], name)
1590
Colin Cross0875c522017-11-28 17:34:01 -08001591 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001592 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001593 Output: name,
1594 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001595 })
1596 }
1597
1598 // Wrap those into host|host-cross|target phony rules
1599 osClasses := sortedKeys(osClass)
1600 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001601 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001602 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001603 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001604 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001605 })
1606 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001607}
Colin Crossd779da42015-12-17 18:00:23 -08001608
Colin Cross2465c3d2018-09-28 10:19:18 -07001609type ModulesByName struct {
1610 slice []blueprint.Module
Colin Crossd779da42015-12-17 18:00:23 -08001611 ctx interface {
1612 ModuleName(blueprint.Module) string
1613 ModuleSubDir(blueprint.Module) string
1614 }
1615}
1616
Colin Cross2465c3d2018-09-28 10:19:18 -07001617func (s ModulesByName) Len() int { return len(s.slice) }
1618func (s ModulesByName) Less(i, j int) bool {
Colin Crossd779da42015-12-17 18:00:23 -08001619 mi, mj := s.slice[i], s.slice[j]
1620 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1621
1622 if ni != nj {
1623 return ni < nj
1624 } else {
1625 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1626 }
1627}
Colin Cross2465c3d2018-09-28 10:19:18 -07001628func (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 -07001629
1630// Collect information for opening IDE project files in java/jdeps.go.
1631type IDEInfo interface {
1632 IDEInfo(ideInfo *IdeInfo)
1633 BaseModuleName() string
1634}
1635
1636// Extract the base module name from the Import name.
1637// Often the Import name has a prefix "prebuilt_".
1638// Remove the prefix explicitly if needed
1639// until we find a better solution to get the Import name.
1640type IDECustomizedModuleName interface {
1641 IDECustomizedModuleName() string
1642}
1643
1644type IdeInfo struct {
1645 Deps []string `json:"dependencies,omitempty"`
1646 Srcs []string `json:"srcs,omitempty"`
1647 Aidl_include_dirs []string `json:"aidl_include_dirs,omitempty"`
1648 Jarjar_rules []string `json:"jarjar_rules,omitempty"`
1649 Jars []string `json:"jars,omitempty"`
1650 Classes []string `json:"class,omitempty"`
1651 Installed_paths []string `json:"installed,omitempty"`
1652}