blob: 74d3d3da1904f2480e9877c05a92086dc8f8479c [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
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
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Dan Albert9e10cd42016-08-03 14:12:14 -070022 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "strings"
24
Colin Cross97ba0732015-03-23 17:50:24 -070025 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070026 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070029 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070030 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070034 android.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070035
Colin Cross1e676be2016-10-12 14:38:15 -070036 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
37 ctx.BottomUp("link", linkageMutator).Parallel()
38 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
39 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
40 ctx.BottomUp("begin", beginMutator).Parallel()
41 })
Colin Cross16b23492016-01-06 14:41:07 -080042
Colin Cross1e676be2016-10-12 14:38:15 -070043 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
45 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080046
Colin Cross1e676be2016-10-12 14:38:15 -070047 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
48 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
49 })
Colin Crossb98c8b02016-07-29 13:44:28 -070050
51 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070052}
53
Colin Crossca860ac2016-01-04 14:34:37 -080054type Deps struct {
55 SharedLibs, LateSharedLibs []string
56 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080057 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070058
Colin Cross5950f382016-12-13 12:50:57 -080059 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070060
Colin Cross81413472016-04-11 14:37:39 -070061 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070062
Dan Willemsenb40aab62016-04-20 14:21:14 -070063 GeneratedSources []string
64 GeneratedHeaders []string
65
Dan Willemsenb3454ab2016-09-28 17:34:58 -070066 ReexportGeneratedHeaders []string
67
Colin Cross97ba0732015-03-23 17:50:24 -070068 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070069}
70
Colin Crossca860ac2016-01-04 14:34:37 -080071type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070072 // Paths to .so files
73 SharedLibs, LateSharedLibs android.Paths
74 // Paths to the dependencies to use for .so files (.so.toc files)
75 SharedLibsDeps, LateSharedLibsDeps android.Paths
76 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070077 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078
Colin Cross26c34ed2016-09-30 17:10:16 -070079 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070080 Objs Objects
81 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082
Colin Cross26c34ed2016-09-30 17:10:16 -070083 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070084 GeneratedSources android.Paths
85 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070086
Dan Willemsen76f08272016-07-09 00:14:08 -070087 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070088 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089
Colin Cross26c34ed2016-09-30 17:10:16 -070090 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070091 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092}
93
Colin Crossca860ac2016-01-04 14:34:37 -080094type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070095 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
96 AsFlags []string // Flags that apply to assembly source files
97 CFlags []string // Flags that apply to C and C++ source files
98 ConlyFlags []string // Flags that apply to C source files
99 CppFlags []string // Flags that apply to C++ source files
100 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700101 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700102 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700103 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800104 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700105 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800106 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700107
Colin Crossb98c8b02016-07-29 13:44:28 -0700108 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700109 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700110 Tidy bool
Colin Crossca860ac2016-01-04 14:34:37 -0800111
112 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800113 DynamicLinker string
114
Colin Cross635c3b02016-05-18 15:37:25 -0700115 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800116
117 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700118}
119
Colin Cross81413472016-04-11 14:37:39 -0700120type ObjectLinkerProperties struct {
121 // names of other cc_object modules to link into this module using partial linking
122 Objs []string `android:"arch_variant"`
123}
124
Colin Crossca860ac2016-01-04 14:34:37 -0800125// Properties used to compile all C or C++ modules
126type BaseProperties struct {
127 // compile module with clang instead of gcc
128 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700129
130 // Minimum sdk version supported when compiling against the ndk
131 Sdk_version string
132
Dan Willemsend2ede872016-11-18 14:54:24 -0800133 // Whether to compile against the VNDK
134 Use_vndk bool
135
Colin Crossca860ac2016-01-04 14:34:37 -0800136 // don't insert default compiler flags into asflags, cflags,
137 // cppflags, conlyflags, ldflags, or include_dirs
138 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700139
140 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700141 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700142 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800143 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800144}
145
Colin Crossca860ac2016-01-04 14:34:37 -0800146type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700147 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700148 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800149}
150
Colin Crossca860ac2016-01-04 14:34:37 -0800151type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800152 static() bool
153 staticBinary() bool
154 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700155 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800156 noDefaultCompilerFlags() bool
157 sdk() bool
158 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800159 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700160 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700161 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800162}
163
164type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700165 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800166 ModuleContextIntf
167}
168
169type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700170 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800171 ModuleContextIntf
172}
173
Colin Cross37047f12016-12-13 17:06:13 -0800174type DepsContext interface {
175 android.BottomUpMutatorContext
176 ModuleContextIntf
177}
178
Colin Crossca860ac2016-01-04 14:34:37 -0800179type feature interface {
180 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800181 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800182 flags(ctx ModuleContext, flags Flags) Flags
183 props() []interface{}
184}
185
186type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700187 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800188 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700189 compilerFlags(ctx ModuleContext, flags Flags) Flags
190 compilerProps() []interface{}
191
Colin Cross76fada02016-07-27 10:31:13 -0700192 appendCflags([]string)
193 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700194 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800195}
196
197type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700198 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800199 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700200 linkerFlags(ctx ModuleContext, flags Flags) Flags
201 linkerProps() []interface{}
202
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700203 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700204 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800205}
206
207type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700208 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700209 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800210 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700211 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800212}
213
Colin Crossc99deeb2016-04-11 15:06:20 -0700214type dependencyTag struct {
215 blueprint.BaseDependencyTag
216 name string
217 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700218
219 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700220}
221
222var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700223 sharedDepTag = dependencyTag{name: "shared", library: true}
224 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
225 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
226 staticDepTag = dependencyTag{name: "static", library: true}
227 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
228 lateStaticDepTag = dependencyTag{name: "late static", library: true}
229 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross5950f382016-12-13 12:50:57 -0800230 headerDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700231 genSourceDepTag = dependencyTag{name: "gen source"}
232 genHeaderDepTag = dependencyTag{name: "gen header"}
233 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
234 objDepTag = dependencyTag{name: "obj"}
235 crtBeginDepTag = dependencyTag{name: "crtbegin"}
236 crtEndDepTag = dependencyTag{name: "crtend"}
237 reuseObjTag = dependencyTag{name: "reuse objects"}
238 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
239 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700240)
241
Colin Crossca860ac2016-01-04 14:34:37 -0800242// Module contains the properties and members used by all C/C++ module types, and implements
243// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
244// to construct the output file. Behavior can be customized with a Customizer interface
245type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700246 android.ModuleBase
247 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700248
Colin Crossca860ac2016-01-04 14:34:37 -0800249 Properties BaseProperties
250 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700251
Colin Crossca860ac2016-01-04 14:34:37 -0800252 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700253 hod android.HostOrDeviceSupported
254 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700255
Colin Crossca860ac2016-01-04 14:34:37 -0800256 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700257 features []feature
258 compiler compiler
259 linker linker
260 installer installer
261 stl *stl
262 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800263
264 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700265
Colin Cross635c3b02016-05-18 15:37:25 -0700266 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800267
Colin Crossb98c8b02016-07-29 13:44:28 -0700268 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700269
270 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700271}
272
Colin Crossca860ac2016-01-04 14:34:37 -0800273func (c *Module) Init() (blueprint.Module, []interface{}) {
274 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800275 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700276 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800277 }
278 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700279 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800280 }
281 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700282 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800283 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700284 if c.stl != nil {
285 props = append(props, c.stl.props()...)
286 }
Colin Cross16b23492016-01-06 14:41:07 -0800287 if c.sanitize != nil {
288 props = append(props, c.sanitize.props()...)
289 }
Colin Crossca860ac2016-01-04 14:34:37 -0800290 for _, feature := range c.features {
291 props = append(props, feature.props()...)
292 }
Colin Crossc472d572015-03-17 15:06:21 -0700293
Colin Cross635c3b02016-05-18 15:37:25 -0700294 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700295
Colin Cross635c3b02016-05-18 15:37:25 -0700296 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700297}
298
Colin Crossb916a382016-07-29 17:28:03 -0700299// Returns true for dependency roots (binaries)
300// TODO(ccross): also handle dlopenable libraries
301func (c *Module) isDependencyRoot() bool {
302 if root, ok := c.linker.(interface {
303 isDependencyRoot() bool
304 }); ok {
305 return root.isDependencyRoot()
306 }
307 return false
308}
309
Colin Crossca860ac2016-01-04 14:34:37 -0800310type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700311 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800312 moduleContextImpl
313}
314
Colin Cross37047f12016-12-13 17:06:13 -0800315type depsContext struct {
316 android.BottomUpMutatorContext
317 moduleContextImpl
318}
319
Colin Crossca860ac2016-01-04 14:34:37 -0800320type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700321 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800322 moduleContextImpl
323}
324
325type moduleContextImpl struct {
326 mod *Module
327 ctx BaseModuleContext
328}
329
Colin Crossca860ac2016-01-04 14:34:37 -0800330func (ctx *moduleContextImpl) clang() bool {
331 return ctx.mod.clang(ctx.ctx)
332}
333
Colin Crossb98c8b02016-07-29 13:44:28 -0700334func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800335 return ctx.mod.toolchain(ctx.ctx)
336}
337
338func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700339 if static, ok := ctx.mod.linker.(interface {
340 static() bool
341 }); ok {
342 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800343 }
Colin Crossb916a382016-07-29 17:28:03 -0700344 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800345}
346
347func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700348 if static, ok := ctx.mod.linker.(interface {
349 staticBinary() bool
350 }); ok {
351 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800352 }
Colin Crossb916a382016-07-29 17:28:03 -0700353 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800354}
355
356func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
357 return Bool(ctx.mod.Properties.No_default_compiler_flags)
358}
359
360func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700361 if ctx.ctx.Device() {
362 return ctx.mod.Properties.Sdk_version != ""
363 }
364 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800365}
366
367func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700368 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800369 if ctx.mod.Properties.Use_vndk {
370 return ctx.mod.Properties.Vndk_version
371 } else {
372 return ctx.mod.Properties.Sdk_version
373 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700374 }
375 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800376}
377
Dan Willemsend2ede872016-11-18 14:54:24 -0800378func (ctx *moduleContextImpl) vndk() bool {
379 if ctx.ctx.Device() {
380 return ctx.mod.Properties.Use_vndk
381 }
382 return false
383}
384
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700385func (ctx *moduleContextImpl) selectedStl() string {
386 if stl := ctx.mod.stl; stl != nil {
387 return stl.Properties.SelectedStl
388 }
389 return ""
390}
391
Colin Crossce75d2c2016-10-06 16:12:58 -0700392func (ctx *moduleContextImpl) baseModuleName() string {
393 return ctx.mod.ModuleBase.BaseModuleName()
394}
395
Colin Cross635c3b02016-05-18 15:37:25 -0700396func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800397 return &Module{
398 hod: hod,
399 multilib: multilib,
400 }
401}
402
Colin Cross635c3b02016-05-18 15:37:25 -0700403func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800404 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700405 module.features = []feature{
406 &tidyFeature{},
407 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700408 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800409 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800410 return module
411}
412
Colin Crossce75d2c2016-10-06 16:12:58 -0700413func (c *Module) Prebuilt() *android.Prebuilt {
414 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
415 return p.prebuilt()
416 }
417 return nil
418}
419
420func (c *Module) Name() string {
421 name := c.ModuleBase.Name()
422 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
423 name = p.Name(name)
424 }
425 return name
426}
427
Colin Cross635c3b02016-05-18 15:37:25 -0700428func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800429 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700430 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800431 moduleContextImpl: moduleContextImpl{
432 mod: c,
433 },
434 }
435 ctx.ctx = ctx
436
437 flags := Flags{
438 Toolchain: c.toolchain(ctx),
439 Clang: c.clang(ctx),
440 }
Colin Crossca860ac2016-01-04 14:34:37 -0800441 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700442 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800443 }
444 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700445 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800446 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700447 if c.stl != nil {
448 flags = c.stl.flags(ctx, flags)
449 }
Colin Cross16b23492016-01-06 14:41:07 -0800450 if c.sanitize != nil {
451 flags = c.sanitize.flags(ctx, flags)
452 }
Colin Crossca860ac2016-01-04 14:34:37 -0800453 for _, feature := range c.features {
454 flags = feature.flags(ctx, flags)
455 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800456 if ctx.Failed() {
457 return
458 }
459
Colin Crossb98c8b02016-07-29 13:44:28 -0700460 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
461 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
462 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800463
Colin Crossca860ac2016-01-04 14:34:37 -0800464 // Optimization to reduce size of build.ninja
465 // Replace the long list of flags for each file with a module-local variable
466 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
467 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
468 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
469 flags.CFlags = []string{"$cflags"}
470 flags.CppFlags = []string{"$cppflags"}
471 flags.AsFlags = []string{"$asflags"}
472
Colin Crossc99deeb2016-04-11 15:06:20 -0700473 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800474 if ctx.Failed() {
475 return
476 }
477
Dan Willemsen76f08272016-07-09 00:14:08 -0700478 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700479
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700480 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800481 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700482 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800483 if ctx.Failed() {
484 return
485 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800486 }
487
Colin Crossca860ac2016-01-04 14:34:37 -0800488 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700489 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800490 if ctx.Failed() {
491 return
492 }
Colin Cross635c3b02016-05-18 15:37:25 -0700493 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700494 }
Colin Cross5049f022015-03-18 13:28:46 -0700495
Colin Crossce75d2c2016-10-06 16:12:58 -0700496 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
497 c.installer.install(ctx, c.outputFile.Path())
498 if ctx.Failed() {
499 return
Colin Crossca860ac2016-01-04 14:34:37 -0800500 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700501 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800502}
503
Colin Crossb98c8b02016-07-29 13:44:28 -0700504func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800505 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700506 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800507 }
Colin Crossca860ac2016-01-04 14:34:37 -0800508 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800509}
510
Colin Crossca860ac2016-01-04 14:34:37 -0800511func (c *Module) begin(ctx BaseModuleContext) {
512 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700513 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700514 }
Colin Crossca860ac2016-01-04 14:34:37 -0800515 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700516 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800517 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700518 if c.stl != nil {
519 c.stl.begin(ctx)
520 }
Colin Cross16b23492016-01-06 14:41:07 -0800521 if c.sanitize != nil {
522 c.sanitize.begin(ctx)
523 }
Colin Crossca860ac2016-01-04 14:34:37 -0800524 for _, feature := range c.features {
525 feature.begin(ctx)
526 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700527 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800528 if ctx.vndk() {
529 ctx.PropertyErrorf("use_vndk",
530 "sdk_version and use_vndk cannot be used at the same time")
531 }
532
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700533 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
534 if err != nil {
535 ctx.PropertyErrorf("sdk_version", err.Error())
536 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800537 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800538 } else if ctx.vndk() {
539 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
540 if err != nil {
541 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
542 }
543 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700544 }
Colin Crossca860ac2016-01-04 14:34:37 -0800545}
546
Colin Cross37047f12016-12-13 17:06:13 -0800547func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700548 deps := Deps{}
549
550 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700551 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700552 }
553 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700554 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700555 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700556 if c.stl != nil {
557 deps = c.stl.deps(ctx, deps)
558 }
Colin Cross16b23492016-01-06 14:41:07 -0800559 if c.sanitize != nil {
560 deps = c.sanitize.deps(ctx, deps)
561 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700562 for _, feature := range c.features {
563 deps = feature.deps(ctx, deps)
564 }
565
566 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
567 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
568 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
569 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
570 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800571 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700572
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700573 for _, lib := range deps.ReexportSharedLibHeaders {
574 if !inList(lib, deps.SharedLibs) {
575 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
576 }
577 }
578
579 for _, lib := range deps.ReexportStaticLibHeaders {
580 if !inList(lib, deps.StaticLibs) {
581 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
582 }
583 }
584
Colin Cross5950f382016-12-13 12:50:57 -0800585 for _, lib := range deps.ReexportHeaderLibHeaders {
586 if !inList(lib, deps.HeaderLibs) {
587 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
588 }
589 }
590
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700591 for _, gen := range deps.ReexportGeneratedHeaders {
592 if !inList(gen, deps.GeneratedHeaders) {
593 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
594 }
595 }
596
Colin Crossc99deeb2016-04-11 15:06:20 -0700597 return deps
598}
599
Dan Albert7e9d2952016-08-04 13:02:36 -0700600func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800601 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700602 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800603 moduleContextImpl: moduleContextImpl{
604 mod: c,
605 },
606 }
607 ctx.ctx = ctx
608
Colin Crossca860ac2016-01-04 14:34:37 -0800609 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700610}
611
Colin Cross1e676be2016-10-12 14:38:15 -0700612func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
613 if !c.Enabled() {
614 return
615 }
616
Colin Cross37047f12016-12-13 17:06:13 -0800617 ctx := &depsContext{
618 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700619 moduleContextImpl: moduleContextImpl{
620 mod: c,
621 },
622 }
623 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800624
Colin Crossc99deeb2016-04-11 15:06:20 -0700625 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800626
Colin Crossb5bc4b42016-07-11 16:11:59 -0700627 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
628 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700629
Dan Albert914449f2016-06-17 16:45:24 -0700630 variantNdkLibs := []string{}
631 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800632 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700633 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700634
Dan Albert914449f2016-06-17 16:45:24 -0700635 // Rewrites the names of shared libraries into the names of the NDK
636 // libraries where appropriate. This returns two slices.
637 //
638 // The first is a list of non-variant shared libraries (either rewritten
639 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
640 // because they are not NDK libraries).
641 //
642 // The second is a list of ndk_library modules. These need to be
643 // separated because they are a variation dependency and must be added
644 // in a different manner.
645 rewriteNdkLibs := func(list []string) ([]string, []string) {
646 variantLibs := []string{}
647 nonvariantLibs := []string{}
648 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700649 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700650 if !inList(entry, ndkMigratedLibs) {
651 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
652 } else {
653 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
654 }
655 } else {
656 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700657 }
658 }
Dan Albert914449f2016-06-17 16:45:24 -0700659 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700660 }
661
Dan Albert914449f2016-06-17 16:45:24 -0700662 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
663 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700664 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700665
Colin Cross5950f382016-12-13 12:50:57 -0800666 actx.AddVariationDependencies(nil, headerDepTag, deps.HeaderLibs...)
667
Colin Crossc99deeb2016-04-11 15:06:20 -0700668 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
669 deps.WholeStaticLibs...)
670
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700671 for _, lib := range deps.StaticLibs {
672 depTag := staticDepTag
673 if inList(lib, deps.ReexportStaticLibHeaders) {
674 depTag = staticExportDepTag
675 }
Colin Cross15a0d462016-07-14 14:49:58 -0700676 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700677 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700678
679 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
680 deps.LateStaticLibs...)
681
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700682 for _, lib := range deps.SharedLibs {
683 depTag := sharedDepTag
684 if inList(lib, deps.ReexportSharedLibHeaders) {
685 depTag = sharedExportDepTag
686 }
Colin Cross15a0d462016-07-14 14:49:58 -0700687 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700688 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700689
690 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
691 deps.LateSharedLibs...)
692
Colin Cross68861832016-07-08 10:41:41 -0700693 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700694
695 for _, gen := range deps.GeneratedHeaders {
696 depTag := genHeaderDepTag
697 if inList(gen, deps.ReexportGeneratedHeaders) {
698 depTag = genHeaderExportDepTag
699 }
700 actx.AddDependency(c, depTag, gen)
701 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700702
Colin Cross68861832016-07-08 10:41:41 -0700703 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700704
705 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700706 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800707 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700708 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700709 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700710 }
Dan Albert914449f2016-06-17 16:45:24 -0700711
712 version := ctx.sdkVersion()
713 actx.AddVariationDependencies([]blueprint.Variation{
714 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
715 actx.AddVariationDependencies([]blueprint.Variation{
716 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700717}
Colin Cross21b9a242015-03-24 14:15:58 -0700718
Dan Albert7e9d2952016-08-04 13:02:36 -0700719func beginMutator(ctx android.BottomUpMutatorContext) {
720 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
721 c.beginMutator(ctx)
722 }
723}
724
Colin Crossca860ac2016-01-04 14:34:37 -0800725func (c *Module) clang(ctx BaseModuleContext) bool {
726 clang := Bool(c.Properties.Clang)
727
728 if c.Properties.Clang == nil {
729 if ctx.Host() {
730 clang = true
731 }
732
733 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
734 clang = true
735 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800736 }
Colin Cross28344522015-04-22 13:07:53 -0700737
Colin Crossca860ac2016-01-04 14:34:37 -0800738 if !c.toolchain(ctx).ClangSupported() {
739 clang = false
740 }
741
742 return clang
743}
744
Colin Crossc99deeb2016-04-11 15:06:20 -0700745// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700746func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800747 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800748
Dan Willemsena96ff642016-06-07 12:34:45 -0700749 // Whether a module can link to another module, taking into
750 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700751 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700752 if from.Target().Os != android.Android {
753 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700754 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700755 }
756 if from.Properties.Sdk_version == "" {
757 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700758 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700759 }
Colin Crossb916a382016-07-29 17:28:03 -0700760 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700761 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700762 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700763 }
764 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
765 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700766 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700767 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700768 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
769 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700770 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700771 }
Colin Crossb916a382016-07-29 17:28:03 -0700772 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700773 // These aren't real libraries, but are the stub shared libraries that are included in
774 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700775 return
Dan Albert914449f2016-06-17 16:45:24 -0700776 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700777 if to.Properties.Sdk_version == "" {
778 // NDK code linking to platform code is never okay.
779 ctx.ModuleErrorf("depends on non-NDK-built library %q",
780 ctx.OtherModuleName(to))
781 }
782
783 // All this point we know we have two NDK libraries, but we need to
784 // check that we're not linking against anything built against a higher
785 // API level, as it is only valid to link against older or equivalent
786 // APIs.
787
788 if from.Properties.Sdk_version == "current" {
789 // Current can link against anything.
790 return
791 } else if to.Properties.Sdk_version == "current" {
792 // Current can't be linked against by anything else.
793 ctx.ModuleErrorf("links %q built against newer API version %q",
794 ctx.OtherModuleName(to), "current")
795 }
796
797 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
798 if err != nil {
799 ctx.PropertyErrorf("sdk_version",
800 "Invalid sdk_version value (must be int): %q",
801 from.Properties.Sdk_version)
802 }
803 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
804 if err != nil {
805 ctx.PropertyErrorf("sdk_version",
806 "Invalid sdk_version value (must be int): %q",
807 to.Properties.Sdk_version)
808 }
809
810 if toApi > fromApi {
811 ctx.ModuleErrorf("links %q built against newer API version %q",
812 ctx.OtherModuleName(to), to.Properties.Sdk_version)
813 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700814 }
815
Colin Crossc99deeb2016-04-11 15:06:20 -0700816 ctx.VisitDirectDeps(func(m blueprint.Module) {
817 name := ctx.OtherModuleName(m)
818 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800819
Colin Cross635c3b02016-05-18 15:37:25 -0700820 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700821 if a == nil {
822 ctx.ModuleErrorf("module %q not an android module", name)
823 return
Colin Crossca860ac2016-01-04 14:34:37 -0800824 }
Colin Crossca860ac2016-01-04 14:34:37 -0800825
Dan Willemsena96ff642016-06-07 12:34:45 -0700826 cc, _ := m.(*Module)
827 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700828 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700829 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700830 case genSourceDepTag:
831 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
832 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
833 genRule.GeneratedSourceFiles()...)
834 } else {
835 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
836 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700837 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700838 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
839 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
840 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800841 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700842 depPaths.Flags = append(depPaths.Flags, flags)
843 if tag == genHeaderExportDepTag {
844 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700845 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
846 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700847 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700848 } else {
849 ctx.ModuleErrorf("module %q is not a genrule", name)
850 }
851 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700852 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800853 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700854 return
855 }
856
857 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800858 if ctx.AConfig().AllowMissingDependencies() {
859 ctx.AddMissingDependencies([]string{name})
860 } else {
861 ctx.ModuleErrorf("depends on disabled module %q", name)
862 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700863 return
864 }
865
Colin Crossa1ad8d12016-06-01 17:09:44 -0700866 if a.Target().Os != ctx.Os() {
867 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
868 return
869 }
870
871 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
872 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700873 return
874 }
875
Colin Crossc99deeb2016-04-11 15:06:20 -0700876 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800877 if l, ok := cc.compiler.(libraryInterface); ok {
878 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
879 return
880 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700881 }
882
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700883 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700884 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700885 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700886 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700887 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700888 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700889
890 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700891 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700892 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700893 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700894 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700895
Dan Albert9e10cd42016-08-03 14:12:14 -0700896 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700897 }
898
Colin Cross26c34ed2016-09-30 17:10:16 -0700899 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700900 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700901
Colin Cross26c34ed2016-09-30 17:10:16 -0700902 linkFile := cc.outputFile
903 depFile := android.OptionalPath{}
904
Colin Crossc99deeb2016-04-11 15:06:20 -0700905 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700906 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700907 ptr = &depPaths.SharedLibs
908 depPtr = &depPaths.SharedLibsDeps
909 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700910 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700911 ptr = &depPaths.LateSharedLibs
912 depPtr = &depPaths.LateSharedLibsDeps
913 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700914 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700915 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700916 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700917 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700918 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700919 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700920 staticLib, ok := cc.linker.(libraryInterface)
921 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700922 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700923 return
924 }
925
926 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
927 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
928 for i := range missingDeps {
929 missingDeps[i] += postfix
930 }
931 ctx.AddMissingDependencies(missingDeps)
932 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700933 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800934 case headerDepTag:
935 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700936 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700937 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700938 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700939 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700940 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700941 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700942 }
943
Colin Cross26c34ed2016-09-30 17:10:16 -0700944 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700945 if !linkFile.Valid() {
946 ctx.ModuleErrorf("module %q missing output file", name)
947 return
948 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700949 *ptr = append(*ptr, linkFile.Path())
950 }
951
Colin Crossc99deeb2016-04-11 15:06:20 -0700952 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700953 dep := depFile
954 if !dep.Valid() {
955 dep = linkFile
956 }
957 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800958 }
959 })
960
961 return depPaths
962}
963
964func (c *Module) InstallInData() bool {
965 if c.installer == nil {
966 return false
967 }
Colin Cross94610402016-08-29 13:41:32 -0700968 if c.sanitize != nil && c.sanitize.inData() {
969 return true
970 }
Colin Crossca860ac2016-01-04 14:34:37 -0800971 return c.installer.inData()
972}
973
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700974func (c *Module) HostToolPath() android.OptionalPath {
975 if c.installer == nil {
976 return android.OptionalPath{}
977 }
978 return c.installer.hostToolPath()
979}
980
Colin Cross2ba19d92015-05-07 15:44:20 -0700981//
Colin Crosscfad1192015-11-02 16:43:11 -0800982// Defaults
983//
Colin Crossca860ac2016-01-04 14:34:37 -0800984type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700985 android.ModuleBase
986 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800987}
988
Colin Cross635c3b02016-05-18 15:37:25 -0700989func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800990}
991
Colin Cross1e676be2016-10-12 14:38:15 -0700992func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
993}
994
Colin Crossca860ac2016-01-04 14:34:37 -0800995func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700996 return DefaultsFactory()
997}
998
999func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001000 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001001
Colin Crosse1d764e2016-08-18 14:18:32 -07001002 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001003 &BaseProperties{},
1004 &BaseCompilerProperties{},
1005 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001006 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001007 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001008 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001009 &TestProperties{},
1010 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001011 &UnusedProperties{},
1012 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001013 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001014 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001015 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001016 &TidyProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001017 )
Colin Crosscfad1192015-11-02 16:43:11 -08001018
Colin Crosse1d764e2016-08-18 14:18:32 -07001019 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001020}
1021
Colin Cross74d1ec02015-04-28 13:30:13 -07001022// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1023// modifies the slice contents in place, and returns a subslice of the original slice
1024func lastUniqueElements(list []string) []string {
1025 totalSkip := 0
1026 for i := len(list) - 1; i >= totalSkip; i-- {
1027 skip := 0
1028 for j := i - 1; j >= totalSkip; j-- {
1029 if list[i] == list[j] {
1030 skip++
1031 } else {
1032 list[j+skip] = list[j]
1033 }
1034 }
1035 totalSkip += skip
1036 }
1037 return list[totalSkip:]
1038}
Colin Cross06a931b2015-10-28 17:23:31 -07001039
1040var Bool = proptools.Bool