blob: a3de5f6bd8a637fd10f015dd37ef1aae31085a3b [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
Colin Crossf6566ed2015-03-24 11:13:38 -070059}
60
61type AndroidBaseContext interface {
62 blueprint.BaseModuleContext
63 androidBaseContext
64}
65
Colin Cross3f40fa42015-01-30 17:27:36 -080066type AndroidModuleContext interface {
67 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070068 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080069
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070 // Similar to Build, but takes Paths instead of []string,
71 // and performs more verification.
72 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070073
Dan Willemsen34cc69e2015-09-23 15:26:20 -070074 ExpandSources(srcFiles, excludes []string) Paths
75 Glob(outDir, globPattern string, excludes []string) Paths
76
77 InstallFile(installPath string, srcPath Path, deps ...Path) Path
78 InstallFileName(installPath, name string, srcPath Path, deps ...Path) Path
79 CheckbuildFile(srcPath Path)
Colin Cross3f40fa42015-01-30 17:27:36 -080080}
81
82type AndroidModule interface {
83 blueprint.Module
84
85 GenerateAndroidBuildActions(AndroidModuleContext)
86
87 base() *AndroidModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -080088 Enabled() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080089 HostOrDevice() HostOrDevice
90}
91
Colin Cross3f40fa42015-01-30 17:27:36 -080092type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -070093 Name string
94 Deps []string
95 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -080096
Dan Willemsen0effe062015-11-30 16:06:01 -080097 // emit build rules for this module
98 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -080099
Colin Cross7d5136f2015-05-11 13:39:40 -0700100 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800101 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
102 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
103 // platform
104 Compile_multilib string
105
Colin Crossd3ba0392015-05-07 14:11:29 -0700106 // Set by HostOrDeviceMutator
107 CompileHostOrDevice HostOrDevice `blueprint:"mutated"`
108
Dan Willemsen490fd492015-11-24 17:53:15 -0800109 // Set by HostTypeMutator
110 CompileHostType HostType `blueprint:"mutated"`
111
Colin Cross3f40fa42015-01-30 17:27:36 -0800112 // Set by ArchMutator
113 CompileArch Arch `blueprint:"mutated"`
114
115 // Set by InitAndroidModule
116 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
117}
118
119type hostAndDeviceProperties struct {
120 Host_supported bool
121 Device_supported bool
122}
123
Colin Crossc472d572015-03-17 15:06:21 -0700124type Multilib string
125
126const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700127 MultilibBoth Multilib = "both"
128 MultilibFirst Multilib = "first"
129 MultilibCommon Multilib = "common"
130 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700131)
132
Colin Cross5049f022015-03-18 13:28:46 -0700133func InitAndroidModule(m AndroidModule,
Colin Cross3f40fa42015-01-30 17:27:36 -0800134 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
135
136 base := m.base()
137 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700138
Colin Cross7f64b6d2015-07-09 13:57:48 -0700139 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700140
141 return m, propertyStructs
142}
143
144func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib,
145 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
146
147 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
148
149 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800150 base.commonProperties.HostOrDeviceSupported = hod
Colin Crosscfad1192015-11-02 16:43:11 -0800151 base.commonProperties.Compile_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800152
Dan Willemsen218f6562015-07-08 18:13:11 -0700153 switch hod {
154 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800155 // Default to module to device supported, host not supported, can override in module
156 // properties
157 base.hostAndDeviceProperties.Device_supported = true
Dan Willemsen218f6562015-07-08 18:13:11 -0700158 fallthrough
159 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800160 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
161 }
162
Colin Crosscfad1192015-11-02 16:43:11 -0800163 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800164}
165
166// A AndroidModuleBase object contains the properties that are common to all Android
167// modules. It should be included as an anonymous field in every module
168// struct definition. InitAndroidModule should then be called from the module's
169// factory function, and the return values from InitAndroidModule should be
170// returned from the factory function.
171//
172// The AndroidModuleBase type is responsible for implementing the
173// GenerateBuildActions method to support the blueprint.Module interface. This
174// method will then call the module's GenerateAndroidBuildActions method once
175// for each build variant that is to be built. GenerateAndroidBuildActions is
176// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
177// AndroidModuleContext exposes extra functionality specific to the Android build
178// system including details about the particular build variant that is to be
179// generated.
180//
181// For example:
182//
183// import (
184// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700185// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800186// )
187//
188// type myModule struct {
189// common.AndroidModuleBase
190// properties struct {
191// MyProperty string
192// }
193// }
194//
195// func NewMyModule() (blueprint.Module, []interface{}) {
196// m := &myModule{}
197// return common.InitAndroidModule(m, &m.properties)
198// }
199//
200// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
201// // Get the CPU architecture for the current build variant.
202// variantArch := ctx.Arch()
203//
204// // ...
205// }
206type AndroidModuleBase struct {
207 // Putting the curiously recurring thing pointing to the thing that contains
208 // the thing pattern to good use.
209 module AndroidModule
210
211 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700212 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800213 hostAndDeviceProperties hostAndDeviceProperties
214 generalProperties []interface{}
215 archProperties []*archProperties
216
217 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700218 installFiles Paths
219 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700220
221 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
222 // Only set on the final variant of each module
223 installTarget string
224 checkbuildTarget string
225 blueprintDir string
Colin Cross3f40fa42015-01-30 17:27:36 -0800226}
227
228func (a *AndroidModuleBase) base() *AndroidModuleBase {
229 return a
230}
231
Colin Crossd3ba0392015-05-07 14:11:29 -0700232func (a *AndroidModuleBase) SetHostOrDevice(hod HostOrDevice) {
233 a.commonProperties.CompileHostOrDevice = hod
234}
235
Dan Willemsen490fd492015-11-24 17:53:15 -0800236func (a *AndroidModuleBase) SetHostType(ht HostType) {
237 a.commonProperties.CompileHostType = ht
238}
239
Colin Cross3f40fa42015-01-30 17:27:36 -0800240func (a *AndroidModuleBase) SetArch(arch Arch) {
241 a.commonProperties.CompileArch = arch
242}
243
244func (a *AndroidModuleBase) HostOrDevice() HostOrDevice {
Colin Crossd3ba0392015-05-07 14:11:29 -0700245 return a.commonProperties.CompileHostOrDevice
Colin Cross3f40fa42015-01-30 17:27:36 -0800246}
247
Dan Willemsen490fd492015-11-24 17:53:15 -0800248func (a *AndroidModuleBase) HostType() HostType {
249 return a.commonProperties.CompileHostType
250}
251
Colin Cross3f40fa42015-01-30 17:27:36 -0800252func (a *AndroidModuleBase) HostSupported() bool {
253 return a.commonProperties.HostOrDeviceSupported == HostSupported ||
254 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
255 a.hostAndDeviceProperties.Host_supported
256}
257
258func (a *AndroidModuleBase) DeviceSupported() bool {
259 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
260 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
261 a.hostAndDeviceProperties.Device_supported
262}
263
Dan Willemsen0effe062015-11-30 16:06:01 -0800264func (a *AndroidModuleBase) Enabled() bool {
265 if a.commonProperties.Enabled == nil {
266 if a.HostSupported() && a.HostOrDevice().Host() && a.HostType() == Windows {
267 return false
268 } else {
269 return true
270 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800271 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800272 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800273}
274
275func (a *AndroidModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700276 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800277
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700278 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800279 ctx.VisitDepsDepthFirstIf(isFileInstaller,
280 func(m blueprint.Module) {
281 fileInstaller := m.(fileInstaller)
282 files := fileInstaller.filesToInstall()
283 result = append(result, files...)
284 })
285
286 return result
287}
288
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700289func (a *AndroidModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800290 return a.installFiles
291}
292
293func (p *AndroidModuleBase) NoAddressSanitizer() bool {
294 return p.noAddressSanitizer
295}
296
Colin Cross3f40fa42015-01-30 17:27:36 -0800297func (a *AndroidModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
298 if a != ctx.FinalModule().(AndroidModule).base() {
299 return
300 }
301
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700302 allInstalledFiles := Paths{}
303 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800304 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Crossc9404352015-03-26 16:10:12 -0700305 a := module.(AndroidModule).base()
306 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
307 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800308 })
309
Colin Cross9454bfa2015-03-17 13:24:18 -0700310 deps := []string{}
311
Colin Cross3f40fa42015-01-30 17:27:36 -0800312 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700313 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800314 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700315 Rule: blueprint.Phony,
316 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700317 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800318 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700319 })
320 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700321 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700322 }
323
324 if len(allCheckbuildFiles) > 0 {
325 name := ctx.ModuleName() + "-checkbuild"
326 ctx.Build(pctx, blueprint.BuildParams{
327 Rule: blueprint.Phony,
328 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700329 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700330 Optional: true,
331 })
332 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700333 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700334 }
335
336 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800337 suffix := ""
338 if ctx.Config().(Config).EmbeddedInMake() {
339 suffix = "-soong"
340 }
341
Colin Cross9454bfa2015-03-17 13:24:18 -0700342 ctx.Build(pctx, blueprint.BuildParams{
343 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800344 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700345 Implicits: deps,
346 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800347 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700348
349 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800350 }
351}
352
Colin Cross6362e272015-10-29 15:25:03 -0700353func (a *AndroidModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
354 return androidBaseContextImpl{
355 arch: a.commonProperties.CompileArch,
356 hod: a.commonProperties.CompileHostOrDevice,
Dan Willemsen490fd492015-11-24 17:53:15 -0800357 ht: a.commonProperties.CompileHostType,
Colin Cross6362e272015-10-29 15:25:03 -0700358 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800359 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800360}
361
362func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
363 androidCtx := &androidModuleContext{
Colin Cross6362e272015-10-29 15:25:03 -0700364 ModuleContext: ctx,
365 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
366 installDeps: a.computeInstallDeps(ctx),
367 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800368 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800369 }
370
Dan Willemsen0effe062015-11-30 16:06:01 -0800371 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800372 return
373 }
374
375 a.module.GenerateAndroidBuildActions(androidCtx)
376 if ctx.Failed() {
377 return
378 }
379
Colin Crossc9404352015-03-26 16:10:12 -0700380 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
381 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
382
Colin Cross3f40fa42015-01-30 17:27:36 -0800383 a.generateModuleTarget(ctx)
384 if ctx.Failed() {
385 return
386 }
387}
388
Colin Crossf6566ed2015-03-24 11:13:38 -0700389type androidBaseContextImpl struct {
Colin Cross1332b002015-04-07 17:11:30 -0700390 arch Arch
Colin Crossd3ba0392015-05-07 14:11:29 -0700391 hod HostOrDevice
Dan Willemsen490fd492015-11-24 17:53:15 -0800392 ht HostType
Colin Cross1332b002015-04-07 17:11:30 -0700393 debug bool
394 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700395}
396
Colin Cross3f40fa42015-01-30 17:27:36 -0800397type androidModuleContext struct {
398 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700399 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700400 installDeps Paths
401 installFiles Paths
402 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800403 missingDeps []string
404}
405
406func (a *androidModuleContext) ninjaError(outputs []string, err error) {
407 a.ModuleContext.Build(pctx, blueprint.BuildParams{
408 Rule: ErrorRule,
409 Outputs: outputs,
410 Optional: true,
411 Args: map[string]string{
412 "error": err.Error(),
413 },
414 })
415 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800416}
417
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800418func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Cross6ff51382015-12-17 16:39:19 -0800419 if a.missingDeps != nil {
420 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
421 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
422 return
423 }
424
Colin Cross3f40fa42015-01-30 17:27:36 -0800425 params.Optional = true
426 a.ModuleContext.Build(pctx, params)
427}
428
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700429func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
430 bparams := blueprint.BuildParams{
431 Rule: params.Rule,
432 Outputs: params.Outputs.Strings(),
433 Inputs: params.Inputs.Strings(),
434 Implicits: params.Implicits.Strings(),
435 OrderOnly: params.OrderOnly.Strings(),
436 Args: params.Args,
437 Optional: !params.Default,
438 }
439
440 if params.Output != nil {
441 bparams.Outputs = append(bparams.Outputs, params.Output.String())
442 }
443 if params.Input != nil {
444 bparams.Inputs = append(bparams.Inputs, params.Input.String())
445 }
446 if params.Implicit != nil {
447 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
448 }
449
Colin Cross6ff51382015-12-17 16:39:19 -0800450 if a.missingDeps != nil {
451 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
452 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
453 return
454 }
455
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700456 a.ModuleContext.Build(pctx, bparams)
457}
458
Colin Cross6ff51382015-12-17 16:39:19 -0800459func (a *androidModuleContext) GetMissingDependencies() []string {
460 return a.missingDeps
461}
462
Colin Crossf6566ed2015-03-24 11:13:38 -0700463func (a *androidBaseContextImpl) Arch() Arch {
Colin Cross3f40fa42015-01-30 17:27:36 -0800464 return a.arch
465}
466
Colin Crossd3ba0392015-05-07 14:11:29 -0700467func (a *androidBaseContextImpl) HostOrDevice() HostOrDevice {
468 return a.hod
469}
470
Dan Willemsen490fd492015-11-24 17:53:15 -0800471func (a *androidBaseContextImpl) HostType() HostType {
472 return a.ht
473}
474
Colin Crossf6566ed2015-03-24 11:13:38 -0700475func (a *androidBaseContextImpl) Host() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700476 return a.hod.Host()
Colin Crossf6566ed2015-03-24 11:13:38 -0700477}
478
479func (a *androidBaseContextImpl) Device() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700480 return a.hod.Device()
Colin Crossf6566ed2015-03-24 11:13:38 -0700481}
482
Colin Cross0af4b842015-04-30 16:36:18 -0700483func (a *androidBaseContextImpl) Darwin() bool {
Dan Willemsen490fd492015-11-24 17:53:15 -0800484 return a.hod.Host() && a.ht == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700485}
486
Colin Crossf6566ed2015-03-24 11:13:38 -0700487func (a *androidBaseContextImpl) Debug() bool {
488 return a.debug
489}
490
Colin Cross1332b002015-04-07 17:11:30 -0700491func (a *androidBaseContextImpl) AConfig() Config {
492 return a.config
493}
494
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700495func (a *androidModuleContext) InstallFileName(installPath, name string, srcPath Path,
496 deps ...Path) Path {
Colin Cross35cec122015-04-02 14:37:16 -0700497
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700498 fullInstallPath := PathForModuleInstall(a, installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800499
Colin Cross35cec122015-04-02 14:37:16 -0700500 deps = append(deps, a.installDeps...)
501
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700502 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross3f40fa42015-01-30 17:27:36 -0800503 Rule: Cp,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700504 Output: fullInstallPath,
505 Input: srcPath,
506 OrderOnly: Paths(deps),
Colin Cross346aa132015-12-17 17:19:51 -0800507 Default: !a.AConfig().EmbeddedInMake(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800508 })
509
510 a.installFiles = append(a.installFiles, fullInstallPath)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700511 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700512 return fullInstallPath
513}
514
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700515func (a *androidModuleContext) InstallFile(installPath string, srcPath Path, deps ...Path) Path {
516 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800517}
518
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700519func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800520 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
521}
522
Colin Cross3f40fa42015-01-30 17:27:36 -0800523type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700524 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800525}
526
527func isFileInstaller(m blueprint.Module) bool {
528 _, ok := m.(fileInstaller)
529 return ok
530}
531
532func isAndroidModule(m blueprint.Module) bool {
533 _, ok := m.(AndroidModule)
534 return ok
535}
Colin Crossfce53272015-04-08 11:21:40 -0700536
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700537func findStringInSlice(str string, slice []string) int {
538 for i, s := range slice {
539 if s == str {
540 return i
Colin Crossfce53272015-04-08 11:21:40 -0700541 }
542 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700543 return -1
544}
545
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700546func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
547 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700548 for i, e := range excludes {
549 j := findStringInSlice(e, srcFiles)
550 if j != -1 {
551 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
552 }
553
554 excludes[i] = filepath.Join(prefix, e)
555 }
556
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700557 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700558 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700559 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700560 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700561 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700562 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700563 }
564 }
565
566 return globbedSrcFiles
567}
568
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700569func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
570 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700571 if err != nil {
572 ctx.ModuleErrorf("glob: %s", err.Error())
573 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700574 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700575}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700576
Colin Cross463a90e2015-06-17 14:20:06 -0700577func init() {
578 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
579}
580
Colin Cross1f8c52b2015-06-16 16:38:17 -0700581func BuildTargetSingleton() blueprint.Singleton {
582 return &buildTargetSingleton{}
583}
584
585type buildTargetSingleton struct{}
586
587func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
588 checkbuildDeps := []string{}
589
590 dirModules := make(map[string][]string)
591
592 ctx.VisitAllModules(func(module blueprint.Module) {
593 if a, ok := module.(AndroidModule); ok {
594 blueprintDir := a.base().blueprintDir
595 installTarget := a.base().installTarget
596 checkbuildTarget := a.base().checkbuildTarget
597
598 if checkbuildTarget != "" {
599 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
600 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
601 }
602
603 if installTarget != "" {
604 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
605 }
606 }
607 })
608
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800609 suffix := ""
610 if ctx.Config().(Config).EmbeddedInMake() {
611 suffix = "-soong"
612 }
613
Colin Cross1f8c52b2015-06-16 16:38:17 -0700614 // Create a top-level checkbuild target that depends on all modules
615 ctx.Build(pctx, blueprint.BuildParams{
616 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800617 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700618 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700619 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700620 })
621
622 // Create a mm/<directory> target that depends on all modules in a directory
623 dirs := sortedKeys(dirModules)
624 for _, dir := range dirs {
625 ctx.Build(pctx, blueprint.BuildParams{
626 Rule: blueprint.Phony,
627 Outputs: []string{filepath.Join("mm", dir)},
628 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800629 // HACK: checkbuild should be an optional build, but force it
630 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800631 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700632 })
633 }
634}