blob: a38c6177cc5282194efc44a228c91f91886c874b [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
Dan Willemsen9f3c5742016-11-03 14:28:31 -070039 Output WritablePath
40 Outputs WritablePaths
41 ImplicitOutput WritablePath
42 ImplicitOutputs WritablePaths
43 Input Path
44 Inputs Paths
45 Implicit Path
46 Implicits Paths
47 OrderOnly Paths
48 Default bool
49 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070050}
51
Colin Crossf6566ed2015-03-24 11:13:38 -070052type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070053 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070054 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070055 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070056 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070057 Host() bool
58 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070059 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070060 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070061 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070062 PrimaryArch() bool
Dan Willemsenaa118f92017-04-06 12:49:58 -070063 Vendor() bool
Colin Cross1332b002015-04-07 17:11:30 -070064 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070065 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070066}
67
Colin Cross635c3b02016-05-18 15:37:25 -070068type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070069 blueprint.BaseModuleContext
70 androidBaseContext
71}
72
Colin Cross635c3b02016-05-18 15:37:25 -070073type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080074 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070075 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080076
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077 // Similar to Build, but takes Paths instead of []string,
78 // and performs more verification.
79 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070080
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 ExpandSources(srcFiles, excludes []string) Paths
Colin Crossfaeb7aa2017-02-01 14:12:44 -080082 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -070083 Glob(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070084
Colin Crossa2344662016-03-24 13:14:12 -070085 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
86 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080087 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070088 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080089
90 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070091
Colin Cross8d8f8e22016-08-03 11:57:50 -070092 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -070093 InstallInSanitizerDir() bool
Nan Zhang6d34b302017-02-04 17:47:46 -080094
95 RequiredModuleNames() []string
Colin Cross3f40fa42015-01-30 17:27:36 -080096}
97
Colin Cross635c3b02016-05-18 15:37:25 -070098type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080099 blueprint.Module
100
Colin Cross635c3b02016-05-18 15:37:25 -0700101 GenerateAndroidBuildActions(ModuleContext)
Colin Cross1e676be2016-10-12 14:38:15 -0700102 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800103
Colin Cross635c3b02016-05-18 15:37:25 -0700104 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800105 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700106 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800107 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700108 InstallInSanitizerDir() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800109 SkipInstall()
Colin Cross3f40fa42015-01-30 17:27:36 -0800110}
111
Colin Crossfc754582016-05-17 16:34:16 -0700112type nameProperties struct {
113 // The name of the module. Must be unique across all modules.
Colin Crossc77f9d12015-04-02 13:54:39 -0700114 Name string
Colin Crossfc754582016-05-17 16:34:16 -0700115}
116
117type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700118 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800119
Dan Willemsen0effe062015-11-30 16:06:01 -0800120 // emit build rules for this module
121 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800122
Colin Cross7d5136f2015-05-11 13:39:40 -0700123 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800124 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
125 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
126 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700127 Compile_multilib string `android:"arch_variant"`
128
129 Target struct {
130 Host struct {
131 Compile_multilib string
132 }
133 Android struct {
134 Compile_multilib string
135 }
136 }
137
138 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800139
Dan Willemsen782a2d12015-12-21 14:55:28 -0800140 // whether this is a proprietary vendor module, and should be installed into /vendor
141 Proprietary bool
142
Colin Cross55708f32017-03-20 13:23:34 -0700143 // vendor who owns this module
144 Owner string
145
Dan Willemsenaa118f92017-04-06 12:49:58 -0700146 // whether this module is device specific and should be installed into /vendor
147 Vendor bool
148
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700149 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
150 // file
151 Logtags []string
152
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700153 // init.rc files to be installed if this module is installed
154 Init_rc []string
155
Chris Wolfe998306e2016-08-15 14:47:23 -0400156 // names of other modules to install if this module is installed
157 Required []string
158
Colin Crossa1ad8d12016-06-01 17:09:44 -0700159 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700160 CompileTarget Target `blueprint:"mutated"`
161 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800162
163 // Set by InitAndroidModule
164 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700165 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700166
167 SkipInstall bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800168}
169
170type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700171 Host_supported *bool
172 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800173}
174
Colin Crossc472d572015-03-17 15:06:21 -0700175type Multilib string
176
177const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700178 MultilibBoth Multilib = "both"
179 MultilibFirst Multilib = "first"
180 MultilibCommon Multilib = "common"
181 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700182)
183
Colin Crossa1ad8d12016-06-01 17:09:44 -0700184type HostOrDeviceSupported int
185
186const (
187 _ HostOrDeviceSupported = iota
188 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700189 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700190 DeviceSupported
191 HostAndDeviceSupported
192 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700193 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700194)
195
Colin Cross635c3b02016-05-18 15:37:25 -0700196func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800197 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
198
199 base := m.base()
200 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700201
Colin Crossfc754582016-05-17 16:34:16 -0700202 propertyStructs = append(propertyStructs,
203 &base.nameProperties,
204 &base.commonProperties,
205 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700206
207 return m, propertyStructs
208}
209
Colin Cross635c3b02016-05-18 15:37:25 -0700210func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700211 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
212
213 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
214
215 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800216 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700217 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700218 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800219
Dan Willemsen218f6562015-07-08 18:13:11 -0700220 switch hod {
221 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800222 // Default to module to device supported, host not supported, can override in module
223 // properties
Colin Crossa4190c12016-07-12 13:11:25 -0700224 base.hostAndDeviceProperties.Device_supported = boolPtr(true)
Dan Willemsen218f6562015-07-08 18:13:11 -0700225 fallthrough
226 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800227 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
228 }
229
Colin Crosscfad1192015-11-02 16:43:11 -0800230 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800231}
232
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800233// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800234// modules. It should be included as an anonymous field in every module
235// struct definition. InitAndroidModule should then be called from the module's
236// factory function, and the return values from InitAndroidModule should be
237// returned from the factory function.
238//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800239// The ModuleBase type is responsible for implementing the GenerateBuildActions
240// method to support the blueprint.Module interface. This method will then call
241// the module's GenerateAndroidBuildActions method once for each build variant
242// that is to be built. GenerateAndroidBuildActions is passed a
243// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800244// AndroidModuleContext exposes extra functionality specific to the Android build
245// system including details about the particular build variant that is to be
246// generated.
247//
248// For example:
249//
250// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800251// "android/soong/android"
Colin Cross70b40592015-03-23 12:57:34 -0700252// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800253// )
254//
255// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800256// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800257// properties struct {
258// MyProperty string
259// }
260// }
261//
262// func NewMyModule() (blueprint.Module, []interface{}) {
263// m := &myModule{}
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800264// return android.InitAndroidModule(m, &m.properties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800265// }
266//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800267// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800268// // Get the CPU architecture for the current build variant.
269// variantArch := ctx.Arch()
270//
271// // ...
272// }
Colin Cross635c3b02016-05-18 15:37:25 -0700273type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800274 // Putting the curiously recurring thing pointing to the thing that contains
275 // the thing pattern to good use.
Colin Cross635c3b02016-05-18 15:37:25 -0700276 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800277
Colin Crossfc754582016-05-17 16:34:16 -0700278 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800279 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700280 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800281 hostAndDeviceProperties hostAndDeviceProperties
282 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700283 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700284 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800285
286 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700287 installFiles Paths
288 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700289
290 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
291 // Only set on the final variant of each module
292 installTarget string
293 checkbuildTarget string
294 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700295
Colin Cross178a5092016-09-13 13:42:32 -0700296 hooks hooks
Colin Cross3f40fa42015-01-30 17:27:36 -0800297}
298
Colin Crossce75d2c2016-10-06 16:12:58 -0700299// Name returns the name of the module. It may be overridden by individual module types, for
300// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700301func (a *ModuleBase) Name() string {
302 return a.nameProperties.Name
303}
304
Colin Crossce75d2c2016-10-06 16:12:58 -0700305// BaseModuleName returns the name of the module as specified in the blueprints file.
306func (a *ModuleBase) BaseModuleName() string {
307 return a.nameProperties.Name
308}
309
Colin Cross635c3b02016-05-18 15:37:25 -0700310func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800311 return a
312}
313
Colin Cross8b74d172016-09-13 09:59:14 -0700314func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700315 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700316 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700317}
318
Colin Crossa1ad8d12016-06-01 17:09:44 -0700319func (a *ModuleBase) Target() Target {
320 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800321}
322
Colin Cross8b74d172016-09-13 09:59:14 -0700323func (a *ModuleBase) TargetPrimary() bool {
324 return a.commonProperties.CompilePrimary
325}
326
Colin Crossa1ad8d12016-06-01 17:09:44 -0700327func (a *ModuleBase) Os() OsType {
328 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800329}
330
Colin Cross635c3b02016-05-18 15:37:25 -0700331func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700332 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800333}
334
Colin Cross635c3b02016-05-18 15:37:25 -0700335func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700336 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800337}
338
Dan Willemsen0b24c742016-10-04 15:13:37 -0700339func (a *ModuleBase) ArchSpecific() bool {
340 return a.commonProperties.ArchSpecific
341}
342
Colin Crossa1ad8d12016-06-01 17:09:44 -0700343func (a *ModuleBase) OsClassSupported() []OsClass {
344 switch a.commonProperties.HostOrDeviceSupported {
345 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700346 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700347 case HostSupportedNoCross:
348 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700349 case DeviceSupported:
350 return []OsClass{Device}
351 case HostAndDeviceSupported:
352 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700353 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700354 supported = append(supported, Host, HostCross)
355 }
Colin Crossa4190c12016-07-12 13:11:25 -0700356 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700357 supported = append(supported, Device)
358 }
359 return supported
360 default:
361 return nil
362 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800363}
364
Colin Cross635c3b02016-05-18 15:37:25 -0700365func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800366 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
367 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700368 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800369}
370
Colin Cross635c3b02016-05-18 15:37:25 -0700371func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800372 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800373 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800374 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800375 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800376}
377
Colin Crossce75d2c2016-10-06 16:12:58 -0700378func (a *ModuleBase) SkipInstall() {
379 a.commonProperties.SkipInstall = true
380}
381
Colin Cross635c3b02016-05-18 15:37:25 -0700382func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700383 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800384
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700385 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800386 ctx.VisitDepsDepthFirstIf(isFileInstaller,
387 func(m blueprint.Module) {
388 fileInstaller := m.(fileInstaller)
389 files := fileInstaller.filesToInstall()
390 result = append(result, files...)
391 })
392
393 return result
394}
395
Colin Cross635c3b02016-05-18 15:37:25 -0700396func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800397 return a.installFiles
398}
399
Colin Cross635c3b02016-05-18 15:37:25 -0700400func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800401 return p.noAddressSanitizer
402}
403
Colin Cross635c3b02016-05-18 15:37:25 -0700404func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800405 return false
406}
407
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700408func (p *ModuleBase) InstallInSanitizerDir() bool {
409 return false
410}
411
Colin Cross635c3b02016-05-18 15:37:25 -0700412func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700413 allInstalledFiles := Paths{}
414 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800415 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700416 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700417 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
418 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800419 })
420
Colin Cross9454bfa2015-03-17 13:24:18 -0700421 deps := []string{}
422
Colin Cross3f40fa42015-01-30 17:27:36 -0800423 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700424 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800425 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700426 Rule: blueprint.Phony,
427 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700428 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800429 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700430 })
431 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700432 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700433 }
434
435 if len(allCheckbuildFiles) > 0 {
436 name := ctx.ModuleName() + "-checkbuild"
437 ctx.Build(pctx, blueprint.BuildParams{
438 Rule: blueprint.Phony,
439 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700440 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700441 Optional: true,
442 })
443 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700444 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700445 }
446
447 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800448 suffix := ""
449 if ctx.Config().(Config).EmbeddedInMake() {
450 suffix = "-soong"
451 }
452
Colin Cross9454bfa2015-03-17 13:24:18 -0700453 ctx.Build(pctx, blueprint.BuildParams{
454 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800455 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700456 Implicits: deps,
457 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800458 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700459
460 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800461 }
462}
463
Colin Cross635c3b02016-05-18 15:37:25 -0700464func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700465 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700466 target: a.commonProperties.CompileTarget,
467 targetPrimary: a.commonProperties.CompilePrimary,
Dan Willemsenaa118f92017-04-06 12:49:58 -0700468 vendor: a.commonProperties.Proprietary || a.commonProperties.Vendor,
Colin Cross8b74d172016-09-13 09:59:14 -0700469 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800471}
472
Colin Cross635c3b02016-05-18 15:37:25 -0700473func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800474 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700475 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700476 ModuleContext: ctx,
477 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
478 installDeps: a.computeInstallDeps(ctx),
479 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800480 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800481 }
482
Colin Cross9b1d13d2016-09-19 15:18:11 -0700483 if a.Enabled() {
484 a.module.GenerateAndroidBuildActions(androidCtx)
485 if ctx.Failed() {
486 return
487 }
488
489 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
490 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800491 }
492
Colin Cross9b1d13d2016-09-19 15:18:11 -0700493 if a == ctx.FinalModule().(Module).base() {
494 a.generateModuleTarget(ctx)
495 if ctx.Failed() {
496 return
497 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800498 }
499}
500
Colin Crossf6566ed2015-03-24 11:13:38 -0700501type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700502 target Target
503 targetPrimary bool
504 debug bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700505 vendor bool
Colin Cross8b74d172016-09-13 09:59:14 -0700506 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700507}
508
Colin Cross3f40fa42015-01-30 17:27:36 -0800509type androidModuleContext struct {
510 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700511 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700512 installDeps Paths
513 installFiles Paths
514 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800515 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700516 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800517}
518
519func (a *androidModuleContext) ninjaError(outputs []string, err error) {
520 a.ModuleContext.Build(pctx, blueprint.BuildParams{
521 Rule: ErrorRule,
522 Outputs: outputs,
523 Optional: true,
524 Args: map[string]string{
525 "error": err.Error(),
526 },
527 })
528 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800529}
530
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800531func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Cross7f19f372016-11-01 11:10:25 -0700532 if a.missingDeps != nil {
Colin Cross6ff51382015-12-17 16:39:19 -0800533 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
534 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
535 return
536 }
537
Colin Cross3f40fa42015-01-30 17:27:36 -0800538 params.Optional = true
539 a.ModuleContext.Build(pctx, params)
540}
541
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700542func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
543 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700544 Rule: params.Rule,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800545 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700546 Outputs: params.Outputs.Strings(),
547 ImplicitOutputs: params.ImplicitOutputs.Strings(),
548 Inputs: params.Inputs.Strings(),
549 Implicits: params.Implicits.Strings(),
550 OrderOnly: params.OrderOnly.Strings(),
551 Args: params.Args,
552 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700553 }
554
Colin Cross33bfb0a2016-11-21 17:23:08 -0800555 if params.Depfile != nil {
556 bparams.Depfile = params.Depfile.String()
557 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700558 if params.Output != nil {
559 bparams.Outputs = append(bparams.Outputs, params.Output.String())
560 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700561 if params.ImplicitOutput != nil {
562 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
563 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700564 if params.Input != nil {
565 bparams.Inputs = append(bparams.Inputs, params.Input.String())
566 }
567 if params.Implicit != nil {
568 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
569 }
570
Colin Cross6ff51382015-12-17 16:39:19 -0800571 if a.missingDeps != nil {
572 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
573 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
574 return
575 }
576
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700577 a.ModuleContext.Build(pctx, bparams)
578}
579
Colin Cross6ff51382015-12-17 16:39:19 -0800580func (a *androidModuleContext) GetMissingDependencies() []string {
581 return a.missingDeps
582}
583
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800584func (a *androidModuleContext) AddMissingDependencies(deps []string) {
585 if deps != nil {
586 a.missingDeps = append(a.missingDeps, deps...)
587 }
588}
589
Colin Crossa1ad8d12016-06-01 17:09:44 -0700590func (a *androidBaseContextImpl) Target() Target {
591 return a.target
592}
593
Colin Cross8b74d172016-09-13 09:59:14 -0700594func (a *androidBaseContextImpl) TargetPrimary() bool {
595 return a.targetPrimary
596}
597
Colin Crossf6566ed2015-03-24 11:13:38 -0700598func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700599 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800600}
601
Colin Crossa1ad8d12016-06-01 17:09:44 -0700602func (a *androidBaseContextImpl) Os() OsType {
603 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800604}
605
Colin Crossf6566ed2015-03-24 11:13:38 -0700606func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700607 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700608}
609
610func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700611 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700612}
613
Colin Cross0af4b842015-04-30 16:36:18 -0700614func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700615 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700616}
617
Colin Cross3edeee12017-04-04 12:59:48 -0700618func (a *androidBaseContextImpl) Windows() bool {
619 return a.target.Os == Windows
620}
621
Colin Crossf6566ed2015-03-24 11:13:38 -0700622func (a *androidBaseContextImpl) Debug() bool {
623 return a.debug
624}
625
Colin Cross1e7d3702016-08-24 15:25:47 -0700626func (a *androidBaseContextImpl) PrimaryArch() bool {
627 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
628}
629
Colin Cross1332b002015-04-07 17:11:30 -0700630func (a *androidBaseContextImpl) AConfig() Config {
631 return a.config
632}
633
Colin Cross9272ade2016-08-17 15:24:12 -0700634func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
635 return DeviceConfig{a.config.deviceConfig}
636}
637
Dan Willemsenaa118f92017-04-06 12:49:58 -0700638func (a *androidBaseContextImpl) Vendor() bool {
639 return a.vendor
Dan Willemsen782a2d12015-12-21 14:55:28 -0800640}
641
Colin Cross8d8f8e22016-08-03 11:57:50 -0700642func (a *androidModuleContext) InstallInData() bool {
643 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800644}
645
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700646func (a *androidModuleContext) InstallInSanitizerDir() bool {
647 return a.module.InstallInSanitizerDir()
648}
649
Colin Cross893d8162017-04-26 17:34:03 -0700650func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
651 if a.module.base().commonProperties.SkipInstall {
652 return true
653 }
654
655 if a.Device() {
656 if a.AConfig().SkipDeviceInstall() {
657 return true
658 }
659
660 if a.AConfig().SkipMegaDeviceInstall(fullInstallPath.String()) {
661 return true
662 }
663 }
664
665 return false
666}
667
Dan Willemsen782a2d12015-12-21 14:55:28 -0800668func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700669 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700670
Dan Willemsen782a2d12015-12-21 14:55:28 -0800671 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700672 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800673
Colin Cross893d8162017-04-26 17:34:03 -0700674 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700675
Dan Willemsen322acaf2016-01-12 23:07:05 -0800676 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700677
Colin Cross89562dc2016-10-03 17:47:19 -0700678 var implicitDeps, orderOnlyDeps Paths
679
680 if a.Host() {
681 // Installed host modules might be used during the build, depend directly on their
682 // dependencies so their timestamp is updated whenever their dependency is updated
683 implicitDeps = deps
684 } else {
685 orderOnlyDeps = deps
686 }
687
Dan Willemsen322acaf2016-01-12 23:07:05 -0800688 a.ModuleBuild(pctx, ModuleBuildParams{
689 Rule: Cp,
690 Output: fullInstallPath,
691 Input: srcPath,
Colin Cross89562dc2016-10-03 17:47:19 -0700692 Implicits: implicitDeps,
693 OrderOnly: orderOnlyDeps,
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800694 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800695 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800696
Dan Willemsen322acaf2016-01-12 23:07:05 -0800697 a.installFiles = append(a.installFiles, fullInstallPath)
698 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700699 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700700 return fullInstallPath
701}
702
Colin Crossa2344662016-03-24 13:14:12 -0700703func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700704 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800705}
706
Colin Cross3854a602016-01-11 12:49:11 -0800707func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
708 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700709 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800710
Colin Cross893d8162017-04-26 17:34:03 -0700711 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700712
Colin Cross12fc4972016-01-11 12:49:11 -0800713 a.ModuleBuild(pctx, ModuleBuildParams{
714 Rule: Symlink,
715 Output: fullInstallPath,
716 OrderOnly: Paths{srcPath},
717 Default: !a.AConfig().EmbeddedInMake(),
718 Args: map[string]string{
719 "fromPath": srcPath.String(),
720 },
721 })
Colin Cross3854a602016-01-11 12:49:11 -0800722
Colin Cross12fc4972016-01-11 12:49:11 -0800723 a.installFiles = append(a.installFiles, fullInstallPath)
724 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
725 }
Colin Cross3854a602016-01-11 12:49:11 -0800726 return fullInstallPath
727}
728
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700729func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800730 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
731}
732
Colin Cross3f40fa42015-01-30 17:27:36 -0800733type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700734 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800735}
736
737func isFileInstaller(m blueprint.Module) bool {
738 _, ok := m.(fileInstaller)
739 return ok
740}
741
742func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700743 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800744 return ok
745}
Colin Crossfce53272015-04-08 11:21:40 -0700746
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700747func findStringInSlice(str string, slice []string) int {
748 for i, s := range slice {
749 if s == str {
750 return i
Colin Crossfce53272015-04-08 11:21:40 -0700751 }
752 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700753 return -1
754}
755
Colin Cross068e0fe2016-12-13 15:23:47 -0800756func SrcIsModule(s string) string {
757 if len(s) > 1 && s[0] == ':' {
758 return s[1:]
759 }
760 return ""
761}
762
763type sourceDependencyTag struct {
764 blueprint.BaseDependencyTag
765}
766
767var SourceDepTag sourceDependencyTag
768
769// Returns a list of modules that must be depended on to satisfy filegroup or generated sources
770// modules listed in srcFiles using ":module" syntax
771func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
772 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -0700773 set := make(map[string]bool)
774
Colin Cross068e0fe2016-12-13 15:23:47 -0800775 for _, s := range srcFiles {
776 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -0700777 if _, found := set[m]; found {
778 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
779 } else {
780 set[m] = true
781 deps = append(deps, m)
782 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800783 }
784 }
785
786 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
787}
788
789type SourceFileProducer interface {
790 Srcs() Paths
791}
792
793// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800794// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700795func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800796 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
797}
798
799func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700800 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800801
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700802 for i, e := range excludes {
803 j := findStringInSlice(e, srcFiles)
804 if j != -1 {
805 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
806 }
807
808 excludes[i] = filepath.Join(prefix, e)
809 }
810
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800811 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700812 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -0800813 if m := SrcIsModule(s); m != "" {
814 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
815 if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800816 expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800817 } else {
818 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
819 }
820 } else if pathtools.IsGlob(s) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800821 globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
822 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
823 for i, s := range expandedSrcFiles {
824 expandedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
825 }
Colin Cross8f101b42015-06-17 15:09:06 -0700826 } else {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800827 s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
828 expandedSrcFiles = append(expandedSrcFiles, s)
Colin Cross8f101b42015-06-17 15:09:06 -0700829 }
830 }
831
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800832 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -0700833}
834
Nan Zhang6d34b302017-02-04 17:47:46 -0800835func (ctx *androidModuleContext) RequiredModuleNames() []string {
836 return ctx.module.base().commonProperties.Required
837}
838
Colin Cross7f19f372016-11-01 11:10:25 -0700839func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
840 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700841 if err != nil {
842 ctx.ModuleErrorf("glob: %s", err.Error())
843 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700844 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700845}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700846
Colin Cross463a90e2015-06-17 14:20:06 -0700847func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700848 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -0700849}
850
Colin Cross1f8c52b2015-06-16 16:38:17 -0700851func BuildTargetSingleton() blueprint.Singleton {
852 return &buildTargetSingleton{}
853}
854
Colin Cross87d8b562017-04-25 10:01:55 -0700855func parentDir(dir string) string {
856 dir, _ = filepath.Split(dir)
857 return filepath.Clean(dir)
858}
859
Colin Cross1f8c52b2015-06-16 16:38:17 -0700860type buildTargetSingleton struct{}
861
862func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
863 checkbuildDeps := []string{}
864
Colin Cross87d8b562017-04-25 10:01:55 -0700865 mmTarget := func(dir string) string {
866 return filepath.Join("mm", dir)
867 }
868
869 modulesInDir := make(map[string][]string)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700870
871 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700872 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700873 blueprintDir := a.base().blueprintDir
874 installTarget := a.base().installTarget
875 checkbuildTarget := a.base().checkbuildTarget
876
877 if checkbuildTarget != "" {
878 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
Colin Cross87d8b562017-04-25 10:01:55 -0700879 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700880 }
881
882 if installTarget != "" {
Colin Cross87d8b562017-04-25 10:01:55 -0700883 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700884 }
885 }
886 })
887
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800888 suffix := ""
889 if ctx.Config().(Config).EmbeddedInMake() {
890 suffix = "-soong"
891 }
892
Colin Cross1f8c52b2015-06-16 16:38:17 -0700893 // Create a top-level checkbuild target that depends on all modules
894 ctx.Build(pctx, blueprint.BuildParams{
895 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800896 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700897 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700898 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700899 })
900
Colin Cross87d8b562017-04-25 10:01:55 -0700901 // Ensure ancestor directories are in modulesInDir
902 dirs := sortedKeys(modulesInDir)
903 for _, dir := range dirs {
904 dir := parentDir(dir)
905 for dir != "." && dir != "/" {
906 if _, exists := modulesInDir[dir]; exists {
907 break
908 }
909 modulesInDir[dir] = nil
910 dir = parentDir(dir)
911 }
912 }
913
914 // Make directories build their direct subdirectories
915 dirs = sortedKeys(modulesInDir)
916 for _, dir := range dirs {
917 p := parentDir(dir)
918 if p != "." && p != "/" {
919 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
920 }
921 }
922
923 // Create a mm/<directory> target that depends on all modules in a directory, and depends
924 // on the mm/* targets of all of its subdirectories that contain Android.bp files.
Colin Cross1f8c52b2015-06-16 16:38:17 -0700925 for _, dir := range dirs {
926 ctx.Build(pctx, blueprint.BuildParams{
927 Rule: blueprint.Phony,
Colin Cross87d8b562017-04-25 10:01:55 -0700928 Outputs: []string{mmTarget(dir)},
929 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800930 // HACK: checkbuild should be an optional build, but force it
931 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800932 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700933 })
934 }
935}
Colin Crossd779da42015-12-17 18:00:23 -0800936
937type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700938 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800939 ctx interface {
940 ModuleName(blueprint.Module) string
941 ModuleSubDir(blueprint.Module) string
942 }
943}
944
945func (s AndroidModulesByName) Len() int { return len(s.slice) }
946func (s AndroidModulesByName) Less(i, j int) bool {
947 mi, mj := s.slice[i], s.slice[j]
948 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
949
950 if ni != nj {
951 return ni < nj
952 } else {
953 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
954 }
955}
956func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }