blob: 08abf78a945417ad1b1926ea2de9b09359f6351b [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6ff51382015-12-17 16:39:19 -080018 "fmt"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "path/filepath"
Colin Cross6ff51382015-12-17 16:39:19 -080020 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070021
Dan Willemsen0effe062015-11-30 16:06:01 -080022 "android/soong"
Colin Cross8f101b42015-06-17 15:09:06 -070023 "android/soong/glob"
24
Colin Crossf6566ed2015-03-24 11:13:38 -070025 "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -080026)
27
28var (
29 DeviceSharedLibrary = "shared_library"
30 DeviceStaticLibrary = "static_library"
31 DeviceExecutable = "executable"
32 HostSharedLibrary = "host_shared_library"
33 HostStaticLibrary = "host_static_library"
34 HostExecutable = "host_executable"
35)
36
Dan Willemsen34cc69e2015-09-23 15:26:20 -070037type ModuleBuildParams struct {
38 Rule blueprint.Rule
39 Output WritablePath
40 Outputs WritablePaths
41 Input Path
42 Inputs Paths
43 Implicit Path
44 Implicits Paths
45 OrderOnly Paths
46 Default bool
47 Args map[string]string
48}
49
Colin Crossf6566ed2015-03-24 11:13:38 -070050type androidBaseContext interface {
51 Arch() Arch
Colin Crossd3ba0392015-05-07 14:11:29 -070052 HostOrDevice() HostOrDevice
Dan Willemsen490fd492015-11-24 17:53:15 -080053 HostType() HostType
Colin Crossf6566ed2015-03-24 11:13:38 -070054 Host() bool
55 Device() bool
Colin Cross0af4b842015-04-30 16:36:18 -070056 Darwin() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070057 Debug() bool
Colin Cross1332b002015-04-07 17:11:30 -070058 AConfig() Config
Dan Willemsen782a2d12015-12-21 14:55:28 -080059 Proprietary() bool
60 InstallInData() bool
Colin Crossf6566ed2015-03-24 11:13:38 -070061}
62
Colin Cross635c3b02016-05-18 15:37:25 -070063type BaseContext interface {
Colin Crossf6566ed2015-03-24 11:13:38 -070064 blueprint.BaseModuleContext
65 androidBaseContext
66}
67
Colin Cross635c3b02016-05-18 15:37:25 -070068type ModuleContext interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080069 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -070070 androidBaseContext
Colin Cross3f40fa42015-01-30 17:27:36 -080071
Dan Willemsen34cc69e2015-09-23 15:26:20 -070072 // Similar to Build, but takes Paths instead of []string,
73 // and performs more verification.
74 ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
Colin Cross8f101b42015-06-17 15:09:06 -070075
Dan Willemsen34cc69e2015-09-23 15:26:20 -070076 ExpandSources(srcFiles, excludes []string) Paths
77 Glob(outDir, globPattern string, excludes []string) Paths
78
Colin Crossa2344662016-03-24 13:14:12 -070079 InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
80 InstallFileName(installPath OutputPath, name string, srcPath Path, deps ...Path) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 CheckbuildFile(srcPath Path)
Dan Willemsen6553f5e2016-03-10 18:14:25 -080082
83 AddMissingDependencies(deps []string)
Colin Cross3f40fa42015-01-30 17:27:36 -080084}
85
Colin Cross635c3b02016-05-18 15:37:25 -070086type Module interface {
Colin Cross3f40fa42015-01-30 17:27:36 -080087 blueprint.Module
88
Colin Cross635c3b02016-05-18 15:37:25 -070089 GenerateAndroidBuildActions(ModuleContext)
Colin Cross3f40fa42015-01-30 17:27:36 -080090
Colin Cross635c3b02016-05-18 15:37:25 -070091 base() *ModuleBase
Dan Willemsen0effe062015-11-30 16:06:01 -080092 Enabled() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080093 HostOrDevice() HostOrDevice
Dan Willemsen782a2d12015-12-21 14:55:28 -080094 InstallInData() bool
Colin Cross3f40fa42015-01-30 17:27:36 -080095}
96
Colin Cross3f40fa42015-01-30 17:27:36 -080097type commonProperties struct {
Colin Crossc77f9d12015-04-02 13:54:39 -070098 Name string
99 Deps []string
100 Tags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800101
Dan Willemsen0effe062015-11-30 16:06:01 -0800102 // emit build rules for this module
103 Enabled *bool `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800104
Colin Cross7d5136f2015-05-11 13:39:40 -0700105 // control whether this module compiles for 32-bit, 64-bit, or both. Possible values
Colin Cross3f40fa42015-01-30 17:27:36 -0800106 // are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
107 // architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
108 // platform
109 Compile_multilib string
110
Dan Willemsen782a2d12015-12-21 14:55:28 -0800111 // whether this is a proprietary vendor module, and should be installed into /vendor
112 Proprietary bool
113
Colin Crossd3ba0392015-05-07 14:11:29 -0700114 // Set by HostOrDeviceMutator
115 CompileHostOrDevice HostOrDevice `blueprint:"mutated"`
116
Dan Willemsen490fd492015-11-24 17:53:15 -0800117 // Set by HostTypeMutator
118 CompileHostType HostType `blueprint:"mutated"`
119
Colin Cross3f40fa42015-01-30 17:27:36 -0800120 // Set by ArchMutator
121 CompileArch Arch `blueprint:"mutated"`
122
123 // Set by InitAndroidModule
124 HostOrDeviceSupported HostOrDeviceSupported `blueprint:"mutated"`
125}
126
127type hostAndDeviceProperties struct {
128 Host_supported bool
129 Device_supported bool
130}
131
Colin Crossc472d572015-03-17 15:06:21 -0700132type Multilib string
133
134const (
Dan Willemsen218f6562015-07-08 18:13:11 -0700135 MultilibBoth Multilib = "both"
136 MultilibFirst Multilib = "first"
137 MultilibCommon Multilib = "common"
138 MultilibDefault Multilib = ""
Colin Crossc472d572015-03-17 15:06:21 -0700139)
140
Colin Cross635c3b02016-05-18 15:37:25 -0700141func InitAndroidModule(m Module,
Colin Cross3f40fa42015-01-30 17:27:36 -0800142 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
143
144 base := m.base()
145 base.module = m
Colin Cross5049f022015-03-18 13:28:46 -0700146
Colin Cross7f64b6d2015-07-09 13:57:48 -0700147 propertyStructs = append(propertyStructs, &base.commonProperties, &base.variableProperties)
Colin Cross5049f022015-03-18 13:28:46 -0700148
149 return m, propertyStructs
150}
151
Colin Cross635c3b02016-05-18 15:37:25 -0700152func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
Colin Cross5049f022015-03-18 13:28:46 -0700153 propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
154
155 _, propertyStructs = InitAndroidModule(m, propertyStructs...)
156
157 base := m.base()
Colin Cross3f40fa42015-01-30 17:27:36 -0800158 base.commonProperties.HostOrDeviceSupported = hod
Colin Crosscfad1192015-11-02 16:43:11 -0800159 base.commonProperties.Compile_multilib = string(defaultMultilib)
Colin Cross3f40fa42015-01-30 17:27:36 -0800160
Dan Willemsen218f6562015-07-08 18:13:11 -0700161 switch hod {
162 case HostAndDeviceSupported:
Colin Cross3f40fa42015-01-30 17:27:36 -0800163 // Default to module to device supported, host not supported, can override in module
164 // properties
165 base.hostAndDeviceProperties.Device_supported = true
Dan Willemsen218f6562015-07-08 18:13:11 -0700166 fallthrough
167 case HostAndDeviceDefault:
Colin Cross3f40fa42015-01-30 17:27:36 -0800168 propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
169 }
170
Colin Crosscfad1192015-11-02 16:43:11 -0800171 return InitArchModule(m, propertyStructs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800172}
173
174// A AndroidModuleBase object contains the properties that are common to all Android
175// modules. It should be included as an anonymous field in every module
176// struct definition. InitAndroidModule should then be called from the module's
177// factory function, and the return values from InitAndroidModule should be
178// returned from the factory function.
179//
180// The AndroidModuleBase type is responsible for implementing the
181// GenerateBuildActions method to support the blueprint.Module interface. This
182// method will then call the module's GenerateAndroidBuildActions method once
183// for each build variant that is to be built. GenerateAndroidBuildActions is
184// passed a AndroidModuleContext rather than the usual blueprint.ModuleContext.
185// AndroidModuleContext exposes extra functionality specific to the Android build
186// system including details about the particular build variant that is to be
187// generated.
188//
189// For example:
190//
191// import (
192// "android/soong/common"
Colin Cross70b40592015-03-23 12:57:34 -0700193// "github.com/google/blueprint"
Colin Cross3f40fa42015-01-30 17:27:36 -0800194// )
195//
196// type myModule struct {
197// common.AndroidModuleBase
198// properties struct {
199// MyProperty string
200// }
201// }
202//
203// func NewMyModule() (blueprint.Module, []interface{}) {
204// m := &myModule{}
205// return common.InitAndroidModule(m, &m.properties)
206// }
207//
208// func (m *myModule) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
209// // Get the CPU architecture for the current build variant.
210// variantArch := ctx.Arch()
211//
212// // ...
213// }
Colin Cross635c3b02016-05-18 15:37:25 -0700214type ModuleBase struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800215 // Putting the curiously recurring thing pointing to the thing that contains
216 // the thing pattern to good use.
Colin Cross635c3b02016-05-18 15:37:25 -0700217 module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800218
219 commonProperties commonProperties
Colin Cross7f64b6d2015-07-09 13:57:48 -0700220 variableProperties variableProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800221 hostAndDeviceProperties hostAndDeviceProperties
222 generalProperties []interface{}
223 archProperties []*archProperties
224
225 noAddressSanitizer bool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700226 installFiles Paths
227 checkbuildFiles Paths
Colin Cross1f8c52b2015-06-16 16:38:17 -0700228
229 // Used by buildTargetSingleton to create checkbuild and per-directory build targets
230 // Only set on the final variant of each module
231 installTarget string
232 checkbuildTarget string
233 blueprintDir string
Colin Cross3f40fa42015-01-30 17:27:36 -0800234}
235
Colin Cross635c3b02016-05-18 15:37:25 -0700236func (a *ModuleBase) base() *ModuleBase {
Colin Cross3f40fa42015-01-30 17:27:36 -0800237 return a
238}
239
Colin Cross635c3b02016-05-18 15:37:25 -0700240func (a *ModuleBase) SetHostOrDevice(hod HostOrDevice) {
Colin Crossd3ba0392015-05-07 14:11:29 -0700241 a.commonProperties.CompileHostOrDevice = hod
242}
243
Colin Cross635c3b02016-05-18 15:37:25 -0700244func (a *ModuleBase) SetHostType(ht HostType) {
Dan Willemsen490fd492015-11-24 17:53:15 -0800245 a.commonProperties.CompileHostType = ht
246}
247
Colin Cross635c3b02016-05-18 15:37:25 -0700248func (a *ModuleBase) SetArch(arch Arch) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800249 a.commonProperties.CompileArch = arch
250}
251
Colin Cross635c3b02016-05-18 15:37:25 -0700252func (a *ModuleBase) HostOrDevice() HostOrDevice {
Colin Crossd3ba0392015-05-07 14:11:29 -0700253 return a.commonProperties.CompileHostOrDevice
Colin Cross3f40fa42015-01-30 17:27:36 -0800254}
255
Colin Cross635c3b02016-05-18 15:37:25 -0700256func (a *ModuleBase) HostType() HostType {
Dan Willemsen490fd492015-11-24 17:53:15 -0800257 return a.commonProperties.CompileHostType
258}
259
Colin Cross635c3b02016-05-18 15:37:25 -0700260func (a *ModuleBase) Host() bool {
Dan Willemsen97750522016-02-09 17:43:51 -0800261 return a.HostOrDevice().Host()
262}
263
Colin Cross635c3b02016-05-18 15:37:25 -0700264func (a *ModuleBase) Arch() Arch {
Dan Willemsen97750522016-02-09 17:43:51 -0800265 return a.commonProperties.CompileArch
266}
267
Colin Cross635c3b02016-05-18 15:37:25 -0700268func (a *ModuleBase) HostSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800269 return a.commonProperties.HostOrDeviceSupported == HostSupported ||
270 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
271 a.hostAndDeviceProperties.Host_supported
272}
273
Colin Cross635c3b02016-05-18 15:37:25 -0700274func (a *ModuleBase) DeviceSupported() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800275 return a.commonProperties.HostOrDeviceSupported == DeviceSupported ||
276 a.commonProperties.HostOrDeviceSupported == HostAndDeviceSupported &&
277 a.hostAndDeviceProperties.Device_supported
278}
279
Colin Cross635c3b02016-05-18 15:37:25 -0700280func (a *ModuleBase) Enabled() bool {
Dan Willemsen0effe062015-11-30 16:06:01 -0800281 if a.commonProperties.Enabled == nil {
282 if a.HostSupported() && a.HostOrDevice().Host() && a.HostType() == Windows {
283 return false
284 } else {
285 return true
286 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800287 }
Dan Willemsen0effe062015-11-30 16:06:01 -0800288 return *a.commonProperties.Enabled
Colin Cross3f40fa42015-01-30 17:27:36 -0800289}
290
Colin Cross635c3b02016-05-18 15:37:25 -0700291func (a *ModuleBase) computeInstallDeps(
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700292 ctx blueprint.ModuleContext) Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800293
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700294 result := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800295 ctx.VisitDepsDepthFirstIf(isFileInstaller,
296 func(m blueprint.Module) {
297 fileInstaller := m.(fileInstaller)
298 files := fileInstaller.filesToInstall()
299 result = append(result, files...)
300 })
301
302 return result
303}
304
Colin Cross635c3b02016-05-18 15:37:25 -0700305func (a *ModuleBase) filesToInstall() Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800306 return a.installFiles
307}
308
Colin Cross635c3b02016-05-18 15:37:25 -0700309func (p *ModuleBase) NoAddressSanitizer() bool {
Colin Cross3f40fa42015-01-30 17:27:36 -0800310 return p.noAddressSanitizer
311}
312
Colin Cross635c3b02016-05-18 15:37:25 -0700313func (p *ModuleBase) InstallInData() bool {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800314 return false
315}
316
Colin Cross635c3b02016-05-18 15:37:25 -0700317func (a *ModuleBase) generateModuleTarget(ctx blueprint.ModuleContext) {
318 if a != ctx.FinalModule().(Module).base() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800319 return
320 }
321
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700322 allInstalledFiles := Paths{}
323 allCheckbuildFiles := Paths{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800324 ctx.VisitAllModuleVariants(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700325 a := module.(Module).base()
Colin Crossc9404352015-03-26 16:10:12 -0700326 allInstalledFiles = append(allInstalledFiles, a.installFiles...)
327 allCheckbuildFiles = append(allCheckbuildFiles, a.checkbuildFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800328 })
329
Colin Cross9454bfa2015-03-17 13:24:18 -0700330 deps := []string{}
331
Colin Cross3f40fa42015-01-30 17:27:36 -0800332 if len(allInstalledFiles) > 0 {
Colin Cross9454bfa2015-03-17 13:24:18 -0700333 name := ctx.ModuleName() + "-install"
Colin Cross3f40fa42015-01-30 17:27:36 -0800334 ctx.Build(pctx, blueprint.BuildParams{
Colin Cross9454bfa2015-03-17 13:24:18 -0700335 Rule: blueprint.Phony,
336 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700337 Implicits: allInstalledFiles.Strings(),
Colin Cross346aa132015-12-17 17:19:51 -0800338 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700339 })
340 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700341 a.installTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700342 }
343
344 if len(allCheckbuildFiles) > 0 {
345 name := ctx.ModuleName() + "-checkbuild"
346 ctx.Build(pctx, blueprint.BuildParams{
347 Rule: blueprint.Phony,
348 Outputs: []string{name},
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700349 Implicits: allCheckbuildFiles.Strings(),
Colin Cross9454bfa2015-03-17 13:24:18 -0700350 Optional: true,
351 })
352 deps = append(deps, name)
Colin Cross1f8c52b2015-06-16 16:38:17 -0700353 a.checkbuildTarget = name
Colin Cross9454bfa2015-03-17 13:24:18 -0700354 }
355
356 if len(deps) > 0 {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800357 suffix := ""
358 if ctx.Config().(Config).EmbeddedInMake() {
359 suffix = "-soong"
360 }
361
Colin Cross9454bfa2015-03-17 13:24:18 -0700362 ctx.Build(pctx, blueprint.BuildParams{
363 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800364 Outputs: []string{ctx.ModuleName() + suffix},
Colin Cross9454bfa2015-03-17 13:24:18 -0700365 Implicits: deps,
366 Optional: true,
Colin Cross3f40fa42015-01-30 17:27:36 -0800367 })
Colin Cross1f8c52b2015-06-16 16:38:17 -0700368
369 a.blueprintDir = ctx.ModuleDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800370 }
371}
372
Colin Cross635c3b02016-05-18 15:37:25 -0700373func (a *ModuleBase) androidBaseContextFactory(ctx blueprint.BaseModuleContext) androidBaseContextImpl {
Colin Cross6362e272015-10-29 15:25:03 -0700374 return androidBaseContextImpl{
Dan Willemsen782a2d12015-12-21 14:55:28 -0800375 arch: a.commonProperties.CompileArch,
376 hod: a.commonProperties.CompileHostOrDevice,
377 ht: a.commonProperties.CompileHostType,
378 proprietary: a.commonProperties.Proprietary,
379 config: ctx.Config().(Config),
380 installInData: a.module.InstallInData(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800382}
383
Colin Cross635c3b02016-05-18 15:37:25 -0700384func (a *ModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800385 androidCtx := &androidModuleContext{
Colin Cross6362e272015-10-29 15:25:03 -0700386 ModuleContext: ctx,
387 androidBaseContextImpl: a.androidBaseContextFactory(ctx),
388 installDeps: a.computeInstallDeps(ctx),
389 installFiles: a.installFiles,
Colin Cross6ff51382015-12-17 16:39:19 -0800390 missingDeps: ctx.GetMissingDependencies(),
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 }
392
Dan Willemsen0effe062015-11-30 16:06:01 -0800393 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800394 return
395 }
396
397 a.module.GenerateAndroidBuildActions(androidCtx)
398 if ctx.Failed() {
399 return
400 }
401
Colin Crossc9404352015-03-26 16:10:12 -0700402 a.installFiles = append(a.installFiles, androidCtx.installFiles...)
403 a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
404
Colin Cross3f40fa42015-01-30 17:27:36 -0800405 a.generateModuleTarget(ctx)
406 if ctx.Failed() {
407 return
408 }
409}
410
Colin Crossf6566ed2015-03-24 11:13:38 -0700411type androidBaseContextImpl struct {
Dan Willemsen782a2d12015-12-21 14:55:28 -0800412 arch Arch
413 hod HostOrDevice
414 ht HostType
415 debug bool
416 config Config
417 proprietary bool
418 installInData bool
Colin Crossf6566ed2015-03-24 11:13:38 -0700419}
420
Colin Cross3f40fa42015-01-30 17:27:36 -0800421type androidModuleContext struct {
422 blueprint.ModuleContext
Colin Crossf6566ed2015-03-24 11:13:38 -0700423 androidBaseContextImpl
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700424 installDeps Paths
425 installFiles Paths
426 checkbuildFiles Paths
Colin Cross6ff51382015-12-17 16:39:19 -0800427 missingDeps []string
428}
429
430func (a *androidModuleContext) ninjaError(outputs []string, err error) {
431 a.ModuleContext.Build(pctx, blueprint.BuildParams{
432 Rule: ErrorRule,
433 Outputs: outputs,
434 Optional: true,
435 Args: map[string]string{
436 "error": err.Error(),
437 },
438 })
439 return
Colin Cross3f40fa42015-01-30 17:27:36 -0800440}
441
Dan Willemsen14e5c2a2015-11-30 13:59:34 -0800442func (a *androidModuleContext) Build(pctx blueprint.PackageContext, params blueprint.BuildParams) {
Colin Crosse2c48742016-04-27 13:47:35 -0700443 if a.missingDeps != nil && params.Rule != globRule {
Colin Cross6ff51382015-12-17 16:39:19 -0800444 a.ninjaError(params.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
445 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
446 return
447 }
448
Colin Cross3f40fa42015-01-30 17:27:36 -0800449 params.Optional = true
450 a.ModuleContext.Build(pctx, params)
451}
452
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700453func (a *androidModuleContext) ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams) {
454 bparams := blueprint.BuildParams{
455 Rule: params.Rule,
456 Outputs: params.Outputs.Strings(),
457 Inputs: params.Inputs.Strings(),
458 Implicits: params.Implicits.Strings(),
459 OrderOnly: params.OrderOnly.Strings(),
460 Args: params.Args,
461 Optional: !params.Default,
462 }
463
464 if params.Output != nil {
465 bparams.Outputs = append(bparams.Outputs, params.Output.String())
466 }
467 if params.Input != nil {
468 bparams.Inputs = append(bparams.Inputs, params.Input.String())
469 }
470 if params.Implicit != nil {
471 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
472 }
473
Colin Cross6ff51382015-12-17 16:39:19 -0800474 if a.missingDeps != nil {
475 a.ninjaError(bparams.Outputs, fmt.Errorf("module %s missing dependencies: %s\n",
476 a.ModuleName(), strings.Join(a.missingDeps, ", ")))
477 return
478 }
479
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700480 a.ModuleContext.Build(pctx, bparams)
481}
482
Colin Cross6ff51382015-12-17 16:39:19 -0800483func (a *androidModuleContext) GetMissingDependencies() []string {
484 return a.missingDeps
485}
486
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800487func (a *androidModuleContext) AddMissingDependencies(deps []string) {
488 if deps != nil {
489 a.missingDeps = append(a.missingDeps, deps...)
490 }
491}
492
Colin Crossf6566ed2015-03-24 11:13:38 -0700493func (a *androidBaseContextImpl) Arch() Arch {
Colin Cross3f40fa42015-01-30 17:27:36 -0800494 return a.arch
495}
496
Colin Crossd3ba0392015-05-07 14:11:29 -0700497func (a *androidBaseContextImpl) HostOrDevice() HostOrDevice {
498 return a.hod
499}
500
Dan Willemsen490fd492015-11-24 17:53:15 -0800501func (a *androidBaseContextImpl) HostType() HostType {
502 return a.ht
503}
504
Colin Crossf6566ed2015-03-24 11:13:38 -0700505func (a *androidBaseContextImpl) Host() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700506 return a.hod.Host()
Colin Crossf6566ed2015-03-24 11:13:38 -0700507}
508
509func (a *androidBaseContextImpl) Device() bool {
Colin Crossd3ba0392015-05-07 14:11:29 -0700510 return a.hod.Device()
Colin Crossf6566ed2015-03-24 11:13:38 -0700511}
512
Colin Cross0af4b842015-04-30 16:36:18 -0700513func (a *androidBaseContextImpl) Darwin() bool {
Dan Willemsen490fd492015-11-24 17:53:15 -0800514 return a.hod.Host() && a.ht == Darwin
Colin Cross0af4b842015-04-30 16:36:18 -0700515}
516
Colin Crossf6566ed2015-03-24 11:13:38 -0700517func (a *androidBaseContextImpl) Debug() bool {
518 return a.debug
519}
520
Colin Cross1332b002015-04-07 17:11:30 -0700521func (a *androidBaseContextImpl) AConfig() Config {
522 return a.config
523}
524
Dan Willemsen782a2d12015-12-21 14:55:28 -0800525func (a *androidBaseContextImpl) Proprietary() bool {
526 return a.proprietary
527}
528
529func (a *androidBaseContextImpl) InstallInData() bool {
530 return a.installInData
531}
532
533func (a *androidModuleContext) InstallFileName(installPath OutputPath, name string, srcPath Path,
Colin Crossa2344662016-03-24 13:14:12 -0700534 deps ...Path) OutputPath {
Colin Cross35cec122015-04-02 14:37:16 -0700535
Dan Willemsen782a2d12015-12-21 14:55:28 -0800536 fullInstallPath := installPath.Join(a, name)
Colin Cross3f40fa42015-01-30 17:27:36 -0800537
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800538 if a.Host() || !a.AConfig().SkipDeviceInstall() {
Dan Willemsen322acaf2016-01-12 23:07:05 -0800539 deps = append(deps, a.installDeps...)
Colin Cross35cec122015-04-02 14:37:16 -0700540
Dan Willemsen322acaf2016-01-12 23:07:05 -0800541 a.ModuleBuild(pctx, ModuleBuildParams{
542 Rule: Cp,
543 Output: fullInstallPath,
544 Input: srcPath,
545 OrderOnly: Paths(deps),
Dan Willemsen7f730fd2016-01-14 11:22:23 -0800546 Default: !a.AConfig().EmbeddedInMake(),
Dan Willemsen322acaf2016-01-12 23:07:05 -0800547 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800548
Dan Willemsen322acaf2016-01-12 23:07:05 -0800549 a.installFiles = append(a.installFiles, fullInstallPath)
550 }
Colin Cross1f8c52b2015-06-16 16:38:17 -0700551 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
Colin Cross35cec122015-04-02 14:37:16 -0700552 return fullInstallPath
553}
554
Colin Crossa2344662016-03-24 13:14:12 -0700555func (a *androidModuleContext) InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700556 return a.InstallFileName(installPath, filepath.Base(srcPath.String()), srcPath, deps...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800557}
558
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700559func (a *androidModuleContext) CheckbuildFile(srcPath Path) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800560 a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
561}
562
Colin Cross3f40fa42015-01-30 17:27:36 -0800563type fileInstaller interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700564 filesToInstall() Paths
Colin Cross3f40fa42015-01-30 17:27:36 -0800565}
566
567func isFileInstaller(m blueprint.Module) bool {
568 _, ok := m.(fileInstaller)
569 return ok
570}
571
572func isAndroidModule(m blueprint.Module) bool {
Colin Cross635c3b02016-05-18 15:37:25 -0700573 _, ok := m.(Module)
Colin Cross3f40fa42015-01-30 17:27:36 -0800574 return ok
575}
Colin Crossfce53272015-04-08 11:21:40 -0700576
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700577func findStringInSlice(str string, slice []string) int {
578 for i, s := range slice {
579 if s == str {
580 return i
Colin Crossfce53272015-04-08 11:21:40 -0700581 }
582 }
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700583 return -1
584}
585
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700586func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
587 prefix := PathForModuleSrc(ctx).String()
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700588 for i, e := range excludes {
589 j := findStringInSlice(e, srcFiles)
590 if j != -1 {
591 srcFiles = append(srcFiles[:j], srcFiles[j+1:]...)
592 }
593
594 excludes[i] = filepath.Join(prefix, e)
595 }
596
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700597 globbedSrcFiles := make(Paths, 0, len(srcFiles))
Colin Cross8f101b42015-06-17 15:09:06 -0700598 for _, s := range srcFiles {
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700599 if glob.IsGlob(s) {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700600 globbedSrcFiles = append(globbedSrcFiles, ctx.Glob("src_glob", filepath.Join(prefix, s), excludes)...)
Colin Cross8f101b42015-06-17 15:09:06 -0700601 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700602 globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
Colin Cross8f101b42015-06-17 15:09:06 -0700603 }
604 }
605
606 return globbedSrcFiles
607}
608
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700609func (ctx *androidModuleContext) Glob(outDir, globPattern string, excludes []string) Paths {
610 ret, err := Glob(ctx, PathForModuleOut(ctx, outDir).String(), globPattern, excludes)
Colin Cross8f101b42015-06-17 15:09:06 -0700611 if err != nil {
612 ctx.ModuleErrorf("glob: %s", err.Error())
613 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700614 return pathsForModuleSrcFromFullPath(ctx, ret)
Colin Crossfce53272015-04-08 11:21:40 -0700615}
Colin Cross1f8c52b2015-06-16 16:38:17 -0700616
Colin Cross463a90e2015-06-17 14:20:06 -0700617func init() {
618 soong.RegisterSingletonType("buildtarget", BuildTargetSingleton)
619}
620
Colin Cross1f8c52b2015-06-16 16:38:17 -0700621func BuildTargetSingleton() blueprint.Singleton {
622 return &buildTargetSingleton{}
623}
624
625type buildTargetSingleton struct{}
626
627func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
628 checkbuildDeps := []string{}
629
630 dirModules := make(map[string][]string)
631
632 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -0700633 if a, ok := module.(Module); ok {
Colin Cross1f8c52b2015-06-16 16:38:17 -0700634 blueprintDir := a.base().blueprintDir
635 installTarget := a.base().installTarget
636 checkbuildTarget := a.base().checkbuildTarget
637
638 if checkbuildTarget != "" {
639 checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
640 dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
641 }
642
643 if installTarget != "" {
644 dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
645 }
646 }
647 })
648
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800649 suffix := ""
650 if ctx.Config().(Config).EmbeddedInMake() {
651 suffix = "-soong"
652 }
653
Colin Cross1f8c52b2015-06-16 16:38:17 -0700654 // Create a top-level checkbuild target that depends on all modules
655 ctx.Build(pctx, blueprint.BuildParams{
656 Rule: blueprint.Phony,
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800657 Outputs: []string{"checkbuild" + suffix},
Colin Cross1f8c52b2015-06-16 16:38:17 -0700658 Implicits: checkbuildDeps,
Dan Willemsen218f6562015-07-08 18:13:11 -0700659 Optional: true,
Colin Cross1f8c52b2015-06-16 16:38:17 -0700660 })
661
662 // Create a mm/<directory> target that depends on all modules in a directory
663 dirs := sortedKeys(dirModules)
664 for _, dir := range dirs {
665 ctx.Build(pctx, blueprint.BuildParams{
666 Rule: blueprint.Phony,
667 Outputs: []string{filepath.Join("mm", dir)},
668 Implicits: dirModules[dir],
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800669 // HACK: checkbuild should be an optional build, but force it
670 // enabled for now in standalone builds
Colin Cross1604ecf2015-12-17 16:33:43 -0800671 Optional: ctx.Config().(Config).EmbeddedInMake(),
Colin Cross1f8c52b2015-06-16 16:38:17 -0700672 })
673 }
674}
Colin Crossd779da42015-12-17 18:00:23 -0800675
676type AndroidModulesByName struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700677 slice []Module
Colin Crossd779da42015-12-17 18:00:23 -0800678 ctx interface {
679 ModuleName(blueprint.Module) string
680 ModuleSubDir(blueprint.Module) string
681 }
682}
683
684func (s AndroidModulesByName) Len() int { return len(s.slice) }
685func (s AndroidModulesByName) Less(i, j int) bool {
686 mi, mj := s.slice[i], s.slice[j]
687 ni, nj := s.ctx.ModuleName(mi), s.ctx.ModuleName(mj)
688
689 if ni != nj {
690 return ni < nj
691 } else {
692 return s.ctx.ModuleSubDir(mi) < s.ctx.ModuleSubDir(mj)
693 }
694}
695func (s AndroidModulesByName) Swap(i, j int) { s.slice[i], s.slice[j] = s.slice[j], s.slice[i] }