blob: e8c4a87e0a71b33546000b10faa755ea2faf2f94 [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 Cross463a90e2015-06-17 14:20:06 -070018 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070020 "runtime"
Dan Willemsenbf9207a2015-06-17 15:17:12 -070021 "sort"
22 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070023
Colin Cross8f101b42015-06-17 15:09:06 -070024 "android/soong/glob"
25
Colin Crossf6566ed2015-03-24 11:13:38 -070026 "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -080027)
28
29var (
30 DeviceSharedLibrary = "shared_library"
31 DeviceStaticLibrary = "static_library"
32 DeviceExecutable = "executable"
33 HostSharedLibrary = "host_shared_library"
34 HostStaticLibrary = "host_static_library"
35 HostExecutable = "host_executable"
36)
37
Colin Crossf6566ed2015-03-24 11:13:38 -070038type androidBaseContext interface {
39 Arch() Arch
Colin Crossd3ba0392015-05-07 14:11:29 -070040 HostOrDevice() HostOrDevice
Colin Crossf6566ed2015-03-24 11:13:38 -070041 Host() bool
42 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070043 Darwin() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070044 Debug() bool
Colin Cross1332b002015-04-07 17:11:30 -070045 AConfig() Config
Colin Crossf6566ed2015-03-24 11:13:38 -070046}
47
48type AndroidBaseContext interface {
49 blueprint.BaseModuleContext
50 androidBaseContext
51}
52
Colin Cross3f40fa42015-01-30 17:27:36 -080053type AndroidModuleContext interface {
54 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070055 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080056
Dan Willemsen2ef08f42015-06-30 18:15:24 -070057 ExpandSources(srcFiles, excludes []string) []string
Colin Crossa819f082015-07-14 18:26:10 -070058 Glob(outDir, globPattern string, excludes []string) []string
Colin Cross8f101b42015-06-17 15:09:06 -070059
Colin Cross35cec122015-04-02 14:37:16 -070060 InstallFile(installPath, srcPath string, deps ...string) string
61 InstallFileName(installPath, name, srcPath string, deps ...string) string
Colin Cross3f40fa42015-01-30 17:27:36 -080062 CheckbuildFile(srcPath string)
63}
64
65type AndroidModule interface {
66 blueprint.Module
67
68 GenerateAndroidBuildActions(AndroidModuleContext)
69
70 base() *AndroidModuleBase
71 Disabled() bool
72 HostOrDevice() HostOrDevice
73}
74
75type AndroidDynamicDepender interface {
76 AndroidDynamicDependencies(ctx AndroidDynamicDependerModuleContext) []string
77}
78
79type AndroidDynamicDependerModuleContext interface {
80 blueprint.DynamicDependerModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070081 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080082}
83
84type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -070085 Name string
86 Deps []string
87 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -080088
Colin Cross7d5136f2015-05-11 13:39:40 -070089 // don't emit any build rules for this module
Colin Cross3f40fa42015-01-30 17:27:36 -080090 Disabled bool `android:"arch_variant"`
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -080093 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
94 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
95 // platform
96 Compile_multilib string
97
Colin Crossd3ba0392015-05-07 14:11:29 -070098 // Set by HostOrDeviceMutator
99 CompileHostOrDevice HostOrDevice `blueprint:"mutated"`
100
Colin Cross3f40fa42015-01-30 17:27:36 -0800101 // Set by ArchMutator
102 CompileArch Arch `blueprint:"mutated"`
103
104 // Set by InitAndroidModule
105 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
106}
107
108type hostAndDeviceProperties struct {
109 Host_supported bool
110 Device_supported bool
111}
112
Colin Crossc472d572015-03-17 15:06:21 -0700113type Multilib string
114
115const (
Colin Cross2fe66872015-03-30 17:20:39 -0700116 MultilibBoth Multilib = "both"
117 MultilibFirst Multilib = "first"
118 MultilibCommon Multilib = "common"
Colin Crossc472d572015-03-17 15:06:21 -0700119)
120
Colin Cross5049f022015-03-18 13:28:46 -0700121func InitAndroidModule(m AndroidModule,
Colin Cross3f40fa42015-01-30 17:27:36 -0800122 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
123
124 base := m.base()
125 base.module = m
Colin Cross28d76592015-03-26 16:14:04 -0700126 base.extendedProperties = make(map[string]struct{})
Colin Cross5049f022015-03-18 13:28:46 -0700127
128 propertyStructs = append(propertyStructs, &base.commonProperties)
129
130 return m, propertyStructs
131}
132
133func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib,
134 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
135
136 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
137
138 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800139 base.commonProperties.HostOrDeviceSupported = hod
140
141 if hod == HostAndDeviceSupported {
142 // Default to module to device supported, host not supported, can override in module
143 // properties
144 base.hostAndDeviceProperties.Device_supported = true
145 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
146 }
147
148 return InitArchModule(m, defaultMultilib, propertyStructs...)
149}
150
151// A AndroidModuleBase object contains the properties that are common to all Android
152// modules. It should be included as an anonymous field in every module
153// struct definition. InitAndroidModule should then be called from the module's
154// factory function, and the return values from InitAndroidModule should be
155// returned from the factory function.
156//
157// The AndroidModuleBase type is responsible for implementing the
158// GenerateBuildActions method to support the blueprint.Module interface. This
159// method will then call the module's GenerateAndroidBuildActions method once
160// for each build variant that is to be built. GenerateAndroidBuildActions is
161// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
162// AndroidModuleContext exposes extra functionality specific to the Android build
163// system including details about the particular build variant that is to be
164// generated.
165//
166// For example:
167//
168// import (
169// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700170// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800171// )
172//
173// type myModule struct {
174// common.AndroidModuleBase
175// properties struct {
176// MyProperty string
177// }
178// }
179//
180// func NewMyModule() (blueprint.Module, []interface{}) {
181// m := &myModule{}
182// return common.InitAndroidModule(m, &m.properties)
183// }
184//
185// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
186// // Get the CPU architecture for the current build variant.
187// variantArch := ctx.Arch()
188//
189// // ...
190// }
191type AndroidModuleBase struct {
192 // Putting the curiously recurring thing pointing to the thing that contains
193 // the thing pattern to good use.
194 module AndroidModule
195
196 commonProperties commonProperties
197 hostAndDeviceProperties hostAndDeviceProperties
198 generalProperties []interface{}
199 archProperties []*archProperties
Colin Cross28d76592015-03-26 16:14:04 -0700200 extendedProperties map[string]struct{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800201
202 noAddressSanitizer bool
203 installFiles []string
204 checkbuildFiles []string
Colin Cross1f8c52b2015-06-16 16:38:17 -0700205
206 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
207 // Only set on the final variant of each module
208 installTarget string
209 checkbuildTarget string
210 blueprintDir string
Colin Cross3f40fa42015-01-30 17:27:36 -0800211}
212
213func (a *AndroidModuleBase) base() *AndroidModuleBase {
214 return a
215}
216
Colin Crossd3ba0392015-05-07 14:11:29 -0700217func (a *AndroidModuleBase) SetHostOrDevice(hod HostOrDevice) {
218 a.commonProperties.CompileHostOrDevice = hod
219}
220
Colin Cross3f40fa42015-01-30 17:27:36 -0800221func (a *AndroidModuleBase) SetArch(arch Arch) {
222 a.commonProperties.CompileArch = arch
223}
224
225func (a *AndroidModuleBase) HostOrDevice() HostOrDevice {
Colin Crossd3ba0392015-05-07 14:11:29 -0700226 return a.commonProperties.CompileHostOrDevice
Colin Cross3f40fa42015-01-30 17:27:36 -0800227}
228
229func (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
241func (a *AndroidModuleBase) Disabled() bool {
242 return a.commonProperties.Disabled
243}
244
245func (a *AndroidModuleBase) computeInstallDeps(
246 ctx blueprint.ModuleContext) []string {
247
248 result := []string{}
249 ctx.VisitDepsDepthFirstIf(isFileInstaller,
250 func(m blueprint.Module) {
251 fileInstaller := m.(fileInstaller)
252 files := fileInstaller.filesToInstall()
253 result = append(result, files...)
254 })
255
256 return result
257}
258
259func (a *AndroidModuleBase) filesToInstall() []string {
260 return a.installFiles
261}
262
263func (p *AndroidModuleBase) NoAddressSanitizer() bool {
264 return p.noAddressSanitizer
265}
266
Colin Cross3f40fa42015-01-30 17:27:36 -0800267func (a *AndroidModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
268 if a != ctx.FinalModule().(AndroidModule).base() {
269 return
270 }
271
272 allInstalledFiles := []string{}
Colin Cross9454bfa2015-03-17 13:24:18 -0700273 allCheckbuildFiles := []string{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800274 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Crossc9404352015-03-26 16:10:12 -0700275 a := module.(AndroidModule).base()
276 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
277 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800278 })
279
Colin Cross9454bfa2015-03-17 13:24:18 -0700280 deps := []string{}
281
Colin Cross3f40fa42015-01-30 17:27:36 -0800282 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700283 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800284 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700285 Rule: blueprint.Phony,
286 Outputs: []string{name},
287 Implicits: allInstalledFiles,
288 })
289 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700290 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700291 }
292
293 if len(allCheckbuildFiles) > 0 {
294 name := ctx.ModuleName() + "-checkbuild"
295 ctx.Build(pctx, blueprint.BuildParams{
296 Rule: blueprint.Phony,
297 Outputs: []string{name},
298 Implicits: allCheckbuildFiles,
299 Optional: true,
300 })
301 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700302 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700303 }
304
305 if len(deps) > 0 {
306 ctx.Build(pctx, blueprint.BuildParams{
307 Rule: blueprint.Phony,
308 Outputs: []string{ctx.ModuleName()},
309 Implicits: deps,
310 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800311 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700312
313 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800314 }
315}
316
317func (a *AndroidModuleBase) DynamicDependencies(ctx blueprint.DynamicDependerModuleContext) []string {
318 actx := &androidDynamicDependerContext{
319 DynamicDependerModuleContext: ctx,
Colin Crossf6566ed2015-03-24 11:13:38 -0700320 androidBaseContextImpl: androidBaseContextImpl{
Colin Cross1332b002015-04-07 17:11:30 -0700321 arch: a.commonProperties.CompileArch,
Colin Crossd3ba0392015-05-07 14:11:29 -0700322 hod: a.commonProperties.CompileHostOrDevice,
Colin Cross1332b002015-04-07 17:11:30 -0700323 config: ctx.Config().(Config),
Colin Crossf6566ed2015-03-24 11:13:38 -0700324 },
Colin Cross3f40fa42015-01-30 17:27:36 -0800325 }
326
327 if dynamic, ok := a.module.(AndroidDynamicDepender); ok {
328 return dynamic.AndroidDynamicDependencies(actx)
329 }
330
331 return nil
332}
333
334func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
335 androidCtx := &androidModuleContext{
336 ModuleContext: ctx,
Colin Crossf6566ed2015-03-24 11:13:38 -0700337 androidBaseContextImpl: androidBaseContextImpl{
Colin Cross1332b002015-04-07 17:11:30 -0700338 arch: a.commonProperties.CompileArch,
Colin Crossd3ba0392015-05-07 14:11:29 -0700339 hod: a.commonProperties.CompileHostOrDevice,
Colin Cross1332b002015-04-07 17:11:30 -0700340 config: ctx.Config().(Config),
Colin Crossf6566ed2015-03-24 11:13:38 -0700341 },
Colin Cross28d76592015-03-26 16:14:04 -0700342 installDeps: a.computeInstallDeps(ctx),
343 installFiles: a.installFiles,
344 extendedProperties: a.extendedProperties,
Colin Cross3f40fa42015-01-30 17:27:36 -0800345 }
346
347 if a.commonProperties.Disabled {
348 return
349 }
350
351 a.module.GenerateAndroidBuildActions(androidCtx)
352 if ctx.Failed() {
353 return
354 }
355
Colin Crossc9404352015-03-26 16:10:12 -0700356 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
357 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
358
Colin Cross3f40fa42015-01-30 17:27:36 -0800359 a.generateModuleTarget(ctx)
360 if ctx.Failed() {
361 return
362 }
363}
364
Colin Crossf6566ed2015-03-24 11:13:38 -0700365type androidBaseContextImpl struct {
Colin Cross1332b002015-04-07 17:11:30 -0700366 arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700367 hod HostOrDevice
Colin Cross1332b002015-04-07 17:11:30 -0700368 debug bool
369 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700370}
371
Colin Cross3f40fa42015-01-30 17:27:36 -0800372type androidModuleContext struct {
373 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700374 androidBaseContextImpl
Colin Cross28d76592015-03-26 16:14:04 -0700375 installDeps []string
376 installFiles []string
377 checkbuildFiles []string
378 extendedProperties map[string]struct{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800379}
380
381func (a *androidModuleContext) Build(pctx *blueprint.PackageContext, params blueprint.BuildParams) {
382 params.Optional = true
383 a.ModuleContext.Build(pctx, params)
384}
385
Colin Cross28d76592015-03-26 16:14:04 -0700386func (a *androidModuleContext) ContainsProperty(property string) bool {
387 if a.ModuleContext.ContainsProperty(property) {
388 return true
389 }
390 _, ok := a.extendedProperties[property]
391 return ok
392}
393
Colin Crossf6566ed2015-03-24 11:13:38 -0700394func (a *androidBaseContextImpl) Arch() Arch {
Colin Cross3f40fa42015-01-30 17:27:36 -0800395 return a.arch
396}
397
Colin Crossd3ba0392015-05-07 14:11:29 -0700398func (a *androidBaseContextImpl) HostOrDevice() HostOrDevice {
399 return a.hod
400}
401
Colin Crossf6566ed2015-03-24 11:13:38 -0700402func (a *androidBaseContextImpl) Host() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700403 return a.hod.Host()
Colin Crossf6566ed2015-03-24 11:13:38 -0700404}
405
406func (a *androidBaseContextImpl) Device() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700407 return a.hod.Device()
Colin Crossf6566ed2015-03-24 11:13:38 -0700408}
409
Colin Cross0af4b842015-04-30 16:36:18 -0700410func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700411 return a.hod.Host() && runtime.GOOS == "darwin"
Colin Cross0af4b842015-04-30 16:36:18 -0700412}
413
Colin Crossf6566ed2015-03-24 11:13:38 -0700414func (a *androidBaseContextImpl) Debug() bool {
415 return a.debug
416}
417
Colin Cross1332b002015-04-07 17:11:30 -0700418func (a *androidBaseContextImpl) AConfig() Config {
419 return a.config
420}
421
Colin Cross35cec122015-04-02 14:37:16 -0700422func (a *androidModuleContext) InstallFileName(installPath, name, srcPath string,
423 deps ...string) string {
424
Colin Cross1332b002015-04-07 17:11:30 -0700425 config := a.AConfig()
Colin Cross3f40fa42015-01-30 17:27:36 -0800426 var fullInstallPath string
Colin Crossd3ba0392015-05-07 14:11:29 -0700427 if a.hod.Device() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800428 // TODO: replace unset with a device name once we have device targeting
Colin Cross35cec122015-04-02 14:37:16 -0700429 fullInstallPath = filepath.Join(config.DeviceOut(), "system",
430 installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800431 } else {
Colin Cross35cec122015-04-02 14:37:16 -0700432 fullInstallPath = filepath.Join(config.HostOut(), installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800433 }
434
Colin Cross35cec122015-04-02 14:37:16 -0700435 deps = append(deps, a.installDeps...)
436
Colin Cross3f40fa42015-01-30 17:27:36 -0800437 a.ModuleContext.Build(pctx, blueprint.BuildParams{
438 Rule: Cp,
439 Outputs: []string{fullInstallPath},
440 Inputs: []string{srcPath},
Colin Cross35cec122015-04-02 14:37:16 -0700441 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800442 })
443
444 a.installFiles = append(a.installFiles, fullInstallPath)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700445 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700446 return fullInstallPath
447}
448
449func (a *androidModuleContext) InstallFile(installPath, srcPath string, deps ...string) string {
450 return a.InstallFileName(installPath, filepath.Base(srcPath), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800451}
452
453func (a *androidModuleContext) CheckbuildFile(srcPath string) {
454 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
455}
456
457type androidDynamicDependerContext struct {
458 blueprint.DynamicDependerModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700459 androidBaseContextImpl
Colin Cross3f40fa42015-01-30 17:27:36 -0800460}
461
462type fileInstaller interface {
463 filesToInstall() []string
464}
465
466func isFileInstaller(m blueprint.Module) bool {
467 _, ok := m.(fileInstaller)
468 return ok
469}
470
471func isAndroidModule(m blueprint.Module) bool {
472 _, ok := m.(AndroidModule)
473 return ok
474}
Colin Crossfce53272015-04-08 11:21:40 -0700475
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700476func findStringInSlice(str string, slice []string) int {
477 for i, s := range slice {
478 if s == str {
479 return i
Colin Crossfce53272015-04-08 11:21:40 -0700480 }
481 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700482 return -1
483}
484
485func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) []string {
486 prefix := ModuleSrcDir(ctx)
487 for i, e := range excludes {
488 j := findStringInSlice(e, srcFiles)
489 if j != -1 {
490 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
491 }
492
493 excludes[i] = filepath.Join(prefix, e)
494 }
495
496 for i, srcFile := range srcFiles {
497 srcFiles[i] = filepath.Join(prefix, srcFile)
498 }
Colin Crossfce53272015-04-08 11:21:40 -0700499
Colin Cross8f101b42015-06-17 15:09:06 -0700500 if !hasGlob(srcFiles) {
501 return srcFiles
502 }
503
Colin Cross8f101b42015-06-17 15:09:06 -0700504 globbedSrcFiles := make([]string, 0, len(srcFiles))
505 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700506 if glob.IsGlob(s) {
Colin Crossa819f082015-07-14 18:26:10 -0700507 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", s, excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700508 } else {
509 globbedSrcFiles = append(globbedSrcFiles, s)
510 }
511 }
512
513 return globbedSrcFiles
514}
515
Colin Crossa819f082015-07-14 18:26:10 -0700516func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) []string {
517 ret, err := Glob(ctx, filepath.Join(ModuleOutDir(ctx), outDir), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700518 if err != nil {
519 ctx.ModuleErrorf("glob: %s", err.Error())
520 }
521 return ret
Colin Crossfce53272015-04-08 11:21:40 -0700522}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700523
Colin Cross463a90e2015-06-17 14:20:06 -0700524func init() {
525 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
526}
527
Colin Cross1f8c52b2015-06-16 16:38:17 -0700528func BuildTargetSingleton() blueprint.Singleton {
529 return &buildTargetSingleton{}
530}
531
532type buildTargetSingleton struct{}
533
534func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
535 checkbuildDeps := []string{}
536
537 dirModules := make(map[string][]string)
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700538 hasBPFile := make(map[string]bool)
539 bpFiles := []string{}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700540
541 ctx.VisitAllModules(func(module blueprint.Module) {
542 if a, ok := module.(AndroidModule); ok {
543 blueprintDir := a.base().blueprintDir
544 installTarget := a.base().installTarget
545 checkbuildTarget := a.base().checkbuildTarget
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700546 bpFile := ctx.BlueprintFile(module)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700547
548 if checkbuildTarget != "" {
549 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
550 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
551 }
552
553 if installTarget != "" {
554 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
555 }
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700556
557 if !hasBPFile[bpFile] {
558 hasBPFile[bpFile] = true
559 bpFiles = append(bpFiles, bpFile)
560 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700561 }
562 })
563
564 // Create a top-level checkbuild target that depends on all modules
565 ctx.Build(pctx, blueprint.BuildParams{
566 Rule: blueprint.Phony,
567 Outputs: []string{"checkbuild"},
568 Implicits: checkbuildDeps,
569 // HACK: checkbuild should be an optional build, but force it enabled for now
570 //Optional: true,
571 })
572
573 // Create a mm/<directory> target that depends on all modules in a directory
574 dirs := sortedKeys(dirModules)
575 for _, dir := range dirs {
576 ctx.Build(pctx, blueprint.BuildParams{
577 Rule: blueprint.Phony,
578 Outputs: []string{filepath.Join("mm", dir)},
579 Implicits: dirModules[dir],
580 Optional: true,
581 })
582 }
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700583
584 // Create Android.bp->mk translation rules
585 androidMks := []string{}
586 srcDir := ctx.Config().(Config).SrcDir()
587 intermediatesDir := filepath.Join(ctx.Config().(Config).IntermediatesDir(), "androidmk")
588 sort.Strings(bpFiles)
589 for _, origBp := range bpFiles {
590 bpFile := filepath.Join(srcDir, origBp)
591 mkFile := filepath.Join(srcDir, filepath.Dir(origBp), "Android.mk")
592
593 files, err := Glob(ctx, intermediatesDir, mkFile, nil)
594 if err != nil {
595 ctx.Errorf("glob: %s", err.Error())
596 continue
597 }
598
599 // Existing Android.mk file, use that instead
600 if len(files) > 0 {
Dan Willemsencdd1a992015-06-19 14:22:08 -0700601 for _, file := range files {
602 ctx.AddNinjaFileDeps(file)
603 }
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700604 continue
605 }
606
607 transMk := filepath.Join("androidmk", "Android_"+strings.Replace(filepath.Dir(origBp), "/", "_", -1)+".mk")
608 ctx.Build(pctx, blueprint.BuildParams{
609 Rule: androidbp,
610 Outputs: []string{transMk},
611 Inputs: []string{bpFile},
612 Implicits: []string{androidbpCmd},
613 Optional: true,
614 })
615
616 androidMks = append(androidMks, transMk)
617 }
618
619 ctx.Build(pctx, blueprint.BuildParams{
620 Rule: blueprint.Phony,
621 Outputs: []string{"androidmk"},
622 Implicits: androidMks,
623 Optional: true,
624 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700625}