blob: e415c0a5a1e07f851548738dc4cafe0463da1274 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6ff51382015-12-17 16:39:19 -080018 "fmt"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "path/filepath"
Colin Cross6ff51382015-12-17 16:39:19 -080020 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070021
Dan Willemsen0effe062015-11-30 16:06:01 -080022 "android/soong"
Colin Cross8f101b42015-06-17 15:09:06 -070023 "android/soong/glob"
24
Colin Crossf6566ed2015-03-24 11:13:38 -070025 "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -080026)
27
Colin Crossa120ec12016-08-19 16:07:38 -070028func init() {
Colin Cross178a5092016-09-13 13:42:32 -070029 RegisterTopDownMutator("load_hooks", loadHookMutator).Parallel()
Colin Crossa120ec12016-08-19 16:07:38 -070030 RegisterBottomUpMutator("defaults_deps", defaultsDepsMutator).Parallel()
31 RegisterTopDownMutator("defaults", defaultsMutator).Parallel()
32
33 RegisterBottomUpMutator("arch", ArchMutator).Parallel()
Colin Cross178a5092016-09-13 13:42:32 -070034 RegisterTopDownMutator("arch_hooks", archHookMutator).Parallel()
Colin Crossa120ec12016-08-19 16:07:38 -070035}
36
Colin Cross3f40fa42015-01-30 17:27:36 -080037var (
38 DeviceSharedLibrary = "shared_library"
39 DeviceStaticLibrary = "static_library"
40 DeviceExecutable = "executable"
41 HostSharedLibrary = "host_shared_library"
42 HostStaticLibrary = "host_static_library"
43 HostExecutable = "host_executable"
44)
45
Dan Willemsen34cc69e2015-09-23 15:26:20 -070046type ModuleBuildParams struct {
47 Rule blueprint.Rule
48 Output WritablePath
49 Outputs WritablePaths
50 Input Path
51 Inputs Paths
52 Implicit Path
53 Implicits Paths
54 OrderOnly Paths
55 Default bool
56 Args map[string]string
57}
58
Colin Crossf6566ed2015-03-24 11:13:38 -070059type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070060 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070061 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070062 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070063 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070064 Host() bool
65 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070066 Darwin() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070067 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070068 PrimaryArch() bool
Colin Cross1332b002015-04-07 17:11:30 -070069 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070070 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070071}
72
Colin Cross635c3b02016-05-18 15:37:25 -070073type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070074 blueprint.BaseModuleContext
75 androidBaseContext
76}
77
Colin Cross635c3b02016-05-18 15:37:25 -070078type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080079 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070080 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080081
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 // Similar to Build, but takes Paths instead of []string,
83 // and performs more verification.
84 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070085
Dan Willemsen34cc69e2015-09-23 15:26:20 -070086 ExpandSources(srcFiles, excludes []string) Paths
87 Glob(outDir, globPattern string, excludes []string) Paths
88
Colin Crossa2344662016-03-24 13:14:12 -070089 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
90 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080091 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080093
94 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070095
96 Proprietary() bool
97 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080098}
99
Colin Cross635c3b02016-05-18 15:37:25 -0700100type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800101 blueprint.Module
102
Colin Cross635c3b02016-05-18 15:37:25 -0700103 GenerateAndroidBuildActions(ModuleContext)
Colin Cross3f40fa42015-01-30 17:27:36 -0800104
Colin Cross635c3b02016-05-18 15:37:25 -0700105 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -0800106 Enabled() bool
Colin Crossa1ad8d12016-06-01 17:09:44 -0700107 Target() Target
Dan Willemsen782a2d12015-12-21 14:55:28 -0800108 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800109}
110
Colin Cross3f40fa42015-01-30 17:27:36 -0800111type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700112 Name string
113 Deps []string
114 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800115
Dan Willemsen0effe062015-11-30 16:06:01 -0800116 // emit build rules for this module
117 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800118
Colin Cross7d5136f2015-05-11 13:39:40 -0700119 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800120 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
121 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
122 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700123 Compile_multilib string `android:"arch_variant"`
124
125 Target struct {
126 Host struct {
127 Compile_multilib string
128 }
129 Android struct {
130 Compile_multilib string
131 }
132 }
133
134 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800135
Dan Willemsen782a2d12015-12-21 14:55:28 -0800136 // whether this is a proprietary vendor module, and should be installed into /vendor
137 Proprietary bool
138
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700139 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
140 // file
141 Logtags []string
142
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700143 // init.rc files to be installed if this module is installed
144 Init_rc []string
145
Chris Wolfe998306e2016-08-15 14:47:23 -0400146 // names of other modules to install if this module is installed
147 Required []string
148
Colin Crossa1ad8d12016-06-01 17:09:44 -0700149 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700150 CompileTarget Target `blueprint:"mutated"`
151 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800152
153 // Set by InitAndroidModule
154 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
155}
156
157type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700158 Host_supported *bool
159 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800160}
161
Colin Crossc472d572015-03-17 15:06:21 -0700162type Multilib string
163
164const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700165 MultilibBoth Multilib = "both"
166 MultilibFirst Multilib = "first"
167 MultilibCommon Multilib = "common"
168 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700169)
170
Colin Crossa1ad8d12016-06-01 17:09:44 -0700171type HostOrDeviceSupported int
172
173const (
174 _ HostOrDeviceSupported = iota
175 HostSupported
176 DeviceSupported
177 HostAndDeviceSupported
178 HostAndDeviceDefault
179)
180
Colin Cross635c3b02016-05-18 15:37:25 -0700181func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800182 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
183
184 base := m.base()
185 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700186
Colin Cross7f64b6d2015-07-09 13:57:48 -0700187 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700188
189 return m, propertyStructs
190}
191
Colin Cross635c3b02016-05-18 15:37:25 -0700192func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700193 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
194
195 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
196
197 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800198 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700199 base.commonProperties.Default_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800200
Dan Willemsen218f6562015-07-08 18:13:11 -0700201 switch hod {
202 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800203 // Default to module to device supported, host not supported, can override in module
204 // properties
Colin Crossa4190c12016-07-12 13:11:25 -0700205 base.hostAndDeviceProperties.Device_supported = boolPtr(true)
Dan Willemsen218f6562015-07-08 18:13:11 -0700206 fallthrough
207 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800208 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
209 }
210
Colin Crosscfad1192015-11-02 16:43:11 -0800211 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800212}
213
214// A AndroidModuleBase object contains the properties that are common to all Android
215// modules. It should be included as an anonymous field in every module
216// struct definition. InitAndroidModule should then be called from the module's
217// factory function, and the return values from InitAndroidModule should be
218// returned from the factory function.
219//
220// The AndroidModuleBase type is responsible for implementing the
221// GenerateBuildActions method to support the blueprint.Module interface. This
222// method will then call the module's GenerateAndroidBuildActions method once
223// for each build variant that is to be built. GenerateAndroidBuildActions is
224// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
225// AndroidModuleContext exposes extra functionality specific to the Android build
226// system including details about the particular build variant that is to be
227// generated.
228//
229// For example:
230//
231// import (
232// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700233// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800234// )
235//
236// type myModule struct {
237// common.AndroidModuleBase
238// properties struct {
239// MyProperty string
240// }
241// }
242//
243// func NewMyModule() (blueprint.Module, []interface{}) {
244// m := &myModule{}
245// return common.InitAndroidModule(m, &m.properties)
246// }
247//
248// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
249// // Get the CPU architecture for the current build variant.
250// variantArch := ctx.Arch()
251//
252// // ...
253// }
Colin Cross635c3b02016-05-18 15:37:25 -0700254type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800255 // Putting the curiously recurring thing pointing to the thing that contains
256 // the thing pattern to good use.
Colin Cross635c3b02016-05-18 15:37:25 -0700257 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800258
259 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700260 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800261 hostAndDeviceProperties hostAndDeviceProperties
262 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700263 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700264 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800265
266 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700267 installFiles Paths
268 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700269
270 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
271 // Only set on the final variant of each module
272 installTarget string
273 checkbuildTarget string
274 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700275
Colin Cross178a5092016-09-13 13:42:32 -0700276 hooks hooks
Colin Cross3f40fa42015-01-30 17:27:36 -0800277}
278
Colin Cross635c3b02016-05-18 15:37:25 -0700279func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800280 return a
281}
282
Colin Cross8b74d172016-09-13 09:59:14 -0700283func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700284 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700285 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700286}
287
Colin Crossa1ad8d12016-06-01 17:09:44 -0700288func (a *ModuleBase) Target() Target {
289 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800290}
291
Colin Cross8b74d172016-09-13 09:59:14 -0700292func (a *ModuleBase) TargetPrimary() bool {
293 return a.commonProperties.CompilePrimary
294}
295
Colin Crossa1ad8d12016-06-01 17:09:44 -0700296func (a *ModuleBase) Os() OsType {
297 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800298}
299
Colin Cross635c3b02016-05-18 15:37:25 -0700300func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700301 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800302}
303
Colin Cross635c3b02016-05-18 15:37:25 -0700304func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700305 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800306}
307
Colin Crossa1ad8d12016-06-01 17:09:44 -0700308func (a *ModuleBase) OsClassSupported() []OsClass {
309 switch a.commonProperties.HostOrDeviceSupported {
310 case HostSupported:
311 // TODO(ccross): explicitly mark host cross support
312 return []OsClass{Host, HostCross}
313 case DeviceSupported:
314 return []OsClass{Device}
315 case HostAndDeviceSupported:
316 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700317 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700318 supported = append(supported, Host, HostCross)
319 }
Colin Crossa4190c12016-07-12 13:11:25 -0700320 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700321 supported = append(supported, Device)
322 }
323 return supported
324 default:
325 return nil
326 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800327}
328
Colin Cross635c3b02016-05-18 15:37:25 -0700329func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800330 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
331 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700332 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800333}
334
Colin Cross635c3b02016-05-18 15:37:25 -0700335func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800336 if a.commonProperties.Enabled == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700337 return a.Os().Class != HostCross
Dan Willemsen490fd492015-11-24 17:53:15 -0800338 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800339 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800340}
341
Colin Cross635c3b02016-05-18 15:37:25 -0700342func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700343 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800344
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700345 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800346 ctx.VisitDepsDepthFirstIf(isFileInstaller,
347 func(m blueprint.Module) {
348 fileInstaller := m.(fileInstaller)
349 files := fileInstaller.filesToInstall()
350 result = append(result, files...)
351 })
352
353 return result
354}
355
Colin Cross635c3b02016-05-18 15:37:25 -0700356func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800357 return a.installFiles
358}
359
Colin Cross635c3b02016-05-18 15:37:25 -0700360func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800361 return p.noAddressSanitizer
362}
363
Colin Cross635c3b02016-05-18 15:37:25 -0700364func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800365 return false
366}
367
Colin Cross635c3b02016-05-18 15:37:25 -0700368func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
369 if a != ctx.FinalModule().(Module).base() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800370 return
371 }
372
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700373 allInstalledFiles := Paths{}
374 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800375 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700376 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700377 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
378 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 })
380
Colin Cross9454bfa2015-03-17 13:24:18 -0700381 deps := []string{}
382
Colin Cross3f40fa42015-01-30 17:27:36 -0800383 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700384 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800385 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700386 Rule: blueprint.Phony,
387 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700388 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800389 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700390 })
391 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700392 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700393 }
394
395 if len(allCheckbuildFiles) > 0 {
396 name := ctx.ModuleName() + "-checkbuild"
397 ctx.Build(pctx, blueprint.BuildParams{
398 Rule: blueprint.Phony,
399 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700400 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700401 Optional: true,
402 })
403 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700404 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700405 }
406
407 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800408 suffix := ""
409 if ctx.Config().(Config).EmbeddedInMake() {
410 suffix = "-soong"
411 }
412
Colin Cross9454bfa2015-03-17 13:24:18 -0700413 ctx.Build(pctx, blueprint.BuildParams{
414 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800415 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700416 Implicits: deps,
417 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800418 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700419
420 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800421 }
422}
423
Colin Cross635c3b02016-05-18 15:37:25 -0700424func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700425 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700426 target: a.commonProperties.CompileTarget,
427 targetPrimary: a.commonProperties.CompilePrimary,
428 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800429 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800430}
431
Colin Cross635c3b02016-05-18 15:37:25 -0700432func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800433 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700434 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700435 ModuleContext: ctx,
436 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
437 installDeps: a.computeInstallDeps(ctx),
438 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800439 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800440 }
441
Dan Willemsen0effe062015-11-30 16:06:01 -0800442 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800443 return
444 }
445
446 a.module.GenerateAndroidBuildActions(androidCtx)
447 if ctx.Failed() {
448 return
449 }
450
Colin Crossc9404352015-03-26 16:10:12 -0700451 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
452 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
453
Colin Cross3f40fa42015-01-30 17:27:36 -0800454 a.generateModuleTarget(ctx)
455 if ctx.Failed() {
456 return
457 }
458}
459
Colin Crossf6566ed2015-03-24 11:13:38 -0700460type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700461 target Target
462 targetPrimary bool
463 debug bool
464 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700465}
466
Colin Cross3f40fa42015-01-30 17:27:36 -0800467type androidModuleContext struct {
468 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700469 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700470 installDeps Paths
471 installFiles Paths
472 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800473 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700474 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800475}
476
477func (a *androidModuleContext) ninjaError(outputs []string, err error) {
478 a.ModuleContext.Build(pctx, blueprint.BuildParams{
479 Rule: ErrorRule,
480 Outputs: outputs,
481 Optional: true,
482 Args: map[string]string{
483 "error": err.Error(),
484 },
485 })
486 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800487}
488
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800489func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Crosse2c48742016-04-27 13:47:35 -0700490 if a.missingDeps != nil && params.Rule != globRule {
Colin Cross6ff51382015-12-17 16:39:19 -0800491 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
492 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
493 return
494 }
495
Colin Cross3f40fa42015-01-30 17:27:36 -0800496 params.Optional = true
497 a.ModuleContext.Build(pctx, params)
498}
499
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700500func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
501 bparams := blueprint.BuildParams{
502 Rule: params.Rule,
503 Outputs: params.Outputs.Strings(),
504 Inputs: params.Inputs.Strings(),
505 Implicits: params.Implicits.Strings(),
506 OrderOnly: params.OrderOnly.Strings(),
507 Args: params.Args,
508 Optional: !params.Default,
509 }
510
511 if params.Output != nil {
512 bparams.Outputs = append(bparams.Outputs, params.Output.String())
513 }
514 if params.Input != nil {
515 bparams.Inputs = append(bparams.Inputs, params.Input.String())
516 }
517 if params.Implicit != nil {
518 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
519 }
520
Colin Cross6ff51382015-12-17 16:39:19 -0800521 if a.missingDeps != nil {
522 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
523 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
524 return
525 }
526
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700527 a.ModuleContext.Build(pctx, bparams)
528}
529
Colin Cross6ff51382015-12-17 16:39:19 -0800530func (a *androidModuleContext) GetMissingDependencies() []string {
531 return a.missingDeps
532}
533
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800534func (a *androidModuleContext) AddMissingDependencies(deps []string) {
535 if deps != nil {
536 a.missingDeps = append(a.missingDeps, deps...)
537 }
538}
539
Colin Crossa1ad8d12016-06-01 17:09:44 -0700540func (a *androidBaseContextImpl) Target() Target {
541 return a.target
542}
543
Colin Cross8b74d172016-09-13 09:59:14 -0700544func (a *androidBaseContextImpl) TargetPrimary() bool {
545 return a.targetPrimary
546}
547
Colin Crossf6566ed2015-03-24 11:13:38 -0700548func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700549 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800550}
551
Colin Crossa1ad8d12016-06-01 17:09:44 -0700552func (a *androidBaseContextImpl) Os() OsType {
553 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800554}
555
Colin Crossf6566ed2015-03-24 11:13:38 -0700556func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700557 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700558}
559
560func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700561 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700562}
563
Colin Cross0af4b842015-04-30 16:36:18 -0700564func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700565 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700566}
567
Colin Crossf6566ed2015-03-24 11:13:38 -0700568func (a *androidBaseContextImpl) Debug() bool {
569 return a.debug
570}
571
Colin Cross1e7d3702016-08-24 15:25:47 -0700572func (a *androidBaseContextImpl) PrimaryArch() bool {
573 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
574}
575
Colin Cross1332b002015-04-07 17:11:30 -0700576func (a *androidBaseContextImpl) AConfig() Config {
577 return a.config
578}
579
Colin Cross9272ade2016-08-17 15:24:12 -0700580func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
581 return DeviceConfig{a.config.deviceConfig}
582}
583
Colin Cross8d8f8e22016-08-03 11:57:50 -0700584func (a *androidModuleContext) Proprietary() bool {
585 return a.module.base().commonProperties.Proprietary
Dan Willemsen782a2d12015-12-21 14:55:28 -0800586}
587
Colin Cross8d8f8e22016-08-03 11:57:50 -0700588func (a *androidModuleContext) InstallInData() bool {
589 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800590}
591
592func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700593 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700594
Dan Willemsen782a2d12015-12-21 14:55:28 -0800595 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700596 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800597
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800598 if a.Host() || !a.AConfig().SkipDeviceInstall() {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800599 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700600
Dan Willemsen322acaf2016-01-12 23:07:05 -0800601 a.ModuleBuild(pctx, ModuleBuildParams{
602 Rule: Cp,
603 Output: fullInstallPath,
604 Input: srcPath,
605 OrderOnly: Paths(deps),
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800606 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800607 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800608
Dan Willemsen322acaf2016-01-12 23:07:05 -0800609 a.installFiles = append(a.installFiles, fullInstallPath)
610 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700611 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700612 return fullInstallPath
613}
614
Colin Crossa2344662016-03-24 13:14:12 -0700615func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700616 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800617}
618
Colin Cross3854a602016-01-11 12:49:11 -0800619func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
620 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700621 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800622
Colin Cross12fc4972016-01-11 12:49:11 -0800623 if a.Host() || !a.AConfig().SkipDeviceInstall() {
624 a.ModuleBuild(pctx, ModuleBuildParams{
625 Rule: Symlink,
626 Output: fullInstallPath,
627 OrderOnly: Paths{srcPath},
628 Default: !a.AConfig().EmbeddedInMake(),
629 Args: map[string]string{
630 "fromPath": srcPath.String(),
631 },
632 })
Colin Cross3854a602016-01-11 12:49:11 -0800633
Colin Cross12fc4972016-01-11 12:49:11 -0800634 a.installFiles = append(a.installFiles, fullInstallPath)
635 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
636 }
Colin Cross3854a602016-01-11 12:49:11 -0800637 return fullInstallPath
638}
639
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700640func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800641 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
642}
643
Colin Cross3f40fa42015-01-30 17:27:36 -0800644type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700645 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800646}
647
648func isFileInstaller(m blueprint.Module) bool {
649 _, ok := m.(fileInstaller)
650 return ok
651}
652
653func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700654 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800655 return ok
656}
Colin Crossfce53272015-04-08 11:21:40 -0700657
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700658func findStringInSlice(str string, slice []string) int {
659 for i, s := range slice {
660 if s == str {
661 return i
Colin Crossfce53272015-04-08 11:21:40 -0700662 }
663 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700664 return -1
665}
666
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700667func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
668 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700669 for i, e := range excludes {
670 j := findStringInSlice(e, srcFiles)
671 if j != -1 {
672 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
673 }
674
675 excludes[i] = filepath.Join(prefix, e)
676 }
677
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700678 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700679 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700680 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700681 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700682 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700683 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700684 }
685 }
686
687 return globbedSrcFiles
688}
689
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700690func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
691 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700692 if err != nil {
693 ctx.ModuleErrorf("glob: %s", err.Error())
694 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700695 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700696}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700697
Colin Cross463a90e2015-06-17 14:20:06 -0700698func init() {
699 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
700}
701
Colin Cross1f8c52b2015-06-16 16:38:17 -0700702func BuildTargetSingleton() blueprint.Singleton {
703 return &buildTargetSingleton{}
704}
705
706type buildTargetSingleton struct{}
707
708func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
709 checkbuildDeps := []string{}
710
711 dirModules := make(map[string][]string)
712
713 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700714 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700715 blueprintDir := a.base().blueprintDir
716 installTarget := a.base().installTarget
717 checkbuildTarget := a.base().checkbuildTarget
718
719 if checkbuildTarget != "" {
720 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
721 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
722 }
723
724 if installTarget != "" {
725 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
726 }
727 }
728 })
729
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800730 suffix := ""
731 if ctx.Config().(Config).EmbeddedInMake() {
732 suffix = "-soong"
733 }
734
Colin Cross1f8c52b2015-06-16 16:38:17 -0700735 // Create a top-level checkbuild target that depends on all modules
736 ctx.Build(pctx, blueprint.BuildParams{
737 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800738 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700739 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700740 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700741 })
742
743 // Create a mm/<directory> target that depends on all modules in a directory
744 dirs := sortedKeys(dirModules)
745 for _, dir := range dirs {
746 ctx.Build(pctx, blueprint.BuildParams{
747 Rule: blueprint.Phony,
748 Outputs: []string{filepath.Join("mm", dir)},
749 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800750 // HACK: checkbuild should be an optional build, but force it
751 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800752 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700753 })
754 }
755}
Colin Crossd779da42015-12-17 18:00:23 -0800756
757type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700758 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800759 ctx interface {
760 ModuleName(blueprint.Module) string
761 ModuleSubDir(blueprint.Module) string
762 }
763}
764
765func (s AndroidModulesByName) Len() int { return len(s.slice) }
766func (s AndroidModulesByName) Less(i, j int) bool {
767 mi, mj := s.slice[i], s.slice[j]
768 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
769
770 if ni != nj {
771 return ni < nj
772 } else {
773 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
774 }
775}
776func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }