blob: 8f8f34b333f0d8f67179c4c4b8ffff7e16d538cd [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6ff51382015-12-17 16:39:19 -080018 "fmt"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "path/filepath"
Colin Cross6ff51382015-12-17 16:39:19 -080020 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070021
22 "github.com/google/blueprint"
Colin Cross7f19f372016-11-01 11:10:25 -070023 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080024)
25
26var (
27 DeviceSharedLibrary = "shared_library"
28 DeviceStaticLibrary = "static_library"
29 DeviceExecutable = "executable"
30 HostSharedLibrary = "host_shared_library"
31 HostStaticLibrary = "host_static_library"
32 HostExecutable = "host_executable"
33)
34
Dan Willemsen34cc69e2015-09-23 15:26:20 -070035type ModuleBuildParams struct {
Dan Willemsen9f3c5742016-11-03 14:28:31 -070036 Rule blueprint.Rule
Colin Cross33bfb0a2016-11-21 17:23:08 -080037 Deps blueprint.Deps
38 Depfile WritablePath
Colin Cross67a5c132017-05-09 13:45:28 -070039 Description string
Dan Willemsen9f3c5742016-11-03 14:28:31 -070040 Output WritablePath
41 Outputs WritablePaths
42 ImplicitOutput WritablePath
43 ImplicitOutputs WritablePaths
44 Input Path
45 Inputs Paths
46 Implicit Path
47 Implicits Paths
48 OrderOnly Paths
49 Default bool
50 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070051}
52
Colin Crossf6566ed2015-03-24 11:13:38 -070053type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070054 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070055 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070056 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070057 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070058 Host() bool
59 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070060 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070061 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070062 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070063 PrimaryArch() bool
Dan Willemsenaa118f92017-04-06 12:49:58 -070064 Vendor() bool
Colin Cross1332b002015-04-07 17:11:30 -070065 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070066 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070067}
68
Colin Cross635c3b02016-05-18 15:37:25 -070069type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070070 blueprint.BaseModuleContext
71 androidBaseContext
72}
73
Colin Cross635c3b02016-05-18 15:37:25 -070074type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080075 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070076 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080077
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078 // Similar to Build, but takes Paths instead of []string,
79 // and performs more verification.
80 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070081
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 ExpandSources(srcFiles, excludes []string) Paths
Colin Crossfaeb7aa2017-02-01 14:12:44 -080083 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -070084 Glob(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085
Colin Crossa2344662016-03-24 13:14:12 -070086 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
87 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080088 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080090
91 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070092
Colin Cross8d8f8e22016-08-03 11:57:50 -070093 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -070094 InstallInSanitizerDir() bool
Nan Zhang6d34b302017-02-04 17:47:46 -080095
96 RequiredModuleNames() []string
Colin Cross3f40fa42015-01-30 17:27:36 -080097}
98
Colin Cross635c3b02016-05-18 15:37:25 -070099type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800100 blueprint.Module
101
Colin Cross635c3b02016-05-18 15:37:25 -0700102 GenerateAndroidBuildActions(ModuleContext)
Colin Cross1e676be2016-10-12 14:38:15 -0700103 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800104
Colin Cross635c3b02016-05-18 15:37:25 -0700105 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800106 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700107 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800108 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700109 InstallInSanitizerDir() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800110 SkipInstall()
Colin Cross3f40fa42015-01-30 17:27:36 -0800111}
112
Colin Crossfc754582016-05-17 16:34:16 -0700113type nameProperties struct {
114 // The name of the module. Must be unique across all modules.
Colin Crossc77f9d12015-04-02 13:54:39 -0700115 Name string
Colin Crossfc754582016-05-17 16:34:16 -0700116}
117
118type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700119 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800120
Dan Willemsen0effe062015-11-30 16:06:01 -0800121 // emit build rules for this module
122 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800123
Colin Cross7d5136f2015-05-11 13:39:40 -0700124 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800125 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
126 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
127 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700128 Compile_multilib string `android:"arch_variant"`
129
130 Target struct {
131 Host struct {
132 Compile_multilib string
133 }
134 Android struct {
135 Compile_multilib string
136 }
137 }
138
139 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800140
Dan Willemsen782a2d12015-12-21 14:55:28 -0800141 // whether this is a proprietary vendor module, and should be installed into /vendor
142 Proprietary bool
143
Colin Cross55708f32017-03-20 13:23:34 -0700144 // vendor who owns this module
145 Owner string
146
Dan Willemsenaa118f92017-04-06 12:49:58 -0700147 // whether this module is device specific and should be installed into /vendor
148 Vendor bool
149
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700150 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
151 // file
152 Logtags []string
153
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700154 // init.rc files to be installed if this module is installed
155 Init_rc []string
156
Chris Wolfe998306e2016-08-15 14:47:23 -0400157 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700158 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400159
Colin Crossa1ad8d12016-06-01 17:09:44 -0700160 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700161 CompileTarget Target `blueprint:"mutated"`
162 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800163
164 // Set by InitAndroidModule
165 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700166 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700167
168 SkipInstall bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800169}
170
171type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700172 Host_supported *bool
173 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800174}
175
Colin Crossc472d572015-03-17 15:06:21 -0700176type Multilib string
177
178const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700179 MultilibBoth Multilib = "both"
180 MultilibFirst Multilib = "first"
181 MultilibCommon Multilib = "common"
182 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700183)
184
Colin Crossa1ad8d12016-06-01 17:09:44 -0700185type HostOrDeviceSupported int
186
187const (
188 _ HostOrDeviceSupported = iota
189 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700190 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700191 DeviceSupported
192 HostAndDeviceSupported
193 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700194 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700195)
196
Colin Cross635c3b02016-05-18 15:37:25 -0700197func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800198 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
199
200 base := m.base()
201 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700202
Colin Crossfc754582016-05-17 16:34:16 -0700203 propertyStructs = append(propertyStructs,
204 &base.nameProperties,
205 &base.commonProperties,
206 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700207
208 return m, propertyStructs
209}
210
Colin Cross635c3b02016-05-18 15:37:25 -0700211func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700212 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
213
214 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
215
216 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800217 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700218 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700219 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800220
Dan Willemsen218f6562015-07-08 18:13:11 -0700221 switch hod {
222 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800223 // Default to module to device supported, host not supported, can override in module
224 // properties
Colin Crossa4190c12016-07-12 13:11:25 -0700225 base.hostAndDeviceProperties.Device_supported = boolPtr(true)
Dan Willemsen218f6562015-07-08 18:13:11 -0700226 fallthrough
227 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800228 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
229 }
230
Colin Crosscfad1192015-11-02 16:43:11 -0800231 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800232}
233
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800234// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800235// modules. It should be included as an anonymous field in every module
236// struct definition. InitAndroidModule should then be called from the module's
237// factory function, and the return values from InitAndroidModule should be
238// returned from the factory function.
239//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800240// The ModuleBase type is responsible for implementing the GenerateBuildActions
241// method to support the blueprint.Module interface. This method will then call
242// the module's GenerateAndroidBuildActions method once for each build variant
243// that is to be built. GenerateAndroidBuildActions is passed a
244// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800245// AndroidModuleContext exposes extra functionality specific to the Android build
246// system including details about the particular build variant that is to be
247// generated.
248//
249// For example:
250//
251// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800252// "android/soong/android"
Colin Cross70b40592015-03-23 12:57:34 -0700253// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800254// )
255//
256// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800257// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800258// properties struct {
259// MyProperty string
260// }
261// }
262//
263// func NewMyModule() (blueprint.Module, []interface{}) {
264// m := &myModule{}
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800265// return android.InitAndroidModule(m, &m.properties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800266// }
267//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800268// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800269// // Get the CPU architecture for the current build variant.
270// variantArch := ctx.Arch()
271//
272// // ...
273// }
Colin Cross635c3b02016-05-18 15:37:25 -0700274type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800275 // Putting the curiously recurring thing pointing to the thing that contains
276 // the thing pattern to good use.
Colin Cross635c3b02016-05-18 15:37:25 -0700277 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800278
Colin Crossfc754582016-05-17 16:34:16 -0700279 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800280 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700281 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800282 hostAndDeviceProperties hostAndDeviceProperties
283 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700284 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700285 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800286
287 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700288 installFiles Paths
289 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700290
291 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
292 // Only set on the final variant of each module
293 installTarget string
294 checkbuildTarget string
295 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700296
Colin Cross178a5092016-09-13 13:42:32 -0700297 hooks hooks
Colin Cross3f40fa42015-01-30 17:27:36 -0800298}
299
Colin Crossce75d2c2016-10-06 16:12:58 -0700300// Name returns the name of the module. It may be overridden by individual module types, for
301// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700302func (a *ModuleBase) Name() string {
303 return a.nameProperties.Name
304}
305
Colin Crossce75d2c2016-10-06 16:12:58 -0700306// BaseModuleName returns the name of the module as specified in the blueprints file.
307func (a *ModuleBase) BaseModuleName() string {
308 return a.nameProperties.Name
309}
310
Colin Cross635c3b02016-05-18 15:37:25 -0700311func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800312 return a
313}
314
Colin Cross8b74d172016-09-13 09:59:14 -0700315func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700316 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700317 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700318}
319
Colin Crossa1ad8d12016-06-01 17:09:44 -0700320func (a *ModuleBase) Target() Target {
321 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800322}
323
Colin Cross8b74d172016-09-13 09:59:14 -0700324func (a *ModuleBase) TargetPrimary() bool {
325 return a.commonProperties.CompilePrimary
326}
327
Colin Crossa1ad8d12016-06-01 17:09:44 -0700328func (a *ModuleBase) Os() OsType {
329 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800330}
331
Colin Cross635c3b02016-05-18 15:37:25 -0700332func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700333 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800334}
335
Colin Cross635c3b02016-05-18 15:37:25 -0700336func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700337 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800338}
339
Dan Willemsen0b24c742016-10-04 15:13:37 -0700340func (a *ModuleBase) ArchSpecific() bool {
341 return a.commonProperties.ArchSpecific
342}
343
Colin Crossa1ad8d12016-06-01 17:09:44 -0700344func (a *ModuleBase) OsClassSupported() []OsClass {
345 switch a.commonProperties.HostOrDeviceSupported {
346 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700347 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700348 case HostSupportedNoCross:
349 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700350 case DeviceSupported:
351 return []OsClass{Device}
352 case HostAndDeviceSupported:
353 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700354 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700355 supported = append(supported, Host, HostCross)
356 }
Colin Crossa4190c12016-07-12 13:11:25 -0700357 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700358 supported = append(supported, Device)
359 }
360 return supported
361 default:
362 return nil
363 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800364}
365
Colin Cross635c3b02016-05-18 15:37:25 -0700366func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800367 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
368 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700369 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800370}
371
Colin Cross635c3b02016-05-18 15:37:25 -0700372func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800373 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800374 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800375 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800376 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800377}
378
Colin Crossce75d2c2016-10-06 16:12:58 -0700379func (a *ModuleBase) SkipInstall() {
380 a.commonProperties.SkipInstall = true
381}
382
Colin Cross635c3b02016-05-18 15:37:25 -0700383func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700384 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800385
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700386 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800387 ctx.VisitDepsDepthFirstIf(isFileInstaller,
388 func(m blueprint.Module) {
389 fileInstaller := m.(fileInstaller)
390 files := fileInstaller.filesToInstall()
391 result = append(result, files...)
392 })
393
394 return result
395}
396
Colin Cross635c3b02016-05-18 15:37:25 -0700397func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800398 return a.installFiles
399}
400
Colin Cross635c3b02016-05-18 15:37:25 -0700401func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800402 return p.noAddressSanitizer
403}
404
Colin Cross635c3b02016-05-18 15:37:25 -0700405func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800406 return false
407}
408
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700409func (p *ModuleBase) InstallInSanitizerDir() bool {
410 return false
411}
412
Colin Cross635c3b02016-05-18 15:37:25 -0700413func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700414 allInstalledFiles := Paths{}
415 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800416 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700417 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700418 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
419 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800420 })
421
Colin Cross9454bfa2015-03-17 13:24:18 -0700422 deps := []string{}
423
Colin Cross3f40fa42015-01-30 17:27:36 -0800424 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700425 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800426 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700427 Rule: blueprint.Phony,
428 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700429 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800430 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700431 })
432 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700433 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700434 }
435
436 if len(allCheckbuildFiles) > 0 {
437 name := ctx.ModuleName() + "-checkbuild"
438 ctx.Build(pctx, blueprint.BuildParams{
439 Rule: blueprint.Phony,
440 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700441 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700442 Optional: true,
443 })
444 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700445 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700446 }
447
448 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800449 suffix := ""
450 if ctx.Config().(Config).EmbeddedInMake() {
451 suffix = "-soong"
452 }
453
Colin Cross9454bfa2015-03-17 13:24:18 -0700454 ctx.Build(pctx, blueprint.BuildParams{
455 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800456 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700457 Implicits: deps,
458 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800459 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700460
461 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800462 }
463}
464
Colin Cross635c3b02016-05-18 15:37:25 -0700465func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700466 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700467 target: a.commonProperties.CompileTarget,
468 targetPrimary: a.commonProperties.CompilePrimary,
Dan Willemsenaa118f92017-04-06 12:49:58 -0700469 vendor: a.commonProperties.Proprietary || a.commonProperties.Vendor,
Colin Cross8b74d172016-09-13 09:59:14 -0700470 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800471 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800472}
473
Colin Cross635c3b02016-05-18 15:37:25 -0700474func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800475 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700476 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700477 ModuleContext: ctx,
478 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
479 installDeps: a.computeInstallDeps(ctx),
480 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800481 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800482 }
483
Colin Cross67a5c132017-05-09 13:45:28 -0700484 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
485 var suffix []string
486 if androidCtx.Os().Class != Device && androidCtx.Os().Class != Generic {
487 suffix = append(suffix, androidCtx.Os().String())
488 }
489 if !androidCtx.PrimaryArch() {
490 suffix = append(suffix, androidCtx.Arch().ArchType.String())
491 }
492
493 ctx.Variable(pctx, "moduleDesc", desc)
494
495 s := ""
496 if len(suffix) > 0 {
497 s = " [" + strings.Join(suffix, " ") + "]"
498 }
499 ctx.Variable(pctx, "moduleDescSuffix", s)
500
Colin Cross9b1d13d2016-09-19 15:18:11 -0700501 if a.Enabled() {
502 a.module.GenerateAndroidBuildActions(androidCtx)
503 if ctx.Failed() {
504 return
505 }
506
507 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
508 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800509 }
510
Colin Cross9b1d13d2016-09-19 15:18:11 -0700511 if a == ctx.FinalModule().(Module).base() {
512 a.generateModuleTarget(ctx)
513 if ctx.Failed() {
514 return
515 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800516 }
517}
518
Colin Crossf6566ed2015-03-24 11:13:38 -0700519type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700520 target Target
521 targetPrimary bool
522 debug bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700523 vendor bool
Colin Cross8b74d172016-09-13 09:59:14 -0700524 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700525}
526
Colin Cross3f40fa42015-01-30 17:27:36 -0800527type androidModuleContext struct {
528 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700529 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700530 installDeps Paths
531 installFiles Paths
532 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800533 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700534 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800535}
536
Colin Cross67a5c132017-05-09 13:45:28 -0700537func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross6ff51382015-12-17 16:39:19 -0800538 a.ModuleContext.Build(pctx, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700539 Rule: ErrorRule,
540 Description: desc,
541 Outputs: outputs,
542 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800543 Args: map[string]string{
544 "error": err.Error(),
545 },
546 })
547 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800548}
549
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800550func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Cross7f19f372016-11-01 11:10:25 -0700551 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700552 a.ninjaError(params.Description, params.Outputs,
553 fmt.Errorf("module %s missing dependencies: %s\n",
554 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800555 return
556 }
557
Colin Cross3f40fa42015-01-30 17:27:36 -0800558 params.Optional = true
559 a.ModuleContext.Build(pctx, params)
560}
561
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700562func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
563 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700564 Rule: params.Rule,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800565 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700566 Outputs: params.Outputs.Strings(),
567 ImplicitOutputs: params.ImplicitOutputs.Strings(),
568 Inputs: params.Inputs.Strings(),
569 Implicits: params.Implicits.Strings(),
570 OrderOnly: params.OrderOnly.Strings(),
571 Args: params.Args,
572 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700573 }
574
Colin Cross67a5c132017-05-09 13:45:28 -0700575 if params.Description != "" {
576 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
577 }
578
Colin Cross33bfb0a2016-11-21 17:23:08 -0800579 if params.Depfile != nil {
580 bparams.Depfile = params.Depfile.String()
581 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700582 if params.Output != nil {
583 bparams.Outputs = append(bparams.Outputs, params.Output.String())
584 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700585 if params.ImplicitOutput != nil {
586 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
587 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700588 if params.Input != nil {
589 bparams.Inputs = append(bparams.Inputs, params.Input.String())
590 }
591 if params.Implicit != nil {
592 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
593 }
594
Colin Cross6ff51382015-12-17 16:39:19 -0800595 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700596 a.ninjaError(bparams.Description, bparams.Outputs,
597 fmt.Errorf("module %s missing dependencies: %s\n",
598 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800599 return
600 }
601
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700602 a.ModuleContext.Build(pctx, bparams)
603}
604
Colin Cross6ff51382015-12-17 16:39:19 -0800605func (a *androidModuleContext) GetMissingDependencies() []string {
606 return a.missingDeps
607}
608
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800609func (a *androidModuleContext) AddMissingDependencies(deps []string) {
610 if deps != nil {
611 a.missingDeps = append(a.missingDeps, deps...)
612 }
613}
614
Colin Crossa1ad8d12016-06-01 17:09:44 -0700615func (a *androidBaseContextImpl) Target() Target {
616 return a.target
617}
618
Colin Cross8b74d172016-09-13 09:59:14 -0700619func (a *androidBaseContextImpl) TargetPrimary() bool {
620 return a.targetPrimary
621}
622
Colin Crossf6566ed2015-03-24 11:13:38 -0700623func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700624 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800625}
626
Colin Crossa1ad8d12016-06-01 17:09:44 -0700627func (a *androidBaseContextImpl) Os() OsType {
628 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800629}
630
Colin Crossf6566ed2015-03-24 11:13:38 -0700631func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700632 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700633}
634
635func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700636 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700637}
638
Colin Cross0af4b842015-04-30 16:36:18 -0700639func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700640 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700641}
642
Colin Cross3edeee12017-04-04 12:59:48 -0700643func (a *androidBaseContextImpl) Windows() bool {
644 return a.target.Os == Windows
645}
646
Colin Crossf6566ed2015-03-24 11:13:38 -0700647func (a *androidBaseContextImpl) Debug() bool {
648 return a.debug
649}
650
Colin Cross1e7d3702016-08-24 15:25:47 -0700651func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700652 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
653 return true
654 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700655 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
656}
657
Colin Cross1332b002015-04-07 17:11:30 -0700658func (a *androidBaseContextImpl) AConfig() Config {
659 return a.config
660}
661
Colin Cross9272ade2016-08-17 15:24:12 -0700662func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
663 return DeviceConfig{a.config.deviceConfig}
664}
665
Dan Willemsenaa118f92017-04-06 12:49:58 -0700666func (a *androidBaseContextImpl) Vendor() bool {
667 return a.vendor
Dan Willemsen782a2d12015-12-21 14:55:28 -0800668}
669
Colin Cross8d8f8e22016-08-03 11:57:50 -0700670func (a *androidModuleContext) InstallInData() bool {
671 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800672}
673
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700674func (a *androidModuleContext) InstallInSanitizerDir() bool {
675 return a.module.InstallInSanitizerDir()
676}
677
Colin Cross893d8162017-04-26 17:34:03 -0700678func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
679 if a.module.base().commonProperties.SkipInstall {
680 return true
681 }
682
683 if a.Device() {
684 if a.AConfig().SkipDeviceInstall() {
685 return true
686 }
687
688 if a.AConfig().SkipMegaDeviceInstall(fullInstallPath.String()) {
689 return true
690 }
691 }
692
693 return false
694}
695
Dan Willemsen782a2d12015-12-21 14:55:28 -0800696func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700697 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700698
Dan Willemsen782a2d12015-12-21 14:55:28 -0800699 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700700 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800701
Colin Cross893d8162017-04-26 17:34:03 -0700702 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700703
Dan Willemsen322acaf2016-01-12 23:07:05 -0800704 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700705
Colin Cross89562dc2016-10-03 17:47:19 -0700706 var implicitDeps, orderOnlyDeps Paths
707
708 if a.Host() {
709 // Installed host modules might be used during the build, depend directly on their
710 // dependencies so their timestamp is updated whenever their dependency is updated
711 implicitDeps = deps
712 } else {
713 orderOnlyDeps = deps
714 }
715
Dan Willemsen322acaf2016-01-12 23:07:05 -0800716 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700717 Rule: Cp,
718 Description: "install " + fullInstallPath.Base(),
719 Output: fullInstallPath,
720 Input: srcPath,
721 Implicits: implicitDeps,
722 OrderOnly: orderOnlyDeps,
723 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800724 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800725
Dan Willemsen322acaf2016-01-12 23:07:05 -0800726 a.installFiles = append(a.installFiles, fullInstallPath)
727 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700728 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700729 return fullInstallPath
730}
731
Colin Crossa2344662016-03-24 13:14:12 -0700732func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700733 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800734}
735
Colin Cross3854a602016-01-11 12:49:11 -0800736func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
737 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700738 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800739
Colin Cross893d8162017-04-26 17:34:03 -0700740 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700741
Colin Cross12fc4972016-01-11 12:49:11 -0800742 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700743 Rule: Symlink,
744 Description: "install symlink " + fullInstallPath.Base(),
745 Output: fullInstallPath,
746 OrderOnly: Paths{srcPath},
747 Default: !a.AConfig().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -0800748 Args: map[string]string{
749 "fromPath": srcPath.String(),
750 },
751 })
Colin Cross3854a602016-01-11 12:49:11 -0800752
Colin Cross12fc4972016-01-11 12:49:11 -0800753 a.installFiles = append(a.installFiles, fullInstallPath)
754 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
755 }
Colin Cross3854a602016-01-11 12:49:11 -0800756 return fullInstallPath
757}
758
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700759func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800760 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
761}
762
Colin Cross3f40fa42015-01-30 17:27:36 -0800763type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700764 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800765}
766
767func isFileInstaller(m blueprint.Module) bool {
768 _, ok := m.(fileInstaller)
769 return ok
770}
771
772func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700773 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800774 return ok
775}
Colin Crossfce53272015-04-08 11:21:40 -0700776
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700777func findStringInSlice(str string, slice []string) int {
778 for i, s := range slice {
779 if s == str {
780 return i
Colin Crossfce53272015-04-08 11:21:40 -0700781 }
782 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700783 return -1
784}
785
Colin Cross068e0fe2016-12-13 15:23:47 -0800786func SrcIsModule(s string) string {
787 if len(s) > 1 && s[0] == ':' {
788 return s[1:]
789 }
790 return ""
791}
792
793type sourceDependencyTag struct {
794 blueprint.BaseDependencyTag
795}
796
797var SourceDepTag sourceDependencyTag
798
799// Returns a list of modules that must be depended on to satisfy filegroup or generated sources
800// modules listed in srcFiles using ":module" syntax
801func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
802 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -0700803 set := make(map[string]bool)
804
Colin Cross068e0fe2016-12-13 15:23:47 -0800805 for _, s := range srcFiles {
806 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -0700807 if _, found := set[m]; found {
808 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
809 } else {
810 set[m] = true
811 deps = append(deps, m)
812 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800813 }
814 }
815
816 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
817}
818
819type SourceFileProducer interface {
820 Srcs() Paths
821}
822
823// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800824// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700825func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800826 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
827}
828
829func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700830 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800831
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700832 for i, e := range excludes {
833 j := findStringInSlice(e, srcFiles)
834 if j != -1 {
835 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
836 }
837
838 excludes[i] = filepath.Join(prefix, e)
839 }
840
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800841 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700842 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -0800843 if m := SrcIsModule(s); m != "" {
844 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
845 if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800846 expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800847 } else {
848 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
849 }
850 } else if pathtools.IsGlob(s) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800851 globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
852 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
853 for i, s := range expandedSrcFiles {
854 expandedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
855 }
Colin Cross8f101b42015-06-17 15:09:06 -0700856 } else {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800857 s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
858 expandedSrcFiles = append(expandedSrcFiles, s)
Colin Cross8f101b42015-06-17 15:09:06 -0700859 }
860 }
861
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800862 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -0700863}
864
Nan Zhang6d34b302017-02-04 17:47:46 -0800865func (ctx *androidModuleContext) RequiredModuleNames() []string {
866 return ctx.module.base().commonProperties.Required
867}
868
Colin Cross7f19f372016-11-01 11:10:25 -0700869func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
870 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700871 if err != nil {
872 ctx.ModuleErrorf("glob: %s", err.Error())
873 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700874 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700875}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700876
Colin Cross463a90e2015-06-17 14:20:06 -0700877func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700878 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -0700879}
880
Colin Cross1f8c52b2015-06-16 16:38:17 -0700881func BuildTargetSingleton() blueprint.Singleton {
882 return &buildTargetSingleton{}
883}
884
Colin Cross87d8b562017-04-25 10:01:55 -0700885func parentDir(dir string) string {
886 dir, _ = filepath.Split(dir)
887 return filepath.Clean(dir)
888}
889
Colin Cross1f8c52b2015-06-16 16:38:17 -0700890type buildTargetSingleton struct{}
891
892func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
893 checkbuildDeps := []string{}
894
Colin Cross87d8b562017-04-25 10:01:55 -0700895 mmTarget := func(dir string) string {
896 return filepath.Join("mm", dir)
897 }
898
899 modulesInDir := make(map[string][]string)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700900
901 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700902 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700903 blueprintDir := a.base().blueprintDir
904 installTarget := a.base().installTarget
905 checkbuildTarget := a.base().checkbuildTarget
906
907 if checkbuildTarget != "" {
908 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
Colin Cross87d8b562017-04-25 10:01:55 -0700909 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700910 }
911
912 if installTarget != "" {
Colin Cross87d8b562017-04-25 10:01:55 -0700913 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700914 }
915 }
916 })
917
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800918 suffix := ""
919 if ctx.Config().(Config).EmbeddedInMake() {
920 suffix = "-soong"
921 }
922
Colin Cross1f8c52b2015-06-16 16:38:17 -0700923 // Create a top-level checkbuild target that depends on all modules
924 ctx.Build(pctx, blueprint.BuildParams{
925 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800926 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700927 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700928 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700929 })
930
Colin Cross87d8b562017-04-25 10:01:55 -0700931 // Ensure ancestor directories are in modulesInDir
932 dirs := sortedKeys(modulesInDir)
933 for _, dir := range dirs {
934 dir := parentDir(dir)
935 for dir != "." && dir != "/" {
936 if _, exists := modulesInDir[dir]; exists {
937 break
938 }
939 modulesInDir[dir] = nil
940 dir = parentDir(dir)
941 }
942 }
943
944 // Make directories build their direct subdirectories
945 dirs = sortedKeys(modulesInDir)
946 for _, dir := range dirs {
947 p := parentDir(dir)
948 if p != "." && p != "/" {
949 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
950 }
951 }
952
953 // Create a mm/<directory> target that depends on all modules in a directory, and depends
954 // on the mm/* targets of all of its subdirectories that contain Android.bp files.
Colin Cross1f8c52b2015-06-16 16:38:17 -0700955 for _, dir := range dirs {
956 ctx.Build(pctx, blueprint.BuildParams{
957 Rule: blueprint.Phony,
Colin Cross87d8b562017-04-25 10:01:55 -0700958 Outputs: []string{mmTarget(dir)},
959 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800960 // HACK: checkbuild should be an optional build, but force it
961 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800962 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700963 })
964 }
965}
Colin Crossd779da42015-12-17 18:00:23 -0800966
967type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700968 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800969 ctx interface {
970 ModuleName(blueprint.Module) string
971 ModuleSubDir(blueprint.Module) string
972 }
973}
974
975func (s AndroidModulesByName) Len() int { return len(s.slice) }
976func (s AndroidModulesByName) Less(i, j int) bool {
977 mi, mj := s.slice[i], s.slice[j]
978 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
979
980 if ni != nj {
981 return ni < nj
982 } else {
983 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
984 }
985}
986func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }