blob: 18810fb8f03924823a28baf355e372ba5c57134c [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
22 "github.com/google/blueprint"
Colin Cross7f19f372016-11-01 11:10:25 -070023 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080024)
25
26var (
27 DeviceSharedLibrary = "shared_library"
28 DeviceStaticLibrary = "static_library"
29 DeviceExecutable = "executable"
30 HostSharedLibrary = "host_shared_library"
31 HostStaticLibrary = "host_static_library"
32 HostExecutable = "host_executable"
33)
34
Dan Willemsen34cc69e2015-09-23 15:26:20 -070035type ModuleBuildParams struct {
Dan Willemsen9f3c5742016-11-03 14:28:31 -070036 Rule blueprint.Rule
Colin Cross33bfb0a2016-11-21 17:23:08 -080037 Deps blueprint.Deps
38 Depfile WritablePath
Colin Cross67a5c132017-05-09 13:45:28 -070039 Description string
Dan Willemsen9f3c5742016-11-03 14:28:31 -070040 Output WritablePath
41 Outputs WritablePaths
42 ImplicitOutput WritablePath
43 ImplicitOutputs WritablePaths
44 Input Path
45 Inputs Paths
46 Implicit Path
47 Implicits Paths
48 OrderOnly Paths
49 Default bool
50 Args map[string]string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070051}
52
Colin Crossf6566ed2015-03-24 11:13:38 -070053type androidBaseContext interface {
Colin Crossa1ad8d12016-06-01 17:09:44 -070054 Target() Target
Colin Cross8b74d172016-09-13 09:59:14 -070055 TargetPrimary() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070056 Arch() Arch
Colin Crossa1ad8d12016-06-01 17:09:44 -070057 Os() OsType
Colin Crossf6566ed2015-03-24 11:13:38 -070058 Host() bool
59 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070060 Darwin() bool
Colin Cross3edeee12017-04-04 12:59:48 -070061 Windows() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070062 Debug() bool
Colin Cross1e7d3702016-08-24 15:25:47 -070063 PrimaryArch() bool
Dan Willemsenaa118f92017-04-06 12:49:58 -070064 Vendor() bool
Colin Cross1332b002015-04-07 17:11:30 -070065 AConfig() Config
Colin Cross9272ade2016-08-17 15:24:12 -070066 DeviceConfig() DeviceConfig
Colin Crossf6566ed2015-03-24 11:13:38 -070067}
68
Colin Cross635c3b02016-05-18 15:37:25 -070069type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070070 blueprint.BaseModuleContext
71 androidBaseContext
72}
73
Colin Cross635c3b02016-05-18 15:37:25 -070074type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080075 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070076 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080077
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078 // Similar to Build, but takes Paths instead of []string,
79 // and performs more verification.
80 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070081
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 ExpandSources(srcFiles, excludes []string) Paths
Colin Crossfaeb7aa2017-02-01 14:12:44 -080083 ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
Colin Cross7f19f372016-11-01 11:10:25 -070084 Glob(globPattern string, excludes []string) Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085
Colin Crossa2344662016-03-24 13:14:12 -070086 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
87 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Colin Cross3854a602016-01-11 12:49:11 -080088 InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080090
91 AddMissingDependencies(deps []string)
Colin Cross8d8f8e22016-08-03 11:57:50 -070092
Colin Cross8d8f8e22016-08-03 11:57:50 -070093 InstallInData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -070094 InstallInSanitizerDir() bool
Nan Zhang6d34b302017-02-04 17:47:46 -080095
96 RequiredModuleNames() []string
Colin Cross3f40fa42015-01-30 17:27:36 -080097}
98
Colin Cross635c3b02016-05-18 15:37:25 -070099type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800100 blueprint.Module
101
Colin Cross635c3b02016-05-18 15:37:25 -0700102 GenerateAndroidBuildActions(ModuleContext)
Colin Cross1e676be2016-10-12 14:38:15 -0700103 DepsMutator(BottomUpMutatorContext)
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
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700109 InstallInSanitizerDir() bool
Colin Crossa2f296f2016-11-29 15:16:18 -0800110 SkipInstall()
Colin Cross36242852017-06-23 15:06:31 -0700111
112 AddProperties(props ...interface{})
113 GetProperties() []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800114}
115
Colin Crossfc754582016-05-17 16:34:16 -0700116type nameProperties struct {
117 // The name of the module. Must be unique across all modules.
Colin Crossc77f9d12015-04-02 13:54:39 -0700118 Name string
Colin Crossfc754582016-05-17 16:34:16 -0700119}
120
121type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -0700122 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800123
Dan Willemsen0effe062015-11-30 16:06:01 -0800124 // emit build rules for this module
125 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800126
Colin Cross7d5136f2015-05-11 13:39:40 -0700127 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
129 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
130 // platform
Colin Cross69617d32016-09-06 10:39:07 -0700131 Compile_multilib string `android:"arch_variant"`
132
133 Target struct {
134 Host struct {
135 Compile_multilib string
136 }
137 Android struct {
138 Compile_multilib string
139 }
140 }
141
142 Default_multilib string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800143
Dan Willemsen782a2d12015-12-21 14:55:28 -0800144 // whether this is a proprietary vendor module, and should be installed into /vendor
145 Proprietary bool
146
Colin Cross55708f32017-03-20 13:23:34 -0700147 // vendor who owns this module
148 Owner string
149
Dan Willemsenaa118f92017-04-06 12:49:58 -0700150 // whether this module is device specific and should be installed into /vendor
151 Vendor bool
152
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700153 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
154 // file
155 Logtags []string
156
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700157 // init.rc files to be installed if this module is installed
158 Init_rc []string
159
Chris Wolfe998306e2016-08-15 14:47:23 -0400160 // names of other modules to install if this module is installed
Colin Crossc602b7d2017-05-05 13:36:36 -0700161 Required []string `android:"arch_variant"`
Chris Wolfe998306e2016-08-15 14:47:23 -0400162
Colin Crossa1ad8d12016-06-01 17:09:44 -0700163 // Set by TargetMutator
Colin Cross8b74d172016-09-13 09:59:14 -0700164 CompileTarget Target `blueprint:"mutated"`
165 CompilePrimary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800166
167 // Set by InitAndroidModule
168 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
Dan Willemsen0b24c742016-10-04 15:13:37 -0700169 ArchSpecific bool `blueprint:"mutated"`
Colin Crossce75d2c2016-10-06 16:12:58 -0700170
171 SkipInstall bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800172}
173
174type hostAndDeviceProperties struct {
Colin Crossa4190c12016-07-12 13:11:25 -0700175 Host_supported *bool
176 Device_supported *bool
Colin Cross3f40fa42015-01-30 17:27:36 -0800177}
178
Colin Crossc472d572015-03-17 15:06:21 -0700179type Multilib string
180
181const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700182 MultilibBoth Multilib = "both"
183 MultilibFirst Multilib = "first"
184 MultilibCommon Multilib = "common"
185 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700186)
187
Colin Crossa1ad8d12016-06-01 17:09:44 -0700188type HostOrDeviceSupported int
189
190const (
191 _ HostOrDeviceSupported = iota
192 HostSupported
Dan Albertc6345fb2016-10-20 01:36:11 -0700193 HostSupportedNoCross
Colin Crossa1ad8d12016-06-01 17:09:44 -0700194 DeviceSupported
195 HostAndDeviceSupported
196 HostAndDeviceDefault
Dan Willemsen0b24c742016-10-04 15:13:37 -0700197 NeitherHostNorDeviceSupported
Colin Crossa1ad8d12016-06-01 17:09:44 -0700198)
199
Colin Cross36242852017-06-23 15:06:31 -0700200func InitAndroidModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800201 base := m.base()
202 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700203
Colin Cross36242852017-06-23 15:06:31 -0700204 m.AddProperties(
Colin Crossfc754582016-05-17 16:34:16 -0700205 &base.nameProperties,
206 &base.commonProperties,
207 &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700208}
209
Colin Cross36242852017-06-23 15:06:31 -0700210func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
211 InitAndroidModule(m)
Colin Cross5049f022015-03-18 13:28:46 -0700212
213 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800214 base.commonProperties.HostOrDeviceSupported = hod
Colin Cross69617d32016-09-06 10:39:07 -0700215 base.commonProperties.Default_multilib = string(defaultMultilib)
Dan Willemsen0b24c742016-10-04 15:13:37 -0700216 base.commonProperties.ArchSpecific = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800217
Dan Willemsen218f6562015-07-08 18:13:11 -0700218 switch hod {
219 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800220 // Default to module to device supported, host not supported, can override in module
221 // properties
Colin Crossa4190c12016-07-12 13:11:25 -0700222 base.hostAndDeviceProperties.Device_supported = boolPtr(true)
Dan Willemsen218f6562015-07-08 18:13:11 -0700223 fallthrough
224 case HostAndDeviceDefault:
Colin Cross36242852017-06-23 15:06:31 -0700225 m.AddProperties(&base.hostAndDeviceProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -0800226 }
227
Colin Cross36242852017-06-23 15:06:31 -0700228 InitArchModule(m)
Colin Cross3f40fa42015-01-30 17:27:36 -0800229}
230
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800231// A ModuleBase object contains the properties that are common to all Android
Colin Cross3f40fa42015-01-30 17:27:36 -0800232// modules. It should be included as an anonymous field in every module
233// struct definition. InitAndroidModule should then be called from the module's
234// factory function, and the return values from InitAndroidModule should be
235// returned from the factory function.
236//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800237// The ModuleBase type is responsible for implementing the GenerateBuildActions
238// method to support the blueprint.Module interface. This method will then call
239// the module's GenerateAndroidBuildActions method once for each build variant
240// that is to be built. GenerateAndroidBuildActions is passed a
241// AndroidModuleContext rather than the usual blueprint.ModuleContext.
Colin Cross3f40fa42015-01-30 17:27:36 -0800242// AndroidModuleContext exposes extra functionality specific to the Android build
243// system including details about the particular build variant that is to be
244// generated.
245//
246// For example:
247//
248// import (
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800249// "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -0800250// )
251//
252// type myModule struct {
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800253// android.ModuleBase
Colin Cross3f40fa42015-01-30 17:27:36 -0800254// properties struct {
255// MyProperty string
256// }
257// }
258//
Colin Cross36242852017-06-23 15:06:31 -0700259// func NewMyModule() android.Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800260// m := &myModule{}
Colin Cross36242852017-06-23 15:06:31 -0700261// m.AddProperties(&m.properties)
262// android.InitAndroidModule(m)
263// return m
Colin Cross3f40fa42015-01-30 17:27:36 -0800264// }
265//
Nan Zhangb9eeb1d2017-02-02 10:46:07 -0800266// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800267// // Get the CPU architecture for the current build variant.
268// variantArch := ctx.Arch()
269//
270// // ...
271// }
Colin Cross635c3b02016-05-18 15:37:25 -0700272type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800273 // Putting the curiously recurring thing pointing to the thing that contains
274 // the thing pattern to good use.
Colin Cross36242852017-06-23 15:06:31 -0700275 // TODO: remove this
Colin Cross635c3b02016-05-18 15:37:25 -0700276 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800277
Colin Crossfc754582016-05-17 16:34:16 -0700278 nameProperties nameProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800279 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700280 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800281 hostAndDeviceProperties hostAndDeviceProperties
282 generalProperties []interface{}
Dan Willemsenb1957a52016-06-23 23:44:54 -0700283 archProperties []interface{}
Colin Crossa120ec12016-08-19 16:07:38 -0700284 customizableProperties []interface{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800285
286 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700287 installFiles Paths
288 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700289
290 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
291 // Only set on the final variant of each module
292 installTarget string
293 checkbuildTarget string
294 blueprintDir string
Colin Crossa120ec12016-08-19 16:07:38 -0700295
Colin Cross178a5092016-09-13 13:42:32 -0700296 hooks hooks
Colin Cross36242852017-06-23 15:06:31 -0700297
298 registerProps []interface{}
299}
300
301func (a *ModuleBase) AddProperties(props ...interface{}) {
302 a.registerProps = append(a.registerProps, props...)
303}
304
305func (a *ModuleBase) GetProperties() []interface{} {
306 return a.registerProps
Colin Cross3f40fa42015-01-30 17:27:36 -0800307}
308
Colin Crossce75d2c2016-10-06 16:12:58 -0700309// Name returns the name of the module. It may be overridden by individual module types, for
310// example prebuilts will prepend prebuilt_ to the name.
Colin Crossfc754582016-05-17 16:34:16 -0700311func (a *ModuleBase) Name() string {
312 return a.nameProperties.Name
313}
314
Colin Crossce75d2c2016-10-06 16:12:58 -0700315// BaseModuleName returns the name of the module as specified in the blueprints file.
316func (a *ModuleBase) BaseModuleName() string {
317 return a.nameProperties.Name
318}
319
Colin Cross635c3b02016-05-18 15:37:25 -0700320func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800321 return a
322}
323
Colin Cross8b74d172016-09-13 09:59:14 -0700324func (a *ModuleBase) SetTarget(target Target, primary bool) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700325 a.commonProperties.CompileTarget = target
Colin Cross8b74d172016-09-13 09:59:14 -0700326 a.commonProperties.CompilePrimary = primary
Colin Crossd3ba0392015-05-07 14:11:29 -0700327}
328
Colin Crossa1ad8d12016-06-01 17:09:44 -0700329func (a *ModuleBase) Target() Target {
330 return a.commonProperties.CompileTarget
Dan Willemsen490fd492015-11-24 17:53:15 -0800331}
332
Colin Cross8b74d172016-09-13 09:59:14 -0700333func (a *ModuleBase) TargetPrimary() bool {
334 return a.commonProperties.CompilePrimary
335}
336
Colin Crossa1ad8d12016-06-01 17:09:44 -0700337func (a *ModuleBase) Os() OsType {
338 return a.Target().Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800339}
340
Colin Cross635c3b02016-05-18 15:37:25 -0700341func (a *ModuleBase) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700342 return a.Os().Class == Host || a.Os().Class == HostCross
Dan Willemsen97750522016-02-09 17:43:51 -0800343}
344
Colin Cross635c3b02016-05-18 15:37:25 -0700345func (a *ModuleBase) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700346 return a.Target().Arch
Dan Willemsen97750522016-02-09 17:43:51 -0800347}
348
Dan Willemsen0b24c742016-10-04 15:13:37 -0700349func (a *ModuleBase) ArchSpecific() bool {
350 return a.commonProperties.ArchSpecific
351}
352
Colin Crossa1ad8d12016-06-01 17:09:44 -0700353func (a *ModuleBase) OsClassSupported() []OsClass {
354 switch a.commonProperties.HostOrDeviceSupported {
355 case HostSupported:
Colin Crossa1ad8d12016-06-01 17:09:44 -0700356 return []OsClass{Host, HostCross}
Dan Albertc6345fb2016-10-20 01:36:11 -0700357 case HostSupportedNoCross:
358 return []OsClass{Host}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700359 case DeviceSupported:
360 return []OsClass{Device}
361 case HostAndDeviceSupported:
362 var supported []OsClass
Colin Crossa4190c12016-07-12 13:11:25 -0700363 if Bool(a.hostAndDeviceProperties.Host_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700364 supported = append(supported, Host, HostCross)
365 }
Colin Crossa4190c12016-07-12 13:11:25 -0700366 if Bool(a.hostAndDeviceProperties.Device_supported) {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700367 supported = append(supported, Device)
368 }
369 return supported
370 default:
371 return nil
372 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800373}
374
Colin Cross635c3b02016-05-18 15:37:25 -0700375func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800376 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
377 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
Colin Crossa4190c12016-07-12 13:11:25 -0700378 Bool(a.hostAndDeviceProperties.Device_supported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800379}
380
Colin Cross635c3b02016-05-18 15:37:25 -0700381func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800382 if a.commonProperties.Enabled == nil {
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800383 return !a.Os().DefaultDisabled
Dan Willemsen490fd492015-11-24 17:53:15 -0800384 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800385 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800386}
387
Colin Crossce75d2c2016-10-06 16:12:58 -0700388func (a *ModuleBase) SkipInstall() {
389 a.commonProperties.SkipInstall = true
390}
391
Colin Cross635c3b02016-05-18 15:37:25 -0700392func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700393 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800394
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700395 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800396 ctx.VisitDepsDepthFirstIf(isFileInstaller,
397 func(m blueprint.Module) {
398 fileInstaller := m.(fileInstaller)
399 files := fileInstaller.filesToInstall()
400 result = append(result, files...)
401 })
402
403 return result
404}
405
Colin Cross635c3b02016-05-18 15:37:25 -0700406func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800407 return a.installFiles
408}
409
Colin Cross635c3b02016-05-18 15:37:25 -0700410func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800411 return p.noAddressSanitizer
412}
413
Colin Cross635c3b02016-05-18 15:37:25 -0700414func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800415 return false
416}
417
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700418func (p *ModuleBase) InstallInSanitizerDir() bool {
419 return false
420}
421
Colin Cross635c3b02016-05-18 15:37:25 -0700422func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700423 allInstalledFiles := Paths{}
424 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800425 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700426 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700427 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
428 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800429 })
430
Colin Cross9454bfa2015-03-17 13:24:18 -0700431 deps := []string{}
432
Colin Cross3f40fa42015-01-30 17:27:36 -0800433 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700434 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800435 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700436 Rule: blueprint.Phony,
437 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700438 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800439 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700440 })
441 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700442 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700443 }
444
445 if len(allCheckbuildFiles) > 0 {
446 name := ctx.ModuleName() + "-checkbuild"
447 ctx.Build(pctx, blueprint.BuildParams{
448 Rule: blueprint.Phony,
449 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700450 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700451 Optional: true,
452 })
453 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700454 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700455 }
456
457 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800458 suffix := ""
459 if ctx.Config().(Config).EmbeddedInMake() {
460 suffix = "-soong"
461 }
462
Colin Cross9454bfa2015-03-17 13:24:18 -0700463 ctx.Build(pctx, blueprint.BuildParams{
464 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800465 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700466 Implicits: deps,
467 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800468 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700469
470 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800471 }
472}
473
Colin Cross635c3b02016-05-18 15:37:25 -0700474func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700475 return androidBaseContextImpl{
Colin Cross8b74d172016-09-13 09:59:14 -0700476 target: a.commonProperties.CompileTarget,
477 targetPrimary: a.commonProperties.CompilePrimary,
Dan Willemsenaa118f92017-04-06 12:49:58 -0700478 vendor: a.commonProperties.Proprietary || a.commonProperties.Vendor,
Colin Cross8b74d172016-09-13 09:59:14 -0700479 config: ctx.Config().(Config),
Colin Cross3f40fa42015-01-30 17:27:36 -0800480 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800481}
482
Colin Cross635c3b02016-05-18 15:37:25 -0700483func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800484 androidCtx := &androidModuleContext{
Colin Cross8d8f8e22016-08-03 11:57:50 -0700485 module: a.module,
Colin Cross6362e272015-10-29 15:25:03 -0700486 ModuleContext: ctx,
487 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
488 installDeps: a.computeInstallDeps(ctx),
489 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800490 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800491 }
492
Colin Cross67a5c132017-05-09 13:45:28 -0700493 desc := "//" + ctx.ModuleDir() + ":" + ctx.ModuleName() + " "
494 var suffix []string
495 if androidCtx.Os().Class != Device && androidCtx.Os().Class != Generic {
496 suffix = append(suffix, androidCtx.Os().String())
497 }
498 if !androidCtx.PrimaryArch() {
499 suffix = append(suffix, androidCtx.Arch().ArchType.String())
500 }
501
502 ctx.Variable(pctx, "moduleDesc", desc)
503
504 s := ""
505 if len(suffix) > 0 {
506 s = " [" + strings.Join(suffix, " ") + "]"
507 }
508 ctx.Variable(pctx, "moduleDescSuffix", s)
509
Colin Cross9b1d13d2016-09-19 15:18:11 -0700510 if a.Enabled() {
511 a.module.GenerateAndroidBuildActions(androidCtx)
512 if ctx.Failed() {
513 return
514 }
515
516 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
517 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800518 }
519
Colin Cross9b1d13d2016-09-19 15:18:11 -0700520 if a == ctx.FinalModule().(Module).base() {
521 a.generateModuleTarget(ctx)
522 if ctx.Failed() {
523 return
524 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800525 }
526}
527
Colin Crossf6566ed2015-03-24 11:13:38 -0700528type androidBaseContextImpl struct {
Colin Cross8b74d172016-09-13 09:59:14 -0700529 target Target
530 targetPrimary bool
531 debug bool
Dan Willemsenaa118f92017-04-06 12:49:58 -0700532 vendor bool
Colin Cross8b74d172016-09-13 09:59:14 -0700533 config Config
Colin Crossf6566ed2015-03-24 11:13:38 -0700534}
535
Colin Cross3f40fa42015-01-30 17:27:36 -0800536type androidModuleContext struct {
537 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700538 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700539 installDeps Paths
540 installFiles Paths
541 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800542 missingDeps []string
Colin Cross8d8f8e22016-08-03 11:57:50 -0700543 module Module
Colin Cross6ff51382015-12-17 16:39:19 -0800544}
545
Colin Cross67a5c132017-05-09 13:45:28 -0700546func (a *androidModuleContext) ninjaError(desc string, outputs []string, err error) {
Colin Cross6ff51382015-12-17 16:39:19 -0800547 a.ModuleContext.Build(pctx, blueprint.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700548 Rule: ErrorRule,
549 Description: desc,
550 Outputs: outputs,
551 Optional: true,
Colin Cross6ff51382015-12-17 16:39:19 -0800552 Args: map[string]string{
553 "error": err.Error(),
554 },
555 })
556 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800557}
558
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800559func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Cross7f19f372016-11-01 11:10:25 -0700560 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700561 a.ninjaError(params.Description, params.Outputs,
562 fmt.Errorf("module %s missing dependencies: %s\n",
563 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800564 return
565 }
566
Colin Cross3f40fa42015-01-30 17:27:36 -0800567 params.Optional = true
568 a.ModuleContext.Build(pctx, params)
569}
570
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700571func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
572 bparams := blueprint.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700573 Rule: params.Rule,
Colin Cross33bfb0a2016-11-21 17:23:08 -0800574 Deps: params.Deps,
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700575 Outputs: params.Outputs.Strings(),
576 ImplicitOutputs: params.ImplicitOutputs.Strings(),
577 Inputs: params.Inputs.Strings(),
578 Implicits: params.Implicits.Strings(),
579 OrderOnly: params.OrderOnly.Strings(),
580 Args: params.Args,
581 Optional: !params.Default,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700582 }
583
Colin Cross67a5c132017-05-09 13:45:28 -0700584 if params.Description != "" {
585 bparams.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
586 }
587
Colin Cross33bfb0a2016-11-21 17:23:08 -0800588 if params.Depfile != nil {
589 bparams.Depfile = params.Depfile.String()
590 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700591 if params.Output != nil {
592 bparams.Outputs = append(bparams.Outputs, params.Output.String())
593 }
Dan Willemsen9f3c5742016-11-03 14:28:31 -0700594 if params.ImplicitOutput != nil {
595 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
596 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700597 if params.Input != nil {
598 bparams.Inputs = append(bparams.Inputs, params.Input.String())
599 }
600 if params.Implicit != nil {
601 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
602 }
603
Colin Cross6ff51382015-12-17 16:39:19 -0800604 if a.missingDeps != nil {
Colin Cross67a5c132017-05-09 13:45:28 -0700605 a.ninjaError(bparams.Description, bparams.Outputs,
606 fmt.Errorf("module %s missing dependencies: %s\n",
607 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
Colin Cross6ff51382015-12-17 16:39:19 -0800608 return
609 }
610
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700611 a.ModuleContext.Build(pctx, bparams)
612}
613
Colin Cross6ff51382015-12-17 16:39:19 -0800614func (a *androidModuleContext) GetMissingDependencies() []string {
615 return a.missingDeps
616}
617
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800618func (a *androidModuleContext) AddMissingDependencies(deps []string) {
619 if deps != nil {
620 a.missingDeps = append(a.missingDeps, deps...)
621 }
622}
623
Colin Crossa1ad8d12016-06-01 17:09:44 -0700624func (a *androidBaseContextImpl) Target() Target {
625 return a.target
626}
627
Colin Cross8b74d172016-09-13 09:59:14 -0700628func (a *androidBaseContextImpl) TargetPrimary() bool {
629 return a.targetPrimary
630}
631
Colin Crossf6566ed2015-03-24 11:13:38 -0700632func (a *androidBaseContextImpl) Arch() Arch {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700633 return a.target.Arch
Colin Cross3f40fa42015-01-30 17:27:36 -0800634}
635
Colin Crossa1ad8d12016-06-01 17:09:44 -0700636func (a *androidBaseContextImpl) Os() OsType {
637 return a.target.Os
Dan Willemsen490fd492015-11-24 17:53:15 -0800638}
639
Colin Crossf6566ed2015-03-24 11:13:38 -0700640func (a *androidBaseContextImpl) Host() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700641 return a.target.Os.Class == Host || a.target.Os.Class == HostCross
Colin Crossf6566ed2015-03-24 11:13:38 -0700642}
643
644func (a *androidBaseContextImpl) Device() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700645 return a.target.Os.Class == Device
Colin Crossf6566ed2015-03-24 11:13:38 -0700646}
647
Colin Cross0af4b842015-04-30 16:36:18 -0700648func (a *androidBaseContextImpl) Darwin() bool {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700649 return a.target.Os == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700650}
651
Colin Cross3edeee12017-04-04 12:59:48 -0700652func (a *androidBaseContextImpl) Windows() bool {
653 return a.target.Os == Windows
654}
655
Colin Crossf6566ed2015-03-24 11:13:38 -0700656func (a *androidBaseContextImpl) Debug() bool {
657 return a.debug
658}
659
Colin Cross1e7d3702016-08-24 15:25:47 -0700660func (a *androidBaseContextImpl) PrimaryArch() bool {
Colin Cross67a5c132017-05-09 13:45:28 -0700661 if len(a.config.Targets[a.target.Os.Class]) <= 1 {
662 return true
663 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700664 return a.target.Arch.ArchType == a.config.Targets[a.target.Os.Class][0].Arch.ArchType
665}
666
Colin Cross1332b002015-04-07 17:11:30 -0700667func (a *androidBaseContextImpl) AConfig() Config {
668 return a.config
669}
670
Colin Cross9272ade2016-08-17 15:24:12 -0700671func (a *androidBaseContextImpl) DeviceConfig() DeviceConfig {
672 return DeviceConfig{a.config.deviceConfig}
673}
674
Dan Willemsenaa118f92017-04-06 12:49:58 -0700675func (a *androidBaseContextImpl) Vendor() bool {
676 return a.vendor
Dan Willemsen782a2d12015-12-21 14:55:28 -0800677}
678
Colin Cross8d8f8e22016-08-03 11:57:50 -0700679func (a *androidModuleContext) InstallInData() bool {
680 return a.module.InstallInData()
Dan Willemsen782a2d12015-12-21 14:55:28 -0800681}
682
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700683func (a *androidModuleContext) InstallInSanitizerDir() bool {
684 return a.module.InstallInSanitizerDir()
685}
686
Colin Cross893d8162017-04-26 17:34:03 -0700687func (a *androidModuleContext) skipInstall(fullInstallPath OutputPath) bool {
688 if a.module.base().commonProperties.SkipInstall {
689 return true
690 }
691
692 if a.Device() {
693 if a.AConfig().SkipDeviceInstall() {
694 return true
695 }
696
697 if a.AConfig().SkipMegaDeviceInstall(fullInstallPath.String()) {
698 return true
699 }
700 }
701
702 return false
703}
704
Dan Willemsen782a2d12015-12-21 14:55:28 -0800705func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700706 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700707
Dan Willemsen782a2d12015-12-21 14:55:28 -0800708 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700709 a.module.base().hooks.runInstallHooks(a, fullInstallPath, false)
Colin Cross3f40fa42015-01-30 17:27:36 -0800710
Colin Cross893d8162017-04-26 17:34:03 -0700711 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700712
Dan Willemsen322acaf2016-01-12 23:07:05 -0800713 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700714
Colin Cross89562dc2016-10-03 17:47:19 -0700715 var implicitDeps, orderOnlyDeps Paths
716
717 if a.Host() {
718 // Installed host modules might be used during the build, depend directly on their
719 // dependencies so their timestamp is updated whenever their dependency is updated
720 implicitDeps = deps
721 } else {
722 orderOnlyDeps = deps
723 }
724
Dan Willemsen322acaf2016-01-12 23:07:05 -0800725 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700726 Rule: Cp,
727 Description: "install " + fullInstallPath.Base(),
728 Output: fullInstallPath,
729 Input: srcPath,
730 Implicits: implicitDeps,
731 OrderOnly: orderOnlyDeps,
732 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800733 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800734
Dan Willemsen322acaf2016-01-12 23:07:05 -0800735 a.installFiles = append(a.installFiles, fullInstallPath)
736 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700737 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700738 return fullInstallPath
739}
740
Colin Crossa2344662016-03-24 13:14:12 -0700741func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700742 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800743}
744
Colin Cross3854a602016-01-11 12:49:11 -0800745func (a *androidModuleContext) InstallSymlink(installPath OutputPath, name string, srcPath OutputPath) OutputPath {
746 fullInstallPath := installPath.Join(a, name)
Colin Cross178a5092016-09-13 13:42:32 -0700747 a.module.base().hooks.runInstallHooks(a, fullInstallPath, true)
Colin Cross3854a602016-01-11 12:49:11 -0800748
Colin Cross893d8162017-04-26 17:34:03 -0700749 if !a.skipInstall(fullInstallPath) {
Colin Crossce75d2c2016-10-06 16:12:58 -0700750
Colin Cross12fc4972016-01-11 12:49:11 -0800751 a.ModuleBuild(pctx, ModuleBuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700752 Rule: Symlink,
753 Description: "install symlink " + fullInstallPath.Base(),
754 Output: fullInstallPath,
755 OrderOnly: Paths{srcPath},
756 Default: !a.AConfig().EmbeddedInMake(),
Colin Cross12fc4972016-01-11 12:49:11 -0800757 Args: map[string]string{
758 "fromPath": srcPath.String(),
759 },
760 })
Colin Cross3854a602016-01-11 12:49:11 -0800761
Colin Cross12fc4972016-01-11 12:49:11 -0800762 a.installFiles = append(a.installFiles, fullInstallPath)
763 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
764 }
Colin Cross3854a602016-01-11 12:49:11 -0800765 return fullInstallPath
766}
767
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700768func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800769 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
770}
771
Colin Cross3f40fa42015-01-30 17:27:36 -0800772type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700773 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800774}
775
776func isFileInstaller(m blueprint.Module) bool {
777 _, ok := m.(fileInstaller)
778 return ok
779}
780
781func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700782 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800783 return ok
784}
Colin Crossfce53272015-04-08 11:21:40 -0700785
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700786func findStringInSlice(str string, slice []string) int {
787 for i, s := range slice {
788 if s == str {
789 return i
Colin Crossfce53272015-04-08 11:21:40 -0700790 }
791 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700792 return -1
793}
794
Colin Cross068e0fe2016-12-13 15:23:47 -0800795func SrcIsModule(s string) string {
796 if len(s) > 1 && s[0] == ':' {
797 return s[1:]
798 }
799 return ""
800}
801
802type sourceDependencyTag struct {
803 blueprint.BaseDependencyTag
804}
805
806var SourceDepTag sourceDependencyTag
807
808// Returns a list of modules that must be depended on to satisfy filegroup or generated sources
809// modules listed in srcFiles using ":module" syntax
810func ExtractSourcesDeps(ctx BottomUpMutatorContext, srcFiles []string) {
811 var deps []string
Nan Zhang2439eb72017-04-10 11:27:50 -0700812 set := make(map[string]bool)
813
Colin Cross068e0fe2016-12-13 15:23:47 -0800814 for _, s := range srcFiles {
815 if m := SrcIsModule(s); m != "" {
Nan Zhang2439eb72017-04-10 11:27:50 -0700816 if _, found := set[m]; found {
817 ctx.ModuleErrorf("found source dependency duplicate: %q!", m)
818 } else {
819 set[m] = true
820 deps = append(deps, m)
821 }
Colin Cross068e0fe2016-12-13 15:23:47 -0800822 }
823 }
824
825 ctx.AddDependency(ctx.Module(), SourceDepTag, deps...)
826}
827
828type SourceFileProducer interface {
829 Srcs() Paths
830}
831
832// Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800833// ExtractSourcesDeps must have already been called during the dependency resolution phase.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700834func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800835 return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
836}
837
838func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700839 prefix := PathForModuleSrc(ctx).String()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800840
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700841 for i, e := range excludes {
842 j := findStringInSlice(e, srcFiles)
843 if j != -1 {
844 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
845 }
846
847 excludes[i] = filepath.Join(prefix, e)
848 }
849
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800850 expandedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700851 for _, s := range srcFiles {
Colin Cross068e0fe2016-12-13 15:23:47 -0800852 if m := SrcIsModule(s); m != "" {
853 module := ctx.GetDirectDepWithTag(m, SourceDepTag)
854 if srcProducer, ok := module.(SourceFileProducer); ok {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800855 expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
Colin Cross068e0fe2016-12-13 15:23:47 -0800856 } else {
857 ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
858 }
859 } else if pathtools.IsGlob(s) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800860 globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
861 expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
862 for i, s := range expandedSrcFiles {
863 expandedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
864 }
Colin Cross8f101b42015-06-17 15:09:06 -0700865 } else {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800866 s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
867 expandedSrcFiles = append(expandedSrcFiles, s)
Colin Cross8f101b42015-06-17 15:09:06 -0700868 }
869 }
870
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800871 return expandedSrcFiles
Colin Cross8f101b42015-06-17 15:09:06 -0700872}
873
Nan Zhang6d34b302017-02-04 17:47:46 -0800874func (ctx *androidModuleContext) RequiredModuleNames() []string {
875 return ctx.module.base().commonProperties.Required
876}
877
Colin Cross7f19f372016-11-01 11:10:25 -0700878func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
879 ret, err := ctx.GlobWithDeps(globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700880 if err != nil {
881 ctx.ModuleErrorf("glob: %s", err.Error())
882 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700883 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700884}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700885
Colin Cross463a90e2015-06-17 14:20:06 -0700886func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700887 RegisterSingletonType("buildtarget", BuildTargetSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -0700888}
889
Colin Cross1f8c52b2015-06-16 16:38:17 -0700890func BuildTargetSingleton() blueprint.Singleton {
891 return &buildTargetSingleton{}
892}
893
Colin Cross87d8b562017-04-25 10:01:55 -0700894func parentDir(dir string) string {
895 dir, _ = filepath.Split(dir)
896 return filepath.Clean(dir)
897}
898
Colin Cross1f8c52b2015-06-16 16:38:17 -0700899type buildTargetSingleton struct{}
900
901func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
902 checkbuildDeps := []string{}
903
Colin Cross87d8b562017-04-25 10:01:55 -0700904 mmTarget := func(dir string) string {
905 return filepath.Join("mm", dir)
906 }
907
908 modulesInDir := make(map[string][]string)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700909
910 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700911 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700912 blueprintDir := a.base().blueprintDir
913 installTarget := a.base().installTarget
914 checkbuildTarget := a.base().checkbuildTarget
915
916 if checkbuildTarget != "" {
917 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
Colin Cross87d8b562017-04-25 10:01:55 -0700918 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], checkbuildTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700919 }
920
921 if installTarget != "" {
Colin Cross87d8b562017-04-25 10:01:55 -0700922 modulesInDir[blueprintDir] = append(modulesInDir[blueprintDir], installTarget)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700923 }
924 }
925 })
926
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800927 suffix := ""
928 if ctx.Config().(Config).EmbeddedInMake() {
929 suffix = "-soong"
930 }
931
Colin Cross1f8c52b2015-06-16 16:38:17 -0700932 // Create a top-level checkbuild target that depends on all modules
933 ctx.Build(pctx, blueprint.BuildParams{
934 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800935 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700936 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700937 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700938 })
939
Colin Cross87d8b562017-04-25 10:01:55 -0700940 // Ensure ancestor directories are in modulesInDir
941 dirs := sortedKeys(modulesInDir)
942 for _, dir := range dirs {
943 dir := parentDir(dir)
944 for dir != "." && dir != "/" {
945 if _, exists := modulesInDir[dir]; exists {
946 break
947 }
948 modulesInDir[dir] = nil
949 dir = parentDir(dir)
950 }
951 }
952
953 // Make directories build their direct subdirectories
954 dirs = sortedKeys(modulesInDir)
955 for _, dir := range dirs {
956 p := parentDir(dir)
957 if p != "." && p != "/" {
958 modulesInDir[p] = append(modulesInDir[p], mmTarget(dir))
959 }
960 }
961
962 // Create a mm/<directory> target that depends on all modules in a directory, and depends
963 // on the mm/* targets of all of its subdirectories that contain Android.bp files.
Colin Cross1f8c52b2015-06-16 16:38:17 -0700964 for _, dir := range dirs {
965 ctx.Build(pctx, blueprint.BuildParams{
966 Rule: blueprint.Phony,
Colin Cross87d8b562017-04-25 10:01:55 -0700967 Outputs: []string{mmTarget(dir)},
968 Implicits: modulesInDir[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800969 // HACK: checkbuild should be an optional build, but force it
970 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800971 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700972 })
973 }
974}
Colin Crossd779da42015-12-17 18:00:23 -0800975
976type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700977 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800978 ctx interface {
979 ModuleName(blueprint.Module) string
980 ModuleSubDir(blueprint.Module) string
981 }
982}
983
984func (s AndroidModulesByName) Len() int { return len(s.slice) }
985func (s AndroidModulesByName) Less(i, j int) bool {
986 mi, mj := s.slice[i], s.slice[j]
987 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
988
989 if ni != nj {
990 return ni < nj
991 } else {
992 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
993 }
994}
995func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }