blob: 33c9611c17e65e67a456c60485ad2d54c9e0f9b8 [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
Colin Cross635c3b02016-05-18 15:37:25 -070079 ObjFiles android.Paths
80 WholeStaticLibObjFiles android.Paths
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
Colin Cross28344522015-04-22 13:07:53 -0700103
Colin Crossb98c8b02016-07-29 13:44:28 -0700104 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700105 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800106
107 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800108 DynamicLinker string
109
Colin Cross635c3b02016-05-18 15:37:25 -0700110 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700111}
112
Colin Cross81413472016-04-11 14:37:39 -0700113type ObjectLinkerProperties struct {
114 // names of other cc_object modules to link into this module using partial linking
115 Objs []string `android:"arch_variant"`
116}
117
Colin Crossca860ac2016-01-04 14:34:37 -0800118// Properties used to compile all C or C++ modules
119type BaseProperties struct {
120 // compile module with clang instead of gcc
121 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700122
123 // Minimum sdk version supported when compiling against the ndk
124 Sdk_version string
125
Colin Crossca860ac2016-01-04 14:34:37 -0800126 // don't insert default compiler flags into asflags, cflags,
127 // cppflags, conlyflags, ldflags, or include_dirs
128 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700129
130 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700131 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700132 PreventInstall bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800133}
134
Colin Crossca860ac2016-01-04 14:34:37 -0800135type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700136 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700137 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800138}
139
Colin Crossca860ac2016-01-04 14:34:37 -0800140type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800141 static() bool
142 staticBinary() bool
143 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700144 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800145 noDefaultCompilerFlags() bool
146 sdk() bool
147 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700148 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700149 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800150}
151
152type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700153 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800154 ModuleContextIntf
155}
156
157type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700158 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800159 ModuleContextIntf
160}
161
Colin Crossca860ac2016-01-04 14:34:37 -0800162type feature interface {
163 begin(ctx BaseModuleContext)
164 deps(ctx BaseModuleContext, deps Deps) Deps
165 flags(ctx ModuleContext, flags Flags) Flags
166 props() []interface{}
167}
168
169type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700170 compilerInit(ctx BaseModuleContext)
171 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
172 compilerFlags(ctx ModuleContext, flags Flags) Flags
173 compilerProps() []interface{}
174
Colin Cross76fada02016-07-27 10:31:13 -0700175 appendCflags([]string)
176 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700177 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800178}
179
180type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700181 linkerInit(ctx BaseModuleContext)
182 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
183 linkerFlags(ctx ModuleContext, flags Flags) Flags
184 linkerProps() []interface{}
185
Colin Cross635c3b02016-05-18 15:37:25 -0700186 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700187 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800188}
189
190type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700191 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700192 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800193 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700194 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800195}
196
Colin Crossc99deeb2016-04-11 15:06:20 -0700197type dependencyTag struct {
198 blueprint.BaseDependencyTag
199 name string
200 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700201
202 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700203}
204
205var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700206 sharedDepTag = dependencyTag{name: "shared", library: true}
207 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
208 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
209 staticDepTag = dependencyTag{name: "static", library: true}
210 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
211 lateStaticDepTag = dependencyTag{name: "late static", library: true}
212 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
213 genSourceDepTag = dependencyTag{name: "gen source"}
214 genHeaderDepTag = dependencyTag{name: "gen header"}
215 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
216 objDepTag = dependencyTag{name: "obj"}
217 crtBeginDepTag = dependencyTag{name: "crtbegin"}
218 crtEndDepTag = dependencyTag{name: "crtend"}
219 reuseObjTag = dependencyTag{name: "reuse objects"}
220 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
221 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700222)
223
Colin Crossca860ac2016-01-04 14:34:37 -0800224// Module contains the properties and members used by all C/C++ module types, and implements
225// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
226// to construct the output file. Behavior can be customized with a Customizer interface
227type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700228 android.ModuleBase
229 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700230
Colin Crossca860ac2016-01-04 14:34:37 -0800231 Properties BaseProperties
232 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700233
Colin Crossca860ac2016-01-04 14:34:37 -0800234 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700235 hod android.HostOrDeviceSupported
236 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700237
Colin Crossca860ac2016-01-04 14:34:37 -0800238 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700239 features []feature
240 compiler compiler
241 linker linker
242 installer installer
243 stl *stl
244 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800245
246 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700247
Colin Cross635c3b02016-05-18 15:37:25 -0700248 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800249
Colin Crossb98c8b02016-07-29 13:44:28 -0700250 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700251
252 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700253}
254
Colin Crossca860ac2016-01-04 14:34:37 -0800255func (c *Module) Init() (blueprint.Module, []interface{}) {
256 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800257 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700258 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800259 }
260 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700261 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800262 }
263 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700264 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800265 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700266 if c.stl != nil {
267 props = append(props, c.stl.props()...)
268 }
Colin Cross16b23492016-01-06 14:41:07 -0800269 if c.sanitize != nil {
270 props = append(props, c.sanitize.props()...)
271 }
Colin Crossca860ac2016-01-04 14:34:37 -0800272 for _, feature := range c.features {
273 props = append(props, feature.props()...)
274 }
Colin Crossc472d572015-03-17 15:06:21 -0700275
Colin Cross635c3b02016-05-18 15:37:25 -0700276 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700277
Colin Cross635c3b02016-05-18 15:37:25 -0700278 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700279}
280
Colin Crossb916a382016-07-29 17:28:03 -0700281// Returns true for dependency roots (binaries)
282// TODO(ccross): also handle dlopenable libraries
283func (c *Module) isDependencyRoot() bool {
284 if root, ok := c.linker.(interface {
285 isDependencyRoot() bool
286 }); ok {
287 return root.isDependencyRoot()
288 }
289 return false
290}
291
Colin Crossca860ac2016-01-04 14:34:37 -0800292type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700293 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800294 moduleContextImpl
295}
296
297type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700298 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800299 moduleContextImpl
300}
301
302type moduleContextImpl struct {
303 mod *Module
304 ctx BaseModuleContext
305}
306
Colin Crossca860ac2016-01-04 14:34:37 -0800307func (ctx *moduleContextImpl) clang() bool {
308 return ctx.mod.clang(ctx.ctx)
309}
310
Colin Crossb98c8b02016-07-29 13:44:28 -0700311func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800312 return ctx.mod.toolchain(ctx.ctx)
313}
314
315func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700316 if static, ok := ctx.mod.linker.(interface {
317 static() bool
318 }); ok {
319 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800320 }
Colin Crossb916a382016-07-29 17:28:03 -0700321 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800322}
323
324func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700325 if static, ok := ctx.mod.linker.(interface {
326 staticBinary() bool
327 }); ok {
328 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800329 }
Colin Crossb916a382016-07-29 17:28:03 -0700330 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800331}
332
333func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
334 return Bool(ctx.mod.Properties.No_default_compiler_flags)
335}
336
337func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700338 if ctx.ctx.Device() {
339 return ctx.mod.Properties.Sdk_version != ""
340 }
341 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800342}
343
344func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700345 if ctx.ctx.Device() {
346 return ctx.mod.Properties.Sdk_version
347 }
348 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800349}
350
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700351func (ctx *moduleContextImpl) selectedStl() string {
352 if stl := ctx.mod.stl; stl != nil {
353 return stl.Properties.SelectedStl
354 }
355 return ""
356}
357
Colin Crossce75d2c2016-10-06 16:12:58 -0700358func (ctx *moduleContextImpl) baseModuleName() string {
359 return ctx.mod.ModuleBase.BaseModuleName()
360}
361
Colin Cross635c3b02016-05-18 15:37:25 -0700362func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800363 return &Module{
364 hod: hod,
365 multilib: multilib,
366 }
367}
368
Colin Cross635c3b02016-05-18 15:37:25 -0700369func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800370 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700371 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800372 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800373 return module
374}
375
Colin Crossce75d2c2016-10-06 16:12:58 -0700376func (c *Module) Prebuilt() *android.Prebuilt {
377 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
378 return p.prebuilt()
379 }
380 return nil
381}
382
383func (c *Module) Name() string {
384 name := c.ModuleBase.Name()
385 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
386 name = p.Name(name)
387 }
388 return name
389}
390
Colin Cross635c3b02016-05-18 15:37:25 -0700391func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800392 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700393 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800394 moduleContextImpl: moduleContextImpl{
395 mod: c,
396 },
397 }
398 ctx.ctx = ctx
399
400 flags := Flags{
401 Toolchain: c.toolchain(ctx),
402 Clang: c.clang(ctx),
403 }
Colin Crossca860ac2016-01-04 14:34:37 -0800404 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700405 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800406 }
407 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700408 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800409 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700410 if c.stl != nil {
411 flags = c.stl.flags(ctx, flags)
412 }
Colin Cross16b23492016-01-06 14:41:07 -0800413 if c.sanitize != nil {
414 flags = c.sanitize.flags(ctx, flags)
415 }
Colin Crossca860ac2016-01-04 14:34:37 -0800416 for _, feature := range c.features {
417 flags = feature.flags(ctx, flags)
418 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800419 if ctx.Failed() {
420 return
421 }
422
Colin Crossb98c8b02016-07-29 13:44:28 -0700423 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
424 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
425 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800426
Colin Crossca860ac2016-01-04 14:34:37 -0800427 // Optimization to reduce size of build.ninja
428 // Replace the long list of flags for each file with a module-local variable
429 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
430 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
431 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
432 flags.CFlags = []string{"$cflags"}
433 flags.CppFlags = []string{"$cppflags"}
434 flags.AsFlags = []string{"$asflags"}
435
Colin Crossc99deeb2016-04-11 15:06:20 -0700436 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800437 if ctx.Failed() {
438 return
439 }
440
Dan Willemsen76f08272016-07-09 00:14:08 -0700441 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700442
Colin Cross635c3b02016-05-18 15:37:25 -0700443 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800444 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700445 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800446 if ctx.Failed() {
447 return
448 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800449 }
450
Colin Crossca860ac2016-01-04 14:34:37 -0800451 if c.linker != nil {
452 outputFile := c.linker.link(ctx, flags, deps, objFiles)
453 if ctx.Failed() {
454 return
455 }
Colin Cross635c3b02016-05-18 15:37:25 -0700456 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700457 }
Colin Cross5049f022015-03-18 13:28:46 -0700458
Colin Crossce75d2c2016-10-06 16:12:58 -0700459 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
460 c.installer.install(ctx, c.outputFile.Path())
461 if ctx.Failed() {
462 return
Colin Crossca860ac2016-01-04 14:34:37 -0800463 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700464 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800465}
466
Colin Crossb98c8b02016-07-29 13:44:28 -0700467func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800468 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700469 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 }
Colin Crossca860ac2016-01-04 14:34:37 -0800471 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800472}
473
Colin Crossca860ac2016-01-04 14:34:37 -0800474func (c *Module) begin(ctx BaseModuleContext) {
475 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700476 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700477 }
Colin Crossca860ac2016-01-04 14:34:37 -0800478 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700479 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800480 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700481 if c.stl != nil {
482 c.stl.begin(ctx)
483 }
Colin Cross16b23492016-01-06 14:41:07 -0800484 if c.sanitize != nil {
485 c.sanitize.begin(ctx)
486 }
Colin Crossca860ac2016-01-04 14:34:37 -0800487 for _, feature := range c.features {
488 feature.begin(ctx)
489 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700490 if ctx.sdk() {
491 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
492 if err != nil {
493 ctx.PropertyErrorf("sdk_version", err.Error())
494 }
495 c.Properties.Sdk_version = strconv.Itoa(version)
496 }
Colin Crossca860ac2016-01-04 14:34:37 -0800497}
498
Colin Crossc99deeb2016-04-11 15:06:20 -0700499func (c *Module) deps(ctx BaseModuleContext) Deps {
500 deps := Deps{}
501
502 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700503 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700504 }
505 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700506 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700507 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700508 if c.stl != nil {
509 deps = c.stl.deps(ctx, deps)
510 }
Colin Cross16b23492016-01-06 14:41:07 -0800511 if c.sanitize != nil {
512 deps = c.sanitize.deps(ctx, deps)
513 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700514 for _, feature := range c.features {
515 deps = feature.deps(ctx, deps)
516 }
517
518 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
519 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
520 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
521 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
522 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
523
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700524 for _, lib := range deps.ReexportSharedLibHeaders {
525 if !inList(lib, deps.SharedLibs) {
526 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
527 }
528 }
529
530 for _, lib := range deps.ReexportStaticLibHeaders {
531 if !inList(lib, deps.StaticLibs) {
532 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
533 }
534 }
535
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700536 for _, gen := range deps.ReexportGeneratedHeaders {
537 if !inList(gen, deps.GeneratedHeaders) {
538 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
539 }
540 }
541
Colin Crossc99deeb2016-04-11 15:06:20 -0700542 return deps
543}
544
Dan Albert7e9d2952016-08-04 13:02:36 -0700545func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800546 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700547 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800548 moduleContextImpl: moduleContextImpl{
549 mod: c,
550 },
551 }
552 ctx.ctx = ctx
553
Colin Crossca860ac2016-01-04 14:34:37 -0800554 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700555}
556
Colin Cross1e676be2016-10-12 14:38:15 -0700557func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
558 if !c.Enabled() {
559 return
560 }
561
Dan Albert7e9d2952016-08-04 13:02:36 -0700562 ctx := &baseModuleContext{
563 BaseContext: actx,
564 moduleContextImpl: moduleContextImpl{
565 mod: c,
566 },
567 }
568 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800569
Colin Crossc99deeb2016-04-11 15:06:20 -0700570 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800571
Colin Crossb5bc4b42016-07-11 16:11:59 -0700572 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
573 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700574
Dan Albert914449f2016-06-17 16:45:24 -0700575 variantNdkLibs := []string{}
576 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700577 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700578 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700579
Dan Albert914449f2016-06-17 16:45:24 -0700580 // Rewrites the names of shared libraries into the names of the NDK
581 // libraries where appropriate. This returns two slices.
582 //
583 // The first is a list of non-variant shared libraries (either rewritten
584 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
585 // because they are not NDK libraries).
586 //
587 // The second is a list of ndk_library modules. These need to be
588 // separated because they are a variation dependency and must be added
589 // in a different manner.
590 rewriteNdkLibs := func(list []string) ([]string, []string) {
591 variantLibs := []string{}
592 nonvariantLibs := []string{}
593 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700594 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700595 if !inList(entry, ndkMigratedLibs) {
596 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
597 } else {
598 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
599 }
600 } else {
601 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700602 }
603 }
Dan Albert914449f2016-06-17 16:45:24 -0700604 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700605 }
606
Dan Albert914449f2016-06-17 16:45:24 -0700607 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
608 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700609 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700610
611 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
612 deps.WholeStaticLibs...)
613
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700614 for _, lib := range deps.StaticLibs {
615 depTag := staticDepTag
616 if inList(lib, deps.ReexportStaticLibHeaders) {
617 depTag = staticExportDepTag
618 }
Colin Cross15a0d462016-07-14 14:49:58 -0700619 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700620 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700621
622 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
623 deps.LateStaticLibs...)
624
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700625 for _, lib := range deps.SharedLibs {
626 depTag := sharedDepTag
627 if inList(lib, deps.ReexportSharedLibHeaders) {
628 depTag = sharedExportDepTag
629 }
Colin Cross15a0d462016-07-14 14:49:58 -0700630 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700631 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700632
633 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
634 deps.LateSharedLibs...)
635
Colin Cross68861832016-07-08 10:41:41 -0700636 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700637
638 for _, gen := range deps.GeneratedHeaders {
639 depTag := genHeaderDepTag
640 if inList(gen, deps.ReexportGeneratedHeaders) {
641 depTag = genHeaderExportDepTag
642 }
643 actx.AddDependency(c, depTag, gen)
644 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700645
Colin Cross68861832016-07-08 10:41:41 -0700646 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700647
648 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700649 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800650 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700651 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700652 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700653 }
Dan Albert914449f2016-06-17 16:45:24 -0700654
655 version := ctx.sdkVersion()
656 actx.AddVariationDependencies([]blueprint.Variation{
657 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
658 actx.AddVariationDependencies([]blueprint.Variation{
659 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700660}
Colin Cross21b9a242015-03-24 14:15:58 -0700661
Dan Albert7e9d2952016-08-04 13:02:36 -0700662func beginMutator(ctx android.BottomUpMutatorContext) {
663 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
664 c.beginMutator(ctx)
665 }
666}
667
Colin Crossca860ac2016-01-04 14:34:37 -0800668func (c *Module) clang(ctx BaseModuleContext) bool {
669 clang := Bool(c.Properties.Clang)
670
671 if c.Properties.Clang == nil {
672 if ctx.Host() {
673 clang = true
674 }
675
676 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
677 clang = true
678 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800679 }
Colin Cross28344522015-04-22 13:07:53 -0700680
Colin Crossca860ac2016-01-04 14:34:37 -0800681 if !c.toolchain(ctx).ClangSupported() {
682 clang = false
683 }
684
685 return clang
686}
687
Colin Crossc99deeb2016-04-11 15:06:20 -0700688// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700689func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800690 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800691
Dan Willemsena96ff642016-06-07 12:34:45 -0700692 // Whether a module can link to another module, taking into
693 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700694 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700695 if from.Target().Os != android.Android {
696 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700697 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700698 }
699 if from.Properties.Sdk_version == "" {
700 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700701 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700702 }
Colin Crossb916a382016-07-29 17:28:03 -0700703 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700704 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700705 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700706 }
707 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
708 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700709 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700710 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700711 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
712 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700713 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700714 }
Colin Crossb916a382016-07-29 17:28:03 -0700715 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700716 // These aren't real libraries, but are the stub shared libraries that are included in
717 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700718 return
Dan Albert914449f2016-06-17 16:45:24 -0700719 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700720 if to.Properties.Sdk_version == "" {
721 // NDK code linking to platform code is never okay.
722 ctx.ModuleErrorf("depends on non-NDK-built library %q",
723 ctx.OtherModuleName(to))
724 }
725
726 // All this point we know we have two NDK libraries, but we need to
727 // check that we're not linking against anything built against a higher
728 // API level, as it is only valid to link against older or equivalent
729 // APIs.
730
731 if from.Properties.Sdk_version == "current" {
732 // Current can link against anything.
733 return
734 } else if to.Properties.Sdk_version == "current" {
735 // Current can't be linked against by anything else.
736 ctx.ModuleErrorf("links %q built against newer API version %q",
737 ctx.OtherModuleName(to), "current")
738 }
739
740 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
741 if err != nil {
742 ctx.PropertyErrorf("sdk_version",
743 "Invalid sdk_version value (must be int): %q",
744 from.Properties.Sdk_version)
745 }
746 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
747 if err != nil {
748 ctx.PropertyErrorf("sdk_version",
749 "Invalid sdk_version value (must be int): %q",
750 to.Properties.Sdk_version)
751 }
752
753 if toApi > fromApi {
754 ctx.ModuleErrorf("links %q built against newer API version %q",
755 ctx.OtherModuleName(to), to.Properties.Sdk_version)
756 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700757 }
758
Colin Crossc99deeb2016-04-11 15:06:20 -0700759 ctx.VisitDirectDeps(func(m blueprint.Module) {
760 name := ctx.OtherModuleName(m)
761 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800762
Colin Cross635c3b02016-05-18 15:37:25 -0700763 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700764 if a == nil {
765 ctx.ModuleErrorf("module %q not an android module", name)
766 return
Colin Crossca860ac2016-01-04 14:34:37 -0800767 }
Colin Crossca860ac2016-01-04 14:34:37 -0800768
Dan Willemsena96ff642016-06-07 12:34:45 -0700769 cc, _ := m.(*Module)
770 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700771 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700772 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700773 case genSourceDepTag:
774 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
775 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
776 genRule.GeneratedSourceFiles()...)
777 } else {
778 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
779 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700780 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700781 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
782 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
783 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700784 flags := includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()})
785 depPaths.Flags = append(depPaths.Flags, flags)
786 if tag == genHeaderExportDepTag {
787 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700788 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
789 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700790 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700791 } else {
792 ctx.ModuleErrorf("module %q is not a genrule", name)
793 }
794 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700795 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800796 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700797 return
798 }
799
800 if !a.Enabled() {
801 ctx.ModuleErrorf("depends on disabled module %q", name)
802 return
803 }
804
Colin Crossa1ad8d12016-06-01 17:09:44 -0700805 if a.Target().Os != ctx.Os() {
806 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
807 return
808 }
809
810 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
811 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700812 return
813 }
814
Colin Crossc99deeb2016-04-11 15:06:20 -0700815 if tag == reuseObjTag {
816 depPaths.ObjFiles = append(depPaths.ObjFiles,
Colin Crossb916a382016-07-29 17:28:03 -0700817 cc.compiler.(libraryInterface).reuseObjs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700818 return
819 }
820
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700821 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700822 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700823 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700824 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700825 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700826 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700827
828 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700829 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700830 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700831 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700832 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700833
Dan Albert9e10cd42016-08-03 14:12:14 -0700834 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700835 }
836
Colin Cross26c34ed2016-09-30 17:10:16 -0700837 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700838 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700839
Colin Cross26c34ed2016-09-30 17:10:16 -0700840 linkFile := cc.outputFile
841 depFile := android.OptionalPath{}
842
Colin Crossc99deeb2016-04-11 15:06:20 -0700843 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700844 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700845 ptr = &depPaths.SharedLibs
846 depPtr = &depPaths.SharedLibsDeps
847 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700848 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700849 ptr = &depPaths.LateSharedLibs
850 depPtr = &depPaths.LateSharedLibsDeps
851 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700852 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700853 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700854 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700855 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700856 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700857 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700858 staticLib, ok := cc.linker.(libraryInterface)
859 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700860 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700861 return
862 }
863
864 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
865 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
866 for i := range missingDeps {
867 missingDeps[i] += postfix
868 }
869 ctx.AddMissingDependencies(missingDeps)
870 }
871 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700872 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700873 case objDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700874 ptr = &depPaths.ObjFiles
Colin Crossc99deeb2016-04-11 15:06:20 -0700875 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700876 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700877 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700878 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700879 }
880
Colin Cross26c34ed2016-09-30 17:10:16 -0700881 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700882 if !linkFile.Valid() {
883 ctx.ModuleErrorf("module %q missing output file", name)
884 return
885 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700886 *ptr = append(*ptr, linkFile.Path())
887 }
888
Colin Crossc99deeb2016-04-11 15:06:20 -0700889 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700890 dep := depFile
891 if !dep.Valid() {
892 dep = linkFile
893 }
894 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800895 }
896 })
897
898 return depPaths
899}
900
901func (c *Module) InstallInData() bool {
902 if c.installer == nil {
903 return false
904 }
Colin Cross94610402016-08-29 13:41:32 -0700905 if c.sanitize != nil && c.sanitize.inData() {
906 return true
907 }
Colin Crossca860ac2016-01-04 14:34:37 -0800908 return c.installer.inData()
909}
910
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700911func (c *Module) HostToolPath() android.OptionalPath {
912 if c.installer == nil {
913 return android.OptionalPath{}
914 }
915 return c.installer.hostToolPath()
916}
917
Colin Cross2ba19d92015-05-07 15:44:20 -0700918//
Colin Crosscfad1192015-11-02 16:43:11 -0800919// Defaults
920//
Colin Crossca860ac2016-01-04 14:34:37 -0800921type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700922 android.ModuleBase
923 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800924}
925
Colin Cross635c3b02016-05-18 15:37:25 -0700926func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800927}
928
Colin Cross1e676be2016-10-12 14:38:15 -0700929func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
930}
931
Colin Crossca860ac2016-01-04 14:34:37 -0800932func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700933 return DefaultsFactory()
934}
935
936func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800937 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800938
Colin Crosse1d764e2016-08-18 14:18:32 -0700939 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800940 &BaseProperties{},
941 &BaseCompilerProperties{},
942 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700943 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700944 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800945 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700946 &TestProperties{},
947 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800948 &UnusedProperties{},
949 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800950 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700951 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700952 &InstallerProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700953 )
Colin Crosscfad1192015-11-02 16:43:11 -0800954
Colin Crosse1d764e2016-08-18 14:18:32 -0700955 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800956}
957
Colin Cross74d1ec02015-04-28 13:30:13 -0700958// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
959// modifies the slice contents in place, and returns a subslice of the original slice
960func lastUniqueElements(list []string) []string {
961 totalSkip := 0
962 for i := len(list) - 1; i >= totalSkip; i-- {
963 skip := 0
964 for j := i - 1; j >= totalSkip; j-- {
965 if list[i] == list[j] {
966 skip++
967 } else {
968 list[j+skip] = list[j]
969 }
970 }
971 totalSkip += skip
972 }
973 return list[totalSkip:]
974}
Colin Cross06a931b2015-10-28 17:23:31 -0700975
976var Bool = proptools.Bool