blob: 3c517acbabde5fd05d3de7330fef25376420500c [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 (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
Dan Albert9e10cd42016-08-03 14:12:14 -070023 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070031 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070032 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Crossca860ac2016-01-04 14:34:37 -080036 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070037
Colin Cross463a90e2015-06-17 14:20:06 -070038 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
39 // the Go initialization order because this package depends on common, so common's init
40 // functions will run first.
Colin Crosse8a67a72016-08-07 21:17:54 -070041 android.RegisterBottomUpMutator("link", linkageMutator).Parallel()
42 android.RegisterBottomUpMutator("ndk_api", ndkApiMutator).Parallel()
43 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator).Parallel()
44 android.RegisterBottomUpMutator("begin", beginMutator).Parallel()
45 android.RegisterBottomUpMutator("deps", depsMutator).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080046
Colin Cross635c3b02016-05-18 15:37:25 -070047 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
Colin Crosse8a67a72016-08-07 21:17:54 -070048 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080049
Colin Cross635c3b02016-05-18 15:37:25 -070050 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
Colin Crosse8a67a72016-08-07 21:17:54 -070051 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan)).Parallel()
Colin Crossb98c8b02016-07-29 13:44:28 -070052
53 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Colin Crossca860ac2016-01-04 14:34:37 -080056type Deps struct {
57 SharedLibs, LateSharedLibs []string
58 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070059
Dan Willemsen490a8dc2016-06-06 18:22:19 -070060 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
61
Colin Cross81413472016-04-11 14:37:39 -070062 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070063
Dan Willemsenb40aab62016-04-20 14:21:14 -070064 GeneratedSources []string
65 GeneratedHeaders []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 Cross635c3b02016-05-18 15:37:25 -070071 SharedLibs, LateSharedLibs android.Paths
72 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070073
Colin Cross635c3b02016-05-18 15:37:25 -070074 ObjFiles android.Paths
75 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070076
Colin Cross635c3b02016-05-18 15:37:25 -070077 GeneratedSources android.Paths
78 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070079
Dan Willemsen76f08272016-07-09 00:14:08 -070080 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081
Colin Cross635c3b02016-05-18 15:37:25 -070082 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070083}
84
Colin Crossca860ac2016-01-04 14:34:37 -080085type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070086 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
87 AsFlags []string // Flags that apply to assembly source files
88 CFlags []string // Flags that apply to C and C++ source files
89 ConlyFlags []string // Flags that apply to C source files
90 CppFlags []string // Flags that apply to C++ source files
91 YaccFlags []string // Flags that apply to Yacc source files
92 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -080093 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -070094
Colin Crossb98c8b02016-07-29 13:44:28 -070095 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -070096 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -080097
98 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -080099 DynamicLinker string
100
Colin Cross635c3b02016-05-18 15:37:25 -0700101 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700102}
103
Colin Cross81413472016-04-11 14:37:39 -0700104type ObjectLinkerProperties struct {
105 // names of other cc_object modules to link into this module using partial linking
106 Objs []string `android:"arch_variant"`
107}
108
Colin Crossca860ac2016-01-04 14:34:37 -0800109// Properties used to compile all C or C++ modules
110type BaseProperties struct {
111 // compile module with clang instead of gcc
112 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700113
114 // Minimum sdk version supported when compiling against the ndk
115 Sdk_version string
116
Colin Crossca860ac2016-01-04 14:34:37 -0800117 // don't insert default compiler flags into asflags, cflags,
118 // cppflags, conlyflags, ldflags, or include_dirs
119 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700120
121 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700122 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800123}
124
Colin Crossca860ac2016-01-04 14:34:37 -0800125type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700126 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700127 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800128}
129
Colin Crossca860ac2016-01-04 14:34:37 -0800130type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800131 static() bool
132 staticBinary() bool
133 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700134 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800135 noDefaultCompilerFlags() bool
136 sdk() bool
137 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700138 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800139}
140
141type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700142 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800143 ModuleContextIntf
144}
145
146type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700147 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800148 ModuleContextIntf
149}
150
Colin Cross76fada02016-07-27 10:31:13 -0700151type CustomizerFlagsContext interface {
152 BaseModuleContext
153 AppendCflags(...string)
154 AppendLdflags(...string)
155 AppendAsflags(...string)
156}
157
Colin Crossca860ac2016-01-04 14:34:37 -0800158type Customizer interface {
Colin Cross76fada02016-07-27 10:31:13 -0700159 Flags(CustomizerFlagsContext)
Colin Crossca860ac2016-01-04 14:34:37 -0800160 Properties() []interface{}
161}
162
163type feature interface {
164 begin(ctx BaseModuleContext)
165 deps(ctx BaseModuleContext, deps Deps) Deps
166 flags(ctx ModuleContext, flags Flags) Flags
167 props() []interface{}
168}
169
170type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700171 compilerInit(ctx BaseModuleContext)
172 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
173 compilerFlags(ctx ModuleContext, flags Flags) Flags
174 compilerProps() []interface{}
175
Colin Cross76fada02016-07-27 10:31:13 -0700176 appendCflags([]string)
177 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700178 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800179}
180
181type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700182 linkerInit(ctx BaseModuleContext)
183 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
184 linkerFlags(ctx ModuleContext, flags Flags) Flags
185 linkerProps() []interface{}
186
Colin Cross635c3b02016-05-18 15:37:25 -0700187 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700188 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800189}
190
191type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700192 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700193 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800194 inData() bool
195}
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 Willemsen490a8dc2016-06-06 18:22:19 -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 objDepTag = dependencyTag{name: "obj"}
216 crtBeginDepTag = dependencyTag{name: "crtbegin"}
217 crtEndDepTag = dependencyTag{name: "crtend"}
218 reuseObjTag = dependencyTag{name: "reuse objects"}
Dan Albert914449f2016-06-17 16:45:24 -0700219 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
220 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700221)
222
Colin Crossca860ac2016-01-04 14:34:37 -0800223// Module contains the properties and members used by all C/C++ module types, and implements
224// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
225// to construct the output file. Behavior can be customized with a Customizer interface
226type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700227 android.ModuleBase
228 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700229
Colin Crossca860ac2016-01-04 14:34:37 -0800230 Properties BaseProperties
231 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700232
Colin Crossca860ac2016-01-04 14:34:37 -0800233 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700234 hod android.HostOrDeviceSupported
235 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700236
Colin Crossca860ac2016-01-04 14:34:37 -0800237 // delegates, initialize before calling Init
Colin Cross76fada02016-07-27 10:31:13 -0700238 Customizer Customizer
Colin Crossca860ac2016-01-04 14:34:37 -0800239 features []feature
240 compiler compiler
241 linker linker
242 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700243 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800244 sanitize *sanitize
245
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 Cross76fada02016-07-27 10:31:13 -0700257 if c.Customizer != nil {
258 props = append(props, c.Customizer.Properties()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800259 }
260 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700261 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800262 }
263 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700264 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800265 }
266 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700267 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800268 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700269 if c.stl != nil {
270 props = append(props, c.stl.props()...)
271 }
Colin Cross16b23492016-01-06 14:41:07 -0800272 if c.sanitize != nil {
273 props = append(props, c.sanitize.props()...)
274 }
Colin Crossca860ac2016-01-04 14:34:37 -0800275 for _, feature := range c.features {
276 props = append(props, feature.props()...)
277 }
Colin Crossc472d572015-03-17 15:06:21 -0700278
Colin Cross635c3b02016-05-18 15:37:25 -0700279 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700280
Colin Cross635c3b02016-05-18 15:37:25 -0700281 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700282}
283
Colin Crossb916a382016-07-29 17:28:03 -0700284// Returns true for dependency roots (binaries)
285// TODO(ccross): also handle dlopenable libraries
286func (c *Module) isDependencyRoot() bool {
287 if root, ok := c.linker.(interface {
288 isDependencyRoot() bool
289 }); ok {
290 return root.isDependencyRoot()
291 }
292 return false
293}
294
Colin Crossca860ac2016-01-04 14:34:37 -0800295type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700296 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800297 moduleContextImpl
298}
299
300type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700301 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800302 moduleContextImpl
303}
304
305type moduleContextImpl struct {
306 mod *Module
307 ctx BaseModuleContext
308}
309
Colin Cross76fada02016-07-27 10:31:13 -0700310func (ctx *moduleContextImpl) AppendCflags(flags ...string) {
311 CheckBadCompilerFlags(ctx.ctx, "", flags)
312 ctx.mod.compiler.appendCflags(flags)
313}
314
315func (ctx *moduleContextImpl) AppendAsflags(flags ...string) {
316 CheckBadCompilerFlags(ctx.ctx, "", flags)
317 ctx.mod.compiler.appendAsflags(flags)
318}
319
320func (ctx *moduleContextImpl) AppendLdflags(flags ...string) {
321 CheckBadLinkerFlags(ctx.ctx, "", flags)
322 ctx.mod.linker.appendLdflags(flags)
323}
324
Colin Crossca860ac2016-01-04 14:34:37 -0800325func (ctx *moduleContextImpl) clang() bool {
326 return ctx.mod.clang(ctx.ctx)
327}
328
Colin Crossb98c8b02016-07-29 13:44:28 -0700329func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800330 return ctx.mod.toolchain(ctx.ctx)
331}
332
333func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700334 if static, ok := ctx.mod.linker.(interface {
335 static() bool
336 }); ok {
337 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800338 }
Colin Crossb916a382016-07-29 17:28:03 -0700339 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800340}
341
342func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700343 if static, ok := ctx.mod.linker.(interface {
344 staticBinary() bool
345 }); ok {
346 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800347 }
Colin Crossb916a382016-07-29 17:28:03 -0700348 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800349}
350
351func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
352 return Bool(ctx.mod.Properties.No_default_compiler_flags)
353}
354
355func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700356 if ctx.ctx.Device() {
357 return ctx.mod.Properties.Sdk_version != ""
358 }
359 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800360}
361
362func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700363 if ctx.ctx.Device() {
364 return ctx.mod.Properties.Sdk_version
365 }
366 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800367}
368
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700369func (ctx *moduleContextImpl) selectedStl() string {
370 if stl := ctx.mod.stl; stl != nil {
371 return stl.Properties.SelectedStl
372 }
373 return ""
374}
375
Colin Cross635c3b02016-05-18 15:37:25 -0700376func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800377 return &Module{
378 hod: hod,
379 multilib: multilib,
380 }
381}
382
Colin Cross635c3b02016-05-18 15:37:25 -0700383func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800384 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700385 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800386 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800387 return module
388}
389
Colin Cross635c3b02016-05-18 15:37:25 -0700390func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800391 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700392 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800393 moduleContextImpl: moduleContextImpl{
394 mod: c,
395 },
396 }
397 ctx.ctx = ctx
398
Colin Cross76fada02016-07-27 10:31:13 -0700399 if c.Customizer != nil {
400 c.Customizer.Flags(ctx)
401 }
402
Colin Crossca860ac2016-01-04 14:34:37 -0800403 flags := Flags{
404 Toolchain: c.toolchain(ctx),
405 Clang: c.clang(ctx),
406 }
Colin Crossca860ac2016-01-04 14:34:37 -0800407 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700408 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800409 }
410 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700411 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800412 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700413 if c.stl != nil {
414 flags = c.stl.flags(ctx, flags)
415 }
Colin Cross16b23492016-01-06 14:41:07 -0800416 if c.sanitize != nil {
417 flags = c.sanitize.flags(ctx, flags)
418 }
Colin Crossca860ac2016-01-04 14:34:37 -0800419 for _, feature := range c.features {
420 flags = feature.flags(ctx, flags)
421 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800422 if ctx.Failed() {
423 return
424 }
425
Colin Crossb98c8b02016-07-29 13:44:28 -0700426 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
427 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
428 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800429
Colin Crossca860ac2016-01-04 14:34:37 -0800430 // Optimization to reduce size of build.ninja
431 // Replace the long list of flags for each file with a module-local variable
432 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
433 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
434 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
435 flags.CFlags = []string{"$cflags"}
436 flags.CppFlags = []string{"$cppflags"}
437 flags.AsFlags = []string{"$asflags"}
438
Colin Crossc99deeb2016-04-11 15:06:20 -0700439 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800440 if ctx.Failed() {
441 return
442 }
443
Dan Willemsen76f08272016-07-09 00:14:08 -0700444 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700445
Colin Cross635c3b02016-05-18 15:37:25 -0700446 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800447 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700448 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800449 if ctx.Failed() {
450 return
451 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800452 }
453
Colin Crossca860ac2016-01-04 14:34:37 -0800454 if c.linker != nil {
455 outputFile := c.linker.link(ctx, flags, deps, objFiles)
456 if ctx.Failed() {
457 return
458 }
Colin Cross635c3b02016-05-18 15:37:25 -0700459 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700460
Colin Crossb916a382016-07-29 17:28:03 -0700461 if c.installer != nil {
Colin Crossca860ac2016-01-04 14:34:37 -0800462 c.installer.install(ctx, outputFile)
463 if ctx.Failed() {
464 return
465 }
466 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700467 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800468}
469
Colin Crossb98c8b02016-07-29 13:44:28 -0700470func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800471 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700472 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800473 }
Colin Crossca860ac2016-01-04 14:34:37 -0800474 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800475}
476
Colin Crossca860ac2016-01-04 14:34:37 -0800477func (c *Module) begin(ctx BaseModuleContext) {
478 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700479 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700480 }
Colin Crossca860ac2016-01-04 14:34:37 -0800481 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700482 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800483 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700484 if c.stl != nil {
485 c.stl.begin(ctx)
486 }
Colin Cross16b23492016-01-06 14:41:07 -0800487 if c.sanitize != nil {
488 c.sanitize.begin(ctx)
489 }
Colin Crossca860ac2016-01-04 14:34:37 -0800490 for _, feature := range c.features {
491 feature.begin(ctx)
492 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700493 if ctx.sdk() {
494 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
495 if err != nil {
496 ctx.PropertyErrorf("sdk_version", err.Error())
497 }
498 c.Properties.Sdk_version = strconv.Itoa(version)
499 }
Colin Crossca860ac2016-01-04 14:34:37 -0800500}
501
Colin Crossc99deeb2016-04-11 15:06:20 -0700502func (c *Module) deps(ctx BaseModuleContext) Deps {
503 deps := Deps{}
504
505 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700506 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700507 }
508 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700509 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700510 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700511 if c.stl != nil {
512 deps = c.stl.deps(ctx, deps)
513 }
Colin Cross16b23492016-01-06 14:41:07 -0800514 if c.sanitize != nil {
515 deps = c.sanitize.deps(ctx, deps)
516 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700517 for _, feature := range c.features {
518 deps = feature.deps(ctx, deps)
519 }
520
521 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
522 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
523 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
524 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
525 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
526
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700527 for _, lib := range deps.ReexportSharedLibHeaders {
528 if !inList(lib, deps.SharedLibs) {
529 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
530 }
531 }
532
533 for _, lib := range deps.ReexportStaticLibHeaders {
534 if !inList(lib, deps.StaticLibs) {
535 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
536 }
537 }
538
Colin Crossc99deeb2016-04-11 15:06:20 -0700539 return deps
540}
541
Dan Albert7e9d2952016-08-04 13:02:36 -0700542func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800543 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700544 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800545 moduleContextImpl: moduleContextImpl{
546 mod: c,
547 },
548 }
549 ctx.ctx = ctx
550
Colin Crossca860ac2016-01-04 14:34:37 -0800551 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700552}
553
554func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
555 ctx := &baseModuleContext{
556 BaseContext: actx,
557 moduleContextImpl: moduleContextImpl{
558 mod: c,
559 },
560 }
561 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800562
Colin Crossc99deeb2016-04-11 15:06:20 -0700563 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800564
Colin Crossb5bc4b42016-07-11 16:11:59 -0700565 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
566 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700567
Dan Albert914449f2016-06-17 16:45:24 -0700568 variantNdkLibs := []string{}
569 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700570 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700571 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700572
Dan Albert914449f2016-06-17 16:45:24 -0700573 // Rewrites the names of shared libraries into the names of the NDK
574 // libraries where appropriate. This returns two slices.
575 //
576 // The first is a list of non-variant shared libraries (either rewritten
577 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
578 // because they are not NDK libraries).
579 //
580 // The second is a list of ndk_library modules. These need to be
581 // separated because they are a variation dependency and must be added
582 // in a different manner.
583 rewriteNdkLibs := func(list []string) ([]string, []string) {
584 variantLibs := []string{}
585 nonvariantLibs := []string{}
586 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700587 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700588 if !inList(entry, ndkMigratedLibs) {
589 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
590 } else {
591 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
592 }
593 } else {
594 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700595 }
596 }
Dan Albert914449f2016-06-17 16:45:24 -0700597 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700598 }
599
Dan Albert914449f2016-06-17 16:45:24 -0700600 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
601 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700602 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700603
604 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
605 deps.WholeStaticLibs...)
606
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700607 for _, lib := range deps.StaticLibs {
608 depTag := staticDepTag
609 if inList(lib, deps.ReexportStaticLibHeaders) {
610 depTag = staticExportDepTag
611 }
Colin Cross15a0d462016-07-14 14:49:58 -0700612 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700613 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700614
615 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
616 deps.LateStaticLibs...)
617
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700618 for _, lib := range deps.SharedLibs {
619 depTag := sharedDepTag
620 if inList(lib, deps.ReexportSharedLibHeaders) {
621 depTag = sharedExportDepTag
622 }
Colin Cross15a0d462016-07-14 14:49:58 -0700623 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700624 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700625
626 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
627 deps.LateSharedLibs...)
628
Colin Cross68861832016-07-08 10:41:41 -0700629 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
630 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700631
Colin Cross68861832016-07-08 10:41:41 -0700632 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700633
634 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700635 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800636 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700637 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700638 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700639 }
Dan Albert914449f2016-06-17 16:45:24 -0700640
641 version := ctx.sdkVersion()
642 actx.AddVariationDependencies([]blueprint.Variation{
643 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
644 actx.AddVariationDependencies([]blueprint.Variation{
645 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700646}
Colin Cross21b9a242015-03-24 14:15:58 -0700647
Dan Albert7e9d2952016-08-04 13:02:36 -0700648func beginMutator(ctx android.BottomUpMutatorContext) {
649 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
650 c.beginMutator(ctx)
651 }
652}
653
Colin Cross635c3b02016-05-18 15:37:25 -0700654func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700655 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700656 c.depsMutator(ctx)
657 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800658}
659
Colin Crossca860ac2016-01-04 14:34:37 -0800660func (c *Module) clang(ctx BaseModuleContext) bool {
661 clang := Bool(c.Properties.Clang)
662
663 if c.Properties.Clang == nil {
664 if ctx.Host() {
665 clang = true
666 }
667
668 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
669 clang = true
670 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800671 }
Colin Cross28344522015-04-22 13:07:53 -0700672
Colin Crossca860ac2016-01-04 14:34:37 -0800673 if !c.toolchain(ctx).ClangSupported() {
674 clang = false
675 }
676
677 return clang
678}
679
Colin Crossc99deeb2016-04-11 15:06:20 -0700680// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700681func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800682 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800683
Dan Willemsena96ff642016-06-07 12:34:45 -0700684 // Whether a module can link to another module, taking into
685 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700686 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700687 if from.Target().Os != android.Android {
688 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700689 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700690 }
691 if from.Properties.Sdk_version == "" {
692 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700693 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700694 }
Colin Crossb916a382016-07-29 17:28:03 -0700695 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700696 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700697 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700698 }
699 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
700 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700701 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700702 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700703 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
704 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700705 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700706 }
Colin Crossb916a382016-07-29 17:28:03 -0700707 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700708 // These aren't real libraries, but are the stub shared libraries that are included in
709 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700710 return
Dan Albert914449f2016-06-17 16:45:24 -0700711 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700712 if to.Properties.Sdk_version == "" {
713 // NDK code linking to platform code is never okay.
714 ctx.ModuleErrorf("depends on non-NDK-built library %q",
715 ctx.OtherModuleName(to))
716 }
717
718 // All this point we know we have two NDK libraries, but we need to
719 // check that we're not linking against anything built against a higher
720 // API level, as it is only valid to link against older or equivalent
721 // APIs.
722
723 if from.Properties.Sdk_version == "current" {
724 // Current can link against anything.
725 return
726 } else if to.Properties.Sdk_version == "current" {
727 // Current can't be linked against by anything else.
728 ctx.ModuleErrorf("links %q built against newer API version %q",
729 ctx.OtherModuleName(to), "current")
730 }
731
732 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
733 if err != nil {
734 ctx.PropertyErrorf("sdk_version",
735 "Invalid sdk_version value (must be int): %q",
736 from.Properties.Sdk_version)
737 }
738 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
739 if err != nil {
740 ctx.PropertyErrorf("sdk_version",
741 "Invalid sdk_version value (must be int): %q",
742 to.Properties.Sdk_version)
743 }
744
745 if toApi > fromApi {
746 ctx.ModuleErrorf("links %q built against newer API version %q",
747 ctx.OtherModuleName(to), to.Properties.Sdk_version)
748 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700749 }
750
Colin Crossc99deeb2016-04-11 15:06:20 -0700751 ctx.VisitDirectDeps(func(m blueprint.Module) {
752 name := ctx.OtherModuleName(m)
753 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800754
Colin Cross635c3b02016-05-18 15:37:25 -0700755 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700756 if a == nil {
757 ctx.ModuleErrorf("module %q not an android module", name)
758 return
Colin Crossca860ac2016-01-04 14:34:37 -0800759 }
Colin Crossca860ac2016-01-04 14:34:37 -0800760
Dan Willemsena96ff642016-06-07 12:34:45 -0700761 cc, _ := m.(*Module)
762 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700763 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700764 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700765 case genSourceDepTag:
766 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
767 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
768 genRule.GeneratedSourceFiles()...)
769 } else {
770 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
771 }
772 case genHeaderDepTag:
773 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
774 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
775 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700776 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700777 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700778 } else {
779 ctx.ModuleErrorf("module %q is not a genrule", name)
780 }
781 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700782 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800783 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700784 return
785 }
786
787 if !a.Enabled() {
788 ctx.ModuleErrorf("depends on disabled module %q", name)
789 return
790 }
791
Colin Crossa1ad8d12016-06-01 17:09:44 -0700792 if a.Target().Os != ctx.Os() {
793 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
794 return
795 }
796
797 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
798 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700799 return
800 }
801
Dan Willemsena96ff642016-06-07 12:34:45 -0700802 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700803 ctx.ModuleErrorf("module %q missing output file", name)
804 return
805 }
806
807 if tag == reuseObjTag {
808 depPaths.ObjFiles = append(depPaths.ObjFiles,
Colin Crossb916a382016-07-29 17:28:03 -0700809 cc.compiler.(libraryInterface).reuseObjs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700810 return
811 }
812
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700813 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700814 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700815 flags := i.exportedFlags()
816 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700817
818 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700819 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700820 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700821 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700822
Dan Albert9e10cd42016-08-03 14:12:14 -0700823 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700824 }
825
Colin Cross635c3b02016-05-18 15:37:25 -0700826 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700827
828 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700829 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700830 depPtr = &depPaths.SharedLibs
Dan Albert914449f2016-06-17 16:45:24 -0700831 case lateSharedDepTag, ndkLateStubDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700832 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700833 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700834 depPtr = &depPaths.StaticLibs
835 case lateStaticDepTag:
836 depPtr = &depPaths.LateStaticLibs
837 case wholeStaticDepTag:
838 depPtr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700839 staticLib, ok := cc.linker.(libraryInterface)
840 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700841 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700842 return
843 }
844
845 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
846 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
847 for i := range missingDeps {
848 missingDeps[i] += postfix
849 }
850 ctx.AddMissingDependencies(missingDeps)
851 }
852 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700853 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700854 case objDepTag:
855 depPtr = &depPaths.ObjFiles
856 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700857 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700858 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700859 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700860 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700861 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -0700862 }
863
864 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -0700865 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800866 }
867 })
868
869 return depPaths
870}
871
872func (c *Module) InstallInData() bool {
873 if c.installer == nil {
874 return false
875 }
Colin Cross94610402016-08-29 13:41:32 -0700876 if c.sanitize != nil && c.sanitize.inData() {
877 return true
878 }
Colin Crossca860ac2016-01-04 14:34:37 -0800879 return c.installer.inData()
880}
881
Colin Cross2ba19d92015-05-07 15:44:20 -0700882//
Colin Crosscfad1192015-11-02 16:43:11 -0800883// Defaults
884//
Colin Crossca860ac2016-01-04 14:34:37 -0800885type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700886 android.ModuleBase
887 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800888}
889
Colin Cross635c3b02016-05-18 15:37:25 -0700890func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800891}
892
Colin Crossca860ac2016-01-04 14:34:37 -0800893func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700894 return DefaultsFactory()
895}
896
897func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800898 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800899
Colin Crosse1d764e2016-08-18 14:18:32 -0700900 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800901 &BaseProperties{},
902 &BaseCompilerProperties{},
903 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700904 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700905 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800906 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700907 &TestProperties{},
908 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800909 &UnusedProperties{},
910 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800911 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700912 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700913 &InstallerProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700914 )
Colin Crosscfad1192015-11-02 16:43:11 -0800915
Colin Crosse1d764e2016-08-18 14:18:32 -0700916 _, props = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
917 android.MultilibDefault, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800918
Colin Crosse1d764e2016-08-18 14:18:32 -0700919 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800920}
921
Colin Cross74d1ec02015-04-28 13:30:13 -0700922// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
923// modifies the slice contents in place, and returns a subslice of the original slice
924func lastUniqueElements(list []string) []string {
925 totalSkip := 0
926 for i := len(list) - 1; i >= totalSkip; i-- {
927 skip := 0
928 for j := i - 1; j >= totalSkip; j-- {
929 if list[i] == list[j] {
930 skip++
931 } else {
932 list[j+skip] = list[j]
933 }
934 }
935 totalSkip += skip
936 }
937 return list[totalSkip:]
938}
Colin Cross06a931b2015-10-28 17:23:31 -0700939
940var Bool = proptools.Bool