blob: 1608f4b91b1baf6fa3552e67f294c4465b1bb941 [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"`
Jeff Gaston63a250a2017-11-09 17:21:27 -0800214
215 NamespaceExportedToMake bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800216}
217
218type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700219 Host_supported *bool
220 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800221}
222
Colin Crossc472d572015-03-17 15:06:21 -0700223type Multilib string
224
225const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700226 MultilibBoth Multilib = "both"
227 MultilibFirst Multilib = "first"
228 MultilibCommon Multilib = "common"
229 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700230)
231
Colin Crossa1ad8d12016-06-01 17:09:44 -0700232type HostOrDeviceSupported int
233
234const (
235 _ HostOrDeviceSupported = iota
236 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700237 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700238 DeviceSupported
239 HostAndDeviceSupported
240 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700241 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700242)
243
Colin Cross36242852017-06-23 15:06:31 -0700244func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800245 base := m.base()
246 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700247
Colin Cross36242852017-06-23 15:06:31 -0700248 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700249 &base.nameProperties,
250 &base.commonProperties,
251 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700252}
253
Colin Cross36242852017-06-23 15:06:31 -0700254func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
255 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700256
257 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800258 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700259 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700260 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800261
Dan Willemsen218f6562015-07-08 18:13:11 -0700262 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700263 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700264 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800265 }
266
Colin Cross36242852017-06-23 15:06:31 -0700267 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800268}
269
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800270// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800271// modules. It should be included as an anonymous field in every module
272// struct definition. InitAndroidModule should then be called from the module's
273// factory function, and the return values from InitAndroidModule should be
274// returned from the factory function.
275//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800276// The ModuleBase type is responsible for implementing the GenerateBuildActions
277// method to support the blueprint.Module interface. This method will then call
278// the module's GenerateAndroidBuildActions method once for each build variant
279// that is to be built. GenerateAndroidBuildActions is passed a
280// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800281// AndroidModuleContext exposes extra functionality specific to the Android build
282// system including details about the particular build variant that is to be
283// generated.
284//
285// For example:
286//
287// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800288// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800289// )
290//
291// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800292// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800293// properties struct {
294// MyProperty string
295// }
296// }
297//
Colin Cross36242852017-06-23 15:06:31 -0700298// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800299// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700300// m.AddProperties(&m.properties)
301// android.InitAndroidModule(m)
302// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800303// }
304//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800305// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800306// // Get the CPU architecture for the current build variant.
307// variantArch := ctx.Arch()
308//
309// // ...
310// }
Colin Cross635c3b02016-05-18 15:37:25 -0700311type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800312 // Putting the curiously recurring thing pointing to the thing that contains
313 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700314 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700315 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800316
Colin Crossfc754582016-05-17 16:34:16 -0700317 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800318 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700319 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800320 hostAndDeviceProperties hostAndDeviceProperties
321 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700322 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700323 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800324
325 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700326 installFiles Paths
327 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700328
329 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
330 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800331 installTarget WritablePath
332 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700333 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700334
Colin Cross178a5092016-09-13 13:42:32 -0700335 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700336
337 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700338
339 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700340 buildParams []BuildParams
Colin Cross36242852017-06-23 15:06:31 -0700341}
342
343func (a *ModuleBase) AddProperties(props ...interface{}) {
344 a.registerProps = append(a.registerProps, props...)
345}
346
347func (a *ModuleBase) GetProperties() []interface{} {
348 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800349}
350
Colin Crossae887032017-10-23 17:16:14 -0700351func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700352 return a.buildParams
353}
354
Colin Crossce75d2c2016-10-06 16:12:58 -0700355// Name returns the name of the module. It may be overridden by individual module types, for
356// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700357func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800358 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700359}
360
Colin Crossce75d2c2016-10-06 16:12:58 -0700361// BaseModuleName returns the name of the module as specified in the blueprints file.
362func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800363 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700364}
365
Colin Cross635c3b02016-05-18 15:37:25 -0700366func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800367 return a
368}
369
Colin Cross8b74d172016-09-13 09:59:14 -0700370func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700371 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700372 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700373}
374
Colin Crossa1ad8d12016-06-01 17:09:44 -0700375func (a *ModuleBase) Target() Target {
376 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800377}
378
Colin Cross8b74d172016-09-13 09:59:14 -0700379func (a *ModuleBase) TargetPrimary() bool {
380 return a.commonProperties.CompilePrimary
381}
382
Colin Crossa1ad8d12016-06-01 17:09:44 -0700383func (a *ModuleBase) Os() OsType {
384 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800385}
386
Colin Cross635c3b02016-05-18 15:37:25 -0700387func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700388 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800389}
390
Colin Cross635c3b02016-05-18 15:37:25 -0700391func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700392 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800393}
394
Dan Willemsen0b24c742016-10-04 15:13:37 -0700395func (a *ModuleBase) ArchSpecific() bool {
396 return a.commonProperties.ArchSpecific
397}
398
Colin Crossa1ad8d12016-06-01 17:09:44 -0700399func (a *ModuleBase) OsClassSupported() []OsClass {
400 switch a.commonProperties.HostOrDeviceSupported {
401 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700402 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700403 case HostSupportedNoCross:
404 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700405 case DeviceSupported:
406 return []OsClass{Device}
407 case HostAndDeviceSupported:
408 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700409 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700410 supported = append(supported, Host, HostCross)
411 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700412 if a.hostAndDeviceProperties.Device_supported == nil ||
413 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700414 supported = append(supported, Device)
415 }
416 return supported
417 default:
418 return nil
419 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800420}
421
Colin Cross635c3b02016-05-18 15:37:25 -0700422func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800423 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
424 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700425 (a.hostAndDeviceProperties.Device_supported == nil ||
426 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800427}
428
Colin Cross635c3b02016-05-18 15:37:25 -0700429func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800430 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800431 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800432 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800433 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800434}
435
Colin Crossce75d2c2016-10-06 16:12:58 -0700436func (a *ModuleBase) SkipInstall() {
437 a.commonProperties.SkipInstall = true
438}
439
Colin Cross635c3b02016-05-18 15:37:25 -0700440func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700441 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800442
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700443 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800444 ctx.VisitDepsDepthFirstIf(isFileInstaller,
445 func(m blueprint.Module) {
446 fileInstaller := m.(fileInstaller)
447 files := fileInstaller.filesToInstall()
448 result = append(result, files...)
449 })
450
451 return result
452}
453
Colin Cross635c3b02016-05-18 15:37:25 -0700454func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800455 return a.installFiles
456}
457
Colin Cross635c3b02016-05-18 15:37:25 -0700458func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800459 return p.noAddressSanitizer
460}
461
Colin Cross635c3b02016-05-18 15:37:25 -0700462func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800463 return false
464}
465
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700466func (p *ModuleBase) InstallInSanitizerDir() bool {
467 return false
468}
469
Colin Cross0875c522017-11-28 17:34:01 -0800470func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700471 allInstalledFiles := Paths{}
472 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800473 ctx.VisitAllModuleVariants(func(module Module) {
474 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700475 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
476 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800477 })
478
Colin Cross0875c522017-11-28 17:34:01 -0800479 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700480
Jeff Gaston63a250a2017-11-09 17:21:27 -0800481 namespacePrefix := ctx.Namespace().(*Namespace).id
482 if namespacePrefix != "" {
483 namespacePrefix = namespacePrefix + "-"
484 }
485
Colin Cross3f40fa42015-01-30 17:27:36 -0800486 if len(allInstalledFiles) > 0 {
Jeff Gaston63a250a2017-11-09 17:21:27 -0800487 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800488 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700489 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800490 Output: name,
491 Implicits: allInstalledFiles,
492 Default: !ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700493 })
494 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700495 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700496 }
497
498 if len(allCheckbuildFiles) > 0 {
Jeff Gaston63a250a2017-11-09 17:21:27 -0800499 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800500 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700501 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800502 Output: name,
503 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700504 })
505 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700506 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700507 }
508
509 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800510 suffix := ""
511 if ctx.Config().(Config).EmbeddedInMake() {
512 suffix = "-soong"
513 }
514
Jeff Gaston63a250a2017-11-09 17:21:27 -0800515 name := PathForPhony(ctx, namespacePrefix+ctx.ModuleName()+suffix)
Colin Cross0875c522017-11-28 17:34:01 -0800516 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700517 Rule: blueprint.Phony,
Jeff Gaston63a250a2017-11-09 17:21:27 -0800518 Outputs: []WritablePath{name},
Colin Cross9454bfa2015-03-17 13:24:18 -0700519 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800520 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700521
522 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800523 }
524}
525
Colin Cross635c3b02016-05-18 15:37:25 -0700526func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700527 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700528 target: a.commonProperties.CompileTarget,
529 targetPrimary: a.commonProperties.CompilePrimary,
Colin Cross7d716ba2017-11-01 10:38:29 -0700530 vendor: Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Vendor),
Colin Cross8b74d172016-09-13 09:59:14 -0700531 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800532 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800533}
534
Colin Cross0875c522017-11-28 17:34:01 -0800535func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
536 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700537 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800538 ModuleContext: blueprintCtx,
539 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
540 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700541 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800542 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800543 }
544
Colin Cross67a5c132017-05-09 13:45:28 -0700545 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
546 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800547 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
548 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700549 }
Colin Cross0875c522017-11-28 17:34:01 -0800550 if !ctx.PrimaryArch() {
551 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700552 }
553
554 ctx.Variable(pctx, "moduleDesc", desc)
555
556 s := ""
557 if len(suffix) > 0 {
558 s = " [" + strings.Join(suffix, " ") + "]"
559 }
560 ctx.Variable(pctx, "moduleDescSuffix", s)
561
Colin Cross9b1d13d2016-09-19 15:18:11 -0700562 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800563 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700564 if ctx.Failed() {
565 return
566 }
567
Colin Cross0875c522017-11-28 17:34:01 -0800568 a.installFiles = append(a.installFiles, ctx.installFiles...)
569 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 }
571
Colin Cross9b1d13d2016-09-19 15:18:11 -0700572 if a == ctx.FinalModule().(Module).base() {
573 a.generateModuleTarget(ctx)
574 if ctx.Failed() {
575 return
576 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800577 }
Colin Crosscec81712017-07-13 14:43:27 -0700578
Colin Cross0875c522017-11-28 17:34:01 -0800579 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800580}
581
Colin Crossf6566ed2015-03-24 11:13:38 -0700582type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700583 target Target
584 targetPrimary bool
585 debug bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700586 vendor bool
Colin Cross8b74d172016-09-13 09:59:14 -0700587 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700588}
589
Colin Cross3f40fa42015-01-30 17:27:36 -0800590type androidModuleContext struct {
591 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700592 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700593 installDeps Paths
594 installFiles Paths
595 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800596 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700597 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700598
599 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700600 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800601}
602
Colin Cross67a5c132017-05-09 13:45:28 -0700603func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800604 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700605 Rule: ErrorRule,
606 Description: desc,
607 Outputs: outputs,
608 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800609 Args: map[string]string{
610 "error": err.Error(),
611 },
612 })
613 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800614}
615
Colin Cross0875c522017-11-28 17:34:01 -0800616func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700617 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800618}
619
Colin Cross0875c522017-11-28 17:34:01 -0800620func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700621 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700622 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800623 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800624 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700625 Outputs: params.Outputs.Strings(),
626 ImplicitOutputs: params.ImplicitOutputs.Strings(),
627 Inputs: params.Inputs.Strings(),
628 Implicits: params.Implicits.Strings(),
629 OrderOnly: params.OrderOnly.Strings(),
630 Args: params.Args,
631 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700632 }
633
Colin Cross33bfb0a2016-11-21 17:23:08 -0800634 if params.Depfile != nil {
635 bparams.Depfile = params.Depfile.String()
636 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700637 if params.Output != nil {
638 bparams.Outputs = append(bparams.Outputs, params.Output.String())
639 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700640 if params.ImplicitOutput != nil {
641 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
642 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700643 if params.Input != nil {
644 bparams.Inputs = append(bparams.Inputs, params.Input.String())
645 }
646 if params.Implicit != nil {
647 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
648 }
649
Colin Cross0875c522017-11-28 17:34:01 -0800650 return bparams
651}
652
653func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
654 a.ModuleContext.Variable(pctx.PackageContext, name, value)
655}
656
657func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
658 argNames ...string) blueprint.Rule {
659
660 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
661}
662
663func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
664 if a.config.captureBuild {
665 a.buildParams = append(a.buildParams, params)
666 }
667
668 bparams := convertBuildParams(params)
669
670 if bparams.Description != "" {
671 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
672 }
673
Colin Cross6ff51382015-12-17 16:39:19 -0800674 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700675 a.ninjaError(bparams.Description, bparams.Outputs,
676 fmt.Errorf("module %s missing dependencies: %s\n",
677 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800678 return
679 }
680
Colin Cross0875c522017-11-28 17:34:01 -0800681 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700682}
683
Colin Cross6ff51382015-12-17 16:39:19 -0800684func (a *androidModuleContext) GetMissingDependencies() []string {
685 return a.missingDeps
686}
687
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800688func (a *androidModuleContext) AddMissingDependencies(deps []string) {
689 if deps != nil {
690 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700691 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800692 }
693}
694
Colin Crossd11fcda2017-10-23 17:59:01 -0700695func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
696 aModule, _ := module.(Module)
697 if aModule == nil {
698 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
699 return nil
700 }
701
702 if !aModule.Enabled() {
703 if a.AConfig().AllowMissingDependencies() {
704 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
705 } else {
706 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
707 }
708 return nil
709 }
710
711 return aModule
712}
713
Colin Cross35143d02017-11-16 00:11:20 -0800714func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
715 a.ModuleContext.VisitDirectDeps(visit)
716}
717
Colin Crossd11fcda2017-10-23 17:59:01 -0700718func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
719 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
720 if aModule := a.validateAndroidModule(module); aModule != nil {
721 visit(aModule)
722 }
723 })
724}
725
726func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
727 a.ModuleContext.VisitDirectDepsIf(
728 // pred
729 func(module blueprint.Module) bool {
730 if aModule := a.validateAndroidModule(module); aModule != nil {
731 return pred(aModule)
732 } else {
733 return false
734 }
735 },
736 // visit
737 func(module blueprint.Module) {
738 visit(module.(Module))
739 })
740}
741
742func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
743 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
744 if aModule := a.validateAndroidModule(module); aModule != nil {
745 visit(aModule)
746 }
747 })
748}
749
750func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
751 a.ModuleContext.VisitDepsDepthFirstIf(
752 // pred
753 func(module blueprint.Module) bool {
754 if aModule := a.validateAndroidModule(module); aModule != nil {
755 return pred(aModule)
756 } else {
757 return false
758 }
759 },
760 // visit
761 func(module blueprint.Module) {
762 visit(module.(Module))
763 })
764}
765
766func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
767 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
768 childAndroidModule := a.validateAndroidModule(child)
769 parentAndroidModule := a.validateAndroidModule(parent)
770 if childAndroidModule != nil && parentAndroidModule != nil {
771 return visit(childAndroidModule, parentAndroidModule)
772 } else {
773 return false
774 }
775 })
776}
777
Colin Cross0875c522017-11-28 17:34:01 -0800778func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
779 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
780 visit(module.(Module))
781 })
782}
783
784func (a *androidModuleContext) PrimaryModule() Module {
785 return a.ModuleContext.PrimaryModule().(Module)
786}
787
788func (a *androidModuleContext) FinalModule() Module {
789 return a.ModuleContext.FinalModule().(Module)
790}
791
Colin Crossa1ad8d12016-06-01 17:09:44 -0700792func (a *androidBaseContextImpl) Target() Target {
793 return a.target
794}
795
Colin Cross8b74d172016-09-13 09:59:14 -0700796func (a *androidBaseContextImpl) TargetPrimary() bool {
797 return a.targetPrimary
798}
799
Colin Crossf6566ed2015-03-24 11:13:38 -0700800func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700801 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800802}
803
Colin Crossa1ad8d12016-06-01 17:09:44 -0700804func (a *androidBaseContextImpl) Os() OsType {
805 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800806}
807
Colin Crossf6566ed2015-03-24 11:13:38 -0700808func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700809 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700810}
811
812func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700813 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700814}
815
Colin Cross0af4b842015-04-30 16:36:18 -0700816func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700817 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700818}
819
Colin Cross3edeee12017-04-04 12:59:48 -0700820func (a *androidBaseContextImpl) Windows() bool {
821 return a.target.Os == Windows
822}
823
Colin Crossf6566ed2015-03-24 11:13:38 -0700824func (a *androidBaseContextImpl) Debug() bool {
825 return a.debug
826}
827
Colin Cross1e7d3702016-08-24 15:25:47 -0700828func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700829 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
830 return true
831 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700832 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
833}
834
Colin Cross1332b002015-04-07 17:11:30 -0700835func (a *androidBaseContextImpl) AConfig() Config {
836 return a.config
837}
838
Colin Cross9272ade2016-08-17 15:24:12 -0700839func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
840 return DeviceConfig{a.config.deviceConfig}
841}
842
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700843func (a *androidBaseContextImpl) InstallOnVendorPartition() bool {
Dan Willemsenaa118f92017-04-06 12:49:58 -0700844 return a.vendor
Dan Willemsen782a2d12015-12-21 14:55:28 -0800845}
846
Colin Cross8d8f8e22016-08-03 11:57:50 -0700847func (a *androidModuleContext) InstallInData() bool {
848 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800849}
850
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700851func (a *androidModuleContext) InstallInSanitizerDir() bool {
852 return a.module.InstallInSanitizerDir()
853}
854
Colin Cross893d8162017-04-26 17:34:03 -0700855func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
856 if a.module.base().commonProperties.SkipInstall {
857 return true
858 }
859
860 if a.Device() {
861 if a.AConfig().SkipDeviceInstall() {
862 return true
863 }
864
865 if a.AConfig().SkipMegaDeviceInstall(fullInstallPath.String()) {
866 return true
867 }
868 }
869
870 return false
871}
872
Colin Cross5c517922017-08-31 12:29:17 -0700873func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700874 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -0700875 return a.installFile(installPath, name, srcPath, Cp, deps)
876}
877
878func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
879 deps ...Path) OutputPath {
880 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
881}
882
883func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
884 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700885
Dan Willemsen782a2d12015-12-21 14:55:28 -0800886 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700887 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800888
Colin Cross893d8162017-04-26 17:34:03 -0700889 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700890
Dan Willemsen322acaf2016-01-12 23:07:05 -0800891 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700892
Colin Cross89562dc2016-10-03 17:47:19 -0700893 var implicitDeps, orderOnlyDeps Paths
894
895 if a.Host() {
896 // Installed host modules might be used during the build, depend directly on their
897 // dependencies so their timestamp is updated whenever their dependency is updated
898 implicitDeps = deps
899 } else {
900 orderOnlyDeps = deps
901 }
902
Colin Crossae887032017-10-23 17:16:14 -0700903 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700904 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700905 Description: "install " + fullInstallPath.Base(),
906 Output: fullInstallPath,
907 Input: srcPath,
908 Implicits: implicitDeps,
909 OrderOnly: orderOnlyDeps,
910 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800911 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800912
Dan Willemsen322acaf2016-01-12 23:07:05 -0800913 a.installFiles = append(a.installFiles, fullInstallPath)
914 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700915 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700916 return fullInstallPath
917}
918
Colin Cross3854a602016-01-11 12:49:11 -0800919func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
920 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700921 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800922
Colin Cross893d8162017-04-26 17:34:03 -0700923 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700924
Colin Crossae887032017-10-23 17:16:14 -0700925 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700926 Rule: Symlink,
927 Description: "install symlink " + fullInstallPath.Base(),
928 Output: fullInstallPath,
929 OrderOnly: Paths{srcPath},
930 Default: !a.AConfig().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -0800931 Args: map[string]string{
932 "fromPath": srcPath.String(),
933 },
934 })
Colin Cross3854a602016-01-11 12:49:11 -0800935
Colin Cross12fc4972016-01-11 12:49:11 -0800936 a.installFiles = append(a.installFiles, fullInstallPath)
937 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
938 }
Colin Cross3854a602016-01-11 12:49:11 -0800939 return fullInstallPath
940}
941
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700942func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800943 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
944}
945
Colin Cross3f40fa42015-01-30 17:27:36 -0800946type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700947 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800948}
949
950func isFileInstaller(m blueprint.Module) bool {
951 _, ok := m.(fileInstaller)
952 return ok
953}
954
955func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700956 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800957 return ok
958}
Colin Crossfce53272015-04-08 11:21:40 -0700959
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700960func findStringInSlice(str string, slice []string) int {
961 for i, s := range slice {
962 if s == str {
963 return i
Colin Crossfce53272015-04-08 11:21:40 -0700964 }
965 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700966 return -1
967}
968
Colin Cross068e0fe2016-12-13 15:23:47 -0800969func SrcIsModule(s string) string {
970 if len(s) > 1 && s[0] == ':' {
971 return s[1:]
972 }
973 return ""
974}
975
976type sourceDependencyTag struct {
977 blueprint.BaseDependencyTag
978}
979
980var SourceDepTag sourceDependencyTag
981
982// Returns a list of modules that must be depended on to satisfy filegroup or generated sources
983// modules listed in srcFiles using ":module" syntax
984func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
985 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -0700986 set := make(map[string]bool)
987
Colin Cross068e0fe2016-12-13 15:23:47 -0800988 for _, s := range srcFiles {
989 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -0700990 if _, found := set[m]; found {
991 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
992 } else {
993 set[m] = true
994 deps = append(deps, m)
995 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800996 }
997 }
998
999 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1000}
1001
1002type SourceFileProducer interface {
1003 Srcs() Paths
1004}
1005
1006// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001007// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001008func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001009 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1010}
1011
1012func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001013 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001014
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001015 for i, e := range excludes {
1016 j := findStringInSlice(e, srcFiles)
1017 if j != -1 {
1018 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
1019 }
1020
1021 excludes[i] = filepath.Join(prefix, e)
1022 }
1023
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001024 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001025 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001026 if m := SrcIsModule(s); m != "" {
1027 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001028 if module == nil {
1029 // Error will have been handled by ExtractSourcesDeps
1030 continue
1031 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001032 if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001033 expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001034 } else {
1035 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1036 }
1037 } else if pathtools.IsGlob(s) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001038 globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001039 for i, s := range globbedSrcFiles {
1040 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001041 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001042 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001043 } else {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001044 s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1045 expandedSrcFiles = append(expandedSrcFiles, s)
Colin Cross8f101b42015-06-17 15:09:06 -07001046 }
1047 }
1048
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001049 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001050}
1051
Nan Zhang6d34b302017-02-04 17:47:46 -08001052func (ctx *androidModuleContext) RequiredModuleNames() []string {
1053 return ctx.module.base().commonProperties.Required
1054}
1055
Colin Cross7f19f372016-11-01 11:10:25 -07001056func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1057 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001058 if err != nil {
1059 ctx.ModuleErrorf("glob: %s", err.Error())
1060 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001061 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -07001062}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001063
Colin Cross463a90e2015-06-17 14:20:06 -07001064func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001065 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001066}
1067
Colin Cross0875c522017-11-28 17:34:01 -08001068func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001069 return &buildTargetSingleton{}
1070}
1071
Colin Cross87d8b562017-04-25 10:01:55 -07001072func parentDir(dir string) string {
1073 dir, _ = filepath.Split(dir)
1074 return filepath.Clean(dir)
1075}
1076
Colin Cross1f8c52b2015-06-16 16:38:17 -07001077type buildTargetSingleton struct{}
1078
Colin Cross0875c522017-11-28 17:34:01 -08001079func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1080 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001081
Colin Cross0875c522017-11-28 17:34:01 -08001082 mmTarget := func(dir string) WritablePath {
1083 return PathForPhony(ctx,
1084 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001085 }
1086
Colin Cross0875c522017-11-28 17:34:01 -08001087 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001088
Colin Cross0875c522017-11-28 17:34:01 -08001089 ctx.VisitAllModules(func(module Module) {
1090 blueprintDir := module.base().blueprintDir
1091 installTarget := module.base().installTarget
1092 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001093
Colin Cross0875c522017-11-28 17:34:01 -08001094 if checkbuildTarget != nil {
1095 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1096 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1097 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001098
Colin Cross0875c522017-11-28 17:34:01 -08001099 if installTarget != nil {
1100 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001101 }
1102 })
1103
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001104 suffix := ""
1105 if ctx.Config().(Config).EmbeddedInMake() {
1106 suffix = "-soong"
1107 }
1108
Colin Cross1f8c52b2015-06-16 16:38:17 -07001109 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001110 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001111 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001112 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001113 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001114 })
1115
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001116 // Make will generate the MODULES-IN-* targets
1117 if ctx.Config().(Config).EmbeddedInMake() {
1118 return
1119 }
1120
Colin Cross0875c522017-11-28 17:34:01 -08001121 sortedKeys := func(m map[string]Paths) []string {
1122 s := make([]string, 0, len(m))
1123 for k := range m {
1124 s = append(s, k)
1125 }
1126 sort.Strings(s)
1127 return s
1128 }
1129
Colin Cross87d8b562017-04-25 10:01:55 -07001130 // Ensure ancestor directories are in modulesInDir
1131 dirs := sortedKeys(modulesInDir)
1132 for _, dir := range dirs {
1133 dir := parentDir(dir)
1134 for dir != "." && dir != "/" {
1135 if _, exists := modulesInDir[dir]; exists {
1136 break
1137 }
1138 modulesInDir[dir] = nil
1139 dir = parentDir(dir)
1140 }
1141 }
1142
1143 // Make directories build their direct subdirectories
1144 dirs = sortedKeys(modulesInDir)
1145 for _, dir := range dirs {
1146 p := parentDir(dir)
1147 if p != "." && p != "/" {
1148 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1149 }
1150 }
1151
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001152 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1153 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1154 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001155 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001156 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001157 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001158 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001159 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001160 // HACK: checkbuild should be an optional build, but force it
1161 // enabled for now in standalone builds
Colin Cross0875c522017-11-28 17:34:01 -08001162 Default: !ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001163 })
1164 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001165
1166 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1167 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001168 ctx.VisitAllModules(func(module Module) {
1169 if module.Enabled() {
1170 os := module.Target().Os
1171 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001172 }
1173 })
1174
Colin Cross0875c522017-11-28 17:34:01 -08001175 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001176 for os, deps := range osDeps {
1177 var className string
1178
1179 switch os.Class {
1180 case Host:
1181 className = "host"
1182 case HostCross:
1183 className = "host-cross"
1184 case Device:
1185 className = "target"
1186 default:
1187 continue
1188 }
1189
Colin Cross0875c522017-11-28 17:34:01 -08001190 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001191 osClass[className] = append(osClass[className], name)
1192
Colin Cross0875c522017-11-28 17:34:01 -08001193 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001194 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001195 Output: name,
1196 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001197 })
1198 }
1199
1200 // Wrap those into host|host-cross|target phony rules
1201 osClasses := sortedKeys(osClass)
1202 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001203 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001204 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001205 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001206 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001207 })
1208 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001209}
Colin Crossd779da42015-12-17 18:00:23 -08001210
1211type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001212 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -08001213 ctx interface {
1214 ModuleName(blueprint.Module) string
1215 ModuleSubDir(blueprint.Module) string
1216 }
1217}
1218
1219func (s AndroidModulesByName) Len() int { return len(s.slice) }
1220func (s AndroidModulesByName) Less(i, j int) bool {
1221 mi, mj := s.slice[i], s.slice[j]
1222 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1223
1224 if ni != nj {
1225 return ni < nj
1226 } else {
1227 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1228 }
1229}
1230func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }