blob: 988104ffeb452e2e748ac19da0d855b5a60d89de [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 Cross6ff51382015-12-17 16:39:19 -080018 "fmt"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "path/filepath"
Colin Cross6ff51382015-12-17 16:39:19 -080020 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070021
Dan Willemsen0effe062015-11-30 16:06:01 -080022 "android/soong"
Colin Cross8f101b42015-06-17 15:09:06 -070023 "android/soong/glob"
24
Colin Crossf6566ed2015-03-24 11:13:38 -070025 "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -080026)
27
28var (
29 DeviceSharedLibrary = "shared_library"
30 DeviceStaticLibrary = "static_library"
31 DeviceExecutable = "executable"
32 HostSharedLibrary = "host_shared_library"
33 HostStaticLibrary = "host_static_library"
34 HostExecutable = "host_executable"
35)
36
Dan Willemsen34cc69e2015-09-23 15:26:20 -070037type ModuleBuildParams struct {
38 Rule blueprint.Rule
39 Output WritablePath
40 Outputs WritablePaths
41 Input Path
42 Inputs Paths
43 Implicit Path
44 Implicits Paths
45 OrderOnly Paths
46 Default bool
47 Args map[string]string
48}
49
Colin Crossf6566ed2015-03-24 11:13:38 -070050type androidBaseContext interface {
51 Arch() Arch
Colin Crossd3ba0392015-05-07 14:11:29 -070052 HostOrDevice() HostOrDevice
Dan Willemsen490fd492015-11-24 17:53:15 -080053 HostType() HostType
Colin Crossf6566ed2015-03-24 11:13:38 -070054 Host() bool
55 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070056 Darwin() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070057 Debug() bool
Colin Cross1332b002015-04-07 17:11:30 -070058 AConfig() Config
Dan Willemsen782a2d12015-12-21 14:55:28 -080059 Proprietary() bool
60 InstallInData() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070061}
62
63type AndroidBaseContext interface {
64 blueprint.BaseModuleContext
65 androidBaseContext
66}
67
Colin Cross3f40fa42015-01-30 17:27:36 -080068type AndroidModuleContext interface {
69 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070070 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080071
Dan Willemsen34cc69e2015-09-23 15:26:20 -070072 // Similar to Build, but takes Paths instead of []string,
73 // and performs more verification.
74 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070075
Dan Willemsen34cc69e2015-09-23 15:26:20 -070076 ExpandSources(srcFiles, excludes []string) Paths
77 Glob(outDir, globPattern string, excludes []string) Paths
78
Dan Willemsen782a2d12015-12-21 14:55:28 -080079 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) Path
80 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) Path
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 CheckbuildFile(srcPath Path)
Colin Cross3f40fa42015-01-30 17:27:36 -080082}
83
84type AndroidModule interface {
85 blueprint.Module
86
87 GenerateAndroidBuildActions(AndroidModuleContext)
88
89 base() *AndroidModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -080090 Enabled() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080091 HostOrDevice() HostOrDevice
Dan Willemsen782a2d12015-12-21 14:55:28 -080092 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080093}
94
Colin Cross3f40fa42015-01-30 17:27:36 -080095type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -070096 Name string
97 Deps []string
98 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -080099
Dan Willemsen0effe062015-11-30 16:06:01 -0800100 // emit build rules for this module
101 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800102
Colin Cross7d5136f2015-05-11 13:39:40 -0700103 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800104 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
105 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
106 // platform
107 Compile_multilib string
108
Dan Willemsen782a2d12015-12-21 14:55:28 -0800109 // whether this is a proprietary vendor module, and should be installed into /vendor
110 Proprietary bool
111
Colin Crossd3ba0392015-05-07 14:11:29 -0700112 // Set by HostOrDeviceMutator
113 CompileHostOrDevice HostOrDevice `blueprint:"mutated"`
114
Dan Willemsen490fd492015-11-24 17:53:15 -0800115 // Set by HostTypeMutator
116 CompileHostType HostType `blueprint:"mutated"`
117
Colin Cross3f40fa42015-01-30 17:27:36 -0800118 // Set by ArchMutator
119 CompileArch Arch `blueprint:"mutated"`
120
121 // Set by InitAndroidModule
122 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
123}
124
125type hostAndDeviceProperties struct {
126 Host_supported bool
127 Device_supported bool
128}
129
Colin Crossc472d572015-03-17 15:06:21 -0700130type Multilib string
131
132const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700133 MultilibBoth Multilib = "both"
134 MultilibFirst Multilib = "first"
135 MultilibCommon Multilib = "common"
136 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700137)
138
Colin Cross5049f022015-03-18 13:28:46 -0700139func InitAndroidModule(m AndroidModule,
Colin Cross3f40fa42015-01-30 17:27:36 -0800140 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
141
142 base := m.base()
143 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700144
Colin Cross7f64b6d2015-07-09 13:57:48 -0700145 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700146
147 return m, propertyStructs
148}
149
150func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib,
151 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
152
153 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
154
155 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800156 base.commonProperties.HostOrDeviceSupported = hod
Colin Crosscfad1192015-11-02 16:43:11 -0800157 base.commonProperties.Compile_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800158
Dan Willemsen218f6562015-07-08 18:13:11 -0700159 switch hod {
160 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800161 // Default to module to device supported, host not supported, can override in module
162 // properties
163 base.hostAndDeviceProperties.Device_supported = true
Dan Willemsen218f6562015-07-08 18:13:11 -0700164 fallthrough
165 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800166 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
167 }
168
Colin Crosscfad1192015-11-02 16:43:11 -0800169 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800170}
171
172// A AndroidModuleBase object contains the properties that are common to all Android
173// modules. It should be included as an anonymous field in every module
174// struct definition. InitAndroidModule should then be called from the module's
175// factory function, and the return values from InitAndroidModule should be
176// returned from the factory function.
177//
178// The AndroidModuleBase type is responsible for implementing the
179// GenerateBuildActions method to support the blueprint.Module interface. This
180// method will then call the module's GenerateAndroidBuildActions method once
181// for each build variant that is to be built. GenerateAndroidBuildActions is
182// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
183// AndroidModuleContext exposes extra functionality specific to the Android build
184// system including details about the particular build variant that is to be
185// generated.
186//
187// For example:
188//
189// import (
190// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700191// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800192// )
193//
194// type myModule struct {
195// common.AndroidModuleBase
196// properties struct {
197// MyProperty string
198// }
199// }
200//
201// func NewMyModule() (blueprint.Module, []interface{}) {
202// m := &myModule{}
203// return common.InitAndroidModule(m, &m.properties)
204// }
205//
206// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
207// // Get the CPU architecture for the current build variant.
208// variantArch := ctx.Arch()
209//
210// // ...
211// }
212type AndroidModuleBase struct {
213 // Putting the curiously recurring thing pointing to the thing that contains
214 // the thing pattern to good use.
215 module AndroidModule
216
217 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700218 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800219 hostAndDeviceProperties hostAndDeviceProperties
220 generalProperties []interface{}
221 archProperties []*archProperties
222
223 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700224 installFiles Paths
225 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700226
227 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
228 // Only set on the final variant of each module
229 installTarget string
230 checkbuildTarget string
231 blueprintDir string
Colin Cross3f40fa42015-01-30 17:27:36 -0800232}
233
234func (a *AndroidModuleBase) base() *AndroidModuleBase {
235 return a
236}
237
Colin Crossd3ba0392015-05-07 14:11:29 -0700238func (a *AndroidModuleBase) SetHostOrDevice(hod HostOrDevice) {
239 a.commonProperties.CompileHostOrDevice = hod
240}
241
Dan Willemsen490fd492015-11-24 17:53:15 -0800242func (a *AndroidModuleBase) SetHostType(ht HostType) {
243 a.commonProperties.CompileHostType = ht
244}
245
Colin Cross3f40fa42015-01-30 17:27:36 -0800246func (a *AndroidModuleBase) SetArch(arch Arch) {
247 a.commonProperties.CompileArch = arch
248}
249
250func (a *AndroidModuleBase) HostOrDevice() HostOrDevice {
Colin Crossd3ba0392015-05-07 14:11:29 -0700251 return a.commonProperties.CompileHostOrDevice
Colin Cross3f40fa42015-01-30 17:27:36 -0800252}
253
Dan Willemsen490fd492015-11-24 17:53:15 -0800254func (a *AndroidModuleBase) HostType() HostType {
255 return a.commonProperties.CompileHostType
256}
257
Colin Cross3f40fa42015-01-30 17:27:36 -0800258func (a *AndroidModuleBase) HostSupported() bool {
259 return a.commonProperties.HostOrDeviceSupported == HostSupported ||
260 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
261 a.hostAndDeviceProperties.Host_supported
262}
263
264func (a *AndroidModuleBase) DeviceSupported() bool {
265 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
266 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
267 a.hostAndDeviceProperties.Device_supported
268}
269
Dan Willemsen0effe062015-11-30 16:06:01 -0800270func (a *AndroidModuleBase) Enabled() bool {
271 if a.commonProperties.Enabled == nil {
272 if a.HostSupported() && a.HostOrDevice().Host() && a.HostType() == Windows {
273 return false
274 } else {
275 return true
276 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800277 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800278 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800279}
280
281func (a *AndroidModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700282 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800283
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700284 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800285 ctx.VisitDepsDepthFirstIf(isFileInstaller,
286 func(m blueprint.Module) {
287 fileInstaller := m.(fileInstaller)
288 files := fileInstaller.filesToInstall()
289 result = append(result, files...)
290 })
291
292 return result
293}
294
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700295func (a *AndroidModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800296 return a.installFiles
297}
298
299func (p *AndroidModuleBase) NoAddressSanitizer() bool {
300 return p.noAddressSanitizer
301}
302
Dan Willemsen782a2d12015-12-21 14:55:28 -0800303func (p *AndroidModuleBase) InstallInData() bool {
304 return false
305}
306
Colin Cross3f40fa42015-01-30 17:27:36 -0800307func (a *AndroidModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
308 if a != ctx.FinalModule().(AndroidModule).base() {
309 return
310 }
311
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700312 allInstalledFiles := Paths{}
313 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800314 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Crossc9404352015-03-26 16:10:12 -0700315 a := module.(AndroidModule).base()
316 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
317 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800318 })
319
Colin Cross9454bfa2015-03-17 13:24:18 -0700320 deps := []string{}
321
Colin Cross3f40fa42015-01-30 17:27:36 -0800322 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700323 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800324 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700325 Rule: blueprint.Phony,
326 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700327 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800328 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700329 })
330 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700331 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700332 }
333
334 if len(allCheckbuildFiles) > 0 {
335 name := ctx.ModuleName() + "-checkbuild"
336 ctx.Build(pctx, blueprint.BuildParams{
337 Rule: blueprint.Phony,
338 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700339 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700340 Optional: true,
341 })
342 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700343 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700344 }
345
346 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800347 suffix := ""
348 if ctx.Config().(Config).EmbeddedInMake() {
349 suffix = "-soong"
350 }
351
Colin Cross9454bfa2015-03-17 13:24:18 -0700352 ctx.Build(pctx, blueprint.BuildParams{
353 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800354 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700355 Implicits: deps,
356 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800357 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700358
359 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800360 }
361}
362
Colin Cross6362e272015-10-29 15:25:03 -0700363func (a *AndroidModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
364 return androidBaseContextImpl{
Dan Willemsen782a2d12015-12-21 14:55:28 -0800365 arch: a.commonProperties.CompileArch,
366 hod: a.commonProperties.CompileHostOrDevice,
367 ht: a.commonProperties.CompileHostType,
368 proprietary: a.commonProperties.Proprietary,
369 config: ctx.Config().(Config),
370 installInData: a.module.InstallInData(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800371 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800372}
373
374func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
375 androidCtx := &androidModuleContext{
Colin Cross6362e272015-10-29 15:25:03 -0700376 ModuleContext: ctx,
377 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
378 installDeps: a.computeInstallDeps(ctx),
379 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800380 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 }
382
Dan Willemsen0effe062015-11-30 16:06:01 -0800383 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800384 return
385 }
386
387 a.module.GenerateAndroidBuildActions(androidCtx)
388 if ctx.Failed() {
389 return
390 }
391
Colin Crossc9404352015-03-26 16:10:12 -0700392 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
393 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
394
Colin Cross3f40fa42015-01-30 17:27:36 -0800395 a.generateModuleTarget(ctx)
396 if ctx.Failed() {
397 return
398 }
399}
400
Colin Crossf6566ed2015-03-24 11:13:38 -0700401type androidBaseContextImpl struct {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800402 arch Arch
403 hod HostOrDevice
404 ht HostType
405 debug bool
406 config Config
407 proprietary bool
408 installInData bool
Colin Crossf6566ed2015-03-24 11:13:38 -0700409}
410
Colin Cross3f40fa42015-01-30 17:27:36 -0800411type androidModuleContext struct {
412 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700413 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700414 installDeps Paths
415 installFiles Paths
416 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800417 missingDeps []string
418}
419
420func (a *androidModuleContext) ninjaError(outputs []string, err error) {
421 a.ModuleContext.Build(pctx, blueprint.BuildParams{
422 Rule: ErrorRule,
423 Outputs: outputs,
424 Optional: true,
425 Args: map[string]string{
426 "error": err.Error(),
427 },
428 })
429 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800430}
431
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800432func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Cross6ff51382015-12-17 16:39:19 -0800433 if a.missingDeps != nil {
434 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
435 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
436 return
437 }
438
Colin Cross3f40fa42015-01-30 17:27:36 -0800439 params.Optional = true
440 a.ModuleContext.Build(pctx, params)
441}
442
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700443func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
444 bparams := blueprint.BuildParams{
445 Rule: params.Rule,
446 Outputs: params.Outputs.Strings(),
447 Inputs: params.Inputs.Strings(),
448 Implicits: params.Implicits.Strings(),
449 OrderOnly: params.OrderOnly.Strings(),
450 Args: params.Args,
451 Optional: !params.Default,
452 }
453
454 if params.Output != nil {
455 bparams.Outputs = append(bparams.Outputs, params.Output.String())
456 }
457 if params.Input != nil {
458 bparams.Inputs = append(bparams.Inputs, params.Input.String())
459 }
460 if params.Implicit != nil {
461 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
462 }
463
Colin Cross6ff51382015-12-17 16:39:19 -0800464 if a.missingDeps != nil {
465 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
466 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
467 return
468 }
469
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700470 a.ModuleContext.Build(pctx, bparams)
471}
472
Colin Cross6ff51382015-12-17 16:39:19 -0800473func (a *androidModuleContext) GetMissingDependencies() []string {
474 return a.missingDeps
475}
476
Colin Crossf6566ed2015-03-24 11:13:38 -0700477func (a *androidBaseContextImpl) Arch() Arch {
Colin Cross3f40fa42015-01-30 17:27:36 -0800478 return a.arch
479}
480
Colin Crossd3ba0392015-05-07 14:11:29 -0700481func (a *androidBaseContextImpl) HostOrDevice() HostOrDevice {
482 return a.hod
483}
484
Dan Willemsen490fd492015-11-24 17:53:15 -0800485func (a *androidBaseContextImpl) HostType() HostType {
486 return a.ht
487}
488
Colin Crossf6566ed2015-03-24 11:13:38 -0700489func (a *androidBaseContextImpl) Host() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700490 return a.hod.Host()
Colin Crossf6566ed2015-03-24 11:13:38 -0700491}
492
493func (a *androidBaseContextImpl) Device() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700494 return a.hod.Device()
Colin Crossf6566ed2015-03-24 11:13:38 -0700495}
496
Colin Cross0af4b842015-04-30 16:36:18 -0700497func (a *androidBaseContextImpl) Darwin() bool {
Dan Willemsen490fd492015-11-24 17:53:15 -0800498 return a.hod.Host() && a.ht == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700499}
500
Colin Crossf6566ed2015-03-24 11:13:38 -0700501func (a *androidBaseContextImpl) Debug() bool {
502 return a.debug
503}
504
Colin Cross1332b002015-04-07 17:11:30 -0700505func (a *androidBaseContextImpl) AConfig() Config {
506 return a.config
507}
508
Dan Willemsen782a2d12015-12-21 14:55:28 -0800509func (a *androidBaseContextImpl) Proprietary() bool {
510 return a.proprietary
511}
512
513func (a *androidBaseContextImpl) InstallInData() bool {
514 return a.installInData
515}
516
517func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700518 deps ...Path) Path {
Colin Cross35cec122015-04-02 14:37:16 -0700519
Dan Willemsen782a2d12015-12-21 14:55:28 -0800520 fullInstallPath := installPath.Join(a, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800521
Dan Willemsen322acaf2016-01-12 23:07:05 -0800522 if !a.AConfig().SkipInstall() {
523 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700524
Dan Willemsen322acaf2016-01-12 23:07:05 -0800525 a.ModuleBuild(pctx, ModuleBuildParams{
526 Rule: Cp,
527 Output: fullInstallPath,
528 Input: srcPath,
529 OrderOnly: Paths(deps),
530 Default: true,
531 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800532
Dan Willemsen322acaf2016-01-12 23:07:05 -0800533 a.installFiles = append(a.installFiles, fullInstallPath)
534 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700535 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700536 return fullInstallPath
537}
538
Dan Willemsen782a2d12015-12-21 14:55:28 -0800539func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) Path {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700540 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800541}
542
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700543func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800544 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
545}
546
Colin Cross3f40fa42015-01-30 17:27:36 -0800547type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700548 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800549}
550
551func isFileInstaller(m blueprint.Module) bool {
552 _, ok := m.(fileInstaller)
553 return ok
554}
555
556func isAndroidModule(m blueprint.Module) bool {
557 _, ok := m.(AndroidModule)
558 return ok
559}
Colin Crossfce53272015-04-08 11:21:40 -0700560
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700561func findStringInSlice(str string, slice []string) int {
562 for i, s := range slice {
563 if s == str {
564 return i
Colin Crossfce53272015-04-08 11:21:40 -0700565 }
566 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700567 return -1
568}
569
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700570func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
571 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700572 for i, e := range excludes {
573 j := findStringInSlice(e, srcFiles)
574 if j != -1 {
575 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
576 }
577
578 excludes[i] = filepath.Join(prefix, e)
579 }
580
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700581 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700582 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700583 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700584 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700585 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700586 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700587 }
588 }
589
590 return globbedSrcFiles
591}
592
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700593func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
594 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700595 if err != nil {
596 ctx.ModuleErrorf("glob: %s", err.Error())
597 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700598 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700599}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700600
Colin Cross463a90e2015-06-17 14:20:06 -0700601func init() {
602 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
603}
604
Colin Cross1f8c52b2015-06-16 16:38:17 -0700605func BuildTargetSingleton() blueprint.Singleton {
606 return &buildTargetSingleton{}
607}
608
609type buildTargetSingleton struct{}
610
611func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
612 checkbuildDeps := []string{}
613
614 dirModules := make(map[string][]string)
615
616 ctx.VisitAllModules(func(module blueprint.Module) {
617 if a, ok := module.(AndroidModule); ok {
618 blueprintDir := a.base().blueprintDir
619 installTarget := a.base().installTarget
620 checkbuildTarget := a.base().checkbuildTarget
621
622 if checkbuildTarget != "" {
623 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
624 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
625 }
626
627 if installTarget != "" {
628 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
629 }
630 }
631 })
632
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800633 suffix := ""
634 if ctx.Config().(Config).EmbeddedInMake() {
635 suffix = "-soong"
636 }
637
Colin Cross1f8c52b2015-06-16 16:38:17 -0700638 // Create a top-level checkbuild target that depends on all modules
639 ctx.Build(pctx, blueprint.BuildParams{
640 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800641 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700642 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700643 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700644 })
645
646 // Create a mm/<directory> target that depends on all modules in a directory
647 dirs := sortedKeys(dirModules)
648 for _, dir := range dirs {
649 ctx.Build(pctx, blueprint.BuildParams{
650 Rule: blueprint.Phony,
651 Outputs: []string{filepath.Join("mm", dir)},
652 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800653 // HACK: checkbuild should be an optional build, but force it
654 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800655 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700656 })
657 }
658}
Colin Crossd779da42015-12-17 18:00:23 -0800659
660type AndroidModulesByName struct {
661 slice []AndroidModule
662 ctx interface {
663 ModuleName(blueprint.Module) string
664 ModuleSubDir(blueprint.Module) string
665 }
666}
667
668func (s AndroidModulesByName) Len() int { return len(s.slice) }
669func (s AndroidModulesByName) Less(i, j int) bool {
670 mi, mj := s.slice[i], s.slice[j]
671 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
672
673 if ni != nj {
674 return ni < nj
675 } else {
676 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
677 }
678}
679func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }