blob: 36710c5a7e00821c608e4eeb080dbad144caead4 [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
Dan Willemsen34cc69e2015-09-23 15:26:20 -070035type ModuleBuildParams struct {
36 Rule blueprint.Rule
37 Output WritablePath
38 Outputs WritablePaths
39 Input Path
40 Inputs Paths
41 Implicit Path
42 Implicits Paths
43 OrderOnly Paths
44 Default bool
45 Args map[string]string
46}
47
Colin Crossf6566ed2015-03-24 11:13:38 -070048type androidBaseContext interface {
49 Arch() Arch
Colin Crossd3ba0392015-05-07 14:11:29 -070050 HostOrDevice() HostOrDevice
Dan Willemsen490fd492015-11-24 17:53:15 -080051 HostType() HostType
Colin Crossf6566ed2015-03-24 11:13:38 -070052 Host() bool
53 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070054 Darwin() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070055 Debug() bool
Colin Cross1332b002015-04-07 17:11:30 -070056 AConfig() Config
Colin Crossf6566ed2015-03-24 11:13:38 -070057}
58
59type AndroidBaseContext interface {
60 blueprint.BaseModuleContext
61 androidBaseContext
62}
63
Colin Cross3f40fa42015-01-30 17:27:36 -080064type AndroidModuleContext interface {
65 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070066 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080067
Dan Willemsen34cc69e2015-09-23 15:26:20 -070068 // Similar to Build, but takes Paths instead of []string,
69 // and performs more verification.
70 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070071
Dan Willemsen34cc69e2015-09-23 15:26:20 -070072 ExpandSources(srcFiles, excludes []string) Paths
73 Glob(outDir, globPattern string, excludes []string) Paths
74
75 InstallFile(installPath string, srcPath Path, deps ...Path) Path
76 InstallFileName(installPath, name string, srcPath Path, deps ...Path) Path
77 CheckbuildFile(srcPath Path)
Colin Cross3f40fa42015-01-30 17:27:36 -080078}
79
80type AndroidModule interface {
81 blueprint.Module
82
83 GenerateAndroidBuildActions(AndroidModuleContext)
84
85 base() *AndroidModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -080086 Enabled() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080087 HostOrDevice() HostOrDevice
88}
89
Colin Cross3f40fa42015-01-30 17:27:36 -080090type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -070091 Name string
92 Deps []string
93 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -080094
Dan Willemsen0effe062015-11-30 16:06:01 -080095 // emit build rules for this module
96 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -080097
Colin Cross7d5136f2015-05-11 13:39:40 -070098 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -080099 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
100 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
101 // platform
102 Compile_multilib string
103
Colin Crossd3ba0392015-05-07 14:11:29 -0700104 // Set by HostOrDeviceMutator
105 CompileHostOrDevice HostOrDevice `blueprint:"mutated"`
106
Dan Willemsen490fd492015-11-24 17:53:15 -0800107 // Set by HostTypeMutator
108 CompileHostType HostType `blueprint:"mutated"`
109
Colin Cross3f40fa42015-01-30 17:27:36 -0800110 // Set by ArchMutator
111 CompileArch Arch `blueprint:"mutated"`
112
113 // Set by InitAndroidModule
114 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
115}
116
117type hostAndDeviceProperties struct {
118 Host_supported bool
119 Device_supported bool
120}
121
Colin Crossc472d572015-03-17 15:06:21 -0700122type Multilib string
123
124const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700125 MultilibBoth Multilib = "both"
126 MultilibFirst Multilib = "first"
127 MultilibCommon Multilib = "common"
128 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700129)
130
Colin Cross5049f022015-03-18 13:28:46 -0700131func InitAndroidModule(m AndroidModule,
Colin Cross3f40fa42015-01-30 17:27:36 -0800132 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
133
134 base := m.base()
135 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700136
Colin Cross7f64b6d2015-07-09 13:57:48 -0700137 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700138
139 return m, propertyStructs
140}
141
142func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib,
143 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
144
145 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
146
147 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800148 base.commonProperties.HostOrDeviceSupported = hod
Colin Crosscfad1192015-11-02 16:43:11 -0800149 base.commonProperties.Compile_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800150
Dan Willemsen218f6562015-07-08 18:13:11 -0700151 switch hod {
152 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800153 // Default to module to device supported, host not supported, can override in module
154 // properties
155 base.hostAndDeviceProperties.Device_supported = true
Dan Willemsen218f6562015-07-08 18:13:11 -0700156 fallthrough
157 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800158 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
159 }
160
Colin Crosscfad1192015-11-02 16:43:11 -0800161 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800162}
163
164// A AndroidModuleBase object contains the properties that are common to all Android
165// modules. It should be included as an anonymous field in every module
166// struct definition. InitAndroidModule should then be called from the module's
167// factory function, and the return values from InitAndroidModule should be
168// returned from the factory function.
169//
170// The AndroidModuleBase type is responsible for implementing the
171// GenerateBuildActions method to support the blueprint.Module interface. This
172// method will then call the module's GenerateAndroidBuildActions method once
173// for each build variant that is to be built. GenerateAndroidBuildActions is
174// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
175// AndroidModuleContext exposes extra functionality specific to the Android build
176// system including details about the particular build variant that is to be
177// generated.
178//
179// For example:
180//
181// import (
182// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700183// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800184// )
185//
186// type myModule struct {
187// common.AndroidModuleBase
188// properties struct {
189// MyProperty string
190// }
191// }
192//
193// func NewMyModule() (blueprint.Module, []interface{}) {
194// m := &myModule{}
195// return common.InitAndroidModule(m, &m.properties)
196// }
197//
198// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
199// // Get the CPU architecture for the current build variant.
200// variantArch := ctx.Arch()
201//
202// // ...
203// }
204type AndroidModuleBase struct {
205 // Putting the curiously recurring thing pointing to the thing that contains
206 // the thing pattern to good use.
207 module AndroidModule
208
209 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700210 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800211 hostAndDeviceProperties hostAndDeviceProperties
212 generalProperties []interface{}
213 archProperties []*archProperties
214
215 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700216 installFiles Paths
217 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700218
219 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
220 // Only set on the final variant of each module
221 installTarget string
222 checkbuildTarget string
223 blueprintDir string
Colin Cross3f40fa42015-01-30 17:27:36 -0800224}
225
226func (a *AndroidModuleBase) base() *AndroidModuleBase {
227 return a
228}
229
Colin Crossd3ba0392015-05-07 14:11:29 -0700230func (a *AndroidModuleBase) SetHostOrDevice(hod HostOrDevice) {
231 a.commonProperties.CompileHostOrDevice = hod
232}
233
Dan Willemsen490fd492015-11-24 17:53:15 -0800234func (a *AndroidModuleBase) SetHostType(ht HostType) {
235 a.commonProperties.CompileHostType = ht
236}
237
Colin Cross3f40fa42015-01-30 17:27:36 -0800238func (a *AndroidModuleBase) SetArch(arch Arch) {
239 a.commonProperties.CompileArch = arch
240}
241
242func (a *AndroidModuleBase) HostOrDevice() HostOrDevice {
Colin Crossd3ba0392015-05-07 14:11:29 -0700243 return a.commonProperties.CompileHostOrDevice
Colin Cross3f40fa42015-01-30 17:27:36 -0800244}
245
Dan Willemsen490fd492015-11-24 17:53:15 -0800246func (a *AndroidModuleBase) HostType() HostType {
247 return a.commonProperties.CompileHostType
248}
249
Colin Cross3f40fa42015-01-30 17:27:36 -0800250func (a *AndroidModuleBase) HostSupported() bool {
251 return a.commonProperties.HostOrDeviceSupported == HostSupported ||
252 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
253 a.hostAndDeviceProperties.Host_supported
254}
255
256func (a *AndroidModuleBase) DeviceSupported() bool {
257 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
258 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
259 a.hostAndDeviceProperties.Device_supported
260}
261
Dan Willemsen0effe062015-11-30 16:06:01 -0800262func (a *AndroidModuleBase) Enabled() bool {
263 if a.commonProperties.Enabled == nil {
264 if a.HostSupported() && a.HostOrDevice().Host() && a.HostType() == Windows {
265 return false
266 } else {
267 return true
268 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800269 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800270 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800271}
272
273func (a *AndroidModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700274 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800275
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700276 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800277 ctx.VisitDepsDepthFirstIf(isFileInstaller,
278 func(m blueprint.Module) {
279 fileInstaller := m.(fileInstaller)
280 files := fileInstaller.filesToInstall()
281 result = append(result, files...)
282 })
283
284 return result
285}
286
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700287func (a *AndroidModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800288 return a.installFiles
289}
290
291func (p *AndroidModuleBase) NoAddressSanitizer() bool {
292 return p.noAddressSanitizer
293}
294
Colin Cross3f40fa42015-01-30 17:27:36 -0800295func (a *AndroidModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
296 if a != ctx.FinalModule().(AndroidModule).base() {
297 return
298 }
299
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700300 allInstalledFiles := Paths{}
301 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800302 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Crossc9404352015-03-26 16:10:12 -0700303 a := module.(AndroidModule).base()
304 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
305 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800306 })
307
Colin Cross9454bfa2015-03-17 13:24:18 -0700308 deps := []string{}
309
Colin Cross3f40fa42015-01-30 17:27:36 -0800310 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700311 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800312 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700313 Rule: blueprint.Phony,
314 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700315 Implicits: allInstalledFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700316 })
317 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700318 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700319 }
320
321 if len(allCheckbuildFiles) > 0 {
322 name := ctx.ModuleName() + "-checkbuild"
323 ctx.Build(pctx, blueprint.BuildParams{
324 Rule: blueprint.Phony,
325 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700326 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700327 Optional: true,
328 })
329 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700330 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700331 }
332
333 if len(deps) > 0 {
334 ctx.Build(pctx, blueprint.BuildParams{
335 Rule: blueprint.Phony,
Dan Willemsen218f6562015-07-08 18:13:11 -0700336 Outputs: []string{ctx.ModuleName() + "-soong"},
Colin Cross9454bfa2015-03-17 13:24:18 -0700337 Implicits: deps,
338 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800339 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700340
341 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800342 }
343}
344
Colin Cross6362e272015-10-29 15:25:03 -0700345func (a *AndroidModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
346 return androidBaseContextImpl{
347 arch: a.commonProperties.CompileArch,
348 hod: a.commonProperties.CompileHostOrDevice,
Dan Willemsen490fd492015-11-24 17:53:15 -0800349 ht: a.commonProperties.CompileHostType,
Colin Cross6362e272015-10-29 15:25:03 -0700350 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800351 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800352}
353
354func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
355 androidCtx := &androidModuleContext{
Colin Cross6362e272015-10-29 15:25:03 -0700356 ModuleContext: ctx,
357 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
358 installDeps: a.computeInstallDeps(ctx),
359 installFiles: a.installFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800360 }
361
Dan Willemsen0effe062015-11-30 16:06:01 -0800362 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800363 return
364 }
365
366 a.module.GenerateAndroidBuildActions(androidCtx)
367 if ctx.Failed() {
368 return
369 }
370
Colin Crossc9404352015-03-26 16:10:12 -0700371 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
372 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
373
Colin Cross3f40fa42015-01-30 17:27:36 -0800374 a.generateModuleTarget(ctx)
375 if ctx.Failed() {
376 return
377 }
378}
379
Colin Crossf6566ed2015-03-24 11:13:38 -0700380type androidBaseContextImpl struct {
Colin Cross1332b002015-04-07 17:11:30 -0700381 arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700382 hod HostOrDevice
Dan Willemsen490fd492015-11-24 17:53:15 -0800383 ht HostType
Colin Cross1332b002015-04-07 17:11:30 -0700384 debug bool
385 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700386}
387
Colin Cross3f40fa42015-01-30 17:27:36 -0800388type androidModuleContext struct {
389 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700390 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700391 installDeps Paths
392 installFiles Paths
393 checkbuildFiles Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800394}
395
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800396func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800397 params.Optional = true
398 a.ModuleContext.Build(pctx, params)
399}
400
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700401func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
402 bparams := blueprint.BuildParams{
403 Rule: params.Rule,
404 Outputs: params.Outputs.Strings(),
405 Inputs: params.Inputs.Strings(),
406 Implicits: params.Implicits.Strings(),
407 OrderOnly: params.OrderOnly.Strings(),
408 Args: params.Args,
409 Optional: !params.Default,
410 }
411
412 if params.Output != nil {
413 bparams.Outputs = append(bparams.Outputs, params.Output.String())
414 }
415 if params.Input != nil {
416 bparams.Inputs = append(bparams.Inputs, params.Input.String())
417 }
418 if params.Implicit != nil {
419 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
420 }
421
422 a.ModuleContext.Build(pctx, bparams)
423}
424
Colin Crossf6566ed2015-03-24 11:13:38 -0700425func (a *androidBaseContextImpl) Arch() Arch {
Colin Cross3f40fa42015-01-30 17:27:36 -0800426 return a.arch
427}
428
Colin Crossd3ba0392015-05-07 14:11:29 -0700429func (a *androidBaseContextImpl) HostOrDevice() HostOrDevice {
430 return a.hod
431}
432
Dan Willemsen490fd492015-11-24 17:53:15 -0800433func (a *androidBaseContextImpl) HostType() HostType {
434 return a.ht
435}
436
Colin Crossf6566ed2015-03-24 11:13:38 -0700437func (a *androidBaseContextImpl) Host() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700438 return a.hod.Host()
Colin Crossf6566ed2015-03-24 11:13:38 -0700439}
440
441func (a *androidBaseContextImpl) Device() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700442 return a.hod.Device()
Colin Crossf6566ed2015-03-24 11:13:38 -0700443}
444
Colin Cross0af4b842015-04-30 16:36:18 -0700445func (a *androidBaseContextImpl) Darwin() bool {
Dan Willemsen490fd492015-11-24 17:53:15 -0800446 return a.hod.Host() && a.ht == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700447}
448
Colin Crossf6566ed2015-03-24 11:13:38 -0700449func (a *androidBaseContextImpl) Debug() bool {
450 return a.debug
451}
452
Colin Cross1332b002015-04-07 17:11:30 -0700453func (a *androidBaseContextImpl) AConfig() Config {
454 return a.config
455}
456
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700457func (a *androidModuleContext) InstallFileName(installPath, name string, srcPath Path,
458 deps ...Path) Path {
Colin Cross35cec122015-04-02 14:37:16 -0700459
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700460 fullInstallPath := PathForModuleInstall(a, installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800461
Colin Cross35cec122015-04-02 14:37:16 -0700462 deps = append(deps, a.installDeps...)
463
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700464 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross3f40fa42015-01-30 17:27:36 -0800465 Rule: Cp,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700466 Output: fullInstallPath,
467 Input: srcPath,
468 OrderOnly: Paths(deps),
469 Default: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 })
471
472 a.installFiles = append(a.installFiles, fullInstallPath)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700473 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700474 return fullInstallPath
475}
476
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700477func (a *androidModuleContext) InstallFile(installPath string, srcPath Path, deps ...Path) Path {
478 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800479}
480
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700481func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800482 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
483}
484
Colin Cross3f40fa42015-01-30 17:27:36 -0800485type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700486 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800487}
488
489func isFileInstaller(m blueprint.Module) bool {
490 _, ok := m.(fileInstaller)
491 return ok
492}
493
494func isAndroidModule(m blueprint.Module) bool {
495 _, ok := m.(AndroidModule)
496 return ok
497}
Colin Crossfce53272015-04-08 11:21:40 -0700498
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700499func findStringInSlice(str string, slice []string) int {
500 for i, s := range slice {
501 if s == str {
502 return i
Colin Crossfce53272015-04-08 11:21:40 -0700503 }
504 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700505 return -1
506}
507
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700508func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
509 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700510 for i, e := range excludes {
511 j := findStringInSlice(e, srcFiles)
512 if j != -1 {
513 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
514 }
515
516 excludes[i] = filepath.Join(prefix, e)
517 }
518
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700519 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700520 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700521 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700522 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700523 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700524 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700525 }
526 }
527
528 return globbedSrcFiles
529}
530
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700531func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
532 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700533 if err != nil {
534 ctx.ModuleErrorf("glob: %s", err.Error())
535 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700536 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700537}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700538
Colin Cross463a90e2015-06-17 14:20:06 -0700539func init() {
540 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
541}
542
Colin Cross1f8c52b2015-06-16 16:38:17 -0700543func BuildTargetSingleton() blueprint.Singleton {
544 return &buildTargetSingleton{}
545}
546
547type buildTargetSingleton struct{}
548
549func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
550 checkbuildDeps := []string{}
551
552 dirModules := make(map[string][]string)
553
554 ctx.VisitAllModules(func(module blueprint.Module) {
555 if a, ok := module.(AndroidModule); ok {
556 blueprintDir := a.base().blueprintDir
557 installTarget := a.base().installTarget
558 checkbuildTarget := a.base().checkbuildTarget
559
560 if checkbuildTarget != "" {
561 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
562 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
563 }
564
565 if installTarget != "" {
566 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
567 }
568 }
569 })
570
571 // Create a top-level checkbuild target that depends on all modules
572 ctx.Build(pctx, blueprint.BuildParams{
573 Rule: blueprint.Phony,
Dan Willemsen218f6562015-07-08 18:13:11 -0700574 Outputs: []string{"checkbuild-soong"},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700575 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700576 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700577 })
578
579 // Create a mm/<directory> target that depends on all modules in a directory
580 dirs := sortedKeys(dirModules)
581 for _, dir := range dirs {
582 ctx.Build(pctx, blueprint.BuildParams{
583 Rule: blueprint.Phony,
584 Outputs: []string{filepath.Join("mm", dir)},
585 Implicits: dirModules[dir],
586 Optional: true,
587 })
588 }
589}