blob: 476929a5949b113412c77a34f9808d17b1728d2b [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6ff51382015-12-17 16:39:19 -080018 "fmt"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "path/filepath"
Colin Cross0875c522017-11-28 17:34:01 -080020 "sort"
Colin Cross6ff51382015-12-17 16:39:19 -080021 "strings"
Colin Crossaabf6792017-11-29 00:27:14 -080022 "text/scanner"
Colin Crossf6566ed2015-03-24 11:13:38 -070023
24 "github.com/google/blueprint"
Colin Cross7f19f372016-11-01 11:10:25 -070025 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080026)
27
28var (
29 DeviceSharedLibrary = "shared_library"
30 DeviceStaticLibrary = "static_library"
31 DeviceExecutable = "executable"
32 HostSharedLibrary = "host_shared_library"
33 HostStaticLibrary = "host_static_library"
34 HostExecutable = "host_executable"
35)
36
Colin Crossae887032017-10-23 17:16:14 -070037type BuildParams struct {
Dan Willemsen9f3c5742016-11-03 14:28:31 -070038 Rule blueprint.Rule
Colin Cross33bfb0a2016-11-21 17:23:08 -080039 Deps blueprint.Deps
40 Depfile WritablePath
Colin Cross67a5c132017-05-09 13:45:28 -070041 Description string
Dan Willemsen9f3c5742016-11-03 14:28:31 -070042 Output WritablePath
43 Outputs WritablePaths
44 ImplicitOutput WritablePath
45 ImplicitOutputs WritablePaths
46 Input Path
47 Inputs Paths
48 Implicit Path
49 Implicits Paths
50 OrderOnly Paths
51 Default bool
52 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070053}
54
Colin Crossae887032017-10-23 17:16:14 -070055type ModuleBuildParams BuildParams
56
Colin Crossf6566ed2015-03-24 11:13:38 -070057type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070058 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070059 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070060 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070061 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070062 Host() bool
63 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070064 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070065 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070066 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070067 PrimaryArch() bool
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070068 InstallOnVendorPartition() bool
Colin Cross1332b002015-04-07 17:11:30 -070069 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070070 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070071}
72
Colin Cross635c3b02016-05-18 15:37:25 -070073type BaseContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080074 BaseModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070075 androidBaseContext
76}
77
Colin Crossaabf6792017-11-29 00:27:14 -080078// BaseModuleContext is the same as blueprint.BaseModuleContext except that Config() returns
79// a Config instead of an interface{}.
80type BaseModuleContext interface {
81 ModuleName() string
82 ModuleDir() string
83 Config() Config
84
85 ContainsProperty(name string) bool
86 Errorf(pos scanner.Position, fmt string, args ...interface{})
87 ModuleErrorf(fmt string, args ...interface{})
88 PropertyErrorf(property, fmt string, args ...interface{})
89 Failed() bool
90
91 // GlobWithDeps returns a list of files that match the specified pattern but do not match any
92 // of the patterns in excludes. It also adds efficient dependencies to rerun the primary
93 // builder whenever a file matching the pattern as added or removed, without rerunning if a
94 // file that does not match the pattern is added to a searched directory.
95 GlobWithDeps(pattern string, excludes []string) ([]string, error)
96
97 Fs() pathtools.FileSystem
98 AddNinjaFileDeps(deps ...string)
99}
100
Colin Cross635c3b02016-05-18 15:37:25 -0700101type ModuleContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -0700102 androidBaseContext
Colin Crossaabf6792017-11-29 00:27:14 -0800103 BaseModuleContext
Colin Cross3f40fa42015-01-30 17:27:36 -0800104
Colin Crossae887032017-10-23 17:16:14 -0700105 // Deprecated: use ModuleContext.Build instead.
Colin Cross0875c522017-11-28 17:34:01 -0800106 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -0700107
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700108 ExpandSources(srcFiles, excludes []string) Paths
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800109 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -0700110 Glob(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700111
Colin Cross5c517922017-08-31 12:29:17 -0700112 InstallExecutable(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
113 InstallFile(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -0800114 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700115 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800116
117 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -0700118
Colin Cross8d8f8e22016-08-03 11:57:50 -0700119 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700120 InstallInSanitizerDir() bool
Nan Zhang6d34b302017-02-04 17:47:46 -0800121
122 RequiredModuleNames() []string
Colin Cross3f68a132017-10-23 17:10:29 -0700123
124 // android.ModuleContext methods
125 // These are duplicated instead of embedded so that can eventually be wrapped to take an
126 // android.Module instead of a blueprint.Module
127 OtherModuleName(m blueprint.Module) string
128 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
129 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
130
131 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
132 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
133
134 ModuleSubDir() string
135
Colin Cross35143d02017-11-16 00:11:20 -0800136 VisitDirectDepsBlueprint(visit func(blueprint.Module))
Colin Crossd11fcda2017-10-23 17:59:01 -0700137 VisitDirectDeps(visit func(Module))
138 VisitDirectDepsIf(pred func(Module) bool, visit func(Module))
139 VisitDepsDepthFirst(visit func(Module))
140 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
141 WalkDeps(visit func(Module, Module) bool)
Colin Cross3f68a132017-10-23 17:10:29 -0700142
Colin Cross0875c522017-11-28 17:34:01 -0800143 Variable(pctx PackageContext, name, value string)
144 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
Colin Crossae887032017-10-23 17:16:14 -0700145 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
146 // and performs more verification.
Colin Cross0875c522017-11-28 17:34:01 -0800147 Build(pctx PackageContext, params BuildParams)
Colin Cross3f68a132017-10-23 17:10:29 -0700148
Colin Cross0875c522017-11-28 17:34:01 -0800149 PrimaryModule() Module
150 FinalModule() Module
151 VisitAllModuleVariants(visit func(Module))
Colin Cross3f68a132017-10-23 17:10:29 -0700152
153 GetMissingDependencies() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800154}
155
Colin Cross635c3b02016-05-18 15:37:25 -0700156type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800157 blueprint.Module
158
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700159 // GenerateAndroidBuildActions is analogous to Blueprints' GenerateBuildActions,
160 // but GenerateAndroidBuildActions also has access to Android-specific information.
161 // For more information, see Module.GenerateBuildActions within Blueprint's module_ctx.go
Colin Cross635c3b02016-05-18 15:37:25 -0700162 GenerateAndroidBuildActions(ModuleContext)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700163
Colin Cross1e676be2016-10-12 14:38:15 -0700164 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800165
Colin Cross635c3b02016-05-18 15:37:25 -0700166 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800167 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700168 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800169 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700170 InstallInSanitizerDir() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800171 SkipInstall()
Colin Cross36242852017-06-23 15:06:31 -0700172
173 AddProperties(props ...interface{})
174 GetProperties() []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700175
Colin Crossae887032017-10-23 17:16:14 -0700176 BuildParamsForTests() []BuildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800177}
178
Colin Crossfc754582016-05-17 16:34:16 -0700179type nameProperties struct {
180 // The name of the module. Must be unique across all modules.
Nan Zhang0007d812017-11-07 10:57:05 -0800181 Name *string
Colin Crossfc754582016-05-17 16:34:16 -0700182}
183
184type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700185 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800186
Dan Willemsen0effe062015-11-30 16:06:01 -0800187 // emit build rules for this module
188 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800189
Colin Cross7d5136f2015-05-11 13:39:40 -0700190 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800191 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
192 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
193 // platform
Colin Cross7d716ba2017-11-01 10:38:29 -0700194 Compile_multilib *string `android:"arch_variant"`
Colin Cross69617d32016-09-06 10:39:07 -0700195
196 Target struct {
197 Host struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700198 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700199 }
200 Android struct {
Colin Cross7d716ba2017-11-01 10:38:29 -0700201 Compile_multilib *string
Colin Cross69617d32016-09-06 10:39:07 -0700202 }
203 }
204
205 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800206
Dan Willemsen782a2d12015-12-21 14:55:28 -0800207 // whether this is a proprietary vendor module, and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700208 Proprietary *bool
Dan Willemsen782a2d12015-12-21 14:55:28 -0800209
Colin Cross55708f32017-03-20 13:23:34 -0700210 // vendor who owns this module
Dan Willemsenefac4a82017-07-18 19:42:09 -0700211 Owner *string
Colin Cross55708f32017-03-20 13:23:34 -0700212
Dan Willemsenaa118f92017-04-06 12:49:58 -0700213 // whether this module is device specific and should be installed into /vendor
Colin Cross7d716ba2017-11-01 10:38:29 -0700214 Vendor *bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700215
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700216 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
217 // file
218 Logtags []string
219
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700220 // init.rc files to be installed if this module is installed
221 Init_rc []string
222
Chris Wolfe998306e2016-08-15 14:47:23 -0400223 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700224 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400225
Colin Cross5aac3622017-08-31 15:07:09 -0700226 // relative path to a file to include in the list of notices for the device
227 Notice *string
228
Colin Crossa1ad8d12016-06-01 17:09:44 -0700229 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700230 CompileTarget Target `blueprint:"mutated"`
231 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800232
233 // Set by InitAndroidModule
234 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700235 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700236
237 SkipInstall bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800238}
239
240type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700241 Host_supported *bool
242 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800243}
244
Colin Crossc472d572015-03-17 15:06:21 -0700245type Multilib string
246
247const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700248 MultilibBoth Multilib = "both"
249 MultilibFirst Multilib = "first"
250 MultilibCommon Multilib = "common"
251 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700252)
253
Colin Crossa1ad8d12016-06-01 17:09:44 -0700254type HostOrDeviceSupported int
255
256const (
257 _ HostOrDeviceSupported = iota
258 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700259 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700260 DeviceSupported
261 HostAndDeviceSupported
262 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700263 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700264)
265
Colin Cross36242852017-06-23 15:06:31 -0700266func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800267 base := m.base()
268 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700269
Colin Cross36242852017-06-23 15:06:31 -0700270 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700271 &base.nameProperties,
272 &base.commonProperties,
273 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700274}
275
Colin Cross36242852017-06-23 15:06:31 -0700276func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
277 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700278
279 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800280 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700281 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700282 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800283
Dan Willemsen218f6562015-07-08 18:13:11 -0700284 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700285 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700286 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800287 }
288
Colin Cross36242852017-06-23 15:06:31 -0700289 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800290}
291
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800292// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800293// modules. It should be included as an anonymous field in every module
294// struct definition. InitAndroidModule should then be called from the module's
295// factory function, and the return values from InitAndroidModule should be
296// returned from the factory function.
297//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800298// The ModuleBase type is responsible for implementing the GenerateBuildActions
299// method to support the blueprint.Module interface. This method will then call
300// the module's GenerateAndroidBuildActions method once for each build variant
301// that is to be built. GenerateAndroidBuildActions is passed a
302// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800303// AndroidModuleContext exposes extra functionality specific to the Android build
304// system including details about the particular build variant that is to be
305// generated.
306//
307// For example:
308//
309// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800310// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800311// )
312//
313// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800314// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800315// properties struct {
316// MyProperty string
317// }
318// }
319//
Colin Cross36242852017-06-23 15:06:31 -0700320// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800321// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700322// m.AddProperties(&m.properties)
323// android.InitAndroidModule(m)
324// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800325// }
326//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800327// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800328// // Get the CPU architecture for the current build variant.
329// variantArch := ctx.Arch()
330//
331// // ...
332// }
Colin Cross635c3b02016-05-18 15:37:25 -0700333type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800334 // Putting the curiously recurring thing pointing to the thing that contains
335 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700336 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700337 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800338
Colin Crossfc754582016-05-17 16:34:16 -0700339 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800340 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700341 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800342 hostAndDeviceProperties hostAndDeviceProperties
343 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700344 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700345 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800346
347 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700348 installFiles Paths
349 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700350
351 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
352 // Only set on the final variant of each module
Colin Cross0875c522017-11-28 17:34:01 -0800353 installTarget WritablePath
354 checkbuildTarget WritablePath
Colin Cross1f8c52b2015-06-16 16:38:17 -0700355 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700356
Colin Cross178a5092016-09-13 13:42:32 -0700357 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700358
359 registerProps []interface{}
Colin Crosscec81712017-07-13 14:43:27 -0700360
361 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700362 buildParams []BuildParams
Colin Cross36242852017-06-23 15:06:31 -0700363}
364
365func (a *ModuleBase) AddProperties(props ...interface{}) {
366 a.registerProps = append(a.registerProps, props...)
367}
368
369func (a *ModuleBase) GetProperties() []interface{} {
370 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800371}
372
Colin Crossae887032017-10-23 17:16:14 -0700373func (a *ModuleBase) BuildParamsForTests() []BuildParams {
Colin Crosscec81712017-07-13 14:43:27 -0700374 return a.buildParams
375}
376
Colin Crossce75d2c2016-10-06 16:12:58 -0700377// Name returns the name of the module. It may be overridden by individual module types, for
378// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700379func (a *ModuleBase) Name() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800380 return String(a.nameProperties.Name)
Colin Crossfc754582016-05-17 16:34:16 -0700381}
382
Colin Crossce75d2c2016-10-06 16:12:58 -0700383// BaseModuleName returns the name of the module as specified in the blueprints file.
384func (a *ModuleBase) BaseModuleName() string {
Nan Zhang0007d812017-11-07 10:57:05 -0800385 return String(a.nameProperties.Name)
Colin Crossce75d2c2016-10-06 16:12:58 -0700386}
387
Colin Cross635c3b02016-05-18 15:37:25 -0700388func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800389 return a
390}
391
Colin Cross8b74d172016-09-13 09:59:14 -0700392func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700393 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700394 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700395}
396
Colin Crossa1ad8d12016-06-01 17:09:44 -0700397func (a *ModuleBase) Target() Target {
398 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800399}
400
Colin Cross8b74d172016-09-13 09:59:14 -0700401func (a *ModuleBase) TargetPrimary() bool {
402 return a.commonProperties.CompilePrimary
403}
404
Colin Crossa1ad8d12016-06-01 17:09:44 -0700405func (a *ModuleBase) Os() OsType {
406 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800407}
408
Colin Cross635c3b02016-05-18 15:37:25 -0700409func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700410 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800411}
412
Colin Cross635c3b02016-05-18 15:37:25 -0700413func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700414 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800415}
416
Dan Willemsen0b24c742016-10-04 15:13:37 -0700417func (a *ModuleBase) ArchSpecific() bool {
418 return a.commonProperties.ArchSpecific
419}
420
Colin Crossa1ad8d12016-06-01 17:09:44 -0700421func (a *ModuleBase) OsClassSupported() []OsClass {
422 switch a.commonProperties.HostOrDeviceSupported {
423 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700424 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700425 case HostSupportedNoCross:
426 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700427 case DeviceSupported:
428 return []OsClass{Device}
429 case HostAndDeviceSupported:
430 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700431 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700432 supported = append(supported, Host, HostCross)
433 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700434 if a.hostAndDeviceProperties.Device_supported == nil ||
435 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700436 supported = append(supported, Device)
437 }
438 return supported
439 default:
440 return nil
441 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800442}
443
Colin Cross635c3b02016-05-18 15:37:25 -0700444func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800445 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
446 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700447 (a.hostAndDeviceProperties.Device_supported == nil ||
448 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800449}
450
Colin Cross635c3b02016-05-18 15:37:25 -0700451func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800452 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800453 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800454 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800455 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800456}
457
Colin Crossce75d2c2016-10-06 16:12:58 -0700458func (a *ModuleBase) SkipInstall() {
459 a.commonProperties.SkipInstall = true
460}
461
Colin Cross635c3b02016-05-18 15:37:25 -0700462func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700463 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800464
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700465 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800466 ctx.VisitDepsDepthFirstIf(isFileInstaller,
467 func(m blueprint.Module) {
468 fileInstaller := m.(fileInstaller)
469 files := fileInstaller.filesToInstall()
470 result = append(result, files...)
471 })
472
473 return result
474}
475
Colin Cross635c3b02016-05-18 15:37:25 -0700476func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800477 return a.installFiles
478}
479
Colin Cross635c3b02016-05-18 15:37:25 -0700480func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800481 return p.noAddressSanitizer
482}
483
Colin Cross635c3b02016-05-18 15:37:25 -0700484func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800485 return false
486}
487
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700488func (p *ModuleBase) InstallInSanitizerDir() bool {
489 return false
490}
491
Colin Cross0875c522017-11-28 17:34:01 -0800492func (a *ModuleBase) generateModuleTarget(ctx ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700493 allInstalledFiles := Paths{}
494 allCheckbuildFiles := Paths{}
Colin Cross0875c522017-11-28 17:34:01 -0800495 ctx.VisitAllModuleVariants(func(module Module) {
496 a := module.base()
Colin Crossc9404352015-03-26 16:10:12 -0700497 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
498 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800499 })
500
Colin Cross0875c522017-11-28 17:34:01 -0800501 var deps Paths
Colin Cross9454bfa2015-03-17 13:24:18 -0700502
Colin Cross3f40fa42015-01-30 17:27:36 -0800503 if len(allInstalledFiles) > 0 {
Jeff Gaston178d5fe2017-11-29 23:54:03 +0000504 name := PathForPhony(ctx, ctx.ModuleName()+"-install")
Colin Cross0875c522017-11-28 17:34:01 -0800505 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700506 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800507 Output: name,
508 Implicits: allInstalledFiles,
Colin Crossaabf6792017-11-29 00:27:14 -0800509 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700510 })
511 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700512 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700513 }
514
515 if len(allCheckbuildFiles) > 0 {
Jeff Gaston178d5fe2017-11-29 23:54:03 +0000516 name := PathForPhony(ctx, ctx.ModuleName()+"-checkbuild")
Colin Cross0875c522017-11-28 17:34:01 -0800517 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700518 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -0800519 Output: name,
520 Implicits: allCheckbuildFiles,
Colin Cross9454bfa2015-03-17 13:24:18 -0700521 })
522 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700523 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700524 }
525
526 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800527 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -0800528 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800529 suffix = "-soong"
530 }
531
Colin Cross0875c522017-11-28 17:34:01 -0800532 ctx.Build(pctx, BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700533 Rule: blueprint.Phony,
Jeff Gaston178d5fe2017-11-29 23:54:03 +0000534 Output: PathForPhony(ctx, ctx.ModuleName()+suffix),
Colin Cross9454bfa2015-03-17 13:24:18 -0700535 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800536 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700537
538 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800539 }
540}
541
Colin Cross635c3b02016-05-18 15:37:25 -0700542func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700543 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700544 target: a.commonProperties.CompileTarget,
545 targetPrimary: a.commonProperties.CompilePrimary,
Colin Cross7d716ba2017-11-01 10:38:29 -0700546 vendor: Bool(a.commonProperties.Proprietary) || Bool(a.commonProperties.Vendor),
Colin Cross8b74d172016-09-13 09:59:14 -0700547 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800548 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800549}
550
Colin Cross0875c522017-11-28 17:34:01 -0800551func (a *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
552 ctx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700553 module: a.module,
Colin Cross0875c522017-11-28 17:34:01 -0800554 ModuleContext: blueprintCtx,
555 androidBaseContextImpl: a.androidBaseContextFactory(blueprintCtx),
556 installDeps: a.computeInstallDeps(blueprintCtx),
Colin Cross6362e272015-10-29 15:25:03 -0700557 installFiles: a.installFiles,
Colin Cross0875c522017-11-28 17:34:01 -0800558 missingDeps: blueprintCtx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800559 }
560
Colin Cross67a5c132017-05-09 13:45:28 -0700561 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
562 var suffix []string
Colin Cross0875c522017-11-28 17:34:01 -0800563 if ctx.Os().Class != Device && ctx.Os().Class != Generic {
564 suffix = append(suffix, ctx.Os().String())
Colin Cross67a5c132017-05-09 13:45:28 -0700565 }
Colin Cross0875c522017-11-28 17:34:01 -0800566 if !ctx.PrimaryArch() {
567 suffix = append(suffix, ctx.Arch().ArchType.String())
Colin Cross67a5c132017-05-09 13:45:28 -0700568 }
569
570 ctx.Variable(pctx, "moduleDesc", desc)
571
572 s := ""
573 if len(suffix) > 0 {
574 s = " [" + strings.Join(suffix, " ") + "]"
575 }
576 ctx.Variable(pctx, "moduleDescSuffix", s)
577
Colin Cross9b1d13d2016-09-19 15:18:11 -0700578 if a.Enabled() {
Colin Cross0875c522017-11-28 17:34:01 -0800579 a.module.GenerateAndroidBuildActions(ctx)
Colin Cross9b1d13d2016-09-19 15:18:11 -0700580 if ctx.Failed() {
581 return
582 }
583
Colin Cross0875c522017-11-28 17:34:01 -0800584 a.installFiles = append(a.installFiles, ctx.installFiles...)
585 a.checkbuildFiles = append(a.checkbuildFiles, ctx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800586 }
587
Colin Cross9b1d13d2016-09-19 15:18:11 -0700588 if a == ctx.FinalModule().(Module).base() {
589 a.generateModuleTarget(ctx)
590 if ctx.Failed() {
591 return
592 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800593 }
Colin Crosscec81712017-07-13 14:43:27 -0700594
Colin Cross0875c522017-11-28 17:34:01 -0800595 a.buildParams = ctx.buildParams
Colin Cross3f40fa42015-01-30 17:27:36 -0800596}
597
Colin Crossf6566ed2015-03-24 11:13:38 -0700598type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700599 target Target
600 targetPrimary bool
601 debug bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700602 vendor bool
Colin Cross8b74d172016-09-13 09:59:14 -0700603 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700604}
605
Colin Cross3f40fa42015-01-30 17:27:36 -0800606type androidModuleContext struct {
607 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700608 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700609 installDeps Paths
610 installFiles Paths
611 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800612 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700613 module Module
Colin Crosscec81712017-07-13 14:43:27 -0700614
615 // For tests
Colin Crossae887032017-10-23 17:16:14 -0700616 buildParams []BuildParams
Colin Cross6ff51382015-12-17 16:39:19 -0800617}
618
Colin Cross67a5c132017-05-09 13:45:28 -0700619func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross0875c522017-11-28 17:34:01 -0800620 a.ModuleContext.Build(pctx.PackageContext, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700621 Rule: ErrorRule,
622 Description: desc,
623 Outputs: outputs,
624 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800625 Args: map[string]string{
626 "error": err.Error(),
627 },
628 })
629 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800630}
631
Colin Crossaabf6792017-11-29 00:27:14 -0800632func (a *androidModuleContext) Config() Config {
633 return a.ModuleContext.Config().(Config)
634}
635
Colin Cross0875c522017-11-28 17:34:01 -0800636func (a *androidModuleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
Colin Crossae887032017-10-23 17:16:14 -0700637 a.Build(pctx, BuildParams(params))
Colin Cross3f40fa42015-01-30 17:27:36 -0800638}
639
Colin Cross0875c522017-11-28 17:34:01 -0800640func convertBuildParams(params BuildParams) blueprint.BuildParams {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700641 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700642 Rule: params.Rule,
Colin Cross0875c522017-11-28 17:34:01 -0800643 Description: params.Description,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800644 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700645 Outputs: params.Outputs.Strings(),
646 ImplicitOutputs: params.ImplicitOutputs.Strings(),
647 Inputs: params.Inputs.Strings(),
648 Implicits: params.Implicits.Strings(),
649 OrderOnly: params.OrderOnly.Strings(),
650 Args: params.Args,
651 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700652 }
653
Colin Cross33bfb0a2016-11-21 17:23:08 -0800654 if params.Depfile != nil {
655 bparams.Depfile = params.Depfile.String()
656 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700657 if params.Output != nil {
658 bparams.Outputs = append(bparams.Outputs, params.Output.String())
659 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700660 if params.ImplicitOutput != nil {
661 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
662 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700663 if params.Input != nil {
664 bparams.Inputs = append(bparams.Inputs, params.Input.String())
665 }
666 if params.Implicit != nil {
667 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
668 }
669
Colin Cross0875c522017-11-28 17:34:01 -0800670 return bparams
671}
672
673func (a *androidModuleContext) Variable(pctx PackageContext, name, value string) {
674 a.ModuleContext.Variable(pctx.PackageContext, name, value)
675}
676
677func (a *androidModuleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
678 argNames ...string) blueprint.Rule {
679
680 return a.ModuleContext.Rule(pctx.PackageContext, name, params, argNames...)
681}
682
683func (a *androidModuleContext) Build(pctx PackageContext, params BuildParams) {
684 if a.config.captureBuild {
685 a.buildParams = append(a.buildParams, params)
686 }
687
688 bparams := convertBuildParams(params)
689
690 if bparams.Description != "" {
691 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
692 }
693
Colin Cross6ff51382015-12-17 16:39:19 -0800694 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700695 a.ninjaError(bparams.Description, bparams.Outputs,
696 fmt.Errorf("module %s missing dependencies: %s\n",
697 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800698 return
699 }
700
Colin Cross0875c522017-11-28 17:34:01 -0800701 a.ModuleContext.Build(pctx.PackageContext, bparams)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700702}
703
Colin Cross6ff51382015-12-17 16:39:19 -0800704func (a *androidModuleContext) GetMissingDependencies() []string {
705 return a.missingDeps
706}
707
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800708func (a *androidModuleContext) AddMissingDependencies(deps []string) {
709 if deps != nil {
710 a.missingDeps = append(a.missingDeps, deps...)
Colin Crossd11fcda2017-10-23 17:59:01 -0700711 a.missingDeps = FirstUniqueStrings(a.missingDeps)
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800712 }
713}
714
Colin Crossd11fcda2017-10-23 17:59:01 -0700715func (a *androidModuleContext) validateAndroidModule(module blueprint.Module) Module {
716 aModule, _ := module.(Module)
717 if aModule == nil {
718 a.ModuleErrorf("module %q not an android module", a.OtherModuleName(aModule))
719 return nil
720 }
721
722 if !aModule.Enabled() {
723 if a.AConfig().AllowMissingDependencies() {
724 a.AddMissingDependencies([]string{a.OtherModuleName(aModule)})
725 } else {
726 a.ModuleErrorf("depends on disabled module %q", a.OtherModuleName(aModule))
727 }
728 return nil
729 }
730
731 return aModule
732}
733
Colin Cross35143d02017-11-16 00:11:20 -0800734func (a *androidModuleContext) VisitDirectDepsBlueprint(visit func(blueprint.Module)) {
735 a.ModuleContext.VisitDirectDeps(visit)
736}
737
Colin Crossd11fcda2017-10-23 17:59:01 -0700738func (a *androidModuleContext) VisitDirectDeps(visit func(Module)) {
739 a.ModuleContext.VisitDirectDeps(func(module blueprint.Module) {
740 if aModule := a.validateAndroidModule(module); aModule != nil {
741 visit(aModule)
742 }
743 })
744}
745
746func (a *androidModuleContext) VisitDirectDepsIf(pred func(Module) bool, visit func(Module)) {
747 a.ModuleContext.VisitDirectDepsIf(
748 // pred
749 func(module blueprint.Module) bool {
750 if aModule := a.validateAndroidModule(module); aModule != nil {
751 return pred(aModule)
752 } else {
753 return false
754 }
755 },
756 // visit
757 func(module blueprint.Module) {
758 visit(module.(Module))
759 })
760}
761
762func (a *androidModuleContext) VisitDepsDepthFirst(visit func(Module)) {
763 a.ModuleContext.VisitDepsDepthFirst(func(module blueprint.Module) {
764 if aModule := a.validateAndroidModule(module); aModule != nil {
765 visit(aModule)
766 }
767 })
768}
769
770func (a *androidModuleContext) VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module)) {
771 a.ModuleContext.VisitDepsDepthFirstIf(
772 // pred
773 func(module blueprint.Module) bool {
774 if aModule := a.validateAndroidModule(module); aModule != nil {
775 return pred(aModule)
776 } else {
777 return false
778 }
779 },
780 // visit
781 func(module blueprint.Module) {
782 visit(module.(Module))
783 })
784}
785
786func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
787 a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
788 childAndroidModule := a.validateAndroidModule(child)
789 parentAndroidModule := a.validateAndroidModule(parent)
790 if childAndroidModule != nil && parentAndroidModule != nil {
791 return visit(childAndroidModule, parentAndroidModule)
792 } else {
793 return false
794 }
795 })
796}
797
Colin Cross0875c522017-11-28 17:34:01 -0800798func (a *androidModuleContext) VisitAllModuleVariants(visit func(Module)) {
799 a.ModuleContext.VisitAllModuleVariants(func(module blueprint.Module) {
800 visit(module.(Module))
801 })
802}
803
804func (a *androidModuleContext) PrimaryModule() Module {
805 return a.ModuleContext.PrimaryModule().(Module)
806}
807
808func (a *androidModuleContext) FinalModule() Module {
809 return a.ModuleContext.FinalModule().(Module)
810}
811
Colin Crossa1ad8d12016-06-01 17:09:44 -0700812func (a *androidBaseContextImpl) Target() Target {
813 return a.target
814}
815
Colin Cross8b74d172016-09-13 09:59:14 -0700816func (a *androidBaseContextImpl) TargetPrimary() bool {
817 return a.targetPrimary
818}
819
Colin Crossf6566ed2015-03-24 11:13:38 -0700820func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700821 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800822}
823
Colin Crossa1ad8d12016-06-01 17:09:44 -0700824func (a *androidBaseContextImpl) Os() OsType {
825 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800826}
827
Colin Crossf6566ed2015-03-24 11:13:38 -0700828func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700829 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700830}
831
832func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700833 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700834}
835
Colin Cross0af4b842015-04-30 16:36:18 -0700836func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700837 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700838}
839
Colin Cross3edeee12017-04-04 12:59:48 -0700840func (a *androidBaseContextImpl) Windows() bool {
841 return a.target.Os == Windows
842}
843
Colin Crossf6566ed2015-03-24 11:13:38 -0700844func (a *androidBaseContextImpl) Debug() bool {
845 return a.debug
846}
847
Colin Cross1e7d3702016-08-24 15:25:47 -0700848func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700849 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
850 return true
851 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700852 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
853}
854
Colin Cross1332b002015-04-07 17:11:30 -0700855func (a *androidBaseContextImpl) AConfig() Config {
856 return a.config
857}
858
Colin Cross9272ade2016-08-17 15:24:12 -0700859func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
860 return DeviceConfig{a.config.deviceConfig}
861}
862
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700863func (a *androidBaseContextImpl) InstallOnVendorPartition() bool {
Dan Willemsenaa118f92017-04-06 12:49:58 -0700864 return a.vendor
Dan Willemsen782a2d12015-12-21 14:55:28 -0800865}
866
Colin Cross8d8f8e22016-08-03 11:57:50 -0700867func (a *androidModuleContext) InstallInData() bool {
868 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800869}
870
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700871func (a *androidModuleContext) InstallInSanitizerDir() bool {
872 return a.module.InstallInSanitizerDir()
873}
874
Colin Cross893d8162017-04-26 17:34:03 -0700875func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
876 if a.module.base().commonProperties.SkipInstall {
877 return true
878 }
879
880 if a.Device() {
881 if a.AConfig().SkipDeviceInstall() {
882 return true
883 }
884
885 if a.AConfig().SkipMegaDeviceInstall(fullInstallPath.String()) {
886 return true
887 }
888 }
889
890 return false
891}
892
Colin Cross5c517922017-08-31 12:29:17 -0700893func (a *androidModuleContext) InstallFile(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700894 deps ...Path) OutputPath {
Colin Cross5c517922017-08-31 12:29:17 -0700895 return a.installFile(installPath, name, srcPath, Cp, deps)
896}
897
898func (a *androidModuleContext) InstallExecutable(installPath OutputPath, name string, srcPath Path,
899 deps ...Path) OutputPath {
900 return a.installFile(installPath, name, srcPath, CpExecutable, deps)
901}
902
903func (a *androidModuleContext) installFile(installPath OutputPath, name string, srcPath Path,
904 rule blueprint.Rule, deps []Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700905
Dan Willemsen782a2d12015-12-21 14:55:28 -0800906 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700907 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800908
Colin Cross893d8162017-04-26 17:34:03 -0700909 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700910
Dan Willemsen322acaf2016-01-12 23:07:05 -0800911 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700912
Colin Cross89562dc2016-10-03 17:47:19 -0700913 var implicitDeps, orderOnlyDeps Paths
914
915 if a.Host() {
916 // Installed host modules might be used during the build, depend directly on their
917 // dependencies so their timestamp is updated whenever their dependency is updated
918 implicitDeps = deps
919 } else {
920 orderOnlyDeps = deps
921 }
922
Colin Crossae887032017-10-23 17:16:14 -0700923 a.Build(pctx, BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700924 Rule: rule,
Colin Cross67a5c132017-05-09 13:45:28 -0700925 Description: "install " + fullInstallPath.Base(),
926 Output: fullInstallPath,
927 Input: srcPath,
928 Implicits: implicitDeps,
929 OrderOnly: orderOnlyDeps,
930 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800931 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800932
Dan Willemsen322acaf2016-01-12 23:07:05 -0800933 a.installFiles = append(a.installFiles, fullInstallPath)
934 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700935 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700936 return fullInstallPath
937}
938
Colin Cross3854a602016-01-11 12:49:11 -0800939func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
940 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700941 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800942
Colin Cross893d8162017-04-26 17:34:03 -0700943 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700944
Colin Crossae887032017-10-23 17:16:14 -0700945 a.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700946 Rule: Symlink,
947 Description: "install symlink " + fullInstallPath.Base(),
948 Output: fullInstallPath,
949 OrderOnly: Paths{srcPath},
950 Default: !a.AConfig().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -0800951 Args: map[string]string{
952 "fromPath": srcPath.String(),
953 },
954 })
Colin Cross3854a602016-01-11 12:49:11 -0800955
Colin Cross12fc4972016-01-11 12:49:11 -0800956 a.installFiles = append(a.installFiles, fullInstallPath)
957 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
958 }
Colin Cross3854a602016-01-11 12:49:11 -0800959 return fullInstallPath
960}
961
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700962func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800963 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
964}
965
Colin Cross3f40fa42015-01-30 17:27:36 -0800966type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700967 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800968}
969
970func isFileInstaller(m blueprint.Module) bool {
971 _, ok := m.(fileInstaller)
972 return ok
973}
974
975func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700976 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800977 return ok
978}
Colin Crossfce53272015-04-08 11:21:40 -0700979
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700980func findStringInSlice(str string, slice []string) int {
981 for i, s := range slice {
982 if s == str {
983 return i
Colin Crossfce53272015-04-08 11:21:40 -0700984 }
985 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700986 return -1
987}
988
Colin Cross068e0fe2016-12-13 15:23:47 -0800989func SrcIsModule(s string) string {
990 if len(s) > 1 && s[0] == ':' {
991 return s[1:]
992 }
993 return ""
994}
995
996type sourceDependencyTag struct {
997 blueprint.BaseDependencyTag
998}
999
1000var SourceDepTag sourceDependencyTag
1001
1002// Returns a list of modules that must be depended on to satisfy filegroup or generated sources
1003// modules listed in srcFiles using ":module" syntax
1004func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
1005 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -07001006 set := make(map[string]bool)
1007
Colin Cross068e0fe2016-12-13 15:23:47 -08001008 for _, s := range srcFiles {
1009 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -07001010 if _, found := set[m]; found {
1011 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
1012 } else {
1013 set[m] = true
1014 deps = append(deps, m)
1015 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001016 }
1017 }
1018
1019 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
1020}
1021
1022type SourceFileProducer interface {
1023 Srcs() Paths
1024}
1025
1026// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001027// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001028func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001029 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
1030}
1031
1032func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001033 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001034
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001035 for i, e := range excludes {
1036 j := findStringInSlice(e, srcFiles)
1037 if j != -1 {
1038 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
1039 }
1040
1041 excludes[i] = filepath.Join(prefix, e)
1042 }
1043
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001044 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -07001045 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -08001046 if m := SrcIsModule(s); m != "" {
1047 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
Colin Cross0617bb82017-10-24 13:01:18 -07001048 if module == nil {
1049 // Error will have been handled by ExtractSourcesDeps
1050 continue
1051 }
Colin Cross068e0fe2016-12-13 15:23:47 -08001052 if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001053 expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
Colin Cross068e0fe2016-12-13 15:23:47 -08001054 } else {
1055 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
1056 }
1057 } else if pathtools.IsGlob(s) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001058 globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
Colin Cross05a39cb2017-10-09 13:35:19 -07001059 for i, s := range globbedSrcFiles {
1060 globbedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001061 }
Colin Cross05a39cb2017-10-09 13:35:19 -07001062 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
Colin Cross8f101b42015-06-17 15:09:06 -07001063 } else {
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001064 s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
1065 expandedSrcFiles = append(expandedSrcFiles, s)
Colin Cross8f101b42015-06-17 15:09:06 -07001066 }
1067 }
1068
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001069 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -07001070}
1071
Nan Zhang6d34b302017-02-04 17:47:46 -08001072func (ctx *androidModuleContext) RequiredModuleNames() []string {
1073 return ctx.module.base().commonProperties.Required
1074}
1075
Colin Cross7f19f372016-11-01 11:10:25 -07001076func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
1077 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -07001078 if err != nil {
1079 ctx.ModuleErrorf("glob: %s", err.Error())
1080 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001081 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -07001082}
Colin Cross1f8c52b2015-06-16 16:38:17 -07001083
Colin Cross463a90e2015-06-17 14:20:06 -07001084func init() {
Colin Cross798bfce2016-10-12 14:28:16 -07001085 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -07001086}
1087
Colin Cross0875c522017-11-28 17:34:01 -08001088func BuildTargetSingleton() Singleton {
Colin Cross1f8c52b2015-06-16 16:38:17 -07001089 return &buildTargetSingleton{}
1090}
1091
Colin Cross87d8b562017-04-25 10:01:55 -07001092func parentDir(dir string) string {
1093 dir, _ = filepath.Split(dir)
1094 return filepath.Clean(dir)
1095}
1096
Colin Cross1f8c52b2015-06-16 16:38:17 -07001097type buildTargetSingleton struct{}
1098
Colin Cross0875c522017-11-28 17:34:01 -08001099func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
1100 var checkbuildDeps Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -07001101
Colin Cross0875c522017-11-28 17:34:01 -08001102 mmTarget := func(dir string) WritablePath {
1103 return PathForPhony(ctx,
1104 "MODULES-IN-"+strings.Replace(filepath.Clean(dir), "/", "-", -1))
Colin Cross87d8b562017-04-25 10:01:55 -07001105 }
1106
Colin Cross0875c522017-11-28 17:34:01 -08001107 modulesInDir := make(map[string]Paths)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001108
Colin Cross0875c522017-11-28 17:34:01 -08001109 ctx.VisitAllModules(func(module Module) {
1110 blueprintDir := module.base().blueprintDir
1111 installTarget := module.base().installTarget
1112 checkbuildTarget := module.base().checkbuildTarget
Colin Cross1f8c52b2015-06-16 16:38:17 -07001113
Colin Cross0875c522017-11-28 17:34:01 -08001114 if checkbuildTarget != nil {
1115 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
1116 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
1117 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001118
Colin Cross0875c522017-11-28 17:34:01 -08001119 if installTarget != nil {
1120 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -07001121 }
1122 })
1123
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001124 suffix := ""
Colin Crossaabf6792017-11-29 00:27:14 -08001125 if ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001126 suffix = "-soong"
1127 }
1128
Colin Cross1f8c52b2015-06-16 16:38:17 -07001129 // Create a top-level checkbuild target that depends on all modules
Colin Cross0875c522017-11-28 17:34:01 -08001130 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001131 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001132 Output: PathForPhony(ctx, "checkbuild"+suffix),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001133 Implicits: checkbuildDeps,
Colin Cross1f8c52b2015-06-16 16:38:17 -07001134 })
1135
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001136 // Make will generate the MODULES-IN-* targets
Colin Crossaabf6792017-11-29 00:27:14 -08001137 if ctx.Config().EmbeddedInMake() {
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001138 return
1139 }
1140
Colin Cross0875c522017-11-28 17:34:01 -08001141 sortedKeys := func(m map[string]Paths) []string {
1142 s := make([]string, 0, len(m))
1143 for k := range m {
1144 s = append(s, k)
1145 }
1146 sort.Strings(s)
1147 return s
1148 }
1149
Colin Cross87d8b562017-04-25 10:01:55 -07001150 // Ensure ancestor directories are in modulesInDir
1151 dirs := sortedKeys(modulesInDir)
1152 for _, dir := range dirs {
1153 dir := parentDir(dir)
1154 for dir != "." && dir != "/" {
1155 if _, exists := modulesInDir[dir]; exists {
1156 break
1157 }
1158 modulesInDir[dir] = nil
1159 dir = parentDir(dir)
1160 }
1161 }
1162
1163 // Make directories build their direct subdirectories
1164 dirs = sortedKeys(modulesInDir)
1165 for _, dir := range dirs {
1166 p := parentDir(dir)
1167 if p != "." && p != "/" {
1168 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
1169 }
1170 }
1171
Dan Willemsend2e95fb2017-09-20 14:30:50 -07001172 // Create a MODULES-IN-<directory> target that depends on all modules in a directory, and
1173 // depends on the MODULES-IN-* targets of all of its subdirectories that contain Android.bp
1174 // files.
Colin Cross1f8c52b2015-06-16 16:38:17 -07001175 for _, dir := range dirs {
Colin Cross0875c522017-11-28 17:34:01 -08001176 ctx.Build(pctx, BuildParams{
Colin Cross1f8c52b2015-06-16 16:38:17 -07001177 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001178 Output: mmTarget(dir),
Colin Cross87d8b562017-04-25 10:01:55 -07001179 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -08001180 // HACK: checkbuild should be an optional build, but force it
1181 // enabled for now in standalone builds
Colin Crossaabf6792017-11-29 00:27:14 -08001182 Default: !ctx.Config().EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -07001183 })
1184 }
Dan Willemsen61d88b82017-09-20 17:29:08 -07001185
1186 // Create (host|host-cross|target)-<OS> phony rules to build a reduced checkbuild.
1187 osDeps := map[OsType]Paths{}
Colin Cross0875c522017-11-28 17:34:01 -08001188 ctx.VisitAllModules(func(module Module) {
1189 if module.Enabled() {
1190 os := module.Target().Os
1191 osDeps[os] = append(osDeps[os], module.base().checkbuildFiles...)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001192 }
1193 })
1194
Colin Cross0875c522017-11-28 17:34:01 -08001195 osClass := make(map[string]Paths)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001196 for os, deps := range osDeps {
1197 var className string
1198
1199 switch os.Class {
1200 case Host:
1201 className = "host"
1202 case HostCross:
1203 className = "host-cross"
1204 case Device:
1205 className = "target"
1206 default:
1207 continue
1208 }
1209
Colin Cross0875c522017-11-28 17:34:01 -08001210 name := PathForPhony(ctx, className+"-"+os.Name)
Dan Willemsen61d88b82017-09-20 17:29:08 -07001211 osClass[className] = append(osClass[className], name)
1212
Colin Cross0875c522017-11-28 17:34:01 -08001213 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001214 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001215 Output: name,
1216 Implicits: deps,
Dan Willemsen61d88b82017-09-20 17:29:08 -07001217 })
1218 }
1219
1220 // Wrap those into host|host-cross|target phony rules
1221 osClasses := sortedKeys(osClass)
1222 for _, class := range osClasses {
Colin Cross0875c522017-11-28 17:34:01 -08001223 ctx.Build(pctx, BuildParams{
Dan Willemsen61d88b82017-09-20 17:29:08 -07001224 Rule: blueprint.Phony,
Colin Cross0875c522017-11-28 17:34:01 -08001225 Output: PathForPhony(ctx, class),
Dan Willemsen61d88b82017-09-20 17:29:08 -07001226 Implicits: osClass[class],
Dan Willemsen61d88b82017-09-20 17:29:08 -07001227 })
1228 }
Colin Cross1f8c52b2015-06-16 16:38:17 -07001229}
Colin Crossd779da42015-12-17 18:00:23 -08001230
1231type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001232 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -08001233 ctx interface {
1234 ModuleName(blueprint.Module) string
1235 ModuleSubDir(blueprint.Module) string
1236 }
1237}
1238
1239func (s AndroidModulesByName) Len() int { return len(s.slice) }
1240func (s AndroidModulesByName) Less(i, j int) bool {
1241 mi, mj := s.slice[i], s.slice[j]
1242 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
1243
1244 if ni != nj {
1245 return ni < nj
1246 } else {
1247 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
1248 }
1249}
1250func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }