blob: c5f1c357585d74caae98ec1f05c1a7972a772962 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Dan Albert9e10cd42016-08-03 14:12:14 -070022 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "strings"
24
Colin Cross97ba0732015-03-23 17:50:24 -070025 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070026 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070029 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070030 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070034 android.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070035
Colin Cross1e676be2016-10-12 14:38:15 -070036 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
37 ctx.BottomUp("link", linkageMutator).Parallel()
38 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
39 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
40 ctx.BottomUp("begin", beginMutator).Parallel()
41 })
Colin Cross16b23492016-01-06 14:41:07 -080042
Colin Cross1e676be2016-10-12 14:38:15 -070043 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
45 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080046
Colin Cross1e676be2016-10-12 14:38:15 -070047 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
48 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
Dan Willemsen581341d2017-02-09 16:16:31 -080049
50 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070051 })
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 Cross5950f382016-12-13 12:50:57 -080059 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070060
Colin Cross5950f382016-12-13 12:50:57 -080061 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070062
Colin Cross81413472016-04-11 14:37:39 -070063 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070064
Dan Willemsenb40aab62016-04-20 14:21:14 -070065 GeneratedSources []string
66 GeneratedHeaders []string
67
Dan Willemsenb3454ab2016-09-28 17:34:58 -070068 ReexportGeneratedHeaders []string
69
Colin Cross97ba0732015-03-23 17:50:24 -070070 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070071}
72
Colin Crossca860ac2016-01-04 14:34:37 -080073type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070074 // Paths to .so files
75 SharedLibs, LateSharedLibs android.Paths
76 // Paths to the dependencies to use for .so files (.so.toc files)
77 SharedLibsDeps, LateSharedLibsDeps android.Paths
78 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070079 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080
Colin Cross26c34ed2016-09-30 17:10:16 -070081 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070082 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080083 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070084 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085
Colin Cross26c34ed2016-09-30 17:10:16 -070086 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070087 GeneratedSources android.Paths
88 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070089
Dan Willemsen76f08272016-07-09 00:14:08 -070090 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070091 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092
Colin Cross26c34ed2016-09-30 17:10:16 -070093 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070094 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070095}
96
Colin Crossca860ac2016-01-04 14:34:37 -080097type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070098 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
Vishwath Mohan83d9f712017-03-16 11:01:23 -070099 ArFlags []string // Flags that apply to ar
Colin Cross28344522015-04-22 13:07:53 -0700100 AsFlags []string // Flags that apply to assembly source files
101 CFlags []string // Flags that apply to C and C++ source files
102 ConlyFlags []string // Flags that apply to C source files
103 CppFlags []string // Flags that apply to C++ source files
104 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700105 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700106 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700107 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800108 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700109 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800110 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700111
Colin Crossb98c8b02016-07-29 13:44:28 -0700112 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700113 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700114 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800115 Coverage bool
Colin Crossca860ac2016-01-04 14:34:37 -0800116
117 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800118 DynamicLinker string
119
Colin Cross635c3b02016-05-18 15:37:25 -0700120 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800121
122 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700123}
124
Colin Cross81413472016-04-11 14:37:39 -0700125type ObjectLinkerProperties struct {
126 // names of other cc_object modules to link into this module using partial linking
127 Objs []string `android:"arch_variant"`
128}
129
Colin Crossca860ac2016-01-04 14:34:37 -0800130// Properties used to compile all C or C++ modules
131type BaseProperties struct {
132 // compile module with clang instead of gcc
133 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700134
135 // Minimum sdk version supported when compiling against the ndk
136 Sdk_version string
137
Dan Willemsend2ede872016-11-18 14:54:24 -0800138 // Whether to compile against the VNDK
139 Use_vndk bool
140
Colin Crossca860ac2016-01-04 14:34:37 -0800141 // don't insert default compiler flags into asflags, cflags,
142 // cppflags, conlyflags, ldflags, or include_dirs
143 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700144
145 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700146 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700147 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800148 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800149}
150
Colin Crossca860ac2016-01-04 14:34:37 -0800151type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800152 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800153}
154
Colin Crossca860ac2016-01-04 14:34:37 -0800155type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800156 static() bool
157 staticBinary() bool
158 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700159 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800160 noDefaultCompilerFlags() bool
161 sdk() bool
162 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800163 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700164 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700165 baseModuleName() string
Jiyong Park4c48f722017-01-20 08:57:02 +0900166 isNdk() bool
167 isVndk() bool
168 isSameProcessHal() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800169}
170
171type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700172 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800173 ModuleContextIntf
174}
175
176type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700177 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800178 ModuleContextIntf
179}
180
Colin Cross37047f12016-12-13 17:06:13 -0800181type DepsContext interface {
182 android.BottomUpMutatorContext
183 ModuleContextIntf
184}
185
Colin Crossca860ac2016-01-04 14:34:37 -0800186type feature interface {
187 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800188 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800189 flags(ctx ModuleContext, flags Flags) Flags
190 props() []interface{}
191}
192
193type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700194 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800195 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700196 compilerFlags(ctx ModuleContext, flags Flags) Flags
197 compilerProps() []interface{}
198
Colin Cross76fada02016-07-27 10:31:13 -0700199 appendCflags([]string)
200 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700201 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800202}
203
204type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700205 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800206 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700207 linkerFlags(ctx ModuleContext, flags Flags) Flags
208 linkerProps() []interface{}
209
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700210 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700211 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800212}
213
214type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700215 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700216 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800217 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700218 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800219}
220
Colin Crossc99deeb2016-04-11 15:06:20 -0700221type dependencyTag struct {
222 blueprint.BaseDependencyTag
223 name string
224 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700225
226 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700227}
228
229var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700230 sharedDepTag = dependencyTag{name: "shared", library: true}
231 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
232 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
233 staticDepTag = dependencyTag{name: "static", library: true}
234 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
235 lateStaticDepTag = dependencyTag{name: "late static", library: true}
236 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800237 headerDepTag = dependencyTag{name: "header", library: true}
238 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700239 genSourceDepTag = dependencyTag{name: "gen source"}
240 genHeaderDepTag = dependencyTag{name: "gen header"}
241 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
242 objDepTag = dependencyTag{name: "obj"}
243 crtBeginDepTag = dependencyTag{name: "crtbegin"}
244 crtEndDepTag = dependencyTag{name: "crtend"}
245 reuseObjTag = dependencyTag{name: "reuse objects"}
246 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
247 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700248)
249
Colin Crossca860ac2016-01-04 14:34:37 -0800250// Module contains the properties and members used by all C/C++ module types, and implements
251// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
252// to construct the output file. Behavior can be customized with a Customizer interface
253type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700254 android.ModuleBase
255 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700256
Colin Crossca860ac2016-01-04 14:34:37 -0800257 Properties BaseProperties
258 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700259
Colin Crossca860ac2016-01-04 14:34:37 -0800260 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700261 hod android.HostOrDeviceSupported
262 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700263
Colin Crossca860ac2016-01-04 14:34:37 -0800264 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700265 features []feature
266 compiler compiler
267 linker linker
268 installer installer
269 stl *stl
270 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800271 coverage *coverage
Colin Cross16b23492016-01-06 14:41:07 -0800272
273 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700274
Colin Cross635c3b02016-05-18 15:37:25 -0700275 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800276
Colin Crossb98c8b02016-07-29 13:44:28 -0700277 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700278
279 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800280
281 // Flags used to compile this module
282 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700283}
284
Colin Crossca860ac2016-01-04 14:34:37 -0800285func (c *Module) Init() (blueprint.Module, []interface{}) {
286 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800287 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700288 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800289 }
290 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700291 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800292 }
293 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700294 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800295 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700296 if c.stl != nil {
297 props = append(props, c.stl.props()...)
298 }
Colin Cross16b23492016-01-06 14:41:07 -0800299 if c.sanitize != nil {
300 props = append(props, c.sanitize.props()...)
301 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800302 if c.coverage != nil {
303 props = append(props, c.coverage.props()...)
304 }
Colin Crossca860ac2016-01-04 14:34:37 -0800305 for _, feature := range c.features {
306 props = append(props, feature.props()...)
307 }
Colin Crossc472d572015-03-17 15:06:21 -0700308
Colin Cross635c3b02016-05-18 15:37:25 -0700309 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700310
Colin Cross635c3b02016-05-18 15:37:25 -0700311 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700312}
313
Colin Crossb916a382016-07-29 17:28:03 -0700314// Returns true for dependency roots (binaries)
315// TODO(ccross): also handle dlopenable libraries
316func (c *Module) isDependencyRoot() bool {
317 if root, ok := c.linker.(interface {
318 isDependencyRoot() bool
319 }); ok {
320 return root.isDependencyRoot()
321 }
322 return false
323}
324
Colin Crossca860ac2016-01-04 14:34:37 -0800325type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700326 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800327 moduleContextImpl
328}
329
Colin Cross37047f12016-12-13 17:06:13 -0800330type depsContext struct {
331 android.BottomUpMutatorContext
332 moduleContextImpl
333}
334
Colin Crossca860ac2016-01-04 14:34:37 -0800335type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700336 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800337 moduleContextImpl
338}
339
340type moduleContextImpl struct {
341 mod *Module
342 ctx BaseModuleContext
343}
344
Colin Crossca860ac2016-01-04 14:34:37 -0800345func (ctx *moduleContextImpl) clang() bool {
346 return ctx.mod.clang(ctx.ctx)
347}
348
Colin Crossb98c8b02016-07-29 13:44:28 -0700349func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800350 return ctx.mod.toolchain(ctx.ctx)
351}
352
353func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700354 if static, ok := ctx.mod.linker.(interface {
355 static() bool
356 }); ok {
357 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800358 }
Colin Crossb916a382016-07-29 17:28:03 -0700359 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800360}
361
362func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700363 if static, ok := ctx.mod.linker.(interface {
364 staticBinary() bool
365 }); ok {
366 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800367 }
Colin Crossb916a382016-07-29 17:28:03 -0700368 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800369}
370
371func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
372 return Bool(ctx.mod.Properties.No_default_compiler_flags)
373}
374
375func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700376 if ctx.ctx.Device() {
377 return ctx.mod.Properties.Sdk_version != ""
378 }
379 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800380}
381
382func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700383 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800384 if ctx.mod.Properties.Use_vndk {
385 return ctx.mod.Properties.Vndk_version
386 } else {
387 return ctx.mod.Properties.Sdk_version
388 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700389 }
390 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800391}
392
Dan Willemsend2ede872016-11-18 14:54:24 -0800393func (ctx *moduleContextImpl) vndk() bool {
394 if ctx.ctx.Device() {
395 return ctx.mod.Properties.Use_vndk
396 }
397 return false
398}
399
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700400func (ctx *moduleContextImpl) selectedStl() string {
401 if stl := ctx.mod.stl; stl != nil {
402 return stl.Properties.SelectedStl
403 }
404 return ""
405}
406
Colin Crossce75d2c2016-10-06 16:12:58 -0700407func (ctx *moduleContextImpl) baseModuleName() string {
408 return ctx.mod.ModuleBase.BaseModuleName()
409}
410
Jiyong Park4c48f722017-01-20 08:57:02 +0900411func (ctx *moduleContextImpl) isNdk() bool {
412 return inList(ctx.baseModuleName(), ndkPrebuiltSharedLibraries)
413}
414
415func (ctx *moduleContextImpl) isVndk() bool {
416 return config.IsVndkLibrary(ctx.baseModuleName())
417}
418
419func (ctx *moduleContextImpl) isSameProcessHal() bool {
420 return inList(ctx.baseModuleName(), ctx.ctx.DeviceConfig().SameProcessHalDeps())
421}
422
Colin Cross635c3b02016-05-18 15:37:25 -0700423func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800424 return &Module{
425 hod: hod,
426 multilib: multilib,
427 }
428}
429
Colin Cross635c3b02016-05-18 15:37:25 -0700430func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800431 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700432 module.features = []feature{
433 &tidyFeature{},
434 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700435 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800436 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800437 module.coverage = &coverage{}
Colin Crossca860ac2016-01-04 14:34:37 -0800438 return module
439}
440
Colin Crossce75d2c2016-10-06 16:12:58 -0700441func (c *Module) Prebuilt() *android.Prebuilt {
442 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
443 return p.prebuilt()
444 }
445 return nil
446}
447
448func (c *Module) Name() string {
449 name := c.ModuleBase.Name()
450 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
451 name = p.Name(name)
452 }
453 return name
454}
455
Colin Cross635c3b02016-05-18 15:37:25 -0700456func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800457 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700458 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800459 moduleContextImpl: moduleContextImpl{
460 mod: c,
461 },
462 }
463 ctx.ctx = ctx
464
465 flags := Flags{
466 Toolchain: c.toolchain(ctx),
467 Clang: c.clang(ctx),
468 }
Colin Crossca860ac2016-01-04 14:34:37 -0800469 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700470 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800471 }
472 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700473 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800474 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700475 if c.stl != nil {
476 flags = c.stl.flags(ctx, flags)
477 }
Colin Cross16b23492016-01-06 14:41:07 -0800478 if c.sanitize != nil {
479 flags = c.sanitize.flags(ctx, flags)
480 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800481 if c.coverage != nil {
482 flags = c.coverage.flags(ctx, flags)
483 }
Colin Crossca860ac2016-01-04 14:34:37 -0800484 for _, feature := range c.features {
485 flags = feature.flags(ctx, flags)
486 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800487 if ctx.Failed() {
488 return
489 }
490
Colin Crossb98c8b02016-07-29 13:44:28 -0700491 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
492 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
493 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800494
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800495 deps := c.depsToPaths(ctx)
496 if ctx.Failed() {
497 return
498 }
499 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
500 c.flags = flags
501
Colin Crossca860ac2016-01-04 14:34:37 -0800502 // Optimization to reduce size of build.ninja
503 // Replace the long list of flags for each file with a module-local variable
504 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
505 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
506 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
507 flags.CFlags = []string{"$cflags"}
508 flags.CppFlags = []string{"$cppflags"}
509 flags.AsFlags = []string{"$asflags"}
510
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700511 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800512 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700513 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800514 if ctx.Failed() {
515 return
516 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800517 }
518
Colin Crossca860ac2016-01-04 14:34:37 -0800519 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700520 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800521 if ctx.Failed() {
522 return
523 }
Colin Cross635c3b02016-05-18 15:37:25 -0700524 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700525 }
Colin Cross5049f022015-03-18 13:28:46 -0700526
Colin Crossce75d2c2016-10-06 16:12:58 -0700527 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
528 c.installer.install(ctx, c.outputFile.Path())
529 if ctx.Failed() {
530 return
Colin Crossca860ac2016-01-04 14:34:37 -0800531 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700532 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800533}
534
Colin Crossb98c8b02016-07-29 13:44:28 -0700535func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800536 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700537 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800538 }
Colin Crossca860ac2016-01-04 14:34:37 -0800539 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800540}
541
Colin Crossca860ac2016-01-04 14:34:37 -0800542func (c *Module) begin(ctx BaseModuleContext) {
543 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700544 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700545 }
Colin Crossca860ac2016-01-04 14:34:37 -0800546 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700547 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800548 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700549 if c.stl != nil {
550 c.stl.begin(ctx)
551 }
Colin Cross16b23492016-01-06 14:41:07 -0800552 if c.sanitize != nil {
553 c.sanitize.begin(ctx)
554 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800555 if c.coverage != nil {
556 c.coverage.begin(ctx)
557 }
Colin Crossca860ac2016-01-04 14:34:37 -0800558 for _, feature := range c.features {
559 feature.begin(ctx)
560 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700561 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800562 if ctx.vndk() {
563 ctx.PropertyErrorf("use_vndk",
564 "sdk_version and use_vndk cannot be used at the same time")
565 }
566
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700567 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
568 if err != nil {
569 ctx.PropertyErrorf("sdk_version", err.Error())
570 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800571 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800572 } else if ctx.vndk() {
573 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
574 if err != nil {
575 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
576 }
577 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700578 }
Colin Crossca860ac2016-01-04 14:34:37 -0800579}
580
Colin Cross37047f12016-12-13 17:06:13 -0800581func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700582 deps := Deps{}
583
584 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700585 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700586 }
587 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700588 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700589 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700590 if c.stl != nil {
591 deps = c.stl.deps(ctx, deps)
592 }
Colin Cross16b23492016-01-06 14:41:07 -0800593 if c.sanitize != nil {
594 deps = c.sanitize.deps(ctx, deps)
595 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800596 if c.coverage != nil {
597 deps = c.coverage.deps(ctx, deps)
598 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700599 for _, feature := range c.features {
600 deps = feature.deps(ctx, deps)
601 }
602
603 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
604 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
605 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
606 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
607 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800608 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700609
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700610 for _, lib := range deps.ReexportSharedLibHeaders {
611 if !inList(lib, deps.SharedLibs) {
612 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
613 }
614 }
615
616 for _, lib := range deps.ReexportStaticLibHeaders {
617 if !inList(lib, deps.StaticLibs) {
618 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
619 }
620 }
621
Colin Cross5950f382016-12-13 12:50:57 -0800622 for _, lib := range deps.ReexportHeaderLibHeaders {
623 if !inList(lib, deps.HeaderLibs) {
624 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
625 }
626 }
627
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700628 for _, gen := range deps.ReexportGeneratedHeaders {
629 if !inList(gen, deps.GeneratedHeaders) {
630 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
631 }
632 }
633
Colin Crossc99deeb2016-04-11 15:06:20 -0700634 return deps
635}
636
Dan Albert7e9d2952016-08-04 13:02:36 -0700637func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800638 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700639 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800640 moduleContextImpl: moduleContextImpl{
641 mod: c,
642 },
643 }
644 ctx.ctx = ctx
645
Colin Crossca860ac2016-01-04 14:34:37 -0800646 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700647}
648
Colin Cross1e676be2016-10-12 14:38:15 -0700649func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
650 if !c.Enabled() {
651 return
652 }
653
Colin Cross37047f12016-12-13 17:06:13 -0800654 ctx := &depsContext{
655 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700656 moduleContextImpl: moduleContextImpl{
657 mod: c,
658 },
659 }
660 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800661
Colin Crossc99deeb2016-04-11 15:06:20 -0700662 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800663
Colin Crossb5bc4b42016-07-11 16:11:59 -0700664 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
665 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700666
Dan Albert914449f2016-06-17 16:45:24 -0700667 variantNdkLibs := []string{}
668 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800669 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700670 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700671
Dan Albert914449f2016-06-17 16:45:24 -0700672 // Rewrites the names of shared libraries into the names of the NDK
673 // libraries where appropriate. This returns two slices.
674 //
675 // The first is a list of non-variant shared libraries (either rewritten
676 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
677 // because they are not NDK libraries).
678 //
679 // The second is a list of ndk_library modules. These need to be
680 // separated because they are a variation dependency and must be added
681 // in a different manner.
682 rewriteNdkLibs := func(list []string) ([]string, []string) {
683 variantLibs := []string{}
684 nonvariantLibs := []string{}
685 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700686 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700687 if !inList(entry, ndkMigratedLibs) {
688 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
689 } else {
690 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
691 }
692 } else {
693 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700694 }
695 }
Dan Albert914449f2016-06-17 16:45:24 -0700696 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700697 }
698
Dan Albert914449f2016-06-17 16:45:24 -0700699 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
700 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700701 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700702
Colin Cross32ec36c2016-12-15 07:39:51 -0800703 for _, lib := range deps.HeaderLibs {
704 depTag := headerDepTag
705 if inList(lib, deps.ReexportHeaderLibHeaders) {
706 depTag = headerExportDepTag
707 }
708 actx.AddVariationDependencies(nil, depTag, lib)
709 }
Colin Cross5950f382016-12-13 12:50:57 -0800710
Colin Crossc99deeb2016-04-11 15:06:20 -0700711 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
712 deps.WholeStaticLibs...)
713
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700714 for _, lib := range deps.StaticLibs {
715 depTag := staticDepTag
716 if inList(lib, deps.ReexportStaticLibHeaders) {
717 depTag = staticExportDepTag
718 }
Colin Cross15a0d462016-07-14 14:49:58 -0700719 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700720 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700721
722 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
723 deps.LateStaticLibs...)
724
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700725 for _, lib := range deps.SharedLibs {
726 depTag := sharedDepTag
727 if inList(lib, deps.ReexportSharedLibHeaders) {
728 depTag = sharedExportDepTag
729 }
Colin Cross15a0d462016-07-14 14:49:58 -0700730 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700731 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700732
733 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
734 deps.LateSharedLibs...)
735
Colin Cross68861832016-07-08 10:41:41 -0700736 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700737
738 for _, gen := range deps.GeneratedHeaders {
739 depTag := genHeaderDepTag
740 if inList(gen, deps.ReexportGeneratedHeaders) {
741 depTag = genHeaderExportDepTag
742 }
743 actx.AddDependency(c, depTag, gen)
744 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700745
Colin Cross68861832016-07-08 10:41:41 -0700746 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700747
748 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700749 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800750 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700751 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700752 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700753 }
Dan Albert914449f2016-06-17 16:45:24 -0700754
755 version := ctx.sdkVersion()
756 actx.AddVariationDependencies([]blueprint.Variation{
757 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
758 actx.AddVariationDependencies([]blueprint.Variation{
759 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700760}
Colin Cross21b9a242015-03-24 14:15:58 -0700761
Dan Albert7e9d2952016-08-04 13:02:36 -0700762func beginMutator(ctx android.BottomUpMutatorContext) {
763 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
764 c.beginMutator(ctx)
765 }
766}
767
Colin Crossca860ac2016-01-04 14:34:37 -0800768func (c *Module) clang(ctx BaseModuleContext) bool {
769 clang := Bool(c.Properties.Clang)
770
771 if c.Properties.Clang == nil {
772 if ctx.Host() {
773 clang = true
774 }
775
776 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
777 clang = true
778 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800779 }
Colin Cross28344522015-04-22 13:07:53 -0700780
Colin Crossca860ac2016-01-04 14:34:37 -0800781 if !c.toolchain(ctx).ClangSupported() {
782 clang = false
783 }
784
785 return clang
786}
787
Colin Crossc99deeb2016-04-11 15:06:20 -0700788// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700789func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800790 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800791
Dan Willemsena96ff642016-06-07 12:34:45 -0700792 // Whether a module can link to another module, taking into
793 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700794 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700795 if from.Target().Os != android.Android {
796 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700797 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700798 }
799 if from.Properties.Sdk_version == "" {
800 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700801 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700802 }
Colin Crossb916a382016-07-29 17:28:03 -0700803 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700804 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700805 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700806 }
807 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
808 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700809 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700810 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700811 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
812 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700813 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700814 }
Colin Crossb916a382016-07-29 17:28:03 -0700815 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700816 // These aren't real libraries, but are the stub shared libraries that are included in
817 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700818 return
Dan Albert914449f2016-06-17 16:45:24 -0700819 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700820 if to.Properties.Sdk_version == "" {
821 // NDK code linking to platform code is never okay.
822 ctx.ModuleErrorf("depends on non-NDK-built library %q",
823 ctx.OtherModuleName(to))
824 }
825
826 // All this point we know we have two NDK libraries, but we need to
827 // check that we're not linking against anything built against a higher
828 // API level, as it is only valid to link against older or equivalent
829 // APIs.
830
831 if from.Properties.Sdk_version == "current" {
832 // Current can link against anything.
833 return
834 } else if to.Properties.Sdk_version == "current" {
835 // Current can't be linked against by anything else.
836 ctx.ModuleErrorf("links %q built against newer API version %q",
837 ctx.OtherModuleName(to), "current")
838 }
839
840 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
841 if err != nil {
842 ctx.PropertyErrorf("sdk_version",
843 "Invalid sdk_version value (must be int): %q",
844 from.Properties.Sdk_version)
845 }
846 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
847 if err != nil {
848 ctx.PropertyErrorf("sdk_version",
849 "Invalid sdk_version value (must be int): %q",
850 to.Properties.Sdk_version)
851 }
852
853 if toApi > fromApi {
854 ctx.ModuleErrorf("links %q built against newer API version %q",
855 ctx.OtherModuleName(to), to.Properties.Sdk_version)
856 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700857 }
858
Colin Crossc99deeb2016-04-11 15:06:20 -0700859 ctx.VisitDirectDeps(func(m blueprint.Module) {
860 name := ctx.OtherModuleName(m)
861 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800862
Colin Cross635c3b02016-05-18 15:37:25 -0700863 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700864 if a == nil {
865 ctx.ModuleErrorf("module %q not an android module", name)
866 return
Colin Crossca860ac2016-01-04 14:34:37 -0800867 }
Colin Crossca860ac2016-01-04 14:34:37 -0800868
Dan Willemsena96ff642016-06-07 12:34:45 -0700869 cc, _ := m.(*Module)
870 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700871 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800872 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700873 case genSourceDepTag:
874 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
875 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
876 genRule.GeneratedSourceFiles()...)
877 } else {
878 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
879 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700880 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700881 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
882 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
883 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800884 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700885 depPaths.Flags = append(depPaths.Flags, flags)
886 if tag == genHeaderExportDepTag {
887 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700888 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
889 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700890 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700891 } else {
892 ctx.ModuleErrorf("module %q is not a genrule", name)
893 }
894 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700895 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800896 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700897 return
898 }
899
900 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800901 if ctx.AConfig().AllowMissingDependencies() {
902 ctx.AddMissingDependencies([]string{name})
903 } else {
904 ctx.ModuleErrorf("depends on disabled module %q", name)
905 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700906 return
907 }
908
Colin Crossa1ad8d12016-06-01 17:09:44 -0700909 if a.Target().Os != ctx.Os() {
910 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
911 return
912 }
913
914 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
915 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700916 return
917 }
918
Colin Crossc99deeb2016-04-11 15:06:20 -0700919 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800920 if l, ok := cc.compiler.(libraryInterface); ok {
921 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
922 return
923 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700924 }
925
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700926 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700927 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700928 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700929 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700930 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700931 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700932
933 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700934 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700935 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700936 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700937 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700938
Dan Albert9e10cd42016-08-03 14:12:14 -0700939 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700940 }
941
Colin Cross26c34ed2016-09-30 17:10:16 -0700942 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700943 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700944
Colin Cross26c34ed2016-09-30 17:10:16 -0700945 linkFile := cc.outputFile
946 depFile := android.OptionalPath{}
947
Colin Crossc99deeb2016-04-11 15:06:20 -0700948 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700949 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700950 ptr = &depPaths.SharedLibs
951 depPtr = &depPaths.SharedLibsDeps
952 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700953 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700954 ptr = &depPaths.LateSharedLibs
955 depPtr = &depPaths.LateSharedLibsDeps
956 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700957 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700958 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700959 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700960 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700961 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700962 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700963 staticLib, ok := cc.linker.(libraryInterface)
964 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700965 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700966 return
967 }
968
969 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
970 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
971 for i := range missingDeps {
972 missingDeps[i] += postfix
973 }
974 ctx.AddMissingDependencies(missingDeps)
975 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700976 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800977 case headerDepTag:
978 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700979 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700980 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700981 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700982 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700983 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700984 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700985 }
986
Dan Willemsen581341d2017-02-09 16:16:31 -0800987 switch tag {
988 case staticDepTag, staticExportDepTag, lateStaticDepTag:
989 staticLib, ok := cc.linker.(libraryInterface)
990 if !ok || !staticLib.static() {
991 ctx.ModuleErrorf("module %q not a static library", name)
992 return
993 }
994
995 // When combining coverage files for shared libraries and executables, coverage files
996 // in static libraries act as if they were whole static libraries.
997 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
998 staticLib.objs().coverageFiles...)
999 }
1000
Colin Cross26c34ed2016-09-30 17:10:16 -07001001 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001002 if !linkFile.Valid() {
1003 ctx.ModuleErrorf("module %q missing output file", name)
1004 return
1005 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001006 *ptr = append(*ptr, linkFile.Path())
1007 }
1008
Colin Crossc99deeb2016-04-11 15:06:20 -07001009 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001010 dep := depFile
1011 if !dep.Valid() {
1012 dep = linkFile
1013 }
1014 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001015 }
1016 })
1017
1018 return depPaths
1019}
1020
1021func (c *Module) InstallInData() bool {
1022 if c.installer == nil {
1023 return false
1024 }
Colin Cross94610402016-08-29 13:41:32 -07001025 if c.sanitize != nil && c.sanitize.inData() {
1026 return true
1027 }
Colin Crossca860ac2016-01-04 14:34:37 -08001028 return c.installer.inData()
1029}
1030
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001031func (c *Module) HostToolPath() android.OptionalPath {
1032 if c.installer == nil {
1033 return android.OptionalPath{}
1034 }
1035 return c.installer.hostToolPath()
1036}
1037
Colin Cross2ba19d92015-05-07 15:44:20 -07001038//
Colin Crosscfad1192015-11-02 16:43:11 -08001039// Defaults
1040//
Colin Crossca860ac2016-01-04 14:34:37 -08001041type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001042 android.ModuleBase
1043 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001044}
1045
Colin Cross635c3b02016-05-18 15:37:25 -07001046func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001047}
1048
Colin Cross1e676be2016-10-12 14:38:15 -07001049func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1050}
1051
Colin Crossca860ac2016-01-04 14:34:37 -08001052func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001053 return DefaultsFactory()
1054}
1055
1056func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001057 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001058
Colin Crosse1d764e2016-08-18 14:18:32 -07001059 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001060 &BaseProperties{},
1061 &BaseCompilerProperties{},
1062 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001063 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001064 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001065 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001066 &TestProperties{},
1067 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001068 &UnusedProperties{},
1069 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001070 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001071 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001072 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001073 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001074 &CoverageProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001075 )
Colin Crosscfad1192015-11-02 16:43:11 -08001076
Colin Crosse1d764e2016-08-18 14:18:32 -07001077 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001078}
1079
Colin Cross74d1ec02015-04-28 13:30:13 -07001080// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1081// modifies the slice contents in place, and returns a subslice of the original slice
1082func lastUniqueElements(list []string) []string {
1083 totalSkip := 0
1084 for i := len(list) - 1; i >= totalSkip; i-- {
1085 skip := 0
1086 for j := i - 1; j >= totalSkip; j-- {
1087 if list[i] == list[j] {
1088 skip++
1089 } else {
1090 list[j+skip] = list[j]
1091 }
1092 }
1093 totalSkip += skip
1094 }
1095 return list[totalSkip:]
1096}
Colin Cross06a931b2015-10-28 17:23:31 -07001097
1098var Bool = proptools.Bool