blob: c008d7de7090ac7427a3430a67c467250196f4e2 [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
Colin Cross35cec122015-04-02 14:37:16 -070028 DeviceOut() string
29 HostOut() string
Colin Cross68f55102015-03-25 14:43:57 -070030}
31
Colin Cross3f40fa42015-01-30 17:27:36 -080032var (
33 DeviceSharedLibrary = "shared_library"
34 DeviceStaticLibrary = "static_library"
35 DeviceExecutable = "executable"
36 HostSharedLibrary = "host_shared_library"
37 HostStaticLibrary = "host_static_library"
38 HostExecutable = "host_executable"
39)
40
Colin Crossf6566ed2015-03-24 11:13:38 -070041type androidBaseContext interface {
42 Arch() Arch
43 Host() bool
44 Device() bool
45 Debug() bool
46}
47
48type AndroidBaseContext interface {
49 blueprint.BaseModuleContext
50 androidBaseContext
51}
52
Colin Cross3f40fa42015-01-30 17:27:36 -080053type AndroidModuleContext interface {
54 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070055 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080056
Colin Cross35cec122015-04-02 14:37:16 -070057 InstallFile(installPath, srcPath string, deps ...string) string
58 InstallFileName(installPath, name, srcPath string, deps ...string) string
Colin Cross3f40fa42015-01-30 17:27:36 -080059 CheckbuildFile(srcPath string)
60}
61
62type AndroidModule interface {
63 blueprint.Module
64
65 GenerateAndroidBuildActions(AndroidModuleContext)
66
67 base() *AndroidModuleBase
68 Disabled() bool
69 HostOrDevice() HostOrDevice
70}
71
72type AndroidDynamicDepender interface {
73 AndroidDynamicDependencies(ctx AndroidDynamicDependerModuleContext) []string
74}
75
76type AndroidDynamicDependerModuleContext interface {
77 blueprint.DynamicDependerModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070078 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080079}
80
81type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -070082 Name string
83 Deps []string
84 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -080085
86 // disabled: don't emit any build rules for this module
87 Disabled bool `android:"arch_variant"`
88
89 // multilib: control whether this module compiles for 32-bit, 64-bit, or both. Possible values
90 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
91 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
92 // platform
93 Compile_multilib string
94
95 // Set by ArchMutator
96 CompileArch Arch `blueprint:"mutated"`
97
98 // Set by InitAndroidModule
99 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
100}
101
102type hostAndDeviceProperties struct {
103 Host_supported bool
104 Device_supported bool
105}
106
Colin Crossc472d572015-03-17 15:06:21 -0700107type Multilib string
108
109const (
Colin Cross2fe66872015-03-30 17:20:39 -0700110 MultilibBoth Multilib = "both"
111 MultilibFirst Multilib = "first"
112 MultilibCommon Multilib = "common"
Colin Crossc472d572015-03-17 15:06:21 -0700113)
114
Colin Cross5049f022015-03-18 13:28:46 -0700115func InitAndroidModule(m AndroidModule,
Colin Cross3f40fa42015-01-30 17:27:36 -0800116 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
117
118 base := m.base()
119 base.module = m
Colin Cross28d76592015-03-26 16:14:04 -0700120 base.extendedProperties = make(map[string]struct{})
Colin Cross5049f022015-03-18 13:28:46 -0700121
122 propertyStructs = append(propertyStructs, &base.commonProperties)
123
124 return m, propertyStructs
125}
126
127func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMultilib Multilib,
128 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
129
130 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
131
132 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800133 base.commonProperties.HostOrDeviceSupported = hod
134
135 if hod == HostAndDeviceSupported {
136 // Default to module to device supported, host not supported, can override in module
137 // properties
138 base.hostAndDeviceProperties.Device_supported = true
139 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
140 }
141
142 return InitArchModule(m, defaultMultilib, propertyStructs...)
143}
144
145// A AndroidModuleBase object contains the properties that are common to all Android
146// modules. It should be included as an anonymous field in every module
147// struct definition. InitAndroidModule should then be called from the module's
148// factory function, and the return values from InitAndroidModule should be
149// returned from the factory function.
150//
151// The AndroidModuleBase type is responsible for implementing the
152// GenerateBuildActions method to support the blueprint.Module interface. This
153// method will then call the module's GenerateAndroidBuildActions method once
154// for each build variant that is to be built. GenerateAndroidBuildActions is
155// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
156// AndroidModuleContext exposes extra functionality specific to the Android build
157// system including details about the particular build variant that is to be
158// generated.
159//
160// For example:
161//
162// import (
163// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700164// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800165// )
166//
167// type myModule struct {
168// common.AndroidModuleBase
169// properties struct {
170// MyProperty string
171// }
172// }
173//
174// func NewMyModule() (blueprint.Module, []interface{}) {
175// m := &myModule{}
176// return common.InitAndroidModule(m, &m.properties)
177// }
178//
179// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
180// // Get the CPU architecture for the current build variant.
181// variantArch := ctx.Arch()
182//
183// // ...
184// }
185type AndroidModuleBase struct {
186 // Putting the curiously recurring thing pointing to the thing that contains
187 // the thing pattern to good use.
188 module AndroidModule
189
190 commonProperties commonProperties
191 hostAndDeviceProperties hostAndDeviceProperties
192 generalProperties []interface{}
193 archProperties []*archProperties
Colin Cross28d76592015-03-26 16:14:04 -0700194 extendedProperties map[string]struct{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800195
196 noAddressSanitizer bool
197 installFiles []string
198 checkbuildFiles []string
199}
200
201func (a *AndroidModuleBase) base() *AndroidModuleBase {
202 return a
203}
204
205func (a *AndroidModuleBase) SetArch(arch Arch) {
206 a.commonProperties.CompileArch = arch
207}
208
209func (a *AndroidModuleBase) HostOrDevice() HostOrDevice {
210 return a.commonProperties.CompileArch.HostOrDevice
211}
212
213func (a *AndroidModuleBase) HostSupported() bool {
214 return a.commonProperties.HostOrDeviceSupported == HostSupported ||
215 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
216 a.hostAndDeviceProperties.Host_supported
217}
218
219func (a *AndroidModuleBase) DeviceSupported() bool {
220 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
221 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
222 a.hostAndDeviceProperties.Device_supported
223}
224
225func (a *AndroidModuleBase) Disabled() bool {
226 return a.commonProperties.Disabled
227}
228
229func (a *AndroidModuleBase) computeInstallDeps(
230 ctx blueprint.ModuleContext) []string {
231
232 result := []string{}
233 ctx.VisitDepsDepthFirstIf(isFileInstaller,
234 func(m blueprint.Module) {
235 fileInstaller := m.(fileInstaller)
236 files := fileInstaller.filesToInstall()
237 result = append(result, files...)
238 })
239
240 return result
241}
242
243func (a *AndroidModuleBase) filesToInstall() []string {
244 return a.installFiles
245}
246
247func (p *AndroidModuleBase) NoAddressSanitizer() bool {
248 return p.noAddressSanitizer
249}
250
Colin Cross3f40fa42015-01-30 17:27:36 -0800251func (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 Cross35cec122015-04-02 14:37:16 -0700384func (a *androidModuleContext) InstallFileName(installPath, name, srcPath string,
385 deps ...string) string {
386
387 config := a.Config().(Config)
Colin Cross3f40fa42015-01-30 17:27:36 -0800388 var fullInstallPath string
389 if a.arch.HostOrDevice.Device() {
390 // TODO: replace unset with a device name once we have device targeting
Colin Cross35cec122015-04-02 14:37:16 -0700391 fullInstallPath = filepath.Join(config.DeviceOut(), "system",
392 installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 } else {
Colin Cross35cec122015-04-02 14:37:16 -0700394 fullInstallPath = filepath.Join(config.HostOut(), installPath, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800395 }
396
Colin Cross35cec122015-04-02 14:37:16 -0700397 deps = append(deps, a.installDeps...)
398
Colin Cross3f40fa42015-01-30 17:27:36 -0800399 a.ModuleContext.Build(pctx, blueprint.BuildParams{
400 Rule: Cp,
401 Outputs: []string{fullInstallPath},
402 Inputs: []string{srcPath},
Colin Cross35cec122015-04-02 14:37:16 -0700403 OrderOnly: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800404 })
405
406 a.installFiles = append(a.installFiles, fullInstallPath)
Colin Cross35cec122015-04-02 14:37:16 -0700407 return fullInstallPath
408}
409
410func (a *androidModuleContext) InstallFile(installPath, srcPath string, deps ...string) string {
411 return a.InstallFileName(installPath, filepath.Base(srcPath), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800412}
413
414func (a *androidModuleContext) CheckbuildFile(srcPath string) {
415 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
416}
417
418type androidDynamicDependerContext struct {
419 blueprint.DynamicDependerModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700420 androidBaseContextImpl
Colin Cross3f40fa42015-01-30 17:27:36 -0800421}
422
423type fileInstaller interface {
424 filesToInstall() []string
425}
426
427func isFileInstaller(m blueprint.Module) bool {
428 _, ok := m.(fileInstaller)
429 return ok
430}
431
432func isAndroidModule(m blueprint.Module) bool {
433 _, ok := m.(AndroidModule)
434 return ok
435}