blob: 5e134b6e96a3ad10011891f50ff75e5ab67bd81f [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 Cross6ff51382015-12-17 16:39:19 -080020 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070021
22 "github.com/google/blueprint"
Colin Cross7f19f372016-11-01 11:10:25 -070023 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080024)
25
26var (
27 DeviceSharedLibrary = "shared_library"
28 DeviceStaticLibrary = "static_library"
29 DeviceExecutable = "executable"
30 HostSharedLibrary = "host_shared_library"
31 HostStaticLibrary = "host_static_library"
32 HostExecutable = "host_executable"
33)
34
Dan Willemsen34cc69e2015-09-23 15:26:20 -070035type ModuleBuildParams struct {
Dan Willemsen9f3c5742016-11-03 14:28:31 -070036 Rule blueprint.Rule
Colin Cross33bfb0a2016-11-21 17:23:08 -080037 Deps blueprint.Deps
38 Depfile WritablePath
Colin Cross67a5c132017-05-09 13:45:28 -070039 Description string
Dan Willemsen9f3c5742016-11-03 14:28:31 -070040 Output WritablePath
41 Outputs WritablePaths
42 ImplicitOutput WritablePath
43 ImplicitOutputs WritablePaths
44 Input Path
45 Inputs Paths
46 Implicit Path
47 Implicits Paths
48 OrderOnly Paths
49 Default bool
50 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070051}
52
Colin Crossf6566ed2015-03-24 11:13:38 -070053type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070054 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070055 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070056 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070057 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070058 Host() bool
59 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070060 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070061 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070062 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070063 PrimaryArch() bool
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070064 InstallOnVendorPartition() bool
Colin Cross1332b002015-04-07 17:11:30 -070065 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070066 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070067}
68
Colin Cross635c3b02016-05-18 15:37:25 -070069type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070070 blueprint.BaseModuleContext
71 androidBaseContext
72}
73
Colin Cross635c3b02016-05-18 15:37:25 -070074type ModuleContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070075 androidBaseContext
Colin Cross3f68a132017-10-23 17:10:29 -070076 blueprint.BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -080077
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078 // Similar to Build, but takes Paths instead of []string,
79 // and performs more verification.
80 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070081
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 ExpandSources(srcFiles, excludes []string) Paths
Colin Crossfaeb7aa2017-02-01 14:12:44 -080083 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -070084 Glob(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085
Colin Cross5c517922017-08-31 12:29:17 -070086 InstallExecutable(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
87 InstallFile(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080088 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080090
91 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070092
Colin Cross8d8f8e22016-08-03 11:57:50 -070093 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -070094 InstallInSanitizerDir() bool
Nan Zhang6d34b302017-02-04 17:47:46 -080095
96 RequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -070097
98 // android.ModuleContext methods
99 // These are duplicated instead of embedded so that can eventually be wrapped to take an
100 // android.Module instead of a blueprint.Module
101 OtherModuleName(m blueprint.Module) string
102 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
103 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
104
105 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
106 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
107
108 ModuleSubDir() string
109
110 VisitDirectDeps(visit func(blueprint.Module))
111 VisitDirectDepsIf(pred func(blueprint.Module) bool, visit func(blueprint.Module))
112 VisitDepsDepthFirst(visit func(blueprint.Module))
113 VisitDepsDepthFirstIf(pred func(blueprint.Module) bool, visit func(blueprint.Module))
114 WalkDeps(visit func(blueprint.Module, blueprint.Module) bool)
115
116 Variable(pctx blueprint.PackageContext, name, value string)
117 Rule(pctx blueprint.PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
118 Build(pctx blueprint.PackageContext, params blueprint.BuildParams)
119
120 PrimaryModule() blueprint.Module
121 FinalModule() blueprint.Module
122 VisitAllModuleVariants(visit func(blueprint.Module))
123
124 GetMissingDependencies() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800125}
126
Colin Cross635c3b02016-05-18 15:37:25 -0700127type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 blueprint.Module
129
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700130 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
131 // but GenerateAndroidBuildActions also has access to Android-specific information.
132 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700133 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700134
Colin Cross1e676be2016-10-12 14:38:15 -0700135 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800136
Colin Cross635c3b02016-05-18 15:37:25 -0700137 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800138 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700139 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800140 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700141 InstallInSanitizerDir() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800142 SkipInstall()
Colin Cross36242852017-06-23 15:06:31 -0700143
144 AddProperties(props ...interface{})
145 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700146
147 BuildParamsForTests() []ModuleBuildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800148}
149
Colin Crossfc754582016-05-17 16:34:16 -0700150type nameProperties struct {
151 // The name of the module. Must be unique across all modules.
Colin Crossc77f9d12015-04-02 13:54:39 -0700152 Name string
Colin Crossfc754582016-05-17 16:34:16 -0700153}
154
155type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700156 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800157
Dan Willemsen0effe062015-11-30 16:06:01 -0800158 // emit build rules for this module
159 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800160
Colin Cross7d5136f2015-05-11 13:39:40 -0700161 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800162 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
163 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
164 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700165 Compile_multilib string `android:"arch_variant"`
166
167 Target struct {
168 Host struct {
169 Compile_multilib string
170 }
171 Android struct {
172 Compile_multilib string
173 }
174 }
175
176 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800177
Dan Willemsen782a2d12015-12-21 14:55:28 -0800178 // whether this is a proprietary vendor module, and should be installed into /vendor
179 Proprietary bool
180
Colin Cross55708f32017-03-20 13:23:34 -0700181 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700182 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700183
Dan Willemsenaa118f92017-04-06 12:49:58 -0700184 // whether this module is device specific and should be installed into /vendor
185 Vendor bool
186
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700187 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
188 // file
189 Logtags []string
190
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700191 // init.rc files to be installed if this module is installed
192 Init_rc []string
193
Chris Wolfe998306e2016-08-15 14:47:23 -0400194 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700195 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400196
Colin Cross5aac3622017-08-31 15:07:09 -0700197 // relative path to a file to include in the list of notices for the device
198 Notice *string
199
Colin Crossa1ad8d12016-06-01 17:09:44 -0700200 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700201 CompileTarget Target `blueprint:"mutated"`
202 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800203
204 // Set by InitAndroidModule
205 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700206 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700207
208 SkipInstall bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800209}
210
211type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700212 Host_supported *bool
213 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800214}
215
Colin Crossc472d572015-03-17 15:06:21 -0700216type Multilib string
217
218const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700219 MultilibBoth Multilib = "both"
220 MultilibFirst Multilib = "first"
221 MultilibCommon Multilib = "common"
222 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700223)
224
Colin Crossa1ad8d12016-06-01 17:09:44 -0700225type HostOrDeviceSupported int
226
227const (
228 _ HostOrDeviceSupported = iota
229 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700230 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700231 DeviceSupported
232 HostAndDeviceSupported
233 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700234 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700235)
236
Colin Cross36242852017-06-23 15:06:31 -0700237func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800238 base := m.base()
239 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700240
Colin Cross36242852017-06-23 15:06:31 -0700241 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700242 &base.nameProperties,
243 &base.commonProperties,
244 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700245}
246
Colin Cross36242852017-06-23 15:06:31 -0700247func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
248 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700249
250 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800251 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700252 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700253 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800254
Dan Willemsen218f6562015-07-08 18:13:11 -0700255 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700256 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700257 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800258 }
259
Colin Cross36242852017-06-23 15:06:31 -0700260 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800261}
262
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800263// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800264// modules. It should be included as an anonymous field in every module
265// struct definition. InitAndroidModule should then be called from the module's
266// factory function, and the return values from InitAndroidModule should be
267// returned from the factory function.
268//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800269// The ModuleBase type is responsible for implementing the GenerateBuildActions
270// method to support the blueprint.Module interface. This method will then call
271// the module's GenerateAndroidBuildActions method once for each build variant
272// that is to be built. GenerateAndroidBuildActions is passed a
273// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800274// AndroidModuleContext exposes extra functionality specific to the Android build
275// system including details about the particular build variant that is to be
276// generated.
277//
278// For example:
279//
280// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800281// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800282// )
283//
284// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800285// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800286// properties struct {
287// MyProperty string
288// }
289// }
290//
Colin Cross36242852017-06-23 15:06:31 -0700291// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800292// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700293// m.AddProperties(&m.properties)
294// android.InitAndroidModule(m)
295// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800296// }
297//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800298// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800299// // Get the CPU architecture for the current build variant.
300// variantArch := ctx.Arch()
301//
302// // ...
303// }
Colin Cross635c3b02016-05-18 15:37:25 -0700304type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800305 // Putting the curiously recurring thing pointing to the thing that contains
306 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700307 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700308 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800309
Colin Crossfc754582016-05-17 16:34:16 -0700310 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800311 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700312 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800313 hostAndDeviceProperties hostAndDeviceProperties
314 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700315 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700316 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800317
318 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700319 installFiles Paths
320 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700321
322 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
323 // Only set on the final variant of each module
324 installTarget string
325 checkbuildTarget string
326 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700327
Colin Cross178a5092016-09-13 13:42:32 -0700328 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700329
330 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700331
332 // For tests
333 buildParams []ModuleBuildParams
Colin Cross36242852017-06-23 15:06:31 -0700334}
335
336func (a *ModuleBase) AddProperties(props ...interface{}) {
337 a.registerProps = append(a.registerProps, props...)
338}
339
340func (a *ModuleBase) GetProperties() []interface{} {
341 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800342}
343
Colin Crosscec81712017-07-13 14:43:27 -0700344func (a *ModuleBase) BuildParamsForTests() []ModuleBuildParams {
345 return a.buildParams
346}
347
Colin Crossce75d2c2016-10-06 16:12:58 -0700348// Name returns the name of the module. It may be overridden by individual module types, for
349// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700350func (a *ModuleBase) Name() string {
351 return a.nameProperties.Name
352}
353
Colin Crossce75d2c2016-10-06 16:12:58 -0700354// BaseModuleName returns the name of the module as specified in the blueprints file.
355func (a *ModuleBase) BaseModuleName() string {
356 return a.nameProperties.Name
357}
358
Colin Cross635c3b02016-05-18 15:37:25 -0700359func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800360 return a
361}
362
Colin Cross8b74d172016-09-13 09:59:14 -0700363func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700364 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700365 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700366}
367
Colin Crossa1ad8d12016-06-01 17:09:44 -0700368func (a *ModuleBase) Target() Target {
369 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800370}
371
Colin Cross8b74d172016-09-13 09:59:14 -0700372func (a *ModuleBase) TargetPrimary() bool {
373 return a.commonProperties.CompilePrimary
374}
375
Colin Crossa1ad8d12016-06-01 17:09:44 -0700376func (a *ModuleBase) Os() OsType {
377 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800378}
379
Colin Cross635c3b02016-05-18 15:37:25 -0700380func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700381 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800382}
383
Colin Cross635c3b02016-05-18 15:37:25 -0700384func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700385 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800386}
387
Dan Willemsen0b24c742016-10-04 15:13:37 -0700388func (a *ModuleBase) ArchSpecific() bool {
389 return a.commonProperties.ArchSpecific
390}
391
Colin Crossa1ad8d12016-06-01 17:09:44 -0700392func (a *ModuleBase) OsClassSupported() []OsClass {
393 switch a.commonProperties.HostOrDeviceSupported {
394 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700395 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700396 case HostSupportedNoCross:
397 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700398 case DeviceSupported:
399 return []OsClass{Device}
400 case HostAndDeviceSupported:
401 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700402 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700403 supported = append(supported, Host, HostCross)
404 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700405 if a.hostAndDeviceProperties.Device_supported == nil ||
406 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700407 supported = append(supported, Device)
408 }
409 return supported
410 default:
411 return nil
412 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800413}
414
Colin Cross635c3b02016-05-18 15:37:25 -0700415func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800416 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
417 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700418 (a.hostAndDeviceProperties.Device_supported == nil ||
419 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800420}
421
Colin Cross635c3b02016-05-18 15:37:25 -0700422func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800423 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800424 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800425 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800426 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800427}
428
Colin Crossce75d2c2016-10-06 16:12:58 -0700429func (a *ModuleBase) SkipInstall() {
430 a.commonProperties.SkipInstall = true
431}
432
Colin Cross635c3b02016-05-18 15:37:25 -0700433func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700434 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800435
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700436 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800437 ctx.VisitDepsDepthFirstIf(isFileInstaller,
438 func(m blueprint.Module) {
439 fileInstaller := m.(fileInstaller)
440 files := fileInstaller.filesToInstall()
441 result = append(result, files...)
442 })
443
444 return result
445}
446
Colin Cross635c3b02016-05-18 15:37:25 -0700447func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800448 return a.installFiles
449}
450
Colin Cross635c3b02016-05-18 15:37:25 -0700451func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800452 return p.noAddressSanitizer
453}
454
Colin Cross635c3b02016-05-18 15:37:25 -0700455func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800456 return false
457}
458
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700459func (p *ModuleBase) InstallInSanitizerDir() bool {
460 return false
461}
462
Colin Cross635c3b02016-05-18 15:37:25 -0700463func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700464 allInstalledFiles := Paths{}
465 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800466 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700467 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700468 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
469 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 })
471
Colin Cross9454bfa2015-03-17 13:24:18 -0700472 deps := []string{}
473
Colin Cross3f40fa42015-01-30 17:27:36 -0800474 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700475 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800476 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700477 Rule: blueprint.Phony,
478 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700479 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800480 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700481 })
482 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700483 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700484 }
485
486 if len(allCheckbuildFiles) > 0 {
487 name := ctx.ModuleName() + "-checkbuild"
488 ctx.Build(pctx, blueprint.BuildParams{
489 Rule: blueprint.Phony,
490 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700491 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700492 Optional: true,
493 })
494 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700495 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700496 }
497
498 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800499 suffix := ""
500 if ctx.Config().(Config).EmbeddedInMake() {
501 suffix = "-soong"
502 }
503
Colin Cross9454bfa2015-03-17 13:24:18 -0700504 ctx.Build(pctx, blueprint.BuildParams{
505 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800506 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700507 Implicits: deps,
508 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800509 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700510
511 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800512 }
513}
514
Colin Cross635c3b02016-05-18 15:37:25 -0700515func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700516 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700517 target: a.commonProperties.CompileTarget,
518 targetPrimary: a.commonProperties.CompilePrimary,
Dan Willemsenaa118f92017-04-06 12:49:58 -0700519 vendor: a.commonProperties.Proprietary || a.commonProperties.Vendor,
Colin Cross8b74d172016-09-13 09:59:14 -0700520 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800521 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800522}
523
Colin Cross635c3b02016-05-18 15:37:25 -0700524func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800525 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700526 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700527 ModuleContext: ctx,
528 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
529 installDeps: a.computeInstallDeps(ctx),
530 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800531 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800532 }
533
Colin Cross67a5c132017-05-09 13:45:28 -0700534 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
535 var suffix []string
536 if androidCtx.Os().Class != Device && androidCtx.Os().Class != Generic {
537 suffix = append(suffix, androidCtx.Os().String())
538 }
539 if !androidCtx.PrimaryArch() {
540 suffix = append(suffix, androidCtx.Arch().ArchType.String())
541 }
542
543 ctx.Variable(pctx, "moduleDesc", desc)
544
545 s := ""
546 if len(suffix) > 0 {
547 s = " [" + strings.Join(suffix, " ") + "]"
548 }
549 ctx.Variable(pctx, "moduleDescSuffix", s)
550
Colin Cross9b1d13d2016-09-19 15:18:11 -0700551 if a.Enabled() {
552 a.module.GenerateAndroidBuildActions(androidCtx)
553 if ctx.Failed() {
554 return
555 }
556
557 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
558 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800559 }
560
Colin Cross9b1d13d2016-09-19 15:18:11 -0700561 if a == ctx.FinalModule().(Module).base() {
562 a.generateModuleTarget(ctx)
563 if ctx.Failed() {
564 return
565 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800566 }
Colin Crosscec81712017-07-13 14:43:27 -0700567
568 a.buildParams = androidCtx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800569}
570
Colin Crossf6566ed2015-03-24 11:13:38 -0700571type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700572 target Target
573 targetPrimary bool
574 debug bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700575 vendor bool
Colin Cross8b74d172016-09-13 09:59:14 -0700576 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700577}
578
Colin Cross3f40fa42015-01-30 17:27:36 -0800579type androidModuleContext struct {
580 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700581 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700582 installDeps Paths
583 installFiles Paths
584 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800585 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700586 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700587
588 // For tests
589 buildParams []ModuleBuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800590}
591
Colin Cross67a5c132017-05-09 13:45:28 -0700592func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross6ff51382015-12-17 16:39:19 -0800593 a.ModuleContext.Build(pctx, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700594 Rule: ErrorRule,
595 Description: desc,
596 Outputs: outputs,
597 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800598 Args: map[string]string{
599 "error": err.Error(),
600 },
601 })
602 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800603}
604
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800605func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Cross7f19f372016-11-01 11:10:25 -0700606 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700607 a.ninjaError(params.Description, params.Outputs,
608 fmt.Errorf("module %s missing dependencies: %s\n",
609 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800610 return
611 }
612
Colin Cross3f40fa42015-01-30 17:27:36 -0800613 params.Optional = true
614 a.ModuleContext.Build(pctx, params)
615}
616
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700617func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
Colin Crosscec81712017-07-13 14:43:27 -0700618 if a.config.captureBuild {
619 a.buildParams = append(a.buildParams, params)
620 }
621
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700622 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700623 Rule: params.Rule,
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 Cross67a5c132017-05-09 13:45:28 -0700634 if params.Description != "" {
635 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
636 }
637
Colin Cross33bfb0a2016-11-21 17:23:08 -0800638 if params.Depfile != nil {
639 bparams.Depfile = params.Depfile.String()
640 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700641 if params.Output != nil {
642 bparams.Outputs = append(bparams.Outputs, params.Output.String())
643 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700644 if params.ImplicitOutput != nil {
645 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
646 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700647 if params.Input != nil {
648 bparams.Inputs = append(bparams.Inputs, params.Input.String())
649 }
650 if params.Implicit != nil {
651 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
652 }
653
Colin Cross6ff51382015-12-17 16:39:19 -0800654 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700655 a.ninjaError(bparams.Description, bparams.Outputs,
656 fmt.Errorf("module %s missing dependencies: %s\n",
657 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800658 return
659 }
660
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700661 a.ModuleContext.Build(pctx, bparams)
662}
663
Colin Cross6ff51382015-12-17 16:39:19 -0800664func (a *androidModuleContext) GetMissingDependencies() []string {
665 return a.missingDeps
666}
667
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800668func (a *androidModuleContext) AddMissingDependencies(deps []string) {
669 if deps != nil {
670 a.missingDeps = append(a.missingDeps, deps...)
671 }
672}
673
Colin Crossa1ad8d12016-06-01 17:09:44 -0700674func (a *androidBaseContextImpl) Target() Target {
675 return a.target
676}
677
Colin Cross8b74d172016-09-13 09:59:14 -0700678func (a *androidBaseContextImpl) TargetPrimary() bool {
679 return a.targetPrimary
680}
681
Colin Crossf6566ed2015-03-24 11:13:38 -0700682func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700683 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800684}
685
Colin Crossa1ad8d12016-06-01 17:09:44 -0700686func (a *androidBaseContextImpl) Os() OsType {
687 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800688}
689
Colin Crossf6566ed2015-03-24 11:13:38 -0700690func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700691 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700692}
693
694func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700695 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700696}
697
Colin Cross0af4b842015-04-30 16:36:18 -0700698func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700699 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700700}
701
Colin Cross3edeee12017-04-04 12:59:48 -0700702func (a *androidBaseContextImpl) Windows() bool {
703 return a.target.Os == Windows
704}
705
Colin Crossf6566ed2015-03-24 11:13:38 -0700706func (a *androidBaseContextImpl) Debug() bool {
707 return a.debug
708}
709
Colin Cross1e7d3702016-08-24 15:25:47 -0700710func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700711 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
712 return true
713 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700714 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
715}
716
Colin Cross1332b002015-04-07 17:11:30 -0700717func (a *androidBaseContextImpl) AConfig() Config {
718 return a.config
719}
720
Colin Cross9272ade2016-08-17 15:24:12 -0700721func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
722 return DeviceConfig{a.config.deviceConfig}
723}
724
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700725func (a *androidBaseContextImpl) InstallOnVendorPartition() bool {
Dan Willemsenaa118f92017-04-06 12:49:58 -0700726 return a.vendor
Dan Willemsen782a2d12015-12-21 14:55:28 -0800727}
728
Colin Cross8d8f8e22016-08-03 11:57:50 -0700729func (a *androidModuleContext) InstallInData() bool {
730 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800731}
732
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700733func (a *androidModuleContext) InstallInSanitizerDir() bool {
734 return a.module.InstallInSanitizerDir()
735}
736
Colin Cross893d8162017-04-26 17:34:03 -0700737func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
738 if a.module.base().commonProperties.SkipInstall {
739 return true
740 }
741
742 if a.Device() {
743 if a.AConfig().SkipDeviceInstall() {
744 return true
745 }
746
747 if a.AConfig().SkipMegaDeviceInstall(fullInstallPath.String()) {
748 return true
749 }
750 }
751
752 return false
753}
754
Colin Cross5c517922017-08-31 12:29:17 -0700755func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700756 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -0700757 return a.installFile(installPath, name, srcPath, Cp, deps)
758}
759
760func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
761 deps ...Path) OutputPath {
762 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
763}
764
765func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
766 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700767
Dan Willemsen782a2d12015-12-21 14:55:28 -0800768 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700769 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800770
Colin Cross893d8162017-04-26 17:34:03 -0700771 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700772
Dan Willemsen322acaf2016-01-12 23:07:05 -0800773 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700774
Colin Cross89562dc2016-10-03 17:47:19 -0700775 var implicitDeps, orderOnlyDeps Paths
776
777 if a.Host() {
778 // Installed host modules might be used during the build, depend directly on their
779 // dependencies so their timestamp is updated whenever their dependency is updated
780 implicitDeps = deps
781 } else {
782 orderOnlyDeps = deps
783 }
784
Dan Willemsen322acaf2016-01-12 23:07:05 -0800785 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700786 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700787 Description: "install " + fullInstallPath.Base(),
788 Output: fullInstallPath,
789 Input: srcPath,
790 Implicits: implicitDeps,
791 OrderOnly: orderOnlyDeps,
792 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800793 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800794
Dan Willemsen322acaf2016-01-12 23:07:05 -0800795 a.installFiles = append(a.installFiles, fullInstallPath)
796 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700797 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700798 return fullInstallPath
799}
800
Colin Cross3854a602016-01-11 12:49:11 -0800801func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
802 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700803 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800804
Colin Cross893d8162017-04-26 17:34:03 -0700805 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700806
Colin Cross12fc4972016-01-11 12:49:11 -0800807 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700808 Rule: Symlink,
809 Description: "install symlink " + fullInstallPath.Base(),
810 Output: fullInstallPath,
811 OrderOnly: Paths{srcPath},
812 Default: !a.AConfig().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -0800813 Args: map[string]string{
814 "fromPath": srcPath.String(),
815 },
816 })
Colin Cross3854a602016-01-11 12:49:11 -0800817
Colin Cross12fc4972016-01-11 12:49:11 -0800818 a.installFiles = append(a.installFiles, fullInstallPath)
819 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
820 }
Colin Cross3854a602016-01-11 12:49:11 -0800821 return fullInstallPath
822}
823
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700824func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800825 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
826}
827
Colin Cross3f40fa42015-01-30 17:27:36 -0800828type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700829 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800830}
831
832func isFileInstaller(m blueprint.Module) bool {
833 _, ok := m.(fileInstaller)
834 return ok
835}
836
837func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700838 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800839 return ok
840}
Colin Crossfce53272015-04-08 11:21:40 -0700841
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700842func findStringInSlice(str string, slice []string) int {
843 for i, s := range slice {
844 if s == str {
845 return i
Colin Crossfce53272015-04-08 11:21:40 -0700846 }
847 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700848 return -1
849}
850
Colin Cross068e0fe2016-12-13 15:23:47 -0800851func SrcIsModule(s string) string {
852 if len(s) > 1 && s[0] == ':' {
853 return s[1:]
854 }
855 return ""
856}
857
858type sourceDependencyTag struct {
859 blueprint.BaseDependencyTag
860}
861
862var SourceDepTag sourceDependencyTag
863
864// Returns a list of modules that must be depended on to satisfy filegroup or generated sources
865// modules listed in srcFiles using ":module" syntax
866func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
867 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -0700868 set := make(map[string]bool)
869
Colin Cross068e0fe2016-12-13 15:23:47 -0800870 for _, s := range srcFiles {
871 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -0700872 if _, found := set[m]; found {
873 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
874 } else {
875 set[m] = true
876 deps = append(deps, m)
877 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800878 }
879 }
880
881 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
882}
883
884type SourceFileProducer interface {
885 Srcs() Paths
886}
887
888// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800889// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700890func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800891 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
892}
893
894func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700895 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800896
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700897 for i, e := range excludes {
898 j := findStringInSlice(e, srcFiles)
899 if j != -1 {
900 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
901 }
902
903 excludes[i] = filepath.Join(prefix, e)
904 }
905
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800906 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700907 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -0800908 if m := SrcIsModule(s); m != "" {
909 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
910 if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800911 expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800912 } else {
913 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
914 }
915 } else if pathtools.IsGlob(s) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800916 globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
Colin Cross05a39cb2017-10-09 13:35:19 -0700917 for i, s := range globbedSrcFiles {
918 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800919 }
Colin Cross05a39cb2017-10-09 13:35:19 -0700920 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -0700921 } else {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800922 s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
923 expandedSrcFiles = append(expandedSrcFiles, s)
Colin Cross8f101b42015-06-17 15:09:06 -0700924 }
925 }
926
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800927 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -0700928}
929
Nan Zhang6d34b302017-02-04 17:47:46 -0800930func (ctx *androidModuleContext) RequiredModuleNames() []string {
931 return ctx.module.base().commonProperties.Required
932}
933
Colin Cross7f19f372016-11-01 11:10:25 -0700934func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
935 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700936 if err != nil {
937 ctx.ModuleErrorf("glob: %s", err.Error())
938 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700939 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700940}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700941
Colin Cross463a90e2015-06-17 14:20:06 -0700942func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700943 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -0700944}
945
Colin Cross1f8c52b2015-06-16 16:38:17 -0700946func BuildTargetSingleton() blueprint.Singleton {
947 return &buildTargetSingleton{}
948}
949
Colin Cross87d8b562017-04-25 10:01:55 -0700950func parentDir(dir string) string {
951 dir, _ = filepath.Split(dir)
952 return filepath.Clean(dir)
953}
954
Colin Cross1f8c52b2015-06-16 16:38:17 -0700955type buildTargetSingleton struct{}
956
957func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
958 checkbuildDeps := []string{}
959
Colin Cross87d8b562017-04-25 10:01:55 -0700960 mmTarget := func(dir string) string {
Dan Willemsend2e95fb2017-09-20 14:30:50 -0700961 return "MODULES-IN-" + strings.Replace(filepath.Clean(dir), "/", "-", -1)
Colin Cross87d8b562017-04-25 10:01:55 -0700962 }
963
964 modulesInDir := make(map[string][]string)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700965
966 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700967 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700968 blueprintDir := a.base().blueprintDir
969 installTarget := a.base().installTarget
970 checkbuildTarget := a.base().checkbuildTarget
971
972 if checkbuildTarget != "" {
973 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
Colin Cross87d8b562017-04-25 10:01:55 -0700974 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700975 }
976
977 if installTarget != "" {
Colin Cross87d8b562017-04-25 10:01:55 -0700978 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700979 }
980 }
981 })
982
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800983 suffix := ""
984 if ctx.Config().(Config).EmbeddedInMake() {
985 suffix = "-soong"
986 }
987
Colin Cross1f8c52b2015-06-16 16:38:17 -0700988 // Create a top-level checkbuild target that depends on all modules
989 ctx.Build(pctx, blueprint.BuildParams{
990 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800991 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700992 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700993 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700994 })
995
Dan Willemsend2e95fb2017-09-20 14:30:50 -0700996 // Make will generate the MODULES-IN-* targets
997 if ctx.Config().(Config).EmbeddedInMake() {
998 return
999 }
1000
Colin Cross87d8b562017-04-25 10:01:55 -07001001 // Ensure ancestor directories are in modulesInDir
1002 dirs := sortedKeys(modulesInDir)
1003 for _, dir := range dirs {
1004 dir := parentDir(dir)
1005 for dir != "." && dir != "/" {
1006 if _, exists := modulesInDir[dir]; exists {
1007 break
1008 }
1009 modulesInDir[dir] = nil
1010 dir = parentDir(dir)
1011 }
1012 }
1013
1014 // Make directories build their direct subdirectories
1015 dirs = sortedKeys(modulesInDir)
1016 for _, dir := range dirs {
1017 p := parentDir(dir)
1018 if p != "." && p != "/" {
1019 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1020 }
1021 }
1022
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001023 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1024 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1025 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001026 for _, dir := range dirs {
1027 ctx.Build(pctx, blueprint.BuildParams{
1028 Rule: blueprint.Phony,
Colin Cross87d8b562017-04-25 10:01:55 -07001029 Outputs: []string{mmTarget(dir)},
1030 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001031 // HACK: checkbuild should be an optional build, but force it
1032 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -08001033 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001034 })
1035 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001036
1037 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1038 osDeps := map[OsType]Paths{}
1039 ctx.VisitAllModules(func(module blueprint.Module) {
1040 if a, ok := module.(Module); ok {
1041 if a.Enabled() {
1042 os := a.Target().Os
1043 osDeps[os] = append(osDeps[os], a.base().checkbuildFiles...)
1044 }
1045 }
1046 })
1047
1048 osClass := make(map[string][]string)
1049 for os, deps := range osDeps {
1050 var className string
1051
1052 switch os.Class {
1053 case Host:
1054 className = "host"
1055 case HostCross:
1056 className = "host-cross"
1057 case Device:
1058 className = "target"
1059 default:
1060 continue
1061 }
1062
1063 name := className + "-" + os.Name
1064 osClass[className] = append(osClass[className], name)
1065
1066 ctx.Build(pctx, blueprint.BuildParams{
1067 Rule: blueprint.Phony,
1068 Outputs: []string{name},
1069 Implicits: deps.Strings(),
1070 Optional: true,
1071 })
1072 }
1073
1074 // Wrap those into host|host-cross|target phony rules
1075 osClasses := sortedKeys(osClass)
1076 for _, class := range osClasses {
1077 ctx.Build(pctx, blueprint.BuildParams{
1078 Rule: blueprint.Phony,
1079 Outputs: []string{class},
1080 Implicits: osClass[class],
1081 Optional: true,
1082 })
1083 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001084}
Colin Crossd779da42015-12-17 18:00:23 -08001085
1086type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001087 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -08001088 ctx interface {
1089 ModuleName(blueprint.Module) string
1090 ModuleSubDir(blueprint.Module) string
1091 }
1092}
1093
1094func (s AndroidModulesByName) Len() int { return len(s.slice) }
1095func (s AndroidModulesByName) Less(i, j int) bool {
1096 mi, mj := s.slice[i], s.slice[j]
1097 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1098
1099 if ni != nj {
1100 return ni < nj
1101 } else {
1102 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1103 }
1104}
1105func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }