blob: c22f000e7e914be6ad0913a50a517615028b5467 [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 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800334 suffix := ""
335 if ctx.Config().(Config).EmbeddedInMake() {
336 suffix = "-soong"
337 }
338
Colin Cross9454bfa2015-03-17 13:24:18 -0700339 ctx.Build(pctx, blueprint.BuildParams{
340 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800341 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700342 Implicits: deps,
343 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800344 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700345
346 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800347 }
348}
349
Colin Cross6362e272015-10-29 15:25:03 -0700350func (a *AndroidModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
351 return androidBaseContextImpl{
352 arch: a.commonProperties.CompileArch,
353 hod: a.commonProperties.CompileHostOrDevice,
Dan Willemsen490fd492015-11-24 17:53:15 -0800354 ht: a.commonProperties.CompileHostType,
Colin Cross6362e272015-10-29 15:25:03 -0700355 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800356 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800357}
358
359func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
360 androidCtx := &androidModuleContext{
Colin Cross6362e272015-10-29 15:25:03 -0700361 ModuleContext: ctx,
362 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
363 installDeps: a.computeInstallDeps(ctx),
364 installFiles: a.installFiles,
Colin Cross3f40fa42015-01-30 17:27:36 -0800365 }
366
Dan Willemsen0effe062015-11-30 16:06:01 -0800367 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800368 return
369 }
370
371 a.module.GenerateAndroidBuildActions(androidCtx)
372 if ctx.Failed() {
373 return
374 }
375
Colin Crossc9404352015-03-26 16:10:12 -0700376 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
377 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
378
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 a.generateModuleTarget(ctx)
380 if ctx.Failed() {
381 return
382 }
383}
384
Colin Crossf6566ed2015-03-24 11:13:38 -0700385type androidBaseContextImpl struct {
Colin Cross1332b002015-04-07 17:11:30 -0700386 arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700387 hod HostOrDevice
Dan Willemsen490fd492015-11-24 17:53:15 -0800388 ht HostType
Colin Cross1332b002015-04-07 17:11:30 -0700389 debug bool
390 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700391}
392
Colin Cross3f40fa42015-01-30 17:27:36 -0800393type androidModuleContext struct {
394 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700395 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700396 installDeps Paths
397 installFiles Paths
398 checkbuildFiles Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800399}
400
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800401func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800402 params.Optional = true
403 a.ModuleContext.Build(pctx, params)
404}
405
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700406func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
407 bparams := blueprint.BuildParams{
408 Rule: params.Rule,
409 Outputs: params.Outputs.Strings(),
410 Inputs: params.Inputs.Strings(),
411 Implicits: params.Implicits.Strings(),
412 OrderOnly: params.OrderOnly.Strings(),
413 Args: params.Args,
414 Optional: !params.Default,
415 }
416
417 if params.Output != nil {
418 bparams.Outputs = append(bparams.Outputs, params.Output.String())
419 }
420 if params.Input != nil {
421 bparams.Inputs = append(bparams.Inputs, params.Input.String())
422 }
423 if params.Implicit != nil {
424 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
425 }
426
427 a.ModuleContext.Build(pctx, bparams)
428}
429
Colin Crossf6566ed2015-03-24 11:13:38 -0700430func (a *androidBaseContextImpl) Arch() Arch {
Colin Cross3f40fa42015-01-30 17:27:36 -0800431 return a.arch
432}
433
Colin Crossd3ba0392015-05-07 14:11:29 -0700434func (a *androidBaseContextImpl) HostOrDevice() HostOrDevice {
435 return a.hod
436}
437
Dan Willemsen490fd492015-11-24 17:53:15 -0800438func (a *androidBaseContextImpl) HostType() HostType {
439 return a.ht
440}
441
Colin Crossf6566ed2015-03-24 11:13:38 -0700442func (a *androidBaseContextImpl) Host() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700443 return a.hod.Host()
Colin Crossf6566ed2015-03-24 11:13:38 -0700444}
445
446func (a *androidBaseContextImpl) Device() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700447 return a.hod.Device()
Colin Crossf6566ed2015-03-24 11:13:38 -0700448}
449
Colin Cross0af4b842015-04-30 16:36:18 -0700450func (a *androidBaseContextImpl) Darwin() bool {
Dan Willemsen490fd492015-11-24 17:53:15 -0800451 return a.hod.Host() && a.ht == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700452}
453
Colin Crossf6566ed2015-03-24 11:13:38 -0700454func (a *androidBaseContextImpl) Debug() bool {
455 return a.debug
456}
457
Colin Cross1332b002015-04-07 17:11:30 -0700458func (a *androidBaseContextImpl) AConfig() Config {
459 return a.config
460}
461
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700462func (a *androidModuleContext) InstallFileName(installPath, name string, srcPath Path,
463 deps ...Path) Path {
Colin Cross35cec122015-04-02 14:37:16 -0700464
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700465 fullInstallPath := PathForModuleInstall(a, installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800466
Colin Cross35cec122015-04-02 14:37:16 -0700467 deps = append(deps, a.installDeps...)
468
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700469 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 Rule: Cp,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700471 Output: fullInstallPath,
472 Input: srcPath,
473 OrderOnly: Paths(deps),
474 Default: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800475 })
476
477 a.installFiles = append(a.installFiles, fullInstallPath)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700478 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700479 return fullInstallPath
480}
481
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700482func (a *androidModuleContext) InstallFile(installPath string, srcPath Path, deps ...Path) Path {
483 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800484}
485
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700486func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800487 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
488}
489
Colin Cross3f40fa42015-01-30 17:27:36 -0800490type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700491 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800492}
493
494func isFileInstaller(m blueprint.Module) bool {
495 _, ok := m.(fileInstaller)
496 return ok
497}
498
499func isAndroidModule(m blueprint.Module) bool {
500 _, ok := m.(AndroidModule)
501 return ok
502}
Colin Crossfce53272015-04-08 11:21:40 -0700503
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700504func findStringInSlice(str string, slice []string) int {
505 for i, s := range slice {
506 if s == str {
507 return i
Colin Crossfce53272015-04-08 11:21:40 -0700508 }
509 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700510 return -1
511}
512
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700513func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
514 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700515 for i, e := range excludes {
516 j := findStringInSlice(e, srcFiles)
517 if j != -1 {
518 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
519 }
520
521 excludes[i] = filepath.Join(prefix, e)
522 }
523
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700524 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700525 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700526 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700527 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700528 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700529 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700530 }
531 }
532
533 return globbedSrcFiles
534}
535
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700536func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
537 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700538 if err != nil {
539 ctx.ModuleErrorf("glob: %s", err.Error())
540 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700541 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700542}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700543
Colin Cross463a90e2015-06-17 14:20:06 -0700544func init() {
545 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
546}
547
Colin Cross1f8c52b2015-06-16 16:38:17 -0700548func BuildTargetSingleton() blueprint.Singleton {
549 return &buildTargetSingleton{}
550}
551
552type buildTargetSingleton struct{}
553
554func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
555 checkbuildDeps := []string{}
556
557 dirModules := make(map[string][]string)
558
559 ctx.VisitAllModules(func(module blueprint.Module) {
560 if a, ok := module.(AndroidModule); ok {
561 blueprintDir := a.base().blueprintDir
562 installTarget := a.base().installTarget
563 checkbuildTarget := a.base().checkbuildTarget
564
565 if checkbuildTarget != "" {
566 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
567 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
568 }
569
570 if installTarget != "" {
571 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
572 }
573 }
574 })
575
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800576 suffix := ""
577 if ctx.Config().(Config).EmbeddedInMake() {
578 suffix = "-soong"
579 }
580
Colin Cross1f8c52b2015-06-16 16:38:17 -0700581 // Create a top-level checkbuild target that depends on all modules
582 ctx.Build(pctx, blueprint.BuildParams{
583 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800584 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700585 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700586 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700587 })
588
589 // Create a mm/<directory> target that depends on all modules in a directory
590 dirs := sortedKeys(dirModules)
591 for _, dir := range dirs {
592 ctx.Build(pctx, blueprint.BuildParams{
593 Rule: blueprint.Phony,
594 Outputs: []string{filepath.Join("mm", dir)},
595 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800596 // HACK: checkbuild should be an optional build, but force it
597 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800598 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700599 })
600 }
601}