blob: b51e13c5a862b3b00d67b03357adef222997b365 [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 Cross1e676be2016-10-12 14:38:15 -070037 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
38 ctx.BottomUp("link", linkageMutator).Parallel()
39 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
40 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
41 ctx.BottomUp("begin", beginMutator).Parallel()
42 })
Colin Cross16b23492016-01-06 14:41:07 -080043
Colin Cross1e676be2016-10-12 14:38:15 -070044 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
45 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
46 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080047
Colin Cross1e676be2016-10-12 14:38:15 -070048 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
49 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
50 })
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
Colin Cross1e676be2016-10-12 14:38:15 -0700537func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
538 if !c.Enabled() {
539 return
540 }
541
Dan Albert7e9d2952016-08-04 13:02:36 -0700542 ctx := &baseModuleContext{
543 BaseContext: actx,
544 moduleContextImpl: moduleContextImpl{
545 mod: c,
546 },
547 }
548 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800549
Colin Crossc99deeb2016-04-11 15:06:20 -0700550 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800551
Colin Crossb5bc4b42016-07-11 16:11:59 -0700552 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
553 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700554
Dan Albert914449f2016-06-17 16:45:24 -0700555 variantNdkLibs := []string{}
556 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700557 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700558 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700559
Dan Albert914449f2016-06-17 16:45:24 -0700560 // Rewrites the names of shared libraries into the names of the NDK
561 // libraries where appropriate. This returns two slices.
562 //
563 // The first is a list of non-variant shared libraries (either rewritten
564 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
565 // because they are not NDK libraries).
566 //
567 // The second is a list of ndk_library modules. These need to be
568 // separated because they are a variation dependency and must be added
569 // in a different manner.
570 rewriteNdkLibs := func(list []string) ([]string, []string) {
571 variantLibs := []string{}
572 nonvariantLibs := []string{}
573 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700574 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700575 if !inList(entry, ndkMigratedLibs) {
576 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
577 } else {
578 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
579 }
580 } else {
581 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700582 }
583 }
Dan Albert914449f2016-06-17 16:45:24 -0700584 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700585 }
586
Dan Albert914449f2016-06-17 16:45:24 -0700587 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
588 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700589 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700590
591 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
592 deps.WholeStaticLibs...)
593
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700594 for _, lib := range deps.StaticLibs {
595 depTag := staticDepTag
596 if inList(lib, deps.ReexportStaticLibHeaders) {
597 depTag = staticExportDepTag
598 }
Colin Cross15a0d462016-07-14 14:49:58 -0700599 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700600 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700601
602 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
603 deps.LateStaticLibs...)
604
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700605 for _, lib := range deps.SharedLibs {
606 depTag := sharedDepTag
607 if inList(lib, deps.ReexportSharedLibHeaders) {
608 depTag = sharedExportDepTag
609 }
Colin Cross15a0d462016-07-14 14:49:58 -0700610 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700611 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700612
613 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
614 deps.LateSharedLibs...)
615
Colin Cross68861832016-07-08 10:41:41 -0700616 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700617
618 for _, gen := range deps.GeneratedHeaders {
619 depTag := genHeaderDepTag
620 if inList(gen, deps.ReexportGeneratedHeaders) {
621 depTag = genHeaderExportDepTag
622 }
623 actx.AddDependency(c, depTag, gen)
624 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700625
Colin Cross68861832016-07-08 10:41:41 -0700626 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700627
628 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700629 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800630 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700631 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700632 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700633 }
Dan Albert914449f2016-06-17 16:45:24 -0700634
635 version := ctx.sdkVersion()
636 actx.AddVariationDependencies([]blueprint.Variation{
637 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
638 actx.AddVariationDependencies([]blueprint.Variation{
639 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700640}
Colin Cross21b9a242015-03-24 14:15:58 -0700641
Dan Albert7e9d2952016-08-04 13:02:36 -0700642func beginMutator(ctx android.BottomUpMutatorContext) {
643 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
644 c.beginMutator(ctx)
645 }
646}
647
Colin Crossca860ac2016-01-04 14:34:37 -0800648func (c *Module) clang(ctx BaseModuleContext) bool {
649 clang := Bool(c.Properties.Clang)
650
651 if c.Properties.Clang == nil {
652 if ctx.Host() {
653 clang = true
654 }
655
656 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
657 clang = true
658 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800659 }
Colin Cross28344522015-04-22 13:07:53 -0700660
Colin Crossca860ac2016-01-04 14:34:37 -0800661 if !c.toolchain(ctx).ClangSupported() {
662 clang = false
663 }
664
665 return clang
666}
667
Colin Crossc99deeb2016-04-11 15:06:20 -0700668// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700669func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800670 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800671
Dan Willemsena96ff642016-06-07 12:34:45 -0700672 // Whether a module can link to another module, taking into
673 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700674 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700675 if from.Target().Os != android.Android {
676 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700677 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700678 }
679 if from.Properties.Sdk_version == "" {
680 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700681 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700682 }
Colin Crossb916a382016-07-29 17:28:03 -0700683 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700684 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700685 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700686 }
687 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
688 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700689 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700690 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700691 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
692 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700693 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700694 }
Colin Crossb916a382016-07-29 17:28:03 -0700695 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700696 // These aren't real libraries, but are the stub shared libraries that are included in
697 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700698 return
Dan Albert914449f2016-06-17 16:45:24 -0700699 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700700 if to.Properties.Sdk_version == "" {
701 // NDK code linking to platform code is never okay.
702 ctx.ModuleErrorf("depends on non-NDK-built library %q",
703 ctx.OtherModuleName(to))
704 }
705
706 // All this point we know we have two NDK libraries, but we need to
707 // check that we're not linking against anything built against a higher
708 // API level, as it is only valid to link against older or equivalent
709 // APIs.
710
711 if from.Properties.Sdk_version == "current" {
712 // Current can link against anything.
713 return
714 } else if to.Properties.Sdk_version == "current" {
715 // Current can't be linked against by anything else.
716 ctx.ModuleErrorf("links %q built against newer API version %q",
717 ctx.OtherModuleName(to), "current")
718 }
719
720 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
721 if err != nil {
722 ctx.PropertyErrorf("sdk_version",
723 "Invalid sdk_version value (must be int): %q",
724 from.Properties.Sdk_version)
725 }
726 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
727 if err != nil {
728 ctx.PropertyErrorf("sdk_version",
729 "Invalid sdk_version value (must be int): %q",
730 to.Properties.Sdk_version)
731 }
732
733 if toApi > fromApi {
734 ctx.ModuleErrorf("links %q built against newer API version %q",
735 ctx.OtherModuleName(to), to.Properties.Sdk_version)
736 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700737 }
738
Colin Crossc99deeb2016-04-11 15:06:20 -0700739 ctx.VisitDirectDeps(func(m blueprint.Module) {
740 name := ctx.OtherModuleName(m)
741 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800742
Colin Cross635c3b02016-05-18 15:37:25 -0700743 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700744 if a == nil {
745 ctx.ModuleErrorf("module %q not an android module", name)
746 return
Colin Crossca860ac2016-01-04 14:34:37 -0800747 }
Colin Crossca860ac2016-01-04 14:34:37 -0800748
Dan Willemsena96ff642016-06-07 12:34:45 -0700749 cc, _ := m.(*Module)
750 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700751 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700752 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700753 case genSourceDepTag:
754 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
755 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
756 genRule.GeneratedSourceFiles()...)
757 } else {
758 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
759 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700760 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700761 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
762 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
763 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700764 flags := includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()})
765 depPaths.Flags = append(depPaths.Flags, flags)
766 if tag == genHeaderExportDepTag {
767 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700768 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
769 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700770 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700771 } else {
772 ctx.ModuleErrorf("module %q is not a genrule", name)
773 }
774 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700775 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800776 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700777 return
778 }
779
780 if !a.Enabled() {
781 ctx.ModuleErrorf("depends on disabled module %q", name)
782 return
783 }
784
Colin Crossa1ad8d12016-06-01 17:09:44 -0700785 if a.Target().Os != ctx.Os() {
786 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
787 return
788 }
789
790 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
791 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700792 return
793 }
794
Dan Willemsena96ff642016-06-07 12:34:45 -0700795 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700796 ctx.ModuleErrorf("module %q missing output file", name)
797 return
798 }
799
800 if tag == reuseObjTag {
801 depPaths.ObjFiles = append(depPaths.ObjFiles,
Colin Crossb916a382016-07-29 17:28:03 -0700802 cc.compiler.(libraryInterface).reuseObjs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700803 return
804 }
805
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700806 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700807 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700808 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700809 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700810 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700811 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700812
813 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700814 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700815 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700816 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700817 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700818
Dan Albert9e10cd42016-08-03 14:12:14 -0700819 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700820 }
821
Colin Cross26c34ed2016-09-30 17:10:16 -0700822 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700823 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700824
Colin Cross26c34ed2016-09-30 17:10:16 -0700825 linkFile := cc.outputFile
826 depFile := android.OptionalPath{}
827
Colin Crossc99deeb2016-04-11 15:06:20 -0700828 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700829 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700830 ptr = &depPaths.SharedLibs
831 depPtr = &depPaths.SharedLibsDeps
832 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700833 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700834 ptr = &depPaths.LateSharedLibs
835 depPtr = &depPaths.LateSharedLibsDeps
836 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700837 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700838 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700839 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700840 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700841 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700842 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700843 staticLib, ok := cc.linker.(libraryInterface)
844 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700845 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700846 return
847 }
848
849 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
850 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
851 for i := range missingDeps {
852 missingDeps[i] += postfix
853 }
854 ctx.AddMissingDependencies(missingDeps)
855 }
856 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700857 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700858 case objDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700859 ptr = &depPaths.ObjFiles
Colin Crossc99deeb2016-04-11 15:06:20 -0700860 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700861 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700862 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700863 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700864 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700865 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -0700866 }
867
Colin Cross26c34ed2016-09-30 17:10:16 -0700868 if ptr != nil {
869 *ptr = append(*ptr, linkFile.Path())
870 }
871
Colin Crossc99deeb2016-04-11 15:06:20 -0700872 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700873 dep := depFile
874 if !dep.Valid() {
875 dep = linkFile
876 }
877 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800878 }
879 })
880
881 return depPaths
882}
883
884func (c *Module) InstallInData() bool {
885 if c.installer == nil {
886 return false
887 }
Colin Cross94610402016-08-29 13:41:32 -0700888 if c.sanitize != nil && c.sanitize.inData() {
889 return true
890 }
Colin Crossca860ac2016-01-04 14:34:37 -0800891 return c.installer.inData()
892}
893
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700894func (c *Module) HostToolPath() android.OptionalPath {
895 if c.installer == nil {
896 return android.OptionalPath{}
897 }
898 return c.installer.hostToolPath()
899}
900
Colin Cross2ba19d92015-05-07 15:44:20 -0700901//
Colin Crosscfad1192015-11-02 16:43:11 -0800902// Defaults
903//
Colin Crossca860ac2016-01-04 14:34:37 -0800904type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700905 android.ModuleBase
906 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800907}
908
Colin Cross635c3b02016-05-18 15:37:25 -0700909func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800910}
911
Colin Cross1e676be2016-10-12 14:38:15 -0700912func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
913}
914
Colin Crossca860ac2016-01-04 14:34:37 -0800915func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700916 return DefaultsFactory()
917}
918
919func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800920 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800921
Colin Crosse1d764e2016-08-18 14:18:32 -0700922 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800923 &BaseProperties{},
924 &BaseCompilerProperties{},
925 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700926 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700927 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800928 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700929 &TestProperties{},
930 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800931 &UnusedProperties{},
932 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800933 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700934 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700935 &InstallerProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700936 )
Colin Crosscfad1192015-11-02 16:43:11 -0800937
Colin Crosse1d764e2016-08-18 14:18:32 -0700938 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800939}
940
Colin Cross74d1ec02015-04-28 13:30:13 -0700941// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
942// modifies the slice contents in place, and returns a subslice of the original slice
943func lastUniqueElements(list []string) []string {
944 totalSkip := 0
945 for i := len(list) - 1; i >= totalSkip; i-- {
946 skip := 0
947 for j := i - 1; j >= totalSkip; j-- {
948 if list[i] == list[j] {
949 skip++
950 } else {
951 list[j+skip] = list[j]
952 }
953 }
954 totalSkip += skip
955 }
956 return list[totalSkip:]
957}
Colin Cross06a931b2015-10-28 17:23:31 -0700958
959var Bool = proptools.Bool