blob: 0eb48203e3cdc08a412a59a73d8eac8fa2e2db2f [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 Crossf6566ed2015-03-24 11:13:38 -070022
23 "github.com/google/blueprint"
Colin Cross7f19f372016-11-01 11:10:25 -070024 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080025)
26
27var (
28 DeviceSharedLibrary = "shared_library"
29 DeviceStaticLibrary = "static_library"
30 DeviceExecutable = "executable"
31 HostSharedLibrary = "host_shared_library"
32 HostStaticLibrary = "host_static_library"
33 HostExecutable = "host_executable"
34)
35
Colin Crossae887032017-10-23 17:16:14 -070036type BuildParams struct {
Dan Willemsen9f3c5742016-11-03 14:28:31 -070037 Rule blueprint.Rule
Colin Cross33bfb0a2016-11-21 17:23:08 -080038 Deps blueprint.Deps
39 Depfile WritablePath
Colin Cross67a5c132017-05-09 13:45:28 -070040 Description string
Dan Willemsen9f3c5742016-11-03 14:28:31 -070041 Output WritablePath
42 Outputs WritablePaths
43 ImplicitOutput WritablePath
44 ImplicitOutputs WritablePaths
45 Input Path
46 Inputs Paths
47 Implicit Path
48 Implicits Paths
49 OrderOnly Paths
50 Default bool
51 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070052}
53
Colin Crossae887032017-10-23 17:16:14 -070054type ModuleBuildParams BuildParams
55
Colin Crossf6566ed2015-03-24 11:13:38 -070056type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070057 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070058 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070059 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070060 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070061 Host() bool
62 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070063 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070064 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070065 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070066 PrimaryArch() bool
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070067 InstallOnVendorPartition() bool
Colin Cross1332b002015-04-07 17:11:30 -070068 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070069 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070070}
71
Colin Cross635c3b02016-05-18 15:37:25 -070072type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070073 blueprint.BaseModuleContext
74 androidBaseContext
75}
76
Colin Cross635c3b02016-05-18 15:37:25 -070077type ModuleContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070078 androidBaseContext
Colin Cross3f68a132017-10-23 17:10:29 -070079 blueprint.BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -080080
Colin Crossae887032017-10-23 17:16:14 -070081 // Deprecated: use ModuleContext.Build instead.
Colin Cross0875c522017-11-28 17:34:01 -080082 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070083
Dan Willemsen34cc69e2015-09-23 15:26:20 -070084 ExpandSources(srcFiles, excludes []string) Paths
Colin Crossfaeb7aa2017-02-01 14:12:44 -080085 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -070086 Glob(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070087
Colin Cross5c517922017-08-31 12:29:17 -070088 InstallExecutable(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
89 InstallFile(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080090 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070091 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080092
93 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070094
Colin Cross8d8f8e22016-08-03 11:57:50 -070095 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -070096 InstallInSanitizerDir() bool
Nan Zhang6d34b302017-02-04 17:47:46 -080097
98 RequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -070099
100 // android.ModuleContext methods
101 // These are duplicated instead of embedded so that can eventually be wrapped to take an
102 // android.Module instead of a blueprint.Module
103 OtherModuleName(m blueprint.Module) string
104 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
105 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
106
107 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
108 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
109
110 ModuleSubDir() string
111
Colin Cross35143d02017-11-16 00:11:20 -0800112 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700113 VisitDirectDeps(visit func(Module))
114 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
115 VisitDepsDepthFirst(visit func(Module))
116 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
117 WalkDeps(visit func(Module, Module) bool)
Colin Cross3f68a132017-10-23 17:10:29 -0700118
Colin Cross0875c522017-11-28 17:34:01 -0800119 Variable(pctx PackageContext, name, value string)
120 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700121 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
122 // and performs more verification.
Colin Cross0875c522017-11-28 17:34:01 -0800123 Build(pctx PackageContext, params BuildParams)
Colin Cross3f68a132017-10-23 17:10:29 -0700124
Colin Cross0875c522017-11-28 17:34:01 -0800125 PrimaryModule() Module
126 FinalModule() Module
127 VisitAllModuleVariants(visit func(Module))
Colin Cross3f68a132017-10-23 17:10:29 -0700128
129 GetMissingDependencies() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800130}
131
Colin Cross635c3b02016-05-18 15:37:25 -0700132type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800133 blueprint.Module
134
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700135 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
136 // but GenerateAndroidBuildActions also has access to Android-specific information.
137 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700138 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700139
Colin Cross1e676be2016-10-12 14:38:15 -0700140 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800141
Colin Cross635c3b02016-05-18 15:37:25 -0700142 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800143 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700144 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800145 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700146 InstallInSanitizerDir() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800147 SkipInstall()
Colin Cross36242852017-06-23 15:06:31 -0700148
149 AddProperties(props ...interface{})
150 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700151
Colin Crossae887032017-10-23 17:16:14 -0700152 BuildParamsForTests() []BuildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800153}
154
Colin Crossfc754582016-05-17 16:34:16 -0700155type nameProperties struct {
156 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800157 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700158}
159
160type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700161 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800162
Dan Willemsen0effe062015-11-30 16:06:01 -0800163 // emit build rules for this module
164 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800165
Colin Cross7d5136f2015-05-11 13:39:40 -0700166 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800167 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
168 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
169 // platform
Colin Cross7d716ba2017-11-01 10:38:29 -0700170 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700171
172 Target struct {
173 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700174 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700175 }
176 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700177 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700178 }
179 }
180
181 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800182
Dan Willemsen782a2d12015-12-21 14:55:28 -0800183 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700184 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800185
Colin Cross55708f32017-03-20 13:23:34 -0700186 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700187 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700188
Dan Willemsenaa118f92017-04-06 12:49:58 -0700189 // whether this module is device specific and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700190 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700191
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700192 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
193 // file
194 Logtags []string
195
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700196 // init.rc files to be installed if this module is installed
197 Init_rc []string
198
Chris Wolfe998306e2016-08-15 14:47:23 -0400199 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700200 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400201
Colin Cross5aac3622017-08-31 15:07:09 -0700202 // relative path to a file to include in the list of notices for the device
203 Notice *string
204
Colin Crossa1ad8d12016-06-01 17:09:44 -0700205 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700206 CompileTarget Target `blueprint:"mutated"`
207 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800208
209 // Set by InitAndroidModule
210 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700211 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700212
213 SkipInstall bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800214}
215
216type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700217 Host_supported *bool
218 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800219}
220
Colin Crossc472d572015-03-17 15:06:21 -0700221type Multilib string
222
223const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700224 MultilibBoth Multilib = "both"
225 MultilibFirst Multilib = "first"
226 MultilibCommon Multilib = "common"
227 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700228)
229
Colin Crossa1ad8d12016-06-01 17:09:44 -0700230type HostOrDeviceSupported int
231
232const (
233 _ HostOrDeviceSupported = iota
234 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700235 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700236 DeviceSupported
237 HostAndDeviceSupported
238 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700239 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700240)
241
Colin Cross36242852017-06-23 15:06:31 -0700242func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800243 base := m.base()
244 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700245
Colin Cross36242852017-06-23 15:06:31 -0700246 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700247 &base.nameProperties,
248 &base.commonProperties,
249 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700250}
251
Colin Cross36242852017-06-23 15:06:31 -0700252func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
253 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700254
255 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800256 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700257 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700258 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800259
Dan Willemsen218f6562015-07-08 18:13:11 -0700260 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700261 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700262 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800263 }
264
Colin Cross36242852017-06-23 15:06:31 -0700265 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800266}
267
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800268// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800269// modules. It should be included as an anonymous field in every module
270// struct definition. InitAndroidModule should then be called from the module's
271// factory function, and the return values from InitAndroidModule should be
272// returned from the factory function.
273//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800274// The ModuleBase type is responsible for implementing the GenerateBuildActions
275// method to support the blueprint.Module interface. This method will then call
276// the module's GenerateAndroidBuildActions method once for each build variant
277// that is to be built. GenerateAndroidBuildActions is passed a
278// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800279// AndroidModuleContext exposes extra functionality specific to the Android build
280// system including details about the particular build variant that is to be
281// generated.
282//
283// For example:
284//
285// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800286// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800287// )
288//
289// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800290// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800291// properties struct {
292// MyProperty string
293// }
294// }
295//
Colin Cross36242852017-06-23 15:06:31 -0700296// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800297// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700298// m.AddProperties(&m.properties)
299// android.InitAndroidModule(m)
300// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800301// }
302//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800303// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800304// // Get the CPU architecture for the current build variant.
305// variantArch := ctx.Arch()
306//
307// // ...
308// }
Colin Cross635c3b02016-05-18 15:37:25 -0700309type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800310 // Putting the curiously recurring thing pointing to the thing that contains
311 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700312 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700313 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800314
Colin Crossfc754582016-05-17 16:34:16 -0700315 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800316 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700317 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800318 hostAndDeviceProperties hostAndDeviceProperties
319 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700320 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700321 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800322
323 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700324 installFiles Paths
325 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700326
327 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
328 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800329 installTarget WritablePath
330 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700331 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700332
Colin Cross178a5092016-09-13 13:42:32 -0700333 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700334
335 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700336
337 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700338 buildParams []BuildParams
Colin Cross36242852017-06-23 15:06:31 -0700339}
340
341func (a *ModuleBase) AddProperties(props ...interface{}) {
342 a.registerProps = append(a.registerProps, props...)
343}
344
345func (a *ModuleBase) GetProperties() []interface{} {
346 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800347}
348
Colin Crossae887032017-10-23 17:16:14 -0700349func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700350 return a.buildParams
351}
352
Colin Crossce75d2c2016-10-06 16:12:58 -0700353// Name returns the name of the module. It may be overridden by individual module types, for
354// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700355func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800356 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700357}
358
Colin Crossce75d2c2016-10-06 16:12:58 -0700359// BaseModuleName returns the name of the module as specified in the blueprints file.
360func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800361 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700362}
363
Colin Cross635c3b02016-05-18 15:37:25 -0700364func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800365 return a
366}
367
Colin Cross8b74d172016-09-13 09:59:14 -0700368func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700369 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700370 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700371}
372
Colin Crossa1ad8d12016-06-01 17:09:44 -0700373func (a *ModuleBase) Target() Target {
374 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800375}
376
Colin Cross8b74d172016-09-13 09:59:14 -0700377func (a *ModuleBase) TargetPrimary() bool {
378 return a.commonProperties.CompilePrimary
379}
380
Colin Crossa1ad8d12016-06-01 17:09:44 -0700381func (a *ModuleBase) Os() OsType {
382 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800383}
384
Colin Cross635c3b02016-05-18 15:37:25 -0700385func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700386 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800387}
388
Colin Cross635c3b02016-05-18 15:37:25 -0700389func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700390 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800391}
392
Dan Willemsen0b24c742016-10-04 15:13:37 -0700393func (a *ModuleBase) ArchSpecific() bool {
394 return a.commonProperties.ArchSpecific
395}
396
Colin Crossa1ad8d12016-06-01 17:09:44 -0700397func (a *ModuleBase) OsClassSupported() []OsClass {
398 switch a.commonProperties.HostOrDeviceSupported {
399 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700400 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700401 case HostSupportedNoCross:
402 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700403 case DeviceSupported:
404 return []OsClass{Device}
405 case HostAndDeviceSupported:
406 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700407 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700408 supported = append(supported, Host, HostCross)
409 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700410 if a.hostAndDeviceProperties.Device_supported == nil ||
411 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700412 supported = append(supported, Device)
413 }
414 return supported
415 default:
416 return nil
417 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800418}
419
Colin Cross635c3b02016-05-18 15:37:25 -0700420func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800421 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
422 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700423 (a.hostAndDeviceProperties.Device_supported == nil ||
424 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800425}
426
Colin Cross635c3b02016-05-18 15:37:25 -0700427func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800428 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800429 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800430 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800431 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800432}
433
Colin Crossce75d2c2016-10-06 16:12:58 -0700434func (a *ModuleBase) SkipInstall() {
435 a.commonProperties.SkipInstall = true
436}
437
Colin Cross635c3b02016-05-18 15:37:25 -0700438func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700439 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800440
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700441 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800442 ctx.VisitDepsDepthFirstIf(isFileInstaller,
443 func(m blueprint.Module) {
444 fileInstaller := m.(fileInstaller)
445 files := fileInstaller.filesToInstall()
446 result = append(result, files...)
447 })
448
449 return result
450}
451
Colin Cross635c3b02016-05-18 15:37:25 -0700452func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800453 return a.installFiles
454}
455
Colin Cross635c3b02016-05-18 15:37:25 -0700456func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800457 return p.noAddressSanitizer
458}
459
Colin Cross635c3b02016-05-18 15:37:25 -0700460func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800461 return false
462}
463
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700464func (p *ModuleBase) InstallInSanitizerDir() bool {
465 return false
466}
467
Colin Cross0875c522017-11-28 17:34:01 -0800468func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700469 allInstalledFiles := Paths{}
470 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800471 ctx.VisitAllModuleVariants(func(module Module) {
472 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700473 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
474 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800475 })
476
Colin Cross0875c522017-11-28 17:34:01 -0800477 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700478
Colin Cross3f40fa42015-01-30 17:27:36 -0800479 if len(allInstalledFiles) > 0 {
Colin Cross0875c522017-11-28 17:34:01 -0800480 name := PathForPhony(ctx, ctx.ModuleName()+"-install")
481 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700482 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800483 Output: name,
484 Implicits: allInstalledFiles,
485 Default: !ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700486 })
487 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700488 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700489 }
490
491 if len(allCheckbuildFiles) > 0 {
Colin Cross0875c522017-11-28 17:34:01 -0800492 name := PathForPhony(ctx, ctx.ModuleName()+"-checkbuild")
493 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700494 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800495 Output: name,
496 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700497 })
498 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700499 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700500 }
501
502 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800503 suffix := ""
504 if ctx.Config().(Config).EmbeddedInMake() {
505 suffix = "-soong"
506 }
507
Colin Cross0875c522017-11-28 17:34:01 -0800508 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700509 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800510 Output: PathForPhony(ctx, ctx.ModuleName()+suffix),
Colin Cross9454bfa2015-03-17 13:24:18 -0700511 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800512 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700513
514 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800515 }
516}
517
Colin Cross635c3b02016-05-18 15:37:25 -0700518func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700519 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700520 target: a.commonProperties.CompileTarget,
521 targetPrimary: a.commonProperties.CompilePrimary,
Colin Cross7d716ba2017-11-01 10:38:29 -0700522 vendor: Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Vendor),
Colin Cross8b74d172016-09-13 09:59:14 -0700523 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800524 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800525}
526
Colin Cross0875c522017-11-28 17:34:01 -0800527func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
528 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700529 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800530 ModuleContext: blueprintCtx,
531 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
532 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700533 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800534 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800535 }
536
Colin Cross67a5c132017-05-09 13:45:28 -0700537 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
538 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800539 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
540 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700541 }
Colin Cross0875c522017-11-28 17:34:01 -0800542 if !ctx.PrimaryArch() {
543 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700544 }
545
546 ctx.Variable(pctx, "moduleDesc", desc)
547
548 s := ""
549 if len(suffix) > 0 {
550 s = " [" + strings.Join(suffix, " ") + "]"
551 }
552 ctx.Variable(pctx, "moduleDescSuffix", s)
553
Colin Cross9b1d13d2016-09-19 15:18:11 -0700554 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800555 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700556 if ctx.Failed() {
557 return
558 }
559
Colin Cross0875c522017-11-28 17:34:01 -0800560 a.installFiles = append(a.installFiles, ctx.installFiles...)
561 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800562 }
563
Colin Cross9b1d13d2016-09-19 15:18:11 -0700564 if a == ctx.FinalModule().(Module).base() {
565 a.generateModuleTarget(ctx)
566 if ctx.Failed() {
567 return
568 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800569 }
Colin Crosscec81712017-07-13 14:43:27 -0700570
Colin Cross0875c522017-11-28 17:34:01 -0800571 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800572}
573
Colin Crossf6566ed2015-03-24 11:13:38 -0700574type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700575 target Target
576 targetPrimary bool
577 debug bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700578 vendor bool
Colin Cross8b74d172016-09-13 09:59:14 -0700579 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700580}
581
Colin Cross3f40fa42015-01-30 17:27:36 -0800582type androidModuleContext struct {
583 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700584 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700585 installDeps Paths
586 installFiles Paths
587 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800588 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700589 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700590
591 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700592 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800593}
594
Colin Cross67a5c132017-05-09 13:45:28 -0700595func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800596 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700597 Rule: ErrorRule,
598 Description: desc,
599 Outputs: outputs,
600 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800601 Args: map[string]string{
602 "error": err.Error(),
603 },
604 })
605 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800606}
607
Colin Cross0875c522017-11-28 17:34:01 -0800608func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700609 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800610}
611
Colin Cross0875c522017-11-28 17:34:01 -0800612func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700613 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700614 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800615 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800616 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700617 Outputs: params.Outputs.Strings(),
618 ImplicitOutputs: params.ImplicitOutputs.Strings(),
619 Inputs: params.Inputs.Strings(),
620 Implicits: params.Implicits.Strings(),
621 OrderOnly: params.OrderOnly.Strings(),
622 Args: params.Args,
623 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700624 }
625
Colin Cross33bfb0a2016-11-21 17:23:08 -0800626 if params.Depfile != nil {
627 bparams.Depfile = params.Depfile.String()
628 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700629 if params.Output != nil {
630 bparams.Outputs = append(bparams.Outputs, params.Output.String())
631 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700632 if params.ImplicitOutput != nil {
633 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
634 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700635 if params.Input != nil {
636 bparams.Inputs = append(bparams.Inputs, params.Input.String())
637 }
638 if params.Implicit != nil {
639 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
640 }
641
Colin Cross0875c522017-11-28 17:34:01 -0800642 return bparams
643}
644
645func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
646 a.ModuleContext.Variable(pctx.PackageContext, name, value)
647}
648
649func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
650 argNames ...string) blueprint.Rule {
651
652 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
653}
654
655func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
656 if a.config.captureBuild {
657 a.buildParams = append(a.buildParams, params)
658 }
659
660 bparams := convertBuildParams(params)
661
662 if bparams.Description != "" {
663 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
664 }
665
Colin Cross6ff51382015-12-17 16:39:19 -0800666 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700667 a.ninjaError(bparams.Description, bparams.Outputs,
668 fmt.Errorf("module %s missing dependencies: %s\n",
669 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800670 return
671 }
672
Colin Cross0875c522017-11-28 17:34:01 -0800673 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700674}
675
Colin Cross6ff51382015-12-17 16:39:19 -0800676func (a *androidModuleContext) GetMissingDependencies() []string {
677 return a.missingDeps
678}
679
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800680func (a *androidModuleContext) AddMissingDependencies(deps []string) {
681 if deps != nil {
682 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700683 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800684 }
685}
686
Colin Crossd11fcda2017-10-23 17:59:01 -0700687func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
688 aModule, _ := module.(Module)
689 if aModule == nil {
690 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
691 return nil
692 }
693
694 if !aModule.Enabled() {
695 if a.AConfig().AllowMissingDependencies() {
696 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
697 } else {
698 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
699 }
700 return nil
701 }
702
703 return aModule
704}
705
Colin Cross35143d02017-11-16 00:11:20 -0800706func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
707 a.ModuleContext.VisitDirectDeps(visit)
708}
709
Colin Crossd11fcda2017-10-23 17:59:01 -0700710func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
711 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
712 if aModule := a.validateAndroidModule(module); aModule != nil {
713 visit(aModule)
714 }
715 })
716}
717
718func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
719 a.ModuleContext.VisitDirectDepsIf(
720 // pred
721 func(module blueprint.Module) bool {
722 if aModule := a.validateAndroidModule(module); aModule != nil {
723 return pred(aModule)
724 } else {
725 return false
726 }
727 },
728 // visit
729 func(module blueprint.Module) {
730 visit(module.(Module))
731 })
732}
733
734func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
735 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
736 if aModule := a.validateAndroidModule(module); aModule != nil {
737 visit(aModule)
738 }
739 })
740}
741
742func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
743 a.ModuleContext.VisitDepsDepthFirstIf(
744 // pred
745 func(module blueprint.Module) bool {
746 if aModule := a.validateAndroidModule(module); aModule != nil {
747 return pred(aModule)
748 } else {
749 return false
750 }
751 },
752 // visit
753 func(module blueprint.Module) {
754 visit(module.(Module))
755 })
756}
757
758func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
759 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
760 childAndroidModule := a.validateAndroidModule(child)
761 parentAndroidModule := a.validateAndroidModule(parent)
762 if childAndroidModule != nil && parentAndroidModule != nil {
763 return visit(childAndroidModule, parentAndroidModule)
764 } else {
765 return false
766 }
767 })
768}
769
Colin Cross0875c522017-11-28 17:34:01 -0800770func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
771 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
772 visit(module.(Module))
773 })
774}
775
776func (a *androidModuleContext) PrimaryModule() Module {
777 return a.ModuleContext.PrimaryModule().(Module)
778}
779
780func (a *androidModuleContext) FinalModule() Module {
781 return a.ModuleContext.FinalModule().(Module)
782}
783
Colin Crossa1ad8d12016-06-01 17:09:44 -0700784func (a *androidBaseContextImpl) Target() Target {
785 return a.target
786}
787
Colin Cross8b74d172016-09-13 09:59:14 -0700788func (a *androidBaseContextImpl) TargetPrimary() bool {
789 return a.targetPrimary
790}
791
Colin Crossf6566ed2015-03-24 11:13:38 -0700792func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700793 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800794}
795
Colin Crossa1ad8d12016-06-01 17:09:44 -0700796func (a *androidBaseContextImpl) Os() OsType {
797 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800798}
799
Colin Crossf6566ed2015-03-24 11:13:38 -0700800func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700801 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700802}
803
804func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700805 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700806}
807
Colin Cross0af4b842015-04-30 16:36:18 -0700808func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700809 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700810}
811
Colin Cross3edeee12017-04-04 12:59:48 -0700812func (a *androidBaseContextImpl) Windows() bool {
813 return a.target.Os == Windows
814}
815
Colin Crossf6566ed2015-03-24 11:13:38 -0700816func (a *androidBaseContextImpl) Debug() bool {
817 return a.debug
818}
819
Colin Cross1e7d3702016-08-24 15:25:47 -0700820func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700821 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
822 return true
823 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700824 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
825}
826
Colin Cross1332b002015-04-07 17:11:30 -0700827func (a *androidBaseContextImpl) AConfig() Config {
828 return a.config
829}
830
Colin Cross9272ade2016-08-17 15:24:12 -0700831func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
832 return DeviceConfig{a.config.deviceConfig}
833}
834
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700835func (a *androidBaseContextImpl) InstallOnVendorPartition() bool {
Dan Willemsenaa118f92017-04-06 12:49:58 -0700836 return a.vendor
Dan Willemsen782a2d12015-12-21 14:55:28 -0800837}
838
Colin Cross8d8f8e22016-08-03 11:57:50 -0700839func (a *androidModuleContext) InstallInData() bool {
840 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800841}
842
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700843func (a *androidModuleContext) InstallInSanitizerDir() bool {
844 return a.module.InstallInSanitizerDir()
845}
846
Colin Cross893d8162017-04-26 17:34:03 -0700847func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
848 if a.module.base().commonProperties.SkipInstall {
849 return true
850 }
851
852 if a.Device() {
853 if a.AConfig().SkipDeviceInstall() {
854 return true
855 }
856
857 if a.AConfig().SkipMegaDeviceInstall(fullInstallPath.String()) {
858 return true
859 }
860 }
861
862 return false
863}
864
Colin Cross5c517922017-08-31 12:29:17 -0700865func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700866 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -0700867 return a.installFile(installPath, name, srcPath, Cp, deps)
868}
869
870func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
871 deps ...Path) OutputPath {
872 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
873}
874
875func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
876 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700877
Dan Willemsen782a2d12015-12-21 14:55:28 -0800878 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700879 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800880
Colin Cross893d8162017-04-26 17:34:03 -0700881 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700882
Dan Willemsen322acaf2016-01-12 23:07:05 -0800883 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700884
Colin Cross89562dc2016-10-03 17:47:19 -0700885 var implicitDeps, orderOnlyDeps Paths
886
887 if a.Host() {
888 // Installed host modules might be used during the build, depend directly on their
889 // dependencies so their timestamp is updated whenever their dependency is updated
890 implicitDeps = deps
891 } else {
892 orderOnlyDeps = deps
893 }
894
Colin Crossae887032017-10-23 17:16:14 -0700895 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700896 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700897 Description: "install " + fullInstallPath.Base(),
898 Output: fullInstallPath,
899 Input: srcPath,
900 Implicits: implicitDeps,
901 OrderOnly: orderOnlyDeps,
902 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800903 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800904
Dan Willemsen322acaf2016-01-12 23:07:05 -0800905 a.installFiles = append(a.installFiles, fullInstallPath)
906 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700907 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700908 return fullInstallPath
909}
910
Colin Cross3854a602016-01-11 12:49:11 -0800911func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
912 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700913 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800914
Colin Cross893d8162017-04-26 17:34:03 -0700915 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700916
Colin Crossae887032017-10-23 17:16:14 -0700917 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700918 Rule: Symlink,
919 Description: "install symlink " + fullInstallPath.Base(),
920 Output: fullInstallPath,
921 OrderOnly: Paths{srcPath},
922 Default: !a.AConfig().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -0800923 Args: map[string]string{
924 "fromPath": srcPath.String(),
925 },
926 })
Colin Cross3854a602016-01-11 12:49:11 -0800927
Colin Cross12fc4972016-01-11 12:49:11 -0800928 a.installFiles = append(a.installFiles, fullInstallPath)
929 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
930 }
Colin Cross3854a602016-01-11 12:49:11 -0800931 return fullInstallPath
932}
933
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700934func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800935 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
936}
937
Colin Cross3f40fa42015-01-30 17:27:36 -0800938type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700939 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800940}
941
942func isFileInstaller(m blueprint.Module) bool {
943 _, ok := m.(fileInstaller)
944 return ok
945}
946
947func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700948 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800949 return ok
950}
Colin Crossfce53272015-04-08 11:21:40 -0700951
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700952func findStringInSlice(str string, slice []string) int {
953 for i, s := range slice {
954 if s == str {
955 return i
Colin Crossfce53272015-04-08 11:21:40 -0700956 }
957 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700958 return -1
959}
960
Colin Cross068e0fe2016-12-13 15:23:47 -0800961func SrcIsModule(s string) string {
962 if len(s) > 1 && s[0] == ':' {
963 return s[1:]
964 }
965 return ""
966}
967
968type sourceDependencyTag struct {
969 blueprint.BaseDependencyTag
970}
971
972var SourceDepTag sourceDependencyTag
973
974// Returns a list of modules that must be depended on to satisfy filegroup or generated sources
975// modules listed in srcFiles using ":module" syntax
976func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
977 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -0700978 set := make(map[string]bool)
979
Colin Cross068e0fe2016-12-13 15:23:47 -0800980 for _, s := range srcFiles {
981 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -0700982 if _, found := set[m]; found {
983 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
984 } else {
985 set[m] = true
986 deps = append(deps, m)
987 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800988 }
989 }
990
991 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
992}
993
994type SourceFileProducer interface {
995 Srcs() Paths
996}
997
998// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800999// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001000func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001001 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1002}
1003
1004func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001005 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001006
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001007 for i, e := range excludes {
1008 j := findStringInSlice(e, srcFiles)
1009 if j != -1 {
1010 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
1011 }
1012
1013 excludes[i] = filepath.Join(prefix, e)
1014 }
1015
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001016 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001017 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001018 if m := SrcIsModule(s); m != "" {
1019 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001020 if module == nil {
1021 // Error will have been handled by ExtractSourcesDeps
1022 continue
1023 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001024 if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001025 expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001026 } else {
1027 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1028 }
1029 } else if pathtools.IsGlob(s) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001030 globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001031 for i, s := range globbedSrcFiles {
1032 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001033 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001034 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001035 } else {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001036 s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1037 expandedSrcFiles = append(expandedSrcFiles, s)
Colin Cross8f101b42015-06-17 15:09:06 -07001038 }
1039 }
1040
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001041 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001042}
1043
Nan Zhang6d34b302017-02-04 17:47:46 -08001044func (ctx *androidModuleContext) RequiredModuleNames() []string {
1045 return ctx.module.base().commonProperties.Required
1046}
1047
Colin Cross7f19f372016-11-01 11:10:25 -07001048func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1049 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001050 if err != nil {
1051 ctx.ModuleErrorf("glob: %s", err.Error())
1052 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001053 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -07001054}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001055
Colin Cross463a90e2015-06-17 14:20:06 -07001056func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001057 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001058}
1059
Colin Cross0875c522017-11-28 17:34:01 -08001060func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001061 return &buildTargetSingleton{}
1062}
1063
Colin Cross87d8b562017-04-25 10:01:55 -07001064func parentDir(dir string) string {
1065 dir, _ = filepath.Split(dir)
1066 return filepath.Clean(dir)
1067}
1068
Colin Cross1f8c52b2015-06-16 16:38:17 -07001069type buildTargetSingleton struct{}
1070
Colin Cross0875c522017-11-28 17:34:01 -08001071func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1072 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001073
Colin Cross0875c522017-11-28 17:34:01 -08001074 mmTarget := func(dir string) WritablePath {
1075 return PathForPhony(ctx,
1076 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001077 }
1078
Colin Cross0875c522017-11-28 17:34:01 -08001079 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001080
Colin Cross0875c522017-11-28 17:34:01 -08001081 ctx.VisitAllModules(func(module Module) {
1082 blueprintDir := module.base().blueprintDir
1083 installTarget := module.base().installTarget
1084 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001085
Colin Cross0875c522017-11-28 17:34:01 -08001086 if checkbuildTarget != nil {
1087 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1088 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1089 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001090
Colin Cross0875c522017-11-28 17:34:01 -08001091 if installTarget != nil {
1092 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001093 }
1094 })
1095
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001096 suffix := ""
1097 if ctx.Config().(Config).EmbeddedInMake() {
1098 suffix = "-soong"
1099 }
1100
Colin Cross1f8c52b2015-06-16 16:38:17 -07001101 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001102 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001103 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001104 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001105 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001106 })
1107
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001108 // Make will generate the MODULES-IN-* targets
1109 if ctx.Config().(Config).EmbeddedInMake() {
1110 return
1111 }
1112
Colin Cross0875c522017-11-28 17:34:01 -08001113 sortedKeys := func(m map[string]Paths) []string {
1114 s := make([]string, 0, len(m))
1115 for k := range m {
1116 s = append(s, k)
1117 }
1118 sort.Strings(s)
1119 return s
1120 }
1121
Colin Cross87d8b562017-04-25 10:01:55 -07001122 // Ensure ancestor directories are in modulesInDir
1123 dirs := sortedKeys(modulesInDir)
1124 for _, dir := range dirs {
1125 dir := parentDir(dir)
1126 for dir != "." && dir != "/" {
1127 if _, exists := modulesInDir[dir]; exists {
1128 break
1129 }
1130 modulesInDir[dir] = nil
1131 dir = parentDir(dir)
1132 }
1133 }
1134
1135 // Make directories build their direct subdirectories
1136 dirs = sortedKeys(modulesInDir)
1137 for _, dir := range dirs {
1138 p := parentDir(dir)
1139 if p != "." && p != "/" {
1140 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1141 }
1142 }
1143
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001144 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1145 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1146 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001147 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001148 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001149 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001150 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001151 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001152 // HACK: checkbuild should be an optional build, but force it
1153 // enabled for now in standalone builds
Colin Cross0875c522017-11-28 17:34:01 -08001154 Default: !ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001155 })
1156 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001157
1158 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1159 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001160 ctx.VisitAllModules(func(module Module) {
1161 if module.Enabled() {
1162 os := module.Target().Os
1163 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001164 }
1165 })
1166
Colin Cross0875c522017-11-28 17:34:01 -08001167 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001168 for os, deps := range osDeps {
1169 var className string
1170
1171 switch os.Class {
1172 case Host:
1173 className = "host"
1174 case HostCross:
1175 className = "host-cross"
1176 case Device:
1177 className = "target"
1178 default:
1179 continue
1180 }
1181
Colin Cross0875c522017-11-28 17:34:01 -08001182 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001183 osClass[className] = append(osClass[className], name)
1184
Colin Cross0875c522017-11-28 17:34:01 -08001185 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001186 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001187 Output: name,
1188 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001189 })
1190 }
1191
1192 // Wrap those into host|host-cross|target phony rules
1193 osClasses := sortedKeys(osClass)
1194 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001195 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001196 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001197 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001198 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001199 })
1200 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001201}
Colin Crossd779da42015-12-17 18:00:23 -08001202
1203type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001204 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -08001205 ctx interface {
1206 ModuleName(blueprint.Module) string
1207 ModuleSubDir(blueprint.Module) string
1208 }
1209}
1210
1211func (s AndroidModulesByName) Len() int { return len(s.slice) }
1212func (s AndroidModulesByName) Less(i, j int) bool {
1213 mi, mj := s.slice[i], s.slice[j]
1214 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1215
1216 if ni != nj {
1217 return ni < nj
1218 } else {
1219 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1220 }
1221}
1222func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }