blob: f1af53a534a764c43b4ede3f5a8290a6b92c8b5b [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
Colin Crossf6566ed2015-03-24 11:13:38 -070059}
60
Colin Cross635c3b02016-05-18 15:37:25 -070061type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070062 blueprint.BaseModuleContext
63 androidBaseContext
64}
65
Colin Cross635c3b02016-05-18 15:37:25 -070066type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080067 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070068 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080069
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070 // Similar to Build, but takes Paths instead of []string,
71 // and performs more verification.
72 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070073
Dan Willemsen34cc69e2015-09-23 15:26:20 -070074 ExpandSources(srcFiles, excludes []string) Paths
75 Glob(outDir, globPattern string, excludes []string) Paths
76
Colin Crossa2344662016-03-24 13:14:12 -070077 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
78 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080079 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080081
82 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070083
84 Proprietary() bool
85 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080086}
87
Colin Cross635c3b02016-05-18 15:37:25 -070088type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080089 blueprint.Module
90
Colin Cross635c3b02016-05-18 15:37:25 -070091 GenerateAndroidBuildActions(ModuleContext)
Colin Cross3f40fa42015-01-30 17:27:36 -080092
Colin Cross635c3b02016-05-18 15:37:25 -070093 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -080094 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -070095 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -080096 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080097}
98
Colin Cross3f40fa42015-01-30 17:27:36 -080099type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700100 Name string
101 Deps []string
102 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800103
Dan Willemsen0effe062015-11-30 16:06:01 -0800104 // emit build rules for this module
105 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800106
Colin Cross7d5136f2015-05-11 13:39:40 -0700107 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800108 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
109 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
110 // platform
111 Compile_multilib string
112
Dan Willemsen782a2d12015-12-21 14:55:28 -0800113 // whether this is a proprietary vendor module, and should be installed into /vendor
114 Proprietary bool
115
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700116 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
117 // file
118 Logtags []string
119
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700120 // init.rc files to be installed if this module is installed
121 Init_rc []string
122
Colin Crossa1ad8d12016-06-01 17:09:44 -0700123 // Set by TargetMutator
124 CompileTarget Target `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800125
126 // Set by InitAndroidModule
127 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
128}
129
130type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700131 Host_supported *bool
132 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800133}
134
Colin Crossc472d572015-03-17 15:06:21 -0700135type Multilib string
136
137const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700138 MultilibBoth Multilib = "both"
139 MultilibFirst Multilib = "first"
140 MultilibCommon Multilib = "common"
141 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700142)
143
Colin Crossa1ad8d12016-06-01 17:09:44 -0700144type HostOrDeviceSupported int
145
146const (
147 _ HostOrDeviceSupported = iota
148 HostSupported
149 DeviceSupported
150 HostAndDeviceSupported
151 HostAndDeviceDefault
152)
153
Colin Cross635c3b02016-05-18 15:37:25 -0700154func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800155 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
156
157 base := m.base()
158 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700159
Colin Cross7f64b6d2015-07-09 13:57:48 -0700160 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700161
162 return m, propertyStructs
163}
164
Colin Cross635c3b02016-05-18 15:37:25 -0700165func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700166 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
167
168 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
169
170 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800171 base.commonProperties.HostOrDeviceSupported = hod
Colin Crosscfad1192015-11-02 16:43:11 -0800172 base.commonProperties.Compile_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800173
Dan Willemsen218f6562015-07-08 18:13:11 -0700174 switch hod {
175 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800176 // Default to module to device supported, host not supported, can override in module
177 // properties
Colin Crossa4190c12016-07-12 13:11:25 -0700178 base.hostAndDeviceProperties.Device_supported = boolPtr(true)
Dan Willemsen218f6562015-07-08 18:13:11 -0700179 fallthrough
180 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800181 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
182 }
183
Colin Crosscfad1192015-11-02 16:43:11 -0800184 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800185}
186
187// A AndroidModuleBase object contains the properties that are common to all Android
188// modules. It should be included as an anonymous field in every module
189// struct definition. InitAndroidModule should then be called from the module's
190// factory function, and the return values from InitAndroidModule should be
191// returned from the factory function.
192//
193// The AndroidModuleBase type is responsible for implementing the
194// GenerateBuildActions method to support the blueprint.Module interface. This
195// method will then call the module's GenerateAndroidBuildActions method once
196// for each build variant that is to be built. GenerateAndroidBuildActions is
197// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
198// AndroidModuleContext exposes extra functionality specific to the Android build
199// system including details about the particular build variant that is to be
200// generated.
201//
202// For example:
203//
204// import (
205// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700206// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800207// )
208//
209// type myModule struct {
210// common.AndroidModuleBase
211// properties struct {
212// MyProperty string
213// }
214// }
215//
216// func NewMyModule() (blueprint.Module, []interface{}) {
217// m := &myModule{}
218// return common.InitAndroidModule(m, &m.properties)
219// }
220//
221// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
222// // Get the CPU architecture for the current build variant.
223// variantArch := ctx.Arch()
224//
225// // ...
226// }
Colin Cross635c3b02016-05-18 15:37:25 -0700227type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800228 // Putting the curiously recurring thing pointing to the thing that contains
229 // the thing pattern to good use.
Colin Cross635c3b02016-05-18 15:37:25 -0700230 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800231
232 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700233 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800234 hostAndDeviceProperties hostAndDeviceProperties
235 generalProperties []interface{}
236 archProperties []*archProperties
237
238 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700239 installFiles Paths
240 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700241
242 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
243 // Only set on the final variant of each module
244 installTarget string
245 checkbuildTarget string
246 blueprintDir string
Colin Cross3f40fa42015-01-30 17:27:36 -0800247}
248
Colin Cross635c3b02016-05-18 15:37:25 -0700249func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800250 return a
251}
252
Colin Crossa1ad8d12016-06-01 17:09:44 -0700253func (a *ModuleBase) SetTarget(target Target) {
254 a.commonProperties.CompileTarget = target
Colin Crossd3ba0392015-05-07 14:11:29 -0700255}
256
Colin Crossa1ad8d12016-06-01 17:09:44 -0700257func (a *ModuleBase) Target() Target {
258 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800259}
260
Colin Crossa1ad8d12016-06-01 17:09:44 -0700261func (a *ModuleBase) Os() OsType {
262 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800263}
264
Colin Cross635c3b02016-05-18 15:37:25 -0700265func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700266 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800267}
268
Colin Cross635c3b02016-05-18 15:37:25 -0700269func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700270 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800271}
272
Colin Crossa1ad8d12016-06-01 17:09:44 -0700273func (a *ModuleBase) OsClassSupported() []OsClass {
274 switch a.commonProperties.HostOrDeviceSupported {
275 case HostSupported:
276 // TODO(ccross): explicitly mark host cross support
277 return []OsClass{Host, HostCross}
278 case DeviceSupported:
279 return []OsClass{Device}
280 case HostAndDeviceSupported:
281 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700282 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700283 supported = append(supported, Host, HostCross)
284 }
Colin Crossa4190c12016-07-12 13:11:25 -0700285 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700286 supported = append(supported, Device)
287 }
288 return supported
289 default:
290 return nil
291 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800292}
293
Colin Cross635c3b02016-05-18 15:37:25 -0700294func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800295 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
296 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700297 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800298}
299
Colin Cross635c3b02016-05-18 15:37:25 -0700300func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800301 if a.commonProperties.Enabled == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700302 return a.Os().Class != HostCross
Dan Willemsen490fd492015-11-24 17:53:15 -0800303 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800304 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800305}
306
Colin Cross635c3b02016-05-18 15:37:25 -0700307func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700308 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800309
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700310 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800311 ctx.VisitDepsDepthFirstIf(isFileInstaller,
312 func(m blueprint.Module) {
313 fileInstaller := m.(fileInstaller)
314 files := fileInstaller.filesToInstall()
315 result = append(result, files...)
316 })
317
318 return result
319}
320
Colin Cross635c3b02016-05-18 15:37:25 -0700321func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800322 return a.installFiles
323}
324
Colin Cross635c3b02016-05-18 15:37:25 -0700325func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800326 return p.noAddressSanitizer
327}
328
Colin Cross635c3b02016-05-18 15:37:25 -0700329func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800330 return false
331}
332
Colin Cross635c3b02016-05-18 15:37:25 -0700333func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
334 if a != ctx.FinalModule().(Module).base() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800335 return
336 }
337
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700338 allInstalledFiles := Paths{}
339 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800340 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700341 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700342 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
343 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800344 })
345
Colin Cross9454bfa2015-03-17 13:24:18 -0700346 deps := []string{}
347
Colin Cross3f40fa42015-01-30 17:27:36 -0800348 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700349 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800350 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700351 Rule: blueprint.Phony,
352 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700353 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800354 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700355 })
356 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700357 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700358 }
359
360 if len(allCheckbuildFiles) > 0 {
361 name := ctx.ModuleName() + "-checkbuild"
362 ctx.Build(pctx, blueprint.BuildParams{
363 Rule: blueprint.Phony,
364 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700365 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700366 Optional: true,
367 })
368 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700369 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700370 }
371
372 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800373 suffix := ""
374 if ctx.Config().(Config).EmbeddedInMake() {
375 suffix = "-soong"
376 }
377
Colin Cross9454bfa2015-03-17 13:24:18 -0700378 ctx.Build(pctx, blueprint.BuildParams{
379 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800380 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700381 Implicits: deps,
382 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800383 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700384
385 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800386 }
387}
388
Colin Cross635c3b02016-05-18 15:37:25 -0700389func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700390 return androidBaseContextImpl{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700391 target: a.commonProperties.CompileTarget,
392 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800394}
395
Colin Cross635c3b02016-05-18 15:37:25 -0700396func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800397 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700398 module: a.module,
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 Cross8d8f8e22016-08-03 11:57:50 -0700425 target Target
426 debug bool
427 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700428}
429
Colin Cross3f40fa42015-01-30 17:27:36 -0800430type androidModuleContext struct {
431 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700432 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700433 installDeps Paths
434 installFiles Paths
435 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800436 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700437 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800438}
439
440func (a *androidModuleContext) ninjaError(outputs []string, err error) {
441 a.ModuleContext.Build(pctx, blueprint.BuildParams{
442 Rule: ErrorRule,
443 Outputs: outputs,
444 Optional: true,
445 Args: map[string]string{
446 "error": err.Error(),
447 },
448 })
449 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800450}
451
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800452func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Crosse2c48742016-04-27 13:47:35 -0700453 if a.missingDeps != nil && params.Rule != globRule {
Colin Cross6ff51382015-12-17 16:39:19 -0800454 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
455 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
456 return
457 }
458
Colin Cross3f40fa42015-01-30 17:27:36 -0800459 params.Optional = true
460 a.ModuleContext.Build(pctx, params)
461}
462
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700463func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
464 bparams := blueprint.BuildParams{
465 Rule: params.Rule,
466 Outputs: params.Outputs.Strings(),
467 Inputs: params.Inputs.Strings(),
468 Implicits: params.Implicits.Strings(),
469 OrderOnly: params.OrderOnly.Strings(),
470 Args: params.Args,
471 Optional: !params.Default,
472 }
473
474 if params.Output != nil {
475 bparams.Outputs = append(bparams.Outputs, params.Output.String())
476 }
477 if params.Input != nil {
478 bparams.Inputs = append(bparams.Inputs, params.Input.String())
479 }
480 if params.Implicit != nil {
481 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
482 }
483
Colin Cross6ff51382015-12-17 16:39:19 -0800484 if a.missingDeps != nil {
485 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
486 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
487 return
488 }
489
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700490 a.ModuleContext.Build(pctx, bparams)
491}
492
Colin Cross6ff51382015-12-17 16:39:19 -0800493func (a *androidModuleContext) GetMissingDependencies() []string {
494 return a.missingDeps
495}
496
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800497func (a *androidModuleContext) AddMissingDependencies(deps []string) {
498 if deps != nil {
499 a.missingDeps = append(a.missingDeps, deps...)
500 }
501}
502
Colin Crossa1ad8d12016-06-01 17:09:44 -0700503func (a *androidBaseContextImpl) Target() Target {
504 return a.target
505}
506
Colin Crossf6566ed2015-03-24 11:13:38 -0700507func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700508 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800509}
510
Colin Crossa1ad8d12016-06-01 17:09:44 -0700511func (a *androidBaseContextImpl) Os() OsType {
512 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800513}
514
Colin Crossf6566ed2015-03-24 11:13:38 -0700515func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700516 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700517}
518
519func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700520 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700521}
522
Colin Cross0af4b842015-04-30 16:36:18 -0700523func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700524 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700525}
526
Colin Crossf6566ed2015-03-24 11:13:38 -0700527func (a *androidBaseContextImpl) Debug() bool {
528 return a.debug
529}
530
Colin Cross1332b002015-04-07 17:11:30 -0700531func (a *androidBaseContextImpl) AConfig() Config {
532 return a.config
533}
534
Colin Cross8d8f8e22016-08-03 11:57:50 -0700535func (a *androidModuleContext) Proprietary() bool {
536 return a.module.base().commonProperties.Proprietary
Dan Willemsen782a2d12015-12-21 14:55:28 -0800537}
538
Colin Cross8d8f8e22016-08-03 11:57:50 -0700539func (a *androidModuleContext) InstallInData() bool {
540 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800541}
542
543func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700544 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700545
Dan Willemsen782a2d12015-12-21 14:55:28 -0800546 fullInstallPath := installPath.Join(a, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800547
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800548 if a.Host() || !a.AConfig().SkipDeviceInstall() {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800549 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700550
Dan Willemsen322acaf2016-01-12 23:07:05 -0800551 a.ModuleBuild(pctx, ModuleBuildParams{
552 Rule: Cp,
553 Output: fullInstallPath,
554 Input: srcPath,
555 OrderOnly: Paths(deps),
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800556 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800557 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800558
Dan Willemsen322acaf2016-01-12 23:07:05 -0800559 a.installFiles = append(a.installFiles, fullInstallPath)
560 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700561 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700562 return fullInstallPath
563}
564
Colin Crossa2344662016-03-24 13:14:12 -0700565func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700566 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800567}
568
Colin Cross3854a602016-01-11 12:49:11 -0800569func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
570 fullInstallPath := installPath.Join(a, name)
571
Colin Cross12fc4972016-01-11 12:49:11 -0800572 if a.Host() || !a.AConfig().SkipDeviceInstall() {
573 a.ModuleBuild(pctx, ModuleBuildParams{
574 Rule: Symlink,
575 Output: fullInstallPath,
576 OrderOnly: Paths{srcPath},
577 Default: !a.AConfig().EmbeddedInMake(),
578 Args: map[string]string{
579 "fromPath": srcPath.String(),
580 },
581 })
Colin Cross3854a602016-01-11 12:49:11 -0800582
Colin Cross12fc4972016-01-11 12:49:11 -0800583 a.installFiles = append(a.installFiles, fullInstallPath)
584 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
585 }
Colin Cross3854a602016-01-11 12:49:11 -0800586 return fullInstallPath
587}
588
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700589func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800590 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
591}
592
Colin Cross3f40fa42015-01-30 17:27:36 -0800593type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700594 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800595}
596
597func isFileInstaller(m blueprint.Module) bool {
598 _, ok := m.(fileInstaller)
599 return ok
600}
601
602func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700603 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800604 return ok
605}
Colin Crossfce53272015-04-08 11:21:40 -0700606
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700607func findStringInSlice(str string, slice []string) int {
608 for i, s := range slice {
609 if s == str {
610 return i
Colin Crossfce53272015-04-08 11:21:40 -0700611 }
612 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700613 return -1
614}
615
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700616func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
617 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700618 for i, e := range excludes {
619 j := findStringInSlice(e, srcFiles)
620 if j != -1 {
621 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
622 }
623
624 excludes[i] = filepath.Join(prefix, e)
625 }
626
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700627 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700628 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700629 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700630 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700631 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700632 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700633 }
634 }
635
636 return globbedSrcFiles
637}
638
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700639func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
640 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700641 if err != nil {
642 ctx.ModuleErrorf("glob: %s", err.Error())
643 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700644 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700645}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700646
Colin Cross463a90e2015-06-17 14:20:06 -0700647func init() {
648 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
649}
650
Colin Cross1f8c52b2015-06-16 16:38:17 -0700651func BuildTargetSingleton() blueprint.Singleton {
652 return &buildTargetSingleton{}
653}
654
655type buildTargetSingleton struct{}
656
657func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
658 checkbuildDeps := []string{}
659
660 dirModules := make(map[string][]string)
661
662 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700663 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700664 blueprintDir := a.base().blueprintDir
665 installTarget := a.base().installTarget
666 checkbuildTarget := a.base().checkbuildTarget
667
668 if checkbuildTarget != "" {
669 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
670 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
671 }
672
673 if installTarget != "" {
674 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
675 }
676 }
677 })
678
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800679 suffix := ""
680 if ctx.Config().(Config).EmbeddedInMake() {
681 suffix = "-soong"
682 }
683
Colin Cross1f8c52b2015-06-16 16:38:17 -0700684 // Create a top-level checkbuild target that depends on all modules
685 ctx.Build(pctx, blueprint.BuildParams{
686 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800687 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700688 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700689 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700690 })
691
692 // Create a mm/<directory> target that depends on all modules in a directory
693 dirs := sortedKeys(dirModules)
694 for _, dir := range dirs {
695 ctx.Build(pctx, blueprint.BuildParams{
696 Rule: blueprint.Phony,
697 Outputs: []string{filepath.Join("mm", dir)},
698 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800699 // HACK: checkbuild should be an optional build, but force it
700 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800701 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700702 })
703 }
704}
Colin Crossd779da42015-12-17 18:00:23 -0800705
706type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700707 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800708 ctx interface {
709 ModuleName(blueprint.Module) string
710 ModuleSubDir(blueprint.Module) string
711 }
712}
713
714func (s AndroidModulesByName) Len() int { return len(s.slice) }
715func (s AndroidModulesByName) Less(i, j int) bool {
716 mi, mj := s.slice[i], s.slice[j]
717 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
718
719 if ni != nj {
720 return ni < nj
721 } else {
722 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
723 }
724}
725func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }