blob: 52280d2425b052733e45b0a19b03f3559abed922 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6ff51382015-12-17 16:39:19 -080018 "fmt"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "path/filepath"
Colin Cross6ff51382015-12-17 16:39:19 -080020 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070021
Colin Cross8f101b42015-06-17 15:09:06 -070022 "android/soong/glob"
23
Colin Crossf6566ed2015-03-24 11:13:38 -070024 "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -080025)
26
27var (
28 DeviceSharedLibrary = "shared_library"
29 DeviceStaticLibrary = "static_library"
30 DeviceExecutable = "executable"
31 HostSharedLibrary = "host_shared_library"
32 HostStaticLibrary = "host_static_library"
33 HostExecutable = "host_executable"
34)
35
Dan Willemsen34cc69e2015-09-23 15:26:20 -070036type ModuleBuildParams struct {
37 Rule blueprint.Rule
38 Output WritablePath
39 Outputs WritablePaths
40 Input Path
41 Inputs Paths
42 Implicit Path
43 Implicits Paths
44 OrderOnly Paths
45 Default bool
46 Args map[string]string
47}
48
Colin Crossf6566ed2015-03-24 11:13:38 -070049type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070050 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070051 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070052 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070053 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070054 Host() bool
55 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070056 Darwin() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070057 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070058 PrimaryArch() bool
Colin Cross1332b002015-04-07 17:11:30 -070059 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070060 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070061}
62
Colin Cross635c3b02016-05-18 15:37:25 -070063type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070064 blueprint.BaseModuleContext
65 androidBaseContext
66}
67
Colin Cross635c3b02016-05-18 15:37:25 -070068type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080069 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070070 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080071
Dan Willemsen34cc69e2015-09-23 15:26:20 -070072 // Similar to Build, but takes Paths instead of []string,
73 // and performs more verification.
74 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070075
Dan Willemsen34cc69e2015-09-23 15:26:20 -070076 ExpandSources(srcFiles, excludes []string) Paths
77 Glob(outDir, globPattern string, excludes []string) Paths
78
Colin Crossa2344662016-03-24 13:14:12 -070079 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
80 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080081 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080083
84 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070085
86 Proprietary() bool
87 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080088}
89
Colin Cross635c3b02016-05-18 15:37:25 -070090type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080091 blueprint.Module
92
Colin Cross635c3b02016-05-18 15:37:25 -070093 GenerateAndroidBuildActions(ModuleContext)
Colin Cross1e676be2016-10-12 14:38:15 -070094 DepsMutator(BottomUpMutatorContext)
Colin Cross3f40fa42015-01-30 17:27:36 -080095
Colin Cross635c3b02016-05-18 15:37:25 -070096 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -080097 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -070098 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -080099 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800100}
101
Colin Crossfc754582016-05-17 16:34:16 -0700102type nameProperties struct {
103 // The name of the module. Must be unique across all modules.
Colin Crossc77f9d12015-04-02 13:54:39 -0700104 Name string
Colin Crossfc754582016-05-17 16:34:16 -0700105}
106
107type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700108 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800109
Dan Willemsen0effe062015-11-30 16:06:01 -0800110 // emit build rules for this module
111 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800112
Colin Cross7d5136f2015-05-11 13:39:40 -0700113 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800114 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
115 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
116 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700117 Compile_multilib string `android:"arch_variant"`
118
119 Target struct {
120 Host struct {
121 Compile_multilib string
122 }
123 Android struct {
124 Compile_multilib string
125 }
126 }
127
128 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800129
Dan Willemsen782a2d12015-12-21 14:55:28 -0800130 // whether this is a proprietary vendor module, and should be installed into /vendor
131 Proprietary bool
132
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700133 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
134 // file
135 Logtags []string
136
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700137 // init.rc files to be installed if this module is installed
138 Init_rc []string
139
Chris Wolfe998306e2016-08-15 14:47:23 -0400140 // names of other modules to install if this module is installed
141 Required []string
142
Colin Crossa1ad8d12016-06-01 17:09:44 -0700143 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700144 CompileTarget Target `blueprint:"mutated"`
145 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800146
147 // Set by InitAndroidModule
148 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700149 ArchSpecific bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800150}
151
152type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700153 Host_supported *bool
154 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800155}
156
Colin Crossc472d572015-03-17 15:06:21 -0700157type Multilib string
158
159const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700160 MultilibBoth Multilib = "both"
161 MultilibFirst Multilib = "first"
162 MultilibCommon Multilib = "common"
163 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700164)
165
Colin Crossa1ad8d12016-06-01 17:09:44 -0700166type HostOrDeviceSupported int
167
168const (
169 _ HostOrDeviceSupported = iota
170 HostSupported
171 DeviceSupported
172 HostAndDeviceSupported
173 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700174 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700175)
176
Colin Cross635c3b02016-05-18 15:37:25 -0700177func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800178 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
179
180 base := m.base()
181 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700182
Colin Crossfc754582016-05-17 16:34:16 -0700183 propertyStructs = append(propertyStructs,
184 &base.nameProperties,
185 &base.commonProperties,
186 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700187
188 return m, propertyStructs
189}
190
Colin Cross635c3b02016-05-18 15:37:25 -0700191func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700192 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
193
194 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
195
196 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800197 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700198 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700199 base.commonProperties.ArchSpecific = true
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
Colin Crossfc754582016-05-17 16:34:16 -0700259 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800260 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700261 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800262 hostAndDeviceProperties hostAndDeviceProperties
263 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700264 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700265 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800266
267 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700268 installFiles Paths
269 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700270
271 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
272 // Only set on the final variant of each module
273 installTarget string
274 checkbuildTarget string
275 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700276
Colin Cross178a5092016-09-13 13:42:32 -0700277 hooks hooks
Colin Cross3f40fa42015-01-30 17:27:36 -0800278}
279
Colin Crossfc754582016-05-17 16:34:16 -0700280func (a *ModuleBase) Name() string {
281 return a.nameProperties.Name
282}
283
Colin Cross635c3b02016-05-18 15:37:25 -0700284func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800285 return a
286}
287
Colin Cross8b74d172016-09-13 09:59:14 -0700288func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700289 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700290 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700291}
292
Colin Crossa1ad8d12016-06-01 17:09:44 -0700293func (a *ModuleBase) Target() Target {
294 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800295}
296
Colin Cross8b74d172016-09-13 09:59:14 -0700297func (a *ModuleBase) TargetPrimary() bool {
298 return a.commonProperties.CompilePrimary
299}
300
Colin Crossa1ad8d12016-06-01 17:09:44 -0700301func (a *ModuleBase) Os() OsType {
302 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800303}
304
Colin Cross635c3b02016-05-18 15:37:25 -0700305func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700306 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800307}
308
Colin Cross635c3b02016-05-18 15:37:25 -0700309func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700310 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800311}
312
Dan Willemsen0b24c742016-10-04 15:13:37 -0700313func (a *ModuleBase) ArchSpecific() bool {
314 return a.commonProperties.ArchSpecific
315}
316
Colin Crossa1ad8d12016-06-01 17:09:44 -0700317func (a *ModuleBase) OsClassSupported() []OsClass {
318 switch a.commonProperties.HostOrDeviceSupported {
319 case HostSupported:
320 // TODO(ccross): explicitly mark host cross support
321 return []OsClass{Host, HostCross}
322 case DeviceSupported:
323 return []OsClass{Device}
324 case HostAndDeviceSupported:
325 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700326 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700327 supported = append(supported, Host, HostCross)
328 }
Colin Crossa4190c12016-07-12 13:11:25 -0700329 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700330 supported = append(supported, Device)
331 }
332 return supported
333 default:
334 return nil
335 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800336}
337
Colin Cross635c3b02016-05-18 15:37:25 -0700338func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800339 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
340 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700341 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800342}
343
Colin Cross635c3b02016-05-18 15:37:25 -0700344func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800345 if a.commonProperties.Enabled == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700346 return a.Os().Class != HostCross
Dan Willemsen490fd492015-11-24 17:53:15 -0800347 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800348 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800349}
350
Colin Cross635c3b02016-05-18 15:37:25 -0700351func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700352 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800353
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700354 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800355 ctx.VisitDepsDepthFirstIf(isFileInstaller,
356 func(m blueprint.Module) {
357 fileInstaller := m.(fileInstaller)
358 files := fileInstaller.filesToInstall()
359 result = append(result, files...)
360 })
361
362 return result
363}
364
Colin Cross635c3b02016-05-18 15:37:25 -0700365func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800366 return a.installFiles
367}
368
Colin Cross635c3b02016-05-18 15:37:25 -0700369func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800370 return p.noAddressSanitizer
371}
372
Colin Cross635c3b02016-05-18 15:37:25 -0700373func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800374 return false
375}
376
Colin Cross635c3b02016-05-18 15:37:25 -0700377func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700378 allInstalledFiles := Paths{}
379 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800380 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700381 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700382 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
383 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800384 })
385
Colin Cross9454bfa2015-03-17 13:24:18 -0700386 deps := []string{}
387
Colin Cross3f40fa42015-01-30 17:27:36 -0800388 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700389 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800390 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700391 Rule: blueprint.Phony,
392 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700393 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800394 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700395 })
396 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700397 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700398 }
399
400 if len(allCheckbuildFiles) > 0 {
401 name := ctx.ModuleName() + "-checkbuild"
402 ctx.Build(pctx, blueprint.BuildParams{
403 Rule: blueprint.Phony,
404 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700405 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700406 Optional: true,
407 })
408 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700409 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700410 }
411
412 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800413 suffix := ""
414 if ctx.Config().(Config).EmbeddedInMake() {
415 suffix = "-soong"
416 }
417
Colin Cross9454bfa2015-03-17 13:24:18 -0700418 ctx.Build(pctx, blueprint.BuildParams{
419 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800420 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700421 Implicits: deps,
422 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800423 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700424
425 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800426 }
427}
428
Colin Cross635c3b02016-05-18 15:37:25 -0700429func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700430 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700431 target: a.commonProperties.CompileTarget,
432 targetPrimary: a.commonProperties.CompilePrimary,
433 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800434 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800435}
436
Colin Cross635c3b02016-05-18 15:37:25 -0700437func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800438 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700439 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700440 ModuleContext: ctx,
441 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
442 installDeps: a.computeInstallDeps(ctx),
443 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800444 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800445 }
446
Colin Cross9b1d13d2016-09-19 15:18:11 -0700447 if a.Enabled() {
448 a.module.GenerateAndroidBuildActions(androidCtx)
449 if ctx.Failed() {
450 return
451 }
452
453 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
454 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800455 }
456
Colin Cross9b1d13d2016-09-19 15:18:11 -0700457 if a == ctx.FinalModule().(Module).base() {
458 a.generateModuleTarget(ctx)
459 if ctx.Failed() {
460 return
461 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800462 }
463}
464
Colin Crossf6566ed2015-03-24 11:13:38 -0700465type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700466 target Target
467 targetPrimary bool
468 debug bool
469 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700470}
471
Colin Cross3f40fa42015-01-30 17:27:36 -0800472type androidModuleContext struct {
473 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700474 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700475 installDeps Paths
476 installFiles Paths
477 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800478 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700479 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800480}
481
482func (a *androidModuleContext) ninjaError(outputs []string, err error) {
483 a.ModuleContext.Build(pctx, blueprint.BuildParams{
484 Rule: ErrorRule,
485 Outputs: outputs,
486 Optional: true,
487 Args: map[string]string{
488 "error": err.Error(),
489 },
490 })
491 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800492}
493
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800494func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Crosse2c48742016-04-27 13:47:35 -0700495 if a.missingDeps != nil && params.Rule != globRule {
Colin Cross6ff51382015-12-17 16:39:19 -0800496 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
497 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
498 return
499 }
500
Colin Cross3f40fa42015-01-30 17:27:36 -0800501 params.Optional = true
502 a.ModuleContext.Build(pctx, params)
503}
504
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700505func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
506 bparams := blueprint.BuildParams{
507 Rule: params.Rule,
508 Outputs: params.Outputs.Strings(),
509 Inputs: params.Inputs.Strings(),
510 Implicits: params.Implicits.Strings(),
511 OrderOnly: params.OrderOnly.Strings(),
512 Args: params.Args,
513 Optional: !params.Default,
514 }
515
516 if params.Output != nil {
517 bparams.Outputs = append(bparams.Outputs, params.Output.String())
518 }
519 if params.Input != nil {
520 bparams.Inputs = append(bparams.Inputs, params.Input.String())
521 }
522 if params.Implicit != nil {
523 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
524 }
525
Colin Cross6ff51382015-12-17 16:39:19 -0800526 if a.missingDeps != nil {
527 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
528 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
529 return
530 }
531
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700532 a.ModuleContext.Build(pctx, bparams)
533}
534
Colin Cross6ff51382015-12-17 16:39:19 -0800535func (a *androidModuleContext) GetMissingDependencies() []string {
536 return a.missingDeps
537}
538
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800539func (a *androidModuleContext) AddMissingDependencies(deps []string) {
540 if deps != nil {
541 a.missingDeps = append(a.missingDeps, deps...)
542 }
543}
544
Colin Crossa1ad8d12016-06-01 17:09:44 -0700545func (a *androidBaseContextImpl) Target() Target {
546 return a.target
547}
548
Colin Cross8b74d172016-09-13 09:59:14 -0700549func (a *androidBaseContextImpl) TargetPrimary() bool {
550 return a.targetPrimary
551}
552
Colin Crossf6566ed2015-03-24 11:13:38 -0700553func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700554 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800555}
556
Colin Crossa1ad8d12016-06-01 17:09:44 -0700557func (a *androidBaseContextImpl) Os() OsType {
558 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800559}
560
Colin Crossf6566ed2015-03-24 11:13:38 -0700561func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700562 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700563}
564
565func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700566 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700567}
568
Colin Cross0af4b842015-04-30 16:36:18 -0700569func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700570 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700571}
572
Colin Crossf6566ed2015-03-24 11:13:38 -0700573func (a *androidBaseContextImpl) Debug() bool {
574 return a.debug
575}
576
Colin Cross1e7d3702016-08-24 15:25:47 -0700577func (a *androidBaseContextImpl) PrimaryArch() bool {
578 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
579}
580
Colin Cross1332b002015-04-07 17:11:30 -0700581func (a *androidBaseContextImpl) AConfig() Config {
582 return a.config
583}
584
Colin Cross9272ade2016-08-17 15:24:12 -0700585func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
586 return DeviceConfig{a.config.deviceConfig}
587}
588
Colin Cross8d8f8e22016-08-03 11:57:50 -0700589func (a *androidModuleContext) Proprietary() bool {
590 return a.module.base().commonProperties.Proprietary
Dan Willemsen782a2d12015-12-21 14:55:28 -0800591}
592
Colin Cross8d8f8e22016-08-03 11:57:50 -0700593func (a *androidModuleContext) InstallInData() bool {
594 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800595}
596
597func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700598 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700599
Dan Willemsen782a2d12015-12-21 14:55:28 -0800600 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700601 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800602
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800603 if a.Host() || !a.AConfig().SkipDeviceInstall() {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800604 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700605
Colin Cross89562dc2016-10-03 17:47:19 -0700606 var implicitDeps, orderOnlyDeps Paths
607
608 if a.Host() {
609 // Installed host modules might be used during the build, depend directly on their
610 // dependencies so their timestamp is updated whenever their dependency is updated
611 implicitDeps = deps
612 } else {
613 orderOnlyDeps = deps
614 }
615
Dan Willemsen322acaf2016-01-12 23:07:05 -0800616 a.ModuleBuild(pctx, ModuleBuildParams{
617 Rule: Cp,
618 Output: fullInstallPath,
619 Input: srcPath,
Colin Cross89562dc2016-10-03 17:47:19 -0700620 Implicits: implicitDeps,
621 OrderOnly: orderOnlyDeps,
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800622 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800623 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800624
Dan Willemsen322acaf2016-01-12 23:07:05 -0800625 a.installFiles = append(a.installFiles, fullInstallPath)
626 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700627 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700628 return fullInstallPath
629}
630
Colin Crossa2344662016-03-24 13:14:12 -0700631func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700632 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800633}
634
Colin Cross3854a602016-01-11 12:49:11 -0800635func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
636 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700637 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800638
Colin Cross12fc4972016-01-11 12:49:11 -0800639 if a.Host() || !a.AConfig().SkipDeviceInstall() {
640 a.ModuleBuild(pctx, ModuleBuildParams{
641 Rule: Symlink,
642 Output: fullInstallPath,
643 OrderOnly: Paths{srcPath},
644 Default: !a.AConfig().EmbeddedInMake(),
645 Args: map[string]string{
646 "fromPath": srcPath.String(),
647 },
648 })
Colin Cross3854a602016-01-11 12:49:11 -0800649
Colin Cross12fc4972016-01-11 12:49:11 -0800650 a.installFiles = append(a.installFiles, fullInstallPath)
651 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
652 }
Colin Cross3854a602016-01-11 12:49:11 -0800653 return fullInstallPath
654}
655
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700656func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800657 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
658}
659
Colin Cross3f40fa42015-01-30 17:27:36 -0800660type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700661 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800662}
663
664func isFileInstaller(m blueprint.Module) bool {
665 _, ok := m.(fileInstaller)
666 return ok
667}
668
669func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700670 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800671 return ok
672}
Colin Crossfce53272015-04-08 11:21:40 -0700673
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700674func findStringInSlice(str string, slice []string) int {
675 for i, s := range slice {
676 if s == str {
677 return i
Colin Crossfce53272015-04-08 11:21:40 -0700678 }
679 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700680 return -1
681}
682
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700683func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
684 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700685 for i, e := range excludes {
686 j := findStringInSlice(e, srcFiles)
687 if j != -1 {
688 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
689 }
690
691 excludes[i] = filepath.Join(prefix, e)
692 }
693
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700694 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700695 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700696 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700697 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700698 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700699 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700700 }
701 }
702
703 return globbedSrcFiles
704}
705
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700706func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
707 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700708 if err != nil {
709 ctx.ModuleErrorf("glob: %s", err.Error())
710 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700711 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700712}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700713
Colin Cross463a90e2015-06-17 14:20:06 -0700714func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700715 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -0700716}
717
Colin Cross1f8c52b2015-06-16 16:38:17 -0700718func BuildTargetSingleton() blueprint.Singleton {
719 return &buildTargetSingleton{}
720}
721
722type buildTargetSingleton struct{}
723
724func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
725 checkbuildDeps := []string{}
726
727 dirModules := make(map[string][]string)
728
729 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700730 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700731 blueprintDir := a.base().blueprintDir
732 installTarget := a.base().installTarget
733 checkbuildTarget := a.base().checkbuildTarget
734
735 if checkbuildTarget != "" {
736 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
737 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
738 }
739
740 if installTarget != "" {
741 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
742 }
743 }
744 })
745
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800746 suffix := ""
747 if ctx.Config().(Config).EmbeddedInMake() {
748 suffix = "-soong"
749 }
750
Colin Cross1f8c52b2015-06-16 16:38:17 -0700751 // Create a top-level checkbuild target that depends on all modules
752 ctx.Build(pctx, blueprint.BuildParams{
753 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800754 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700755 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700756 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700757 })
758
759 // Create a mm/<directory> target that depends on all modules in a directory
760 dirs := sortedKeys(dirModules)
761 for _, dir := range dirs {
762 ctx.Build(pctx, blueprint.BuildParams{
763 Rule: blueprint.Phony,
764 Outputs: []string{filepath.Join("mm", dir)},
765 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800766 // HACK: checkbuild should be an optional build, but force it
767 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800768 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700769 })
770 }
771}
Colin Crossd779da42015-12-17 18:00:23 -0800772
773type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700774 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800775 ctx interface {
776 ModuleName(blueprint.Module) string
777 ModuleSubDir(blueprint.Module) string
778 }
779}
780
781func (s AndroidModulesByName) Len() int { return len(s.slice) }
782func (s AndroidModulesByName) Less(i, j int) bool {
783 mi, mj := s.slice[i], s.slice[j]
784 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
785
786 if ni != nj {
787 return ni < nj
788 } else {
789 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
790 }
791}
792func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }