blob: d3f2383873ee088434a81f83bee9ff37222ca1d3 [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
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
Colin Crossa120ec12016-08-19 16:07:38 -070028func init() {
29 RegisterTopDownMutator("customizer", customizerMutator).Parallel()
30 RegisterBottomUpMutator("defaults_deps", defaultsDepsMutator).Parallel()
31 RegisterTopDownMutator("defaults", defaultsMutator).Parallel()
32
33 RegisterBottomUpMutator("arch", ArchMutator).Parallel()
34}
35
Colin Cross3f40fa42015-01-30 17:27:36 -080036var (
37 DeviceSharedLibrary = "shared_library"
38 DeviceStaticLibrary = "static_library"
39 DeviceExecutable = "executable"
40 HostSharedLibrary = "host_shared_library"
41 HostStaticLibrary = "host_static_library"
42 HostExecutable = "host_executable"
43)
44
Dan Willemsen34cc69e2015-09-23 15:26:20 -070045type ModuleBuildParams struct {
46 Rule blueprint.Rule
47 Output WritablePath
48 Outputs WritablePaths
49 Input Path
50 Inputs Paths
51 Implicit Path
52 Implicits Paths
53 OrderOnly Paths
54 Default bool
55 Args map[string]string
56}
57
Colin Crossf6566ed2015-03-24 11:13:38 -070058type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070059 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070060 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070061 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070062 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070063 Host() bool
64 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070065 Darwin() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070066 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070067 PrimaryArch() bool
Colin Cross1332b002015-04-07 17:11:30 -070068 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070069 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070070}
71
Colin Cross635c3b02016-05-18 15:37:25 -070072type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070073 blueprint.BaseModuleContext
74 androidBaseContext
75}
76
Colin Cross635c3b02016-05-18 15:37:25 -070077type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080078 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070079 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080080
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 // Similar to Build, but takes Paths instead of []string,
82 // and performs more verification.
83 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070084
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085 ExpandSources(srcFiles, excludes []string) Paths
86 Glob(outDir, globPattern string, excludes []string) Paths
87
Colin Crossa2344662016-03-24 13:14:12 -070088 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
89 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080090 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070091 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080092
93 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070094
95 Proprietary() bool
96 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080097}
98
Colin Cross635c3b02016-05-18 15:37:25 -070099type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800100 blueprint.Module
101
Colin Cross635c3b02016-05-18 15:37:25 -0700102 GenerateAndroidBuildActions(ModuleContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800103
Colin Cross635c3b02016-05-18 15:37:25 -0700104 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800105 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700106 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800107 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800108}
109
Colin Cross3f40fa42015-01-30 17:27:36 -0800110type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700111 Name string
112 Deps []string
113 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800114
Dan Willemsen0effe062015-11-30 16:06:01 -0800115 // emit build rules for this module
116 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800117
Colin Cross7d5136f2015-05-11 13:39:40 -0700118 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800119 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
120 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
121 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700122 Compile_multilib string `android:"arch_variant"`
123
124 Target struct {
125 Host struct {
126 Compile_multilib string
127 }
128 Android struct {
129 Compile_multilib string
130 }
131 }
132
133 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800134
Dan Willemsen782a2d12015-12-21 14:55:28 -0800135 // whether this is a proprietary vendor module, and should be installed into /vendor
136 Proprietary bool
137
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700138 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
139 // file
140 Logtags []string
141
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700142 // init.rc files to be installed if this module is installed
143 Init_rc []string
144
Chris Wolfe998306e2016-08-15 14:47:23 -0400145 // names of other modules to install if this module is installed
146 Required []string
147
Colin Crossa1ad8d12016-06-01 17:09:44 -0700148 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700149 CompileTarget Target `blueprint:"mutated"`
150 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800151
152 // Set by InitAndroidModule
153 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
154}
155
156type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700157 Host_supported *bool
158 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800159}
160
Colin Crossc472d572015-03-17 15:06:21 -0700161type Multilib string
162
163const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700164 MultilibBoth Multilib = "both"
165 MultilibFirst Multilib = "first"
166 MultilibCommon Multilib = "common"
167 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700168)
169
Colin Crossa1ad8d12016-06-01 17:09:44 -0700170type HostOrDeviceSupported int
171
172const (
173 _ HostOrDeviceSupported = iota
174 HostSupported
175 DeviceSupported
176 HostAndDeviceSupported
177 HostAndDeviceDefault
178)
179
Colin Cross635c3b02016-05-18 15:37:25 -0700180func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800181 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
182
183 base := m.base()
184 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700185
Colin Cross7f64b6d2015-07-09 13:57:48 -0700186 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700187
188 return m, propertyStructs
189}
190
Colin Cross635c3b02016-05-18 15:37:25 -0700191func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700192 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
193
194 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
195
196 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800197 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700198 base.commonProperties.Default_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800199
Dan Willemsen218f6562015-07-08 18:13:11 -0700200 switch hod {
201 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800202 // Default to module to device supported, host not supported, can override in module
203 // properties
Colin Crossa4190c12016-07-12 13:11:25 -0700204 base.hostAndDeviceProperties.Device_supported = boolPtr(true)
Dan Willemsen218f6562015-07-08 18:13:11 -0700205 fallthrough
206 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800207 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
208 }
209
Colin Crosscfad1192015-11-02 16:43:11 -0800210 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800211}
212
Colin Crossa120ec12016-08-19 16:07:38 -0700213func AddCustomizer(m blueprint.Module, c PropertyCustomizer) {
214 base := m.(Module).base()
215 base.customizers = append(base.customizers, c)
216}
217
Colin Cross3f40fa42015-01-30 17:27:36 -0800218// A AndroidModuleBase object contains the properties that are common to all Android
219// modules. It should be included as an anonymous field in every module
220// struct definition. InitAndroidModule should then be called from the module's
221// factory function, and the return values from InitAndroidModule should be
222// returned from the factory function.
223//
224// The AndroidModuleBase type is responsible for implementing the
225// GenerateBuildActions method to support the blueprint.Module interface. This
226// method will then call the module's GenerateAndroidBuildActions method once
227// for each build variant that is to be built. GenerateAndroidBuildActions is
228// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
229// AndroidModuleContext exposes extra functionality specific to the Android build
230// system including details about the particular build variant that is to be
231// generated.
232//
233// For example:
234//
235// import (
236// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700237// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800238// )
239//
240// type myModule struct {
241// common.AndroidModuleBase
242// properties struct {
243// MyProperty string
244// }
245// }
246//
247// func NewMyModule() (blueprint.Module, []interface{}) {
248// m := &myModule{}
249// return common.InitAndroidModule(m, &m.properties)
250// }
251//
252// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
253// // Get the CPU architecture for the current build variant.
254// variantArch := ctx.Arch()
255//
256// // ...
257// }
Colin Cross635c3b02016-05-18 15:37:25 -0700258type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800259 // Putting the curiously recurring thing pointing to the thing that contains
260 // the thing pattern to good use.
Colin Cross635c3b02016-05-18 15:37:25 -0700261 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800262
263 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700264 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800265 hostAndDeviceProperties hostAndDeviceProperties
266 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700267 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700268 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800269
270 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700271 installFiles Paths
272 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700273
274 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
275 // Only set on the final variant of each module
276 installTarget string
277 checkbuildTarget string
278 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700279
280 customizers []PropertyCustomizer
Colin Cross3f40fa42015-01-30 17:27:36 -0800281}
282
Colin Cross635c3b02016-05-18 15:37:25 -0700283func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800284 return a
285}
286
Colin Cross8b74d172016-09-13 09:59:14 -0700287func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700288 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700289 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700290}
291
Colin Crossa1ad8d12016-06-01 17:09:44 -0700292func (a *ModuleBase) Target() Target {
293 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800294}
295
Colin Cross8b74d172016-09-13 09:59:14 -0700296func (a *ModuleBase) TargetPrimary() bool {
297 return a.commonProperties.CompilePrimary
298}
299
Colin Crossa1ad8d12016-06-01 17:09:44 -0700300func (a *ModuleBase) Os() OsType {
301 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800302}
303
Colin Cross635c3b02016-05-18 15:37:25 -0700304func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700305 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800306}
307
Colin Cross635c3b02016-05-18 15:37:25 -0700308func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700309 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800310}
311
Colin Crossa1ad8d12016-06-01 17:09:44 -0700312func (a *ModuleBase) OsClassSupported() []OsClass {
313 switch a.commonProperties.HostOrDeviceSupported {
314 case HostSupported:
315 // TODO(ccross): explicitly mark host cross support
316 return []OsClass{Host, HostCross}
317 case DeviceSupported:
318 return []OsClass{Device}
319 case HostAndDeviceSupported:
320 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700321 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700322 supported = append(supported, Host, HostCross)
323 }
Colin Crossa4190c12016-07-12 13:11:25 -0700324 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700325 supported = append(supported, Device)
326 }
327 return supported
328 default:
329 return nil
330 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800331}
332
Colin Cross635c3b02016-05-18 15:37:25 -0700333func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800334 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
335 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700336 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800337}
338
Colin Cross635c3b02016-05-18 15:37:25 -0700339func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800340 if a.commonProperties.Enabled == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700341 return a.Os().Class != HostCross
Dan Willemsen490fd492015-11-24 17:53:15 -0800342 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800343 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800344}
345
Colin Cross635c3b02016-05-18 15:37:25 -0700346func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700347 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800348
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700349 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800350 ctx.VisitDepsDepthFirstIf(isFileInstaller,
351 func(m blueprint.Module) {
352 fileInstaller := m.(fileInstaller)
353 files := fileInstaller.filesToInstall()
354 result = append(result, files...)
355 })
356
357 return result
358}
359
Colin Cross635c3b02016-05-18 15:37:25 -0700360func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800361 return a.installFiles
362}
363
Colin Cross635c3b02016-05-18 15:37:25 -0700364func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800365 return p.noAddressSanitizer
366}
367
Colin Cross635c3b02016-05-18 15:37:25 -0700368func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800369 return false
370}
371
Colin Cross635c3b02016-05-18 15:37:25 -0700372func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
373 if a != ctx.FinalModule().(Module).base() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800374 return
375 }
376
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700377 allInstalledFiles := Paths{}
378 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700380 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700381 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
382 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800383 })
384
Colin Cross9454bfa2015-03-17 13:24:18 -0700385 deps := []string{}
386
Colin Cross3f40fa42015-01-30 17:27:36 -0800387 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700388 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800389 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700390 Rule: blueprint.Phony,
391 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700392 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800393 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700394 })
395 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700396 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700397 }
398
399 if len(allCheckbuildFiles) > 0 {
400 name := ctx.ModuleName() + "-checkbuild"
401 ctx.Build(pctx, blueprint.BuildParams{
402 Rule: blueprint.Phony,
403 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700404 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700405 Optional: true,
406 })
407 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700408 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700409 }
410
411 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800412 suffix := ""
413 if ctx.Config().(Config).EmbeddedInMake() {
414 suffix = "-soong"
415 }
416
Colin Cross9454bfa2015-03-17 13:24:18 -0700417 ctx.Build(pctx, blueprint.BuildParams{
418 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800419 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700420 Implicits: deps,
421 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800422 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700423
424 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800425 }
426}
427
Colin Cross635c3b02016-05-18 15:37:25 -0700428func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700429 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700430 target: a.commonProperties.CompileTarget,
431 targetPrimary: a.commonProperties.CompilePrimary,
432 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800433 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800434}
435
Colin Cross635c3b02016-05-18 15:37:25 -0700436func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800437 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700438 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700439 ModuleContext: ctx,
440 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
441 installDeps: a.computeInstallDeps(ctx),
442 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800443 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800444 }
445
Dan Willemsen0effe062015-11-30 16:06:01 -0800446 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800447 return
448 }
449
450 a.module.GenerateAndroidBuildActions(androidCtx)
451 if ctx.Failed() {
452 return
453 }
454
Colin Crossc9404352015-03-26 16:10:12 -0700455 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
456 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
457
Colin Cross3f40fa42015-01-30 17:27:36 -0800458 a.generateModuleTarget(ctx)
459 if ctx.Failed() {
460 return
461 }
462}
463
Colin Crossf6566ed2015-03-24 11:13:38 -0700464type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700465 target Target
466 targetPrimary bool
467 debug bool
468 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700469}
470
Colin Cross3f40fa42015-01-30 17:27:36 -0800471type androidModuleContext struct {
472 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700473 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700474 installDeps Paths
475 installFiles Paths
476 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800477 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700478 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800479}
480
481func (a *androidModuleContext) ninjaError(outputs []string, err error) {
482 a.ModuleContext.Build(pctx, blueprint.BuildParams{
483 Rule: ErrorRule,
484 Outputs: outputs,
485 Optional: true,
486 Args: map[string]string{
487 "error": err.Error(),
488 },
489 })
490 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800491}
492
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800493func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Crosse2c48742016-04-27 13:47:35 -0700494 if a.missingDeps != nil && params.Rule != globRule {
Colin Cross6ff51382015-12-17 16:39:19 -0800495 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
496 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
497 return
498 }
499
Colin Cross3f40fa42015-01-30 17:27:36 -0800500 params.Optional = true
501 a.ModuleContext.Build(pctx, params)
502}
503
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700504func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
505 bparams := blueprint.BuildParams{
506 Rule: params.Rule,
507 Outputs: params.Outputs.Strings(),
508 Inputs: params.Inputs.Strings(),
509 Implicits: params.Implicits.Strings(),
510 OrderOnly: params.OrderOnly.Strings(),
511 Args: params.Args,
512 Optional: !params.Default,
513 }
514
515 if params.Output != nil {
516 bparams.Outputs = append(bparams.Outputs, params.Output.String())
517 }
518 if params.Input != nil {
519 bparams.Inputs = append(bparams.Inputs, params.Input.String())
520 }
521 if params.Implicit != nil {
522 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
523 }
524
Colin Cross6ff51382015-12-17 16:39:19 -0800525 if a.missingDeps != nil {
526 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
527 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
528 return
529 }
530
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700531 a.ModuleContext.Build(pctx, bparams)
532}
533
Colin Cross6ff51382015-12-17 16:39:19 -0800534func (a *androidModuleContext) GetMissingDependencies() []string {
535 return a.missingDeps
536}
537
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800538func (a *androidModuleContext) AddMissingDependencies(deps []string) {
539 if deps != nil {
540 a.missingDeps = append(a.missingDeps, deps...)
541 }
542}
543
Colin Crossa1ad8d12016-06-01 17:09:44 -0700544func (a *androidBaseContextImpl) Target() Target {
545 return a.target
546}
547
Colin Cross8b74d172016-09-13 09:59:14 -0700548func (a *androidBaseContextImpl) TargetPrimary() bool {
549 return a.targetPrimary
550}
551
Colin Crossf6566ed2015-03-24 11:13:38 -0700552func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700553 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800554}
555
Colin Crossa1ad8d12016-06-01 17:09:44 -0700556func (a *androidBaseContextImpl) Os() OsType {
557 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800558}
559
Colin Crossf6566ed2015-03-24 11:13:38 -0700560func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700561 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700562}
563
564func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700565 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700566}
567
Colin Cross0af4b842015-04-30 16:36:18 -0700568func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700569 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700570}
571
Colin Crossf6566ed2015-03-24 11:13:38 -0700572func (a *androidBaseContextImpl) Debug() bool {
573 return a.debug
574}
575
Colin Cross1e7d3702016-08-24 15:25:47 -0700576func (a *androidBaseContextImpl) PrimaryArch() bool {
577 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
578}
579
Colin Cross1332b002015-04-07 17:11:30 -0700580func (a *androidBaseContextImpl) AConfig() Config {
581 return a.config
582}
583
Colin Cross9272ade2016-08-17 15:24:12 -0700584func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
585 return DeviceConfig{a.config.deviceConfig}
586}
587
Colin Cross8d8f8e22016-08-03 11:57:50 -0700588func (a *androidModuleContext) Proprietary() bool {
589 return a.module.base().commonProperties.Proprietary
Dan Willemsen782a2d12015-12-21 14:55:28 -0800590}
591
Colin Cross8d8f8e22016-08-03 11:57:50 -0700592func (a *androidModuleContext) InstallInData() bool {
593 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800594}
595
596func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700597 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700598
Dan Willemsen782a2d12015-12-21 14:55:28 -0800599 fullInstallPath := installPath.Join(a, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800600
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800601 if a.Host() || !a.AConfig().SkipDeviceInstall() {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800602 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700603
Dan Willemsen322acaf2016-01-12 23:07:05 -0800604 a.ModuleBuild(pctx, ModuleBuildParams{
605 Rule: Cp,
606 Output: fullInstallPath,
607 Input: srcPath,
608 OrderOnly: Paths(deps),
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800609 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800610 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800611
Dan Willemsen322acaf2016-01-12 23:07:05 -0800612 a.installFiles = append(a.installFiles, fullInstallPath)
613 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700614 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700615 return fullInstallPath
616}
617
Colin Crossa2344662016-03-24 13:14:12 -0700618func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700619 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800620}
621
Colin Cross3854a602016-01-11 12:49:11 -0800622func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
623 fullInstallPath := installPath.Join(a, name)
624
Colin Cross12fc4972016-01-11 12:49:11 -0800625 if a.Host() || !a.AConfig().SkipDeviceInstall() {
626 a.ModuleBuild(pctx, ModuleBuildParams{
627 Rule: Symlink,
628 Output: fullInstallPath,
629 OrderOnly: Paths{srcPath},
630 Default: !a.AConfig().EmbeddedInMake(),
631 Args: map[string]string{
632 "fromPath": srcPath.String(),
633 },
634 })
Colin Cross3854a602016-01-11 12:49:11 -0800635
Colin Cross12fc4972016-01-11 12:49:11 -0800636 a.installFiles = append(a.installFiles, fullInstallPath)
637 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
638 }
Colin Cross3854a602016-01-11 12:49:11 -0800639 return fullInstallPath
640}
641
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700642func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800643 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
644}
645
Colin Cross3f40fa42015-01-30 17:27:36 -0800646type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700647 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800648}
649
650func isFileInstaller(m blueprint.Module) bool {
651 _, ok := m.(fileInstaller)
652 return ok
653}
654
655func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700656 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800657 return ok
658}
Colin Crossfce53272015-04-08 11:21:40 -0700659
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700660func findStringInSlice(str string, slice []string) int {
661 for i, s := range slice {
662 if s == str {
663 return i
Colin Crossfce53272015-04-08 11:21:40 -0700664 }
665 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700666 return -1
667}
668
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700669func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
670 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700671 for i, e := range excludes {
672 j := findStringInSlice(e, srcFiles)
673 if j != -1 {
674 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
675 }
676
677 excludes[i] = filepath.Join(prefix, e)
678 }
679
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700680 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700681 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700682 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700683 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700684 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700685 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700686 }
687 }
688
689 return globbedSrcFiles
690}
691
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700692func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
693 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700694 if err != nil {
695 ctx.ModuleErrorf("glob: %s", err.Error())
696 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700697 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700698}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700699
Colin Cross463a90e2015-06-17 14:20:06 -0700700func init() {
701 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
702}
703
Colin Cross1f8c52b2015-06-16 16:38:17 -0700704func BuildTargetSingleton() blueprint.Singleton {
705 return &buildTargetSingleton{}
706}
707
708type buildTargetSingleton struct{}
709
710func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
711 checkbuildDeps := []string{}
712
713 dirModules := make(map[string][]string)
714
715 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700716 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700717 blueprintDir := a.base().blueprintDir
718 installTarget := a.base().installTarget
719 checkbuildTarget := a.base().checkbuildTarget
720
721 if checkbuildTarget != "" {
722 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
723 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
724 }
725
726 if installTarget != "" {
727 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
728 }
729 }
730 })
731
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800732 suffix := ""
733 if ctx.Config().(Config).EmbeddedInMake() {
734 suffix = "-soong"
735 }
736
Colin Cross1f8c52b2015-06-16 16:38:17 -0700737 // Create a top-level checkbuild target that depends on all modules
738 ctx.Build(pctx, blueprint.BuildParams{
739 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800740 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700741 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700742 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700743 })
744
745 // Create a mm/<directory> target that depends on all modules in a directory
746 dirs := sortedKeys(dirModules)
747 for _, dir := range dirs {
748 ctx.Build(pctx, blueprint.BuildParams{
749 Rule: blueprint.Phony,
750 Outputs: []string{filepath.Join("mm", dir)},
751 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800752 // HACK: checkbuild should be an optional build, but force it
753 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800754 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700755 })
756 }
757}
Colin Crossd779da42015-12-17 18:00:23 -0800758
759type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700760 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800761 ctx interface {
762 ModuleName(blueprint.Module) string
763 ModuleSubDir(blueprint.Module) string
764 }
765}
766
767func (s AndroidModulesByName) Len() int { return len(s.slice) }
768func (s AndroidModulesByName) Less(i, j int) bool {
769 mi, mj := s.slice[i], s.slice[j]
770 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
771
772 if ni != nj {
773 return ni < nj
774 } else {
775 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
776 }
777}
778func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }