blob: 66859fa56c40a3a218ad5efa5cbfb5c29c29c0e1 [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
Colin Crossae887032017-10-23 17:16:14 -070035type BuildParams 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 Crossae887032017-10-23 17:16:14 -070053type ModuleBuildParams BuildParams
54
Colin Crossf6566ed2015-03-24 11:13:38 -070055type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070056 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070057 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070058 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070059 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070060 Host() bool
61 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070062 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070063 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070064 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070065 PrimaryArch() bool
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070066 InstallOnVendorPartition() bool
Colin Cross1332b002015-04-07 17:11:30 -070067 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070068 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070069}
70
Colin Cross635c3b02016-05-18 15:37:25 -070071type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070072 blueprint.BaseModuleContext
73 androidBaseContext
74}
75
Colin Cross635c3b02016-05-18 15:37:25 -070076type ModuleContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070077 androidBaseContext
Colin Cross3f68a132017-10-23 17:10:29 -070078 blueprint.BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -080079
Colin Crossae887032017-10-23 17:16:14 -070080 // Deprecated: use ModuleContext.Build instead.
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070082
Dan Willemsen34cc69e2015-09-23 15:26:20 -070083 ExpandSources(srcFiles, excludes []string) Paths
Colin Crossfaeb7aa2017-02-01 14:12:44 -080084 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -070085 Glob(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070086
Colin Cross5c517922017-08-31 12:29:17 -070087 InstallExecutable(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
88 InstallFile(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080089 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070090 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080091
92 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070093
Colin Cross8d8f8e22016-08-03 11:57:50 -070094 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -070095 InstallInSanitizerDir() bool
Nan Zhang6d34b302017-02-04 17:47:46 -080096
97 RequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -070098
99 // android.ModuleContext methods
100 // These are duplicated instead of embedded so that can eventually be wrapped to take an
101 // android.Module instead of a blueprint.Module
102 OtherModuleName(m blueprint.Module) string
103 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
104 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
105
106 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
107 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
108
109 ModuleSubDir() string
110
Colin Cross35143d02017-11-16 00:11:20 -0800111 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700112 VisitDirectDeps(visit func(Module))
113 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
114 VisitDepsDepthFirst(visit func(Module))
115 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
116 WalkDeps(visit func(Module, Module) bool)
Colin Cross3f68a132017-10-23 17:10:29 -0700117
118 Variable(pctx blueprint.PackageContext, name, value string)
119 Rule(pctx blueprint.PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700120 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
121 // and performs more verification.
122 Build(pctx blueprint.PackageContext, params BuildParams)
Colin Cross3f68a132017-10-23 17:10:29 -0700123
124 PrimaryModule() blueprint.Module
125 FinalModule() blueprint.Module
126 VisitAllModuleVariants(visit func(blueprint.Module))
127
128 GetMissingDependencies() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800129}
130
Colin Cross635c3b02016-05-18 15:37:25 -0700131type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800132 blueprint.Module
133
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700134 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
135 // but GenerateAndroidBuildActions also has access to Android-specific information.
136 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700137 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700138
Colin Cross1e676be2016-10-12 14:38:15 -0700139 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800140
Colin Cross635c3b02016-05-18 15:37:25 -0700141 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800142 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700143 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800144 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700145 InstallInSanitizerDir() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800146 SkipInstall()
Colin Cross36242852017-06-23 15:06:31 -0700147
148 AddProperties(props ...interface{})
149 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700150
Colin Crossae887032017-10-23 17:16:14 -0700151 BuildParamsForTests() []BuildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800152}
153
Colin Crossfc754582016-05-17 16:34:16 -0700154type nameProperties struct {
155 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800156 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700157}
158
159type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700160 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800161
Dan Willemsen0effe062015-11-30 16:06:01 -0800162 // emit build rules for this module
163 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800164
Colin Cross7d5136f2015-05-11 13:39:40 -0700165 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800166 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
167 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
168 // platform
Colin Cross7d716ba2017-11-01 10:38:29 -0700169 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700170
171 Target struct {
172 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700173 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700174 }
175 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700176 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700177 }
178 }
179
180 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800181
Dan Willemsen782a2d12015-12-21 14:55:28 -0800182 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700183 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800184
Colin Cross55708f32017-03-20 13:23:34 -0700185 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700186 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700187
Dan Willemsenaa118f92017-04-06 12:49:58 -0700188 // whether this module is device specific and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700189 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700190
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700191 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
192 // file
193 Logtags []string
194
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700195 // init.rc files to be installed if this module is installed
196 Init_rc []string
197
Chris Wolfe998306e2016-08-15 14:47:23 -0400198 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700199 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400200
Colin Cross5aac3622017-08-31 15:07:09 -0700201 // relative path to a file to include in the list of notices for the device
202 Notice *string
203
Colin Crossa1ad8d12016-06-01 17:09:44 -0700204 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700205 CompileTarget Target `blueprint:"mutated"`
206 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800207
208 // Set by InitAndroidModule
209 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700210 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700211
212 SkipInstall bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800213}
214
215type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700216 Host_supported *bool
217 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800218}
219
Colin Crossc472d572015-03-17 15:06:21 -0700220type Multilib string
221
222const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700223 MultilibBoth Multilib = "both"
224 MultilibFirst Multilib = "first"
225 MultilibCommon Multilib = "common"
226 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700227)
228
Colin Crossa1ad8d12016-06-01 17:09:44 -0700229type HostOrDeviceSupported int
230
231const (
232 _ HostOrDeviceSupported = iota
233 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700234 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700235 DeviceSupported
236 HostAndDeviceSupported
237 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700238 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700239)
240
Colin Cross36242852017-06-23 15:06:31 -0700241func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800242 base := m.base()
243 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700244
Colin Cross36242852017-06-23 15:06:31 -0700245 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700246 &base.nameProperties,
247 &base.commonProperties,
248 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700249}
250
Colin Cross36242852017-06-23 15:06:31 -0700251func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
252 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700253
254 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800255 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700256 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700257 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800258
Dan Willemsen218f6562015-07-08 18:13:11 -0700259 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700260 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700261 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800262 }
263
Colin Cross36242852017-06-23 15:06:31 -0700264 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800265}
266
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800267// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800268// modules. It should be included as an anonymous field in every module
269// struct definition. InitAndroidModule should then be called from the module's
270// factory function, and the return values from InitAndroidModule should be
271// returned from the factory function.
272//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800273// The ModuleBase type is responsible for implementing the GenerateBuildActions
274// method to support the blueprint.Module interface. This method will then call
275// the module's GenerateAndroidBuildActions method once for each build variant
276// that is to be built. GenerateAndroidBuildActions is passed a
277// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800278// AndroidModuleContext exposes extra functionality specific to the Android build
279// system including details about the particular build variant that is to be
280// generated.
281//
282// For example:
283//
284// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800285// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800286// )
287//
288// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800289// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800290// properties struct {
291// MyProperty string
292// }
293// }
294//
Colin Cross36242852017-06-23 15:06:31 -0700295// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800296// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700297// m.AddProperties(&m.properties)
298// android.InitAndroidModule(m)
299// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800300// }
301//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800302// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800303// // Get the CPU architecture for the current build variant.
304// variantArch := ctx.Arch()
305//
306// // ...
307// }
Colin Cross635c3b02016-05-18 15:37:25 -0700308type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800309 // Putting the curiously recurring thing pointing to the thing that contains
310 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700311 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700312 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800313
Colin Crossfc754582016-05-17 16:34:16 -0700314 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800315 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700316 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800317 hostAndDeviceProperties hostAndDeviceProperties
318 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700319 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700320 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800321
322 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700323 installFiles Paths
324 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700325
326 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
327 // Only set on the final variant of each module
328 installTarget string
329 checkbuildTarget string
330 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700331
Colin Cross178a5092016-09-13 13:42:32 -0700332 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700333
334 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700335
336 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700337 buildParams []BuildParams
Colin Cross36242852017-06-23 15:06:31 -0700338}
339
340func (a *ModuleBase) AddProperties(props ...interface{}) {
341 a.registerProps = append(a.registerProps, props...)
342}
343
344func (a *ModuleBase) GetProperties() []interface{} {
345 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800346}
347
Colin Crossae887032017-10-23 17:16:14 -0700348func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700349 return a.buildParams
350}
351
Colin Crossce75d2c2016-10-06 16:12:58 -0700352// Name returns the name of the module. It may be overridden by individual module types, for
353// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700354func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800355 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700356}
357
Colin Crossce75d2c2016-10-06 16:12:58 -0700358// BaseModuleName returns the name of the module as specified in the blueprints file.
359func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800360 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700361}
362
Colin Cross635c3b02016-05-18 15:37:25 -0700363func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800364 return a
365}
366
Colin Cross8b74d172016-09-13 09:59:14 -0700367func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700368 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700369 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700370}
371
Colin Crossa1ad8d12016-06-01 17:09:44 -0700372func (a *ModuleBase) Target() Target {
373 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800374}
375
Colin Cross8b74d172016-09-13 09:59:14 -0700376func (a *ModuleBase) TargetPrimary() bool {
377 return a.commonProperties.CompilePrimary
378}
379
Colin Crossa1ad8d12016-06-01 17:09:44 -0700380func (a *ModuleBase) Os() OsType {
381 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800382}
383
Colin Cross635c3b02016-05-18 15:37:25 -0700384func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700385 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800386}
387
Colin Cross635c3b02016-05-18 15:37:25 -0700388func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700389 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800390}
391
Dan Willemsen0b24c742016-10-04 15:13:37 -0700392func (a *ModuleBase) ArchSpecific() bool {
393 return a.commonProperties.ArchSpecific
394}
395
Colin Crossa1ad8d12016-06-01 17:09:44 -0700396func (a *ModuleBase) OsClassSupported() []OsClass {
397 switch a.commonProperties.HostOrDeviceSupported {
398 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700399 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700400 case HostSupportedNoCross:
401 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700402 case DeviceSupported:
403 return []OsClass{Device}
404 case HostAndDeviceSupported:
405 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700406 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700407 supported = append(supported, Host, HostCross)
408 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700409 if a.hostAndDeviceProperties.Device_supported == nil ||
410 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700411 supported = append(supported, Device)
412 }
413 return supported
414 default:
415 return nil
416 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800417}
418
Colin Cross635c3b02016-05-18 15:37:25 -0700419func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800420 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
421 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700422 (a.hostAndDeviceProperties.Device_supported == nil ||
423 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800424}
425
Colin Cross635c3b02016-05-18 15:37:25 -0700426func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800427 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800428 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800429 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800430 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800431}
432
Colin Crossce75d2c2016-10-06 16:12:58 -0700433func (a *ModuleBase) SkipInstall() {
434 a.commonProperties.SkipInstall = true
435}
436
Colin Cross635c3b02016-05-18 15:37:25 -0700437func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700438 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800439
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700440 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800441 ctx.VisitDepsDepthFirstIf(isFileInstaller,
442 func(m blueprint.Module) {
443 fileInstaller := m.(fileInstaller)
444 files := fileInstaller.filesToInstall()
445 result = append(result, files...)
446 })
447
448 return result
449}
450
Colin Cross635c3b02016-05-18 15:37:25 -0700451func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800452 return a.installFiles
453}
454
Colin Cross635c3b02016-05-18 15:37:25 -0700455func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800456 return p.noAddressSanitizer
457}
458
Colin Cross635c3b02016-05-18 15:37:25 -0700459func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800460 return false
461}
462
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700463func (p *ModuleBase) InstallInSanitizerDir() bool {
464 return false
465}
466
Colin Cross635c3b02016-05-18 15:37:25 -0700467func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700468 allInstalledFiles := Paths{}
469 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700471 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700472 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
473 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800474 })
475
Colin Cross9454bfa2015-03-17 13:24:18 -0700476 deps := []string{}
477
Colin Cross3f40fa42015-01-30 17:27:36 -0800478 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700479 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800480 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700481 Rule: blueprint.Phony,
482 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700483 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800484 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700485 })
486 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700487 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700488 }
489
490 if len(allCheckbuildFiles) > 0 {
491 name := ctx.ModuleName() + "-checkbuild"
492 ctx.Build(pctx, blueprint.BuildParams{
493 Rule: blueprint.Phony,
494 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700495 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700496 Optional: true,
497 })
498 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700499 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700500 }
501
502 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800503 suffix := ""
504 if ctx.Config().(Config).EmbeddedInMake() {
505 suffix = "-soong"
506 }
507
Colin Cross9454bfa2015-03-17 13:24:18 -0700508 ctx.Build(pctx, blueprint.BuildParams{
509 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800510 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700511 Implicits: deps,
512 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800513 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700514
515 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800516 }
517}
518
Colin Cross635c3b02016-05-18 15:37:25 -0700519func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700520 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700521 target: a.commonProperties.CompileTarget,
522 targetPrimary: a.commonProperties.CompilePrimary,
Colin Cross7d716ba2017-11-01 10:38:29 -0700523 vendor: Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Vendor),
Colin Cross8b74d172016-09-13 09:59:14 -0700524 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800525 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800526}
527
Colin Cross635c3b02016-05-18 15:37:25 -0700528func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800529 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700530 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700531 ModuleContext: ctx,
532 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
533 installDeps: a.computeInstallDeps(ctx),
534 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800535 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800536 }
537
Colin Cross67a5c132017-05-09 13:45:28 -0700538 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
539 var suffix []string
540 if androidCtx.Os().Class != Device && androidCtx.Os().Class != Generic {
541 suffix = append(suffix, androidCtx.Os().String())
542 }
543 if !androidCtx.PrimaryArch() {
544 suffix = append(suffix, androidCtx.Arch().ArchType.String())
545 }
546
547 ctx.Variable(pctx, "moduleDesc", desc)
548
549 s := ""
550 if len(suffix) > 0 {
551 s = " [" + strings.Join(suffix, " ") + "]"
552 }
553 ctx.Variable(pctx, "moduleDescSuffix", s)
554
Colin Cross9b1d13d2016-09-19 15:18:11 -0700555 if a.Enabled() {
556 a.module.GenerateAndroidBuildActions(androidCtx)
557 if ctx.Failed() {
558 return
559 }
560
561 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
562 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800563 }
564
Colin Cross9b1d13d2016-09-19 15:18:11 -0700565 if a == ctx.FinalModule().(Module).base() {
566 a.generateModuleTarget(ctx)
567 if ctx.Failed() {
568 return
569 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 }
Colin Crosscec81712017-07-13 14:43:27 -0700571
572 a.buildParams = androidCtx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800573}
574
Colin Crossf6566ed2015-03-24 11:13:38 -0700575type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700576 target Target
577 targetPrimary bool
578 debug bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700579 vendor bool
Colin Cross8b74d172016-09-13 09:59:14 -0700580 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700581}
582
Colin Cross3f40fa42015-01-30 17:27:36 -0800583type androidModuleContext struct {
584 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700585 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700586 installDeps Paths
587 installFiles Paths
588 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800589 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700590 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700591
592 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700593 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800594}
595
Colin Cross67a5c132017-05-09 13:45:28 -0700596func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross6ff51382015-12-17 16:39:19 -0800597 a.ModuleContext.Build(pctx, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700598 Rule: ErrorRule,
599 Description: desc,
600 Outputs: outputs,
601 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800602 Args: map[string]string{
603 "error": err.Error(),
604 },
605 })
606 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800607}
608
Colin Crossae887032017-10-23 17:16:14 -0700609func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
610 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800611}
612
Colin Crossae887032017-10-23 17:16:14 -0700613func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params BuildParams) {
Colin Crosscec81712017-07-13 14:43:27 -0700614 if a.config.captureBuild {
615 a.buildParams = append(a.buildParams, params)
616 }
617
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700618 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700619 Rule: params.Rule,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800620 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700621 Outputs: params.Outputs.Strings(),
622 ImplicitOutputs: params.ImplicitOutputs.Strings(),
623 Inputs: params.Inputs.Strings(),
624 Implicits: params.Implicits.Strings(),
625 OrderOnly: params.OrderOnly.Strings(),
626 Args: params.Args,
627 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700628 }
629
Colin Cross67a5c132017-05-09 13:45:28 -0700630 if params.Description != "" {
631 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
632 }
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 Cross6ff51382015-12-17 16:39:19 -0800650 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700651 a.ninjaError(bparams.Description, bparams.Outputs,
652 fmt.Errorf("module %s missing dependencies: %s\n",
653 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800654 return
655 }
656
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700657 a.ModuleContext.Build(pctx, bparams)
658}
659
Colin Cross6ff51382015-12-17 16:39:19 -0800660func (a *androidModuleContext) GetMissingDependencies() []string {
661 return a.missingDeps
662}
663
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800664func (a *androidModuleContext) AddMissingDependencies(deps []string) {
665 if deps != nil {
666 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700667 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800668 }
669}
670
Colin Crossd11fcda2017-10-23 17:59:01 -0700671func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
672 aModule, _ := module.(Module)
673 if aModule == nil {
674 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
675 return nil
676 }
677
678 if !aModule.Enabled() {
679 if a.AConfig().AllowMissingDependencies() {
680 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
681 } else {
682 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
683 }
684 return nil
685 }
686
687 return aModule
688}
689
Colin Cross35143d02017-11-16 00:11:20 -0800690func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
691 a.ModuleContext.VisitDirectDeps(visit)
692}
693
Colin Crossd11fcda2017-10-23 17:59:01 -0700694func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
695 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
696 if aModule := a.validateAndroidModule(module); aModule != nil {
697 visit(aModule)
698 }
699 })
700}
701
702func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
703 a.ModuleContext.VisitDirectDepsIf(
704 // pred
705 func(module blueprint.Module) bool {
706 if aModule := a.validateAndroidModule(module); aModule != nil {
707 return pred(aModule)
708 } else {
709 return false
710 }
711 },
712 // visit
713 func(module blueprint.Module) {
714 visit(module.(Module))
715 })
716}
717
718func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
719 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
720 if aModule := a.validateAndroidModule(module); aModule != nil {
721 visit(aModule)
722 }
723 })
724}
725
726func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
727 a.ModuleContext.VisitDepsDepthFirstIf(
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) WalkDeps(visit func(Module, Module) bool) {
743 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
744 childAndroidModule := a.validateAndroidModule(child)
745 parentAndroidModule := a.validateAndroidModule(parent)
746 if childAndroidModule != nil && parentAndroidModule != nil {
747 return visit(childAndroidModule, parentAndroidModule)
748 } else {
749 return false
750 }
751 })
752}
753
Colin Crossa1ad8d12016-06-01 17:09:44 -0700754func (a *androidBaseContextImpl) Target() Target {
755 return a.target
756}
757
Colin Cross8b74d172016-09-13 09:59:14 -0700758func (a *androidBaseContextImpl) TargetPrimary() bool {
759 return a.targetPrimary
760}
761
Colin Crossf6566ed2015-03-24 11:13:38 -0700762func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700763 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800764}
765
Colin Crossa1ad8d12016-06-01 17:09:44 -0700766func (a *androidBaseContextImpl) Os() OsType {
767 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800768}
769
Colin Crossf6566ed2015-03-24 11:13:38 -0700770func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700771 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700772}
773
774func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700775 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700776}
777
Colin Cross0af4b842015-04-30 16:36:18 -0700778func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700779 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700780}
781
Colin Cross3edeee12017-04-04 12:59:48 -0700782func (a *androidBaseContextImpl) Windows() bool {
783 return a.target.Os == Windows
784}
785
Colin Crossf6566ed2015-03-24 11:13:38 -0700786func (a *androidBaseContextImpl) Debug() bool {
787 return a.debug
788}
789
Colin Cross1e7d3702016-08-24 15:25:47 -0700790func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700791 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
792 return true
793 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700794 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
795}
796
Colin Cross1332b002015-04-07 17:11:30 -0700797func (a *androidBaseContextImpl) AConfig() Config {
798 return a.config
799}
800
Colin Cross9272ade2016-08-17 15:24:12 -0700801func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
802 return DeviceConfig{a.config.deviceConfig}
803}
804
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700805func (a *androidBaseContextImpl) InstallOnVendorPartition() bool {
Dan Willemsenaa118f92017-04-06 12:49:58 -0700806 return a.vendor
Dan Willemsen782a2d12015-12-21 14:55:28 -0800807}
808
Colin Cross8d8f8e22016-08-03 11:57:50 -0700809func (a *androidModuleContext) InstallInData() bool {
810 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800811}
812
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700813func (a *androidModuleContext) InstallInSanitizerDir() bool {
814 return a.module.InstallInSanitizerDir()
815}
816
Colin Cross893d8162017-04-26 17:34:03 -0700817func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
818 if a.module.base().commonProperties.SkipInstall {
819 return true
820 }
821
822 if a.Device() {
823 if a.AConfig().SkipDeviceInstall() {
824 return true
825 }
826
827 if a.AConfig().SkipMegaDeviceInstall(fullInstallPath.String()) {
828 return true
829 }
830 }
831
832 return false
833}
834
Colin Cross5c517922017-08-31 12:29:17 -0700835func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700836 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -0700837 return a.installFile(installPath, name, srcPath, Cp, deps)
838}
839
840func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
841 deps ...Path) OutputPath {
842 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
843}
844
845func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
846 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700847
Dan Willemsen782a2d12015-12-21 14:55:28 -0800848 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700849 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800850
Colin Cross893d8162017-04-26 17:34:03 -0700851 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700852
Dan Willemsen322acaf2016-01-12 23:07:05 -0800853 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700854
Colin Cross89562dc2016-10-03 17:47:19 -0700855 var implicitDeps, orderOnlyDeps Paths
856
857 if a.Host() {
858 // Installed host modules might be used during the build, depend directly on their
859 // dependencies so their timestamp is updated whenever their dependency is updated
860 implicitDeps = deps
861 } else {
862 orderOnlyDeps = deps
863 }
864
Colin Crossae887032017-10-23 17:16:14 -0700865 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700866 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700867 Description: "install " + fullInstallPath.Base(),
868 Output: fullInstallPath,
869 Input: srcPath,
870 Implicits: implicitDeps,
871 OrderOnly: orderOnlyDeps,
872 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800873 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800874
Dan Willemsen322acaf2016-01-12 23:07:05 -0800875 a.installFiles = append(a.installFiles, fullInstallPath)
876 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700877 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700878 return fullInstallPath
879}
880
Colin Cross3854a602016-01-11 12:49:11 -0800881func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
882 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700883 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800884
Colin Cross893d8162017-04-26 17:34:03 -0700885 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700886
Colin Crossae887032017-10-23 17:16:14 -0700887 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700888 Rule: Symlink,
889 Description: "install symlink " + fullInstallPath.Base(),
890 Output: fullInstallPath,
891 OrderOnly: Paths{srcPath},
892 Default: !a.AConfig().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -0800893 Args: map[string]string{
894 "fromPath": srcPath.String(),
895 },
896 })
Colin Cross3854a602016-01-11 12:49:11 -0800897
Colin Cross12fc4972016-01-11 12:49:11 -0800898 a.installFiles = append(a.installFiles, fullInstallPath)
899 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
900 }
Colin Cross3854a602016-01-11 12:49:11 -0800901 return fullInstallPath
902}
903
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700904func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800905 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
906}
907
Colin Cross3f40fa42015-01-30 17:27:36 -0800908type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700909 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800910}
911
912func isFileInstaller(m blueprint.Module) bool {
913 _, ok := m.(fileInstaller)
914 return ok
915}
916
917func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700918 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800919 return ok
920}
Colin Crossfce53272015-04-08 11:21:40 -0700921
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700922func findStringInSlice(str string, slice []string) int {
923 for i, s := range slice {
924 if s == str {
925 return i
Colin Crossfce53272015-04-08 11:21:40 -0700926 }
927 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700928 return -1
929}
930
Colin Cross068e0fe2016-12-13 15:23:47 -0800931func SrcIsModule(s string) string {
932 if len(s) > 1 && s[0] == ':' {
933 return s[1:]
934 }
935 return ""
936}
937
938type sourceDependencyTag struct {
939 blueprint.BaseDependencyTag
940}
941
942var SourceDepTag sourceDependencyTag
943
944// Returns a list of modules that must be depended on to satisfy filegroup or generated sources
945// modules listed in srcFiles using ":module" syntax
946func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
947 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -0700948 set := make(map[string]bool)
949
Colin Cross068e0fe2016-12-13 15:23:47 -0800950 for _, s := range srcFiles {
951 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -0700952 if _, found := set[m]; found {
953 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
954 } else {
955 set[m] = true
956 deps = append(deps, m)
957 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800958 }
959 }
960
961 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
962}
963
964type SourceFileProducer interface {
965 Srcs() Paths
966}
967
968// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800969// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700970func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800971 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
972}
973
974func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700975 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800976
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700977 for i, e := range excludes {
978 j := findStringInSlice(e, srcFiles)
979 if j != -1 {
980 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
981 }
982
983 excludes[i] = filepath.Join(prefix, e)
984 }
985
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800986 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700987 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -0800988 if m := SrcIsModule(s); m != "" {
989 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -0700990 if module == nil {
991 // Error will have been handled by ExtractSourcesDeps
992 continue
993 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800994 if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800995 expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800996 } else {
997 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
998 }
999 } else if pathtools.IsGlob(s) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001000 globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001001 for i, s := range globbedSrcFiles {
1002 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001003 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001004 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001005 } else {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001006 s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1007 expandedSrcFiles = append(expandedSrcFiles, s)
Colin Cross8f101b42015-06-17 15:09:06 -07001008 }
1009 }
1010
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001011 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001012}
1013
Nan Zhang6d34b302017-02-04 17:47:46 -08001014func (ctx *androidModuleContext) RequiredModuleNames() []string {
1015 return ctx.module.base().commonProperties.Required
1016}
1017
Colin Cross7f19f372016-11-01 11:10:25 -07001018func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1019 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001020 if err != nil {
1021 ctx.ModuleErrorf("glob: %s", err.Error())
1022 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001023 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -07001024}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001025
Colin Cross463a90e2015-06-17 14:20:06 -07001026func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001027 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001028}
1029
Colin Cross1f8c52b2015-06-16 16:38:17 -07001030func BuildTargetSingleton() blueprint.Singleton {
1031 return &buildTargetSingleton{}
1032}
1033
Colin Cross87d8b562017-04-25 10:01:55 -07001034func parentDir(dir string) string {
1035 dir, _ = filepath.Split(dir)
1036 return filepath.Clean(dir)
1037}
1038
Colin Cross1f8c52b2015-06-16 16:38:17 -07001039type buildTargetSingleton struct{}
1040
1041func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
1042 checkbuildDeps := []string{}
1043
Colin Cross87d8b562017-04-25 10:01:55 -07001044 mmTarget := func(dir string) string {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001045 return "MODULES-IN-" + strings.Replace(filepath.Clean(dir), "/", "-", -1)
Colin Cross87d8b562017-04-25 10:01:55 -07001046 }
1047
1048 modulesInDir := make(map[string][]string)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001049
1050 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -07001051 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001052 blueprintDir := a.base().blueprintDir
1053 installTarget := a.base().installTarget
1054 checkbuildTarget := a.base().checkbuildTarget
1055
1056 if checkbuildTarget != "" {
1057 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
Colin Cross87d8b562017-04-25 10:01:55 -07001058 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001059 }
1060
1061 if installTarget != "" {
Colin Cross87d8b562017-04-25 10:01:55 -07001062 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001063 }
1064 }
1065 })
1066
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001067 suffix := ""
1068 if ctx.Config().(Config).EmbeddedInMake() {
1069 suffix = "-soong"
1070 }
1071
Colin Cross1f8c52b2015-06-16 16:38:17 -07001072 // Create a top-level checkbuild target that depends on all modules
1073 ctx.Build(pctx, blueprint.BuildParams{
1074 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001075 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -07001076 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -07001077 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001078 })
1079
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001080 // Make will generate the MODULES-IN-* targets
1081 if ctx.Config().(Config).EmbeddedInMake() {
1082 return
1083 }
1084
Colin Cross87d8b562017-04-25 10:01:55 -07001085 // Ensure ancestor directories are in modulesInDir
1086 dirs := sortedKeys(modulesInDir)
1087 for _, dir := range dirs {
1088 dir := parentDir(dir)
1089 for dir != "." && dir != "/" {
1090 if _, exists := modulesInDir[dir]; exists {
1091 break
1092 }
1093 modulesInDir[dir] = nil
1094 dir = parentDir(dir)
1095 }
1096 }
1097
1098 // Make directories build their direct subdirectories
1099 dirs = sortedKeys(modulesInDir)
1100 for _, dir := range dirs {
1101 p := parentDir(dir)
1102 if p != "." && p != "/" {
1103 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1104 }
1105 }
1106
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001107 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1108 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1109 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001110 for _, dir := range dirs {
1111 ctx.Build(pctx, blueprint.BuildParams{
1112 Rule: blueprint.Phony,
Colin Cross87d8b562017-04-25 10:01:55 -07001113 Outputs: []string{mmTarget(dir)},
1114 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001115 // HACK: checkbuild should be an optional build, but force it
1116 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -08001117 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001118 })
1119 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001120
1121 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1122 osDeps := map[OsType]Paths{}
1123 ctx.VisitAllModules(func(module blueprint.Module) {
1124 if a, ok := module.(Module); ok {
1125 if a.Enabled() {
1126 os := a.Target().Os
1127 osDeps[os] = append(osDeps[os], a.base().checkbuildFiles...)
1128 }
1129 }
1130 })
1131
1132 osClass := make(map[string][]string)
1133 for os, deps := range osDeps {
1134 var className string
1135
1136 switch os.Class {
1137 case Host:
1138 className = "host"
1139 case HostCross:
1140 className = "host-cross"
1141 case Device:
1142 className = "target"
1143 default:
1144 continue
1145 }
1146
1147 name := className + "-" + os.Name
1148 osClass[className] = append(osClass[className], name)
1149
1150 ctx.Build(pctx, blueprint.BuildParams{
1151 Rule: blueprint.Phony,
1152 Outputs: []string{name},
1153 Implicits: deps.Strings(),
1154 Optional: true,
1155 })
1156 }
1157
1158 // Wrap those into host|host-cross|target phony rules
1159 osClasses := sortedKeys(osClass)
1160 for _, class := range osClasses {
1161 ctx.Build(pctx, blueprint.BuildParams{
1162 Rule: blueprint.Phony,
1163 Outputs: []string{class},
1164 Implicits: osClass[class],
1165 Optional: true,
1166 })
1167 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001168}
Colin Crossd779da42015-12-17 18:00:23 -08001169
1170type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001171 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -08001172 ctx interface {
1173 ModuleName(blueprint.Module) string
1174 ModuleSubDir(blueprint.Module) string
1175 }
1176}
1177
1178func (s AndroidModulesByName) Len() int { return len(s.slice) }
1179func (s AndroidModulesByName) Less(i, j int) bool {
1180 mi, mj := s.slice[i], s.slice[j]
1181 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1182
1183 if ni != nj {
1184 return ni < nj
1185 } else {
1186 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1187 }
1188}
1189func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }