blob: b6e98b1ccc443e2ff8fa4d5a8e82bde347179faf [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 Crossc472d572015-03-17 15:06:21 -070057
Dan Willemsen490a8dc2016-06-06 18:22:19 -070058 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
59
Colin Cross81413472016-04-11 14:37:39 -070060 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070061
Dan Willemsenb40aab62016-04-20 14:21:14 -070062 GeneratedSources []string
63 GeneratedHeaders []string
64
Dan Willemsenb3454ab2016-09-28 17:34:58 -070065 ReexportGeneratedHeaders []string
66
Colin Cross97ba0732015-03-23 17:50:24 -070067 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070068}
69
Colin Crossca860ac2016-01-04 14:34:37 -080070type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070071 // Paths to .so files
72 SharedLibs, LateSharedLibs android.Paths
73 // Paths to the dependencies to use for .so files (.so.toc files)
74 SharedLibsDeps, LateSharedLibsDeps android.Paths
75 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070076 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077
Colin Cross26c34ed2016-09-30 17:10:16 -070078 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070079 Objs Objects
80 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081
Colin Cross26c34ed2016-09-30 17:10:16 -070082 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070083 GeneratedSources android.Paths
84 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070085
Dan Willemsen76f08272016-07-09 00:14:08 -070086 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070087 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070088
Colin Cross26c34ed2016-09-30 17:10:16 -070089 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070090 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070091}
92
Colin Crossca860ac2016-01-04 14:34:37 -080093type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070094 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
95 AsFlags []string // Flags that apply to assembly source files
96 CFlags []string // Flags that apply to C and C++ source files
97 ConlyFlags []string // Flags that apply to C source files
98 CppFlags []string // Flags that apply to C++ source files
99 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700100 protoFlags []string // Flags that apply to proto source files
Colin Cross28344522015-04-22 13:07:53 -0700101 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800102 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700103 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross28344522015-04-22 13:07:53 -0700104
Colin Crossb98c8b02016-07-29 13:44:28 -0700105 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700106 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700107 Tidy bool
Colin Crossca860ac2016-01-04 14:34:37 -0800108
109 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800110 DynamicLinker string
111
Colin Cross635c3b02016-05-18 15:37:25 -0700112 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700113}
114
Colin Cross81413472016-04-11 14:37:39 -0700115type ObjectLinkerProperties struct {
116 // names of other cc_object modules to link into this module using partial linking
117 Objs []string `android:"arch_variant"`
118}
119
Colin Crossca860ac2016-01-04 14:34:37 -0800120// Properties used to compile all C or C++ modules
121type BaseProperties struct {
122 // compile module with clang instead of gcc
123 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700124
125 // Minimum sdk version supported when compiling against the ndk
126 Sdk_version string
127
Colin Crossca860ac2016-01-04 14:34:37 -0800128 // don't insert default compiler flags into asflags, cflags,
129 // cppflags, conlyflags, ldflags, or include_dirs
130 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700131
132 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700133 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700134 PreventInstall bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800135}
136
Colin Crossca860ac2016-01-04 14:34:37 -0800137type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700138 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700139 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800140}
141
Colin Crossca860ac2016-01-04 14:34:37 -0800142type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800143 static() bool
144 staticBinary() bool
145 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700146 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800147 noDefaultCompilerFlags() bool
148 sdk() bool
149 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700150 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700151 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800152}
153
154type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700155 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800156 ModuleContextIntf
157}
158
159type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700160 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800161 ModuleContextIntf
162}
163
Colin Crossca860ac2016-01-04 14:34:37 -0800164type feature interface {
165 begin(ctx BaseModuleContext)
166 deps(ctx BaseModuleContext, deps Deps) Deps
167 flags(ctx ModuleContext, flags Flags) Flags
168 props() []interface{}
169}
170
171type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700172 compilerInit(ctx BaseModuleContext)
173 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
174 compilerFlags(ctx ModuleContext, flags Flags) Flags
175 compilerProps() []interface{}
176
Colin Cross76fada02016-07-27 10:31:13 -0700177 appendCflags([]string)
178 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700179 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800180}
181
182type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700183 linkerInit(ctx BaseModuleContext)
184 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
185 linkerFlags(ctx ModuleContext, flags Flags) Flags
186 linkerProps() []interface{}
187
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700188 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700189 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800190}
191
192type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700193 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700194 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800195 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700196 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800197}
198
Colin Crossc99deeb2016-04-11 15:06:20 -0700199type dependencyTag struct {
200 blueprint.BaseDependencyTag
201 name string
202 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700203
204 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700205}
206
207var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700208 sharedDepTag = dependencyTag{name: "shared", library: true}
209 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
210 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
211 staticDepTag = dependencyTag{name: "static", library: true}
212 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
213 lateStaticDepTag = dependencyTag{name: "late static", library: true}
214 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
215 genSourceDepTag = dependencyTag{name: "gen source"}
216 genHeaderDepTag = dependencyTag{name: "gen header"}
217 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
218 objDepTag = dependencyTag{name: "obj"}
219 crtBeginDepTag = dependencyTag{name: "crtbegin"}
220 crtEndDepTag = dependencyTag{name: "crtend"}
221 reuseObjTag = dependencyTag{name: "reuse objects"}
222 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
223 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700224)
225
Colin Crossca860ac2016-01-04 14:34:37 -0800226// Module contains the properties and members used by all C/C++ module types, and implements
227// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
228// to construct the output file. Behavior can be customized with a Customizer interface
229type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700230 android.ModuleBase
231 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700232
Colin Crossca860ac2016-01-04 14:34:37 -0800233 Properties BaseProperties
234 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700235
Colin Crossca860ac2016-01-04 14:34:37 -0800236 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700237 hod android.HostOrDeviceSupported
238 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700239
Colin Crossca860ac2016-01-04 14:34:37 -0800240 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700241 features []feature
242 compiler compiler
243 linker linker
244 installer installer
245 stl *stl
246 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800247
248 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700249
Colin Cross635c3b02016-05-18 15:37:25 -0700250 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800251
Colin Crossb98c8b02016-07-29 13:44:28 -0700252 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700253
254 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700255}
256
Colin Crossca860ac2016-01-04 14:34:37 -0800257func (c *Module) Init() (blueprint.Module, []interface{}) {
258 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800259 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700260 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800261 }
262 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700263 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800264 }
265 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700266 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800267 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700268 if c.stl != nil {
269 props = append(props, c.stl.props()...)
270 }
Colin Cross16b23492016-01-06 14:41:07 -0800271 if c.sanitize != nil {
272 props = append(props, c.sanitize.props()...)
273 }
Colin Crossca860ac2016-01-04 14:34:37 -0800274 for _, feature := range c.features {
275 props = append(props, feature.props()...)
276 }
Colin Crossc472d572015-03-17 15:06:21 -0700277
Colin Cross635c3b02016-05-18 15:37:25 -0700278 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700279
Colin Cross635c3b02016-05-18 15:37:25 -0700280 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700281}
282
Colin Crossb916a382016-07-29 17:28:03 -0700283// Returns true for dependency roots (binaries)
284// TODO(ccross): also handle dlopenable libraries
285func (c *Module) isDependencyRoot() bool {
286 if root, ok := c.linker.(interface {
287 isDependencyRoot() bool
288 }); ok {
289 return root.isDependencyRoot()
290 }
291 return false
292}
293
Colin Crossca860ac2016-01-04 14:34:37 -0800294type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700295 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800296 moduleContextImpl
297}
298
299type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700300 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800301 moduleContextImpl
302}
303
304type moduleContextImpl struct {
305 mod *Module
306 ctx BaseModuleContext
307}
308
Colin Crossca860ac2016-01-04 14:34:37 -0800309func (ctx *moduleContextImpl) clang() bool {
310 return ctx.mod.clang(ctx.ctx)
311}
312
Colin Crossb98c8b02016-07-29 13:44:28 -0700313func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800314 return ctx.mod.toolchain(ctx.ctx)
315}
316
317func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700318 if static, ok := ctx.mod.linker.(interface {
319 static() bool
320 }); ok {
321 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800322 }
Colin Crossb916a382016-07-29 17:28:03 -0700323 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800324}
325
326func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700327 if static, ok := ctx.mod.linker.(interface {
328 staticBinary() bool
329 }); ok {
330 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800331 }
Colin Crossb916a382016-07-29 17:28:03 -0700332 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800333}
334
335func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
336 return Bool(ctx.mod.Properties.No_default_compiler_flags)
337}
338
339func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700340 if ctx.ctx.Device() {
341 return ctx.mod.Properties.Sdk_version != ""
342 }
343 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800344}
345
346func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700347 if ctx.ctx.Device() {
348 return ctx.mod.Properties.Sdk_version
349 }
350 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800351}
352
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700353func (ctx *moduleContextImpl) selectedStl() string {
354 if stl := ctx.mod.stl; stl != nil {
355 return stl.Properties.SelectedStl
356 }
357 return ""
358}
359
Colin Crossce75d2c2016-10-06 16:12:58 -0700360func (ctx *moduleContextImpl) baseModuleName() string {
361 return ctx.mod.ModuleBase.BaseModuleName()
362}
363
Colin Cross635c3b02016-05-18 15:37:25 -0700364func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800365 return &Module{
366 hod: hod,
367 multilib: multilib,
368 }
369}
370
Colin Cross635c3b02016-05-18 15:37:25 -0700371func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800372 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700373 module.features = []feature{
374 &tidyFeature{},
375 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700376 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800377 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800378 return module
379}
380
Colin Crossce75d2c2016-10-06 16:12:58 -0700381func (c *Module) Prebuilt() *android.Prebuilt {
382 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
383 return p.prebuilt()
384 }
385 return nil
386}
387
388func (c *Module) Name() string {
389 name := c.ModuleBase.Name()
390 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
391 name = p.Name(name)
392 }
393 return name
394}
395
Colin Cross635c3b02016-05-18 15:37:25 -0700396func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800397 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700398 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800399 moduleContextImpl: moduleContextImpl{
400 mod: c,
401 },
402 }
403 ctx.ctx = ctx
404
405 flags := Flags{
406 Toolchain: c.toolchain(ctx),
407 Clang: c.clang(ctx),
408 }
Colin Crossca860ac2016-01-04 14:34:37 -0800409 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700410 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800411 }
412 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700413 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800414 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700415 if c.stl != nil {
416 flags = c.stl.flags(ctx, flags)
417 }
Colin Cross16b23492016-01-06 14:41:07 -0800418 if c.sanitize != nil {
419 flags = c.sanitize.flags(ctx, flags)
420 }
Colin Crossca860ac2016-01-04 14:34:37 -0800421 for _, feature := range c.features {
422 flags = feature.flags(ctx, flags)
423 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800424 if ctx.Failed() {
425 return
426 }
427
Colin Crossb98c8b02016-07-29 13:44:28 -0700428 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
429 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
430 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800431
Colin Crossca860ac2016-01-04 14:34:37 -0800432 // Optimization to reduce size of build.ninja
433 // Replace the long list of flags for each file with a module-local variable
434 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
435 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
436 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
437 flags.CFlags = []string{"$cflags"}
438 flags.CppFlags = []string{"$cppflags"}
439 flags.AsFlags = []string{"$asflags"}
440
Colin Crossc99deeb2016-04-11 15:06:20 -0700441 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800442 if ctx.Failed() {
443 return
444 }
445
Dan Willemsen76f08272016-07-09 00:14:08 -0700446 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700447
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700448 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800449 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700450 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800451 if ctx.Failed() {
452 return
453 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800454 }
455
Colin Crossca860ac2016-01-04 14:34:37 -0800456 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700457 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800458 if ctx.Failed() {
459 return
460 }
Colin Cross635c3b02016-05-18 15:37:25 -0700461 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700462 }
Colin Cross5049f022015-03-18 13:28:46 -0700463
Colin Crossce75d2c2016-10-06 16:12:58 -0700464 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
465 c.installer.install(ctx, c.outputFile.Path())
466 if ctx.Failed() {
467 return
Colin Crossca860ac2016-01-04 14:34:37 -0800468 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700469 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800470}
471
Colin Crossb98c8b02016-07-29 13:44:28 -0700472func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800473 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700474 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800475 }
Colin Crossca860ac2016-01-04 14:34:37 -0800476 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800477}
478
Colin Crossca860ac2016-01-04 14:34:37 -0800479func (c *Module) begin(ctx BaseModuleContext) {
480 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700481 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700482 }
Colin Crossca860ac2016-01-04 14:34:37 -0800483 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700484 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800485 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700486 if c.stl != nil {
487 c.stl.begin(ctx)
488 }
Colin Cross16b23492016-01-06 14:41:07 -0800489 if c.sanitize != nil {
490 c.sanitize.begin(ctx)
491 }
Colin Crossca860ac2016-01-04 14:34:37 -0800492 for _, feature := range c.features {
493 feature.begin(ctx)
494 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700495 if ctx.sdk() {
496 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
497 if err != nil {
498 ctx.PropertyErrorf("sdk_version", err.Error())
499 }
500 c.Properties.Sdk_version = strconv.Itoa(version)
501 }
Colin Crossca860ac2016-01-04 14:34:37 -0800502}
503
Colin Crossc99deeb2016-04-11 15:06:20 -0700504func (c *Module) deps(ctx BaseModuleContext) Deps {
505 deps := Deps{}
506
507 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700508 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700509 }
510 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700511 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700512 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700513 if c.stl != nil {
514 deps = c.stl.deps(ctx, deps)
515 }
Colin Cross16b23492016-01-06 14:41:07 -0800516 if c.sanitize != nil {
517 deps = c.sanitize.deps(ctx, deps)
518 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700519 for _, feature := range c.features {
520 deps = feature.deps(ctx, deps)
521 }
522
523 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
524 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
525 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
526 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
527 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
528
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700529 for _, lib := range deps.ReexportSharedLibHeaders {
530 if !inList(lib, deps.SharedLibs) {
531 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
532 }
533 }
534
535 for _, lib := range deps.ReexportStaticLibHeaders {
536 if !inList(lib, deps.StaticLibs) {
537 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
538 }
539 }
540
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700541 for _, gen := range deps.ReexportGeneratedHeaders {
542 if !inList(gen, deps.GeneratedHeaders) {
543 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
544 }
545 }
546
Colin Crossc99deeb2016-04-11 15:06:20 -0700547 return deps
548}
549
Dan Albert7e9d2952016-08-04 13:02:36 -0700550func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800551 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700552 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800553 moduleContextImpl: moduleContextImpl{
554 mod: c,
555 },
556 }
557 ctx.ctx = ctx
558
Colin Crossca860ac2016-01-04 14:34:37 -0800559 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700560}
561
Colin Cross1e676be2016-10-12 14:38:15 -0700562func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
563 if !c.Enabled() {
564 return
565 }
566
Dan Albert7e9d2952016-08-04 13:02:36 -0700567 ctx := &baseModuleContext{
568 BaseContext: actx,
569 moduleContextImpl: moduleContextImpl{
570 mod: c,
571 },
572 }
573 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800574
Colin Crossc99deeb2016-04-11 15:06:20 -0700575 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800576
Colin Crossb5bc4b42016-07-11 16:11:59 -0700577 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
578 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700579
Dan Albert914449f2016-06-17 16:45:24 -0700580 variantNdkLibs := []string{}
581 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700582 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700583 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700584
Dan Albert914449f2016-06-17 16:45:24 -0700585 // Rewrites the names of shared libraries into the names of the NDK
586 // libraries where appropriate. This returns two slices.
587 //
588 // The first is a list of non-variant shared libraries (either rewritten
589 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
590 // because they are not NDK libraries).
591 //
592 // The second is a list of ndk_library modules. These need to be
593 // separated because they are a variation dependency and must be added
594 // in a different manner.
595 rewriteNdkLibs := func(list []string) ([]string, []string) {
596 variantLibs := []string{}
597 nonvariantLibs := []string{}
598 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700599 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700600 if !inList(entry, ndkMigratedLibs) {
601 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
602 } else {
603 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
604 }
605 } else {
606 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700607 }
608 }
Dan Albert914449f2016-06-17 16:45:24 -0700609 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700610 }
611
Dan Albert914449f2016-06-17 16:45:24 -0700612 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
613 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700614 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700615
616 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
617 deps.WholeStaticLibs...)
618
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700619 for _, lib := range deps.StaticLibs {
620 depTag := staticDepTag
621 if inList(lib, deps.ReexportStaticLibHeaders) {
622 depTag = staticExportDepTag
623 }
Colin Cross15a0d462016-07-14 14:49:58 -0700624 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700625 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700626
627 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
628 deps.LateStaticLibs...)
629
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700630 for _, lib := range deps.SharedLibs {
631 depTag := sharedDepTag
632 if inList(lib, deps.ReexportSharedLibHeaders) {
633 depTag = sharedExportDepTag
634 }
Colin Cross15a0d462016-07-14 14:49:58 -0700635 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700636 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700637
638 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
639 deps.LateSharedLibs...)
640
Colin Cross68861832016-07-08 10:41:41 -0700641 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700642
643 for _, gen := range deps.GeneratedHeaders {
644 depTag := genHeaderDepTag
645 if inList(gen, deps.ReexportGeneratedHeaders) {
646 depTag = genHeaderExportDepTag
647 }
648 actx.AddDependency(c, depTag, gen)
649 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700650
Colin Cross68861832016-07-08 10:41:41 -0700651 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700652
653 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700654 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800655 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700656 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700657 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700658 }
Dan Albert914449f2016-06-17 16:45:24 -0700659
660 version := ctx.sdkVersion()
661 actx.AddVariationDependencies([]blueprint.Variation{
662 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
663 actx.AddVariationDependencies([]blueprint.Variation{
664 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700665}
Colin Cross21b9a242015-03-24 14:15:58 -0700666
Dan Albert7e9d2952016-08-04 13:02:36 -0700667func beginMutator(ctx android.BottomUpMutatorContext) {
668 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
669 c.beginMutator(ctx)
670 }
671}
672
Colin Crossca860ac2016-01-04 14:34:37 -0800673func (c *Module) clang(ctx BaseModuleContext) bool {
674 clang := Bool(c.Properties.Clang)
675
676 if c.Properties.Clang == nil {
677 if ctx.Host() {
678 clang = true
679 }
680
681 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
682 clang = true
683 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800684 }
Colin Cross28344522015-04-22 13:07:53 -0700685
Colin Crossca860ac2016-01-04 14:34:37 -0800686 if !c.toolchain(ctx).ClangSupported() {
687 clang = false
688 }
689
690 return clang
691}
692
Colin Crossc99deeb2016-04-11 15:06:20 -0700693// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700694func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800695 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800696
Dan Willemsena96ff642016-06-07 12:34:45 -0700697 // Whether a module can link to another module, taking into
698 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700699 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700700 if from.Target().Os != android.Android {
701 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700702 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700703 }
704 if from.Properties.Sdk_version == "" {
705 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700706 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700707 }
Colin Crossb916a382016-07-29 17:28:03 -0700708 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700709 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700710 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700711 }
712 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
713 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700714 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700715 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700716 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
717 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700718 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700719 }
Colin Crossb916a382016-07-29 17:28:03 -0700720 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700721 // These aren't real libraries, but are the stub shared libraries that are included in
722 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700723 return
Dan Albert914449f2016-06-17 16:45:24 -0700724 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700725 if to.Properties.Sdk_version == "" {
726 // NDK code linking to platform code is never okay.
727 ctx.ModuleErrorf("depends on non-NDK-built library %q",
728 ctx.OtherModuleName(to))
729 }
730
731 // All this point we know we have two NDK libraries, but we need to
732 // check that we're not linking against anything built against a higher
733 // API level, as it is only valid to link against older or equivalent
734 // APIs.
735
736 if from.Properties.Sdk_version == "current" {
737 // Current can link against anything.
738 return
739 } else if to.Properties.Sdk_version == "current" {
740 // Current can't be linked against by anything else.
741 ctx.ModuleErrorf("links %q built against newer API version %q",
742 ctx.OtherModuleName(to), "current")
743 }
744
745 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
746 if err != nil {
747 ctx.PropertyErrorf("sdk_version",
748 "Invalid sdk_version value (must be int): %q",
749 from.Properties.Sdk_version)
750 }
751 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
752 if err != nil {
753 ctx.PropertyErrorf("sdk_version",
754 "Invalid sdk_version value (must be int): %q",
755 to.Properties.Sdk_version)
756 }
757
758 if toApi > fromApi {
759 ctx.ModuleErrorf("links %q built against newer API version %q",
760 ctx.OtherModuleName(to), to.Properties.Sdk_version)
761 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700762 }
763
Colin Crossc99deeb2016-04-11 15:06:20 -0700764 ctx.VisitDirectDeps(func(m blueprint.Module) {
765 name := ctx.OtherModuleName(m)
766 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800767
Colin Cross635c3b02016-05-18 15:37:25 -0700768 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700769 if a == nil {
770 ctx.ModuleErrorf("module %q not an android module", name)
771 return
Colin Crossca860ac2016-01-04 14:34:37 -0800772 }
Colin Crossca860ac2016-01-04 14:34:37 -0800773
Dan Willemsena96ff642016-06-07 12:34:45 -0700774 cc, _ := m.(*Module)
775 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700776 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700777 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700778 case genSourceDepTag:
779 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
780 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
781 genRule.GeneratedSourceFiles()...)
782 } else {
783 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
784 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700785 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700786 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
787 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
788 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700789 flags := includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()})
790 depPaths.Flags = append(depPaths.Flags, flags)
791 if tag == genHeaderExportDepTag {
792 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700793 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
794 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700795 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700796 } else {
797 ctx.ModuleErrorf("module %q is not a genrule", name)
798 }
799 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700800 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800801 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700802 return
803 }
804
805 if !a.Enabled() {
806 ctx.ModuleErrorf("depends on disabled module %q", name)
807 return
808 }
809
Colin Crossa1ad8d12016-06-01 17:09:44 -0700810 if a.Target().Os != ctx.Os() {
811 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
812 return
813 }
814
815 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
816 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700817 return
818 }
819
Colin Crossc99deeb2016-04-11 15:06:20 -0700820 if tag == reuseObjTag {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700821 depPaths.Objs = depPaths.Objs.Append(cc.compiler.(libraryInterface).reuseObjs())
Colin Crossc99deeb2016-04-11 15:06:20 -0700822 return
823 }
824
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700825 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700826 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700827 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700828 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700829 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700830 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700831
832 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700833 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700834 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700835 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700836 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700837
Dan Albert9e10cd42016-08-03 14:12:14 -0700838 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700839 }
840
Colin Cross26c34ed2016-09-30 17:10:16 -0700841 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700842 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700843
Colin Cross26c34ed2016-09-30 17:10:16 -0700844 linkFile := cc.outputFile
845 depFile := android.OptionalPath{}
846
Colin Crossc99deeb2016-04-11 15:06:20 -0700847 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700848 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700849 ptr = &depPaths.SharedLibs
850 depPtr = &depPaths.SharedLibsDeps
851 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700852 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700853 ptr = &depPaths.LateSharedLibs
854 depPtr = &depPaths.LateSharedLibsDeps
855 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700856 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700857 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700858 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700859 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700860 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700861 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700862 staticLib, ok := cc.linker.(libraryInterface)
863 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700864 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700865 return
866 }
867
868 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
869 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
870 for i := range missingDeps {
871 missingDeps[i] += postfix
872 }
873 ctx.AddMissingDependencies(missingDeps)
874 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700875 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Crossc99deeb2016-04-11 15:06:20 -0700876 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700877 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700878 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700879 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700880 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700881 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700882 }
883
Colin Cross26c34ed2016-09-30 17:10:16 -0700884 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700885 if !linkFile.Valid() {
886 ctx.ModuleErrorf("module %q missing output file", name)
887 return
888 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700889 *ptr = append(*ptr, linkFile.Path())
890 }
891
Colin Crossc99deeb2016-04-11 15:06:20 -0700892 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700893 dep := depFile
894 if !dep.Valid() {
895 dep = linkFile
896 }
897 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800898 }
899 })
900
901 return depPaths
902}
903
904func (c *Module) InstallInData() bool {
905 if c.installer == nil {
906 return false
907 }
Colin Cross94610402016-08-29 13:41:32 -0700908 if c.sanitize != nil && c.sanitize.inData() {
909 return true
910 }
Colin Crossca860ac2016-01-04 14:34:37 -0800911 return c.installer.inData()
912}
913
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700914func (c *Module) HostToolPath() android.OptionalPath {
915 if c.installer == nil {
916 return android.OptionalPath{}
917 }
918 return c.installer.hostToolPath()
919}
920
Colin Cross2ba19d92015-05-07 15:44:20 -0700921//
Colin Crosscfad1192015-11-02 16:43:11 -0800922// Defaults
923//
Colin Crossca860ac2016-01-04 14:34:37 -0800924type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700925 android.ModuleBase
926 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800927}
928
Colin Cross635c3b02016-05-18 15:37:25 -0700929func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800930}
931
Colin Cross1e676be2016-10-12 14:38:15 -0700932func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
933}
934
Colin Crossca860ac2016-01-04 14:34:37 -0800935func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700936 return DefaultsFactory()
937}
938
939func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800940 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800941
Colin Crosse1d764e2016-08-18 14:18:32 -0700942 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800943 &BaseProperties{},
944 &BaseCompilerProperties{},
945 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700946 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700947 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800948 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700949 &TestProperties{},
950 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800951 &UnusedProperties{},
952 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800953 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700954 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700955 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700956 &TidyProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700957 )
Colin Crosscfad1192015-11-02 16:43:11 -0800958
Colin Crosse1d764e2016-08-18 14:18:32 -0700959 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800960}
961
Colin Cross74d1ec02015-04-28 13:30:13 -0700962// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
963// modifies the slice contents in place, and returns a subslice of the original slice
964func lastUniqueElements(list []string) []string {
965 totalSkip := 0
966 for i := len(list) - 1; i >= totalSkip; i-- {
967 skip := 0
968 for j := i - 1; j >= totalSkip; j-- {
969 if list[i] == list[j] {
970 skip++
971 } else {
972 list[j+skip] = list[j]
973 }
974 }
975 totalSkip += skip
976 }
977 return list[totalSkip:]
978}
Colin Cross06a931b2015-10-28 17:23:31 -0700979
980var Bool = proptools.Bool