blob: d31a9fc238ab801705916cddbdf33952ca872f4e [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 Cross5049f022015-03-18 13:28:46 -0700126
Colin Cross7f64b6d2015-07-09 13:57:48 -0700127 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700128
129 return m, propertyStructs
130}
131
132func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib,
133 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
134
135 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
136
137 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800138 base.commonProperties.HostOrDeviceSupported = hod
139
140 if hod == HostAndDeviceSupported {
141 // Default to module to device supported, host not supported, can override in module
142 // properties
143 base.hostAndDeviceProperties.Device_supported = true
144 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
145 }
146
147 return InitArchModule(m, defaultMultilib, propertyStructs...)
148}
149
150// A AndroidModuleBase object contains the properties that are common to all Android
151// modules. It should be included as an anonymous field in every module
152// struct definition. InitAndroidModule should then be called from the module's
153// factory function, and the return values from InitAndroidModule should be
154// returned from the factory function.
155//
156// The AndroidModuleBase type is responsible for implementing the
157// GenerateBuildActions method to support the blueprint.Module interface. This
158// method will then call the module's GenerateAndroidBuildActions method once
159// for each build variant that is to be built. GenerateAndroidBuildActions is
160// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
161// AndroidModuleContext exposes extra functionality specific to the Android build
162// system including details about the particular build variant that is to be
163// generated.
164//
165// For example:
166//
167// import (
168// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700169// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800170// )
171//
172// type myModule struct {
173// common.AndroidModuleBase
174// properties struct {
175// MyProperty string
176// }
177// }
178//
179// func NewMyModule() (blueprint.Module, []interface{}) {
180// m := &myModule{}
181// return common.InitAndroidModule(m, &m.properties)
182// }
183//
184// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
185// // Get the CPU architecture for the current build variant.
186// variantArch := ctx.Arch()
187//
188// // ...
189// }
190type AndroidModuleBase struct {
191 // Putting the curiously recurring thing pointing to the thing that contains
192 // the thing pattern to good use.
193 module AndroidModule
194
195 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700196 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800197 hostAndDeviceProperties hostAndDeviceProperties
198 generalProperties []interface{}
199 archProperties []*archProperties
200
201 noAddressSanitizer bool
202 installFiles []string
203 checkbuildFiles []string
Colin Cross1f8c52b2015-06-16 16:38:17 -0700204
205 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
206 // Only set on the final variant of each module
207 installTarget string
208 checkbuildTarget string
209 blueprintDir string
Colin Cross3f40fa42015-01-30 17:27:36 -0800210}
211
212func (a *AndroidModuleBase) base() *AndroidModuleBase {
213 return a
214}
215
Colin Crossd3ba0392015-05-07 14:11:29 -0700216func (a *AndroidModuleBase) SetHostOrDevice(hod HostOrDevice) {
217 a.commonProperties.CompileHostOrDevice = hod
218}
219
Colin Cross3f40fa42015-01-30 17:27:36 -0800220func (a *AndroidModuleBase) SetArch(arch Arch) {
221 a.commonProperties.CompileArch = arch
222}
223
224func (a *AndroidModuleBase) HostOrDevice() HostOrDevice {
Colin Crossd3ba0392015-05-07 14:11:29 -0700225 return a.commonProperties.CompileHostOrDevice
Colin Cross3f40fa42015-01-30 17:27:36 -0800226}
227
228func (a *AndroidModuleBase) HostSupported() bool {
229 return a.commonProperties.HostOrDeviceSupported == HostSupported ||
230 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
231 a.hostAndDeviceProperties.Host_supported
232}
233
234func (a *AndroidModuleBase) DeviceSupported() bool {
235 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
236 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
237 a.hostAndDeviceProperties.Device_supported
238}
239
240func (a *AndroidModuleBase) Disabled() bool {
241 return a.commonProperties.Disabled
242}
243
244func (a *AndroidModuleBase) computeInstallDeps(
245 ctx blueprint.ModuleContext) []string {
246
247 result := []string{}
248 ctx.VisitDepsDepthFirstIf(isFileInstaller,
249 func(m blueprint.Module) {
250 fileInstaller := m.(fileInstaller)
251 files := fileInstaller.filesToInstall()
252 result = append(result, files...)
253 })
254
255 return result
256}
257
258func (a *AndroidModuleBase) filesToInstall() []string {
259 return a.installFiles
260}
261
262func (p *AndroidModuleBase) NoAddressSanitizer() bool {
263 return p.noAddressSanitizer
264}
265
Colin Cross3f40fa42015-01-30 17:27:36 -0800266func (a *AndroidModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
267 if a != ctx.FinalModule().(AndroidModule).base() {
268 return
269 }
270
271 allInstalledFiles := []string{}
Colin Cross9454bfa2015-03-17 13:24:18 -0700272 allCheckbuildFiles := []string{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800273 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Crossc9404352015-03-26 16:10:12 -0700274 a := module.(AndroidModule).base()
275 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
276 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800277 })
278
Colin Cross9454bfa2015-03-17 13:24:18 -0700279 deps := []string{}
280
Colin Cross3f40fa42015-01-30 17:27:36 -0800281 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700282 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800283 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700284 Rule: blueprint.Phony,
285 Outputs: []string{name},
286 Implicits: allInstalledFiles,
287 })
288 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700289 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700290 }
291
292 if len(allCheckbuildFiles) > 0 {
293 name := ctx.ModuleName() + "-checkbuild"
294 ctx.Build(pctx, blueprint.BuildParams{
295 Rule: blueprint.Phony,
296 Outputs: []string{name},
297 Implicits: allCheckbuildFiles,
298 Optional: true,
299 })
300 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700301 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700302 }
303
304 if len(deps) > 0 {
305 ctx.Build(pctx, blueprint.BuildParams{
306 Rule: blueprint.Phony,
307 Outputs: []string{ctx.ModuleName()},
308 Implicits: deps,
309 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800310 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700311
312 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800313 }
314}
315
316func (a *AndroidModuleBase) DynamicDependencies(ctx blueprint.DynamicDependerModuleContext) []string {
317 actx := &androidDynamicDependerContext{
318 DynamicDependerModuleContext: ctx,
Colin Crossf6566ed2015-03-24 11:13:38 -0700319 androidBaseContextImpl: androidBaseContextImpl{
Colin Cross1332b002015-04-07 17:11:30 -0700320 arch: a.commonProperties.CompileArch,
Colin Crossd3ba0392015-05-07 14:11:29 -0700321 hod: a.commonProperties.CompileHostOrDevice,
Colin Cross1332b002015-04-07 17:11:30 -0700322 config: ctx.Config().(Config),
Colin Crossf6566ed2015-03-24 11:13:38 -0700323 },
Colin Cross3f40fa42015-01-30 17:27:36 -0800324 }
325
326 if dynamic, ok := a.module.(AndroidDynamicDepender); ok {
327 return dynamic.AndroidDynamicDependencies(actx)
328 }
329
330 return nil
331}
332
333func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
334 androidCtx := &androidModuleContext{
335 ModuleContext: ctx,
Colin Crossf6566ed2015-03-24 11:13:38 -0700336 androidBaseContextImpl: androidBaseContextImpl{
Colin Cross1332b002015-04-07 17:11:30 -0700337 arch: a.commonProperties.CompileArch,
Colin Crossd3ba0392015-05-07 14:11:29 -0700338 hod: a.commonProperties.CompileHostOrDevice,
Colin Cross1332b002015-04-07 17:11:30 -0700339 config: ctx.Config().(Config),
Colin Crossf6566ed2015-03-24 11:13:38 -0700340 },
Colin Cross06a931b2015-10-28 17:23:31 -0700341 installDeps: a.computeInstallDeps(ctx),
342 installFiles: a.installFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800343 }
344
345 if a.commonProperties.Disabled {
346 return
347 }
348
349 a.module.GenerateAndroidBuildActions(androidCtx)
350 if ctx.Failed() {
351 return
352 }
353
Colin Crossc9404352015-03-26 16:10:12 -0700354 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
355 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
356
Colin Cross3f40fa42015-01-30 17:27:36 -0800357 a.generateModuleTarget(ctx)
358 if ctx.Failed() {
359 return
360 }
361}
362
Colin Crossf6566ed2015-03-24 11:13:38 -0700363type androidBaseContextImpl struct {
Colin Cross1332b002015-04-07 17:11:30 -0700364 arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700365 hod HostOrDevice
Colin Cross1332b002015-04-07 17:11:30 -0700366 debug bool
367 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700368}
369
Colin Cross3f40fa42015-01-30 17:27:36 -0800370type androidModuleContext struct {
371 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700372 androidBaseContextImpl
Colin Cross06a931b2015-10-28 17:23:31 -0700373 installDeps []string
374 installFiles []string
375 checkbuildFiles []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800376}
377
378func (a *androidModuleContext) Build(pctx *blueprint.PackageContext, params blueprint.BuildParams) {
379 params.Optional = true
380 a.ModuleContext.Build(pctx, params)
381}
382
Colin Crossf6566ed2015-03-24 11:13:38 -0700383func (a *androidBaseContextImpl) Arch() Arch {
Colin Cross3f40fa42015-01-30 17:27:36 -0800384 return a.arch
385}
386
Colin Crossd3ba0392015-05-07 14:11:29 -0700387func (a *androidBaseContextImpl) HostOrDevice() HostOrDevice {
388 return a.hod
389}
390
Colin Crossf6566ed2015-03-24 11:13:38 -0700391func (a *androidBaseContextImpl) Host() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700392 return a.hod.Host()
Colin Crossf6566ed2015-03-24 11:13:38 -0700393}
394
395func (a *androidBaseContextImpl) Device() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700396 return a.hod.Device()
Colin Crossf6566ed2015-03-24 11:13:38 -0700397}
398
Colin Cross0af4b842015-04-30 16:36:18 -0700399func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700400 return a.hod.Host() && runtime.GOOS == "darwin"
Colin Cross0af4b842015-04-30 16:36:18 -0700401}
402
Colin Crossf6566ed2015-03-24 11:13:38 -0700403func (a *androidBaseContextImpl) Debug() bool {
404 return a.debug
405}
406
Colin Cross1332b002015-04-07 17:11:30 -0700407func (a *androidBaseContextImpl) AConfig() Config {
408 return a.config
409}
410
Colin Cross35cec122015-04-02 14:37:16 -0700411func (a *androidModuleContext) InstallFileName(installPath, name, srcPath string,
412 deps ...string) string {
413
Colin Cross1332b002015-04-07 17:11:30 -0700414 config := a.AConfig()
Colin Cross3f40fa42015-01-30 17:27:36 -0800415 var fullInstallPath string
Colin Crossd3ba0392015-05-07 14:11:29 -0700416 if a.hod.Device() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800417 // TODO: replace unset with a device name once we have device targeting
Colin Cross35cec122015-04-02 14:37:16 -0700418 fullInstallPath = filepath.Join(config.DeviceOut(), "system",
419 installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800420 } else {
Colin Cross35cec122015-04-02 14:37:16 -0700421 fullInstallPath = filepath.Join(config.HostOut(), installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800422 }
423
Colin Cross35cec122015-04-02 14:37:16 -0700424 deps = append(deps, a.installDeps...)
425
Colin Cross3f40fa42015-01-30 17:27:36 -0800426 a.ModuleContext.Build(pctx, blueprint.BuildParams{
427 Rule: Cp,
428 Outputs: []string{fullInstallPath},
429 Inputs: []string{srcPath},
Colin Cross35cec122015-04-02 14:37:16 -0700430 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800431 })
432
433 a.installFiles = append(a.installFiles, fullInstallPath)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700434 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700435 return fullInstallPath
436}
437
438func (a *androidModuleContext) InstallFile(installPath, srcPath string, deps ...string) string {
439 return a.InstallFileName(installPath, filepath.Base(srcPath), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800440}
441
442func (a *androidModuleContext) CheckbuildFile(srcPath string) {
443 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
444}
445
446type androidDynamicDependerContext struct {
447 blueprint.DynamicDependerModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700448 androidBaseContextImpl
Colin Cross3f40fa42015-01-30 17:27:36 -0800449}
450
451type fileInstaller interface {
452 filesToInstall() []string
453}
454
455func isFileInstaller(m blueprint.Module) bool {
456 _, ok := m.(fileInstaller)
457 return ok
458}
459
460func isAndroidModule(m blueprint.Module) bool {
461 _, ok := m.(AndroidModule)
462 return ok
463}
Colin Crossfce53272015-04-08 11:21:40 -0700464
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700465func findStringInSlice(str string, slice []string) int {
466 for i, s := range slice {
467 if s == str {
468 return i
Colin Crossfce53272015-04-08 11:21:40 -0700469 }
470 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700471 return -1
472}
473
474func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) []string {
475 prefix := ModuleSrcDir(ctx)
476 for i, e := range excludes {
477 j := findStringInSlice(e, srcFiles)
478 if j != -1 {
479 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
480 }
481
482 excludes[i] = filepath.Join(prefix, e)
483 }
484
485 for i, srcFile := range srcFiles {
486 srcFiles[i] = filepath.Join(prefix, srcFile)
487 }
Colin Crossfce53272015-04-08 11:21:40 -0700488
Colin Cross8f101b42015-06-17 15:09:06 -0700489 if !hasGlob(srcFiles) {
490 return srcFiles
491 }
492
Colin Cross8f101b42015-06-17 15:09:06 -0700493 globbedSrcFiles := make([]string, 0, len(srcFiles))
494 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700495 if glob.IsGlob(s) {
Colin Crossa819f082015-07-14 18:26:10 -0700496 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", s, excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700497 } else {
498 globbedSrcFiles = append(globbedSrcFiles, s)
499 }
500 }
501
502 return globbedSrcFiles
503}
504
Colin Crossa819f082015-07-14 18:26:10 -0700505func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) []string {
506 ret, err := Glob(ctx, filepath.Join(ModuleOutDir(ctx), outDir), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700507 if err != nil {
508 ctx.ModuleErrorf("glob: %s", err.Error())
509 }
510 return ret
Colin Crossfce53272015-04-08 11:21:40 -0700511}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700512
Colin Cross463a90e2015-06-17 14:20:06 -0700513func init() {
514 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
515}
516
Colin Cross1f8c52b2015-06-16 16:38:17 -0700517func BuildTargetSingleton() blueprint.Singleton {
518 return &buildTargetSingleton{}
519}
520
521type buildTargetSingleton struct{}
522
523func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
524 checkbuildDeps := []string{}
525
526 dirModules := make(map[string][]string)
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700527 hasBPFile := make(map[string]bool)
528 bpFiles := []string{}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700529
530 ctx.VisitAllModules(func(module blueprint.Module) {
531 if a, ok := module.(AndroidModule); ok {
532 blueprintDir := a.base().blueprintDir
533 installTarget := a.base().installTarget
534 checkbuildTarget := a.base().checkbuildTarget
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700535 bpFile := ctx.BlueprintFile(module)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700536
537 if checkbuildTarget != "" {
538 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
539 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
540 }
541
542 if installTarget != "" {
543 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
544 }
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700545
546 if !hasBPFile[bpFile] {
547 hasBPFile[bpFile] = true
548 bpFiles = append(bpFiles, bpFile)
549 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700550 }
551 })
552
553 // Create a top-level checkbuild target that depends on all modules
554 ctx.Build(pctx, blueprint.BuildParams{
555 Rule: blueprint.Phony,
556 Outputs: []string{"checkbuild"},
557 Implicits: checkbuildDeps,
558 // HACK: checkbuild should be an optional build, but force it enabled for now
559 //Optional: true,
560 })
561
562 // Create a mm/<directory> target that depends on all modules in a directory
563 dirs := sortedKeys(dirModules)
564 for _, dir := range dirs {
565 ctx.Build(pctx, blueprint.BuildParams{
566 Rule: blueprint.Phony,
567 Outputs: []string{filepath.Join("mm", dir)},
568 Implicits: dirModules[dir],
569 Optional: true,
570 })
571 }
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700572
573 // Create Android.bp->mk translation rules
574 androidMks := []string{}
575 srcDir := ctx.Config().(Config).SrcDir()
576 intermediatesDir := filepath.Join(ctx.Config().(Config).IntermediatesDir(), "androidmk")
577 sort.Strings(bpFiles)
578 for _, origBp := range bpFiles {
579 bpFile := filepath.Join(srcDir, origBp)
580 mkFile := filepath.Join(srcDir, filepath.Dir(origBp), "Android.mk")
581
582 files, err := Glob(ctx, intermediatesDir, mkFile, nil)
583 if err != nil {
584 ctx.Errorf("glob: %s", err.Error())
585 continue
586 }
587
588 // Existing Android.mk file, use that instead
589 if len(files) > 0 {
Dan Willemsencdd1a992015-06-19 14:22:08 -0700590 for _, file := range files {
591 ctx.AddNinjaFileDeps(file)
592 }
Dan Willemsenbf9207a2015-06-17 15:17:12 -0700593 continue
594 }
595
596 transMk := filepath.Join("androidmk", "Android_"+strings.Replace(filepath.Dir(origBp), "/", "_", -1)+".mk")
597 ctx.Build(pctx, blueprint.BuildParams{
598 Rule: androidbp,
599 Outputs: []string{transMk},
600 Inputs: []string{bpFile},
601 Implicits: []string{androidbpCmd},
602 Optional: true,
603 })
604
605 androidMks = append(androidMks, transMk)
606 }
607
608 ctx.Build(pctx, blueprint.BuildParams{
609 Rule: blueprint.Phony,
610 Outputs: []string{"androidmk"},
611 Implicits: androidMks,
612 Optional: true,
613 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700614}