blob: 41499ca7d871d60bda1e3618437c089b8369275a [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
28var (
29 DeviceSharedLibrary = "shared_library"
30 DeviceStaticLibrary = "static_library"
31 DeviceExecutable = "executable"
32 HostSharedLibrary = "host_shared_library"
33 HostStaticLibrary = "host_static_library"
34 HostExecutable = "host_executable"
35)
36
Dan Willemsen34cc69e2015-09-23 15:26:20 -070037type ModuleBuildParams struct {
38 Rule blueprint.Rule
39 Output WritablePath
40 Outputs WritablePaths
41 Input Path
42 Inputs Paths
43 Implicit Path
44 Implicits Paths
45 OrderOnly Paths
46 Default bool
47 Args map[string]string
48}
49
Colin Crossf6566ed2015-03-24 11:13:38 -070050type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070051 Target() Target
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 Cross1332b002015-04-07 17:11:30 -070058 AConfig() Config
Dan Willemsen782a2d12015-12-21 14:55:28 -080059 Proprietary() bool
60 InstallInData() bool
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 Cross3f40fa42015-01-30 17:27:36 -080085}
86
Colin Cross635c3b02016-05-18 15:37:25 -070087type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080088 blueprint.Module
89
Colin Cross635c3b02016-05-18 15:37:25 -070090 GenerateAndroidBuildActions(ModuleContext)
Colin Cross3f40fa42015-01-30 17:27:36 -080091
Colin Cross635c3b02016-05-18 15:37:25 -070092 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -080093 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -070094 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -080095 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080096}
97
Colin Cross3f40fa42015-01-30 17:27:36 -080098type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -070099 Name string
100 Deps []string
101 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800102
Dan Willemsen0effe062015-11-30 16:06:01 -0800103 // emit build rules for this module
104 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800105
Colin Cross7d5136f2015-05-11 13:39:40 -0700106 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800107 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
108 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
109 // platform
110 Compile_multilib string
111
Dan Willemsen782a2d12015-12-21 14:55:28 -0800112 // whether this is a proprietary vendor module, and should be installed into /vendor
113 Proprietary bool
114
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700115 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
116 // file
117 Logtags []string
118
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700119 // init.rc files to be installed if this module is installed
120 Init_rc []string
121
Colin Crossa1ad8d12016-06-01 17:09:44 -0700122 // Set by TargetMutator
123 CompileTarget Target `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800124
125 // Set by InitAndroidModule
126 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
127}
128
129type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700130 Host_supported *bool
131 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800132}
133
Colin Crossc472d572015-03-17 15:06:21 -0700134type Multilib string
135
136const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700137 MultilibBoth Multilib = "both"
138 MultilibFirst Multilib = "first"
139 MultilibCommon Multilib = "common"
140 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700141)
142
Colin Crossa1ad8d12016-06-01 17:09:44 -0700143type HostOrDeviceSupported int
144
145const (
146 _ HostOrDeviceSupported = iota
147 HostSupported
148 DeviceSupported
149 HostAndDeviceSupported
150 HostAndDeviceDefault
151)
152
Colin Cross635c3b02016-05-18 15:37:25 -0700153func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800154 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
155
156 base := m.base()
157 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700158
Colin Cross7f64b6d2015-07-09 13:57:48 -0700159 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700160
161 return m, propertyStructs
162}
163
Colin Cross635c3b02016-05-18 15:37:25 -0700164func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700165 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
166
167 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
168
169 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800170 base.commonProperties.HostOrDeviceSupported = hod
Colin Crosscfad1192015-11-02 16:43:11 -0800171 base.commonProperties.Compile_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800172
Dan Willemsen218f6562015-07-08 18:13:11 -0700173 switch hod {
174 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800175 // Default to module to device supported, host not supported, can override in module
176 // properties
Colin Crossa4190c12016-07-12 13:11:25 -0700177 base.hostAndDeviceProperties.Device_supported = boolPtr(true)
Dan Willemsen218f6562015-07-08 18:13:11 -0700178 fallthrough
179 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800180 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
181 }
182
Colin Crosscfad1192015-11-02 16:43:11 -0800183 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800184}
185
186// A AndroidModuleBase object contains the properties that are common to all Android
187// modules. It should be included as an anonymous field in every module
188// struct definition. InitAndroidModule should then be called from the module's
189// factory function, and the return values from InitAndroidModule should be
190// returned from the factory function.
191//
192// The AndroidModuleBase type is responsible for implementing the
193// GenerateBuildActions method to support the blueprint.Module interface. This
194// method will then call the module's GenerateAndroidBuildActions method once
195// for each build variant that is to be built. GenerateAndroidBuildActions is
196// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
197// AndroidModuleContext exposes extra functionality specific to the Android build
198// system including details about the particular build variant that is to be
199// generated.
200//
201// For example:
202//
203// import (
204// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700205// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800206// )
207//
208// type myModule struct {
209// common.AndroidModuleBase
210// properties struct {
211// MyProperty string
212// }
213// }
214//
215// func NewMyModule() (blueprint.Module, []interface{}) {
216// m := &myModule{}
217// return common.InitAndroidModule(m, &m.properties)
218// }
219//
220// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
221// // Get the CPU architecture for the current build variant.
222// variantArch := ctx.Arch()
223//
224// // ...
225// }
Colin Cross635c3b02016-05-18 15:37:25 -0700226type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800227 // Putting the curiously recurring thing pointing to the thing that contains
228 // the thing pattern to good use.
Colin Cross635c3b02016-05-18 15:37:25 -0700229 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800230
231 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700232 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800233 hostAndDeviceProperties hostAndDeviceProperties
234 generalProperties []interface{}
235 archProperties []*archProperties
236
237 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700238 installFiles Paths
239 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700240
241 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
242 // Only set on the final variant of each module
243 installTarget string
244 checkbuildTarget string
245 blueprintDir string
Colin Cross3f40fa42015-01-30 17:27:36 -0800246}
247
Colin Cross635c3b02016-05-18 15:37:25 -0700248func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800249 return a
250}
251
Colin Crossa1ad8d12016-06-01 17:09:44 -0700252func (a *ModuleBase) SetTarget(target Target) {
253 a.commonProperties.CompileTarget = target
Colin Crossd3ba0392015-05-07 14:11:29 -0700254}
255
Colin Crossa1ad8d12016-06-01 17:09:44 -0700256func (a *ModuleBase) Target() Target {
257 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800258}
259
Colin Crossa1ad8d12016-06-01 17:09:44 -0700260func (a *ModuleBase) Os() OsType {
261 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800262}
263
Colin Cross635c3b02016-05-18 15:37:25 -0700264func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700265 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800266}
267
Colin Cross635c3b02016-05-18 15:37:25 -0700268func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700269 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800270}
271
Colin Crossa1ad8d12016-06-01 17:09:44 -0700272func (a *ModuleBase) OsClassSupported() []OsClass {
273 switch a.commonProperties.HostOrDeviceSupported {
274 case HostSupported:
275 // TODO(ccross): explicitly mark host cross support
276 return []OsClass{Host, HostCross}
277 case DeviceSupported:
278 return []OsClass{Device}
279 case HostAndDeviceSupported:
280 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700281 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700282 supported = append(supported, Host, HostCross)
283 }
Colin Crossa4190c12016-07-12 13:11:25 -0700284 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700285 supported = append(supported, Device)
286 }
287 return supported
288 default:
289 return nil
290 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800291}
292
Colin Cross635c3b02016-05-18 15:37:25 -0700293func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800294 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
295 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700296 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800297}
298
Colin Cross635c3b02016-05-18 15:37:25 -0700299func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800300 if a.commonProperties.Enabled == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700301 return a.Os().Class != HostCross
Dan Willemsen490fd492015-11-24 17:53:15 -0800302 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800303 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800304}
305
Colin Cross635c3b02016-05-18 15:37:25 -0700306func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700307 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800308
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700309 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800310 ctx.VisitDepsDepthFirstIf(isFileInstaller,
311 func(m blueprint.Module) {
312 fileInstaller := m.(fileInstaller)
313 files := fileInstaller.filesToInstall()
314 result = append(result, files...)
315 })
316
317 return result
318}
319
Colin Cross635c3b02016-05-18 15:37:25 -0700320func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800321 return a.installFiles
322}
323
Colin Cross635c3b02016-05-18 15:37:25 -0700324func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800325 return p.noAddressSanitizer
326}
327
Colin Cross635c3b02016-05-18 15:37:25 -0700328func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800329 return false
330}
331
Colin Cross635c3b02016-05-18 15:37:25 -0700332func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
333 if a != ctx.FinalModule().(Module).base() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800334 return
335 }
336
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700337 allInstalledFiles := Paths{}
338 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800339 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700340 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700341 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
342 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800343 })
344
Colin Cross9454bfa2015-03-17 13:24:18 -0700345 deps := []string{}
346
Colin Cross3f40fa42015-01-30 17:27:36 -0800347 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700348 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800349 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700350 Rule: blueprint.Phony,
351 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700352 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800353 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700354 })
355 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700356 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700357 }
358
359 if len(allCheckbuildFiles) > 0 {
360 name := ctx.ModuleName() + "-checkbuild"
361 ctx.Build(pctx, blueprint.BuildParams{
362 Rule: blueprint.Phony,
363 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700364 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700365 Optional: true,
366 })
367 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700368 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700369 }
370
371 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800372 suffix := ""
373 if ctx.Config().(Config).EmbeddedInMake() {
374 suffix = "-soong"
375 }
376
Colin Cross9454bfa2015-03-17 13:24:18 -0700377 ctx.Build(pctx, blueprint.BuildParams{
378 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800379 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700380 Implicits: deps,
381 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800382 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700383
384 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800385 }
386}
387
Colin Cross635c3b02016-05-18 15:37:25 -0700388func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700389 return androidBaseContextImpl{
Colin Crossa1ad8d12016-06-01 17:09:44 -0700390 target: a.commonProperties.CompileTarget,
Dan Willemsen782a2d12015-12-21 14:55:28 -0800391 proprietary: a.commonProperties.Proprietary,
392 config: ctx.Config().(Config),
393 installInData: a.module.InstallInData(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800394 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800395}
396
Colin Cross635c3b02016-05-18 15:37:25 -0700397func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800398 androidCtx := &androidModuleContext{
Colin Cross6362e272015-10-29 15:25:03 -0700399 ModuleContext: ctx,
400 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
401 installDeps: a.computeInstallDeps(ctx),
402 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800403 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800404 }
405
Dan Willemsen0effe062015-11-30 16:06:01 -0800406 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800407 return
408 }
409
410 a.module.GenerateAndroidBuildActions(androidCtx)
411 if ctx.Failed() {
412 return
413 }
414
Colin Crossc9404352015-03-26 16:10:12 -0700415 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
416 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
417
Colin Cross3f40fa42015-01-30 17:27:36 -0800418 a.generateModuleTarget(ctx)
419 if ctx.Failed() {
420 return
421 }
422}
423
Colin Crossf6566ed2015-03-24 11:13:38 -0700424type androidBaseContextImpl struct {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700425 target Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800426 debug bool
427 config Config
428 proprietary bool
429 installInData bool
Colin Crossf6566ed2015-03-24 11:13:38 -0700430}
431
Colin Cross3f40fa42015-01-30 17:27:36 -0800432type androidModuleContext struct {
433 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700434 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700435 installDeps Paths
436 installFiles Paths
437 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800438 missingDeps []string
439}
440
441func (a *androidModuleContext) ninjaError(outputs []string, err error) {
442 a.ModuleContext.Build(pctx, blueprint.BuildParams{
443 Rule: ErrorRule,
444 Outputs: outputs,
445 Optional: true,
446 Args: map[string]string{
447 "error": err.Error(),
448 },
449 })
450 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800451}
452
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800453func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Crosse2c48742016-04-27 13:47:35 -0700454 if a.missingDeps != nil && params.Rule != globRule {
Colin Cross6ff51382015-12-17 16:39:19 -0800455 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
456 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
457 return
458 }
459
Colin Cross3f40fa42015-01-30 17:27:36 -0800460 params.Optional = true
461 a.ModuleContext.Build(pctx, params)
462}
463
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700464func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
465 bparams := blueprint.BuildParams{
466 Rule: params.Rule,
467 Outputs: params.Outputs.Strings(),
468 Inputs: params.Inputs.Strings(),
469 Implicits: params.Implicits.Strings(),
470 OrderOnly: params.OrderOnly.Strings(),
471 Args: params.Args,
472 Optional: !params.Default,
473 }
474
475 if params.Output != nil {
476 bparams.Outputs = append(bparams.Outputs, params.Output.String())
477 }
478 if params.Input != nil {
479 bparams.Inputs = append(bparams.Inputs, params.Input.String())
480 }
481 if params.Implicit != nil {
482 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
483 }
484
Colin Cross6ff51382015-12-17 16:39:19 -0800485 if a.missingDeps != nil {
486 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
487 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
488 return
489 }
490
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700491 a.ModuleContext.Build(pctx, bparams)
492}
493
Colin Cross6ff51382015-12-17 16:39:19 -0800494func (a *androidModuleContext) GetMissingDependencies() []string {
495 return a.missingDeps
496}
497
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800498func (a *androidModuleContext) AddMissingDependencies(deps []string) {
499 if deps != nil {
500 a.missingDeps = append(a.missingDeps, deps...)
501 }
502}
503
Colin Crossa1ad8d12016-06-01 17:09:44 -0700504func (a *androidBaseContextImpl) Target() Target {
505 return a.target
506}
507
Colin Crossf6566ed2015-03-24 11:13:38 -0700508func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700509 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800510}
511
Colin Crossa1ad8d12016-06-01 17:09:44 -0700512func (a *androidBaseContextImpl) Os() OsType {
513 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800514}
515
Colin Crossf6566ed2015-03-24 11:13:38 -0700516func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700517 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700518}
519
520func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700521 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700522}
523
Colin Cross0af4b842015-04-30 16:36:18 -0700524func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700525 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700526}
527
Colin Crossf6566ed2015-03-24 11:13:38 -0700528func (a *androidBaseContextImpl) Debug() bool {
529 return a.debug
530}
531
Colin Cross1332b002015-04-07 17:11:30 -0700532func (a *androidBaseContextImpl) AConfig() Config {
533 return a.config
534}
535
Dan Willemsen782a2d12015-12-21 14:55:28 -0800536func (a *androidBaseContextImpl) Proprietary() bool {
537 return a.proprietary
538}
539
540func (a *androidBaseContextImpl) InstallInData() bool {
541 return a.installInData
542}
543
544func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700545 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700546
Dan Willemsen782a2d12015-12-21 14:55:28 -0800547 fullInstallPath := installPath.Join(a, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800548
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800549 if a.Host() || !a.AConfig().SkipDeviceInstall() {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800550 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700551
Dan Willemsen322acaf2016-01-12 23:07:05 -0800552 a.ModuleBuild(pctx, ModuleBuildParams{
553 Rule: Cp,
554 Output: fullInstallPath,
555 Input: srcPath,
556 OrderOnly: Paths(deps),
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800557 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800558 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800559
Dan Willemsen322acaf2016-01-12 23:07:05 -0800560 a.installFiles = append(a.installFiles, fullInstallPath)
561 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700562 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700563 return fullInstallPath
564}
565
Colin Crossa2344662016-03-24 13:14:12 -0700566func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700567 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800568}
569
Colin Cross3854a602016-01-11 12:49:11 -0800570func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
571 fullInstallPath := installPath.Join(a, name)
572
Colin Cross12fc4972016-01-11 12:49:11 -0800573 if a.Host() || !a.AConfig().SkipDeviceInstall() {
574 a.ModuleBuild(pctx, ModuleBuildParams{
575 Rule: Symlink,
576 Output: fullInstallPath,
577 OrderOnly: Paths{srcPath},
578 Default: !a.AConfig().EmbeddedInMake(),
579 Args: map[string]string{
580 "fromPath": srcPath.String(),
581 },
582 })
Colin Cross3854a602016-01-11 12:49:11 -0800583
Colin Cross12fc4972016-01-11 12:49:11 -0800584 a.installFiles = append(a.installFiles, fullInstallPath)
585 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
586 }
Colin Cross3854a602016-01-11 12:49:11 -0800587 return fullInstallPath
588}
589
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700590func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800591 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
592}
593
Colin Cross3f40fa42015-01-30 17:27:36 -0800594type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700595 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800596}
597
598func isFileInstaller(m blueprint.Module) bool {
599 _, ok := m.(fileInstaller)
600 return ok
601}
602
603func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700604 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 return ok
606}
Colin Crossfce53272015-04-08 11:21:40 -0700607
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700608func findStringInSlice(str string, slice []string) int {
609 for i, s := range slice {
610 if s == str {
611 return i
Colin Crossfce53272015-04-08 11:21:40 -0700612 }
613 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700614 return -1
615}
616
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700617func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
618 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700619 for i, e := range excludes {
620 j := findStringInSlice(e, srcFiles)
621 if j != -1 {
622 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
623 }
624
625 excludes[i] = filepath.Join(prefix, e)
626 }
627
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700628 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700629 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700630 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700631 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700632 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700633 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700634 }
635 }
636
637 return globbedSrcFiles
638}
639
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700640func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
641 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700642 if err != nil {
643 ctx.ModuleErrorf("glob: %s", err.Error())
644 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700645 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700646}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700647
Colin Cross463a90e2015-06-17 14:20:06 -0700648func init() {
649 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
650}
651
Colin Cross1f8c52b2015-06-16 16:38:17 -0700652func BuildTargetSingleton() blueprint.Singleton {
653 return &buildTargetSingleton{}
654}
655
656type buildTargetSingleton struct{}
657
658func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
659 checkbuildDeps := []string{}
660
661 dirModules := make(map[string][]string)
662
663 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700664 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700665 blueprintDir := a.base().blueprintDir
666 installTarget := a.base().installTarget
667 checkbuildTarget := a.base().checkbuildTarget
668
669 if checkbuildTarget != "" {
670 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
671 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
672 }
673
674 if installTarget != "" {
675 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
676 }
677 }
678 })
679
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800680 suffix := ""
681 if ctx.Config().(Config).EmbeddedInMake() {
682 suffix = "-soong"
683 }
684
Colin Cross1f8c52b2015-06-16 16:38:17 -0700685 // Create a top-level checkbuild target that depends on all modules
686 ctx.Build(pctx, blueprint.BuildParams{
687 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800688 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700689 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700690 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700691 })
692
693 // Create a mm/<directory> target that depends on all modules in a directory
694 dirs := sortedKeys(dirModules)
695 for _, dir := range dirs {
696 ctx.Build(pctx, blueprint.BuildParams{
697 Rule: blueprint.Phony,
698 Outputs: []string{filepath.Join("mm", dir)},
699 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800700 // HACK: checkbuild should be an optional build, but force it
701 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800702 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700703 })
704 }
705}
Colin Crossd779da42015-12-17 18:00:23 -0800706
707type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700708 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800709 ctx interface {
710 ModuleName(blueprint.Module) string
711 ModuleSubDir(blueprint.Module) string
712 }
713}
714
715func (s AndroidModulesByName) Len() int { return len(s.slice) }
716func (s AndroidModulesByName) Less(i, j int) bool {
717 mi, mj := s.slice[i], s.slice[j]
718 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
719
720 if ni != nj {
721 return ni < nj
722 } else {
723 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
724 }
725}
726func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }