blob: aed16b3fdf2695dc35af9c3dbbceeb4b4e7f0587 [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
Doug Horn21b94272019-01-16 12:06:11 -080067 Fuchsia() bool
Colin Cross3edeee12017-04-04 12:59:48 -070068 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070069 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070070 PrimaryArch() bool
Jiyong Park2db76922017-11-08 16:03:48 +090071 Platform() bool
72 DeviceSpecific() bool
73 SocSpecific() bool
74 ProductSpecific() bool
Dario Frenifd05a742018-05-29 13:28:54 +010075 ProductServicesSpecific() bool
Colin Cross1332b002015-04-07 17:11:30 -070076 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070077 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070078}
79
Colin Cross635c3b02016-05-18 15:37:25 -070080type BaseContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080081 BaseModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070082 androidBaseContext
83}
84
Colin Crossaabf6792017-11-29 00:27:14 -080085// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
86// a Config instead of an interface{}.
87type BaseModuleContext interface {
88 ModuleName() string
89 ModuleDir() string
90 Config() Config
91
92 ContainsProperty(name string) bool
93 Errorf(pos scanner.Position, fmt string, args ...interface{})
94 ModuleErrorf(fmt string, args ...interface{})
95 PropertyErrorf(property, fmt string, args ...interface{})
96 Failed() bool
97
98 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
99 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
100 // builder whenever a file matching the pattern as added or removed, without rerunning if a
101 // file that does not match the pattern is added to a searched directory.
102 GlobWithDeps(pattern string, excludes []string) ([]string, error)
103
104 Fs() pathtools.FileSystem
105 AddNinjaFileDeps(deps ...string)
106}
107
Colin Cross635c3b02016-05-18 15:37:25 -0700108type ModuleContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -0700109 androidBaseContext
Colin Crossaabf6792017-11-29 00:27:14 -0800110 BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -0800111
Colin Crossae887032017-10-23 17:16:14 -0700112 // Deprecated: use ModuleContext.Build instead.
Colin Cross0875c522017-11-28 17:34:01 -0800113 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -0700114
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700115 ExpandSources(srcFiles, excludes []string) Paths
Colin Cross366938f2017-12-11 16:29:02 -0800116 ExpandSource(srcFile, prop string) Path
Colin Cross2383f3b2018-02-06 14:40:13 -0800117 ExpandOptionalSource(srcFile *string, prop string) OptionalPath
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800118 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -0700119 Glob(globPattern string, excludes []string) Paths
Nan Zhang581fd212018-01-10 16:06:12 -0800120 GlobFiles(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121
Colin Cross5c517922017-08-31 12:29:17 -0700122 InstallExecutable(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
123 InstallFile(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -0800124 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700125 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800126
127 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -0700128
Colin Cross8d8f8e22016-08-03 11:57:50 -0700129 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700130 InstallInSanitizerDir() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900131 InstallInRecovery() bool
Nan Zhang6d34b302017-02-04 17:47:46 -0800132
133 RequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -0700134
135 // android.ModuleContext methods
136 // These are duplicated instead of embedded so that can eventually be wrapped to take an
137 // android.Module instead of a blueprint.Module
138 OtherModuleName(m blueprint.Module) string
139 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
140 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
141
142 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
143 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
144
145 ModuleSubDir() string
146
Colin Cross35143d02017-11-16 00:11:20 -0800147 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700148 VisitDirectDeps(visit func(Module))
Colin Crossee6143c2017-12-30 17:54:27 -0800149 VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700150 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -0700151 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Crossd11fcda2017-10-23 17:59:01 -0700152 VisitDepsDepthFirst(visit func(Module))
Colin Cross6b753602018-06-21 13:03:07 -0700153 // Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
Colin Crossd11fcda2017-10-23 17:59:01 -0700154 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
155 WalkDeps(visit func(Module, Module) bool)
Colin Cross3f68a132017-10-23 17:10:29 -0700156
Colin Cross0875c522017-11-28 17:34:01 -0800157 Variable(pctx PackageContext, name, value string)
158 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700159 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
160 // and performs more verification.
Colin Cross0875c522017-11-28 17:34:01 -0800161 Build(pctx PackageContext, params BuildParams)
Colin Cross3f68a132017-10-23 17:10:29 -0700162
Colin Cross0875c522017-11-28 17:34:01 -0800163 PrimaryModule() Module
164 FinalModule() Module
165 VisitAllModuleVariants(visit func(Module))
Colin Cross3f68a132017-10-23 17:10:29 -0700166
167 GetMissingDependencies() []string
Jeff Gaston088e29e2017-11-29 16:47:17 -0800168 Namespace() blueprint.Namespace
Colin Cross3f40fa42015-01-30 17:27:36 -0800169}
170
Colin Cross635c3b02016-05-18 15:37:25 -0700171type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800172 blueprint.Module
173
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700174 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
175 // but GenerateAndroidBuildActions also has access to Android-specific information.
176 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700177 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700178
Colin Cross1e676be2016-10-12 14:38:15 -0700179 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800180
Colin Cross635c3b02016-05-18 15:37:25 -0700181 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800182 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700183 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800184 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700185 InstallInSanitizerDir() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900186 InstallInRecovery() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800187 SkipInstall()
Jiyong Park374510b2018-03-19 18:23:01 +0900188 ExportedToMake() bool
Colin Cross36242852017-06-23 15:06:31 -0700189
190 AddProperties(props ...interface{})
191 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700192
Colin Crossae887032017-10-23 17:16:14 -0700193 BuildParamsForTests() []BuildParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800194 VariablesForTests() map[string]string
Colin Cross3f40fa42015-01-30 17:27:36 -0800195}
196
Colin Crossfc754582016-05-17 16:34:16 -0700197type nameProperties struct {
198 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800199 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700200}
201
202type commonProperties struct {
Dan Willemsen0effe062015-11-30 16:06:01 -0800203 // emit build rules for this module
204 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800205
Colin Cross7d5136f2015-05-11 13:39:40 -0700206 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800207 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
208 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
209 // platform
Colin Cross7d716ba2017-11-01 10:38:29 -0700210 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700211
212 Target struct {
213 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700214 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700215 }
216 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700217 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700218 }
219 }
220
Colin Crossee0bc3b2018-10-02 22:01:37 -0700221 UseTargetVariants bool `blueprint:"mutated"`
222 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800223
Dan Willemsen782a2d12015-12-21 14:55:28 -0800224 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700225 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800226
Colin Cross55708f32017-03-20 13:23:34 -0700227 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700228 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700229
Jiyong Park2db76922017-11-08 16:03:48 +0900230 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
231 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
232 // Use `soc_specific` instead for better meaning.
Colin Cross7d716ba2017-11-01 10:38:29 -0700233 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700234
Jiyong Park2db76922017-11-08 16:03:48 +0900235 // whether this module is specific to an SoC (System-On-a-Chip). When set to true,
236 // it is installed into /vendor (or /system/vendor if vendor partition does not exist).
237 Soc_specific *bool
238
239 // whether this module is specific to a device, not only for SoC, but also for off-chip
240 // peripherals. When set to true, it is installed into /odm (or /vendor/odm if odm partition
241 // does not exist, or /system/vendor/odm if both odm and vendor partitions do not exist).
242 // This implies `soc_specific:true`.
243 Device_specific *bool
244
245 // whether this module is specific to a software configuration of a product (e.g. country,
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900246 // network operator, etc). When set to true, it is installed into /product (or
247 // /system/product if product partition does not exist).
Jiyong Park2db76922017-11-08 16:03:48 +0900248 Product_specific *bool
249
Dario Frenifd05a742018-05-29 13:28:54 +0100250 // whether this module provides services owned by the OS provider to the core platform. When set
Dario Freni95cf7672018-08-17 00:57:57 +0100251 // to true, it is installed into /product_services (or /system/product_services if
252 // product_services partition does not exist).
253 Product_services_specific *bool
Dario Frenifd05a742018-05-29 13:28:54 +0100254
Jiyong Parkf9332f12018-02-01 00:54:12 +0900255 // Whether this module is installed to recovery partition
256 Recovery *bool
257
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700258 // init.rc files to be installed if this module is installed
259 Init_rc []string
260
Steven Moreland57a23d22018-04-04 15:42:19 -0700261 // VINTF manifest fragments to be installed if this module is installed
262 Vintf_fragments []string
263
Chris Wolfe998306e2016-08-15 14:47:23 -0400264 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700265 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400266
Colin Cross5aac3622017-08-31 15:07:09 -0700267 // relative path to a file to include in the list of notices for the device
268 Notice *string
269
Dan Willemsen569edc52018-11-19 09:33:29 -0800270 Dist struct {
271 // copy the output of this module to the $DIST_DIR when `dist` is specified on the
272 // command line and any of these targets are also on the command line, or otherwise
273 // built
274 Targets []string `android:"arch_variant"`
275
276 // The name of the output artifact. This defaults to the basename of the output of
277 // the module.
278 Dest *string `android:"arch_variant"`
279
280 // The directory within the dist directory to store the artifact. Defaults to the
281 // top level directory ("").
282 Dir *string `android:"arch_variant"`
283
284 // A suffix to add to the artifact file name (before any extension).
285 Suffix *string `android:"arch_variant"`
286 } `android:"arch_variant"`
287
Colin Crossa1ad8d12016-06-01 17:09:44 -0700288 // Set by TargetMutator
Colin Crossee0bc3b2018-10-02 22:01:37 -0700289 CompileTarget Target `blueprint:"mutated"`
290 CompileMultiTargets []Target `blueprint:"mutated"`
291 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800292
293 // Set by InitAndroidModule
294 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700295 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700296
297 SkipInstall bool `blueprint:"mutated"`
Jeff Gaston088e29e2017-11-29 16:47:17 -0800298
299 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800300}
301
302type hostAndDeviceProperties struct {
Colin Cross4e81d702018-11-09 10:36:55 -0800303 // If set to true, build a variant of the module for the host. Defaults to false.
304 Host_supported *bool
305
306 // If set to true, build a variant of the module for the device. Defaults to true.
Colin Crossa4190c12016-07-12 13:11:25 -0700307 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800308}
309
Colin Crossc472d572015-03-17 15:06:21 -0700310type Multilib string
311
312const (
Colin Cross6b4a32d2017-12-05 13:42:45 -0800313 MultilibBoth Multilib = "both"
314 MultilibFirst Multilib = "first"
315 MultilibCommon Multilib = "common"
316 MultilibCommonFirst Multilib = "common_first"
317 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700318)
319
Colin Crossa1ad8d12016-06-01 17:09:44 -0700320type HostOrDeviceSupported int
321
322const (
323 _ HostOrDeviceSupported = iota
Dan Albert0981b5c2018-08-02 13:46:35 -0700324
325 // Host and HostCross are built by default. Device is not supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700326 HostSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700327
328 // Host is built by default. HostCross and Device are not supported.
Dan Albertc6345fb2016-10-20 01:36:11 -0700329 HostSupportedNoCross
Dan Albert0981b5c2018-08-02 13:46:35 -0700330
331 // Device is built by default. Host and HostCross are not supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700332 DeviceSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700333
334 // Device is built by default. Host and HostCross are supported.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700335 HostAndDeviceSupported
Dan Albert0981b5c2018-08-02 13:46:35 -0700336
337 // Host, HostCross, and Device are built by default.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700338 HostAndDeviceDefault
Dan Albert0981b5c2018-08-02 13:46:35 -0700339
340 // Nothing is supported. This is not exposed to the user, but used to mark a
341 // host only module as unsupported when the module type is not supported on
342 // the host OS. E.g. benchmarks are supported on Linux but not Darwin.
Dan Willemsen0b24c742016-10-04 15:13:37 -0700343 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700344)
345
Jiyong Park2db76922017-11-08 16:03:48 +0900346type moduleKind int
347
348const (
349 platformModule moduleKind = iota
350 deviceSpecificModule
351 socSpecificModule
352 productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100353 productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900354)
355
356func (k moduleKind) String() string {
357 switch k {
358 case platformModule:
359 return "platform"
360 case deviceSpecificModule:
361 return "device-specific"
362 case socSpecificModule:
363 return "soc-specific"
364 case productSpecificModule:
365 return "product-specific"
Dario Frenifd05a742018-05-29 13:28:54 +0100366 case productServicesSpecificModule:
367 return "productservices-specific"
Jiyong Park2db76922017-11-08 16:03:48 +0900368 default:
369 panic(fmt.Errorf("unknown module kind %d", k))
370 }
371}
372
Colin Cross36242852017-06-23 15:06:31 -0700373func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800374 base := m.base()
375 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700376
Colin Cross36242852017-06-23 15:06:31 -0700377 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700378 &base.nameProperties,
379 &base.commonProperties,
380 &base.variableProperties)
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700381 base.customizableProperties = m.GetProperties()
Colin Cross5049f022015-03-18 13:28:46 -0700382}
383
Colin Cross36242852017-06-23 15:06:31 -0700384func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
385 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700386
387 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800388 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700389 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700390 base.commonProperties.ArchSpecific = true
Colin Crossee0bc3b2018-10-02 22:01:37 -0700391 base.commonProperties.UseTargetVariants = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800392
Dan Willemsen218f6562015-07-08 18:13:11 -0700393 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700394 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700395 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800396 }
397
Colin Cross36242852017-06-23 15:06:31 -0700398 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800399}
400
Colin Crossee0bc3b2018-10-02 22:01:37 -0700401func InitAndroidMultiTargetsArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
402 InitAndroidArchModule(m, hod, defaultMultilib)
403 m.base().commonProperties.UseTargetVariants = false
404}
405
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800406// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800407// modules. It should be included as an anonymous field in every module
408// struct definition. InitAndroidModule should then be called from the module's
409// factory function, and the return values from InitAndroidModule should be
410// returned from the factory function.
411//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800412// The ModuleBase type is responsible for implementing the GenerateBuildActions
413// method to support the blueprint.Module interface. This method will then call
414// the module's GenerateAndroidBuildActions method once for each build variant
415// that is to be built. GenerateAndroidBuildActions is passed a
416// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800417// AndroidModuleContext exposes extra functionality specific to the Android build
418// system including details about the particular build variant that is to be
419// generated.
420//
421// For example:
422//
423// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800424// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800425// )
426//
427// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800428// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800429// properties struct {
430// MyProperty string
431// }
432// }
433//
Colin Cross36242852017-06-23 15:06:31 -0700434// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800435// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700436// m.AddProperties(&m.properties)
437// android.InitAndroidModule(m)
438// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800439// }
440//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800441// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800442// // Get the CPU architecture for the current build variant.
443// variantArch := ctx.Arch()
444//
445// // ...
446// }
Colin Cross635c3b02016-05-18 15:37:25 -0700447type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800448 // Putting the curiously recurring thing pointing to the thing that contains
449 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700450 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700451 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800452
Colin Crossfc754582016-05-17 16:34:16 -0700453 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800454 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700455 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800456 hostAndDeviceProperties hostAndDeviceProperties
457 generalProperties []interface{}
Colin Crossc17727d2018-10-24 12:42:09 -0700458 archProperties [][]interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700459 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800460
461 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700462 installFiles Paths
463 checkbuildFiles Paths
Jaewoong Jung62707f72018-11-16 13:26:43 -0800464 noticeFile Path
Colin Cross1f8c52b2015-06-16 16:38:17 -0700465
466 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
467 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800468 installTarget WritablePath
469 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700470 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700471
Colin Cross178a5092016-09-13 13:42:32 -0700472 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700473
474 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700475
476 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700477 buildParams []BuildParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800478 variables map[string]string
Colin Crossa9d8bee2018-10-02 13:59:46 -0700479
480 prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool
Colin Cross36242852017-06-23 15:06:31 -0700481}
482
483func (a *ModuleBase) AddProperties(props ...interface{}) {
484 a.registerProps = append(a.registerProps, props...)
485}
486
487func (a *ModuleBase) GetProperties() []interface{} {
488 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800489}
490
Colin Crossae887032017-10-23 17:16:14 -0700491func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700492 return a.buildParams
493}
494
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800495func (a *ModuleBase) VariablesForTests() map[string]string {
496 return a.variables
497}
498
Colin Crossa9d8bee2018-10-02 13:59:46 -0700499func (a *ModuleBase) Prefer32(prefer32 func(ctx BaseModuleContext, base *ModuleBase, class OsClass) bool) {
500 a.prefer32 = prefer32
501}
502
Colin Crossce75d2c2016-10-06 16:12:58 -0700503// Name returns the name of the module. It may be overridden by individual module types, for
504// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700505func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800506 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700507}
508
Colin Crossce75d2c2016-10-06 16:12:58 -0700509// BaseModuleName returns the name of the module as specified in the blueprints file.
510func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800511 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700512}
513
Colin Cross635c3b02016-05-18 15:37:25 -0700514func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800515 return a
516}
517
Colin Crossee0bc3b2018-10-02 22:01:37 -0700518func (a *ModuleBase) SetTarget(target Target, multiTargets []Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700519 a.commonProperties.CompileTarget = target
Colin Crossee0bc3b2018-10-02 22:01:37 -0700520 a.commonProperties.CompileMultiTargets = multiTargets
Colin Cross8b74d172016-09-13 09:59:14 -0700521 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700522}
523
Colin Crossa1ad8d12016-06-01 17:09:44 -0700524func (a *ModuleBase) Target() Target {
525 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800526}
527
Colin Cross8b74d172016-09-13 09:59:14 -0700528func (a *ModuleBase) TargetPrimary() bool {
529 return a.commonProperties.CompilePrimary
530}
531
Colin Crossee0bc3b2018-10-02 22:01:37 -0700532func (a *ModuleBase) MultiTargets() []Target {
533 return a.commonProperties.CompileMultiTargets
534}
535
Colin Crossa1ad8d12016-06-01 17:09:44 -0700536func (a *ModuleBase) Os() OsType {
537 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800538}
539
Colin Cross635c3b02016-05-18 15:37:25 -0700540func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700541 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800542}
543
Colin Cross635c3b02016-05-18 15:37:25 -0700544func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700545 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800546}
547
Dan Willemsen0b24c742016-10-04 15:13:37 -0700548func (a *ModuleBase) ArchSpecific() bool {
549 return a.commonProperties.ArchSpecific
550}
551
Colin Crossa1ad8d12016-06-01 17:09:44 -0700552func (a *ModuleBase) OsClassSupported() []OsClass {
553 switch a.commonProperties.HostOrDeviceSupported {
554 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700555 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700556 case HostSupportedNoCross:
557 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700558 case DeviceSupported:
559 return []OsClass{Device}
Dan Albert0981b5c2018-08-02 13:46:35 -0700560 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700561 var supported []OsClass
Dan Albert0981b5c2018-08-02 13:46:35 -0700562 if Bool(a.hostAndDeviceProperties.Host_supported) ||
563 (a.commonProperties.HostOrDeviceSupported == HostAndDeviceDefault &&
564 a.hostAndDeviceProperties.Host_supported == nil) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700565 supported = append(supported, Host, HostCross)
566 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700567 if a.hostAndDeviceProperties.Device_supported == nil ||
568 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700569 supported = append(supported, Device)
570 }
571 return supported
572 default:
573 return nil
574 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800575}
576
Colin Cross635c3b02016-05-18 15:37:25 -0700577func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800578 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
579 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700580 (a.hostAndDeviceProperties.Device_supported == nil ||
581 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800582}
583
Jiyong Parkc678ad32018-04-10 13:07:10 +0900584func (a *ModuleBase) Platform() bool {
Dario Frenifd05a742018-05-29 13:28:54 +0100585 return !a.DeviceSpecific() && !a.SocSpecific() && !a.ProductSpecific() && !a.ProductServicesSpecific()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900586}
587
588func (a *ModuleBase) DeviceSpecific() bool {
589 return Bool(a.commonProperties.Device_specific)
590}
591
592func (a *ModuleBase) SocSpecific() bool {
593 return Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
594}
595
596func (a *ModuleBase) ProductSpecific() bool {
597 return Bool(a.commonProperties.Product_specific)
598}
599
Dario Frenifd05a742018-05-29 13:28:54 +0100600func (a *ModuleBase) ProductServicesSpecific() bool {
Dario Freni95cf7672018-08-17 00:57:57 +0100601 return Bool(a.commonProperties.Product_services_specific)
Dario Frenifd05a742018-05-29 13:28:54 +0100602}
603
Colin Cross635c3b02016-05-18 15:37:25 -0700604func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800605 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800606 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800607 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800608 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800609}
610
Colin Crossce75d2c2016-10-06 16:12:58 -0700611func (a *ModuleBase) SkipInstall() {
612 a.commonProperties.SkipInstall = true
613}
614
Jiyong Park374510b2018-03-19 18:23:01 +0900615func (a *ModuleBase) ExportedToMake() bool {
616 return a.commonProperties.NamespaceExportedToMake
617}
618
Colin Cross635c3b02016-05-18 15:37:25 -0700619func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700620 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800621
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700622 result := Paths{}
Colin Cross6b753602018-06-21 13:03:07 -0700623 // TODO(ccross): we need to use WalkDeps and have some way to know which dependencies require installation
Colin Cross3f40fa42015-01-30 17:27:36 -0800624 ctx.VisitDepsDepthFirstIf(isFileInstaller,
625 func(m blueprint.Module) {
626 fileInstaller := m.(fileInstaller)
627 files := fileInstaller.filesToInstall()
628 result = append(result, files...)
629 })
630
631 return result
632}
633
Colin Cross635c3b02016-05-18 15:37:25 -0700634func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800635 return a.installFiles
636}
637
Colin Cross635c3b02016-05-18 15:37:25 -0700638func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800639 return p.noAddressSanitizer
640}
641
Colin Cross635c3b02016-05-18 15:37:25 -0700642func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800643 return false
644}
645
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700646func (p *ModuleBase) InstallInSanitizerDir() bool {
647 return false
648}
649
Jiyong Parkf9332f12018-02-01 00:54:12 +0900650func (p *ModuleBase) InstallInRecovery() bool {
651 return Bool(p.commonProperties.Recovery)
652}
653
Sundong Ahn4fd04bb2018-08-31 18:01:37 +0900654func (a *ModuleBase) Owner() string {
655 return String(a.commonProperties.Owner)
656}
657
Colin Cross0875c522017-11-28 17:34:01 -0800658func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700659 allInstalledFiles := Paths{}
660 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800661 ctx.VisitAllModuleVariants(func(module Module) {
662 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700663 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
664 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800665 })
666
Colin Cross0875c522017-11-28 17:34:01 -0800667 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700668
Jeff Gaston088e29e2017-11-29 16:47:17 -0800669 namespacePrefix := ctx.Namespace().(*Namespace).id
670 if namespacePrefix != "" {
671 namespacePrefix = namespacePrefix + "-"
672 }
673
Colin Cross3f40fa42015-01-30 17:27:36 -0800674 if len(allInstalledFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800675 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800676 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700677 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800678 Output: name,
679 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800680 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700681 })
682 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700683 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700684 }
685
686 if len(allCheckbuildFiles) > 0 {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800687 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800688 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700689 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800690 Output: name,
691 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700692 })
693 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700694 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700695 }
696
697 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800698 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800699 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800700 suffix = "-soong"
701 }
702
Jeff Gaston088e29e2017-11-29 16:47:17 -0800703 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800704 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700705 Rule: blueprint.Phony,
Jeff Gaston088e29e2017-11-29 16:47:17 -0800706 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700707 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800708 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700709
710 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800711 }
712}
713
Jiyong Park2db76922017-11-08 16:03:48 +0900714func determineModuleKind(a *ModuleBase, ctx blueprint.BaseModuleContext) moduleKind {
715 var socSpecific = Bool(a.commonProperties.Vendor) || Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Soc_specific)
716 var deviceSpecific = Bool(a.commonProperties.Device_specific)
717 var productSpecific = Bool(a.commonProperties.Product_specific)
Dario Freni95cf7672018-08-17 00:57:57 +0100718 var productServicesSpecific = Bool(a.commonProperties.Product_services_specific)
Jiyong Park2db76922017-11-08 16:03:48 +0900719
Dario Frenifd05a742018-05-29 13:28:54 +0100720 msg := "conflicting value set here"
721 if socSpecific && deviceSpecific {
722 ctx.PropertyErrorf("device_specific", "a module cannot be specific to SoC and device at the same time.")
Jiyong Park2db76922017-11-08 16:03:48 +0900723 if Bool(a.commonProperties.Vendor) {
724 ctx.PropertyErrorf("vendor", msg)
725 }
726 if Bool(a.commonProperties.Proprietary) {
727 ctx.PropertyErrorf("proprietary", msg)
728 }
729 if Bool(a.commonProperties.Soc_specific) {
730 ctx.PropertyErrorf("soc_specific", msg)
731 }
732 }
733
Dario Frenifd05a742018-05-29 13:28:54 +0100734 if productSpecific && productServicesSpecific {
735 ctx.PropertyErrorf("product_specific", "a module cannot be specific to product and product_services at the same time.")
736 ctx.PropertyErrorf("product_services_specific", msg)
737 }
738
739 if (socSpecific || deviceSpecific) && (productSpecific || productServicesSpecific) {
740 if productSpecific {
741 ctx.PropertyErrorf("product_specific", "a module cannot be specific to SoC or device and product at the same time.")
742 } else {
743 ctx.PropertyErrorf("product_services_specific", "a module cannot be specific to SoC or device and product_services at the same time.")
744 }
745 if deviceSpecific {
746 ctx.PropertyErrorf("device_specific", msg)
747 } else {
748 if Bool(a.commonProperties.Vendor) {
749 ctx.PropertyErrorf("vendor", msg)
750 }
751 if Bool(a.commonProperties.Proprietary) {
752 ctx.PropertyErrorf("proprietary", msg)
753 }
754 if Bool(a.commonProperties.Soc_specific) {
755 ctx.PropertyErrorf("soc_specific", msg)
756 }
757 }
758 }
759
Jiyong Park2db76922017-11-08 16:03:48 +0900760 if productSpecific {
761 return productSpecificModule
Dario Frenifd05a742018-05-29 13:28:54 +0100762 } else if productServicesSpecific {
763 return productServicesSpecificModule
Jiyong Park2db76922017-11-08 16:03:48 +0900764 } else if deviceSpecific {
765 return deviceSpecificModule
766 } else if socSpecific {
767 return socSpecificModule
768 } else {
769 return platformModule
770 }
771}
772
Colin Cross635c3b02016-05-18 15:37:25 -0700773func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700774 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700775 target: a.commonProperties.CompileTarget,
776 targetPrimary: a.commonProperties.CompilePrimary,
Colin Crossee0bc3b2018-10-02 22:01:37 -0700777 multiTargets: a.commonProperties.CompileMultiTargets,
Jiyong Park2db76922017-11-08 16:03:48 +0900778 kind: determineModuleKind(a, ctx),
Colin Cross8b74d172016-09-13 09:59:14 -0700779 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800780 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800781}
782
Colin Cross0875c522017-11-28 17:34:01 -0800783func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
784 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700785 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800786 ModuleContext: blueprintCtx,
787 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
788 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700789 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800790 missingDeps: blueprintCtx.GetMissingDependencies(),
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800791 variables: make(map[string]string),
Colin Cross3f40fa42015-01-30 17:27:36 -0800792 }
793
Colin Cross67a5c132017-05-09 13:45:28 -0700794 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
795 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800796 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
797 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700798 }
Colin Cross0875c522017-11-28 17:34:01 -0800799 if !ctx.PrimaryArch() {
800 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700801 }
802
803 ctx.Variable(pctx, "moduleDesc", desc)
804
805 s := ""
806 if len(suffix) > 0 {
807 s = " [" + strings.Join(suffix, " ") + "]"
808 }
809 ctx.Variable(pctx, "moduleDescSuffix", s)
810
Dan Willemsen569edc52018-11-19 09:33:29 -0800811 // Some common property checks for properties that will be used later in androidmk.go
812 if a.commonProperties.Dist.Dest != nil {
813 _, err := validateSafePath(*a.commonProperties.Dist.Dest)
814 if err != nil {
815 ctx.PropertyErrorf("dist.dest", "%s", err.Error())
816 }
817 }
818 if a.commonProperties.Dist.Dir != nil {
819 _, err := validateSafePath(*a.commonProperties.Dist.Dir)
820 if err != nil {
821 ctx.PropertyErrorf("dist.dir", "%s", err.Error())
822 }
823 }
824 if a.commonProperties.Dist.Suffix != nil {
825 if strings.Contains(*a.commonProperties.Dist.Suffix, "/") {
826 ctx.PropertyErrorf("dist.suffix", "Suffix may not contain a '/' character.")
827 }
828 }
829
Colin Cross9b1d13d2016-09-19 15:18:11 -0700830 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800831 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700832 if ctx.Failed() {
833 return
834 }
835
Colin Cross0875c522017-11-28 17:34:01 -0800836 a.installFiles = append(a.installFiles, ctx.installFiles...)
837 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Jaewoong Jung62707f72018-11-16 13:26:43 -0800838
839 if a.commonProperties.Notice != nil {
840 // For filegroup-based notice file references.
841 a.noticeFile = ctx.ExpandSource(*a.commonProperties.Notice, "notice")
842 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800843 }
844
Colin Cross9b1d13d2016-09-19 15:18:11 -0700845 if a == ctx.FinalModule().(Module).base() {
846 a.generateModuleTarget(ctx)
847 if ctx.Failed() {
848 return
849 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800850 }
Colin Crosscec81712017-07-13 14:43:27 -0700851
Colin Cross0875c522017-11-28 17:34:01 -0800852 a.buildParams = ctx.buildParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800853 a.variables = ctx.variables
Colin Cross3f40fa42015-01-30 17:27:36 -0800854}
855
Colin Crossf6566ed2015-03-24 11:13:38 -0700856type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700857 target Target
Colin Crossee0bc3b2018-10-02 22:01:37 -0700858 multiTargets []Target
Colin Cross8b74d172016-09-13 09:59:14 -0700859 targetPrimary bool
860 debug bool
Jiyong Park2db76922017-11-08 16:03:48 +0900861 kind moduleKind
Colin Cross8b74d172016-09-13 09:59:14 -0700862 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700863}
864
Colin Cross3f40fa42015-01-30 17:27:36 -0800865type androidModuleContext struct {
866 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700867 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700868 installDeps Paths
869 installFiles Paths
870 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800871 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700872 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700873
874 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700875 buildParams []BuildParams
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800876 variables map[string]string
Colin Cross6ff51382015-12-17 16:39:19 -0800877}
878
Colin Cross67a5c132017-05-09 13:45:28 -0700879func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800880 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700881 Rule: ErrorRule,
882 Description: desc,
883 Outputs: outputs,
884 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800885 Args: map[string]string{
886 "error": err.Error(),
887 },
888 })
889 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800890}
891
Colin Crossaabf6792017-11-29 00:27:14 -0800892func (a *androidModuleContext) Config() Config {
893 return a.ModuleContext.Config().(Config)
894}
895
Colin Cross0875c522017-11-28 17:34:01 -0800896func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700897 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800898}
899
Colin Cross0875c522017-11-28 17:34:01 -0800900func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700901 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700902 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800903 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800904 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700905 Outputs: params.Outputs.Strings(),
906 ImplicitOutputs: params.ImplicitOutputs.Strings(),
907 Inputs: params.Inputs.Strings(),
908 Implicits: params.Implicits.Strings(),
909 OrderOnly: params.OrderOnly.Strings(),
910 Args: params.Args,
911 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700912 }
913
Colin Cross33bfb0a2016-11-21 17:23:08 -0800914 if params.Depfile != nil {
915 bparams.Depfile = params.Depfile.String()
916 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700917 if params.Output != nil {
918 bparams.Outputs = append(bparams.Outputs, params.Output.String())
919 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700920 if params.ImplicitOutput != nil {
921 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
922 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700923 if params.Input != nil {
924 bparams.Inputs = append(bparams.Inputs, params.Input.String())
925 }
926 if params.Implicit != nil {
927 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
928 }
929
Colin Crossfe4bc362018-09-12 10:02:13 -0700930 bparams.Outputs = proptools.NinjaEscape(bparams.Outputs)
931 bparams.ImplicitOutputs = proptools.NinjaEscape(bparams.ImplicitOutputs)
932 bparams.Inputs = proptools.NinjaEscape(bparams.Inputs)
933 bparams.Implicits = proptools.NinjaEscape(bparams.Implicits)
934 bparams.OrderOnly = proptools.NinjaEscape(bparams.OrderOnly)
935 bparams.Depfile = proptools.NinjaEscape([]string{bparams.Depfile})[0]
936
Colin Cross0875c522017-11-28 17:34:01 -0800937 return bparams
938}
939
940func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800941 if a.config.captureBuild {
942 a.variables[name] = value
943 }
944
Colin Cross0875c522017-11-28 17:34:01 -0800945 a.ModuleContext.Variable(pctx.PackageContext, name, value)
946}
947
948func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
949 argNames ...string) blueprint.Rule {
950
951 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
952}
953
954func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
955 if a.config.captureBuild {
956 a.buildParams = append(a.buildParams, params)
957 }
958
959 bparams := convertBuildParams(params)
960
961 if bparams.Description != "" {
962 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
963 }
964
Colin Cross6ff51382015-12-17 16:39:19 -0800965 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700966 a.ninjaError(bparams.Description, bparams.Outputs,
967 fmt.Errorf("module %s missing dependencies: %s\n",
968 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800969 return
970 }
971
Colin Cross0875c522017-11-28 17:34:01 -0800972 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700973}
974
Colin Cross6ff51382015-12-17 16:39:19 -0800975func (a *androidModuleContext) GetMissingDependencies() []string {
976 return a.missingDeps
977}
978
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800979func (a *androidModuleContext) AddMissingDependencies(deps []string) {
980 if deps != nil {
981 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700982 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800983 }
984}
985
Colin Crossd11fcda2017-10-23 17:59:01 -0700986func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
987 aModule, _ := module.(Module)
988 if aModule == nil {
989 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
990 return nil
991 }
992
993 if !aModule.Enabled() {
Colin Cross6510f912017-11-29 00:27:14 -0800994 if a.Config().AllowMissingDependencies() {
Colin Crossd11fcda2017-10-23 17:59:01 -0700995 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
996 } else {
997 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
998 }
999 return nil
1000 }
1001
1002 return aModule
1003}
1004
Colin Cross35143d02017-11-16 00:11:20 -08001005func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
1006 a.ModuleContext.VisitDirectDeps(visit)
1007}
1008
Colin Crossd11fcda2017-10-23 17:59:01 -07001009func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
1010 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
1011 if aModule := a.validateAndroidModule(module); aModule != nil {
1012 visit(aModule)
1013 }
1014 })
1015}
1016
Colin Crossee6143c2017-12-30 17:54:27 -08001017func (a *androidModuleContext) VisitDirectDepsWithTag(tag blueprint.DependencyTag, visit func(Module)) {
1018 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
1019 if aModule := a.validateAndroidModule(module); aModule != nil {
1020 if a.ModuleContext.OtherModuleDependencyTag(aModule) == tag {
1021 visit(aModule)
1022 }
1023 }
1024 })
1025}
1026
Colin Crossd11fcda2017-10-23 17:59:01 -07001027func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
1028 a.ModuleContext.VisitDirectDepsIf(
1029 // pred
1030 func(module blueprint.Module) bool {
1031 if aModule := a.validateAndroidModule(module); aModule != nil {
1032 return pred(aModule)
1033 } else {
1034 return false
1035 }
1036 },
1037 // visit
1038 func(module blueprint.Module) {
1039 visit(module.(Module))
1040 })
1041}
1042
1043func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
1044 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
1045 if aModule := a.validateAndroidModule(module); aModule != nil {
1046 visit(aModule)
1047 }
1048 })
1049}
1050
1051func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
1052 a.ModuleContext.VisitDepsDepthFirstIf(
1053 // pred
1054 func(module blueprint.Module) bool {
1055 if aModule := a.validateAndroidModule(module); aModule != nil {
1056 return pred(aModule)
1057 } else {
1058 return false
1059 }
1060 },
1061 // visit
1062 func(module blueprint.Module) {
1063 visit(module.(Module))
1064 })
1065}
1066
1067func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
1068 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
1069 childAndroidModule := a.validateAndroidModule(child)
1070 parentAndroidModule := a.validateAndroidModule(parent)
1071 if childAndroidModule != nil && parentAndroidModule != nil {
1072 return visit(childAndroidModule, parentAndroidModule)
1073 } else {
1074 return false
1075 }
1076 })
1077}
1078
Colin Cross0875c522017-11-28 17:34:01 -08001079func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
1080 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
1081 visit(module.(Module))
1082 })
1083}
1084
1085func (a *androidModuleContext) PrimaryModule() Module {
1086 return a.ModuleContext.PrimaryModule().(Module)
1087}
1088
1089func (a *androidModuleContext) FinalModule() Module {
1090 return a.ModuleContext.FinalModule().(Module)
1091}
1092
Colin Crossa1ad8d12016-06-01 17:09:44 -07001093func (a *androidBaseContextImpl) Target() Target {
1094 return a.target
1095}
1096
Colin Cross8b74d172016-09-13 09:59:14 -07001097func (a *androidBaseContextImpl) TargetPrimary() bool {
1098 return a.targetPrimary
1099}
1100
Colin Crossee0bc3b2018-10-02 22:01:37 -07001101func (a *androidBaseContextImpl) MultiTargets() []Target {
1102 return a.multiTargets
1103}
1104
Colin Crossf6566ed2015-03-24 11:13:38 -07001105func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001106 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -08001107}
1108
Colin Crossa1ad8d12016-06-01 17:09:44 -07001109func (a *androidBaseContextImpl) Os() OsType {
1110 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -08001111}
1112
Colin Crossf6566ed2015-03-24 11:13:38 -07001113func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001114 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -07001115}
1116
1117func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001118 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -07001119}
1120
Colin Cross0af4b842015-04-30 16:36:18 -07001121func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001122 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -07001123}
1124
Doug Horn21b94272019-01-16 12:06:11 -08001125func (a *androidBaseContextImpl) Fuchsia() bool {
1126 return a.target.Os == Fuchsia
1127}
1128
Colin Cross3edeee12017-04-04 12:59:48 -07001129func (a *androidBaseContextImpl) Windows() bool {
1130 return a.target.Os == Windows
1131}
1132
Colin Crossf6566ed2015-03-24 11:13:38 -07001133func (a *androidBaseContextImpl) Debug() bool {
1134 return a.debug
1135}
1136
Colin Cross1e7d3702016-08-24 15:25:47 -07001137func (a *androidBaseContextImpl) PrimaryArch() bool {
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001138 if len(a.config.Targets[a.target.Os]) <= 1 {
Colin Cross67a5c132017-05-09 13:45:28 -07001139 return true
1140 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001141 return a.target.Arch.ArchType == a.config.Targets[a.target.Os][0].Arch.ArchType
Colin Cross1e7d3702016-08-24 15:25:47 -07001142}
1143
Colin Cross1332b002015-04-07 17:11:30 -07001144func (a *androidBaseContextImpl) AConfig() Config {
1145 return a.config
1146}
1147
Colin Cross9272ade2016-08-17 15:24:12 -07001148func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
1149 return DeviceConfig{a.config.deviceConfig}
1150}
1151
Jiyong Park2db76922017-11-08 16:03:48 +09001152func (a *androidBaseContextImpl) Platform() bool {
1153 return a.kind == platformModule
1154}
1155
1156func (a *androidBaseContextImpl) DeviceSpecific() bool {
1157 return a.kind == deviceSpecificModule
1158}
1159
1160func (a *androidBaseContextImpl) SocSpecific() bool {
1161 return a.kind == socSpecificModule
1162}
1163
1164func (a *androidBaseContextImpl) ProductSpecific() bool {
1165 return a.kind == productSpecificModule
Dan Willemsen782a2d12015-12-21 14:55:28 -08001166}
1167
Dario Frenifd05a742018-05-29 13:28:54 +01001168func (a *androidBaseContextImpl) ProductServicesSpecific() bool {
1169 return a.kind == productServicesSpecificModule
1170}
1171
Jiyong Park5baac542018-08-28 09:55:37 +09001172// Makes this module a platform module, i.e. not specific to soc, device,
1173// product, or product_services.
1174func (a *ModuleBase) MakeAsPlatform() {
1175 a.commonProperties.Vendor = boolPtr(false)
1176 a.commonProperties.Proprietary = boolPtr(false)
1177 a.commonProperties.Soc_specific = boolPtr(false)
1178 a.commonProperties.Product_specific = boolPtr(false)
1179 a.commonProperties.Product_services_specific = boolPtr(false)
1180}
1181
Colin Cross8d8f8e22016-08-03 11:57:50 -07001182func (a *androidModuleContext) InstallInData() bool {
1183 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -08001184}
1185
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001186func (a *androidModuleContext) InstallInSanitizerDir() bool {
1187 return a.module.InstallInSanitizerDir()
1188}
1189
Jiyong Parkf9332f12018-02-01 00:54:12 +09001190func (a *androidModuleContext) InstallInRecovery() bool {
1191 return a.module.InstallInRecovery()
1192}
1193
Colin Cross893d8162017-04-26 17:34:03 -07001194func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
1195 if a.module.base().commonProperties.SkipInstall {
1196 return true
1197 }
1198
Colin Cross3607f212018-05-07 15:28:05 -07001199 // We'll need a solution for choosing which of modules with the same name in different
1200 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
1201 // list of namespaces to install in a Soong-only build.
1202 if !a.module.base().commonProperties.NamespaceExportedToMake {
1203 return true
1204 }
1205
Colin Cross893d8162017-04-26 17:34:03 -07001206 if a.Device() {
Colin Cross6510f912017-11-29 00:27:14 -08001207 if a.Config().SkipDeviceInstall() {
Colin Cross893d8162017-04-26 17:34:03 -07001208 return true
1209 }
1210
Colin Cross6510f912017-11-29 00:27:14 -08001211 if a.Config().SkipMegaDeviceInstall(fullInstallPath.String()) {
Colin Cross893d8162017-04-26 17:34:03 -07001212 return true
1213 }
1214 }
1215
1216 return false
1217}
1218
Colin Cross5c517922017-08-31 12:29:17 -07001219func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -07001220 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -07001221 return a.installFile(installPath, name, srcPath, Cp, deps)
1222}
1223
1224func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
1225 deps ...Path) OutputPath {
1226 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
1227}
1228
1229func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
1230 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -07001231
Dan Willemsen782a2d12015-12-21 14:55:28 -08001232 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001233 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001234
Colin Cross893d8162017-04-26 17:34:03 -07001235 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001236
Dan Willemsen322acaf2016-01-12 23:07:05 -08001237 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -07001238
Colin Cross89562dc2016-10-03 17:47:19 -07001239 var implicitDeps, orderOnlyDeps Paths
1240
1241 if a.Host() {
1242 // Installed host modules might be used during the build, depend directly on their
1243 // dependencies so their timestamp is updated whenever their dependency is updated
1244 implicitDeps = deps
1245 } else {
1246 orderOnlyDeps = deps
1247 }
1248
Colin Crossae887032017-10-23 17:16:14 -07001249 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -07001250 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -07001251 Description: "install " + fullInstallPath.Base(),
1252 Output: fullInstallPath,
1253 Input: srcPath,
1254 Implicits: implicitDeps,
1255 OrderOnly: orderOnlyDeps,
Colin Cross6510f912017-11-29 00:27:14 -08001256 Default: !a.Config().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -08001257 })
Colin Cross3f40fa42015-01-30 17:27:36 -08001258
Dan Willemsen322acaf2016-01-12 23:07:05 -08001259 a.installFiles = append(a.installFiles, fullInstallPath)
1260 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001261 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -07001262 return fullInstallPath
1263}
1264
Colin Cross3854a602016-01-11 12:49:11 -08001265func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
1266 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -07001267 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -08001268
Colin Cross893d8162017-04-26 17:34:03 -07001269 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -07001270
Colin Crossae887032017-10-23 17:16:14 -07001271 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -07001272 Rule: Symlink,
1273 Description: "install symlink " + fullInstallPath.Base(),
1274 Output: fullInstallPath,
1275 OrderOnly: Paths{srcPath},
Colin Cross6510f912017-11-29 00:27:14 -08001276 Default: !a.Config().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -08001277 Args: map[string]string{
1278 "fromPath": srcPath.String(),
1279 },
1280 })
Colin Cross3854a602016-01-11 12:49:11 -08001281
Colin Cross12fc4972016-01-11 12:49:11 -08001282 a.installFiles = append(a.installFiles, fullInstallPath)
1283 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1284 }
Colin Cross3854a602016-01-11 12:49:11 -08001285 return fullInstallPath
1286}
1287
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001288func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001289 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
1290}
1291
Colin Cross3f40fa42015-01-30 17:27:36 -08001292type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001293 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001294}
1295
1296func isFileInstaller(m blueprint.Module) bool {
1297 _, ok := m.(fileInstaller)
1298 return ok
1299}
1300
1301func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -07001302 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -08001303 return ok
1304}
Colin Crossfce53272015-04-08 11:21:40 -07001305
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001306func findStringInSlice(str string, slice []string) int {
1307 for i, s := range slice {
1308 if s == str {
1309 return i
Colin Crossfce53272015-04-08 11:21:40 -07001310 }
1311 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001312 return -1
1313}
1314
Colin Cross068e0fe2016-12-13 15:23:47 -08001315func SrcIsModule(s string) string {
1316 if len(s) > 1 && s[0] == ':' {
1317 return s[1:]
1318 }
1319 return ""
1320}
1321
1322type sourceDependencyTag struct {
1323 blueprint.BaseDependencyTag
1324}
1325
1326var SourceDepTag sourceDependencyTag
1327
Colin Cross366938f2017-12-11 16:29:02 -08001328// Adds necessary dependencies to satisfy filegroup or generated sources modules listed in srcFiles
1329// using ":module" syntax, if any.
Colin Cross068e0fe2016-12-13 15:23:47 -08001330func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1331 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001332 set := make(map[string]bool)
1333
Colin Cross068e0fe2016-12-13 15:23:47 -08001334 for _, s := range srcFiles {
1335 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001336 if _, found := set[m]; found {
1337 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1338 } else {
1339 set[m] = true
1340 deps = append(deps, m)
1341 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001342 }
1343 }
1344
1345 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1346}
1347
Colin Cross366938f2017-12-11 16:29:02 -08001348// Adds necessary dependencies to satisfy filegroup or generated sources modules specified in s
1349// using ":module" syntax, if any.
1350func ExtractSourceDeps(ctx BottomUpMutatorContext, s *string) {
1351 if s != nil {
1352 if m := SrcIsModule(*s); m != "" {
1353 ctx.AddDependency(ctx.Module(), SourceDepTag, m)
1354 }
1355 }
1356}
1357
Colin Cross068e0fe2016-12-13 15:23:47 -08001358type SourceFileProducer interface {
1359 Srcs() Paths
1360}
1361
1362// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001363// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001364func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001365 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1366}
1367
Colin Cross366938f2017-12-11 16:29:02 -08001368// Returns a single path expanded from globs and modules referenced using ":module" syntax.
1369// ExtractSourceDeps must have already been called during the dependency resolution phase.
1370func (ctx *androidModuleContext) ExpandSource(srcFile, prop string) Path {
1371 srcFiles := ctx.ExpandSourcesSubDir([]string{srcFile}, nil, "")
1372 if len(srcFiles) == 1 {
1373 return srcFiles[0]
Jaewoong Jung62707f72018-11-16 13:26:43 -08001374 } else if len(srcFiles) == 0 {
1375 if ctx.Config().AllowMissingDependencies() {
1376 ctx.AddMissingDependencies([]string{srcFile})
1377 } else {
1378 ctx.PropertyErrorf(prop, "%s path %s does not exist", prop, srcFile)
1379 }
1380 return nil
Colin Cross366938f2017-12-11 16:29:02 -08001381 } else {
1382 ctx.PropertyErrorf(prop, "module providing %s must produce exactly one file", prop)
1383 return nil
1384 }
1385}
1386
Colin Cross2383f3b2018-02-06 14:40:13 -08001387// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
1388// the srcFile is non-nil.
1389// ExtractSourceDeps must have already been called during the dependency resolution phase.
1390func (ctx *androidModuleContext) ExpandOptionalSource(srcFile *string, prop string) OptionalPath {
1391 if srcFile != nil {
1392 return OptionalPathForPath(ctx.ExpandSource(*srcFile, prop))
1393 }
1394 return OptionalPath{}
1395}
1396
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001397func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001398 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001399
Colin Cross461b4452018-02-23 09:22:42 -08001400 var expandedExcludes []string
1401 if excludes != nil {
1402 expandedExcludes = make([]string, 0, len(excludes))
1403 }
Nan Zhang27e284d2018-02-09 21:03:53 +00001404
1405 for _, e := range excludes {
1406 if m := SrcIsModule(e); m != "" {
1407 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
1408 if module == nil {
1409 // Error will have been handled by ExtractSourcesDeps
1410 continue
1411 }
1412 if srcProducer, ok := module.(SourceFileProducer); ok {
1413 expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...)
1414 } else {
1415 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1416 }
1417 } else {
1418 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001419 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001420 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001421 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001422 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001423 if m := SrcIsModule(s); m != "" {
1424 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001425 if module == nil {
1426 // Error will have been handled by ExtractSourcesDeps
1427 continue
1428 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001429 if srcProducer, ok := module.(SourceFileProducer); ok {
Nan Zhang27e284d2018-02-09 21:03:53 +00001430 moduleSrcs := srcProducer.Srcs()
1431 for _, e := range expandedExcludes {
1432 for j, ms := range moduleSrcs {
1433 if ms.String() == e {
1434 moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...)
1435 }
1436 }
1437 }
1438 expandedSrcFiles = append(expandedSrcFiles, moduleSrcs...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001439 } else {
1440 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1441 }
1442 } else if pathtools.IsGlob(s) {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001443 globbedSrcFiles := ctx.GlobFiles(filepath.Join(prefix, s), expandedExcludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001444 for i, s := range globbedSrcFiles {
1445 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001446 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001447 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001448 } else {
Nan Zhang27e284d2018-02-09 21:03:53 +00001449 p := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1450 j := findStringInSlice(p.String(), expandedExcludes)
1451 if j == -1 {
1452 expandedSrcFiles = append(expandedSrcFiles, p)
1453 }
1454
Colin Cross8f101b42015-06-17 15:09:06 -07001455 }
1456 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001457 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001458}
1459
Nan Zhang6d34b302017-02-04 17:47:46 -08001460func (ctx *androidModuleContext) RequiredModuleNames() []string {
1461 return ctx.module.base().commonProperties.Required
1462}
1463
Colin Cross7f19f372016-11-01 11:10:25 -07001464func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1465 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001466 if err != nil {
1467 ctx.ModuleErrorf("glob: %s", err.Error())
1468 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001469 return pathsForModuleSrcFromFullPath(ctx, ret, true)
Colin Crossfce53272015-04-08 11:21:40 -07001470}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001471
Nan Zhang581fd212018-01-10 16:06:12 -08001472func (ctx *androidModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -08001473 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Nan Zhang581fd212018-01-10 16:06:12 -08001474 if err != nil {
1475 ctx.ModuleErrorf("glob: %s", err.Error())
1476 }
Dan Willemsen540a78c2018-02-26 21:50:08 -08001477 return pathsForModuleSrcFromFullPath(ctx, ret, false)
Nan Zhang581fd212018-01-10 16:06:12 -08001478}
1479
Colin Cross463a90e2015-06-17 14:20:06 -07001480func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001481 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001482}
1483
Colin Cross0875c522017-11-28 17:34:01 -08001484func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001485 return &buildTargetSingleton{}
1486}
1487
Colin Cross87d8b562017-04-25 10:01:55 -07001488func parentDir(dir string) string {
1489 dir, _ = filepath.Split(dir)
1490 return filepath.Clean(dir)
1491}
1492
Colin Cross1f8c52b2015-06-16 16:38:17 -07001493type buildTargetSingleton struct{}
1494
Colin Cross0875c522017-11-28 17:34:01 -08001495func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1496 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001497
Colin Cross0875c522017-11-28 17:34:01 -08001498 mmTarget := func(dir string) WritablePath {
1499 return PathForPhony(ctx,
1500 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001501 }
1502
Colin Cross0875c522017-11-28 17:34:01 -08001503 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001504
Colin Cross0875c522017-11-28 17:34:01 -08001505 ctx.VisitAllModules(func(module Module) {
1506 blueprintDir := module.base().blueprintDir
1507 installTarget := module.base().installTarget
1508 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001509
Colin Cross0875c522017-11-28 17:34:01 -08001510 if checkbuildTarget != nil {
1511 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1512 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1513 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001514
Colin Cross0875c522017-11-28 17:34:01 -08001515 if installTarget != nil {
1516 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001517 }
1518 })
1519
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001520 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001521 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001522 suffix = "-soong"
1523 }
1524
Colin Cross1f8c52b2015-06-16 16:38:17 -07001525 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001526 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001527 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001528 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001529 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001530 })
1531
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001532 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001533 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001534 return
1535 }
1536
Colin Cross0875c522017-11-28 17:34:01 -08001537 sortedKeys := func(m map[string]Paths) []string {
1538 s := make([]string, 0, len(m))
1539 for k := range m {
1540 s = append(s, k)
1541 }
1542 sort.Strings(s)
1543 return s
1544 }
1545
Colin Cross87d8b562017-04-25 10:01:55 -07001546 // Ensure ancestor directories are in modulesInDir
1547 dirs := sortedKeys(modulesInDir)
1548 for _, dir := range dirs {
1549 dir := parentDir(dir)
1550 for dir != "." && dir != "/" {
1551 if _, exists := modulesInDir[dir]; exists {
1552 break
1553 }
1554 modulesInDir[dir] = nil
1555 dir = parentDir(dir)
1556 }
1557 }
1558
1559 // Make directories build their direct subdirectories
1560 dirs = sortedKeys(modulesInDir)
1561 for _, dir := range dirs {
1562 p := parentDir(dir)
1563 if p != "." && p != "/" {
1564 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1565 }
1566 }
1567
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001568 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1569 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1570 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001571 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001572 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001573 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001574 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001575 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001576 // HACK: checkbuild should be an optional build, but force it
1577 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001578 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001579 })
1580 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001581
1582 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1583 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001584 ctx.VisitAllModules(func(module Module) {
1585 if module.Enabled() {
1586 os := module.Target().Os
1587 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001588 }
1589 })
1590
Colin Cross0875c522017-11-28 17:34:01 -08001591 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001592 for os, deps := range osDeps {
1593 var className string
1594
1595 switch os.Class {
1596 case Host:
1597 className = "host"
1598 case HostCross:
1599 className = "host-cross"
1600 case Device:
1601 className = "target"
1602 default:
1603 continue
1604 }
1605
Colin Cross0875c522017-11-28 17:34:01 -08001606 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001607 osClass[className] = append(osClass[className], name)
1608
Colin Cross0875c522017-11-28 17:34:01 -08001609 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001610 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001611 Output: name,
1612 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001613 })
1614 }
1615
1616 // Wrap those into host|host-cross|target phony rules
1617 osClasses := sortedKeys(osClass)
1618 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001619 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001620 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001621 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001622 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001623 })
1624 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001625}
Colin Crossd779da42015-12-17 18:00:23 -08001626
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001627// Collect information for opening IDE project files in java/jdeps.go.
1628type IDEInfo interface {
1629 IDEInfo(ideInfo *IdeInfo)
1630 BaseModuleName() string
1631}
1632
1633// Extract the base module name from the Import name.
1634// Often the Import name has a prefix "prebuilt_".
1635// Remove the prefix explicitly if needed
1636// until we find a better solution to get the Import name.
1637type IDECustomizedModuleName interface {
1638 IDECustomizedModuleName() string
1639}
1640
1641type IdeInfo struct {
1642 Deps []string `json:"dependencies,omitempty"`
1643 Srcs []string `json:"srcs,omitempty"`
1644 Aidl_include_dirs []string `json:"aidl_include_dirs,omitempty"`
1645 Jarjar_rules []string `json:"jarjar_rules,omitempty"`
1646 Jars []string `json:"jars,omitempty"`
1647 Classes []string `json:"class,omitempty"`
1648 Installed_paths []string `json:"installed,omitempty"`
1649}