blob: f815ced7f01ed298e280f9bc5c6a46ba357bc156 [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() {
Colin Cross178a5092016-09-13 13:42:32 -070029 RegisterTopDownMutator("load_hooks", loadHookMutator).Parallel()
Colin Crossa120ec12016-08-19 16:07:38 -070030 RegisterBottomUpMutator("defaults_deps", defaultsDepsMutator).Parallel()
31 RegisterTopDownMutator("defaults", defaultsMutator).Parallel()
32
33 RegisterBottomUpMutator("arch", ArchMutator).Parallel()
Colin Cross178a5092016-09-13 13:42:32 -070034 RegisterTopDownMutator("arch_hooks", archHookMutator).Parallel()
Colin Crossa120ec12016-08-19 16:07:38 -070035}
36
Colin Cross3f40fa42015-01-30 17:27:36 -080037var (
38 DeviceSharedLibrary = "shared_library"
39 DeviceStaticLibrary = "static_library"
40 DeviceExecutable = "executable"
41 HostSharedLibrary = "host_shared_library"
42 HostStaticLibrary = "host_static_library"
43 HostExecutable = "host_executable"
44)
45
Dan Willemsen34cc69e2015-09-23 15:26:20 -070046type ModuleBuildParams struct {
47 Rule blueprint.Rule
48 Output WritablePath
49 Outputs WritablePaths
50 Input Path
51 Inputs Paths
52 Implicit Path
53 Implicits Paths
54 OrderOnly Paths
55 Default bool
56 Args map[string]string
57}
58
Colin Crossf6566ed2015-03-24 11:13:38 -070059type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070060 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070061 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070062 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070063 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070064 Host() bool
65 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070066 Darwin() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070067 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070068 PrimaryArch() bool
Colin Cross1332b002015-04-07 17:11:30 -070069 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070070 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070071}
72
Colin Cross635c3b02016-05-18 15:37:25 -070073type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070074 blueprint.BaseModuleContext
75 androidBaseContext
76}
77
Colin Cross635c3b02016-05-18 15:37:25 -070078type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080079 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070080 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080081
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 // Similar to Build, but takes Paths instead of []string,
83 // and performs more verification.
84 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070085
Dan Willemsen34cc69e2015-09-23 15:26:20 -070086 ExpandSources(srcFiles, excludes []string) Paths
87 Glob(outDir, globPattern string, excludes []string) Paths
88
Colin Crossa2344662016-03-24 13:14:12 -070089 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
90 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080091 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080093
94 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070095
96 Proprietary() bool
97 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080098}
99
Colin Cross635c3b02016-05-18 15:37:25 -0700100type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800101 blueprint.Module
102
Colin Cross635c3b02016-05-18 15:37:25 -0700103 GenerateAndroidBuildActions(ModuleContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800104
Colin Cross635c3b02016-05-18 15:37:25 -0700105 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800106 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700107 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800108 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800109}
110
Colin Cross3f40fa42015-01-30 17:27:36 -0800111type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700112 Name string
113 Deps []string
114 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800115
Dan Willemsen0effe062015-11-30 16:06:01 -0800116 // emit build rules for this module
117 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800118
Colin Cross7d5136f2015-05-11 13:39:40 -0700119 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800120 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
121 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
122 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700123 Compile_multilib string `android:"arch_variant"`
124
125 Target struct {
126 Host struct {
127 Compile_multilib string
128 }
129 Android struct {
130 Compile_multilib string
131 }
132 }
133
134 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800135
Dan Willemsen782a2d12015-12-21 14:55:28 -0800136 // whether this is a proprietary vendor module, and should be installed into /vendor
137 Proprietary bool
138
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700139 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
140 // file
141 Logtags []string
142
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700143 // init.rc files to be installed if this module is installed
144 Init_rc []string
145
Chris Wolfe998306e2016-08-15 14:47:23 -0400146 // names of other modules to install if this module is installed
147 Required []string
148
Colin Crossa1ad8d12016-06-01 17:09:44 -0700149 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700150 CompileTarget Target `blueprint:"mutated"`
151 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800152
153 // Set by InitAndroidModule
154 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
155}
156
157type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700158 Host_supported *bool
159 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800160}
161
Colin Crossc472d572015-03-17 15:06:21 -0700162type Multilib string
163
164const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700165 MultilibBoth Multilib = "both"
166 MultilibFirst Multilib = "first"
167 MultilibCommon Multilib = "common"
168 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700169)
170
Colin Crossa1ad8d12016-06-01 17:09:44 -0700171type HostOrDeviceSupported int
172
173const (
174 _ HostOrDeviceSupported = iota
175 HostSupported
176 DeviceSupported
177 HostAndDeviceSupported
178 HostAndDeviceDefault
179)
180
Colin Cross635c3b02016-05-18 15:37:25 -0700181func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800182 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
183
184 base := m.base()
185 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700186
Colin Cross7f64b6d2015-07-09 13:57:48 -0700187 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700188
189 return m, propertyStructs
190}
191
Colin Cross635c3b02016-05-18 15:37:25 -0700192func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700193 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
194
195 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
196
197 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800198 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700199 base.commonProperties.Default_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800200
Dan Willemsen218f6562015-07-08 18:13:11 -0700201 switch hod {
202 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800203 // Default to module to device supported, host not supported, can override in module
204 // properties
Colin Crossa4190c12016-07-12 13:11:25 -0700205 base.hostAndDeviceProperties.Device_supported = boolPtr(true)
Dan Willemsen218f6562015-07-08 18:13:11 -0700206 fallthrough
207 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800208 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
209 }
210
Colin Crosscfad1192015-11-02 16:43:11 -0800211 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800212}
213
214// A AndroidModuleBase object contains the properties that are common to all Android
215// modules. It should be included as an anonymous field in every module
216// struct definition. InitAndroidModule should then be called from the module's
217// factory function, and the return values from InitAndroidModule should be
218// returned from the factory function.
219//
220// The AndroidModuleBase type is responsible for implementing the
221// GenerateBuildActions method to support the blueprint.Module interface. This
222// method will then call the module's GenerateAndroidBuildActions method once
223// for each build variant that is to be built. GenerateAndroidBuildActions is
224// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
225// AndroidModuleContext exposes extra functionality specific to the Android build
226// system including details about the particular build variant that is to be
227// generated.
228//
229// For example:
230//
231// import (
232// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700233// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800234// )
235//
236// type myModule struct {
237// common.AndroidModuleBase
238// properties struct {
239// MyProperty string
240// }
241// }
242//
243// func NewMyModule() (blueprint.Module, []interface{}) {
244// m := &myModule{}
245// return common.InitAndroidModule(m, &m.properties)
246// }
247//
248// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
249// // Get the CPU architecture for the current build variant.
250// variantArch := ctx.Arch()
251//
252// // ...
253// }
Colin Cross635c3b02016-05-18 15:37:25 -0700254type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800255 // Putting the curiously recurring thing pointing to the thing that contains
256 // the thing pattern to good use.
Colin Cross635c3b02016-05-18 15:37:25 -0700257 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800258
259 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700260 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800261 hostAndDeviceProperties hostAndDeviceProperties
262 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700263 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700264 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800265
266 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700267 installFiles Paths
268 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700269
270 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
271 // Only set on the final variant of each module
272 installTarget string
273 checkbuildTarget string
274 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700275
Colin Cross178a5092016-09-13 13:42:32 -0700276 hooks hooks
Colin Cross3f40fa42015-01-30 17:27:36 -0800277}
278
Colin Cross635c3b02016-05-18 15:37:25 -0700279func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800280 return a
281}
282
Colin Cross8b74d172016-09-13 09:59:14 -0700283func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700284 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700285 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700286}
287
Colin Crossa1ad8d12016-06-01 17:09:44 -0700288func (a *ModuleBase) Target() Target {
289 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800290}
291
Colin Cross8b74d172016-09-13 09:59:14 -0700292func (a *ModuleBase) TargetPrimary() bool {
293 return a.commonProperties.CompilePrimary
294}
295
Colin Crossa1ad8d12016-06-01 17:09:44 -0700296func (a *ModuleBase) Os() OsType {
297 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800298}
299
Colin Cross635c3b02016-05-18 15:37:25 -0700300func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700301 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800302}
303
Colin Cross635c3b02016-05-18 15:37:25 -0700304func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700305 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800306}
307
Colin Crossa1ad8d12016-06-01 17:09:44 -0700308func (a *ModuleBase) OsClassSupported() []OsClass {
309 switch a.commonProperties.HostOrDeviceSupported {
310 case HostSupported:
311 // TODO(ccross): explicitly mark host cross support
312 return []OsClass{Host, HostCross}
313 case DeviceSupported:
314 return []OsClass{Device}
315 case HostAndDeviceSupported:
316 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700317 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700318 supported = append(supported, Host, HostCross)
319 }
Colin Crossa4190c12016-07-12 13:11:25 -0700320 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700321 supported = append(supported, Device)
322 }
323 return supported
324 default:
325 return nil
326 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800327}
328
Colin Cross635c3b02016-05-18 15:37:25 -0700329func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800330 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
331 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700332 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800333}
334
Colin Cross635c3b02016-05-18 15:37:25 -0700335func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800336 if a.commonProperties.Enabled == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700337 return a.Os().Class != HostCross
Dan Willemsen490fd492015-11-24 17:53:15 -0800338 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800339 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800340}
341
Colin Cross635c3b02016-05-18 15:37:25 -0700342func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700343 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800344
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700345 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800346 ctx.VisitDepsDepthFirstIf(isFileInstaller,
347 func(m blueprint.Module) {
348 fileInstaller := m.(fileInstaller)
349 files := fileInstaller.filesToInstall()
350 result = append(result, files...)
351 })
352
353 return result
354}
355
Colin Cross635c3b02016-05-18 15:37:25 -0700356func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800357 return a.installFiles
358}
359
Colin Cross635c3b02016-05-18 15:37:25 -0700360func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800361 return p.noAddressSanitizer
362}
363
Colin Cross635c3b02016-05-18 15:37:25 -0700364func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800365 return false
366}
367
Colin Cross635c3b02016-05-18 15:37:25 -0700368func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700369 allInstalledFiles := Paths{}
370 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800371 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700372 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700373 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
374 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800375 })
376
Colin Cross9454bfa2015-03-17 13:24:18 -0700377 deps := []string{}
378
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700380 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700382 Rule: blueprint.Phony,
383 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700384 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800385 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700386 })
387 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700388 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700389 }
390
391 if len(allCheckbuildFiles) > 0 {
392 name := ctx.ModuleName() + "-checkbuild"
393 ctx.Build(pctx, blueprint.BuildParams{
394 Rule: blueprint.Phony,
395 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700396 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700397 Optional: true,
398 })
399 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700400 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700401 }
402
403 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800404 suffix := ""
405 if ctx.Config().(Config).EmbeddedInMake() {
406 suffix = "-soong"
407 }
408
Colin Cross9454bfa2015-03-17 13:24:18 -0700409 ctx.Build(pctx, blueprint.BuildParams{
410 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800411 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700412 Implicits: deps,
413 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800414 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700415
416 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800417 }
418}
419
Colin Cross635c3b02016-05-18 15:37:25 -0700420func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700421 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700422 target: a.commonProperties.CompileTarget,
423 targetPrimary: a.commonProperties.CompilePrimary,
424 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800425 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800426}
427
Colin Cross635c3b02016-05-18 15:37:25 -0700428func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800429 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700430 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700431 ModuleContext: ctx,
432 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
433 installDeps: a.computeInstallDeps(ctx),
434 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800435 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800436 }
437
Colin Cross9b1d13d2016-09-19 15:18:11 -0700438 if a.Enabled() {
439 a.module.GenerateAndroidBuildActions(androidCtx)
440 if ctx.Failed() {
441 return
442 }
443
444 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
445 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800446 }
447
Colin Cross9b1d13d2016-09-19 15:18:11 -0700448 if a == ctx.FinalModule().(Module).base() {
449 a.generateModuleTarget(ctx)
450 if ctx.Failed() {
451 return
452 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800453 }
454}
455
Colin Crossf6566ed2015-03-24 11:13:38 -0700456type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700457 target Target
458 targetPrimary bool
459 debug bool
460 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700461}
462
Colin Cross3f40fa42015-01-30 17:27:36 -0800463type androidModuleContext struct {
464 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700465 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700466 installDeps Paths
467 installFiles Paths
468 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800469 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700470 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800471}
472
473func (a *androidModuleContext) ninjaError(outputs []string, err error) {
474 a.ModuleContext.Build(pctx, blueprint.BuildParams{
475 Rule: ErrorRule,
476 Outputs: outputs,
477 Optional: true,
478 Args: map[string]string{
479 "error": err.Error(),
480 },
481 })
482 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800483}
484
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800485func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Crosse2c48742016-04-27 13:47:35 -0700486 if a.missingDeps != nil && params.Rule != globRule {
Colin Cross6ff51382015-12-17 16:39:19 -0800487 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
488 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
489 return
490 }
491
Colin Cross3f40fa42015-01-30 17:27:36 -0800492 params.Optional = true
493 a.ModuleContext.Build(pctx, params)
494}
495
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700496func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
497 bparams := blueprint.BuildParams{
498 Rule: params.Rule,
499 Outputs: params.Outputs.Strings(),
500 Inputs: params.Inputs.Strings(),
501 Implicits: params.Implicits.Strings(),
502 OrderOnly: params.OrderOnly.Strings(),
503 Args: params.Args,
504 Optional: !params.Default,
505 }
506
507 if params.Output != nil {
508 bparams.Outputs = append(bparams.Outputs, params.Output.String())
509 }
510 if params.Input != nil {
511 bparams.Inputs = append(bparams.Inputs, params.Input.String())
512 }
513 if params.Implicit != nil {
514 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
515 }
516
Colin Cross6ff51382015-12-17 16:39:19 -0800517 if a.missingDeps != nil {
518 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
519 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
520 return
521 }
522
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700523 a.ModuleContext.Build(pctx, bparams)
524}
525
Colin Cross6ff51382015-12-17 16:39:19 -0800526func (a *androidModuleContext) GetMissingDependencies() []string {
527 return a.missingDeps
528}
529
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800530func (a *androidModuleContext) AddMissingDependencies(deps []string) {
531 if deps != nil {
532 a.missingDeps = append(a.missingDeps, deps...)
533 }
534}
535
Colin Crossa1ad8d12016-06-01 17:09:44 -0700536func (a *androidBaseContextImpl) Target() Target {
537 return a.target
538}
539
Colin Cross8b74d172016-09-13 09:59:14 -0700540func (a *androidBaseContextImpl) TargetPrimary() bool {
541 return a.targetPrimary
542}
543
Colin Crossf6566ed2015-03-24 11:13:38 -0700544func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700545 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800546}
547
Colin Crossa1ad8d12016-06-01 17:09:44 -0700548func (a *androidBaseContextImpl) Os() OsType {
549 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800550}
551
Colin Crossf6566ed2015-03-24 11:13:38 -0700552func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700553 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700554}
555
556func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700557 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700558}
559
Colin Cross0af4b842015-04-30 16:36:18 -0700560func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700561 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700562}
563
Colin Crossf6566ed2015-03-24 11:13:38 -0700564func (a *androidBaseContextImpl) Debug() bool {
565 return a.debug
566}
567
Colin Cross1e7d3702016-08-24 15:25:47 -0700568func (a *androidBaseContextImpl) PrimaryArch() bool {
569 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
570}
571
Colin Cross1332b002015-04-07 17:11:30 -0700572func (a *androidBaseContextImpl) AConfig() Config {
573 return a.config
574}
575
Colin Cross9272ade2016-08-17 15:24:12 -0700576func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
577 return DeviceConfig{a.config.deviceConfig}
578}
579
Colin Cross8d8f8e22016-08-03 11:57:50 -0700580func (a *androidModuleContext) Proprietary() bool {
581 return a.module.base().commonProperties.Proprietary
Dan Willemsen782a2d12015-12-21 14:55:28 -0800582}
583
Colin Cross8d8f8e22016-08-03 11:57:50 -0700584func (a *androidModuleContext) InstallInData() bool {
585 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800586}
587
588func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700589 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700590
Dan Willemsen782a2d12015-12-21 14:55:28 -0800591 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700592 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800593
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800594 if a.Host() || !a.AConfig().SkipDeviceInstall() {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800595 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700596
Colin Cross89562dc2016-10-03 17:47:19 -0700597 var implicitDeps, orderOnlyDeps Paths
598
599 if a.Host() {
600 // Installed host modules might be used during the build, depend directly on their
601 // dependencies so their timestamp is updated whenever their dependency is updated
602 implicitDeps = deps
603 } else {
604 orderOnlyDeps = deps
605 }
606
Dan Willemsen322acaf2016-01-12 23:07:05 -0800607 a.ModuleBuild(pctx, ModuleBuildParams{
608 Rule: Cp,
609 Output: fullInstallPath,
610 Input: srcPath,
Colin Cross89562dc2016-10-03 17:47:19 -0700611 Implicits: implicitDeps,
612 OrderOnly: orderOnlyDeps,
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800613 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800614 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800615
Dan Willemsen322acaf2016-01-12 23:07:05 -0800616 a.installFiles = append(a.installFiles, fullInstallPath)
617 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700618 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700619 return fullInstallPath
620}
621
Colin Crossa2344662016-03-24 13:14:12 -0700622func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700623 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800624}
625
Colin Cross3854a602016-01-11 12:49:11 -0800626func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
627 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700628 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800629
Colin Cross12fc4972016-01-11 12:49:11 -0800630 if a.Host() || !a.AConfig().SkipDeviceInstall() {
631 a.ModuleBuild(pctx, ModuleBuildParams{
632 Rule: Symlink,
633 Output: fullInstallPath,
634 OrderOnly: Paths{srcPath},
635 Default: !a.AConfig().EmbeddedInMake(),
636 Args: map[string]string{
637 "fromPath": srcPath.String(),
638 },
639 })
Colin Cross3854a602016-01-11 12:49:11 -0800640
Colin Cross12fc4972016-01-11 12:49:11 -0800641 a.installFiles = append(a.installFiles, fullInstallPath)
642 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
643 }
Colin Cross3854a602016-01-11 12:49:11 -0800644 return fullInstallPath
645}
646
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700647func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800648 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
649}
650
Colin Cross3f40fa42015-01-30 17:27:36 -0800651type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700652 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800653}
654
655func isFileInstaller(m blueprint.Module) bool {
656 _, ok := m.(fileInstaller)
657 return ok
658}
659
660func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700661 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800662 return ok
663}
Colin Crossfce53272015-04-08 11:21:40 -0700664
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700665func findStringInSlice(str string, slice []string) int {
666 for i, s := range slice {
667 if s == str {
668 return i
Colin Crossfce53272015-04-08 11:21:40 -0700669 }
670 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700671 return -1
672}
673
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700674func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
675 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700676 for i, e := range excludes {
677 j := findStringInSlice(e, srcFiles)
678 if j != -1 {
679 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
680 }
681
682 excludes[i] = filepath.Join(prefix, e)
683 }
684
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700685 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700686 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700687 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700688 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700689 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700690 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700691 }
692 }
693
694 return globbedSrcFiles
695}
696
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700697func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
698 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700699 if err != nil {
700 ctx.ModuleErrorf("glob: %s", err.Error())
701 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700702 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700703}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700704
Colin Cross463a90e2015-06-17 14:20:06 -0700705func init() {
706 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
707}
708
Colin Cross1f8c52b2015-06-16 16:38:17 -0700709func BuildTargetSingleton() blueprint.Singleton {
710 return &buildTargetSingleton{}
711}
712
713type buildTargetSingleton struct{}
714
715func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
716 checkbuildDeps := []string{}
717
718 dirModules := make(map[string][]string)
719
720 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700721 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700722 blueprintDir := a.base().blueprintDir
723 installTarget := a.base().installTarget
724 checkbuildTarget := a.base().checkbuildTarget
725
726 if checkbuildTarget != "" {
727 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
728 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
729 }
730
731 if installTarget != "" {
732 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
733 }
734 }
735 })
736
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800737 suffix := ""
738 if ctx.Config().(Config).EmbeddedInMake() {
739 suffix = "-soong"
740 }
741
Colin Cross1f8c52b2015-06-16 16:38:17 -0700742 // Create a top-level checkbuild target that depends on all modules
743 ctx.Build(pctx, blueprint.BuildParams{
744 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800745 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700746 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700747 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700748 })
749
750 // Create a mm/<directory> target that depends on all modules in a directory
751 dirs := sortedKeys(dirModules)
752 for _, dir := range dirs {
753 ctx.Build(pctx, blueprint.BuildParams{
754 Rule: blueprint.Phony,
755 Outputs: []string{filepath.Join("mm", dir)},
756 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800757 // HACK: checkbuild should be an optional build, but force it
758 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800759 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700760 })
761 }
762}
Colin Crossd779da42015-12-17 18:00:23 -0800763
764type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700765 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800766 ctx interface {
767 ModuleName(blueprint.Module) string
768 ModuleSubDir(blueprint.Module) string
769 }
770}
771
772func (s AndroidModulesByName) Len() int { return len(s.slice) }
773func (s AndroidModulesByName) Less(i, j int) bool {
774 mi, mj := s.slice[i], s.slice[j]
775 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
776
777 if ni != nj {
778 return ni < nj
779 } else {
780 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
781 }
782}
783func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }