blob: 791d9ea7e861cda8a6f2ddf008ddbe91a06b041c [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 Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070030 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070035 android.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070036
Colin Cross463a90e2015-06-17 14:20:06 -070037 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
38 // the Go initialization order because this package depends on common, so common's init
39 // functions will run first.
Colin Crosse8a67a72016-08-07 21:17:54 -070040 android.RegisterBottomUpMutator("link", linkageMutator).Parallel()
41 android.RegisterBottomUpMutator("ndk_api", ndkApiMutator).Parallel()
42 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator).Parallel()
43 android.RegisterBottomUpMutator("begin", beginMutator).Parallel()
44 android.RegisterBottomUpMutator("deps", depsMutator).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080045
Colin Cross635c3b02016-05-18 15:37:25 -070046 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
Colin Crosse8a67a72016-08-07 21:17:54 -070047 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080048
Colin Cross635c3b02016-05-18 15:37:25 -070049 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
Colin Crosse8a67a72016-08-07 21:17:54 -070050 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan)).Parallel()
Colin Crossb98c8b02016-07-29 13:44:28 -070051
52 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070053}
54
Colin Crossca860ac2016-01-04 14:34:37 -080055type Deps struct {
56 SharedLibs, LateSharedLibs []string
57 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070058
Dan Willemsen490a8dc2016-06-06 18:22:19 -070059 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
60
Colin Cross81413472016-04-11 14:37:39 -070061 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070062
Dan Willemsenb40aab62016-04-20 14:21:14 -070063 GeneratedSources []string
64 GeneratedHeaders []string
65
Dan Willemsenb3454ab2016-09-28 17:34:58 -070066 ReexportGeneratedHeaders []string
67
Colin Cross97ba0732015-03-23 17:50:24 -070068 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070069}
70
Colin Crossca860ac2016-01-04 14:34:37 -080071type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070072 // Paths to .so files
73 SharedLibs, LateSharedLibs android.Paths
74 // Paths to the dependencies to use for .so files (.so.toc files)
75 SharedLibsDeps, LateSharedLibsDeps android.Paths
76 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070077 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078
Colin Cross26c34ed2016-09-30 17:10:16 -070079 // Paths to .o files
Colin Cross635c3b02016-05-18 15:37:25 -070080 ObjFiles android.Paths
81 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082
Colin Cross26c34ed2016-09-30 17:10:16 -070083 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070084 GeneratedSources android.Paths
85 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070086
Dan Willemsen76f08272016-07-09 00:14:08 -070087 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070088 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089
Colin Cross26c34ed2016-09-30 17:10:16 -070090 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070091 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092}
93
Colin Crossca860ac2016-01-04 14:34:37 -080094type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070095 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
96 AsFlags []string // Flags that apply to assembly source files
97 CFlags []string // Flags that apply to C and C++ source files
98 ConlyFlags []string // Flags that apply to C source files
99 CppFlags []string // Flags that apply to C++ source files
100 YaccFlags []string // Flags that apply to Yacc source files
101 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 Crossca860ac2016-01-04 14:34:37 -0800149}
150
151type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700152 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800153 ModuleContextIntf
154}
155
156type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700157 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800158 ModuleContextIntf
159}
160
Colin Crossca860ac2016-01-04 14:34:37 -0800161type feature interface {
162 begin(ctx BaseModuleContext)
163 deps(ctx BaseModuleContext, deps Deps) Deps
164 flags(ctx ModuleContext, flags Flags) Flags
165 props() []interface{}
166}
167
168type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700169 compilerInit(ctx BaseModuleContext)
170 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
171 compilerFlags(ctx ModuleContext, flags Flags) Flags
172 compilerProps() []interface{}
173
Colin Cross76fada02016-07-27 10:31:13 -0700174 appendCflags([]string)
175 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700176 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800177}
178
179type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700180 linkerInit(ctx BaseModuleContext)
181 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
182 linkerFlags(ctx ModuleContext, flags Flags) Flags
183 linkerProps() []interface{}
184
Colin Cross635c3b02016-05-18 15:37:25 -0700185 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700186 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800187}
188
189type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700190 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700191 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800192 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700193 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800194}
195
Colin Crossc99deeb2016-04-11 15:06:20 -0700196type dependencyTag struct {
197 blueprint.BaseDependencyTag
198 name string
199 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700200
201 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700202}
203
204var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700205 sharedDepTag = dependencyTag{name: "shared", library: true}
206 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
207 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
208 staticDepTag = dependencyTag{name: "static", library: true}
209 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
210 lateStaticDepTag = dependencyTag{name: "late static", library: true}
211 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
212 genSourceDepTag = dependencyTag{name: "gen source"}
213 genHeaderDepTag = dependencyTag{name: "gen header"}
214 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
215 objDepTag = dependencyTag{name: "obj"}
216 crtBeginDepTag = dependencyTag{name: "crtbegin"}
217 crtEndDepTag = dependencyTag{name: "crtend"}
218 reuseObjTag = dependencyTag{name: "reuse objects"}
219 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 Crossb4ce0ec2016-09-13 13:41:39 -0700238 features []feature
239 compiler compiler
240 linker linker
241 installer installer
242 stl *stl
243 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800244
245 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700246
Colin Cross635c3b02016-05-18 15:37:25 -0700247 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800248
Colin Crossb98c8b02016-07-29 13:44:28 -0700249 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700250
251 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700252}
253
Colin Crossca860ac2016-01-04 14:34:37 -0800254func (c *Module) Init() (blueprint.Module, []interface{}) {
255 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800256 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700257 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800258 }
259 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700260 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800261 }
262 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700263 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800264 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700265 if c.stl != nil {
266 props = append(props, c.stl.props()...)
267 }
Colin Cross16b23492016-01-06 14:41:07 -0800268 if c.sanitize != nil {
269 props = append(props, c.sanitize.props()...)
270 }
Colin Crossca860ac2016-01-04 14:34:37 -0800271 for _, feature := range c.features {
272 props = append(props, feature.props()...)
273 }
Colin Crossc472d572015-03-17 15:06:21 -0700274
Colin Cross635c3b02016-05-18 15:37:25 -0700275 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700276
Colin Cross635c3b02016-05-18 15:37:25 -0700277 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700278}
279
Colin Crossb916a382016-07-29 17:28:03 -0700280// Returns true for dependency roots (binaries)
281// TODO(ccross): also handle dlopenable libraries
282func (c *Module) isDependencyRoot() bool {
283 if root, ok := c.linker.(interface {
284 isDependencyRoot() bool
285 }); ok {
286 return root.isDependencyRoot()
287 }
288 return false
289}
290
Colin Crossca860ac2016-01-04 14:34:37 -0800291type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700292 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800293 moduleContextImpl
294}
295
296type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700297 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800298 moduleContextImpl
299}
300
301type moduleContextImpl struct {
302 mod *Module
303 ctx BaseModuleContext
304}
305
Colin Crossca860ac2016-01-04 14:34:37 -0800306func (ctx *moduleContextImpl) clang() bool {
307 return ctx.mod.clang(ctx.ctx)
308}
309
Colin Crossb98c8b02016-07-29 13:44:28 -0700310func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800311 return ctx.mod.toolchain(ctx.ctx)
312}
313
314func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700315 if static, ok := ctx.mod.linker.(interface {
316 static() bool
317 }); ok {
318 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800319 }
Colin Crossb916a382016-07-29 17:28:03 -0700320 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800321}
322
323func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700324 if static, ok := ctx.mod.linker.(interface {
325 staticBinary() bool
326 }); ok {
327 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800328 }
Colin Crossb916a382016-07-29 17:28:03 -0700329 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800330}
331
332func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
333 return Bool(ctx.mod.Properties.No_default_compiler_flags)
334}
335
336func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700337 if ctx.ctx.Device() {
338 return ctx.mod.Properties.Sdk_version != ""
339 }
340 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800341}
342
343func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700344 if ctx.ctx.Device() {
345 return ctx.mod.Properties.Sdk_version
346 }
347 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800348}
349
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700350func (ctx *moduleContextImpl) selectedStl() string {
351 if stl := ctx.mod.stl; stl != nil {
352 return stl.Properties.SelectedStl
353 }
354 return ""
355}
356
Colin Cross635c3b02016-05-18 15:37:25 -0700357func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800358 return &Module{
359 hod: hod,
360 multilib: multilib,
361 }
362}
363
Colin Cross635c3b02016-05-18 15:37:25 -0700364func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800365 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700366 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800367 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800368 return module
369}
370
Colin Cross635c3b02016-05-18 15:37:25 -0700371func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800372 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700373 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800374 moduleContextImpl: moduleContextImpl{
375 mod: c,
376 },
377 }
378 ctx.ctx = ctx
379
380 flags := Flags{
381 Toolchain: c.toolchain(ctx),
382 Clang: c.clang(ctx),
383 }
Colin Crossca860ac2016-01-04 14:34:37 -0800384 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700385 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800386 }
387 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700388 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800389 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700390 if c.stl != nil {
391 flags = c.stl.flags(ctx, flags)
392 }
Colin Cross16b23492016-01-06 14:41:07 -0800393 if c.sanitize != nil {
394 flags = c.sanitize.flags(ctx, flags)
395 }
Colin Crossca860ac2016-01-04 14:34:37 -0800396 for _, feature := range c.features {
397 flags = feature.flags(ctx, flags)
398 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800399 if ctx.Failed() {
400 return
401 }
402
Colin Crossb98c8b02016-07-29 13:44:28 -0700403 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
404 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
405 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800406
Colin Crossca860ac2016-01-04 14:34:37 -0800407 // Optimization to reduce size of build.ninja
408 // Replace the long list of flags for each file with a module-local variable
409 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
410 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
411 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
412 flags.CFlags = []string{"$cflags"}
413 flags.CppFlags = []string{"$cppflags"}
414 flags.AsFlags = []string{"$asflags"}
415
Colin Crossc99deeb2016-04-11 15:06:20 -0700416 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800417 if ctx.Failed() {
418 return
419 }
420
Dan Willemsen76f08272016-07-09 00:14:08 -0700421 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700422
Colin Cross635c3b02016-05-18 15:37:25 -0700423 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800424 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700425 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800426 if ctx.Failed() {
427 return
428 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800429 }
430
Colin Crossca860ac2016-01-04 14:34:37 -0800431 if c.linker != nil {
432 outputFile := c.linker.link(ctx, flags, deps, objFiles)
433 if ctx.Failed() {
434 return
435 }
Colin Cross635c3b02016-05-18 15:37:25 -0700436 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700437
Colin Crossb0f28952016-09-19 16:46:53 -0700438 if c.installer != nil && !c.Properties.PreventInstall {
Colin Crossca860ac2016-01-04 14:34:37 -0800439 c.installer.install(ctx, outputFile)
440 if ctx.Failed() {
441 return
442 }
443 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700444 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800445}
446
Colin Crossb98c8b02016-07-29 13:44:28 -0700447func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800448 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700449 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800450 }
Colin Crossca860ac2016-01-04 14:34:37 -0800451 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800452}
453
Colin Crossca860ac2016-01-04 14:34:37 -0800454func (c *Module) begin(ctx BaseModuleContext) {
455 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700456 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700457 }
Colin Crossca860ac2016-01-04 14:34:37 -0800458 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700459 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800460 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700461 if c.stl != nil {
462 c.stl.begin(ctx)
463 }
Colin Cross16b23492016-01-06 14:41:07 -0800464 if c.sanitize != nil {
465 c.sanitize.begin(ctx)
466 }
Colin Crossca860ac2016-01-04 14:34:37 -0800467 for _, feature := range c.features {
468 feature.begin(ctx)
469 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700470 if ctx.sdk() {
471 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
472 if err != nil {
473 ctx.PropertyErrorf("sdk_version", err.Error())
474 }
475 c.Properties.Sdk_version = strconv.Itoa(version)
476 }
Colin Crossca860ac2016-01-04 14:34:37 -0800477}
478
Colin Crossc99deeb2016-04-11 15:06:20 -0700479func (c *Module) deps(ctx BaseModuleContext) Deps {
480 deps := Deps{}
481
482 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700483 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700484 }
485 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700486 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700487 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700488 if c.stl != nil {
489 deps = c.stl.deps(ctx, deps)
490 }
Colin Cross16b23492016-01-06 14:41:07 -0800491 if c.sanitize != nil {
492 deps = c.sanitize.deps(ctx, deps)
493 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700494 for _, feature := range c.features {
495 deps = feature.deps(ctx, deps)
496 }
497
498 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
499 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
500 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
501 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
502 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
503
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700504 for _, lib := range deps.ReexportSharedLibHeaders {
505 if !inList(lib, deps.SharedLibs) {
506 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
507 }
508 }
509
510 for _, lib := range deps.ReexportStaticLibHeaders {
511 if !inList(lib, deps.StaticLibs) {
512 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
513 }
514 }
515
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700516 for _, gen := range deps.ReexportGeneratedHeaders {
517 if !inList(gen, deps.GeneratedHeaders) {
518 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
519 }
520 }
521
Colin Crossc99deeb2016-04-11 15:06:20 -0700522 return deps
523}
524
Dan Albert7e9d2952016-08-04 13:02:36 -0700525func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800526 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700527 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800528 moduleContextImpl: moduleContextImpl{
529 mod: c,
530 },
531 }
532 ctx.ctx = ctx
533
Colin Crossca860ac2016-01-04 14:34:37 -0800534 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700535}
536
537func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
538 ctx := &baseModuleContext{
539 BaseContext: actx,
540 moduleContextImpl: moduleContextImpl{
541 mod: c,
542 },
543 }
544 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800545
Colin Crossc99deeb2016-04-11 15:06:20 -0700546 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800547
Colin Crossb5bc4b42016-07-11 16:11:59 -0700548 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
549 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700550
Dan Albert914449f2016-06-17 16:45:24 -0700551 variantNdkLibs := []string{}
552 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700553 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700554 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700555
Dan Albert914449f2016-06-17 16:45:24 -0700556 // Rewrites the names of shared libraries into the names of the NDK
557 // libraries where appropriate. This returns two slices.
558 //
559 // The first is a list of non-variant shared libraries (either rewritten
560 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
561 // because they are not NDK libraries).
562 //
563 // The second is a list of ndk_library modules. These need to be
564 // separated because they are a variation dependency and must be added
565 // in a different manner.
566 rewriteNdkLibs := func(list []string) ([]string, []string) {
567 variantLibs := []string{}
568 nonvariantLibs := []string{}
569 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700570 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700571 if !inList(entry, ndkMigratedLibs) {
572 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
573 } else {
574 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
575 }
576 } else {
577 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700578 }
579 }
Dan Albert914449f2016-06-17 16:45:24 -0700580 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700581 }
582
Dan Albert914449f2016-06-17 16:45:24 -0700583 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
584 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700585 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700586
587 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
588 deps.WholeStaticLibs...)
589
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700590 for _, lib := range deps.StaticLibs {
591 depTag := staticDepTag
592 if inList(lib, deps.ReexportStaticLibHeaders) {
593 depTag = staticExportDepTag
594 }
Colin Cross15a0d462016-07-14 14:49:58 -0700595 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700596 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700597
598 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
599 deps.LateStaticLibs...)
600
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700601 for _, lib := range deps.SharedLibs {
602 depTag := sharedDepTag
603 if inList(lib, deps.ReexportSharedLibHeaders) {
604 depTag = sharedExportDepTag
605 }
Colin Cross15a0d462016-07-14 14:49:58 -0700606 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700607 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700608
609 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
610 deps.LateSharedLibs...)
611
Colin Cross68861832016-07-08 10:41:41 -0700612 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700613
614 for _, gen := range deps.GeneratedHeaders {
615 depTag := genHeaderDepTag
616 if inList(gen, deps.ReexportGeneratedHeaders) {
617 depTag = genHeaderExportDepTag
618 }
619 actx.AddDependency(c, depTag, gen)
620 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700621
Colin Cross68861832016-07-08 10:41:41 -0700622 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700623
624 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700625 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800626 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700627 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700628 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700629 }
Dan Albert914449f2016-06-17 16:45:24 -0700630
631 version := ctx.sdkVersion()
632 actx.AddVariationDependencies([]blueprint.Variation{
633 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
634 actx.AddVariationDependencies([]blueprint.Variation{
635 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700636}
Colin Cross21b9a242015-03-24 14:15:58 -0700637
Dan Albert7e9d2952016-08-04 13:02:36 -0700638func beginMutator(ctx android.BottomUpMutatorContext) {
639 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
640 c.beginMutator(ctx)
641 }
642}
643
Colin Cross635c3b02016-05-18 15:37:25 -0700644func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700645 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700646 c.depsMutator(ctx)
647 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800648}
649
Colin Crossca860ac2016-01-04 14:34:37 -0800650func (c *Module) clang(ctx BaseModuleContext) bool {
651 clang := Bool(c.Properties.Clang)
652
653 if c.Properties.Clang == nil {
654 if ctx.Host() {
655 clang = true
656 }
657
658 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
659 clang = true
660 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800661 }
Colin Cross28344522015-04-22 13:07:53 -0700662
Colin Crossca860ac2016-01-04 14:34:37 -0800663 if !c.toolchain(ctx).ClangSupported() {
664 clang = false
665 }
666
667 return clang
668}
669
Colin Crossc99deeb2016-04-11 15:06:20 -0700670// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700671func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800672 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800673
Dan Willemsena96ff642016-06-07 12:34:45 -0700674 // Whether a module can link to another module, taking into
675 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700676 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700677 if from.Target().Os != android.Android {
678 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700679 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700680 }
681 if from.Properties.Sdk_version == "" {
682 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700683 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700684 }
Colin Crossb916a382016-07-29 17:28:03 -0700685 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700686 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700687 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700688 }
689 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
690 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700691 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700692 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700693 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
694 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700695 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700696 }
Colin Crossb916a382016-07-29 17:28:03 -0700697 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700698 // These aren't real libraries, but are the stub shared libraries that are included in
699 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700700 return
Dan Albert914449f2016-06-17 16:45:24 -0700701 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700702 if to.Properties.Sdk_version == "" {
703 // NDK code linking to platform code is never okay.
704 ctx.ModuleErrorf("depends on non-NDK-built library %q",
705 ctx.OtherModuleName(to))
706 }
707
708 // All this point we know we have two NDK libraries, but we need to
709 // check that we're not linking against anything built against a higher
710 // API level, as it is only valid to link against older or equivalent
711 // APIs.
712
713 if from.Properties.Sdk_version == "current" {
714 // Current can link against anything.
715 return
716 } else if to.Properties.Sdk_version == "current" {
717 // Current can't be linked against by anything else.
718 ctx.ModuleErrorf("links %q built against newer API version %q",
719 ctx.OtherModuleName(to), "current")
720 }
721
722 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
723 if err != nil {
724 ctx.PropertyErrorf("sdk_version",
725 "Invalid sdk_version value (must be int): %q",
726 from.Properties.Sdk_version)
727 }
728 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
729 if err != nil {
730 ctx.PropertyErrorf("sdk_version",
731 "Invalid sdk_version value (must be int): %q",
732 to.Properties.Sdk_version)
733 }
734
735 if toApi > fromApi {
736 ctx.ModuleErrorf("links %q built against newer API version %q",
737 ctx.OtherModuleName(to), to.Properties.Sdk_version)
738 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700739 }
740
Colin Crossc99deeb2016-04-11 15:06:20 -0700741 ctx.VisitDirectDeps(func(m blueprint.Module) {
742 name := ctx.OtherModuleName(m)
743 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800744
Colin Cross635c3b02016-05-18 15:37:25 -0700745 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700746 if a == nil {
747 ctx.ModuleErrorf("module %q not an android module", name)
748 return
Colin Crossca860ac2016-01-04 14:34:37 -0800749 }
Colin Crossca860ac2016-01-04 14:34:37 -0800750
Dan Willemsena96ff642016-06-07 12:34:45 -0700751 cc, _ := m.(*Module)
752 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700753 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700754 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700755 case genSourceDepTag:
756 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
757 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
758 genRule.GeneratedSourceFiles()...)
759 } else {
760 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
761 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700762 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700763 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
764 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
765 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700766 flags := includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()})
767 depPaths.Flags = append(depPaths.Flags, flags)
768 if tag == genHeaderExportDepTag {
769 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700770 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
771 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700772 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700773 } else {
774 ctx.ModuleErrorf("module %q is not a genrule", name)
775 }
776 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700777 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800778 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700779 return
780 }
781
782 if !a.Enabled() {
783 ctx.ModuleErrorf("depends on disabled module %q", name)
784 return
785 }
786
Colin Crossa1ad8d12016-06-01 17:09:44 -0700787 if a.Target().Os != ctx.Os() {
788 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
789 return
790 }
791
792 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
793 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700794 return
795 }
796
Dan Willemsena96ff642016-06-07 12:34:45 -0700797 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700798 ctx.ModuleErrorf("module %q missing output file", name)
799 return
800 }
801
802 if tag == reuseObjTag {
803 depPaths.ObjFiles = append(depPaths.ObjFiles,
Colin Crossb916a382016-07-29 17:28:03 -0700804 cc.compiler.(libraryInterface).reuseObjs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700805 return
806 }
807
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700808 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700809 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700810 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700811 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700812 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700813 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700814
815 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700816 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700817 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700818 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700819 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700820
Dan Albert9e10cd42016-08-03 14:12:14 -0700821 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700822 }
823
Colin Cross26c34ed2016-09-30 17:10:16 -0700824 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700825 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700826
Colin Cross26c34ed2016-09-30 17:10:16 -0700827 linkFile := cc.outputFile
828 depFile := android.OptionalPath{}
829
Colin Crossc99deeb2016-04-11 15:06:20 -0700830 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700831 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700832 ptr = &depPaths.SharedLibs
833 depPtr = &depPaths.SharedLibsDeps
834 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700835 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700836 ptr = &depPaths.LateSharedLibs
837 depPtr = &depPaths.LateSharedLibsDeps
838 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700839 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700840 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700841 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700842 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700843 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700844 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700845 staticLib, ok := cc.linker.(libraryInterface)
846 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700847 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700848 return
849 }
850
851 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
852 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
853 for i := range missingDeps {
854 missingDeps[i] += postfix
855 }
856 ctx.AddMissingDependencies(missingDeps)
857 }
858 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700859 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700860 case objDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700861 ptr = &depPaths.ObjFiles
Colin Crossc99deeb2016-04-11 15:06:20 -0700862 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700863 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700864 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700865 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700866 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700867 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -0700868 }
869
Colin Cross26c34ed2016-09-30 17:10:16 -0700870 if ptr != nil {
871 *ptr = append(*ptr, linkFile.Path())
872 }
873
Colin Crossc99deeb2016-04-11 15:06:20 -0700874 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700875 dep := depFile
876 if !dep.Valid() {
877 dep = linkFile
878 }
879 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800880 }
881 })
882
883 return depPaths
884}
885
886func (c *Module) InstallInData() bool {
887 if c.installer == nil {
888 return false
889 }
Colin Cross94610402016-08-29 13:41:32 -0700890 if c.sanitize != nil && c.sanitize.inData() {
891 return true
892 }
Colin Crossca860ac2016-01-04 14:34:37 -0800893 return c.installer.inData()
894}
895
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700896func (c *Module) HostToolPath() android.OptionalPath {
897 if c.installer == nil {
898 return android.OptionalPath{}
899 }
900 return c.installer.hostToolPath()
901}
902
Colin Cross2ba19d92015-05-07 15:44:20 -0700903//
Colin Crosscfad1192015-11-02 16:43:11 -0800904// Defaults
905//
Colin Crossca860ac2016-01-04 14:34:37 -0800906type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700907 android.ModuleBase
908 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800909}
910
Colin Cross635c3b02016-05-18 15:37:25 -0700911func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800912}
913
Colin Crossca860ac2016-01-04 14:34:37 -0800914func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700915 return DefaultsFactory()
916}
917
918func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800919 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800920
Colin Crosse1d764e2016-08-18 14:18:32 -0700921 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800922 &BaseProperties{},
923 &BaseCompilerProperties{},
924 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700925 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700926 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800927 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700928 &TestProperties{},
929 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800930 &UnusedProperties{},
931 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800932 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700933 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700934 &InstallerProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700935 )
Colin Crosscfad1192015-11-02 16:43:11 -0800936
Colin Crosse1d764e2016-08-18 14:18:32 -0700937 _, props = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
938 android.MultilibDefault, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800939
Colin Crosse1d764e2016-08-18 14:18:32 -0700940 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800941}
942
Colin Cross74d1ec02015-04-28 13:30:13 -0700943// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
944// modifies the slice contents in place, and returns a subslice of the original slice
945func lastUniqueElements(list []string) []string {
946 totalSkip := 0
947 for i := len(list) - 1; i >= totalSkip; i-- {
948 skip := 0
949 for j := i - 1; j >= totalSkip; j-- {
950 if list[i] == list[j] {
951 skip++
952 } else {
953 list[j+skip] = list[j]
954 }
955 }
956 totalSkip += skip
957 }
958 return list[totalSkip:]
959}
Colin Cross06a931b2015-10-28 17:23:31 -0700960
961var Bool = proptools.Bool