blob: 7f541be2921691909f431815c2aa2f3ff4518392 [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 Cross36242852017-06-23 15:06:31 -0700111
112 AddProperties(props ...interface{})
113 GetProperties() []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800114}
115
Colin Crossfc754582016-05-17 16:34:16 -0700116type nameProperties struct {
117 // The name of the module. Must be unique across all modules.
Colin Crossc77f9d12015-04-02 13:54:39 -0700118 Name string
Colin Crossfc754582016-05-17 16:34:16 -0700119}
120
121type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700122 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800123
Dan Willemsen0effe062015-11-30 16:06:01 -0800124 // emit build rules for this module
125 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800126
Colin Cross7d5136f2015-05-11 13:39:40 -0700127 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
129 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
130 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700131 Compile_multilib string `android:"arch_variant"`
132
133 Target struct {
134 Host struct {
135 Compile_multilib string
136 }
137 Android struct {
138 Compile_multilib string
139 }
140 }
141
142 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800143
Dan Willemsen782a2d12015-12-21 14:55:28 -0800144 // whether this is a proprietary vendor module, and should be installed into /vendor
145 Proprietary bool
146
Colin Cross55708f32017-03-20 13:23:34 -0700147 // vendor who owns this module
148 Owner string
149
Dan Willemsenaa118f92017-04-06 12:49:58 -0700150 // whether this module is device specific and should be installed into /vendor
151 Vendor bool
152
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700153 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
154 // file
155 Logtags []string
156
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700157 // init.rc files to be installed if this module is installed
158 Init_rc []string
159
Chris Wolfe998306e2016-08-15 14:47:23 -0400160 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700161 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400162
Colin Crossa1ad8d12016-06-01 17:09:44 -0700163 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700164 CompileTarget Target `blueprint:"mutated"`
165 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800166
167 // Set by InitAndroidModule
168 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700169 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700170
171 SkipInstall bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800172}
173
174type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700175 Host_supported *bool
176 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800177}
178
Colin Crossc472d572015-03-17 15:06:21 -0700179type Multilib string
180
181const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700182 MultilibBoth Multilib = "both"
183 MultilibFirst Multilib = "first"
184 MultilibCommon Multilib = "common"
185 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700186)
187
Colin Crossa1ad8d12016-06-01 17:09:44 -0700188type HostOrDeviceSupported int
189
190const (
191 _ HostOrDeviceSupported = iota
192 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700193 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700194 DeviceSupported
195 HostAndDeviceSupported
196 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700197 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700198)
199
Colin Cross36242852017-06-23 15:06:31 -0700200func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800201 base := m.base()
202 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700203
Colin Cross36242852017-06-23 15:06:31 -0700204 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700205 &base.nameProperties,
206 &base.commonProperties,
207 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700208}
209
Colin Cross36242852017-06-23 15:06:31 -0700210func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
211 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700212
213 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800214 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700215 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700216 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800217
Dan Willemsen218f6562015-07-08 18:13:11 -0700218 switch hod {
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700219 case HostAndDeviceSupported, HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700220 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800221 }
222
Colin Cross36242852017-06-23 15:06:31 -0700223 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800224}
225
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800226// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800227// modules. It should be included as an anonymous field in every module
228// struct definition. InitAndroidModule should then be called from the module's
229// factory function, and the return values from InitAndroidModule should be
230// returned from the factory function.
231//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800232// The ModuleBase type is responsible for implementing the GenerateBuildActions
233// method to support the blueprint.Module interface. This method will then call
234// the module's GenerateAndroidBuildActions method once for each build variant
235// that is to be built. GenerateAndroidBuildActions is passed a
236// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800237// AndroidModuleContext exposes extra functionality specific to the Android build
238// system including details about the particular build variant that is to be
239// generated.
240//
241// For example:
242//
243// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800244// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800245// )
246//
247// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800248// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800249// properties struct {
250// MyProperty string
251// }
252// }
253//
Colin Cross36242852017-06-23 15:06:31 -0700254// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800255// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700256// m.AddProperties(&m.properties)
257// android.InitAndroidModule(m)
258// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800259// }
260//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800261// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800262// // Get the CPU architecture for the current build variant.
263// variantArch := ctx.Arch()
264//
265// // ...
266// }
Colin Cross635c3b02016-05-18 15:37:25 -0700267type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800268 // Putting the curiously recurring thing pointing to the thing that contains
269 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700270 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700271 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800272
Colin Crossfc754582016-05-17 16:34:16 -0700273 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800274 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700275 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800276 hostAndDeviceProperties hostAndDeviceProperties
277 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700278 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700279 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800280
281 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700282 installFiles Paths
283 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700284
285 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
286 // Only set on the final variant of each module
287 installTarget string
288 checkbuildTarget string
289 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700290
Colin Cross178a5092016-09-13 13:42:32 -0700291 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700292
293 registerProps []interface{}
294}
295
296func (a *ModuleBase) AddProperties(props ...interface{}) {
297 a.registerProps = append(a.registerProps, props...)
298}
299
300func (a *ModuleBase) GetProperties() []interface{} {
301 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800302}
303
Colin Crossce75d2c2016-10-06 16:12:58 -0700304// Name returns the name of the module. It may be overridden by individual module types, for
305// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700306func (a *ModuleBase) Name() string {
307 return a.nameProperties.Name
308}
309
Colin Crossce75d2c2016-10-06 16:12:58 -0700310// BaseModuleName returns the name of the module as specified in the blueprints file.
311func (a *ModuleBase) BaseModuleName() string {
312 return a.nameProperties.Name
313}
314
Colin Cross635c3b02016-05-18 15:37:25 -0700315func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800316 return a
317}
318
Colin Cross8b74d172016-09-13 09:59:14 -0700319func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700320 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700321 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700322}
323
Colin Crossa1ad8d12016-06-01 17:09:44 -0700324func (a *ModuleBase) Target() Target {
325 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800326}
327
Colin Cross8b74d172016-09-13 09:59:14 -0700328func (a *ModuleBase) TargetPrimary() bool {
329 return a.commonProperties.CompilePrimary
330}
331
Colin Crossa1ad8d12016-06-01 17:09:44 -0700332func (a *ModuleBase) Os() OsType {
333 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800334}
335
Colin Cross635c3b02016-05-18 15:37:25 -0700336func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700337 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800338}
339
Colin Cross635c3b02016-05-18 15:37:25 -0700340func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700341 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800342}
343
Dan Willemsen0b24c742016-10-04 15:13:37 -0700344func (a *ModuleBase) ArchSpecific() bool {
345 return a.commonProperties.ArchSpecific
346}
347
Colin Crossa1ad8d12016-06-01 17:09:44 -0700348func (a *ModuleBase) OsClassSupported() []OsClass {
349 switch a.commonProperties.HostOrDeviceSupported {
350 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700351 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700352 case HostSupportedNoCross:
353 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700354 case DeviceSupported:
355 return []OsClass{Device}
356 case HostAndDeviceSupported:
357 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700358 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700359 supported = append(supported, Host, HostCross)
360 }
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700361 if a.hostAndDeviceProperties.Device_supported == nil ||
362 *a.hostAndDeviceProperties.Device_supported {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700363 supported = append(supported, Device)
364 }
365 return supported
366 default:
367 return nil
368 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800369}
370
Colin Cross635c3b02016-05-18 15:37:25 -0700371func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800372 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
373 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Nan Zhang1a0f09b2017-07-05 10:35:11 -0700374 (a.hostAndDeviceProperties.Device_supported == nil ||
375 *a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800376}
377
Colin Cross635c3b02016-05-18 15:37:25 -0700378func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800379 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800380 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800381 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800382 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800383}
384
Colin Crossce75d2c2016-10-06 16:12:58 -0700385func (a *ModuleBase) SkipInstall() {
386 a.commonProperties.SkipInstall = true
387}
388
Colin Cross635c3b02016-05-18 15:37:25 -0700389func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700390 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800391
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700392 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 ctx.VisitDepsDepthFirstIf(isFileInstaller,
394 func(m blueprint.Module) {
395 fileInstaller := m.(fileInstaller)
396 files := fileInstaller.filesToInstall()
397 result = append(result, files...)
398 })
399
400 return result
401}
402
Colin Cross635c3b02016-05-18 15:37:25 -0700403func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800404 return a.installFiles
405}
406
Colin Cross635c3b02016-05-18 15:37:25 -0700407func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800408 return p.noAddressSanitizer
409}
410
Colin Cross635c3b02016-05-18 15:37:25 -0700411func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800412 return false
413}
414
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700415func (p *ModuleBase) InstallInSanitizerDir() bool {
416 return false
417}
418
Colin Cross635c3b02016-05-18 15:37:25 -0700419func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700420 allInstalledFiles := Paths{}
421 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800422 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700423 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700424 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
425 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800426 })
427
Colin Cross9454bfa2015-03-17 13:24:18 -0700428 deps := []string{}
429
Colin Cross3f40fa42015-01-30 17:27:36 -0800430 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700431 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800432 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700433 Rule: blueprint.Phony,
434 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700435 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800436 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700437 })
438 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700439 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700440 }
441
442 if len(allCheckbuildFiles) > 0 {
443 name := ctx.ModuleName() + "-checkbuild"
444 ctx.Build(pctx, blueprint.BuildParams{
445 Rule: blueprint.Phony,
446 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700447 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700448 Optional: true,
449 })
450 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700451 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700452 }
453
454 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800455 suffix := ""
456 if ctx.Config().(Config).EmbeddedInMake() {
457 suffix = "-soong"
458 }
459
Colin Cross9454bfa2015-03-17 13:24:18 -0700460 ctx.Build(pctx, blueprint.BuildParams{
461 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800462 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700463 Implicits: deps,
464 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800465 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700466
467 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800468 }
469}
470
Colin Cross635c3b02016-05-18 15:37:25 -0700471func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700472 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700473 target: a.commonProperties.CompileTarget,
474 targetPrimary: a.commonProperties.CompilePrimary,
Dan Willemsenaa118f92017-04-06 12:49:58 -0700475 vendor: a.commonProperties.Proprietary || a.commonProperties.Vendor,
Colin Cross8b74d172016-09-13 09:59:14 -0700476 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800477 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800478}
479
Colin Cross635c3b02016-05-18 15:37:25 -0700480func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800481 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700482 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700483 ModuleContext: ctx,
484 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
485 installDeps: a.computeInstallDeps(ctx),
486 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800487 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800488 }
489
Colin Cross67a5c132017-05-09 13:45:28 -0700490 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
491 var suffix []string
492 if androidCtx.Os().Class != Device && androidCtx.Os().Class != Generic {
493 suffix = append(suffix, androidCtx.Os().String())
494 }
495 if !androidCtx.PrimaryArch() {
496 suffix = append(suffix, androidCtx.Arch().ArchType.String())
497 }
498
499 ctx.Variable(pctx, "moduleDesc", desc)
500
501 s := ""
502 if len(suffix) > 0 {
503 s = " [" + strings.Join(suffix, " ") + "]"
504 }
505 ctx.Variable(pctx, "moduleDescSuffix", s)
506
Colin Cross9b1d13d2016-09-19 15:18:11 -0700507 if a.Enabled() {
508 a.module.GenerateAndroidBuildActions(androidCtx)
509 if ctx.Failed() {
510 return
511 }
512
513 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
514 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800515 }
516
Colin Cross9b1d13d2016-09-19 15:18:11 -0700517 if a == ctx.FinalModule().(Module).base() {
518 a.generateModuleTarget(ctx)
519 if ctx.Failed() {
520 return
521 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800522 }
523}
524
Colin Crossf6566ed2015-03-24 11:13:38 -0700525type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700526 target Target
527 targetPrimary bool
528 debug bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700529 vendor bool
Colin Cross8b74d172016-09-13 09:59:14 -0700530 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700531}
532
Colin Cross3f40fa42015-01-30 17:27:36 -0800533type androidModuleContext struct {
534 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700535 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700536 installDeps Paths
537 installFiles Paths
538 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800539 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700540 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800541}
542
Colin Cross67a5c132017-05-09 13:45:28 -0700543func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross6ff51382015-12-17 16:39:19 -0800544 a.ModuleContext.Build(pctx, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700545 Rule: ErrorRule,
546 Description: desc,
547 Outputs: outputs,
548 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800549 Args: map[string]string{
550 "error": err.Error(),
551 },
552 })
553 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800554}
555
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800556func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Cross7f19f372016-11-01 11:10:25 -0700557 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700558 a.ninjaError(params.Description, params.Outputs,
559 fmt.Errorf("module %s missing dependencies: %s\n",
560 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800561 return
562 }
563
Colin Cross3f40fa42015-01-30 17:27:36 -0800564 params.Optional = true
565 a.ModuleContext.Build(pctx, params)
566}
567
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700568func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
569 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700570 Rule: params.Rule,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800571 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700572 Outputs: params.Outputs.Strings(),
573 ImplicitOutputs: params.ImplicitOutputs.Strings(),
574 Inputs: params.Inputs.Strings(),
575 Implicits: params.Implicits.Strings(),
576 OrderOnly: params.OrderOnly.Strings(),
577 Args: params.Args,
578 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700579 }
580
Colin Cross67a5c132017-05-09 13:45:28 -0700581 if params.Description != "" {
582 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
583 }
584
Colin Cross33bfb0a2016-11-21 17:23:08 -0800585 if params.Depfile != nil {
586 bparams.Depfile = params.Depfile.String()
587 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700588 if params.Output != nil {
589 bparams.Outputs = append(bparams.Outputs, params.Output.String())
590 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700591 if params.ImplicitOutput != nil {
592 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
593 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700594 if params.Input != nil {
595 bparams.Inputs = append(bparams.Inputs, params.Input.String())
596 }
597 if params.Implicit != nil {
598 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
599 }
600
Colin Cross6ff51382015-12-17 16:39:19 -0800601 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700602 a.ninjaError(bparams.Description, bparams.Outputs,
603 fmt.Errorf("module %s missing dependencies: %s\n",
604 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800605 return
606 }
607
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700608 a.ModuleContext.Build(pctx, bparams)
609}
610
Colin Cross6ff51382015-12-17 16:39:19 -0800611func (a *androidModuleContext) GetMissingDependencies() []string {
612 return a.missingDeps
613}
614
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800615func (a *androidModuleContext) AddMissingDependencies(deps []string) {
616 if deps != nil {
617 a.missingDeps = append(a.missingDeps, deps...)
618 }
619}
620
Colin Crossa1ad8d12016-06-01 17:09:44 -0700621func (a *androidBaseContextImpl) Target() Target {
622 return a.target
623}
624
Colin Cross8b74d172016-09-13 09:59:14 -0700625func (a *androidBaseContextImpl) TargetPrimary() bool {
626 return a.targetPrimary
627}
628
Colin Crossf6566ed2015-03-24 11:13:38 -0700629func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700630 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800631}
632
Colin Crossa1ad8d12016-06-01 17:09:44 -0700633func (a *androidBaseContextImpl) Os() OsType {
634 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800635}
636
Colin Crossf6566ed2015-03-24 11:13:38 -0700637func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700638 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700639}
640
641func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700642 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700643}
644
Colin Cross0af4b842015-04-30 16:36:18 -0700645func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700646 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700647}
648
Colin Cross3edeee12017-04-04 12:59:48 -0700649func (a *androidBaseContextImpl) Windows() bool {
650 return a.target.Os == Windows
651}
652
Colin Crossf6566ed2015-03-24 11:13:38 -0700653func (a *androidBaseContextImpl) Debug() bool {
654 return a.debug
655}
656
Colin Cross1e7d3702016-08-24 15:25:47 -0700657func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700658 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
659 return true
660 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700661 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
662}
663
Colin Cross1332b002015-04-07 17:11:30 -0700664func (a *androidBaseContextImpl) AConfig() Config {
665 return a.config
666}
667
Colin Cross9272ade2016-08-17 15:24:12 -0700668func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
669 return DeviceConfig{a.config.deviceConfig}
670}
671
Dan Willemsenaa118f92017-04-06 12:49:58 -0700672func (a *androidBaseContextImpl) Vendor() bool {
673 return a.vendor
Dan Willemsen782a2d12015-12-21 14:55:28 -0800674}
675
Colin Cross8d8f8e22016-08-03 11:57:50 -0700676func (a *androidModuleContext) InstallInData() bool {
677 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800678}
679
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700680func (a *androidModuleContext) InstallInSanitizerDir() bool {
681 return a.module.InstallInSanitizerDir()
682}
683
Colin Cross893d8162017-04-26 17:34:03 -0700684func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
685 if a.module.base().commonProperties.SkipInstall {
686 return true
687 }
688
689 if a.Device() {
690 if a.AConfig().SkipDeviceInstall() {
691 return true
692 }
693
694 if a.AConfig().SkipMegaDeviceInstall(fullInstallPath.String()) {
695 return true
696 }
697 }
698
699 return false
700}
701
Dan Willemsen782a2d12015-12-21 14:55:28 -0800702func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700703 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700704
Dan Willemsen782a2d12015-12-21 14:55:28 -0800705 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700706 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800707
Colin Cross893d8162017-04-26 17:34:03 -0700708 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700709
Dan Willemsen322acaf2016-01-12 23:07:05 -0800710 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700711
Colin Cross89562dc2016-10-03 17:47:19 -0700712 var implicitDeps, orderOnlyDeps Paths
713
714 if a.Host() {
715 // Installed host modules might be used during the build, depend directly on their
716 // dependencies so their timestamp is updated whenever their dependency is updated
717 implicitDeps = deps
718 } else {
719 orderOnlyDeps = deps
720 }
721
Dan Willemsen322acaf2016-01-12 23:07:05 -0800722 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700723 Rule: Cp,
724 Description: "install " + fullInstallPath.Base(),
725 Output: fullInstallPath,
726 Input: srcPath,
727 Implicits: implicitDeps,
728 OrderOnly: orderOnlyDeps,
729 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800730 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800731
Dan Willemsen322acaf2016-01-12 23:07:05 -0800732 a.installFiles = append(a.installFiles, fullInstallPath)
733 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700734 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700735 return fullInstallPath
736}
737
Colin Crossa2344662016-03-24 13:14:12 -0700738func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700739 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800740}
741
Colin Cross3854a602016-01-11 12:49:11 -0800742func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
743 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700744 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800745
Colin Cross893d8162017-04-26 17:34:03 -0700746 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700747
Colin Cross12fc4972016-01-11 12:49:11 -0800748 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700749 Rule: Symlink,
750 Description: "install symlink " + fullInstallPath.Base(),
751 Output: fullInstallPath,
752 OrderOnly: Paths{srcPath},
753 Default: !a.AConfig().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -0800754 Args: map[string]string{
755 "fromPath": srcPath.String(),
756 },
757 })
Colin Cross3854a602016-01-11 12:49:11 -0800758
Colin Cross12fc4972016-01-11 12:49:11 -0800759 a.installFiles = append(a.installFiles, fullInstallPath)
760 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
761 }
Colin Cross3854a602016-01-11 12:49:11 -0800762 return fullInstallPath
763}
764
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700765func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800766 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
767}
768
Colin Cross3f40fa42015-01-30 17:27:36 -0800769type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700770 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800771}
772
773func isFileInstaller(m blueprint.Module) bool {
774 _, ok := m.(fileInstaller)
775 return ok
776}
777
778func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700779 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800780 return ok
781}
Colin Crossfce53272015-04-08 11:21:40 -0700782
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700783func findStringInSlice(str string, slice []string) int {
784 for i, s := range slice {
785 if s == str {
786 return i
Colin Crossfce53272015-04-08 11:21:40 -0700787 }
788 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700789 return -1
790}
791
Colin Cross068e0fe2016-12-13 15:23:47 -0800792func SrcIsModule(s string) string {
793 if len(s) > 1 && s[0] == ':' {
794 return s[1:]
795 }
796 return ""
797}
798
799type sourceDependencyTag struct {
800 blueprint.BaseDependencyTag
801}
802
803var SourceDepTag sourceDependencyTag
804
805// Returns a list of modules that must be depended on to satisfy filegroup or generated sources
806// modules listed in srcFiles using ":module" syntax
807func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
808 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -0700809 set := make(map[string]bool)
810
Colin Cross068e0fe2016-12-13 15:23:47 -0800811 for _, s := range srcFiles {
812 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -0700813 if _, found := set[m]; found {
814 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
815 } else {
816 set[m] = true
817 deps = append(deps, m)
818 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800819 }
820 }
821
822 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
823}
824
825type SourceFileProducer interface {
826 Srcs() Paths
827}
828
829// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800830// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700831func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800832 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
833}
834
835func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700836 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800837
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700838 for i, e := range excludes {
839 j := findStringInSlice(e, srcFiles)
840 if j != -1 {
841 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
842 }
843
844 excludes[i] = filepath.Join(prefix, e)
845 }
846
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800847 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700848 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -0800849 if m := SrcIsModule(s); m != "" {
850 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
851 if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800852 expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800853 } else {
854 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
855 }
856 } else if pathtools.IsGlob(s) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800857 globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
858 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
859 for i, s := range expandedSrcFiles {
860 expandedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
861 }
Colin Cross8f101b42015-06-17 15:09:06 -0700862 } else {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800863 s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
864 expandedSrcFiles = append(expandedSrcFiles, s)
Colin Cross8f101b42015-06-17 15:09:06 -0700865 }
866 }
867
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800868 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -0700869}
870
Nan Zhang6d34b302017-02-04 17:47:46 -0800871func (ctx *androidModuleContext) RequiredModuleNames() []string {
872 return ctx.module.base().commonProperties.Required
873}
874
Colin Cross7f19f372016-11-01 11:10:25 -0700875func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
876 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700877 if err != nil {
878 ctx.ModuleErrorf("glob: %s", err.Error())
879 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700880 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700881}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700882
Colin Cross463a90e2015-06-17 14:20:06 -0700883func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700884 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -0700885}
886
Colin Cross1f8c52b2015-06-16 16:38:17 -0700887func BuildTargetSingleton() blueprint.Singleton {
888 return &buildTargetSingleton{}
889}
890
Colin Cross87d8b562017-04-25 10:01:55 -0700891func parentDir(dir string) string {
892 dir, _ = filepath.Split(dir)
893 return filepath.Clean(dir)
894}
895
Colin Cross1f8c52b2015-06-16 16:38:17 -0700896type buildTargetSingleton struct{}
897
898func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
899 checkbuildDeps := []string{}
900
Colin Cross87d8b562017-04-25 10:01:55 -0700901 mmTarget := func(dir string) string {
902 return filepath.Join("mm", dir)
903 }
904
905 modulesInDir := make(map[string][]string)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700906
907 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700908 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700909 blueprintDir := a.base().blueprintDir
910 installTarget := a.base().installTarget
911 checkbuildTarget := a.base().checkbuildTarget
912
913 if checkbuildTarget != "" {
914 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
Colin Cross87d8b562017-04-25 10:01:55 -0700915 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700916 }
917
918 if installTarget != "" {
Colin Cross87d8b562017-04-25 10:01:55 -0700919 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700920 }
921 }
922 })
923
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800924 suffix := ""
925 if ctx.Config().(Config).EmbeddedInMake() {
926 suffix = "-soong"
927 }
928
Colin Cross1f8c52b2015-06-16 16:38:17 -0700929 // Create a top-level checkbuild target that depends on all modules
930 ctx.Build(pctx, blueprint.BuildParams{
931 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800932 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700933 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700934 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700935 })
936
Colin Cross87d8b562017-04-25 10:01:55 -0700937 // Ensure ancestor directories are in modulesInDir
938 dirs := sortedKeys(modulesInDir)
939 for _, dir := range dirs {
940 dir := parentDir(dir)
941 for dir != "." && dir != "/" {
942 if _, exists := modulesInDir[dir]; exists {
943 break
944 }
945 modulesInDir[dir] = nil
946 dir = parentDir(dir)
947 }
948 }
949
950 // Make directories build their direct subdirectories
951 dirs = sortedKeys(modulesInDir)
952 for _, dir := range dirs {
953 p := parentDir(dir)
954 if p != "." && p != "/" {
955 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
956 }
957 }
958
959 // Create a mm/<directory> target that depends on all modules in a directory, and depends
960 // on the mm/* targets of all of its subdirectories that contain Android.bp files.
Colin Cross1f8c52b2015-06-16 16:38:17 -0700961 for _, dir := range dirs {
962 ctx.Build(pctx, blueprint.BuildParams{
963 Rule: blueprint.Phony,
Colin Cross87d8b562017-04-25 10:01:55 -0700964 Outputs: []string{mmTarget(dir)},
965 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800966 // HACK: checkbuild should be an optional build, but force it
967 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800968 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700969 })
970 }
971}
Colin Crossd779da42015-12-17 18:00:23 -0800972
973type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700974 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800975 ctx interface {
976 ModuleName(blueprint.Module) string
977 ModuleSubDir(blueprint.Module) string
978 }
979}
980
981func (s AndroidModulesByName) Len() int { return len(s.slice) }
982func (s AndroidModulesByName) Less(i, j int) bool {
983 mi, mj := s.slice[i], s.slice[j]
984 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
985
986 if ni != nj {
987 return ni < nj
988 } else {
989 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
990 }
991}
992func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }