blob: 06f1fca7c5465e8a853d6195d43d41a43c9c920f [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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
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
Colin Cross8f101b42015-06-17 15:09:06 -070022 "android/soong/glob"
23
Colin Crossf6566ed2015-03-24 11:13:38 -070024 "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -080025)
26
27var (
28 DeviceSharedLibrary = "shared_library"
29 DeviceStaticLibrary = "static_library"
30 DeviceExecutable = "executable"
31 HostSharedLibrary = "host_shared_library"
32 HostStaticLibrary = "host_static_library"
33 HostExecutable = "host_executable"
34)
35
Dan Willemsen34cc69e2015-09-23 15:26:20 -070036type ModuleBuildParams struct {
37 Rule blueprint.Rule
38 Output WritablePath
39 Outputs WritablePaths
40 Input Path
41 Inputs Paths
42 Implicit Path
43 Implicits Paths
44 OrderOnly Paths
45 Default bool
46 Args map[string]string
47}
48
Colin Crossf6566ed2015-03-24 11:13:38 -070049type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070050 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070051 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070052 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070053 Os() OsType
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 Cross1e7d3702016-08-24 15:25:47 -070058 PrimaryArch() bool
Colin Cross1332b002015-04-07 17:11:30 -070059 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070060 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070061}
62
Colin Cross635c3b02016-05-18 15:37:25 -070063type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070064 blueprint.BaseModuleContext
65 androidBaseContext
66}
67
Colin Cross635c3b02016-05-18 15:37:25 -070068type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080069 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
Colin Crossa2344662016-03-24 13:14:12 -070079 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
80 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080081 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080083
84 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070085
86 Proprietary() bool
87 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080088}
89
Colin Cross635c3b02016-05-18 15:37:25 -070090type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080091 blueprint.Module
92
Colin Cross635c3b02016-05-18 15:37:25 -070093 GenerateAndroidBuildActions(ModuleContext)
Colin Cross1e676be2016-10-12 14:38:15 -070094 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -080095
Colin Cross635c3b02016-05-18 15:37:25 -070096 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -080097 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -070098 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -080099 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800100}
101
Colin Cross3f40fa42015-01-30 17:27:36 -0800102type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700103 Name string
104 Deps []string
105 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800106
Dan Willemsen0effe062015-11-30 16:06:01 -0800107 // emit build rules for this module
108 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800109
Colin Cross7d5136f2015-05-11 13:39:40 -0700110 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800111 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
112 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
113 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700114 Compile_multilib string `android:"arch_variant"`
115
116 Target struct {
117 Host struct {
118 Compile_multilib string
119 }
120 Android struct {
121 Compile_multilib string
122 }
123 }
124
125 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800126
Dan Willemsen782a2d12015-12-21 14:55:28 -0800127 // whether this is a proprietary vendor module, and should be installed into /vendor
128 Proprietary bool
129
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700130 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
131 // file
132 Logtags []string
133
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700134 // init.rc files to be installed if this module is installed
135 Init_rc []string
136
Chris Wolfe998306e2016-08-15 14:47:23 -0400137 // names of other modules to install if this module is installed
138 Required []string
139
Colin Crossa1ad8d12016-06-01 17:09:44 -0700140 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700141 CompileTarget Target `blueprint:"mutated"`
142 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800143
144 // Set by InitAndroidModule
145 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700146 ArchSpecific bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800147}
148
149type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700150 Host_supported *bool
151 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800152}
153
Colin Crossc472d572015-03-17 15:06:21 -0700154type Multilib string
155
156const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700157 MultilibBoth Multilib = "both"
158 MultilibFirst Multilib = "first"
159 MultilibCommon Multilib = "common"
160 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700161)
162
Colin Crossa1ad8d12016-06-01 17:09:44 -0700163type HostOrDeviceSupported int
164
165const (
166 _ HostOrDeviceSupported = iota
167 HostSupported
168 DeviceSupported
169 HostAndDeviceSupported
170 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700171 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700172)
173
Colin Cross635c3b02016-05-18 15:37:25 -0700174func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800175 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
176
177 base := m.base()
178 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700179
Colin Cross7f64b6d2015-07-09 13:57:48 -0700180 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700181
182 return m, propertyStructs
183}
184
Colin Cross635c3b02016-05-18 15:37:25 -0700185func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700186 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
187
188 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
189
190 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800191 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700192 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700193 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800194
Dan Willemsen218f6562015-07-08 18:13:11 -0700195 switch hod {
196 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800197 // Default to module to device supported, host not supported, can override in module
198 // properties
Colin Crossa4190c12016-07-12 13:11:25 -0700199 base.hostAndDeviceProperties.Device_supported = boolPtr(true)
Dan Willemsen218f6562015-07-08 18:13:11 -0700200 fallthrough
201 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800202 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
203 }
204
Colin Crosscfad1192015-11-02 16:43:11 -0800205 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800206}
207
208// A AndroidModuleBase object contains the properties that are common to all Android
209// modules. It should be included as an anonymous field in every module
210// struct definition. InitAndroidModule should then be called from the module's
211// factory function, and the return values from InitAndroidModule should be
212// returned from the factory function.
213//
214// The AndroidModuleBase type is responsible for implementing the
215// GenerateBuildActions method to support the blueprint.Module interface. This
216// method will then call the module's GenerateAndroidBuildActions method once
217// for each build variant that is to be built. GenerateAndroidBuildActions is
218// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
219// AndroidModuleContext exposes extra functionality specific to the Android build
220// system including details about the particular build variant that is to be
221// generated.
222//
223// For example:
224//
225// import (
226// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700227// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800228// )
229//
230// type myModule struct {
231// common.AndroidModuleBase
232// properties struct {
233// MyProperty string
234// }
235// }
236//
237// func NewMyModule() (blueprint.Module, []interface{}) {
238// m := &myModule{}
239// return common.InitAndroidModule(m, &m.properties)
240// }
241//
242// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
243// // Get the CPU architecture for the current build variant.
244// variantArch := ctx.Arch()
245//
246// // ...
247// }
Colin Cross635c3b02016-05-18 15:37:25 -0700248type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800249 // Putting the curiously recurring thing pointing to the thing that contains
250 // the thing pattern to good use.
Colin Cross635c3b02016-05-18 15:37:25 -0700251 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800252
253 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700254 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800255 hostAndDeviceProperties hostAndDeviceProperties
256 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700257 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700258 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800259
260 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700261 installFiles Paths
262 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700263
264 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
265 // Only set on the final variant of each module
266 installTarget string
267 checkbuildTarget string
268 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700269
Colin Cross178a5092016-09-13 13:42:32 -0700270 hooks hooks
Colin Cross3f40fa42015-01-30 17:27:36 -0800271}
272
Colin Cross635c3b02016-05-18 15:37:25 -0700273func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800274 return a
275}
276
Colin Cross8b74d172016-09-13 09:59:14 -0700277func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700278 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700279 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700280}
281
Colin Crossa1ad8d12016-06-01 17:09:44 -0700282func (a *ModuleBase) Target() Target {
283 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800284}
285
Colin Cross8b74d172016-09-13 09:59:14 -0700286func (a *ModuleBase) TargetPrimary() bool {
287 return a.commonProperties.CompilePrimary
288}
289
Colin Crossa1ad8d12016-06-01 17:09:44 -0700290func (a *ModuleBase) Os() OsType {
291 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800292}
293
Colin Cross635c3b02016-05-18 15:37:25 -0700294func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700295 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800296}
297
Colin Cross635c3b02016-05-18 15:37:25 -0700298func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700299 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800300}
301
Dan Willemsen0b24c742016-10-04 15:13:37 -0700302func (a *ModuleBase) ArchSpecific() bool {
303 return a.commonProperties.ArchSpecific
304}
305
Colin Crossa1ad8d12016-06-01 17:09:44 -0700306func (a *ModuleBase) OsClassSupported() []OsClass {
307 switch a.commonProperties.HostOrDeviceSupported {
308 case HostSupported:
309 // TODO(ccross): explicitly mark host cross support
310 return []OsClass{Host, HostCross}
311 case DeviceSupported:
312 return []OsClass{Device}
313 case HostAndDeviceSupported:
314 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700315 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700316 supported = append(supported, Host, HostCross)
317 }
Colin Crossa4190c12016-07-12 13:11:25 -0700318 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700319 supported = append(supported, Device)
320 }
321 return supported
322 default:
323 return nil
324 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800325}
326
Colin Cross635c3b02016-05-18 15:37:25 -0700327func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800328 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
329 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700330 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800331}
332
Colin Cross635c3b02016-05-18 15:37:25 -0700333func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800334 if a.commonProperties.Enabled == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700335 return a.Os().Class != HostCross
Dan Willemsen490fd492015-11-24 17:53:15 -0800336 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800337 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800338}
339
Colin Cross635c3b02016-05-18 15:37:25 -0700340func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700341 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800342
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700343 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800344 ctx.VisitDepsDepthFirstIf(isFileInstaller,
345 func(m blueprint.Module) {
346 fileInstaller := m.(fileInstaller)
347 files := fileInstaller.filesToInstall()
348 result = append(result, files...)
349 })
350
351 return result
352}
353
Colin Cross635c3b02016-05-18 15:37:25 -0700354func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800355 return a.installFiles
356}
357
Colin Cross635c3b02016-05-18 15:37:25 -0700358func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800359 return p.noAddressSanitizer
360}
361
Colin Cross635c3b02016-05-18 15:37:25 -0700362func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800363 return false
364}
365
Colin Cross635c3b02016-05-18 15:37:25 -0700366func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700367 allInstalledFiles := Paths{}
368 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800369 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700370 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700371 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
372 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800373 })
374
Colin Cross9454bfa2015-03-17 13:24:18 -0700375 deps := []string{}
376
Colin Cross3f40fa42015-01-30 17:27:36 -0800377 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700378 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700380 Rule: blueprint.Phony,
381 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700382 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800383 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700384 })
385 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700386 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700387 }
388
389 if len(allCheckbuildFiles) > 0 {
390 name := ctx.ModuleName() + "-checkbuild"
391 ctx.Build(pctx, blueprint.BuildParams{
392 Rule: blueprint.Phony,
393 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700394 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700395 Optional: true,
396 })
397 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700398 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700399 }
400
401 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800402 suffix := ""
403 if ctx.Config().(Config).EmbeddedInMake() {
404 suffix = "-soong"
405 }
406
Colin Cross9454bfa2015-03-17 13:24:18 -0700407 ctx.Build(pctx, blueprint.BuildParams{
408 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800409 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700410 Implicits: deps,
411 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800412 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700413
414 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800415 }
416}
417
Colin Cross635c3b02016-05-18 15:37:25 -0700418func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700419 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700420 target: a.commonProperties.CompileTarget,
421 targetPrimary: a.commonProperties.CompilePrimary,
422 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800423 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800424}
425
Colin Cross635c3b02016-05-18 15:37:25 -0700426func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800427 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700428 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700429 ModuleContext: ctx,
430 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
431 installDeps: a.computeInstallDeps(ctx),
432 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800433 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800434 }
435
Colin Cross9b1d13d2016-09-19 15:18:11 -0700436 if a.Enabled() {
437 a.module.GenerateAndroidBuildActions(androidCtx)
438 if ctx.Failed() {
439 return
440 }
441
442 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
443 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800444 }
445
Colin Cross9b1d13d2016-09-19 15:18:11 -0700446 if a == ctx.FinalModule().(Module).base() {
447 a.generateModuleTarget(ctx)
448 if ctx.Failed() {
449 return
450 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800451 }
452}
453
Colin Crossf6566ed2015-03-24 11:13:38 -0700454type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700455 target Target
456 targetPrimary bool
457 debug bool
458 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700459}
460
Colin Cross3f40fa42015-01-30 17:27:36 -0800461type androidModuleContext struct {
462 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700463 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700464 installDeps Paths
465 installFiles Paths
466 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800467 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700468 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800469}
470
471func (a *androidModuleContext) ninjaError(outputs []string, err error) {
472 a.ModuleContext.Build(pctx, blueprint.BuildParams{
473 Rule: ErrorRule,
474 Outputs: outputs,
475 Optional: true,
476 Args: map[string]string{
477 "error": err.Error(),
478 },
479 })
480 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800481}
482
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800483func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Crosse2c48742016-04-27 13:47:35 -0700484 if a.missingDeps != nil && params.Rule != globRule {
Colin Cross6ff51382015-12-17 16:39:19 -0800485 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
486 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
487 return
488 }
489
Colin Cross3f40fa42015-01-30 17:27:36 -0800490 params.Optional = true
491 a.ModuleContext.Build(pctx, params)
492}
493
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700494func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
495 bparams := blueprint.BuildParams{
496 Rule: params.Rule,
497 Outputs: params.Outputs.Strings(),
498 Inputs: params.Inputs.Strings(),
499 Implicits: params.Implicits.Strings(),
500 OrderOnly: params.OrderOnly.Strings(),
501 Args: params.Args,
502 Optional: !params.Default,
503 }
504
505 if params.Output != nil {
506 bparams.Outputs = append(bparams.Outputs, params.Output.String())
507 }
508 if params.Input != nil {
509 bparams.Inputs = append(bparams.Inputs, params.Input.String())
510 }
511 if params.Implicit != nil {
512 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
513 }
514
Colin Cross6ff51382015-12-17 16:39:19 -0800515 if a.missingDeps != nil {
516 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
517 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
518 return
519 }
520
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700521 a.ModuleContext.Build(pctx, bparams)
522}
523
Colin Cross6ff51382015-12-17 16:39:19 -0800524func (a *androidModuleContext) GetMissingDependencies() []string {
525 return a.missingDeps
526}
527
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800528func (a *androidModuleContext) AddMissingDependencies(deps []string) {
529 if deps != nil {
530 a.missingDeps = append(a.missingDeps, deps...)
531 }
532}
533
Colin Crossa1ad8d12016-06-01 17:09:44 -0700534func (a *androidBaseContextImpl) Target() Target {
535 return a.target
536}
537
Colin Cross8b74d172016-09-13 09:59:14 -0700538func (a *androidBaseContextImpl) TargetPrimary() bool {
539 return a.targetPrimary
540}
541
Colin Crossf6566ed2015-03-24 11:13:38 -0700542func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700543 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800544}
545
Colin Crossa1ad8d12016-06-01 17:09:44 -0700546func (a *androidBaseContextImpl) Os() OsType {
547 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800548}
549
Colin Crossf6566ed2015-03-24 11:13:38 -0700550func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700551 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700552}
553
554func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700555 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700556}
557
Colin Cross0af4b842015-04-30 16:36:18 -0700558func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700559 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700560}
561
Colin Crossf6566ed2015-03-24 11:13:38 -0700562func (a *androidBaseContextImpl) Debug() bool {
563 return a.debug
564}
565
Colin Cross1e7d3702016-08-24 15:25:47 -0700566func (a *androidBaseContextImpl) PrimaryArch() bool {
567 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
568}
569
Colin Cross1332b002015-04-07 17:11:30 -0700570func (a *androidBaseContextImpl) AConfig() Config {
571 return a.config
572}
573
Colin Cross9272ade2016-08-17 15:24:12 -0700574func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
575 return DeviceConfig{a.config.deviceConfig}
576}
577
Colin Cross8d8f8e22016-08-03 11:57:50 -0700578func (a *androidModuleContext) Proprietary() bool {
579 return a.module.base().commonProperties.Proprietary
Dan Willemsen782a2d12015-12-21 14:55:28 -0800580}
581
Colin Cross8d8f8e22016-08-03 11:57:50 -0700582func (a *androidModuleContext) InstallInData() bool {
583 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800584}
585
586func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700587 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700588
Dan Willemsen782a2d12015-12-21 14:55:28 -0800589 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700590 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800591
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800592 if a.Host() || !a.AConfig().SkipDeviceInstall() {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800593 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700594
Colin Cross89562dc2016-10-03 17:47:19 -0700595 var implicitDeps, orderOnlyDeps Paths
596
597 if a.Host() {
598 // Installed host modules might be used during the build, depend directly on their
599 // dependencies so their timestamp is updated whenever their dependency is updated
600 implicitDeps = deps
601 } else {
602 orderOnlyDeps = deps
603 }
604
Dan Willemsen322acaf2016-01-12 23:07:05 -0800605 a.ModuleBuild(pctx, ModuleBuildParams{
606 Rule: Cp,
607 Output: fullInstallPath,
608 Input: srcPath,
Colin Cross89562dc2016-10-03 17:47:19 -0700609 Implicits: implicitDeps,
610 OrderOnly: orderOnlyDeps,
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800611 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800612 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800613
Dan Willemsen322acaf2016-01-12 23:07:05 -0800614 a.installFiles = append(a.installFiles, fullInstallPath)
615 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700616 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700617 return fullInstallPath
618}
619
Colin Crossa2344662016-03-24 13:14:12 -0700620func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700621 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800622}
623
Colin Cross3854a602016-01-11 12:49:11 -0800624func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
625 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700626 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800627
Colin Cross12fc4972016-01-11 12:49:11 -0800628 if a.Host() || !a.AConfig().SkipDeviceInstall() {
629 a.ModuleBuild(pctx, ModuleBuildParams{
630 Rule: Symlink,
631 Output: fullInstallPath,
632 OrderOnly: Paths{srcPath},
633 Default: !a.AConfig().EmbeddedInMake(),
634 Args: map[string]string{
635 "fromPath": srcPath.String(),
636 },
637 })
Colin Cross3854a602016-01-11 12:49:11 -0800638
Colin Cross12fc4972016-01-11 12:49:11 -0800639 a.installFiles = append(a.installFiles, fullInstallPath)
640 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
641 }
Colin Cross3854a602016-01-11 12:49:11 -0800642 return fullInstallPath
643}
644
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700645func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800646 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
647}
648
Colin Cross3f40fa42015-01-30 17:27:36 -0800649type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700650 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800651}
652
653func isFileInstaller(m blueprint.Module) bool {
654 _, ok := m.(fileInstaller)
655 return ok
656}
657
658func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700659 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800660 return ok
661}
Colin Crossfce53272015-04-08 11:21:40 -0700662
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700663func findStringInSlice(str string, slice []string) int {
664 for i, s := range slice {
665 if s == str {
666 return i
Colin Crossfce53272015-04-08 11:21:40 -0700667 }
668 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700669 return -1
670}
671
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700672func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
673 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700674 for i, e := range excludes {
675 j := findStringInSlice(e, srcFiles)
676 if j != -1 {
677 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
678 }
679
680 excludes[i] = filepath.Join(prefix, e)
681 }
682
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700683 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700684 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700685 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700686 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700687 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700688 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700689 }
690 }
691
692 return globbedSrcFiles
693}
694
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700695func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
696 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700697 if err != nil {
698 ctx.ModuleErrorf("glob: %s", err.Error())
699 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700700 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700701}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700702
Colin Cross463a90e2015-06-17 14:20:06 -0700703func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700704 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -0700705}
706
Colin Cross1f8c52b2015-06-16 16:38:17 -0700707func BuildTargetSingleton() blueprint.Singleton {
708 return &buildTargetSingleton{}
709}
710
711type buildTargetSingleton struct{}
712
713func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
714 checkbuildDeps := []string{}
715
716 dirModules := make(map[string][]string)
717
718 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700719 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700720 blueprintDir := a.base().blueprintDir
721 installTarget := a.base().installTarget
722 checkbuildTarget := a.base().checkbuildTarget
723
724 if checkbuildTarget != "" {
725 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
726 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
727 }
728
729 if installTarget != "" {
730 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
731 }
732 }
733 })
734
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800735 suffix := ""
736 if ctx.Config().(Config).EmbeddedInMake() {
737 suffix = "-soong"
738 }
739
Colin Cross1f8c52b2015-06-16 16:38:17 -0700740 // Create a top-level checkbuild target that depends on all modules
741 ctx.Build(pctx, blueprint.BuildParams{
742 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800743 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700744 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700745 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700746 })
747
748 // Create a mm/<directory> target that depends on all modules in a directory
749 dirs := sortedKeys(dirModules)
750 for _, dir := range dirs {
751 ctx.Build(pctx, blueprint.BuildParams{
752 Rule: blueprint.Phony,
753 Outputs: []string{filepath.Join("mm", dir)},
754 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800755 // HACK: checkbuild should be an optional build, but force it
756 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800757 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700758 })
759 }
760}
Colin Crossd779da42015-12-17 18:00:23 -0800761
762type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700763 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800764 ctx interface {
765 ModuleName(blueprint.Module) string
766 ModuleSubDir(blueprint.Module) string
767 }
768}
769
770func (s AndroidModulesByName) Len() int { return len(s.slice) }
771func (s AndroidModulesByName) Less(i, j int) bool {
772 mi, mj := s.slice[i], s.slice[j]
773 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
774
775 if ni != nj {
776 return ni < nj
777 } else {
778 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
779 }
780}
781func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }