blob: 73a9a61a8941ac6e4376c55a2d49909e804cdf88 [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
20 "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -080021)
22
Colin Cross68f55102015-03-25 14:43:57 -070023type Config interface {
24 CpPreserveSymlinksFlags() string
25 SrcDir() string
26 Getenv(string) string
27 EnvDeps() map[string]string
28}
29
Colin Cross3f40fa42015-01-30 17:27:36 -080030var (
31 DeviceSharedLibrary = "shared_library"
32 DeviceStaticLibrary = "static_library"
33 DeviceExecutable = "executable"
34 HostSharedLibrary = "host_shared_library"
35 HostStaticLibrary = "host_static_library"
36 HostExecutable = "host_executable"
37)
38
Colin Crossf6566ed2015-03-24 11:13:38 -070039type androidBaseContext interface {
40 Arch() Arch
41 Host() bool
42 Device() bool
43 Debug() bool
44}
45
46type AndroidBaseContext interface {
47 blueprint.BaseModuleContext
48 androidBaseContext
49}
50
Colin Cross3f40fa42015-01-30 17:27:36 -080051type AndroidModuleContext interface {
52 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070053 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080054
Colin Cross3f40fa42015-01-30 17:27:36 -080055 InstallFile(installPath, srcPath string)
56 CheckbuildFile(srcPath string)
57}
58
59type AndroidModule interface {
60 blueprint.Module
61
62 GenerateAndroidBuildActions(AndroidModuleContext)
63
64 base() *AndroidModuleBase
65 Disabled() bool
66 HostOrDevice() HostOrDevice
67}
68
69type AndroidDynamicDepender interface {
70 AndroidDynamicDependencies(ctx AndroidDynamicDependerModuleContext) []string
71}
72
73type AndroidDynamicDependerModuleContext interface {
74 blueprint.DynamicDependerModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070075 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080076}
77
78type commonProperties struct {
79 Name string
80 Deps []string
81 ResourceDirs []string
82
83 // disabled: don't emit any build rules for this module
84 Disabled bool `android:"arch_variant"`
85
86 // multilib: control whether this module compiles for 32-bit, 64-bit, or both. Possible values
87 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
88 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
89 // platform
90 Compile_multilib string
91
92 // Set by ArchMutator
93 CompileArch Arch `blueprint:"mutated"`
94
95 // Set by InitAndroidModule
96 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
97}
98
99type hostAndDeviceProperties struct {
100 Host_supported bool
101 Device_supported bool
102}
103
Colin Crossc472d572015-03-17 15:06:21 -0700104type Multilib string
105
106const (
107 MultilibBoth Multilib = "both"
108 MultilibFirst Multilib = "first"
109)
110
Colin Cross5049f022015-03-18 13:28:46 -0700111func InitAndroidModule(m AndroidModule,
Colin Cross3f40fa42015-01-30 17:27:36 -0800112 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
113
114 base := m.base()
115 base.module = m
Colin Cross28d76592015-03-26 16:14:04 -0700116 base.extendedProperties = make(map[string]struct{})
Colin Cross5049f022015-03-18 13:28:46 -0700117
118 propertyStructs = append(propertyStructs, &base.commonProperties)
119
120 return m, propertyStructs
121}
122
123func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib,
124 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
125
126 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
127
128 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800129 base.commonProperties.HostOrDeviceSupported = hod
130
131 if hod == HostAndDeviceSupported {
132 // Default to module to device supported, host not supported, can override in module
133 // properties
134 base.hostAndDeviceProperties.Device_supported = true
135 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
136 }
137
138 return InitArchModule(m, defaultMultilib, propertyStructs...)
139}
140
141// A AndroidModuleBase object contains the properties that are common to all Android
142// modules. It should be included as an anonymous field in every module
143// struct definition. InitAndroidModule should then be called from the module's
144// factory function, and the return values from InitAndroidModule should be
145// returned from the factory function.
146//
147// The AndroidModuleBase type is responsible for implementing the
148// GenerateBuildActions method to support the blueprint.Module interface. This
149// method will then call the module's GenerateAndroidBuildActions method once
150// for each build variant that is to be built. GenerateAndroidBuildActions is
151// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
152// AndroidModuleContext exposes extra functionality specific to the Android build
153// system including details about the particular build variant that is to be
154// generated.
155//
156// For example:
157//
158// import (
159// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700160// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800161// )
162//
163// type myModule struct {
164// common.AndroidModuleBase
165// properties struct {
166// MyProperty string
167// }
168// }
169//
170// func NewMyModule() (blueprint.Module, []interface{}) {
171// m := &myModule{}
172// return common.InitAndroidModule(m, &m.properties)
173// }
174//
175// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
176// // Get the CPU architecture for the current build variant.
177// variantArch := ctx.Arch()
178//
179// // ...
180// }
181type AndroidModuleBase struct {
182 // Putting the curiously recurring thing pointing to the thing that contains
183 // the thing pattern to good use.
184 module AndroidModule
185
186 commonProperties commonProperties
187 hostAndDeviceProperties hostAndDeviceProperties
188 generalProperties []interface{}
189 archProperties []*archProperties
Colin Cross28d76592015-03-26 16:14:04 -0700190 extendedProperties map[string]struct{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800191
192 noAddressSanitizer bool
193 installFiles []string
194 checkbuildFiles []string
195}
196
197func (a *AndroidModuleBase) base() *AndroidModuleBase {
198 return a
199}
200
201func (a *AndroidModuleBase) SetArch(arch Arch) {
202 a.commonProperties.CompileArch = arch
203}
204
205func (a *AndroidModuleBase) HostOrDevice() HostOrDevice {
206 return a.commonProperties.CompileArch.HostOrDevice
207}
208
209func (a *AndroidModuleBase) HostSupported() bool {
210 return a.commonProperties.HostOrDeviceSupported == HostSupported ||
211 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
212 a.hostAndDeviceProperties.Host_supported
213}
214
215func (a *AndroidModuleBase) DeviceSupported() bool {
216 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
217 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
218 a.hostAndDeviceProperties.Device_supported
219}
220
221func (a *AndroidModuleBase) Disabled() bool {
222 return a.commonProperties.Disabled
223}
224
225func (a *AndroidModuleBase) computeInstallDeps(
226 ctx blueprint.ModuleContext) []string {
227
228 result := []string{}
229 ctx.VisitDepsDepthFirstIf(isFileInstaller,
230 func(m blueprint.Module) {
231 fileInstaller := m.(fileInstaller)
232 files := fileInstaller.filesToInstall()
233 result = append(result, files...)
234 })
235
236 return result
237}
238
239func (a *AndroidModuleBase) filesToInstall() []string {
240 return a.installFiles
241}
242
243func (p *AndroidModuleBase) NoAddressSanitizer() bool {
244 return p.noAddressSanitizer
245}
246
247func (p *AndroidModuleBase) resourceDirs() []string {
248 return p.commonProperties.ResourceDirs
249}
250
251func (a *AndroidModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
252 if a != ctx.FinalModule().(AndroidModule).base() {
253 return
254 }
255
256 allInstalledFiles := []string{}
Colin Cross9454bfa2015-03-17 13:24:18 -0700257 allCheckbuildFiles := []string{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800258 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Crossc9404352015-03-26 16:10:12 -0700259 a := module.(AndroidModule).base()
260 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
261 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800262 })
263
Colin Cross9454bfa2015-03-17 13:24:18 -0700264 deps := []string{}
265
Colin Cross3f40fa42015-01-30 17:27:36 -0800266 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700267 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800268 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700269 Rule: blueprint.Phony,
270 Outputs: []string{name},
271 Implicits: allInstalledFiles,
272 })
273 deps = append(deps, name)
274 }
275
276 if len(allCheckbuildFiles) > 0 {
277 name := ctx.ModuleName() + "-checkbuild"
278 ctx.Build(pctx, blueprint.BuildParams{
279 Rule: blueprint.Phony,
280 Outputs: []string{name},
281 Implicits: allCheckbuildFiles,
282 Optional: true,
283 })
284 deps = append(deps, name)
285 }
286
287 if len(deps) > 0 {
288 ctx.Build(pctx, blueprint.BuildParams{
289 Rule: blueprint.Phony,
290 Outputs: []string{ctx.ModuleName()},
291 Implicits: deps,
292 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800293 })
294 }
295}
296
297func (a *AndroidModuleBase) DynamicDependencies(ctx blueprint.DynamicDependerModuleContext) []string {
298 actx := &androidDynamicDependerContext{
299 DynamicDependerModuleContext: ctx,
Colin Crossf6566ed2015-03-24 11:13:38 -0700300 androidBaseContextImpl: androidBaseContextImpl{
301 arch: a.commonProperties.CompileArch,
302 },
Colin Cross3f40fa42015-01-30 17:27:36 -0800303 }
304
305 if dynamic, ok := a.module.(AndroidDynamicDepender); ok {
306 return dynamic.AndroidDynamicDependencies(actx)
307 }
308
309 return nil
310}
311
312func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
313 androidCtx := &androidModuleContext{
314 ModuleContext: ctx,
Colin Crossf6566ed2015-03-24 11:13:38 -0700315 androidBaseContextImpl: androidBaseContextImpl{
316 arch: a.commonProperties.CompileArch,
317 },
Colin Cross28d76592015-03-26 16:14:04 -0700318 installDeps: a.computeInstallDeps(ctx),
319 installFiles: a.installFiles,
320 extendedProperties: a.extendedProperties,
Colin Cross3f40fa42015-01-30 17:27:36 -0800321 }
322
323 if a.commonProperties.Disabled {
324 return
325 }
326
327 a.module.GenerateAndroidBuildActions(androidCtx)
328 if ctx.Failed() {
329 return
330 }
331
Colin Crossc9404352015-03-26 16:10:12 -0700332 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
333 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
334
Colin Cross3f40fa42015-01-30 17:27:36 -0800335 a.generateModuleTarget(ctx)
336 if ctx.Failed() {
337 return
338 }
339}
340
Colin Crossf6566ed2015-03-24 11:13:38 -0700341type androidBaseContextImpl struct {
342 arch Arch
343 debug bool
344}
345
Colin Cross3f40fa42015-01-30 17:27:36 -0800346type androidModuleContext struct {
347 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700348 androidBaseContextImpl
Colin Cross28d76592015-03-26 16:14:04 -0700349 installDeps []string
350 installFiles []string
351 checkbuildFiles []string
352 extendedProperties map[string]struct{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800353}
354
355func (a *androidModuleContext) Build(pctx *blueprint.PackageContext, params blueprint.BuildParams) {
356 params.Optional = true
357 a.ModuleContext.Build(pctx, params)
358}
359
Colin Cross28d76592015-03-26 16:14:04 -0700360func (a *androidModuleContext) ContainsProperty(property string) bool {
361 if a.ModuleContext.ContainsProperty(property) {
362 return true
363 }
364 _, ok := a.extendedProperties[property]
365 return ok
366}
367
Colin Crossf6566ed2015-03-24 11:13:38 -0700368func (a *androidBaseContextImpl) Arch() Arch {
Colin Cross3f40fa42015-01-30 17:27:36 -0800369 return a.arch
370}
371
Colin Crossf6566ed2015-03-24 11:13:38 -0700372func (a *androidBaseContextImpl) Host() bool {
373 return a.arch.HostOrDevice.Host()
374}
375
376func (a *androidBaseContextImpl) Device() bool {
377 return a.arch.HostOrDevice.Device()
378}
379
380func (a *androidBaseContextImpl) Debug() bool {
381 return a.debug
382}
383
Colin Cross3f40fa42015-01-30 17:27:36 -0800384func (a *androidModuleContext) InstallFile(installPath, srcPath string) {
385 var fullInstallPath string
386 if a.arch.HostOrDevice.Device() {
387 // TODO: replace unset with a device name once we have device targeting
388 fullInstallPath = filepath.Join("out/target/product/unset/system", installPath,
389 filepath.Base(srcPath))
390 } else {
391 // TODO: replace unset with a host name
392 fullInstallPath = filepath.Join("out/host/unset/", installPath, filepath.Base(srcPath))
393 }
394
395 a.ModuleContext.Build(pctx, blueprint.BuildParams{
396 Rule: Cp,
397 Outputs: []string{fullInstallPath},
398 Inputs: []string{srcPath},
399 OrderOnly: a.installDeps,
400 })
401
402 a.installFiles = append(a.installFiles, fullInstallPath)
Colin Cross3f40fa42015-01-30 17:27:36 -0800403}
404
405func (a *androidModuleContext) CheckbuildFile(srcPath string) {
406 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
407}
408
409type androidDynamicDependerContext struct {
410 blueprint.DynamicDependerModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700411 androidBaseContextImpl
Colin Cross3f40fa42015-01-30 17:27:36 -0800412}
413
414type fileInstaller interface {
415 filesToInstall() []string
416}
417
418func isFileInstaller(m blueprint.Module) bool {
419 _, ok := m.(fileInstaller)
420 return ok
421}
422
423func isAndroidModule(m blueprint.Module) bool {
424 _, ok := m.(AndroidModule)
425 return ok
426}