blob: e11082e87fe0fe0b5b095b58b6b0f64f1bd61ca9 [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
15package common
16
17import (
Colin Cross3f40fa42015-01-30 17:27:36 -080018 "path/filepath"
Colin Crossf6566ed2015-03-24 11:13:38 -070019
Dan Willemsen0effe062015-11-30 16:06:01 -080020 "android/soong"
Colin Cross8f101b42015-06-17 15:09:06 -070021 "android/soong/glob"
22
Colin Crossf6566ed2015-03-24 11:13:38 -070023 "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -080024)
25
26var (
27 DeviceSharedLibrary = "shared_library"
28 DeviceStaticLibrary = "static_library"
29 DeviceExecutable = "executable"
30 HostSharedLibrary = "host_shared_library"
31 HostStaticLibrary = "host_static_library"
32 HostExecutable = "host_executable"
33)
34
Colin Crossf6566ed2015-03-24 11:13:38 -070035type androidBaseContext interface {
36 Arch() Arch
Colin Crossd3ba0392015-05-07 14:11:29 -070037 HostOrDevice() HostOrDevice
Dan Willemsen490fd492015-11-24 17:53:15 -080038 HostType() HostType
Colin Crossf6566ed2015-03-24 11:13:38 -070039 Host() bool
40 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070041 Darwin() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070042 Debug() bool
Colin Cross1332b002015-04-07 17:11:30 -070043 AConfig() Config
Colin Crossf6566ed2015-03-24 11:13:38 -070044}
45
46type AndroidBaseContext interface {
47 blueprint.BaseModuleContext
48 androidBaseContext
49}
50
Colin Cross3f40fa42015-01-30 17:27:36 -080051type AndroidModuleContext interface {
52 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070053 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080054
Dan Willemsen2ef08f42015-06-30 18:15:24 -070055 ExpandSources(srcFiles, excludes []string) []string
Colin Crossa819f082015-07-14 18:26:10 -070056 Glob(outDir, globPattern string, excludes []string) []string
Colin Cross8f101b42015-06-17 15:09:06 -070057
Colin Cross35cec122015-04-02 14:37:16 -070058 InstallFile(installPath, srcPath string, deps ...string) string
59 InstallFileName(installPath, name, srcPath string, deps ...string) string
Colin Cross3f40fa42015-01-30 17:27:36 -080060 CheckbuildFile(srcPath string)
61}
62
63type AndroidModule interface {
64 blueprint.Module
65
66 GenerateAndroidBuildActions(AndroidModuleContext)
67
68 base() *AndroidModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -080069 Enabled() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080070 HostOrDevice() HostOrDevice
71}
72
Colin Cross3f40fa42015-01-30 17:27:36 -080073type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -070074 Name string
75 Deps []string
76 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -080077
Dan Willemsen0effe062015-11-30 16:06:01 -080078 // emit build rules for this module
79 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -080080
Colin Cross7d5136f2015-05-11 13:39:40 -070081 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -080082 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
83 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
84 // platform
85 Compile_multilib string
86
Colin Crossd3ba0392015-05-07 14:11:29 -070087 // Set by HostOrDeviceMutator
88 CompileHostOrDevice HostOrDevice `blueprint:"mutated"`
89
Dan Willemsen490fd492015-11-24 17:53:15 -080090 // Set by HostTypeMutator
91 CompileHostType HostType `blueprint:"mutated"`
92
Colin Cross3f40fa42015-01-30 17:27:36 -080093 // Set by ArchMutator
94 CompileArch Arch `blueprint:"mutated"`
95
96 // Set by InitAndroidModule
97 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
98}
99
100type hostAndDeviceProperties struct {
101 Host_supported bool
102 Device_supported bool
103}
104
Colin Crossc472d572015-03-17 15:06:21 -0700105type Multilib string
106
107const (
Colin Cross2fe66872015-03-30 17:20:39 -0700108 MultilibBoth Multilib = "both"
109 MultilibFirst Multilib = "first"
110 MultilibCommon Multilib = "common"
Colin Crossc472d572015-03-17 15:06:21 -0700111)
112
Colin Cross5049f022015-03-18 13:28:46 -0700113func InitAndroidModule(m AndroidModule,
Colin Cross3f40fa42015-01-30 17:27:36 -0800114 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
115
116 base := m.base()
117 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700118
Colin Cross7f64b6d2015-07-09 13:57:48 -0700119 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700120
121 return m, propertyStructs
122}
123
124func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib,
125 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
126
127 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
128
129 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800130 base.commonProperties.HostOrDeviceSupported = hod
Colin Crosscfad1192015-11-02 16:43:11 -0800131 base.commonProperties.Compile_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800132
133 if hod == HostAndDeviceSupported {
134 // Default to module to device supported, host not supported, can override in module
135 // properties
136 base.hostAndDeviceProperties.Device_supported = true
137 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
138 }
139
Colin Crosscfad1192015-11-02 16:43:11 -0800140 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800141}
142
143// A AndroidModuleBase object contains the properties that are common to all Android
144// modules. It should be included as an anonymous field in every module
145// struct definition. InitAndroidModule should then be called from the module's
146// factory function, and the return values from InitAndroidModule should be
147// returned from the factory function.
148//
149// The AndroidModuleBase type is responsible for implementing the
150// GenerateBuildActions method to support the blueprint.Module interface. This
151// method will then call the module's GenerateAndroidBuildActions method once
152// for each build variant that is to be built. GenerateAndroidBuildActions is
153// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
154// AndroidModuleContext exposes extra functionality specific to the Android build
155// system including details about the particular build variant that is to be
156// generated.
157//
158// For example:
159//
160// import (
161// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700162// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800163// )
164//
165// type myModule struct {
166// common.AndroidModuleBase
167// properties struct {
168// MyProperty string
169// }
170// }
171//
172// func NewMyModule() (blueprint.Module, []interface{}) {
173// m := &myModule{}
174// return common.InitAndroidModule(m, &m.properties)
175// }
176//
177// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
178// // Get the CPU architecture for the current build variant.
179// variantArch := ctx.Arch()
180//
181// // ...
182// }
183type AndroidModuleBase struct {
184 // Putting the curiously recurring thing pointing to the thing that contains
185 // the thing pattern to good use.
186 module AndroidModule
187
188 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700189 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800190 hostAndDeviceProperties hostAndDeviceProperties
191 generalProperties []interface{}
192 archProperties []*archProperties
193
194 noAddressSanitizer bool
195 installFiles []string
196 checkbuildFiles []string
Colin Cross1f8c52b2015-06-16 16:38:17 -0700197
198 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
199 // Only set on the final variant of each module
200 installTarget string
201 checkbuildTarget string
202 blueprintDir string
Colin Cross3f40fa42015-01-30 17:27:36 -0800203}
204
205func (a *AndroidModuleBase) base() *AndroidModuleBase {
206 return a
207}
208
Colin Crossd3ba0392015-05-07 14:11:29 -0700209func (a *AndroidModuleBase) SetHostOrDevice(hod HostOrDevice) {
210 a.commonProperties.CompileHostOrDevice = hod
211}
212
Dan Willemsen490fd492015-11-24 17:53:15 -0800213func (a *AndroidModuleBase) SetHostType(ht HostType) {
214 a.commonProperties.CompileHostType = ht
215}
216
Colin Cross3f40fa42015-01-30 17:27:36 -0800217func (a *AndroidModuleBase) SetArch(arch Arch) {
218 a.commonProperties.CompileArch = arch
219}
220
221func (a *AndroidModuleBase) HostOrDevice() HostOrDevice {
Colin Crossd3ba0392015-05-07 14:11:29 -0700222 return a.commonProperties.CompileHostOrDevice
Colin Cross3f40fa42015-01-30 17:27:36 -0800223}
224
Dan Willemsen490fd492015-11-24 17:53:15 -0800225func (a *AndroidModuleBase) HostType() HostType {
226 return a.commonProperties.CompileHostType
227}
228
Colin Cross3f40fa42015-01-30 17:27:36 -0800229func (a *AndroidModuleBase) HostSupported() bool {
230 return a.commonProperties.HostOrDeviceSupported == HostSupported ||
231 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
232 a.hostAndDeviceProperties.Host_supported
233}
234
235func (a *AndroidModuleBase) DeviceSupported() bool {
236 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
237 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
238 a.hostAndDeviceProperties.Device_supported
239}
240
Dan Willemsen0effe062015-11-30 16:06:01 -0800241func (a *AndroidModuleBase) Enabled() bool {
242 if a.commonProperties.Enabled == nil {
243 if a.HostSupported() && a.HostOrDevice().Host() && a.HostType() == Windows {
244 return false
245 } else {
246 return true
247 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800248 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800249 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800250}
251
252func (a *AndroidModuleBase) computeInstallDeps(
253 ctx blueprint.ModuleContext) []string {
254
255 result := []string{}
256 ctx.VisitDepsDepthFirstIf(isFileInstaller,
257 func(m blueprint.Module) {
258 fileInstaller := m.(fileInstaller)
259 files := fileInstaller.filesToInstall()
260 result = append(result, files...)
261 })
262
263 return result
264}
265
266func (a *AndroidModuleBase) filesToInstall() []string {
267 return a.installFiles
268}
269
270func (p *AndroidModuleBase) NoAddressSanitizer() bool {
271 return p.noAddressSanitizer
272}
273
Colin Cross3f40fa42015-01-30 17:27:36 -0800274func (a *AndroidModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
275 if a != ctx.FinalModule().(AndroidModule).base() {
276 return
277 }
278
279 allInstalledFiles := []string{}
Colin Cross9454bfa2015-03-17 13:24:18 -0700280 allCheckbuildFiles := []string{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800281 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Crossc9404352015-03-26 16:10:12 -0700282 a := module.(AndroidModule).base()
283 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
284 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800285 })
286
Colin Cross9454bfa2015-03-17 13:24:18 -0700287 deps := []string{}
288
Colin Cross3f40fa42015-01-30 17:27:36 -0800289 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700290 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800291 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700292 Rule: blueprint.Phony,
293 Outputs: []string{name},
294 Implicits: allInstalledFiles,
295 })
296 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700297 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700298 }
299
300 if len(allCheckbuildFiles) > 0 {
301 name := ctx.ModuleName() + "-checkbuild"
302 ctx.Build(pctx, blueprint.BuildParams{
303 Rule: blueprint.Phony,
304 Outputs: []string{name},
305 Implicits: allCheckbuildFiles,
306 Optional: true,
307 })
308 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700309 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700310 }
311
312 if len(deps) > 0 {
313 ctx.Build(pctx, blueprint.BuildParams{
314 Rule: blueprint.Phony,
315 Outputs: []string{ctx.ModuleName()},
316 Implicits: deps,
317 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800318 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700319
320 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800321 }
322}
323
Colin Cross6362e272015-10-29 15:25:03 -0700324func (a *AndroidModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
325 return androidBaseContextImpl{
326 arch: a.commonProperties.CompileArch,
327 hod: a.commonProperties.CompileHostOrDevice,
Dan Willemsen490fd492015-11-24 17:53:15 -0800328 ht: a.commonProperties.CompileHostType,
Colin Cross6362e272015-10-29 15:25:03 -0700329 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800330 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800331}
332
333func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
334 androidCtx := &androidModuleContext{
Colin Cross6362e272015-10-29 15:25:03 -0700335 ModuleContext: ctx,
336 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
337 installDeps: a.computeInstallDeps(ctx),
338 installFiles: a.installFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800339 }
340
Dan Willemsen0effe062015-11-30 16:06:01 -0800341 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800342 return
343 }
344
345 a.module.GenerateAndroidBuildActions(androidCtx)
346 if ctx.Failed() {
347 return
348 }
349
Colin Crossc9404352015-03-26 16:10:12 -0700350 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
351 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
352
Colin Cross3f40fa42015-01-30 17:27:36 -0800353 a.generateModuleTarget(ctx)
354 if ctx.Failed() {
355 return
356 }
357}
358
Colin Crossf6566ed2015-03-24 11:13:38 -0700359type androidBaseContextImpl struct {
Colin Cross1332b002015-04-07 17:11:30 -0700360 arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700361 hod HostOrDevice
Dan Willemsen490fd492015-11-24 17:53:15 -0800362 ht HostType
Colin Cross1332b002015-04-07 17:11:30 -0700363 debug bool
364 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700365}
366
Colin Cross3f40fa42015-01-30 17:27:36 -0800367type androidModuleContext struct {
368 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700369 androidBaseContextImpl
Colin Cross06a931b2015-10-28 17:23:31 -0700370 installDeps []string
371 installFiles []string
372 checkbuildFiles []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800373}
374
375func (a *androidModuleContext) Build(pctx *blueprint.PackageContext, params blueprint.BuildParams) {
376 params.Optional = true
377 a.ModuleContext.Build(pctx, params)
378}
379
Colin Crossf6566ed2015-03-24 11:13:38 -0700380func (a *androidBaseContextImpl) Arch() Arch {
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 return a.arch
382}
383
Colin Crossd3ba0392015-05-07 14:11:29 -0700384func (a *androidBaseContextImpl) HostOrDevice() HostOrDevice {
385 return a.hod
386}
387
Dan Willemsen490fd492015-11-24 17:53:15 -0800388func (a *androidBaseContextImpl) HostType() HostType {
389 return a.ht
390}
391
Colin Crossf6566ed2015-03-24 11:13:38 -0700392func (a *androidBaseContextImpl) Host() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700393 return a.hod.Host()
Colin Crossf6566ed2015-03-24 11:13:38 -0700394}
395
396func (a *androidBaseContextImpl) Device() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700397 return a.hod.Device()
Colin Crossf6566ed2015-03-24 11:13:38 -0700398}
399
Colin Cross0af4b842015-04-30 16:36:18 -0700400func (a *androidBaseContextImpl) Darwin() bool {
Dan Willemsen490fd492015-11-24 17:53:15 -0800401 return a.hod.Host() && a.ht == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700402}
403
Colin Crossf6566ed2015-03-24 11:13:38 -0700404func (a *androidBaseContextImpl) Debug() bool {
405 return a.debug
406}
407
Colin Cross1332b002015-04-07 17:11:30 -0700408func (a *androidBaseContextImpl) AConfig() Config {
409 return a.config
410}
411
Colin Cross35cec122015-04-02 14:37:16 -0700412func (a *androidModuleContext) InstallFileName(installPath, name, srcPath string,
413 deps ...string) string {
414
Colin Cross1332b002015-04-07 17:11:30 -0700415 config := a.AConfig()
Colin Cross3f40fa42015-01-30 17:27:36 -0800416 var fullInstallPath string
Colin Crossd3ba0392015-05-07 14:11:29 -0700417 if a.hod.Device() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800418 // TODO: replace unset with a device name once we have device targeting
Colin Cross35cec122015-04-02 14:37:16 -0700419 fullInstallPath = filepath.Join(config.DeviceOut(), "system",
420 installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800421 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800422 // TODO
423 if a.ht == Windows {
424 fullInstallPath = filepath.Join(config.BuildDir(), "host", "windows-x86", installPath, name)
425 } else {
426 fullInstallPath = filepath.Join(config.HostOut(), installPath, name)
427 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800428 }
429
Colin Cross35cec122015-04-02 14:37:16 -0700430 deps = append(deps, a.installDeps...)
431
Colin Cross3f40fa42015-01-30 17:27:36 -0800432 a.ModuleContext.Build(pctx, blueprint.BuildParams{
433 Rule: Cp,
434 Outputs: []string{fullInstallPath},
435 Inputs: []string{srcPath},
Colin Cross35cec122015-04-02 14:37:16 -0700436 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800437 })
438
439 a.installFiles = append(a.installFiles, fullInstallPath)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700440 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700441 return fullInstallPath
442}
443
444func (a *androidModuleContext) InstallFile(installPath, srcPath string, deps ...string) string {
445 return a.InstallFileName(installPath, filepath.Base(srcPath), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800446}
447
448func (a *androidModuleContext) CheckbuildFile(srcPath string) {
449 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
450}
451
Colin Cross3f40fa42015-01-30 17:27:36 -0800452type fileInstaller interface {
453 filesToInstall() []string
454}
455
456func isFileInstaller(m blueprint.Module) bool {
457 _, ok := m.(fileInstaller)
458 return ok
459}
460
461func isAndroidModule(m blueprint.Module) bool {
462 _, ok := m.(AndroidModule)
463 return ok
464}
Colin Crossfce53272015-04-08 11:21:40 -0700465
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700466func findStringInSlice(str string, slice []string) int {
467 for i, s := range slice {
468 if s == str {
469 return i
Colin Crossfce53272015-04-08 11:21:40 -0700470 }
471 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700472 return -1
473}
474
475func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) []string {
476 prefix := ModuleSrcDir(ctx)
477 for i, e := range excludes {
478 j := findStringInSlice(e, srcFiles)
479 if j != -1 {
480 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
481 }
482
483 excludes[i] = filepath.Join(prefix, e)
484 }
485
486 for i, srcFile := range srcFiles {
487 srcFiles[i] = filepath.Join(prefix, srcFile)
488 }
Colin Crossfce53272015-04-08 11:21:40 -0700489
Colin Cross8f101b42015-06-17 15:09:06 -0700490 if !hasGlob(srcFiles) {
491 return srcFiles
492 }
493
Colin Cross8f101b42015-06-17 15:09:06 -0700494 globbedSrcFiles := make([]string, 0, len(srcFiles))
495 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700496 if glob.IsGlob(s) {
Colin Crossa819f082015-07-14 18:26:10 -0700497 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", s, excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700498 } else {
499 globbedSrcFiles = append(globbedSrcFiles, s)
500 }
501 }
502
503 return globbedSrcFiles
504}
505
Colin Crossa819f082015-07-14 18:26:10 -0700506func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) []string {
507 ret, err := Glob(ctx, filepath.Join(ModuleOutDir(ctx), outDir), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700508 if err != nil {
509 ctx.ModuleErrorf("glob: %s", err.Error())
510 }
511 return ret
Colin Crossfce53272015-04-08 11:21:40 -0700512}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700513
Colin Cross463a90e2015-06-17 14:20:06 -0700514func init() {
515 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
516}
517
Colin Cross1f8c52b2015-06-16 16:38:17 -0700518func BuildTargetSingleton() blueprint.Singleton {
519 return &buildTargetSingleton{}
520}
521
522type buildTargetSingleton struct{}
523
524func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
525 checkbuildDeps := []string{}
526
527 dirModules := make(map[string][]string)
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700528 hasBPFile := make(map[string]bool)
529 bpFiles := []string{}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700530
531 ctx.VisitAllModules(func(module blueprint.Module) {
532 if a, ok := module.(AndroidModule); ok {
533 blueprintDir := a.base().blueprintDir
534 installTarget := a.base().installTarget
535 checkbuildTarget := a.base().checkbuildTarget
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700536 bpFile := ctx.BlueprintFile(module)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700537
538 if checkbuildTarget != "" {
539 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
540 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
541 }
542
543 if installTarget != "" {
544 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
545 }
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700546
547 if !hasBPFile[bpFile] {
548 hasBPFile[bpFile] = true
549 bpFiles = append(bpFiles, bpFile)
550 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700551 }
552 })
553
554 // Create a top-level checkbuild target that depends on all modules
555 ctx.Build(pctx, blueprint.BuildParams{
556 Rule: blueprint.Phony,
557 Outputs: []string{"checkbuild"},
558 Implicits: checkbuildDeps,
559 // HACK: checkbuild should be an optional build, but force it enabled for now
560 //Optional: true,
561 })
562
563 // Create a mm/<directory> target that depends on all modules in a directory
564 dirs := sortedKeys(dirModules)
565 for _, dir := range dirs {
566 ctx.Build(pctx, blueprint.BuildParams{
567 Rule: blueprint.Phony,
568 Outputs: []string{filepath.Join("mm", dir)},
569 Implicits: dirModules[dir],
570 Optional: true,
571 })
572 }
573}